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