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