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