Implement changes from Chris's feedback.
[oota-llvm.git] / lib / Target / IA64 / IA64ISelDAGToDAG.cpp
1 //===---- IA64ISelDAGToDAG.cpp - IA64 pattern matching inst selector ------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a pattern matching instruction selector for IA64,
11 // converting a legalized dag to an IA64 dag.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "ia64-codegen"
16 #include "IA64.h"
17 #include "IA64TargetMachine.h"
18 #include "IA64ISelLowering.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/SelectionDAG.h"
22 #include "llvm/CodeGen/SelectionDAGISel.h"
23 #include "llvm/Target/TargetOptions.h"
24 #include "llvm/Constants.h"
25 #include "llvm/GlobalValue.h"
26 #include "llvm/Intrinsics.h"
27 #include "llvm/Support/Compiler.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/MathExtras.h"
31 #include "llvm/Support/raw_ostream.h"
32 using namespace llvm;
33
34 namespace {
35   //===--------------------------------------------------------------------===//
36   /// IA64DAGToDAGISel - IA64 specific code to select IA64 machine
37   /// instructions for SelectionDAG operations.
38   ///
39   class IA64DAGToDAGISel : public SelectionDAGISel {
40     unsigned GlobalBaseReg;
41   public:
42     explicit IA64DAGToDAGISel(IA64TargetMachine &TM)
43       : SelectionDAGISel(TM) {}
44     
45     virtual bool runOnFunction(Function &Fn) {
46       // Make sure we re-emit a set of the global base reg if necessary
47       GlobalBaseReg = 0;
48       return SelectionDAGISel::runOnFunction(Fn);
49     }
50  
51     /// getI64Imm - Return a target constant with the specified value, of type
52     /// i64.
53     inline SDValue getI64Imm(uint64_t Imm) {
54       return CurDAG->getTargetConstant(Imm, MVT::i64);
55     }
56
57     /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
58     /// base register.  Return the virtual register that holds this value.
59     // SDValue getGlobalBaseReg(); TODO: hmm
60     
61     // Select - Convert the specified operand from a target-independent to a
62     // target-specific node if it hasn't already been changed.
63     SDNode *Select(SDValue N);
64     
65     SDNode *SelectIntImmediateExpr(SDValue LHS, SDValue RHS,
66                                    unsigned OCHi, unsigned OCLo,
67                                    bool IsArithmetic = false,
68                                    bool Negate = false);
69     SDNode *SelectBitfieldInsert(SDNode *N);
70
71     /// SelectCC - Select a comparison of the specified values with the
72     /// specified condition code, returning the CR# of the expression.
73     SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC);
74
75     /// SelectAddr - Given the specified address, return the two operands for a
76     /// load/store instruction, and return true if it should be an indexed [r+r]
77     /// operation.
78     bool SelectAddr(SDValue Addr, SDValue &Op1, SDValue &Op2);
79
80     /// InstructionSelect - This callback is invoked by
81     /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
82     virtual void InstructionSelect();
83     
84     virtual const char *getPassName() const {
85       return "IA64 (Itanium) DAG->DAG Instruction Selector";
86     } 
87
88 // Include the pieces autogenerated from the target description.
89 #include "IA64GenDAGISel.inc"
90     
91 private:
92     SDNode *SelectDIV(SDValue Op);
93   };
94 }
95
96 /// InstructionSelect - This callback is invoked by
97 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
98 void IA64DAGToDAGISel::InstructionSelect() {
99   DEBUG(BB->dump());
100
101   // Select target instructions for the DAG.
102   SelectRoot(*CurDAG);
103   CurDAG->RemoveDeadNodes();
104 }
105
106 SDNode *IA64DAGToDAGISel::SelectDIV(SDValue Op) {
107   SDNode *N = Op.getNode();
108   SDValue Chain = N->getOperand(0);
109   SDValue Tmp1 = N->getOperand(0);
110   SDValue Tmp2 = N->getOperand(1);
111   DebugLoc dl = N->getDebugLoc();
112
113   bool isFP=false;
114
115   if(Tmp1.getValueType().isFloatingPoint())
116     isFP=true;
117     
118   bool isModulus=false; // is it a division or a modulus?
119   bool isSigned=false;
120
121   switch(N->getOpcode()) {
122     case ISD::FDIV:
123     case ISD::SDIV:  isModulus=false; isSigned=true;  break;
124     case ISD::UDIV:  isModulus=false; isSigned=false; break;
125     case ISD::FREM:
126     case ISD::SREM:  isModulus=true;  isSigned=true;  break;
127     case ISD::UREM:  isModulus=true;  isSigned=false; break;
128   }
129
130   // TODO: check for integer divides by powers of 2 (or other simple patterns?)
131
132     SDValue TmpPR, TmpPR2;
133     SDValue TmpF1, TmpF2, TmpF3, TmpF4, TmpF5, TmpF6, TmpF7, TmpF8;
134     SDValue TmpF9, TmpF10,TmpF11,TmpF12,TmpF13,TmpF14,TmpF15;
135     SDNode *Result;
136
137     // we'll need copies of F0 and F1
138     SDValue F0 = CurDAG->getRegister(IA64::F0, MVT::f64);
139     SDValue F1 = CurDAG->getRegister(IA64::F1, MVT::f64);
140     
141     // OK, emit some code:
142
143     if(!isFP) {
144       // first, load the inputs into FP regs.
145       TmpF1 =
146         SDValue(CurDAG->getTargetNode(IA64::SETFSIG, dl, MVT::f64, Tmp1), 0);
147       Chain = TmpF1.getValue(1);
148       TmpF2 =
149         SDValue(CurDAG->getTargetNode(IA64::SETFSIG, dl, MVT::f64, Tmp2), 0);
150       Chain = TmpF2.getValue(1);
151       
152       // next, convert the inputs to FP
153       if(isSigned) {
154         TmpF3 =
155           SDValue(CurDAG->getTargetNode(IA64::FCVTXF, dl, MVT::f64, TmpF1), 0);
156         Chain = TmpF3.getValue(1);
157         TmpF4 =
158           SDValue(CurDAG->getTargetNode(IA64::FCVTXF, dl, MVT::f64, TmpF2), 0);
159         Chain = TmpF4.getValue(1);
160       } else { // is unsigned
161         TmpF3 =
162           SDValue(CurDAG->getTargetNode(IA64::FCVTXUFS1, dl, MVT::f64, TmpF1), 
163                   0);
164         Chain = TmpF3.getValue(1);
165         TmpF4 =
166           SDValue(CurDAG->getTargetNode(IA64::FCVTXUFS1, dl, MVT::f64, TmpF2), 
167                   0);
168         Chain = TmpF4.getValue(1);
169       }
170
171     } else { // this is an FP divide/remainder, so we 'leak' some temp
172              // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
173       TmpF3=Tmp1;
174       TmpF4=Tmp2;
175     }
176
177     // we start by computing an approximate reciprocal (good to 9 bits?)
178     // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
179     if(isFP)
180       TmpF5 = SDValue(CurDAG->getTargetNode(IA64::FRCPAS0, dl, MVT::f64,
181                                             MVT::i1, TmpF3, TmpF4), 0);
182     else
183       TmpF5 = SDValue(CurDAG->getTargetNode(IA64::FRCPAS1, dl, MVT::f64,
184                                             MVT::i1, TmpF3, TmpF4), 0);
185                                   
186     TmpPR = TmpF5.getValue(1);
187     Chain = TmpF5.getValue(2);
188
189     SDValue minusB;
190     if(isModulus) { // for remainders, it'll be handy to have
191                              // copies of -input_b
192       minusB = SDValue(CurDAG->getTargetNode(IA64::SUB, dl, MVT::i64,
193                   CurDAG->getRegister(IA64::r0, MVT::i64), Tmp2), 0);
194       Chain = minusB.getValue(1);
195     }
196     
197     SDValue TmpE0, TmpY1, TmpE1, TmpY2;
198
199     SDValue OpsE0[] = { TmpF4, TmpF5, F1, TmpPR };
200     TmpE0 = SDValue(CurDAG->getTargetNode(IA64::CFNMAS1, dl, MVT::f64,
201                                             OpsE0, 4), 0);
202     Chain = TmpE0.getValue(1);
203     SDValue OpsY1[] = { TmpF5, TmpE0, TmpF5, TmpPR };
204     TmpY1 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, dl, MVT::f64,
205                                             OpsY1, 4), 0);
206     Chain = TmpY1.getValue(1);
207     SDValue OpsE1[] = { TmpE0, TmpE0, F0, TmpPR };
208     TmpE1 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, dl, MVT::f64,
209                                             OpsE1, 4), 0);
210     Chain = TmpE1.getValue(1);
211     SDValue OpsY2[] = { TmpY1, TmpE1, TmpY1, TmpPR };
212     TmpY2 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, dl, MVT::f64,
213                                             OpsY2, 4), 0);
214     Chain = TmpY2.getValue(1);
215     
216     if(isFP) { // if this is an FP divide, we finish up here and exit early
217       if(isModulus)
218         assert(0 && "Sorry, try another FORTRAN compiler.");
219  
220       SDValue TmpE2, TmpY3, TmpQ0, TmpR0;
221
222       SDValue OpsE2[] = { TmpE1, TmpE1, F0, TmpPR };
223       TmpE2 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, dl, MVT::f64,
224                                               OpsE2, 4), 0);
225       Chain = TmpE2.getValue(1);
226       SDValue OpsY3[] = { TmpY2, TmpE2, TmpY2, TmpPR };
227       TmpY3 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, dl, MVT::f64,
228                                               OpsY3, 4), 0);
229       Chain = TmpY3.getValue(1);
230       SDValue OpsQ0[] = { Tmp1, TmpY3, F0, TmpPR };
231       TmpQ0 =
232         SDValue(CurDAG->getTargetNode(IA64::CFMADS1, dl,  // double prec!
233                                       MVT::f64, OpsQ0, 4), 0);
234       Chain = TmpQ0.getValue(1);
235       SDValue OpsR0[] = { Tmp2, TmpQ0, Tmp1, TmpPR };
236       TmpR0 =
237         SDValue(CurDAG->getTargetNode(IA64::CFNMADS1, dl, // double prec!
238                                       MVT::f64, OpsR0, 4), 0);
239       Chain = TmpR0.getValue(1);
240
241 // we want Result to have the same target register as the frcpa, so
242 // we two-address hack it. See the comment "for this to work..." on
243 // page 48 of Intel application note #245415
244       SDValue Ops[] = { TmpF5, TmpY3, TmpR0, TmpQ0, TmpPR };
245       Result = CurDAG->getTargetNode(IA64::TCFMADS0, dl, // d.p. s0 rndg!
246                                      MVT::f64, Ops, 5);
247       Chain = SDValue(Result, 1);
248       return Result; // XXX: early exit!
249     } else { // this is *not* an FP divide, so there's a bit left to do:
250     
251       SDValue TmpQ2, TmpR2, TmpQ3, TmpQ;
252
253       SDValue OpsQ2[] = { TmpF3, TmpY2, F0, TmpPR };
254       TmpQ2 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, dl, MVT::f64,
255                                               OpsQ2, 4), 0);
256       Chain = TmpQ2.getValue(1);
257       SDValue OpsR2[] = { TmpF4, TmpQ2, TmpF3, TmpPR };
258       TmpR2 = SDValue(CurDAG->getTargetNode(IA64::CFNMAS1, dl, MVT::f64,
259                                               OpsR2, 4), 0);
260       Chain = TmpR2.getValue(1);
261       
262 // we want TmpQ3 to have the same target register as the frcpa? maybe we
263 // should two-address hack it. See the comment "for this to work..." on page
264 // 48 of Intel application note #245415
265       SDValue OpsQ3[] = { TmpF5, TmpR2, TmpY2, TmpQ2, TmpPR };
266       TmpQ3 = SDValue(CurDAG->getTargetNode(IA64::TCFMAS1, dl, MVT::f64,
267                                          OpsQ3, 5), 0);
268       Chain = TmpQ3.getValue(1);
269
270       // STORY: without these two-address instructions (TCFMAS1 and TCFMADS0)
271       // the FPSWA won't be able to help out in the case of large/tiny
272       // arguments. Other fun bugs may also appear, e.g. 0/x = x, not 0.
273       
274       if(isSigned)
275         TmpQ = SDValue(CurDAG->getTargetNode(IA64::FCVTFXTRUNCS1, dl,
276                                                MVT::f64, TmpQ3), 0);
277       else
278         TmpQ = SDValue(CurDAG->getTargetNode(IA64::FCVTFXUTRUNCS1, dl,
279                                                MVT::f64, TmpQ3), 0);
280       
281       Chain = TmpQ.getValue(1);
282
283       if(isModulus) {
284         SDValue FPminusB =
285           SDValue(CurDAG->getTargetNode(IA64::SETFSIG, dl, MVT::f64, minusB),
286                   0);
287         Chain = FPminusB.getValue(1);
288         SDValue Remainder =
289           SDValue(CurDAG->getTargetNode(IA64::XMAL, dl, MVT::f64,
290                                           TmpQ, FPminusB, TmpF1), 0);
291         Chain = Remainder.getValue(1);
292         Result = CurDAG->getTargetNode(IA64::GETFSIG, dl, MVT::i64, Remainder);
293         Chain = SDValue(Result, 1);
294       } else { // just an integer divide
295         Result = CurDAG->getTargetNode(IA64::GETFSIG, dl, MVT::i64, TmpQ);
296         Chain = SDValue(Result, 1);
297       }
298
299       return Result;
300     } // wasn't an FP divide
301 }
302
303 // Select - Convert the specified operand from a target-independent to a
304 // target-specific node if it hasn't already been changed.
305 SDNode *IA64DAGToDAGISel::Select(SDValue Op) {
306   SDNode *N = Op.getNode();
307   if (N->isMachineOpcode())
308     return NULL;   // Already selected.
309   DebugLoc dl = Op.getDebugLoc();
310
311   switch (N->getOpcode()) {
312   default: break;
313
314   case IA64ISD::BRCALL: { // XXX: this is also a hack!
315     SDValue Chain = N->getOperand(0);
316     SDValue InFlag;  // Null incoming flag value.
317
318     if(N->getNumOperands()==3) { // we have an incoming chain, callee and flag
319       InFlag = N->getOperand(2);
320     }
321
322     unsigned CallOpcode;
323     SDValue CallOperand;
324     
325     // if we can call directly, do so
326     if (GlobalAddressSDNode *GASD =
327       dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
328       CallOpcode = IA64::BRCALL_IPREL_GA;
329       CallOperand = CurDAG->getTargetGlobalAddress(GASD->getGlobal(), MVT::i64);
330     } else if (isa<ExternalSymbolSDNode>(N->getOperand(1))) {
331       // FIXME: we currently NEED this case for correctness, to avoid
332       // "non-pic code with imm reloc.n against dynamic symbol" errors
333     CallOpcode = IA64::BRCALL_IPREL_ES;
334     CallOperand = N->getOperand(1);
335   } else {
336     // otherwise we need to load the function descriptor,
337     // load the branch target (function)'s entry point and GP,
338     // branch (call) then restore the GP
339     SDValue FnDescriptor = N->getOperand(1);
340    
341     // load the branch target's entry point [mem] and 
342     // GP value [mem+8]
343     SDValue targetEntryPoint=
344       SDValue(CurDAG->getTargetNode(IA64::LD8, dl, MVT::i64, MVT::Other,
345                                       FnDescriptor, CurDAG->getEntryNode()), 0);
346     Chain = targetEntryPoint.getValue(1);
347     SDValue targetGPAddr=
348       SDValue(CurDAG->getTargetNode(IA64::ADDS, dl, MVT::i64, 
349                                       FnDescriptor,
350                                       CurDAG->getConstant(8, MVT::i64)), 0);
351     Chain = targetGPAddr.getValue(1);
352     SDValue targetGP =
353       SDValue(CurDAG->getTargetNode(IA64::LD8, dl, MVT::i64,MVT::Other,
354                                       targetGPAddr, CurDAG->getEntryNode()), 0);
355     Chain = targetGP.getValue(1);
356
357     Chain = CurDAG->getCopyToReg(Chain, dl, IA64::r1, targetGP, InFlag);
358     InFlag = Chain.getValue(1);
359     Chain = CurDAG->getCopyToReg(Chain, dl, IA64::B6,
360                                  targetEntryPoint, InFlag); // FLAG these?
361     InFlag = Chain.getValue(1);
362     
363     CallOperand = CurDAG->getRegister(IA64::B6, MVT::i64);
364     CallOpcode = IA64::BRCALL_INDIRECT;
365   }
366  
367    // Finally, once everything is setup, emit the call itself
368    if (InFlag.getNode())
369      Chain = SDValue(CurDAG->getTargetNode(CallOpcode, dl, MVT::Other,
370                                            MVT::Flag, CallOperand, InFlag), 0);
371    else // there might be no arguments
372      Chain = SDValue(CurDAG->getTargetNode(CallOpcode, dl, MVT::Other,
373                                            MVT::Flag, CallOperand, Chain), 0);
374    InFlag = Chain.getValue(1);
375
376    std::vector<SDValue> CallResults;
377
378    CallResults.push_back(Chain);
379    CallResults.push_back(InFlag);
380
381    for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
382      ReplaceUses(Op.getValue(i), CallResults[i]);
383    return NULL;
384   }
385   
386   case IA64ISD::GETFD: {
387     SDValue Input = N->getOperand(0);
388     return CurDAG->getTargetNode(IA64::GETFD, dl, MVT::i64, Input);
389   } 
390   
391   case ISD::FDIV:
392   case ISD::SDIV:
393   case ISD::UDIV:
394   case ISD::SREM:
395   case ISD::UREM:
396     return SelectDIV(Op);
397  
398   case ISD::TargetConstantFP: {
399     SDValue Chain = CurDAG->getEntryNode(); // this is a constant, so..
400
401     SDValue V;
402     ConstantFPSDNode* N2 = cast<ConstantFPSDNode>(N);
403     if (N2->getValueAPF().isPosZero()) {
404       V = CurDAG->getCopyFromReg(Chain, dl, IA64::F0, MVT::f64);
405     } else if (N2->isExactlyValue(N2->getValueType(0) == MVT::f32 ? 
406                                   APFloat(+1.0f) : APFloat(+1.0))) {
407       V = CurDAG->getCopyFromReg(Chain, dl, IA64::F1, MVT::f64);
408     } else
409       assert(0 && "Unexpected FP constant!");
410     
411     ReplaceUses(SDValue(N, 0), V);
412     return 0;
413   }
414
415   case ISD::FrameIndex: { // TODO: reduce creepyness
416     int FI = cast<FrameIndexSDNode>(N)->getIndex();
417     if (N->hasOneUse())
418       return CurDAG->SelectNodeTo(N, IA64::MOV, MVT::i64,
419                                   CurDAG->getTargetFrameIndex(FI, MVT::i64));
420     else
421       return CurDAG->getTargetNode(IA64::MOV, dl, MVT::i64,
422                                    CurDAG->getTargetFrameIndex(FI, MVT::i64));
423   }
424
425   case ISD::ConstantPool: { // TODO: nuke the constant pool
426     // (ia64 doesn't need one)
427     ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
428     Constant *C = CP->getConstVal();
429     SDValue CPI = CurDAG->getTargetConstantPool(C, MVT::i64,
430                                                   CP->getAlignment());
431     return CurDAG->getTargetNode(IA64::ADDL_GA, dl, MVT::i64, // ?
432                                  CurDAG->getRegister(IA64::r1, MVT::i64), CPI);
433   }
434
435   case ISD::GlobalAddress: {
436     GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
437     SDValue GA = CurDAG->getTargetGlobalAddress(GV, MVT::i64);
438     SDValue Tmp =
439       SDValue(CurDAG->getTargetNode(IA64::ADDL_GA, dl, MVT::i64, 
440                                       CurDAG->getRegister(IA64::r1,
441                                                           MVT::i64), GA), 0);
442     return CurDAG->getTargetNode(IA64::LD8, dl, MVT::i64, MVT::Other, Tmp,
443                                  CurDAG->getEntryNode());
444   }
445   
446 /* XXX
447    case ISD::ExternalSymbol: {
448      SDValue EA = CurDAG->getTargetExternalSymbol(
449        cast<ExternalSymbolSDNode>(N)->getSymbol(),
450        MVT::i64);
451      SDValue Tmp = CurDAG->getTargetNode(IA64::ADDL_EA, dl, MVT::i64, 
452                                            CurDAG->getRegister(IA64::r1,
453                                                                MVT::i64),
454                                            EA);
455      return CurDAG->getTargetNode(IA64::LD8, dl, MVT::i64, Tmp);
456    }
457 */
458
459   case ISD::LOAD: { // FIXME: load -1, not 1, for bools?
460     LoadSDNode *LD = cast<LoadSDNode>(N);
461     SDValue Chain = LD->getChain();
462     SDValue Address = LD->getBasePtr();
463
464     MVT TypeBeingLoaded = LD->getMemoryVT();
465     unsigned Opc;
466     switch (TypeBeingLoaded.getSimpleVT()) {
467     default:
468 #ifndef NDEBUG
469       N->dump(CurDAG);
470 #endif
471       assert(0 && "Cannot load this type!");
472     case MVT::i1: { // this is a bool
473       Opc = IA64::LD1; // first we load a byte, then compare for != 0
474       if(N->getValueType(0) == MVT::i1) { // XXX: early exit!
475         return CurDAG->SelectNodeTo(N, IA64::CMPNE, MVT::i1, MVT::Other,
476                                     SDValue(CurDAG->getTargetNode(Opc, dl,
477                                                                   MVT::i64,
478                                                                   Address), 0),
479                                     CurDAG->getRegister(IA64::r0, MVT::i64),
480                                     Chain);
481       }
482       /* otherwise, we want to load a bool into something bigger: LD1
483          will do that for us, so we just fall through */
484     }
485     case MVT::i8:  Opc = IA64::LD1; break;
486     case MVT::i16: Opc = IA64::LD2; break;
487     case MVT::i32: Opc = IA64::LD4; break;
488     case MVT::i64: Opc = IA64::LD8; break;
489     
490     case MVT::f32: Opc = IA64::LDF4; break;
491     case MVT::f64: Opc = IA64::LDF8; break;
492     }
493
494     // TODO: comment this
495     return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
496                                 Address, Chain);
497   }
498   
499   case ISD::STORE: {
500     StoreSDNode *ST = cast<StoreSDNode>(N);
501     SDValue Address = ST->getBasePtr();
502     SDValue Chain = ST->getChain();
503    
504     unsigned Opc;
505     if (ISD::isNON_TRUNCStore(N)) {
506       switch (N->getOperand(1).getValueType().getSimpleVT()) {
507       default: assert(0 && "unknown type in store");
508       case MVT::i1: { // this is a bool
509         Opc = IA64::ST1; // we store either 0 or 1 as a byte 
510         // first load zero!
511         SDValue Initial = CurDAG->getCopyFromReg(Chain, dl, IA64::r0, MVT::i64);
512         Chain = Initial.getValue(1);
513         // then load 1 into the same reg iff the predicate to store is 1
514         SDValue Tmp = ST->getValue();
515         Tmp =
516           SDValue(CurDAG->getTargetNode(IA64::TPCADDS, dl, MVT::i64, Initial,
517                                           CurDAG->getTargetConstant(1,
518                                                                     MVT::i64),
519                                           Tmp), 0);
520         return CurDAG->SelectNodeTo(N, Opc, MVT::Other, Address, Tmp, Chain);
521       }
522       case MVT::i64: Opc = IA64::ST8;  break;
523       case MVT::f64: Opc = IA64::STF8; break;
524       }
525     } else { // Truncating store
526       switch(ST->getMemoryVT().getSimpleVT()) {
527       default: assert(0 && "unknown type in truncstore");
528       case MVT::i8:  Opc = IA64::ST1;  break;
529       case MVT::i16: Opc = IA64::ST2;  break;
530       case MVT::i32: Opc = IA64::ST4;  break;
531       case MVT::f32: Opc = IA64::STF4; break;
532       }
533     }
534     
535     SDValue N1 = N->getOperand(1);
536     SDValue N2 = N->getOperand(2);
537     return CurDAG->SelectNodeTo(N, Opc, MVT::Other, N2, N1, Chain);
538   }
539
540   case ISD::BRCOND: {
541     SDValue Chain = N->getOperand(0);
542     SDValue CC = N->getOperand(1);
543     MachineBasicBlock *Dest =
544       cast<BasicBlockSDNode>(N->getOperand(2))->getBasicBlock();
545     //FIXME - we do NOT need long branches all the time
546     return CurDAG->SelectNodeTo(N, IA64::BRLCOND_NOTCALL, MVT::Other, CC, 
547                                 CurDAG->getBasicBlock(Dest), Chain);
548   }
549
550   case ISD::CALLSEQ_START:
551   case ISD::CALLSEQ_END: {
552     int64_t Amt = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
553     unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
554       IA64::ADJUSTCALLSTACKDOWN : IA64::ADJUSTCALLSTACKUP;
555     SDValue N0 = N->getOperand(0);
556     return CurDAG->SelectNodeTo(N, Opc, MVT::Other, getI64Imm(Amt), N0);
557   }
558
559   case ISD::BR:
560     // FIXME: we don't need long branches all the time!
561     SDValue N0 = N->getOperand(0);
562     return CurDAG->SelectNodeTo(N, IA64::BRL_NOTCALL, MVT::Other, 
563                                 N->getOperand(1), N0);
564   }
565   
566   return SelectCode(Op);
567 }
568
569
570 /// createIA64DAGToDAGInstructionSelector - This pass converts a legalized DAG
571 /// into an IA64-specific DAG, ready for instruction scheduling.
572 ///
573 FunctionPass
574 *llvm::createIA64DAGToDAGInstructionSelector(IA64TargetMachine &TM) {
575   return new IA64DAGToDAGISel(TM);
576 }
577