30386d8bd810b8662936a02e94bad6da7efae769
[oota-llvm.git] / lib / Target / Alpha / AlphaISelPattern.cpp
1 //===- AlphaISelPattern.cpp - A pattern matching inst selector for Alpha --===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group 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 Alpha.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "Alpha.h"
15 #include "AlphaRegisterInfo.h"
16 #include "llvm/Constants.h"                   // FIXME: REMOVE
17 #include "llvm/Function.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/SelectionDAG.h"
23 #include "llvm/CodeGen/SelectionDAGISel.h"
24 #include "llvm/CodeGen/SSARegMap.h"
25 #include "llvm/Target/TargetData.h"
26 #include "llvm/Target/TargetLowering.h"
27 #include "llvm/Support/MathExtras.h"
28 #include "llvm/ADT/Statistic.h"
29 #include <set>
30 #include <algorithm>
31 using namespace llvm;
32
33 //===----------------------------------------------------------------------===//
34 //  AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
35 namespace {
36   class AlphaTargetLowering : public TargetLowering {
37     int VarArgsFrameIndex;            // FrameIndex for start of varargs area.
38     unsigned GP; //GOT vreg
39   public:
40     AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
41       // Set up the TargetLowering object.
42       //I am having problems with shr n ubyte 1
43       setShiftAmountType(MVT::i64);
44       setSetCCResultType(MVT::i64);
45       
46       addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
47       addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
48       addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
49       
50       setOperationAction(ISD::EXTLOAD          , MVT::i1   , Promote);
51
52       setOperationAction(ISD::ZEXTLOAD         , MVT::i1   , Expand);
53       setOperationAction(ISD::ZEXTLOAD         , MVT::i32  , Expand);
54
55       setOperationAction(ISD::SEXTLOAD         , MVT::i1   , Expand);
56       setOperationAction(ISD::SEXTLOAD         , MVT::i8   , Expand);
57       setOperationAction(ISD::SEXTLOAD         , MVT::i16  , Expand);
58
59       setOperationAction(ISD::SREM             , MVT::f32  , Expand);
60       setOperationAction(ISD::SREM             , MVT::f64  , Expand);
61
62       setOperationAction(ISD::MEMMOVE          , MVT::Other, Expand);
63       setOperationAction(ISD::MEMSET           , MVT::Other, Expand);
64       setOperationAction(ISD::MEMCPY           , MVT::Other, Expand);
65
66       computeRegisterProperties();
67       
68       addLegalFPImmediate(+0.0); //F31
69       addLegalFPImmediate(-0.0); //-F31
70     }
71
72     /// LowerArguments - This hook must be implemented to indicate how we should
73     /// lower the arguments for the specified function, into the specified DAG.
74     virtual std::vector<SDOperand>
75     LowerArguments(Function &F, SelectionDAG &DAG);
76     
77     /// LowerCallTo - This hook lowers an abstract call to a function into an
78     /// actual call.
79     virtual std::pair<SDOperand, SDOperand>
80     LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
81                 ArgListTy &Args, SelectionDAG &DAG);
82     
83     virtual std::pair<SDOperand, SDOperand>
84     LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
85     
86     virtual std::pair<SDOperand,SDOperand>
87     LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
88                    const Type *ArgTy, SelectionDAG &DAG);
89
90     virtual std::pair<SDOperand, SDOperand>
91     LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
92                             SelectionDAG &DAG);
93
94     void restoreGP(MachineBasicBlock* BB)
95     {
96       BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
97     }
98   };
99 }
100
101 //http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
102
103 //For now, just use variable size stack frame format
104
105 //In a standard call, the first six items are passed in registers $16
106 //- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
107 //of argument-to-register correspondence.) The remaining items are
108 //collected in a memory argument list that is a naturally aligned
109 //array of quadwords. In a standard call, this list, if present, must
110 //be passed at 0(SP).
111 //7 ... n                       0(SP) ... (n-7)*8(SP)
112
113 std::vector<SDOperand>
114 AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) 
115 {
116   std::vector<SDOperand> ArgValues;
117   
118   // //#define FP    $15
119   // //#define RA    $26
120   // //#define PV    $27
121   // //#define GP    $29
122   // //#define SP    $30
123   
124   //  assert(0 && "TODO");
125   MachineFunction &MF = DAG.getMachineFunction();
126   MachineFrameInfo*MFI = MF.getFrameInfo();
127
128   GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
129   MachineBasicBlock& BB = MF.front();
130
131   //Handle the return address
132   //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
133
134   unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18, 
135                          Alpha::R19, Alpha::R20, Alpha::R21};
136   unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18, 
137                            Alpha::F19, Alpha::F20, Alpha::F21};
138   unsigned argVreg[6];
139   unsigned argPreg[6];
140   unsigned argOpc[6];
141
142   int count = 0;
143
144   for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I)
145   {
146     SDOperand newroot, argt;
147     if (count  < 6) {
148       switch (getValueType(I->getType())) {
149       default: 
150         std::cerr << "Unknown Type " << getValueType(I->getType()) << "\n"; 
151         abort();
152       case MVT::f64:
153       case MVT::f32:
154         BuildMI(&BB, Alpha::IDEF, 0, args_float[count]);
155         argVreg[count] = 
156           MF.getSSARegMap()->createVirtualRegister(
157                              getRegClassFor(getValueType(I->getType())));
158         argPreg[count] = args_float[count];
159         argOpc[count] = Alpha::CPYS;
160         argt = newroot = DAG.getCopyFromReg(argVreg[count], 
161                                             getValueType(I->getType()), 
162                                             DAG.getRoot());
163         break;
164       case MVT::i1:
165       case MVT::i8:
166       case MVT::i16:
167       case MVT::i32:
168       case MVT::i64:
169         BuildMI(&BB, Alpha::IDEF, 0, args_int[count]);
170         argVreg[count] = 
171           MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
172         argPreg[count] = args_int[count];
173         argOpc[count] = Alpha::BIS;
174         argt = newroot = 
175           DAG.getCopyFromReg(argVreg[count], MVT::i64, DAG.getRoot());
176         if (getValueType(I->getType()) != MVT::i64)
177           argt = 
178             DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()), newroot);
179         break;
180       }
181       ++count;
182     } else { //more args
183       // Create the frame index object for this incoming parameter...
184       int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
185         
186       // Create the SelectionDAG nodes corresponding to a load 
187       //from this parameter
188       SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
189       argt = newroot = DAG.getLoad(getValueType(I->getType()), 
190                                    DAG.getEntryNode(), FIN);
191     }
192     DAG.setRoot(newroot.getValue(1));
193     ArgValues.push_back(argt);
194   }
195
196   BuildMI(&BB, Alpha::IDEF, 0, Alpha::R29);
197   BuildMI(&BB, Alpha::BIS, 2, GP).addReg(Alpha::R29).addReg(Alpha::R29);
198   for (int i = 0; i < count; ++i) {
199     if (argPreg[i] == Alpha::F16 || argPreg[i] == Alpha::F17 || 
200         argPreg[i] == Alpha::F18 || argPreg[i] == Alpha::F19 || 
201         argPreg[i] == Alpha::F20 || argPreg[i] == Alpha::F21)
202     {
203       assert(argOpc[i] == Alpha::CPYS && "Using BIS for a float??");
204     }
205     BuildMI(&BB, argOpc[i], 2, 
206             argVreg[i]).addReg(argPreg[i]).addReg(argPreg[i]);
207   }
208   
209   return ArgValues;
210 }
211
212 std::pair<SDOperand, SDOperand>
213 AlphaTargetLowering::LowerCallTo(SDOperand Chain,
214                                  const Type *RetTy, SDOperand Callee,
215                                  ArgListTy &Args, SelectionDAG &DAG) {
216   int NumBytes = 0;
217   if (Args.size() > 6)
218     NumBytes = (Args.size() - 6) * 8;
219
220   Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
221                       DAG.getConstant(NumBytes, getPointerTy()));
222   std::vector<SDOperand> args_to_use;
223   for (unsigned i = 0, e = Args.size(); i != e; ++i)
224   {
225     switch (getValueType(Args[i].second)) {
226     default: assert(0 && "Unexpected ValueType for argument!");
227     case MVT::i1:
228     case MVT::i8:
229     case MVT::i16:
230     case MVT::i32:
231       // Promote the integer to 64 bits.  If the input type is signed use a
232       // sign extend, otherwise use a zero extend.
233       if (Args[i].second->isSigned())
234         Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
235       else
236         Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
237       break;
238     case MVT::i64:
239     case MVT::f64:
240     case MVT::f32:
241       break;
242     }
243     args_to_use.push_back(Args[i].first);
244   }
245   
246   std::vector<MVT::ValueType> RetVals;
247   MVT::ValueType RetTyVT = getValueType(RetTy);
248   if (RetTyVT != MVT::isVoid)
249     RetVals.push_back(RetTyVT);
250   RetVals.push_back(MVT::Other);
251
252   SDOperand TheCall = SDOperand(DAG.getCall(RetVals, 
253                                             Chain, Callee, args_to_use), 0);
254   Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
255   Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
256                       DAG.getConstant(NumBytes, getPointerTy()));
257   return std::make_pair(TheCall, Chain);
258 }
259
260 std::pair<SDOperand, SDOperand>
261 AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
262   //vastart just returns the address of the VarArgsFrameIndex slot.
263   return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
264 }
265
266 std::pair<SDOperand,SDOperand> AlphaTargetLowering::
267 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
268                const Type *ArgTy, SelectionDAG &DAG) {
269   abort();
270 }
271                
272
273 std::pair<SDOperand, SDOperand> AlphaTargetLowering::
274 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
275                         SelectionDAG &DAG) {
276   abort();
277 }
278
279
280
281
282
283 namespace {
284
285 //===--------------------------------------------------------------------===//
286 /// ISel - Alpha specific code to select Alpha machine instructions for
287 /// SelectionDAG operations.
288 //===--------------------------------------------------------------------===//
289 class ISel : public SelectionDAGISel {
290   
291   /// AlphaLowering - This object fully describes how to lower LLVM code to an
292   /// Alpha-specific SelectionDAG.
293   AlphaTargetLowering AlphaLowering;
294   
295   
296   /// ExprMap - As shared expressions are codegen'd, we keep track of which
297   /// vreg the value is produced in, so we only emit one copy of each compiled
298   /// tree.
299   static const unsigned notIn = (unsigned)(-1);
300   std::map<SDOperand, unsigned> ExprMap;
301   
302   //CCInvMap sometimes (SetNE) we have the inverse CC code for free
303   std::map<SDOperand, unsigned> CCInvMap;
304   
305 public:
306   ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM) 
307   {}
308   
309   /// InstructionSelectBasicBlock - This callback is invoked by
310   /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
311   virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
312     // Codegen the basic block.
313     Select(DAG.getRoot());
314     
315     // Clear state used for selection.
316     ExprMap.clear();
317     CCInvMap.clear();
318   }
319   
320   unsigned SelectExpr(SDOperand N);
321   unsigned SelectExprFP(SDOperand N, unsigned Result);
322   void Select(SDOperand N);
323   
324   void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
325   void SelectBranchCC(SDOperand N);
326 };
327 }
328
329 static unsigned GetSymVersion(unsigned opcode)
330 {
331   switch (opcode) {
332   default: assert(0 && "unknown load or store"); return 0;
333   case Alpha::LDQ: return Alpha::LDQ_SYM;
334   case Alpha::LDS: return Alpha::LDS_SYM;
335   case Alpha::LDT: return Alpha::LDT_SYM;
336   case Alpha::LDL: return Alpha::LDL_SYM;
337   case Alpha::LDBU: return Alpha::LDBU_SYM;
338   case Alpha::LDWU: return Alpha::LDWU_SYM;
339   case Alpha::LDW: return Alpha::LDW_SYM;
340   case Alpha::LDB: return Alpha::LDB_SYM;
341   case Alpha::STQ: return Alpha::STQ_SYM;
342   case Alpha::STS: return Alpha::STS_SYM;
343   case Alpha::STT: return Alpha::STT_SYM;
344   case Alpha::STL: return Alpha::STL_SYM;
345   case Alpha::STW: return Alpha::STW_SYM;
346   case Alpha::STB: return Alpha::STB_SYM;
347   }
348 }
349
350 //Check to see if the load is a constant offset from a base register
351 void ISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
352 {
353   unsigned opcode = N.getOpcode();
354   if (opcode == ISD::ADD) {
355     if(N.getOperand(1).getOpcode() == ISD::Constant && 
356        cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
357     { //Normal imm add
358       Reg = SelectExpr(N.getOperand(0));
359       offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
360       return;
361     }
362     else if(N.getOperand(0).getOpcode() == ISD::Constant && 
363             cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
364     {
365       Reg = SelectExpr(N.getOperand(1));
366       offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
367       return;
368     }
369   }
370   Reg = SelectExpr(N);
371   offset = 0;
372   return;
373 }
374
375 void ISel::SelectBranchCC(SDOperand N)
376 {
377   assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
378   MachineBasicBlock *Dest = 
379     cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
380   unsigned Opc = Alpha::WTF;
381   
382   Select(N.getOperand(0));  //chain
383   SDOperand CC = N.getOperand(1);
384   
385   if (CC.getOpcode() == ISD::SETCC)
386   {
387     SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
388     if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
389       //Dropping the CC is only useful if we are comparing to 0
390       bool isZero0 = false;
391       bool isZero1 = false;
392       bool isNE = false;
393
394       if(SetCC->getOperand(0).getOpcode() == ISD::Constant &&
395          cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0)
396         isZero0 = true;
397       if(SetCC->getOperand(1).getOpcode() == ISD::Constant &&
398          cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0)
399         isZero1 = true;
400       if(SetCC->getCondition() == ISD::SETNE)
401         isNE = true;
402
403       if (isZero0) {
404         switch (SetCC->getCondition()) {
405         default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
406         case ISD::SETEQ:  Opc = Alpha::BEQ; break;
407         case ISD::SETLT:  Opc = Alpha::BGT; break;
408         case ISD::SETLE:  Opc = Alpha::BGE; break;
409         case ISD::SETGT:  Opc = Alpha::BLT; break;
410         case ISD::SETGE:  Opc = Alpha::BLE; break;
411         case ISD::SETULT: Opc = Alpha::BNE; break;
412         case ISD::SETUGT: assert(0 && "0 > (unsigned) x is never true"); break;
413         case ISD::SETULE: assert(0 && "0 <= (unsigned) x is always true"); break;
414         case ISD::SETUGE: Opc = Alpha::BEQ; break; //Technically you could have this CC
415         case ISD::SETNE:  Opc = Alpha::BNE; break;
416         }
417         unsigned Tmp1 = SelectExpr(SetCC->getOperand(1));
418         BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
419         return;
420       } else if (isZero1) {
421         switch (SetCC->getCondition()) {
422         default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
423         case ISD::SETEQ:  Opc = Alpha::BEQ; break;
424         case ISD::SETLT:  Opc = Alpha::BLT; break;
425         case ISD::SETLE:  Opc = Alpha::BLE; break;
426         case ISD::SETGT:  Opc = Alpha::BGT; break;
427         case ISD::SETGE:  Opc = Alpha::BGE; break;
428         case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
429         case ISD::SETUGT: Opc = Alpha::BNE; break;
430         case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
431         case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
432         case ISD::SETNE:  Opc = Alpha::BNE; break;
433         }
434         unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
435         BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
436         return;
437       } else {
438         unsigned Tmp1 = SelectExpr(CC);
439         if (isNE)
440           BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
441         else
442           BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
443         return;
444       }
445     } else { //FP
446       //Any comparison between 2 values should be codegened as an folded branch, as moving
447       //CC to the integer register is very expensive
448       //for a cmp b: c = a - b;
449       //a = b: c = 0
450       //a < b: c < 0
451       //a > b: c > 0
452       unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
453       unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
454       unsigned Tmp3 = MakeReg(MVT::f64);
455       BuildMI(BB, Alpha::SUBT, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
456
457       switch (SetCC->getCondition()) {
458       default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
459       case ISD::SETEQ: Opc = Alpha::FBEQ; break;
460       case ISD::SETLT: Opc = Alpha::FBLT; break;
461       case ISD::SETLE: Opc = Alpha::FBLE; break;
462       case ISD::SETGT: Opc = Alpha::FBGT; break;
463       case ISD::SETGE: Opc = Alpha::FBGE; break;
464       case ISD::SETNE: Opc = Alpha::FBNE; break;
465       }
466       BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
467       return;
468     }
469     abort(); //Should never be reached
470   } else {
471     //Giveup and do the stupid thing
472     unsigned Tmp1 = SelectExpr(CC);
473     BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
474     return;
475   }
476   abort(); //Should never be reached
477 }
478
479 unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
480 {
481   unsigned Tmp1, Tmp2, Tmp3;
482   unsigned Opc = 0;
483   SDNode *Node = N.Val;
484   MVT::ValueType DestType = N.getValueType();
485   unsigned opcode = N.getOpcode();
486
487   switch (opcode) {
488   default:
489     Node->dump();
490     assert(0 && "Node not handled!\n");
491
492   case ISD::SELECT:
493     {
494       Tmp1 = SelectExpr(N.getOperand(0)); //Cond
495       Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
496       Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
497
498
499       // Spill the cond to memory and reload it from there.
500       unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
501       MachineFunction *F = BB->getParent();
502       int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
503       unsigned Tmp4 = MakeReg(MVT::f64);
504       BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
505       BuildMI(BB, Alpha::LDT, 2, Tmp4).addFrameIndex(FrameIdx).addReg(Alpha::F31);
506       //now ideally, we don't have to do anything to the flag...
507       // Get the condition into the zero flag.
508       BuildMI(BB, Alpha::FCMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp4);
509       return Result;
510     }
511
512   case ISD::FP_ROUND:
513     assert (DestType == MVT::f32 && 
514             N.getOperand(0).getValueType() == MVT::f64 && 
515             "only f64 to f32 conversion supported here");
516     Tmp1 = SelectExpr(N.getOperand(0));
517     BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
518     return Result;
519
520   case ISD::FP_EXTEND:
521     assert (DestType == MVT::f64 && 
522             N.getOperand(0).getValueType() == MVT::f32 && 
523             "only f32 to f64 conversion supported here");
524     Tmp1 = SelectExpr(N.getOperand(0));
525     BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
526     return Result;
527
528   case ISD::CopyFromReg:
529     {
530       // Make sure we generate both values.
531       if (Result != notIn)
532         ExprMap[N.getValue(1)] = notIn;   // Generate the token
533       else
534         Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
535       
536       SDOperand Chain   = N.getOperand(0);
537       
538       Select(Chain);
539       unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
540       //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
541       BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
542       return Result;
543     }
544     
545   case ISD::LOAD:
546     {
547       // Make sure we generate both values.
548       if (Result != notIn)
549         ExprMap[N.getValue(1)] = notIn;   // Generate the token
550       else
551         Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
552
553       DestType = N.getValue(0).getValueType();
554
555       SDOperand Chain   = N.getOperand(0);
556       SDOperand Address = N.getOperand(1);
557       Select(Chain);
558       Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
559
560       if (Address.getOpcode() == ISD::GlobalAddress) {
561         AlphaLowering.restoreGP(BB);
562         Opc = GetSymVersion(Opc);
563         BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
564       }
565       else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
566         AlphaLowering.restoreGP(BB);
567         Opc = GetSymVersion(Opc);
568         BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
569       }
570       else if(Address.getOpcode() == ISD::FrameIndex) {
571         Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
572         BuildMI(BB, Opc, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
573       } else {
574         long offset;
575         SelectAddr(Address, Tmp1, offset);
576         BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
577       }
578       return Result;
579     }
580   case ISD::ConstantFP:
581     if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
582       if (CN->isExactlyValue(+0.0)) {
583         BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
584       } else if ( CN->isExactlyValue(-0.0)) {
585         BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
586       } else {
587         abort();
588       }
589     }
590     return Result;
591     
592   case ISD::MUL:
593   case ISD::ADD:
594   case ISD::SUB:
595   case ISD::SDIV:
596     switch( opcode ) {
597     case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
598     case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
599     case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
600     case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
601     };
602     Tmp1 = SelectExpr(N.getOperand(0));
603     Tmp2 = SelectExpr(N.getOperand(1));
604     BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
605     return Result;
606
607   case ISD::EXTLOAD:
608     {
609       //include a conversion sequence for float loads to double
610       if (Result != notIn)
611         ExprMap[N.getValue(1)] = notIn;   // Generate the token
612       else
613         Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
614       
615       Tmp1 = MakeReg(MVT::f32);
616       
617       assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 && 
618              "EXTLOAD not from f32");
619       assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
620       
621       SDOperand Chain   = N.getOperand(0);
622       SDOperand Address = N.getOperand(1);
623       Select(Chain);
624       
625       if (Address.getOpcode() == ISD::GlobalAddress) {
626         AlphaLowering.restoreGP(BB);
627         BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
628       }
629       else if (ConstantPoolSDNode *CP = 
630                dyn_cast<ConstantPoolSDNode>(N.getOperand(1))) 
631       {
632         AlphaLowering.restoreGP(BB);
633         BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
634       }
635       else if(Address.getOpcode() == ISD::FrameIndex) {
636         Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
637         BuildMI(BB, Alpha::LDS, 2, Tmp1).addFrameIndex(Tmp2).addReg(Alpha::F31);
638       } else {
639         long offset;
640         SelectAddr(Address, Tmp2, offset);
641         BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
642       }
643       BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
644       return Result;
645     }
646
647   case ISD::UINT_TO_FP:
648   case ISD::SINT_TO_FP:
649     {
650       assert (N.getOperand(0).getValueType() == MVT::i64 
651               && "only quads can be loaded from");
652       Tmp1 = SelectExpr(N.getOperand(0));  // Get the operand register
653       Tmp2 = MakeReg(MVT::f64);
654
655       //The hard way:
656       // Spill the integer to memory and reload it from there.
657       unsigned Size = MVT::getSizeInBits(MVT::i64)/8;
658       MachineFunction *F = BB->getParent();
659       int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
660
661       BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
662       BuildMI(BB, Alpha::LDT, 2, Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
663       Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
664       BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
665
666       //The easy way: doesn't work
667       //       //so these instructions are not supported on ev56
668       //       Opc = DestType == MVT::f64 ? Alpha::ITOFT : Alpha::ITOFS;
669       //       BuildMI(BB,  Opc, 1, Tmp2).addReg(Tmp1);
670       //       Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
671       //       BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
672
673       return Result;
674     }
675   }
676   assert(0 && "should not get here");
677   return 0;
678 }
679
680 unsigned ISel::SelectExpr(SDOperand N) {
681   unsigned Result;
682   unsigned Tmp1, Tmp2, Tmp3;
683   unsigned Opc = 0;
684   unsigned opcode = N.getOpcode();
685
686   SDNode *Node = N.Val;
687   MVT::ValueType DestType = N.getValueType();
688
689   unsigned &Reg = ExprMap[N];
690   if (Reg) return Reg;
691
692   if (N.getOpcode() != ISD::CALL)
693     Reg = Result = (N.getValueType() != MVT::Other) ?
694       MakeReg(N.getValueType()) : notIn;
695   else {
696     // If this is a call instruction, make sure to prepare ALL of the result
697     // values as well as the chain.
698     if (Node->getNumValues() == 1)
699       Reg = Result = notIn;  // Void call, just a chain.
700     else {
701       Result = MakeReg(Node->getValueType(0));
702       ExprMap[N.getValue(0)] = Result;
703       for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
704         ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
705       ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
706     }
707   }
708
709   if (DestType == MVT::f64 || DestType == MVT::f32 ||
710       (
711        (opcode == ISD::LOAD || opcode == ISD::CopyFromReg || 
712         opcode == ISD::EXTLOAD) &&
713        (N.getValue(0).getValueType() == MVT::f32 || 
714         N.getValue(0).getValueType() == MVT::f64)
715        )
716       )
717     return SelectExprFP(N, Result);
718
719   switch (opcode) {
720   default:
721     Node->dump();
722     assert(0 && "Node not handled!\n");
723  
724   case ISD::ConstantPool:
725     Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
726     AlphaLowering.restoreGP(BB);
727     BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
728     return Result;
729
730   case ISD::FrameIndex:
731     Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
732     BuildMI(BB, Alpha::LDA, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
733     return Result;
734   
735   case ISD::EXTLOAD:
736   case ISD::ZEXTLOAD:
737   case ISD::SEXTLOAD:
738   case ISD::LOAD: 
739     {
740       // Make sure we generate both values.
741       if (Result != notIn)
742         ExprMap[N.getValue(1)] = notIn;   // Generate the token
743       else
744         Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
745     
746       SDOperand Chain   = N.getOperand(0);
747       SDOperand Address = N.getOperand(1);
748       Select(Chain);
749
750       assert(Node->getValueType(0) == MVT::i64 && 
751              "Unknown type to sign extend to.");
752       if (opcode == ISD::LOAD)
753         Opc = Alpha::LDQ;
754       else
755         switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
756         default: Node->dump(); assert(0 && "Bad sign extend!");
757         case MVT::i32: Opc = Alpha::LDL; 
758           assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
759         case MVT::i16: Opc = Alpha::LDWU; 
760           assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
761         case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
762         case MVT::i8: Opc = Alpha::LDBU; 
763           assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
764         }
765
766       if (Address.getOpcode() == ISD::GlobalAddress) {
767         AlphaLowering.restoreGP(BB);
768         Opc = GetSymVersion(Opc);
769         BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
770       }
771       else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
772         AlphaLowering.restoreGP(BB);
773         Opc = GetSymVersion(Opc);
774         BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
775       }
776       else if(Address.getOpcode() == ISD::FrameIndex) {
777         Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
778         BuildMI(BB, Opc, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
779       } else {
780         long offset;
781         SelectAddr(Address, Tmp1, offset);
782         BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
783       }
784       return Result;
785     }
786
787   case ISD::GlobalAddress:
788     AlphaLowering.restoreGP(BB);
789     BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
790       .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
791     return Result;
792
793   case ISD::CALL:
794     {
795       Select(N.getOperand(0));
796       
797       // The chain for this call is now lowered.
798       ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
799       
800       //grab the arguments
801       std::vector<unsigned> argvregs;
802       //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
803       for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
804         argvregs.push_back(SelectExpr(N.getOperand(i)));
805       
806       //in reg args
807       for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
808       {
809         unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18, 
810                                Alpha::R19, Alpha::R20, Alpha::R21};
811         unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18, 
812                                  Alpha::F19, Alpha::F20, Alpha::F21};
813         switch(N.getOperand(i+2).getValueType()) {
814         default: 
815           Node->dump(); 
816           N.getOperand(i).Val->dump();
817           std::cerr << "Type for " << i << " is: " << 
818             N.getOperand(i+2).getValueType() << "\n";
819           assert(0 && "Unknown value type for call");
820         case MVT::i1:
821         case MVT::i8:
822         case MVT::i16:
823         case MVT::i32:
824         case MVT::i64:
825           BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
826           break;
827         case MVT::f32:
828         case MVT::f64:
829           BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
830           break;
831         }
832       }
833       //in mem args
834       for (int i = 6, e = argvregs.size(); i < e; ++i)
835       {
836         switch(N.getOperand(i+2).getValueType()) {
837         default: 
838           Node->dump(); 
839           N.getOperand(i).Val->dump();
840           std::cerr << "Type for " << i << " is: " << 
841             N.getOperand(i+2).getValueType() << "\n";
842           assert(0 && "Unknown value type for call");
843         case MVT::i1:
844         case MVT::i8:
845         case MVT::i16:
846         case MVT::i32:
847         case MVT::i64:
848           BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
849           break;
850         case MVT::f32:
851           BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
852           break;
853         case MVT::f64:
854           BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
855           break;
856         }
857       }
858       //build the right kind of call
859       if (GlobalAddressSDNode *GASD =
860           dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) 
861       {
862         //if (GASD->getGlobal()->isExternal()) {
863           //use safe calling convention
864           AlphaLowering.restoreGP(BB);
865           BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal(),true);
866           //} else {
867           //use PC relative branch call
868           //BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
869           //}
870       } 
871       else if (ExternalSymbolSDNode *ESSDN =
872                dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) 
873       {
874         AlphaLowering.restoreGP(BB);
875         BuildMI(BB, Alpha::CALL, 0).addExternalSymbol(ESSDN->getSymbol(), true);
876       } else {
877         //no need to restore GP as we are doing an indirect call
878         Tmp1 = SelectExpr(N.getOperand(1));
879         BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
880         BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
881       }
882       
883       //push the result into a virtual register
884       
885       switch (Node->getValueType(0)) {
886       default: Node->dump(); assert(0 && "Unknown value type for call result!");
887       case MVT::Other: return notIn;
888       case MVT::i1:
889       case MVT::i8:
890       case MVT::i16:
891       case MVT::i32:
892       case MVT::i64:
893         BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
894         break;
895       case MVT::f32:
896       case MVT::f64:
897         BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
898         break;
899       }
900       return Result+N.ResNo;
901     }    
902     
903   case ISD::SIGN_EXTEND:
904     abort();
905     
906   case ISD::SIGN_EXTEND_INREG:
907     {
908       //Alpha has instructions for a bunch of signed 32 bit stuff
909       if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
910       {     
911         switch (N.getOperand(0).getOpcode()) {
912         case ISD::ADD:
913         case ISD::SUB:
914         case ISD::MUL:
915           {
916             bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
917             bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
918             //FIXME: first check for Scaled Adds and Subs!
919             if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
920                cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
921             { //Normal imm add/sub
922               Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
923               //if the value was really originally a i32, skip the up conversion
924               if (N.getOperand(0).getOperand(0).getOpcode() == ISD::SIGN_EXTEND_INREG &&
925                   dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(0).Val)
926                   ->getExtraValueType() == MVT::i32)
927                 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
928               else
929                 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
930               Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
931               BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
932             }
933             else
934             { //Normal add/sub
935               Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULLi : Alpha::SUBL);
936               //if the value was really originally a i32, skip the up conversion
937               if (N.getOperand(0).getOperand(0).getOpcode() == ISD::SIGN_EXTEND_INREG &&
938                   dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(0).Val)
939                   ->getExtraValueType() == MVT::i32)
940                 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
941               else
942                 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
943               //if the value was really originally a i32, skip the up conversion
944               if (N.getOperand(0).getOperand(1).getOpcode() == ISD::SIGN_EXTEND_INREG &&
945                   dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(1).Val)
946                   ->getExtraValueType() == MVT::i32)
947                 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
948               else
949                 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
950
951               Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
952               Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
953               BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
954             }
955             return Result;
956           }
957         default: break; //Fall Though;
958         }
959       } //Every thing else fall though too, including unhandled opcodes above
960       Tmp1 = SelectExpr(N.getOperand(0));
961       MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
962       //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
963       switch(MVN->getExtraValueType())
964       {
965       default:
966         Node->dump();
967         assert(0 && "Sign Extend InReg not there yet");
968         break;
969       case MVT::i32:
970         {
971           BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
972           break;
973         }
974       case MVT::i16:
975         BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
976         break;
977       case MVT::i8:
978         BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
979         break;
980       case MVT::i1:
981         Tmp2 = MakeReg(MVT::i64);
982         BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
983         BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::F31).addReg(Tmp2);
984         break;
985       }
986       return Result;
987     }
988   case ISD::ZERO_EXTEND_INREG:
989     {
990       Tmp1 = SelectExpr(N.getOperand(0));
991       MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
992       //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
993       switch(MVN->getExtraValueType())
994       {
995       default:
996         Node->dump();
997         assert(0 && "Zero Extend InReg not there yet");
998         break;
999       case MVT::i32: Tmp2 = 0xf0; break;
1000       case MVT::i16: Tmp2 = 0xfc; break;
1001       case MVT::i8: Tmp2 = 0xfe; break;
1002       case MVT::i1: //handle this one special
1003         BuildMI(BB, Alpha::ANDi, 2, Result).addReg(Tmp1).addImm(1);
1004         return Result;
1005       }
1006       BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1007       return Result;
1008     }
1009     
1010   case ISD::SETCC:
1011     {
1012       if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1013         if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1014           bool isConst1 = false;
1015           bool isConst2 = false;
1016           int dir;
1017           
1018           //Tmp1 = SelectExpr(N.getOperand(0));
1019           if(N.getOperand(0).getOpcode() == ISD::Constant &&
1020              cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1021             isConst1 = true;
1022           if(N.getOperand(1).getOpcode() == ISD::Constant &&
1023              cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1024             isConst2 = true;
1025
1026           switch (SetCC->getCondition()) {
1027           default: Node->dump(); assert(0 && "Unknown integer comparison!");
1028           case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
1029           case ISD::SETLT: 
1030             Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
1031           case ISD::SETLE: 
1032             Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1033           case ISD::SETGT: 
1034             Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
1035           case ISD::SETGE: 
1036             Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
1037           case ISD::SETULT: 
1038             Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1039           case ISD::SETUGT: 
1040             Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
1041           case ISD::SETULE: 
1042             Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1043           case ISD::SETUGE: 
1044             Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
1045           case ISD::SETNE: {//Handle this one special
1046             //std::cerr << "Alpha does not have a setne.\n";
1047             //abort();
1048             Tmp1 = SelectExpr(N.getOperand(0));
1049             Tmp2 = SelectExpr(N.getOperand(1));
1050             Tmp3 = MakeReg(MVT::i64);
1051             BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1052             //Remeber we have the Inv for this CC
1053             CCInvMap[N] = Tmp3;
1054             //and invert
1055             BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
1056             return Result;
1057           }
1058           }
1059           if (dir == 1) {
1060             Tmp1 = SelectExpr(N.getOperand(0));
1061             if (isConst2) {
1062               Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1063               BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1064             } else {
1065               Tmp2 = SelectExpr(N.getOperand(1));
1066               BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1067             }
1068           } else if (dir == 2) {
1069             Tmp1 = SelectExpr(N.getOperand(1));
1070             if (isConst1) {
1071               Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1072               BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1073             } else {
1074               Tmp2 = SelectExpr(N.getOperand(0));
1075               BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1076             }
1077           } else { //dir == 0
1078             if (isConst1) {
1079               Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1080               Tmp2 = SelectExpr(N.getOperand(1));
1081               BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1082             } else if (isConst2) {
1083               Tmp1 = SelectExpr(N.getOperand(0));
1084               Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1085               BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1086             } else {
1087               Tmp1 = SelectExpr(N.getOperand(0));
1088               Tmp2 = SelectExpr(N.getOperand(1));
1089               BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1090             }
1091           }
1092         } else {
1093           //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
1094           bool rev = false;
1095           bool inv = false;
1096           
1097           switch (SetCC->getCondition()) {
1098           default: Node->dump(); assert(0 && "Unknown FP comparison!");
1099           case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
1100           case ISD::SETLT: Opc = Alpha::CMPTLT; break;
1101           case ISD::SETLE: Opc = Alpha::CMPTLE; break;
1102           case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
1103           case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
1104           case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
1105           }
1106           
1107           Tmp1 = SelectExpr(N.getOperand(0));
1108           Tmp2 = SelectExpr(N.getOperand(1));
1109           //Can only compare doubles, and dag won't promote for me
1110           if (SetCC->getOperand(0).getValueType() == MVT::f32)
1111           {
1112             Tmp3 = MakeReg(MVT::f64);
1113             BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
1114             Tmp1 = Tmp3;
1115           }
1116           if (SetCC->getOperand(1).getValueType() == MVT::f32)
1117           {
1118             Tmp3 = MakeReg(MVT::f64);
1119             BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
1120             Tmp1 = Tmp2;
1121           }
1122
1123           if (rev) std::swap(Tmp1, Tmp2);
1124           Tmp3 = MakeReg(MVT::f64);
1125           //do the comparison
1126           BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1127           
1128           //now arrange for Result (int) to have a 1 or 0
1129           
1130           // Spill the FP to memory and reload it from there.
1131           unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1132           MachineFunction *F = BB->getParent();
1133           int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1134           unsigned Tmp4 = MakeReg(MVT::f64);
1135           BuildMI(BB, Alpha::CVTTQ, 1, Tmp4).addReg(Tmp3);
1136           BuildMI(BB, Alpha::STT, 3).addReg(Tmp4).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1137           unsigned Tmp5 = MakeReg(MVT::i64);
1138           BuildMI(BB, Alpha::LDQ, 2, Tmp5).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1139           
1140           //now, set result based on Tmp5
1141           //Set Tmp6 if fp cmp was false
1142           unsigned Tmp6 = MakeReg(MVT::i64);
1143           BuildMI(BB, Alpha::CMPEQ, 2, Tmp6).addReg(Tmp5).addReg(Alpha::R31);
1144           //and invert
1145           BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp6).addReg(Alpha::R31);
1146           
1147         }
1148         //       else
1149         //         {
1150         //           Node->dump();
1151         //           assert(0 && "Not a setcc in setcc");
1152         //         }
1153       }
1154       return Result;
1155     }
1156     
1157   case ISD::CopyFromReg:
1158     {
1159       // Make sure we generate both values.
1160       if (Result != notIn)
1161         ExprMap[N.getValue(1)] = notIn;   // Generate the token
1162       else
1163         Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1164         
1165       SDOperand Chain   = N.getOperand(0);
1166
1167       Select(Chain);
1168       unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1169       //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1170       BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1171       return Result;
1172     }
1173
1174     //Most of the plain arithmetic and logic share the same form, and the same 
1175     //constant immediate test
1176   case ISD::AND:
1177   case ISD::OR:
1178   case ISD::XOR:
1179   case ISD::SHL:
1180   case ISD::SRL:
1181   case ISD::SRA:
1182   case ISD::MUL:
1183     assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1184     if(N.getOperand(1).getOpcode() == ISD::Constant &&
1185        cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1186     {
1187       switch(opcode) {
1188       case ISD::AND: Opc = Alpha::ANDi; break;
1189       case ISD::OR:  Opc = Alpha::BISi; break;
1190       case ISD::XOR: Opc = Alpha::XORi; break;
1191       case ISD::SHL: Opc = Alpha::SLi; break;
1192       case ISD::SRL: Opc = Alpha::SRLi; break;
1193       case ISD::SRA: Opc = Alpha::SRAi; break;
1194       case ISD::MUL: Opc = Alpha::MULQi; break;
1195       };
1196       Tmp1 = SelectExpr(N.getOperand(0));
1197       Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1198       BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1199     } else {
1200       switch(opcode) {
1201       case ISD::AND: Opc = Alpha::AND; break;
1202       case ISD::OR:  Opc = Alpha::BIS; break;
1203       case ISD::XOR: Opc = Alpha::XOR; break;
1204       case ISD::SHL: Opc = Alpha::SL; break;
1205       case ISD::SRL: Opc = Alpha::SRL; break;
1206       case ISD::SRA: Opc = Alpha::SRA; break;
1207       case ISD::MUL: Opc = Alpha::MULQ; break;
1208       };
1209       Tmp1 = SelectExpr(N.getOperand(0));
1210       Tmp2 = SelectExpr(N.getOperand(1));
1211       BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1212     }
1213     return Result;
1214     
1215   case ISD::ADD:
1216   case ISD::SUB:
1217     {
1218       bool isAdd = opcode == ISD::ADD;
1219
1220       //FIXME: first check for Scaled Adds and Subs!
1221       if(N.getOperand(1).getOpcode() == ISD::Constant &&
1222          cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1223       { //Normal imm add/sub
1224         Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1225         Tmp1 = SelectExpr(N.getOperand(0));
1226         Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1227         BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1228       }
1229       else if(N.getOperand(1).getOpcode() == ISD::Constant &&
1230               cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
1231       { //LDA  //FIXME: expand the above condition a bit
1232         Tmp1 = SelectExpr(N.getOperand(0));
1233         Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1234         if (!isAdd)
1235           Tmp2 = -Tmp2;
1236         BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
1237       } else {
1238         //Normal add/sub
1239         Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1240         Tmp1 = SelectExpr(N.getOperand(0));
1241         Tmp2 = SelectExpr(N.getOperand(1));
1242         BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1243       }
1244       return Result;
1245     }
1246
1247   case ISD::UREM:
1248   case ISD::SREM:
1249   case ISD::SDIV:
1250   case ISD::UDIV:
1251     //FIXME: alpha really doesn't support any of these operations, 
1252     // the ops are expanded into special library calls with
1253     // special calling conventions
1254     //Restore GP because it is a call after all...
1255     switch(opcode) {
1256     case ISD::UREM: AlphaLowering.restoreGP(BB); Opc = Alpha::REMQU; break;
1257     case ISD::SREM: AlphaLowering.restoreGP(BB); Opc = Alpha::REMQ; break;
1258     case ISD::UDIV: AlphaLowering.restoreGP(BB); Opc = Alpha::DIVQU; break;
1259     case ISD::SDIV: AlphaLowering.restoreGP(BB); Opc = Alpha::DIVQ; break;
1260     }
1261     Tmp1 = SelectExpr(N.getOperand(0));
1262     Tmp2 = SelectExpr(N.getOperand(1));
1263     BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1264     return Result;
1265
1266   case ISD::FP_TO_UINT:
1267   case ISD::FP_TO_SINT:
1268     {
1269       assert (DestType == MVT::i64 && "only quads can be loaded to");
1270       MVT::ValueType SrcType = N.getOperand(0).getValueType();
1271       assert (SrcType == MVT::f32 || SrcType == MVT::f64);
1272       Tmp1 = SelectExpr(N.getOperand(0));  // Get the operand register
1273
1274       //The hard way:
1275       // Spill the integer to memory and reload it from there.
1276       unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1277       MachineFunction *F = BB->getParent();
1278       int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1279
1280       //CVTTQ STT LDQ
1281       //CVTST CVTTQ STT LDQ
1282       if (SrcType == MVT::f32)
1283       {
1284         Tmp2 = MakeReg(MVT::f64);
1285         BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1286         Tmp1 = Tmp2;
1287       }
1288       Tmp2 = MakeReg(MVT::f64);
1289       BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
1290       BuildMI(BB, Alpha::STT, 3).addReg(Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1291       BuildMI(BB, Alpha::LDQ, 2, Result).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1292       
1293       return Result;
1294     }
1295
1296     //     //  case ISD::FP_TO_UINT: 
1297  
1298   case ISD::SELECT:
1299     {
1300       Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1301       Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1302       Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1303       // Get the condition into the zero flag.
1304       BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1305       return Result;
1306     }
1307
1308   case ISD::Constant:
1309     {
1310       unsigned long val = cast<ConstantSDNode>(N)->getValue();
1311       if (val < 32000 && (long)val > -32000)
1312         BuildMI(BB, Alpha::LOAD_IMM, 1, Result).addImm(val);
1313       else {
1314         MachineConstantPool *CP = BB->getParent()->getConstantPool();
1315         ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
1316         unsigned CPI = CP->getConstantPoolIndex(C);
1317         AlphaLowering.restoreGP(BB);
1318         BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
1319       }
1320       return Result;
1321     }
1322   }
1323
1324   return 0;
1325 }
1326
1327 void ISel::Select(SDOperand N) {
1328   unsigned Tmp1, Tmp2, Opc;
1329   unsigned opcode = N.getOpcode();
1330
1331   // FIXME: Disable for our current expansion model!
1332   if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, notIn)).second)
1333     return;  // Already selected.
1334
1335   SDNode *Node = N.Val;
1336   
1337
1338   switch (opcode) {
1339
1340   default:
1341     Node->dump(); std::cerr << "\n";
1342     assert(0 && "Node not handled yet!");
1343
1344   case ISD::BRCOND: {
1345     SelectBranchCC(N);
1346     return;
1347   }
1348
1349   case ISD::BR: {
1350     MachineBasicBlock *Dest =
1351       cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1352
1353     Select(N.getOperand(0));
1354     BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1355     return;
1356   }
1357
1358   case ISD::ImplicitDef:
1359     Select(N.getOperand(0));
1360     BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
1361     return;
1362     
1363   case ISD::EntryToken: return;  // Noop
1364
1365   case ISD::TokenFactor:
1366     for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1367       Select(Node->getOperand(i));
1368     
1369     //N.Val->dump(); std::cerr << "\n";
1370     //assert(0 && "Node not handled yet!");
1371     
1372     return;
1373
1374   case ISD::CopyToReg:
1375     Select(N.getOperand(0));
1376     Tmp1 = SelectExpr(N.getOperand(1));
1377     Tmp2 = cast<RegSDNode>(N)->getReg();
1378     
1379     if (Tmp1 != Tmp2) {
1380       if (N.getOperand(1).getValueType() == MVT::f64 || 
1381           N.getOperand(1).getValueType() == MVT::f32)
1382         BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1383       else
1384         BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1385     }
1386     return;
1387
1388   case ISD::RET:
1389     switch (N.getNumOperands()) {
1390     default:
1391       std::cerr << N.getNumOperands() << "\n";
1392       for (unsigned i = 0; i < N.getNumOperands(); ++i)
1393         std::cerr << N.getOperand(i).getValueType() << "\n";
1394       Node->dump();
1395       assert(0 && "Unknown return instruction!");
1396     case 2:
1397       Select(N.getOperand(0));
1398       Tmp1 = SelectExpr(N.getOperand(1));
1399       switch (N.getOperand(1).getValueType()) {
1400       default: Node->dump(); 
1401         assert(0 && "All other types should have been promoted!!");
1402       case MVT::f64:
1403       case MVT::f32:
1404         BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1405         break;
1406       case MVT::i32:
1407       case MVT::i64:
1408         BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1409         break;
1410       }
1411       break;
1412     case 1:
1413       Select(N.getOperand(0));
1414       break;
1415     }
1416     //Tmp2 = AlphaLowering.getRetAddr();
1417     //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
1418     BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
1419     return;
1420
1421   case ISD::TRUNCSTORE: 
1422   case ISD::STORE: 
1423     {
1424       SDOperand Chain   = N.getOperand(0);
1425       SDOperand Value = N.getOperand(1);
1426       SDOperand Address = N.getOperand(2);
1427       Select(Chain);
1428
1429       Tmp1 = SelectExpr(Value); //value
1430
1431       if (opcode == ISD::STORE) {
1432         switch(Value.getValueType()) {
1433         default: assert(0 && "unknown Type in store");
1434         case MVT::i64: Opc = Alpha::STQ; break;
1435         case MVT::f64: Opc = Alpha::STT; break;
1436         case MVT::f32: Opc = Alpha::STS; break;
1437         }
1438       } else { //ISD::TRUNCSTORE
1439         switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1440         default: assert(0 && "unknown Type in store");
1441         case MVT::i1: //FIXME: DAG does not promote this load
1442         case MVT::i8: Opc = Alpha::STB; break;
1443         case MVT::i16: Opc = Alpha::STW; break;
1444         case MVT::i32: Opc = Alpha::STL; break;
1445         }
1446       }
1447
1448       if (Address.getOpcode() == ISD::GlobalAddress)
1449       {
1450         AlphaLowering.restoreGP(BB);
1451         Opc = GetSymVersion(Opc);
1452         BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1453       }
1454       else if(Address.getOpcode() == ISD::FrameIndex)
1455       {
1456         Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1457         BuildMI(BB, Opc, 3).addReg(Tmp1).addFrameIndex(Tmp2).addReg(Alpha::F31);
1458       }
1459       else
1460       {
1461         long offset;
1462         SelectAddr(Address, Tmp2, offset);
1463         BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1464       }
1465       return;
1466     }
1467
1468   case ISD::EXTLOAD:
1469   case ISD::SEXTLOAD:
1470   case ISD::ZEXTLOAD:
1471   case ISD::LOAD:
1472   case ISD::CopyFromReg:
1473   case ISD::CALL:
1474     //   case ISD::DYNAMIC_STACKALLOC:
1475     ExprMap.erase(N);
1476     SelectExpr(N);
1477     return;
1478
1479   case ISD::ADJCALLSTACKDOWN:
1480   case ISD::ADJCALLSTACKUP:
1481     Select(N.getOperand(0));
1482     Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1483     
1484     Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
1485       Alpha::ADJUSTSTACKUP;
1486     BuildMI(BB, Opc, 1).addImm(Tmp1);
1487     return;
1488   }
1489   assert(0 && "Should not be reached!");
1490 }
1491
1492
1493 /// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1494 /// into a machine code representation using pattern matching and a machine
1495 /// description file.
1496 ///
1497 FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
1498   return new ISel(TM);  
1499 }