OK, this does wonders for broken stuff
[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 "AlphaTargetMachine.h"
17 #include "AlphaISelLowering.h"
18 #include "llvm/Constants.h"                   // FIXME: REMOVE
19 #include "llvm/Function.h"
20 #include "llvm/Module.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/SelectionDAG.h"
26 #include "llvm/CodeGen/SelectionDAGISel.h"
27 #include "llvm/CodeGen/SSARegMap.h"
28 #include "llvm/Target/TargetData.h"
29 #include "llvm/Target/TargetLowering.h"
30 #include "llvm/Support/MathExtras.h"
31 #include "llvm/ADT/Statistic.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/CommandLine.h"
34 #include <set>
35 #include <algorithm>
36 using namespace llvm;
37
38 namespace llvm {
39   cl::opt<bool> EnableAlphaIDIV("enable-alpha-intfpdiv",
40     cl::desc("Use the FP div instruction for integer div when possible"),
41                              cl::Hidden);
42   cl::opt<bool> EnableAlphaCount("enable-alpha-count",
43     cl::desc("Print estimates on live ins and outs"),
44     cl::Hidden);
45   cl::opt<bool> EnableAlphaLSMark("enable-alpha-lsmark",
46     cl::desc("Emit symbols to correlate Mem ops to LLVM Values"),
47     cl::Hidden);
48 }
49
50 namespace {
51
52 //===--------------------------------------------------------------------===//
53 /// ISel - Alpha specific code to select Alpha machine instructions for
54 /// SelectionDAG operations.
55 //===--------------------------------------------------------------------===//
56 class AlphaISel : public SelectionDAGISel {
57
58   /// AlphaLowering - This object fully describes how to lower LLVM code to an
59   /// Alpha-specific SelectionDAG.
60   AlphaTargetLowering AlphaLowering;
61
62   SelectionDAG *ISelDAG;  // Hack to support us having a dag->dag transform
63                           // for sdiv and udiv until it is put into the future
64                           // dag combiner.
65
66   /// ExprMap - As shared expressions are codegen'd, we keep track of which
67   /// vreg the value is produced in, so we only emit one copy of each compiled
68   /// tree.
69   static const unsigned notIn = (unsigned)(-1);
70   std::map<SDOperand, unsigned> ExprMap;
71
72   //CCInvMap sometimes (SetNE) we have the inverse CC code for free
73   std::map<SDOperand, unsigned> CCInvMap;
74
75   int count_ins;
76   int count_outs;
77   bool has_sym;
78   int max_depth;
79
80 public:
81   AlphaISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering),
82     AlphaLowering(TM)
83   {}
84
85     virtual const char *getPassName() const {
86       return "Alpha Pattern Instruction Selection";
87     } 
88
89   /// InstructionSelectBasicBlock - This callback is invoked by
90   /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
91   virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
92     DEBUG(BB->dump());
93     count_ins = 0;
94     count_outs = 0;
95     max_depth = 0;
96     has_sym = false;
97
98     // Codegen the basic block.
99     ISelDAG = &DAG;
100     max_depth = DAG.getRoot().getNodeDepth();
101     Select(DAG.getRoot());
102
103     if(has_sym)
104       ++count_ins;
105     if(EnableAlphaCount)
106       std::cerr << "COUNT: "
107                 << BB->getParent()->getFunction ()->getName() << " "
108                 << BB->getNumber() << " "
109                 << max_depth << " "
110                 << count_ins << " "
111                 << count_outs << "\n";
112
113     // Clear state used for selection.
114     ExprMap.clear();
115     CCInvMap.clear();
116   }
117
118   unsigned SelectExpr(SDOperand N);
119   void Select(SDOperand N);
120
121   void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
122   void SelectBranchCC(SDOperand N);
123   void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
124   void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
125   //returns whether the sense of the comparison was inverted
126   bool SelectFPSetCC(SDOperand N, unsigned dst);
127
128   // dag -> dag expanders for integer divide by constant
129   SDOperand BuildSDIVSequence(SDOperand N);
130   SDOperand BuildUDIVSequence(SDOperand N);
131
132 };
133 }
134
135 static bool isSIntImmediate(SDOperand N, int64_t& Imm) {
136   // test for constant
137   if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
138     // retrieve value
139     Imm = CN->getSignExtended();
140     // passes muster
141     return true;
142   }
143   // not a constant
144   return false;
145 }
146
147 // isSIntImmediateBounded - This method tests to see if a constant operand
148 // bounded s.t. low <= Imm <= high
149 // If so Imm will receive the 64 bit value.
150 static bool isSIntImmediateBounded(SDOperand N, int64_t& Imm, 
151                                    int64_t low, int64_t high) {
152   if (isSIntImmediate(N, Imm) && Imm <= high && Imm >= low)
153     return true;
154   return false;
155 }
156 static bool isUIntImmediate(SDOperand N, uint64_t& Imm) {
157   // test for constant
158   if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
159     // retrieve value
160     Imm = (uint64_t)CN->getValue();
161     // passes muster
162     return true;
163   }
164   // not a constant
165   return false;
166 }
167
168 static bool isUIntImmediateBounded(SDOperand N, uint64_t& Imm, 
169                                    uint64_t low, uint64_t high) {
170   if (isUIntImmediate(N, Imm) && Imm <= high && Imm >= low)
171     return true;
172   return false;
173 }
174
175 static void getValueInfo(const Value* v, int& type, int& fun, int& offset)
176 {
177   fun = type = offset = 0;
178   if (v == NULL) {
179     type = 0;
180   } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(v)) {
181     type = 1;
182     const Module* M = GV->getParent();
183     for(Module::const_global_iterator ii = M->global_begin(); &*ii != GV; ++ii)
184       ++offset;
185   } else if (const Argument* Arg = dyn_cast<Argument>(v)) {
186     type = 2;
187     const Function* F = Arg->getParent();
188     const Module* M = F->getParent();
189     for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
190       ++fun;
191     for(Function::const_arg_iterator ii = F->arg_begin(); &*ii != Arg; ++ii)
192       ++offset;
193   } else if (const Instruction* I = dyn_cast<Instruction>(v)) {
194     assert(dyn_cast<PointerType>(I->getType()));
195     type = 3;
196     const BasicBlock* bb = I->getParent();
197     const Function* F = bb->getParent();
198     const Module* M = F->getParent();
199     for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
200       ++fun;
201     for(Function::const_iterator ii = F->begin(); &*ii != bb; ++ii)
202       offset += ii->size();
203     for(BasicBlock::const_iterator ii = bb->begin(); &*ii != I; ++ii)
204       ++offset;
205   } else if (const Constant* C = dyn_cast<Constant>(v)) {
206     //Don't know how to look these up yet
207     type = 0;
208   } else {
209     assert(0 && "Error in value marking");
210   }
211   //type = 4: register spilling
212   //type = 5: global address loading or constant loading
213 }
214
215 static int getUID()
216 {
217   static int id = 0;
218   return ++id;
219 }
220
221 //Factorize a number using the list of constants
222 static bool factorize(int v[], int res[], int size, uint64_t c)
223 {
224   bool cont = true;
225   while (c != 1 && cont)
226   {
227     cont = false;
228     for(int i = 0; i < size; ++i)
229     {
230       if (c % v[i] == 0)
231       {
232         c /= v[i];
233         ++res[i];
234         cont=true;
235       }
236     }
237   }
238   return c == 1;
239 }
240
241
242 //These describe LDAx
243 static const int IMM_LOW  = -32768;
244 static const int IMM_HIGH = 32767;
245 static const int IMM_MULT = 65536;
246
247 static long getUpper16(long l)
248 {
249   long y = l / IMM_MULT;
250   if (l % IMM_MULT > IMM_HIGH)
251     ++y;
252   return y;
253 }
254
255 static long getLower16(long l)
256 {
257   long h = getUpper16(l);
258   return l - h * IMM_MULT;
259 }
260
261 static unsigned GetRelVersion(unsigned opcode)
262 {
263   switch (opcode) {
264   default: assert(0 && "unknown load or store"); return 0;
265   case Alpha::LDQ: return Alpha::LDQr;
266   case Alpha::LDS: return Alpha::LDSr;
267   case Alpha::LDT: return Alpha::LDTr;
268   case Alpha::LDL: return Alpha::LDLr;
269   case Alpha::LDBU: return Alpha::LDBUr;
270   case Alpha::LDWU: return Alpha::LDWUr;
271   case Alpha::STB: return Alpha::STBr;
272   case Alpha::STW: return Alpha::STWr;
273   case Alpha::STL: return Alpha::STLr;
274   case Alpha::STQ: return Alpha::STQr;
275   case Alpha::STS: return Alpha::STSr;
276   case Alpha::STT: return Alpha::STTr;
277
278   }
279 }
280
281 void AlphaISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
282 {
283   unsigned Opc = Alpha::WTF;
284   if (TLI.getTargetMachine().getSubtarget<AlphaSubtarget>().hasF2I()) {
285     Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
286     BuildMI(BB, Opc, 1, dst).addReg(src).addReg(Alpha::F31);
287   } else {
288     //The hard way:
289     // Spill the integer to memory and reload it from there.
290     unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
291     MachineFunction *F = BB->getParent();
292     int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
293
294     if (EnableAlphaLSMark)
295       BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
296         .addImm(getUID());
297     Opc = isDouble ? Alpha::STT : Alpha::STS;
298     BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
299
300     if (EnableAlphaLSMark)
301       BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
302         .addImm(getUID());
303     Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
304     BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
305   }
306 }
307
308 void AlphaISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
309 {
310   unsigned Opc = Alpha::WTF;
311   if (TLI.getTargetMachine().getSubtarget<AlphaSubtarget>().hasF2I()) {
312     Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
313     BuildMI(BB, Opc, 1, dst).addReg(src).addReg(Alpha::R31);
314   } else {
315     //The hard way:
316     // Spill the integer to memory and reload it from there.
317     unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
318     MachineFunction *F = BB->getParent();
319     int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
320
321     if (EnableAlphaLSMark)
322       BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
323         .addImm(getUID());
324     Opc = isDouble ? Alpha::STQ : Alpha::STL;
325     BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
326
327     if (EnableAlphaLSMark)
328       BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
329         .addImm(getUID());
330     Opc = isDouble ? Alpha::LDT : Alpha::LDS;
331     BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
332   }
333 }
334
335 bool AlphaISel::SelectFPSetCC(SDOperand N, unsigned dst)
336 {
337   SDNode *SetCC = N.Val;
338   unsigned Tmp1, Tmp2, Tmp3, Opc = Alpha::WTF;
339   ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get();
340   bool rev = false;
341   bool inv = false;
342
343   switch (CC) {
344   default: SetCC->dump(); assert(0 && "Unknown FP comparison!");
345   case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
346   case ISD::SETLT: Opc = Alpha::CMPTLT; break;
347   case ISD::SETLE: Opc = Alpha::CMPTLE; break;
348   case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
349   case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
350   case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
351   }
352
353   ConstantFPSDNode *CN;
354   if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
355       && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
356     Tmp1 = Alpha::F31;
357   else
358     Tmp1 = SelectExpr(N.getOperand(0));
359
360   if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
361       && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
362     Tmp2 = Alpha::F31;
363   else
364     Tmp2 = SelectExpr(N.getOperand(1));
365
366   //Can only compare doubles, and dag won't promote for me
367   if (SetCC->getOperand(0).getValueType() == MVT::f32)
368       assert(0 && "Setcc On float?\n");
369   if (SetCC->getOperand(1).getValueType() == MVT::f32)
370     assert (0 && "Setcc On float?\n");
371
372   if (rev) std::swap(Tmp1, Tmp2);
373   //do the comparison
374   BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
375   return inv;
376 }
377
378 //Check to see if the load is a constant offset from a base register
379 void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
380 {
381   unsigned opcode = N.getOpcode();
382   if (opcode == ISD::ADD && N.getOperand(1).getOpcode() == ISD::Constant &&
383       cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
384   { //Normal imm add
385     Reg = SelectExpr(N.getOperand(0));
386     offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
387     return;
388   }
389   Reg = SelectExpr(N);
390   offset = 0;
391   return;
392 }
393
394 void AlphaISel::SelectBranchCC(SDOperand N)
395 {
396   assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
397   MachineBasicBlock *Dest =
398     cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
399   unsigned Opc = Alpha::WTF;
400
401   Select(N.getOperand(0));  //chain
402   SDOperand CC = N.getOperand(1);
403
404   if (CC.getOpcode() == ISD::SETCC)
405   {
406     ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
407     if (MVT::isInteger(CC.getOperand(0).getValueType())) {
408       //Dropping the CC is only useful if we are comparing to 0
409       bool RightZero = CC.getOperand(1).getOpcode() == ISD::Constant &&
410         cast<ConstantSDNode>(CC.getOperand(1))->getValue() == 0;
411       bool isNE = false;
412
413       //Fix up CC
414       if(cCode == ISD::SETNE)
415         isNE = true;
416
417       if (RightZero) {
418         switch (cCode) {
419         default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
420         case ISD::SETEQ:  Opc = Alpha::BEQ; break;
421         case ISD::SETLT:  Opc = Alpha::BLT; break;
422         case ISD::SETLE:  Opc = Alpha::BLE; break;
423         case ISD::SETGT:  Opc = Alpha::BGT; break;
424         case ISD::SETGE:  Opc = Alpha::BGE; break;
425         case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
426         case ISD::SETUGT: Opc = Alpha::BNE; break;
427         //Technically you could have this CC
428         case ISD::SETULE: Opc = Alpha::BEQ; break;
429         case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
430         case ISD::SETNE:  Opc = Alpha::BNE; break;
431         }
432         unsigned Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
433         BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
434         return;
435       } else {
436         unsigned Tmp1 = SelectExpr(CC);
437         if (isNE)
438           BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
439         else
440           BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
441         return;
442       }
443     } else { //FP
444       //Any comparison between 2 values should be codegened as an folded
445       //branch, as moving CC to the integer register is very expensive
446       //for a cmp b: c = a - b;
447       //a = b: c = 0
448       //a < b: c < 0
449       //a > b: c > 0
450
451       bool invTest = false;
452       unsigned Tmp3;
453
454       ConstantFPSDNode *CN;
455       if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
456           && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
457         Tmp3 = SelectExpr(CC.getOperand(0));
458       else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
459           && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
460       {
461         Tmp3 = SelectExpr(CC.getOperand(1));
462         invTest = true;
463       }
464       else
465       {
466         unsigned Tmp1 = SelectExpr(CC.getOperand(0));
467         unsigned Tmp2 = SelectExpr(CC.getOperand(1));
468         bool isD = CC.getOperand(0).getValueType() == MVT::f64;
469         Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
470         BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
471           .addReg(Tmp1).addReg(Tmp2);
472       }
473
474       switch (cCode) {
475       default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
476       case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
477       case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
478       case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
479       case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
480       case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
481       case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
482       }
483       BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
484       return;
485     }
486     abort(); //Should never be reached
487   } else {
488     //Giveup and do the stupid thing
489     unsigned Tmp1 = SelectExpr(CC);
490     BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
491     return;
492   }
493   abort(); //Should never be reached
494 }
495
496 unsigned AlphaISel::SelectExpr(SDOperand N) {
497   unsigned Result;
498   unsigned Tmp1, Tmp2 = 0, Tmp3;
499   unsigned Opc = 0;
500   unsigned opcode = N.getOpcode();
501   int64_t SImm = 0;
502   uint64_t UImm;
503
504   SDNode *Node = N.Val;
505   MVT::ValueType DestType = N.getValueType();
506   bool isFP = DestType == MVT::f64 || DestType == MVT::f32;
507
508   unsigned &Reg = ExprMap[N];
509   if (Reg) return Reg;
510
511   switch(N.getOpcode()) {
512   default:
513     Reg = Result = (N.getValueType() != MVT::Other) ?
514       MakeReg(N.getValueType()) : notIn;
515       break;
516   case ISD::AssertSext:
517   case ISD::AssertZext:
518     return Reg = SelectExpr(N.getOperand(0));
519   case ISD::CALL:
520   case ISD::TAILCALL:
521     // If this is a call instruction, make sure to prepare ALL of the result
522     // values as well as the chain.
523     if (Node->getNumValues() == 1)
524       Reg = Result = notIn;  // Void call, just a chain.
525     else {
526       Result = MakeReg(Node->getValueType(0));
527       ExprMap[N.getValue(0)] = Result;
528       for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
529         ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
530       ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
531     }
532     break;
533   }
534
535   switch (opcode) {
536   default:
537     Node->dump();
538     assert(0 && "Node not handled!\n");
539
540   case ISD::READCYCLECOUNTER:
541     Select(N.getOperand(0)); //Select chain
542     if (Result != notIn)
543       ExprMap[N.getValue(1)] = notIn;   // Generate the token
544     else
545       Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
546
547     BuildMI(BB, Alpha::RPCC, 1, Result).addReg(Alpha::R31);
548     return Result;
549
550   case ISD::CTPOP:
551   case ISD::CTTZ:
552   case ISD::CTLZ:
553     Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
554     (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
555     Tmp1 = SelectExpr(N.getOperand(0));
556     BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
557     return Result;
558
559   case ISD::MULHU:
560     Tmp1 = SelectExpr(N.getOperand(0));
561     Tmp2 = SelectExpr(N.getOperand(1));
562     BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
563     return Result;
564   case ISD::MULHS:
565     {
566       //MULHU - Ra<63>*Rb - Rb<63>*Ra
567       Tmp1 = SelectExpr(N.getOperand(0));
568       Tmp2 = SelectExpr(N.getOperand(1));
569       Tmp3 = MakeReg(MVT::i64);
570       BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
571       unsigned V1 = MakeReg(MVT::i64);
572       unsigned V2 = MakeReg(MVT::i64);
573       BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31)
574         .addReg(Tmp1);
575       BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31)
576         .addReg(Tmp2);
577       unsigned IRes = MakeReg(MVT::i64);
578       BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
579       BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
580       return Result;
581     }
582   case ISD::UNDEF: {
583     Opc = isFP ? (DestType == MVT::f32 ? Alpha::IDEF_F32 : Alpha::IDEF_F64) 
584       : Alpha::IDEF_I;
585     BuildMI(BB, Opc, 0, Result);
586     return Result;
587   }
588
589   case ISD::DYNAMIC_STACKALLOC:
590     // Generate both result values.
591     if (Result != notIn)
592       ExprMap[N.getValue(1)] = notIn;   // Generate the token
593     else
594       Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
595
596     // FIXME: We are currently ignoring the requested alignment for handling
597     // greater than the stack alignment.  This will need to be revisited at some
598     // point.  Align = N.getOperand(2);
599
600     if (!isa<ConstantSDNode>(N.getOperand(2)) ||
601         cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
602       std::cerr << "Cannot allocate stack object with greater alignment than"
603                 << " the stack alignment yet!";
604       abort();
605     }
606
607     Select(N.getOperand(0));
608     if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 32767))
609       BuildMI(BB, Alpha::LDA, 2, Alpha::R30).addImm(-SImm).addReg(Alpha::R30);
610     else {
611       Tmp1 = SelectExpr(N.getOperand(1));
612       // Subtract size from stack pointer, thereby allocating some space.
613       BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
614     }
615
616     // Put a pointer to the space into the result register, by copying the stack
617     // pointer.
618     BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
619     return Result;
620
621   case ISD::ConstantPool:
622     Tmp1 = BB->getParent()->getConstantPool()->
623        getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
624     AlphaLowering.restoreGP(BB);
625     Tmp2 = MakeReg(MVT::i64);
626     BuildMI(BB, Alpha::LDAHr, 2, Tmp2).addConstantPoolIndex(Tmp1)
627       .addReg(Alpha::R29);
628     BuildMI(BB, Alpha::LDAr, 2, Result).addConstantPoolIndex(Tmp1)
629       .addReg(Tmp2);
630     return Result;
631
632   case ISD::FrameIndex:
633     BuildMI(BB, Alpha::LDA, 2, Result)
634       .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
635       .addReg(Alpha::F31);
636     return Result;
637
638   case ISD::EXTLOAD:
639   case ISD::ZEXTLOAD:
640   case ISD::SEXTLOAD:
641   case ISD::LOAD:
642     {
643       // Make sure we generate both values.
644       if (Result != notIn)
645         ExprMap[N.getValue(1)] = notIn;   // Generate the token
646       else
647         Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
648
649       SDOperand Chain   = N.getOperand(0);
650       SDOperand Address = N.getOperand(1);
651       Select(Chain);
652
653       bool fpext = true;
654
655       if (opcode == ISD::LOAD)
656         switch (Node->getValueType(0)) {
657         default: Node->dump(); assert(0 && "Bad load!");
658         case MVT::i64: Opc = Alpha::LDQ; break;
659         case MVT::f64: Opc = Alpha::LDT; break;
660         case MVT::f32: Opc = Alpha::LDS; break;
661         }
662       else
663         switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
664         default: Node->dump(); assert(0 && "Bad sign extend!");
665         case MVT::i32: Opc = Alpha::LDL;
666           assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
667         case MVT::i16: Opc = Alpha::LDWU;
668           assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
669         case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
670         case MVT::i8: Opc = Alpha::LDBU;
671           assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
672         }
673
674       int i, j, k;
675       if (EnableAlphaLSMark)
676         getValueInfo(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue(),
677                      i, j, k);
678
679       GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
680       if (GASD && !GASD->getGlobal()->isExternal()) {
681         Tmp1 = MakeReg(MVT::i64);
682         AlphaLowering.restoreGP(BB);
683         BuildMI(BB, Alpha::LDAHr, 2, Tmp1)
684           .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
685         if (EnableAlphaLSMark)
686           BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
687             .addImm(getUID());
688         BuildMI(BB, GetRelVersion(Opc), 2, Result)
689           .addGlobalAddress(GASD->getGlobal()).addReg(Tmp1);
690       } else if (ConstantPoolSDNode *CP =
691                      dyn_cast<ConstantPoolSDNode>(Address)) {
692         unsigned CPIdx = BB->getParent()->getConstantPool()->
693              getConstantPoolIndex(CP->get());
694         AlphaLowering.restoreGP(BB);
695         has_sym = true;
696         Tmp1 = MakeReg(MVT::i64);
697         BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPIdx)
698           .addReg(Alpha::R29);
699         if (EnableAlphaLSMark)
700           BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
701             .addImm(getUID());
702         BuildMI(BB, GetRelVersion(Opc), 2, Result)
703           .addConstantPoolIndex(CPIdx).addReg(Tmp1);
704       } else if(Address.getOpcode() == ISD::FrameIndex) {
705         if (EnableAlphaLSMark)
706           BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
707             .addImm(getUID());
708         BuildMI(BB, Opc, 2, Result)
709           .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
710           .addReg(Alpha::F31);
711       } else {
712         long offset;
713         SelectAddr(Address, Tmp1, offset);
714         if (EnableAlphaLSMark)
715           BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
716             .addImm(getUID());
717         BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
718       }
719       return Result;
720     }
721
722   case ISD::GlobalAddress:
723     AlphaLowering.restoreGP(BB);
724     has_sym = true;
725
726     Reg = Result = MakeReg(MVT::i64);
727
728     if (EnableAlphaLSMark)
729       BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
730         .addImm(getUID());
731
732     BuildMI(BB, Alpha::LDQl, 2, Result)
733       .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal())
734       .addReg(Alpha::R29);
735     return Result;
736
737   case ISD::ExternalSymbol:
738     AlphaLowering.restoreGP(BB);
739     has_sym = true;
740
741     Reg = Result = MakeReg(MVT::i64);
742
743     if (EnableAlphaLSMark)
744       BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
745         .addImm(getUID());
746
747     BuildMI(BB, Alpha::LDQl, 2, Result)
748       .addExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol())
749       .addReg(Alpha::R29);
750     return Result;
751
752   case ISD::TAILCALL:
753   case ISD::CALL:
754     {
755       Select(N.getOperand(0));
756
757       // The chain for this call is now lowered.
758       ExprMap[N.getValue(Node->getNumValues()-1)] = notIn;
759
760       //grab the arguments
761       std::vector<unsigned> argvregs;
762       //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
763       for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
764         argvregs.push_back(SelectExpr(N.getOperand(i)));
765
766       //in reg args
767       for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
768       {
769         unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
770                                Alpha::R19, Alpha::R20, Alpha::R21};
771         unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
772                                  Alpha::F19, Alpha::F20, Alpha::F21};
773         switch(N.getOperand(i+2).getValueType()) {
774         default:
775           Node->dump();
776           N.getOperand(i).Val->dump();
777           std::cerr << "Type for " << i << " is: " <<
778             N.getOperand(i+2).getValueType() << "\n";
779           assert(0 && "Unknown value type for call");
780         case MVT::i1:
781         case MVT::i8:
782         case MVT::i16:
783         case MVT::i32:
784         case MVT::i64:
785           BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i])
786             .addReg(argvregs[i]);
787           break;
788         case MVT::f32:
789           BuildMI(BB, Alpha::CPYSS, 2, args_float[i]).addReg(argvregs[i])
790             .addReg(argvregs[i]);
791           break;
792         case MVT::f64:
793           BuildMI(BB, Alpha::CPYST, 2, args_float[i]).addReg(argvregs[i])
794             .addReg(argvregs[i]);
795           break;
796         }
797       }
798       //in mem args
799       for (int i = 6, e = argvregs.size(); i < e; ++i)
800       {
801         switch(N.getOperand(i+2).getValueType()) {
802         default:
803           Node->dump();
804           N.getOperand(i).Val->dump();
805           std::cerr << "Type for " << i << " is: " <<
806             N.getOperand(i+2).getValueType() << "\n";
807           assert(0 && "Unknown value type for call");
808         case MVT::i1:
809         case MVT::i8:
810         case MVT::i16:
811         case MVT::i32:
812         case MVT::i64:
813           BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
814             .addReg(Alpha::R30);
815           break;
816         case MVT::f32:
817           BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
818             .addReg(Alpha::R30);
819           break;
820         case MVT::f64:
821           BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
822             .addReg(Alpha::R30);
823           break;
824         }
825       }
826       //build the right kind of call
827       GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(N.getOperand(1));
828       if (GASD && !GASD->getGlobal()->isExternal()) {
829         //use PC relative branch call
830         AlphaLowering.restoreGP(BB);
831         BuildMI(BB, Alpha::BSR, 1, Alpha::R26)
832           .addGlobalAddress(GASD->getGlobal(),true);
833       } else {
834         //no need to restore GP as we are doing an indirect call
835         Tmp1 = SelectExpr(N.getOperand(1));
836         BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
837         BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
838       }
839
840       //push the result into a virtual register
841
842       switch (Node->getValueType(0)) {
843       default: Node->dump(); assert(0 && "Unknown value type for call result!");
844       case MVT::Other: return notIn;
845       case MVT::i64:
846         BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
847         break;
848       case MVT::f32:
849         BuildMI(BB, Alpha::CPYSS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
850         break;
851        case MVT::f64:
852         BuildMI(BB, Alpha::CPYST, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
853         break;
854       }
855       return Result+N.ResNo;
856     }
857
858   case ISD::SIGN_EXTEND_INREG:
859     {
860       //do SDIV opt for all levels of ints if not dividing by a constant
861       if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
862           && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
863       {
864         unsigned Tmp4 = MakeReg(MVT::f64);
865         unsigned Tmp5 = MakeReg(MVT::f64);
866         unsigned Tmp6 = MakeReg(MVT::f64);
867         unsigned Tmp7 = MakeReg(MVT::f64);
868         unsigned Tmp8 = MakeReg(MVT::f64);
869         unsigned Tmp9 = MakeReg(MVT::f64);
870
871         Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
872         Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
873         MoveInt2FP(Tmp1, Tmp4, true);
874         MoveInt2FP(Tmp2, Tmp5, true);
875         BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Alpha::F31).addReg(Tmp4);
876         BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Alpha::F31).addReg(Tmp5);
877         BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
878         BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Alpha::F31).addReg(Tmp8);
879         MoveFP2Int(Tmp9, Result, true);
880         return Result;
881       }
882
883       //Alpha has instructions for a bunch of signed 32 bit stuff
884       if(cast<VTSDNode>(Node->getOperand(1))->getVT() == MVT::i32) {
885         switch (N.getOperand(0).getOpcode()) {
886         case ISD::ADD:
887         case ISD::SUB:
888         case ISD::MUL:
889           {
890             bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
891             bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
892             //FIXME: first check for Scaled Adds and Subs!
893             if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
894                isSIntImmediateBounded(N.getOperand(0).getOperand(0).getOperand(1), SImm, 2, 3))
895             {
896               bool use4 = SImm == 2;
897               Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
898               Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
899               BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
900                       2,Result).addReg(Tmp1).addReg(Tmp2);
901             }
902             else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
903                     isSIntImmediateBounded(N.getOperand(0).getOperand(1).getOperand(1), SImm, 2, 3))
904             {
905               bool use4 = SImm == 2;
906               Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
907               Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
908               BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
909             }
910             else if(isSIntImmediateBounded(N.getOperand(0).getOperand(1), SImm, 0, 255)) 
911             { //Normal imm add/sub
912               Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
913               Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
914               BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
915             }
916             else if(!isMul && isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
917                     (((SImm << 32) >> 32) >= -255) && (((SImm << 32) >> 32) <= 0))
918             { //handle canonicalization
919               Opc = isAdd ? Alpha::SUBLi : Alpha::ADDLi;
920               Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
921               SImm = 0 - ((SImm << 32) >> 32);
922               assert(SImm >= 0 && SImm <= 255);
923               BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
924             }
925             else
926             { //Normal add/sub
927               Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
928               Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
929               Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
930               BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
931             }
932             return Result;
933           }
934         default: break; //Fall Though;
935         }
936       } //Every thing else fall though too, including unhandled opcodes above
937       Tmp1 = SelectExpr(N.getOperand(0));
938       //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
939       switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
940       default:
941         Node->dump();
942         assert(0 && "Sign Extend InReg not there yet");
943         break;
944       case MVT::i32:
945         {
946           BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
947           break;
948         }
949       case MVT::i16:
950         BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
951         break;
952       case MVT::i8:
953         BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
954         break;
955       case MVT::i1:
956         Tmp2 = MakeReg(MVT::i64);
957         BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
958         BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
959         break;
960       }
961       return Result;
962     }
963
964   case ISD::SETCC:
965     {
966       ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(2))->get();
967       if (MVT::isInteger(N.getOperand(0).getValueType())) {
968         bool isConst = false;
969         int dir;
970
971         //Tmp1 = SelectExpr(N.getOperand(0));
972         if(isSIntImmediate(N.getOperand(1), SImm) && SImm <= 255 && SImm >= 0)
973           isConst = true;
974
975         switch (CC) {
976         default: Node->dump(); assert(0 && "Unknown integer comparison!");
977         case ISD::SETEQ:
978           Opc = isConst ? Alpha::CMPEQi : Alpha::CMPEQ; dir=1; break;
979         case ISD::SETLT:
980           Opc = isConst ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
981         case ISD::SETLE:
982           Opc = isConst ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
983         case ISD::SETGT: Opc = Alpha::CMPLT; dir = 2; break;
984         case ISD::SETGE: Opc = Alpha::CMPLE; dir = 2; break;
985         case ISD::SETULT:
986           Opc = isConst ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
987         case ISD::SETUGT: Opc = Alpha::CMPULT; dir = 2; break;
988         case ISD::SETULE:
989           Opc = isConst ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
990         case ISD::SETUGE: Opc = Alpha::CMPULE; dir = 2; break;
991         case ISD::SETNE: {//Handle this one special
992           //std::cerr << "Alpha does not have a setne.\n";
993           //abort();
994           Tmp1 = SelectExpr(N.getOperand(0));
995           Tmp2 = SelectExpr(N.getOperand(1));
996           Tmp3 = MakeReg(MVT::i64);
997           BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
998           //Remeber we have the Inv for this CC
999           CCInvMap[N] = Tmp3;
1000           //and invert
1001           BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
1002           return Result;
1003         }
1004         }
1005         if (dir == 1) {
1006           Tmp1 = SelectExpr(N.getOperand(0));
1007           if (isConst) {
1008             BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
1009           } else {
1010             Tmp2 = SelectExpr(N.getOperand(1));
1011             BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1012           }
1013         } else { //if (dir == 2) {
1014           Tmp1 = SelectExpr(N.getOperand(1));
1015           Tmp2 = SelectExpr(N.getOperand(0));
1016           BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1017         }
1018       } else {
1019         //do the comparison
1020         Tmp1 = MakeReg(MVT::f64);
1021         bool inv = SelectFPSetCC(N, Tmp1);
1022
1023         //now arrange for Result (int) to have a 1 or 0
1024         Tmp2 = MakeReg(MVT::i64);
1025         BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
1026         Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
1027         BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
1028       }
1029       return Result;
1030     }
1031
1032   case ISD::CopyFromReg:
1033     {
1034       ++count_ins;
1035
1036       // Make sure we generate both values.
1037       if (Result != notIn)
1038         ExprMap[N.getValue(1)] = notIn;   // Generate the token
1039       else
1040         Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1041
1042       SDOperand Chain   = N.getOperand(0);
1043
1044       Select(Chain);
1045       unsigned r = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1046       //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1047       switch(N.getValue(0).getValueType()) {
1048       case MVT::f32:
1049         BuildMI(BB, Alpha::CPYSS, 2, Result).addReg(r).addReg(r);
1050         break;
1051       case MVT::f64:
1052         BuildMI(BB, Alpha::CPYST, 2, Result).addReg(r).addReg(r);
1053         break;
1054       default:
1055         BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1056         break;
1057       }
1058       return Result;
1059     }
1060
1061     //Most of the plain arithmetic and logic share the same form, and the same
1062     //constant immediate test
1063   case ISD::XOR:
1064     //Match Not
1065     if (isSIntImmediate(N.getOperand(1), SImm) && SImm == -1) {
1066       Tmp1 = SelectExpr(N.getOperand(0));
1067       BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1068       return Result;
1069     }
1070     //Fall through
1071   case ISD::AND:
1072     //handle zap
1073     if (opcode == ISD::AND && isUIntImmediate(N.getOperand(1), UImm))
1074     {
1075       unsigned int build = 0;
1076       for(int i = 0; i < 8; ++i)
1077       {
1078         if ((UImm & 0x00FF) == 0x00FF)
1079           build |= 1 << i;
1080         else if ((UImm & 0x00FF) != 0)
1081         { build = 0; break; }
1082         UImm >>= 8;
1083       }
1084       if (build)
1085       {
1086         Tmp1 = SelectExpr(N.getOperand(0));
1087         BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1088         return Result;
1089       }
1090     }
1091   case ISD::OR:
1092     //Check operand(0) == Not
1093     if (N.getOperand(0).getOpcode() == ISD::XOR &&
1094         isSIntImmediate(N.getOperand(0).getOperand(1), SImm) && SImm == -1) {
1095       switch(opcode) {
1096         case ISD::AND: Opc = Alpha::BIC; break;
1097         case ISD::OR:  Opc = Alpha::ORNOT; break;
1098         case ISD::XOR: Opc = Alpha::EQV; break;
1099       }
1100       Tmp1 = SelectExpr(N.getOperand(1));
1101       Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1102       BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1103       return Result;
1104     }
1105     //Check operand(1) == Not
1106     if (N.getOperand(1).getOpcode() == ISD::XOR &&
1107         isSIntImmediate(N.getOperand(1).getOperand(1), SImm) && SImm == -1) {
1108       switch(opcode) {
1109         case ISD::AND: Opc = Alpha::BIC; break;
1110         case ISD::OR:  Opc = Alpha::ORNOT; break;
1111         case ISD::XOR: Opc = Alpha::EQV; break;
1112       }
1113       Tmp1 = SelectExpr(N.getOperand(0));
1114       Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1115       BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1116       return Result;
1117     }
1118     //Fall through
1119   case ISD::SHL:
1120   case ISD::SRL:
1121   case ISD::SRA:
1122   case ISD::MUL:
1123     if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255)) {
1124       switch(opcode) {
1125       case ISD::AND: Opc = Alpha::ANDi; break;
1126       case ISD::OR:  Opc = Alpha::BISi; break;
1127       case ISD::XOR: Opc = Alpha::XORi; break;
1128       case ISD::SHL: Opc = Alpha::SLi; break;
1129       case ISD::SRL: Opc = Alpha::SRLi; break;
1130       case ISD::SRA: Opc = Alpha::SRAi; break;
1131       case ISD::MUL: Opc = Alpha::MULQi; break;
1132       };
1133       Tmp1 = SelectExpr(N.getOperand(0));
1134       BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
1135     } else {
1136       switch(opcode) {
1137       case ISD::AND: Opc = Alpha::AND; break;
1138       case ISD::OR:  Opc = Alpha::BIS; break;
1139       case ISD::XOR: Opc = Alpha::XOR; break;
1140       case ISD::SHL: Opc = Alpha::SL; break;
1141       case ISD::SRL: Opc = Alpha::SRL; break;
1142       case ISD::SRA: Opc = Alpha::SRA; break;
1143       case ISD::MUL: Opc = Alpha::MULQ; break;
1144       };
1145       Tmp1 = SelectExpr(N.getOperand(0));
1146       Tmp2 = SelectExpr(N.getOperand(1));
1147       BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1148     }
1149     return Result;
1150
1151   case ISD::ADD:
1152   case ISD::SUB:
1153     {
1154       bool isAdd = opcode == ISD::ADD;
1155
1156       //first check for Scaled Adds and Subs!
1157       //Valid for add and sub
1158       if(N.getOperand(0).getOpcode() == ISD::SHL && 
1159          isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
1160          (SImm == 2 || SImm == 3)) {
1161         bool use4 = SImm == 2;
1162         Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1163         if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
1164           BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1165                   2, Result).addReg(Tmp2).addImm(SImm);
1166         else {
1167           Tmp1 = SelectExpr(N.getOperand(1));
1168           BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1169                   2, Result).addReg(Tmp2).addReg(Tmp1);
1170         }
1171       }
1172       //Position prevents subs
1173       else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
1174               isSIntImmediate(N.getOperand(1).getOperand(1), SImm) &&
1175               (SImm == 2 || SImm == 3)) {
1176         bool use4 = SImm == 2;
1177         Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1178         if (isSIntImmediateBounded(N.getOperand(0), SImm, 0, 255))
1179           BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2).addImm(SImm);
1180         else {
1181           Tmp1 = SelectExpr(N.getOperand(0));
1182           BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1183         }
1184       }
1185       //small addi
1186       else if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
1187       { //Normal imm add/sub
1188         Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1189         Tmp1 = SelectExpr(N.getOperand(0));
1190         BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
1191       }
1192       else if(isSIntImmediateBounded(N.getOperand(1), SImm, -255, 0))
1193       { //inverted imm add/sub
1194         Opc = isAdd ? Alpha::SUBQi : Alpha::ADDQi;
1195         Tmp1 = SelectExpr(N.getOperand(0));
1196         BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(-SImm);
1197       }
1198       //larger addi
1199       else if(isSIntImmediateBounded(N.getOperand(1), SImm, -32767, 32767))
1200       { //LDA
1201         Tmp1 = SelectExpr(N.getOperand(0));
1202         if (!isAdd)
1203           SImm = -SImm;
1204         BuildMI(BB, Alpha::LDA, 2, Result).addImm(SImm).addReg(Tmp1);
1205       }
1206       //give up and do the operation
1207       else {
1208         //Normal add/sub
1209         Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1210         Tmp1 = SelectExpr(N.getOperand(0));
1211         Tmp2 = SelectExpr(N.getOperand(1));
1212         BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1213       }
1214       return Result;
1215     }
1216   case ISD::FADD:
1217   case ISD::FSUB:
1218   case ISD::FMUL:
1219   case ISD::FDIV: {
1220     if (opcode == ISD::FADD)
1221       Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS;
1222     else if (opcode == ISD::FSUB)
1223       Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS;
1224     else if (opcode == ISD::FMUL)
1225       Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS;
1226     else
1227       Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS;
1228     Tmp1 = SelectExpr(N.getOperand(0));
1229     Tmp2 = SelectExpr(N.getOperand(1));
1230     BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1231     return Result;
1232   }
1233   case ISD::SDIV:
1234     {
1235       //check if we can convert into a shift!
1236       if (isSIntImmediate(N.getOperand(1), SImm) &&
1237           SImm != 0 && isPowerOf2_64(llabs(SImm))) {
1238         unsigned k = Log2_64(llabs(SImm));
1239         Tmp1 = SelectExpr(N.getOperand(0));
1240         if (k == 1)
1241           Tmp2 = Tmp1;
1242         else
1243         {
1244           Tmp2 = MakeReg(MVT::i64);
1245           BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1246         }
1247         Tmp3 = MakeReg(MVT::i64);
1248         BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1249         unsigned Tmp4 = MakeReg(MVT::i64);
1250         BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
1251         if (SImm > 0)
1252           BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1253         else
1254         {
1255           unsigned Tmp5 = MakeReg(MVT::i64);
1256           BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1257           BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1258         }
1259         return Result;
1260       }
1261     }
1262     //Else fall through
1263   case ISD::UDIV:
1264     //else fall though
1265   case ISD::UREM:
1266   case ISD::SREM: {
1267     const char* opstr = 0;
1268     switch(opcode) {
1269     case ISD::UREM: opstr = "__remqu"; break;
1270     case ISD::SREM: opstr = "__remq";  break;
1271     case ISD::UDIV: opstr = "__divqu"; break;
1272     case ISD::SDIV: opstr = "__divq";  break;
1273     }
1274     Tmp1 = SelectExpr(N.getOperand(0));
1275     Tmp2 = SelectExpr(N.getOperand(1));
1276     SDOperand Addr =
1277       ISelDAG->getExternalSymbol(opstr, AlphaLowering.getPointerTy());
1278     Tmp3 = SelectExpr(Addr);
1279     //set up regs explicitly (helps Reg alloc)
1280     BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
1281     BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
1282     BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp3).addReg(Tmp3);
1283     BuildMI(BB, Alpha::JSRs, 2, Alpha::R23).addReg(Alpha::R27).addImm(0);
1284     BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
1285     return Result;
1286   }
1287
1288   case ISD::SELECT:
1289     if (isFP) {
1290       //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1291       unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1292       unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1293
1294       SDOperand CC = N.getOperand(0);
1295
1296       if (CC.getOpcode() == ISD::SETCC &&
1297           !MVT::isInteger(CC.getOperand(0).getValueType())) {
1298         //FP Setcc -> Select yay!
1299
1300
1301         //for a cmp b: c = a - b;
1302         //a = b: c = 0
1303         //a < b: c < 0
1304         //a > b: c > 0
1305
1306         bool invTest = false;
1307         unsigned Tmp3;
1308         bool isD = CC.getOperand(0).getValueType() == MVT::f64;
1309         ConstantFPSDNode *CN;
1310         if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
1311             && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1312           Tmp3 = SelectExpr(CC.getOperand(0));
1313         else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
1314                  && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1315         {
1316           Tmp3 = SelectExpr(CC.getOperand(1));
1317           invTest = true;
1318         }
1319         else
1320         {
1321           unsigned Tmp1 = SelectExpr(CC.getOperand(0));
1322           unsigned Tmp2 = SelectExpr(CC.getOperand(1));
1323           Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1324           BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1325             .addReg(Tmp1).addReg(Tmp2);
1326         }
1327
1328         if(isD)
1329           switch (cast<CondCodeSDNode>(CC.getOperand(2))->get()) {
1330           default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1331           case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNET : Alpha::FCMOVEQT; break;
1332           case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGTT : Alpha::FCMOVLTT; break;
1333           case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGET : Alpha::FCMOVLET; break;
1334           case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLTT : Alpha::FCMOVGTT; break;
1335           case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLET : Alpha::FCMOVGET; break;
1336           case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQT : Alpha::FCMOVNET; break;
1337           }
1338         else
1339           switch (cast<CondCodeSDNode>(CC.getOperand(2))->get()) {
1340           default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1341           case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNES : Alpha::FCMOVEQS; break;
1342           case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGTS : Alpha::FCMOVLTS; break;
1343           case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGES : Alpha::FCMOVLES; break;
1344           case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLTS : Alpha::FCMOVGTS; break;
1345           case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLES : Alpha::FCMOVGES; break;
1346           case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQS : Alpha::FCMOVNES; break;
1347           }
1348         BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
1349         return Result;
1350       }
1351       else
1352       {
1353         Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1354         BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV)
1355           .addReg(Tmp1);
1356 //         // Spill the cond to memory and reload it from there.
1357 //         unsigned Tmp4 = MakeReg(MVT::f64);
1358 //         MoveIntFP(Tmp1, Tmp4, true);
1359 //         //now ideally, we don't have to do anything to the flag...
1360 //         // Get the condition into the zero flag.
1361 //         BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
1362         return Result;
1363       }
1364     } else {
1365       //FIXME: look at parent to decide if intCC can be folded, or if setCC(FP)
1366       //and can save stack use
1367       //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1368       //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1369       //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1370       // Get the condition into the zero flag.
1371       //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1372
1373       SDOperand CC = N.getOperand(0);
1374
1375       if (CC.getOpcode() == ISD::SETCC &&
1376           !MVT::isInteger(CC.getOperand(0).getValueType()))
1377       { //FP Setcc -> Int Select
1378         Tmp1 = MakeReg(MVT::f64);
1379         Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1380         Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1381         bool inv = SelectFPSetCC(CC, Tmp1);
1382         BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
1383           .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1384         return Result;
1385       }
1386       if (CC.getOpcode() == ISD::SETCC) {
1387         //Int SetCC -> Select
1388         //Dropping the CC is only useful if we are comparing to 0
1389         if(isSIntImmediateBounded(CC.getOperand(1), SImm, 0, 0)) {
1390           //figure out a few things
1391           bool useImm = isSIntImmediateBounded(N.getOperand(2), SImm, 0, 255);
1392
1393           //Fix up CC
1394           ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
1395           if (useImm) //Invert sense to get Imm field right
1396             cCode = ISD::getSetCCInverse(cCode, true);
1397
1398           //Choose the CMOV
1399           switch (cCode) {
1400           default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
1401           case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ;     break;
1402           case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT;     break;
1403           case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE;     break;
1404           case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT;     break;
1405           case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE;     break;
1406           case ISD::SETULT: assert(0 && "unsigned < 0 is never true"); break;
1407           case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE;    break;
1408           //Technically you could have this CC
1409           case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ;    break;
1410           case ISD::SETUGE: assert(0 && "unsgined >= 0 is always true"); break;
1411           case ISD::SETNE:  Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE;    break;
1412           }
1413           Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
1414
1415           if (useImm) {
1416             Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
1417             BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addImm(SImm).addReg(Tmp1);
1418           } else {
1419             Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1420             Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1421             BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
1422           }
1423           return Result;
1424         }
1425         //Otherwise, fall though
1426       }
1427       Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1428       Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1429       Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1430       BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3)
1431         .addReg(Tmp1);
1432
1433       return Result;
1434     }
1435
1436   case ISD::Constant:
1437     {
1438       int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
1439       int zero_extend_top = 0;
1440       if (val > 0 && (val & 0xFFFFFFFF00000000ULL) == 0 &&
1441           ((int32_t)val < 0)) {
1442         //try a small load and zero extend
1443         val = (int32_t)val;
1444         zero_extend_top = 15;
1445       }
1446
1447       if (val <= IMM_HIGH && val >= IMM_LOW) {
1448         if(!zero_extend_top)
1449           BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
1450         else {
1451           Tmp1 = MakeReg(MVT::i64);
1452           BuildMI(BB, Alpha::LDA, 2, Tmp1).addImm(val).addReg(Alpha::R31);
1453           BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp1).addImm(zero_extend_top);
1454         }
1455       }
1456       else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
1457                val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
1458         Tmp1 = MakeReg(MVT::i64);
1459         BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val))
1460           .addReg(Alpha::R31);
1461         if (!zero_extend_top)
1462           BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
1463         else {
1464           Tmp3 = MakeReg(MVT::i64);
1465           BuildMI(BB, Alpha::LDA, 2, Tmp3).addImm(getLower16(val)).addReg(Tmp1);
1466           BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp3).addImm(zero_extend_top);
1467         }
1468       }
1469       else {
1470         //re-get the val since we are going to mem anyway
1471         val = (int64_t)cast<ConstantSDNode>(N)->getValue();
1472         MachineConstantPool *CP = BB->getParent()->getConstantPool();
1473         ConstantUInt *C =
1474           ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
1475         unsigned CPI = CP->getConstantPoolIndex(C);
1476         AlphaLowering.restoreGP(BB);
1477         has_sym = true;
1478         Tmp1 = MakeReg(MVT::i64);
1479         BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPI)
1480           .addReg(Alpha::R29);
1481         if (EnableAlphaLSMark)
1482           BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
1483             .addImm(getUID());
1484         BuildMI(BB, Alpha::LDQr, 2, Result).addConstantPoolIndex(CPI)
1485           .addReg(Tmp1);
1486       }
1487       return Result;
1488     }
1489   case ISD::FNEG:
1490     if(ISD::FABS == N.getOperand(0).getOpcode())
1491       {
1492         Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1493         BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYSNT : Alpha::CPYSNS, 
1494                 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1495       } else {
1496         Tmp1 = SelectExpr(N.getOperand(0));
1497         BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYSNT : Alpha::CPYSNS
1498                 , 2, Result).addReg(Tmp1).addReg(Tmp1);
1499       }
1500     return Result;
1501
1502   case ISD::FABS:
1503     Tmp1 = SelectExpr(N.getOperand(0));
1504     BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYST : Alpha::CPYSS, 2, Result)
1505       .addReg(Alpha::F31).addReg(Tmp1);
1506     return Result;
1507
1508   case ISD::FP_ROUND:
1509     assert (DestType == MVT::f32 &&
1510             N.getOperand(0).getValueType() == MVT::f64 &&
1511             "only f64 to f32 conversion supported here");
1512     Tmp1 = SelectExpr(N.getOperand(0));
1513     BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
1514     return Result;
1515
1516   case ISD::FP_EXTEND:
1517     assert (DestType == MVT::f64 &&
1518             N.getOperand(0).getValueType() == MVT::f32 &&
1519             "only f32 to f64 conversion supported here");
1520     Tmp1 = SelectExpr(N.getOperand(0));
1521     BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
1522     return Result;
1523
1524   case ISD::ConstantFP:
1525     if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1526       if (CN->isExactlyValue(+0.0)) {
1527         BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYST : Alpha::CPYSS
1528                 , 2, Result).addReg(Alpha::F31)
1529           .addReg(Alpha::F31);
1530       } else if ( CN->isExactlyValue(-0.0)) {
1531         BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYSNT : Alpha::CPYSNS,
1532                 2, Result).addReg(Alpha::F31)
1533           .addReg(Alpha::F31);
1534       } else {
1535         abort();
1536       }
1537     }
1538     return Result;
1539
1540   case AlphaISD::CVTQT_:
1541     Tmp1 = SelectExpr(N.getOperand(0));
1542     BuildMI(BB, Alpha::CVTQT, 1, Result).addReg(Tmp1);
1543     return Result;
1544
1545   case AlphaISD::CVTQS_:
1546     Tmp1 = SelectExpr(N.getOperand(0));
1547     BuildMI(BB, Alpha::CVTQS, 1, Result).addReg(Tmp1);
1548     return Result;
1549
1550   case AlphaISD::CVTTQ_:
1551     Tmp1 = SelectExpr(N.getOperand(0));
1552     BuildMI(BB, Alpha::CVTTQ, 1, Result).addReg(Tmp1);
1553     return Result;
1554
1555   case AlphaISD::ITOFT_:
1556     Tmp1 = SelectExpr(N.getOperand(0));
1557     BuildMI(BB, Alpha::ITOFT, 1, Result).addReg(Tmp1);
1558     return Result;
1559
1560   case AlphaISD::FTOIT_:
1561     Tmp1 = SelectExpr(N.getOperand(0));
1562     BuildMI(BB, Alpha::FTOIT, 1, Result).addReg(Tmp1);
1563     return Result;
1564
1565   case ISD::AssertSext:
1566   case ISD::AssertZext:
1567     return SelectExpr(N.getOperand(0));
1568
1569   }
1570
1571   return 0;
1572 }
1573
1574 void AlphaISel::Select(SDOperand N) {
1575   unsigned Tmp1, Tmp2, Opc = Alpha::WTF;
1576   unsigned opcode = N.getOpcode();
1577
1578   if (!ExprMap.insert(std::make_pair(N, notIn)).second)
1579     return;  // Already selected.
1580
1581   SDNode *Node = N.Val;
1582
1583   switch (opcode) {
1584
1585   default:
1586     Node->dump(); std::cerr << "\n";
1587     assert(0 && "Node not handled yet!");
1588
1589   case ISD::BRCOND: {
1590     SelectBranchCC(N);
1591     return;
1592   }
1593
1594   case ISD::BR: {
1595     MachineBasicBlock *Dest =
1596       cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1597
1598     Select(N.getOperand(0));
1599     BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1600     return;
1601   }
1602
1603   case ISD::ImplicitDef:
1604     ++count_ins;
1605     Select(N.getOperand(0));
1606     switch(N.getValueType()) {
1607     case MVT::f32: Opc = Alpha::IDEF_F32; break;
1608     case MVT::f64: Opc = Alpha::IDEF_F64; break;
1609     case MVT::i64: Opc = Alpha::IDEF_I; break;
1610     default: assert(0 && "should have been legalized");
1611     };
1612     BuildMI(BB, Opc, 0,
1613             cast<RegisterSDNode>(N.getOperand(1))->getReg());
1614     return;
1615
1616   case ISD::EntryToken: return;  // Noop
1617
1618   case ISD::TokenFactor:
1619     for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1620       Select(Node->getOperand(i));
1621
1622     //N.Val->dump(); std::cerr << "\n";
1623     //assert(0 && "Node not handled yet!");
1624
1625     return;
1626
1627   case ISD::CopyToReg:
1628     ++count_outs;
1629     Select(N.getOperand(0));
1630     Tmp1 = SelectExpr(N.getOperand(2));
1631     Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
1632
1633     if (Tmp1 != Tmp2) {
1634       switch(N.getOperand(2).getValueType()) {
1635       case MVT::f64:
1636         BuildMI(BB, Alpha::CPYST, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1637         break;
1638       case MVT::f32:
1639         BuildMI(BB, Alpha::CPYSS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1640         break;
1641       default:
1642         BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1643         break;
1644       }
1645     }
1646     return;
1647
1648   case ISD::RET:
1649     ++count_outs;
1650     switch (N.getNumOperands()) {
1651     default:
1652       std::cerr << N.getNumOperands() << "\n";
1653       for (unsigned i = 0; i < N.getNumOperands(); ++i)
1654         std::cerr << N.getOperand(i).getValueType() << "\n";
1655       Node->dump();
1656       assert(0 && "Unknown return instruction!");
1657     case 2:
1658       Select(N.getOperand(0));
1659       Tmp1 = SelectExpr(N.getOperand(1));
1660       switch (N.getOperand(1).getValueType()) {
1661       default: Node->dump();
1662         assert(0 && "All other types should have been promoted!!");
1663       case MVT::f64:
1664         BuildMI(BB, Alpha::CPYST, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1665         break;
1666       case MVT::f32:
1667         BuildMI(BB, Alpha::CPYSS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1668         break;
1669       case MVT::i32:
1670       case MVT::i64:
1671         BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1672         break;
1673       }
1674       break;
1675     case 1:
1676       Select(N.getOperand(0));
1677       break;
1678     }
1679     // Just emit a 'ret' instruction
1680     AlphaLowering.restoreRA(BB);
1681     BuildMI(BB, Alpha::RET, 2, Alpha::R31).addReg(Alpha::R26).addImm(1);
1682     return;
1683
1684   case ISD::TRUNCSTORE:
1685   case ISD::STORE:
1686     {
1687       SDOperand Chain   = N.getOperand(0);
1688       SDOperand Value = N.getOperand(1);
1689       SDOperand Address = N.getOperand(2);
1690       Select(Chain);
1691
1692       Tmp1 = SelectExpr(Value); //value
1693
1694       if (opcode == ISD::STORE) {
1695         switch(Value.getValueType()) {
1696         default: assert(0 && "unknown Type in store");
1697         case MVT::i64: Opc = Alpha::STQ; break;
1698         case MVT::f64: Opc = Alpha::STT; break;
1699         case MVT::f32: Opc = Alpha::STS; break;
1700         }
1701       } else { //ISD::TRUNCSTORE
1702         switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
1703         default: assert(0 && "unknown Type in store");
1704         case MVT::i8: Opc = Alpha::STB; break;
1705         case MVT::i16: Opc = Alpha::STW; break;
1706         case MVT::i32: Opc = Alpha::STL; break;
1707         }
1708       }
1709
1710       int i, j, k;
1711       if (EnableAlphaLSMark)
1712         getValueInfo(cast<SrcValueSDNode>(N.getOperand(3))->getValue(),
1713                      i, j, k);
1714
1715       GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
1716       if (GASD && !GASD->getGlobal()->isExternal()) {
1717         Tmp2 = MakeReg(MVT::i64);
1718         AlphaLowering.restoreGP(BB);
1719         BuildMI(BB, Alpha::LDAHr, 2, Tmp2)
1720           .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
1721         if (EnableAlphaLSMark)
1722           BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1723             .addImm(getUID());
1724         BuildMI(BB, GetRelVersion(Opc), 3).addReg(Tmp1)
1725           .addGlobalAddress(GASD->getGlobal()).addReg(Tmp2);
1726       } else if(Address.getOpcode() == ISD::FrameIndex) {
1727         if (EnableAlphaLSMark)
1728           BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1729             .addImm(getUID());
1730         BuildMI(BB, Opc, 3).addReg(Tmp1)
1731           .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1732           .addReg(Alpha::F31);
1733       } else {
1734         long offset;
1735         SelectAddr(Address, Tmp2, offset);
1736         if (EnableAlphaLSMark)
1737           BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1738             .addImm(getUID());
1739         BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1740       }
1741       return;
1742     }
1743
1744   case ISD::EXTLOAD:
1745   case ISD::SEXTLOAD:
1746   case ISD::ZEXTLOAD:
1747   case ISD::LOAD:
1748   case ISD::CopyFromReg:
1749   case ISD::TAILCALL:
1750   case ISD::CALL:
1751   case ISD::READCYCLECOUNTER:
1752   case ISD::DYNAMIC_STACKALLOC:
1753     ExprMap.erase(N);
1754     SelectExpr(N);
1755     return;
1756
1757   case ISD::CALLSEQ_START:
1758   case ISD::CALLSEQ_END:
1759     Select(N.getOperand(0));
1760     Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1761
1762     Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
1763       Alpha::ADJUSTSTACKUP;
1764     BuildMI(BB, Opc, 1).addImm(Tmp1);
1765     return;
1766
1767   case ISD::PCMARKER:
1768     Select(N.getOperand(0)); //Chain
1769     BuildMI(BB, Alpha::PCLABEL, 2)
1770       .addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
1771     return;
1772   }
1773   assert(0 && "Should not be reached!");
1774 }
1775
1776
1777 /// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1778 /// into a machine code representation using pattern matching and a machine
1779 /// description file.
1780 ///
1781 FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
1782   return new AlphaISel(TM);
1783 }
1784