Remove InFlightSet hack. No longer needed.
[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 <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     void Select(SDOperand &Result, 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     SDOperand 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   // The selection process is inherently a bottom-up recursive process (users
106   // select their uses before themselves).  Given infinite stack space, we
107   // could just start selecting on the root and traverse the whole graph.  In
108   // practice however, this causes us to run out of stack space on large basic
109   // blocks.  To avoid this problem, select the entry node, then all its uses,
110   // iteratively instead of recursively.
111   std::vector<SDOperand> Worklist;
112   Worklist.push_back(DAG.getEntryNode());
113   
114   // Note that we can do this in the IA64 target (scanning forward across token
115   // chain edges) because no nodes ever get folded across these edges.  On a
116   // target like X86 which supports load/modify/store operations, this would
117   // have to be more careful.
118   while (!Worklist.empty()) {
119     SDOperand Node = Worklist.back();
120     Worklist.pop_back();
121
122     if ((Node.Val->getOpcode() >= ISD::BUILTIN_OP_END &&
123          Node.Val->getOpcode() < IA64ISD::FIRST_NUMBER) ||
124         CodeGenMap.count(Node)) continue;
125     
126     for (SDNode::use_iterator UI = Node.Val->use_begin(),
127          E = Node.Val->use_end(); UI != E; ++UI) {
128       // Scan the values.  If this use has a value that is a token chain, add it
129       // to the worklist.
130       SDNode *User = *UI;
131       for (unsigned i = 0, e = User->getNumValues(); i != e; ++i)
132         if (User->getValueType(i) == MVT::Other) {
133           Worklist.push_back(SDOperand(User, i));
134           break; 
135         }
136     }
137
138     // Finally, legalize this node.
139     SDOperand Dummy;
140     Select(Dummy, Node);
141   }
142     
143   // Select target instructions for the DAG.
144   DAG.setRoot(SelectRoot(DAG.getRoot()));
145   CodeGenMap.clear();
146   HandleMap.clear();
147   ReplaceMap.clear();
148   DAG.RemoveDeadNodes();
149   
150   // Emit machine code to BB. 
151   ScheduleAndEmitDAG(DAG);
152 }
153
154 SDOperand IA64DAGToDAGISel::SelectDIV(SDOperand Op) {
155   SDNode *N = Op.Val;
156   SDOperand Chain, Tmp1, Tmp2;
157   Select(Chain, N->getOperand(0));
158
159   Select(Tmp1, N->getOperand(0));
160   Select(Tmp2, N->getOperand(1));
161
162   bool isFP=false;
163
164   if(MVT::isFloatingPoint(Tmp1.getValueType()))
165     isFP=true;
166     
167   bool isModulus=false; // is it a division or a modulus?
168   bool isSigned=false;
169
170   switch(N->getOpcode()) {
171     case ISD::FDIV:
172     case ISD::SDIV:  isModulus=false; isSigned=true;  break;
173     case ISD::UDIV:  isModulus=false; isSigned=false; break;
174     case ISD::FREM:
175     case ISD::SREM:  isModulus=true;  isSigned=true;  break;
176     case ISD::UREM:  isModulus=true;  isSigned=false; break;
177   }
178
179   // TODO: check for integer divides by powers of 2 (or other simple patterns?)
180
181     SDOperand TmpPR, TmpPR2;
182     SDOperand TmpF1, TmpF2, TmpF3, TmpF4, TmpF5, TmpF6, TmpF7, TmpF8;
183     SDOperand TmpF9, TmpF10,TmpF11,TmpF12,TmpF13,TmpF14,TmpF15;
184     SDNode *Result;
185
186     // we'll need copies of F0 and F1
187     SDOperand F0 = CurDAG->getRegister(IA64::F0, MVT::f64);
188     SDOperand F1 = CurDAG->getRegister(IA64::F1, MVT::f64);
189     
190     // OK, emit some code:
191
192     if(!isFP) {
193       // first, load the inputs into FP regs.
194       TmpF1 =
195         SDOperand(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, Tmp1), 0);
196       Chain = TmpF1.getValue(1);
197       TmpF2 =
198         SDOperand(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, Tmp2), 0);
199       Chain = TmpF2.getValue(1);
200       
201       // next, convert the inputs to FP
202       if(isSigned) {
203         TmpF3 =
204           SDOperand(CurDAG->getTargetNode(IA64::FCVTXF, MVT::f64, TmpF1), 0);
205         Chain = TmpF3.getValue(1);
206         TmpF4 =
207           SDOperand(CurDAG->getTargetNode(IA64::FCVTXF, MVT::f64, TmpF2), 0);
208         Chain = TmpF4.getValue(1);
209       } else { // is unsigned
210         TmpF3 =
211           SDOperand(CurDAG->getTargetNode(IA64::FCVTXUFS1, MVT::f64, TmpF1), 0);
212         Chain = TmpF3.getValue(1);
213         TmpF4 =
214           SDOperand(CurDAG->getTargetNode(IA64::FCVTXUFS1, MVT::f64, TmpF2), 0);
215         Chain = TmpF4.getValue(1);
216       }
217
218     } else { // this is an FP divide/remainder, so we 'leak' some temp
219              // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
220       TmpF3=Tmp1;
221       TmpF4=Tmp2;
222     }
223
224     // we start by computing an approximate reciprocal (good to 9 bits?)
225     // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
226     if(isFP)
227       TmpF5 = SDOperand(CurDAG->getTargetNode(IA64::FRCPAS0, MVT::f64, MVT::i1,
228                                               TmpF3, TmpF4), 0);
229     else
230       TmpF5 = SDOperand(CurDAG->getTargetNode(IA64::FRCPAS1, MVT::f64, MVT::i1,
231                                               TmpF3, TmpF4), 0);
232                                   
233     TmpPR = TmpF5.getValue(1);
234     Chain = TmpF5.getValue(2);
235
236     SDOperand minusB;
237     if(isModulus) { // for remainders, it'll be handy to have
238                              // copies of -input_b
239       minusB = SDOperand(CurDAG->getTargetNode(IA64::SUB, MVT::i64,
240                   CurDAG->getRegister(IA64::r0, MVT::i64), Tmp2), 0);
241       Chain = minusB.getValue(1);
242     }
243     
244     SDOperand TmpE0, TmpY1, TmpE1, TmpY2;
245     
246     TmpE0 = SDOperand(CurDAG->getTargetNode(IA64::CFNMAS1, MVT::f64,
247                                             TmpF4, TmpF5, F1, TmpPR), 0);
248     Chain = TmpE0.getValue(1);
249     TmpY1 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
250                                             TmpF5, TmpE0, TmpF5, TmpPR), 0);
251     Chain = TmpY1.getValue(1);
252     TmpE1 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
253                                             TmpE0, TmpE0, F0, TmpPR), 0);
254     Chain = TmpE1.getValue(1);
255     TmpY2 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
256                                             TmpY1, TmpE1, TmpY1, TmpPR), 0);
257     Chain = TmpY2.getValue(1);
258     
259     if(isFP) { // if this is an FP divide, we finish up here and exit early
260       if(isModulus)
261         assert(0 && "Sorry, try another FORTRAN compiler.");
262  
263       SDOperand TmpE2, TmpY3, TmpQ0, TmpR0;
264       
265       TmpE2 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
266                                               TmpE1, TmpE1, F0, TmpPR), 0);
267       Chain = TmpE2.getValue(1);
268       TmpY3 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
269                                               TmpY2, TmpE2, TmpY2, TmpPR), 0);
270       Chain = TmpY3.getValue(1);
271       TmpQ0 =
272         SDOperand(CurDAG->getTargetNode(IA64::CFMADS1, MVT::f64, // double prec!
273                                         Tmp1, TmpY3, F0, TmpPR), 0);
274       Chain = TmpQ0.getValue(1);
275       TmpR0 =
276         SDOperand(CurDAG->getTargetNode(IA64::CFNMADS1, MVT::f64, // double prec!
277                                         Tmp2, TmpQ0, Tmp1, TmpPR), 0);
278       Chain = TmpR0.getValue(1);
279
280 // we want Result to have the same target register as the frcpa, so
281 // we two-address hack it. See the comment "for this to work..." on
282 // page 48 of Intel application note #245415
283       Result = CurDAG->getTargetNode(IA64::TCFMADS0, MVT::f64, // d.p. s0 rndg!
284                                      TmpF5, TmpY3, TmpR0, TmpQ0, TmpPR);
285       Chain = SDOperand(Result, 1);
286       return SDOperand(Result, 0); // XXX: early exit!
287     } else { // this is *not* an FP divide, so there's a bit left to do:
288     
289       SDOperand TmpQ2, TmpR2, TmpQ3, TmpQ;
290       
291       TmpQ2 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
292                                               TmpF3, TmpY2, F0, TmpPR), 0);
293       Chain = TmpQ2.getValue(1);
294       TmpR2 = SDOperand(CurDAG->getTargetNode(IA64::CFNMAS1, MVT::f64,
295                                               TmpF4, TmpQ2, TmpF3, TmpPR), 0);
296       Chain = TmpR2.getValue(1);
297       
298 // we want TmpQ3 to have the same target register as the frcpa? maybe we
299 // should two-address hack it. See the comment "for this to work..." on page
300 // 48 of Intel application note #245415
301       TmpQ3 = SDOperand(CurDAG->getTargetNode(IA64::TCFMAS1, MVT::f64,
302                                          TmpF5, TmpR2, TmpY2, TmpQ2, TmpPR), 0);
303       Chain = TmpQ3.getValue(1);
304
305       // STORY: without these two-address instructions (TCFMAS1 and TCFMADS0)
306       // the FPSWA won't be able to help out in the case of large/tiny
307       // arguments. Other fun bugs may also appear, e.g. 0/x = x, not 0.
308       
309       if(isSigned)
310         TmpQ = SDOperand(CurDAG->getTargetNode(IA64::FCVTFXTRUNCS1,
311                                                MVT::f64, TmpQ3), 0);
312       else
313         TmpQ = SDOperand(CurDAG->getTargetNode(IA64::FCVTFXUTRUNCS1,
314                                                MVT::f64, TmpQ3), 0);
315       
316       Chain = TmpQ.getValue(1);
317
318       if(isModulus) {
319         SDOperand FPminusB =
320           SDOperand(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, minusB), 0);
321         Chain = FPminusB.getValue(1);
322         SDOperand Remainder =
323           SDOperand(CurDAG->getTargetNode(IA64::XMAL, MVT::f64,
324                                           TmpQ, FPminusB, TmpF1), 0);
325         Chain = Remainder.getValue(1);
326         Result = CurDAG->getTargetNode(IA64::GETFSIG, MVT::i64, Remainder);
327         Chain = SDOperand(Result, 1);
328       } else { // just an integer divide
329         Result = CurDAG->getTargetNode(IA64::GETFSIG, MVT::i64, TmpQ);
330         Chain = SDOperand(Result, 1);
331       }
332
333       return SDOperand(Result, 0);
334     } // wasn't an FP divide
335 }
336
337 // Select - Convert the specified operand from a target-independent to a
338 // target-specific node if it hasn't already been changed.
339 void IA64DAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
340   SDNode *N = Op.Val;
341   if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
342       N->getOpcode() < IA64ISD::FIRST_NUMBER) {
343     Result = Op;
344     return;   // Already selected.
345   }
346
347   // If this has already been converted, use it.
348   std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
349   if (CGMI != CodeGenMap.end()) {
350     Result = CGMI->second;
351     return;
352   }
353   
354   switch (N->getOpcode()) {
355   default: break;
356
357   case IA64ISD::BRCALL: { // XXX: this is also a hack!
358     SDOperand Chain;
359     SDOperand InFlag;  // Null incoming flag value.
360
361     Select(Chain, N->getOperand(0));
362     if(N->getNumOperands()==3) // we have an incoming chain, callee and flag
363       Select(InFlag, N->getOperand(2));
364
365     unsigned CallOpcode;
366     SDOperand CallOperand;
367     
368     // if we can call directly, do so
369     if (GlobalAddressSDNode *GASD =
370       dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
371       CallOpcode = IA64::BRCALL_IPREL_GA;
372       CallOperand = CurDAG->getTargetGlobalAddress(GASD->getGlobal(), MVT::i64);
373     } else if (ExternalSymbolSDNode *ESSDN = // FIXME: we currently NEED this
374                                          // case for correctness, to avoid
375                                          // "non-pic code with imm reloc.n
376                                          // against dynamic symbol" errors
377              dyn_cast<ExternalSymbolSDNode>(N->getOperand(1))) {
378     CallOpcode = IA64::BRCALL_IPREL_ES;
379     CallOperand = N->getOperand(1);
380   } else {
381     // otherwise we need to load the function descriptor,
382     // load the branch target (function)'s entry point and GP,
383     // branch (call) then restore the GP
384     SDOperand FnDescriptor;
385     Select(FnDescriptor, N->getOperand(1));
386    
387     // load the branch target's entry point [mem] and 
388     // GP value [mem+8]
389     SDOperand targetEntryPoint=
390       SDOperand(CurDAG->getTargetNode(IA64::LD8, MVT::i64, FnDescriptor), 0);
391     Chain = targetEntryPoint.getValue(1);
392     SDOperand targetGPAddr=
393       SDOperand(CurDAG->getTargetNode(IA64::ADDS, MVT::i64, 
394                     FnDescriptor, CurDAG->getConstant(8, MVT::i64)), 0);
395     Chain = targetGPAddr.getValue(1);
396     SDOperand targetGP=
397       SDOperand(CurDAG->getTargetNode(IA64::LD8, MVT::i64, targetGPAddr), 0);
398     Chain = targetGP.getValue(1);
399
400     Chain = CurDAG->getCopyToReg(Chain, IA64::r1, targetGP, InFlag);
401     InFlag = Chain.getValue(1);
402     Chain = CurDAG->getCopyToReg(Chain, IA64::B6, targetEntryPoint, InFlag); // FLAG these?
403     InFlag = Chain.getValue(1);
404     
405     CallOperand = CurDAG->getRegister(IA64::B6, MVT::i64);
406     CallOpcode = IA64::BRCALL_INDIRECT;
407   }
408  
409    // Finally, once everything is setup, emit the call itself
410    if(InFlag.Val)
411      Chain = SDOperand(CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
412                                              CallOperand, InFlag), 0);
413    else // there might be no arguments
414      Chain = SDOperand(CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
415                                              CallOperand, Chain), 0);
416    InFlag = Chain.getValue(1);
417
418    std::vector<SDOperand> CallResults;
419
420    CallResults.push_back(Chain);
421    CallResults.push_back(InFlag);
422
423    for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
424      CodeGenMap[Op.getValue(i)] = CallResults[i];
425    Result = CallResults[Op.ResNo];
426    return;
427   }
428   
429   case IA64ISD::GETFD: {
430     SDOperand Input;
431     Select(Input, N->getOperand(0));
432     Result = SDOperand(CurDAG->getTargetNode(IA64::GETFD, MVT::i64, Input), 0);
433     CodeGenMap[Op] = Result;
434     return;
435   } 
436   
437   case ISD::FDIV:
438   case ISD::SDIV:
439   case ISD::UDIV:
440   case ISD::SREM:
441   case ISD::UREM:
442     Result = SelectDIV(Op);
443     return;
444  
445   case ISD::TargetConstantFP: {
446     SDOperand Chain = CurDAG->getEntryNode(); // this is a constant, so..
447
448     if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0)) {
449       Result = CurDAG->getCopyFromReg(Chain, IA64::F0, MVT::f64);
450     } else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0)) {
451       Result = CurDAG->getCopyFromReg(Chain, IA64::F1, MVT::f64);
452     } else
453       assert(0 && "Unexpected FP constant!");
454     return;
455   }
456
457   case ISD::FrameIndex: { // TODO: reduce creepyness
458     int FI = cast<FrameIndexSDNode>(N)->getIndex();
459     if (N->hasOneUse())
460       Result = CurDAG->SelectNodeTo(N, IA64::MOV, MVT::i64,
461                                   CurDAG->getTargetFrameIndex(FI, MVT::i64));
462     else
463       Result = CodeGenMap[Op] = SDOperand(CurDAG->getTargetNode(IA64::MOV, MVT::i64,
464                                 CurDAG->getTargetFrameIndex(FI, MVT::i64)), 0);
465     return;
466   }
467
468   case ISD::ConstantPool: { // TODO: nuke the constant pool
469                             //       (ia64 doesn't need one)
470     ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
471     Constant *C = CP->get();
472     SDOperand CPI = CurDAG->getTargetConstantPool(C, MVT::i64,
473                                                   CP->getAlignment());
474     Result = SDOperand(CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64, // ?
475                               CurDAG->getRegister(IA64::r1, MVT::i64), CPI), 0);
476     return;
477   }
478
479   case ISD::GlobalAddress: {
480     GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
481     SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i64);
482     SDOperand Tmp = SDOperand(CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64, 
483                                   CurDAG->getRegister(IA64::r1, MVT::i64), GA), 0);
484     Result = SDOperand(CurDAG->getTargetNode(IA64::LD8, MVT::i64, Tmp), 0);
485     return;
486   }
487   
488 /* XXX  case ISD::ExternalSymbol: {
489     SDOperand EA = CurDAG->getTargetExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol(),
490           MVT::i64);
491     SDOperand Tmp = CurDAG->getTargetNode(IA64::ADDL_EA, MVT::i64, 
492                                   CurDAG->getRegister(IA64::r1, MVT::i64), EA);
493     return CurDAG->getTargetNode(IA64::LD8, MVT::i64, Tmp);
494  }
495 */
496
497   case ISD::LOAD:
498   case ISD::EXTLOAD: // FIXME: load -1, not 1, for bools?
499   case ISD::ZEXTLOAD: {
500     SDOperand Chain, Address;
501     Select(Chain, N->getOperand(0));
502     Select(Address, N->getOperand(1));
503
504     MVT::ValueType TypeBeingLoaded = (N->getOpcode() == ISD::LOAD) ?
505       N->getValueType(0) : cast<VTSDNode>(N->getOperand(3))->getVT();
506     unsigned Opc;
507     switch (TypeBeingLoaded) {
508     default:
509 #ifndef NDEBUG
510       N->dump();
511 #endif
512       assert(0 && "Cannot load this type!");
513     case MVT::i1: { // this is a bool
514       Opc = IA64::LD1; // first we load a byte, then compare for != 0
515       if(N->getValueType(0) == MVT::i1) { // XXX: early exit!
516         Result = CurDAG->SelectNodeTo(N, IA64::CMPNE, MVT::i1, MVT::Other, 
517                     SDOperand(CurDAG->getTargetNode(Opc, MVT::i64, Address), 0),
518                                   CurDAG->getRegister(IA64::r0, MVT::i64), 
519                                   Chain).getValue(Op.ResNo);
520         return;
521       }
522       /* otherwise, we want to load a bool into something bigger: LD1
523          will do that for us, so we just fall through */
524     }
525     case MVT::i8:  Opc = IA64::LD1; break;
526     case MVT::i16: Opc = IA64::LD2; break;
527     case MVT::i32: Opc = IA64::LD4; break;
528     case MVT::i64: Opc = IA64::LD8; break;
529     
530     case MVT::f32: Opc = IA64::LDF4; break;
531     case MVT::f64: Opc = IA64::LDF8; break;
532     }
533
534     // TODO: comment this
535     Result = CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
536                                 Address, Chain).getValue(Op.ResNo);
537     return;
538   }
539   
540   case ISD::TRUNCSTORE:
541   case ISD::STORE: {
542     SDOperand Address, Chain;
543     Select(Address, N->getOperand(2));
544     Select(Chain, N->getOperand(0));
545    
546     unsigned Opc;
547     if (N->getOpcode() == ISD::STORE) {
548       switch (N->getOperand(1).getValueType()) {
549       default: assert(0 && "unknown type in store");
550       case MVT::i1: { // this is a bool
551         Opc = IA64::ST1; // we store either 0 or 1 as a byte 
552         // first load zero!
553         SDOperand Initial = CurDAG->getCopyFromReg(Chain, IA64::r0, MVT::i64);
554         Chain = Initial.getValue(1);
555         // then load 1 into the same reg iff the predicate to store is 1
556         SDOperand Tmp;
557         Select(Tmp, N->getOperand(1));
558         Tmp = SDOperand(CurDAG->getTargetNode(IA64::TPCADDS, MVT::i64, Initial,
559                                               CurDAG->getConstant(1, MVT::i64),
560                                               Tmp), 0);
561         Result = CurDAG->SelectNodeTo(N, Opc, MVT::Other, Address, Tmp, Chain);
562         return;
563       }
564       case MVT::i64: Opc = IA64::ST8;  break;
565       case MVT::f64: Opc = IA64::STF8; break;
566       }
567     } else { //ISD::TRUNCSTORE
568       switch(cast<VTSDNode>(N->getOperand(4))->getVT()) {
569       default: assert(0 && "unknown type in truncstore");
570       case MVT::i8:  Opc = IA64::ST1;  break;
571       case MVT::i16: Opc = IA64::ST2;  break;
572       case MVT::i32: Opc = IA64::ST4;  break;
573       case MVT::f32: Opc = IA64::STF4; break;
574       }
575     }
576     
577     SDOperand N1, N2;
578     Select(N1, N->getOperand(1));
579     Select(N2, N->getOperand(2));
580     Result = CurDAG->SelectNodeTo(N, Opc, MVT::Other, N2, N1, Chain);
581     return;
582   }
583
584   case ISD::BRCOND: {
585     SDOperand Chain, CC;
586     Select(Chain, N->getOperand(0));
587     Select(CC, N->getOperand(1));
588     MachineBasicBlock *Dest =
589       cast<BasicBlockSDNode>(N->getOperand(2))->getBasicBlock();
590     //FIXME - we do NOT need long branches all the time
591     Result = CurDAG->SelectNodeTo(N, IA64::BRLCOND_NOTCALL, MVT::Other, CC, 
592                                 CurDAG->getBasicBlock(Dest), Chain);
593     return;
594   }
595
596   case ISD::CALLSEQ_START:
597   case ISD::CALLSEQ_END: {
598     int64_t Amt = cast<ConstantSDNode>(N->getOperand(1))->getValue();
599     unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
600                        IA64::ADJUSTCALLSTACKDOWN : IA64::ADJUSTCALLSTACKUP;
601     SDOperand N0;
602     Select(N0, N->getOperand(0));
603     Result = CurDAG->SelectNodeTo(N, Opc, MVT::Other, getI64Imm(Amt), N0);
604     return;
605   }
606
607   case ISD::BR:
608                  // FIXME: we don't need long branches all the time!
609     SDOperand N0;
610     Select(N0, N->getOperand(0));
611     Result = CurDAG->SelectNodeTo(N, IA64::BRL_NOTCALL, MVT::Other, 
612                                 N->getOperand(1), N0);
613     return;
614   }
615   
616   SelectCode(Result, Op);
617 }
618
619
620 /// createIA64DAGToDAGInstructionSelector - This pass converts a legalized DAG
621 /// into an IA64-specific DAG, ready for instruction scheduling.
622 ///
623 FunctionPass
624 *llvm::createIA64DAGToDAGInstructionSelector(IA64TargetMachine &TM) {
625   return new IA64DAGToDAGISel(TM);
626 }
627