Fix a bunch more alpha regressions
[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;
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;
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 Opc, Tmp1, Tmp2, Tmp3;
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     {
369       //assert(0 && "Setcc On float?\n");
370       std::cerr << "Setcc on float!\n";
371       Tmp3 = MakeReg(MVT::f64);
372       BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Alpha::F31).addReg(Tmp1);
373       Tmp1 = Tmp3;
374     }
375   if (SetCC->getOperand(1).getValueType() == MVT::f32)
376     {
377       //assert (0 && "Setcc On float?\n");
378       std::cerr << "Setcc on float!\n";
379       Tmp3 = MakeReg(MVT::f64);
380       BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Alpha::F31).addReg(Tmp2);
381       Tmp2 = Tmp3;
382     }
383
384   if (rev) std::swap(Tmp1, Tmp2);
385   //do the comparison
386   BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
387   return inv;
388 }
389
390 //Check to see if the load is a constant offset from a base register
391 void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
392 {
393   unsigned opcode = N.getOpcode();
394   if (opcode == ISD::ADD && N.getOperand(1).getOpcode() == ISD::Constant &&
395       cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
396   { //Normal imm add
397     Reg = SelectExpr(N.getOperand(0));
398     offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
399     return;
400   }
401   Reg = SelectExpr(N);
402   offset = 0;
403   return;
404 }
405
406 void AlphaISel::SelectBranchCC(SDOperand N)
407 {
408   assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
409   MachineBasicBlock *Dest =
410     cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
411   unsigned Opc = Alpha::WTF;
412
413   Select(N.getOperand(0));  //chain
414   SDOperand CC = N.getOperand(1);
415
416   if (CC.getOpcode() == ISD::SETCC)
417   {
418     ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
419     if (MVT::isInteger(CC.getOperand(0).getValueType())) {
420       //Dropping the CC is only useful if we are comparing to 0
421       bool RightZero = CC.getOperand(1).getOpcode() == ISD::Constant &&
422         cast<ConstantSDNode>(CC.getOperand(1))->getValue() == 0;
423       bool isNE = false;
424
425       //Fix up CC
426       if(cCode == ISD::SETNE)
427         isNE = true;
428
429       if (RightZero) {
430         switch (cCode) {
431         default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
432         case ISD::SETEQ:  Opc = Alpha::BEQ; break;
433         case ISD::SETLT:  Opc = Alpha::BLT; break;
434         case ISD::SETLE:  Opc = Alpha::BLE; break;
435         case ISD::SETGT:  Opc = Alpha::BGT; break;
436         case ISD::SETGE:  Opc = Alpha::BGE; break;
437         case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
438         case ISD::SETUGT: Opc = Alpha::BNE; break;
439         //Technically you could have this CC
440         case ISD::SETULE: Opc = Alpha::BEQ; break;
441         case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
442         case ISD::SETNE:  Opc = Alpha::BNE; break;
443         }
444         unsigned Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
445         BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
446         return;
447       } else {
448         unsigned Tmp1 = SelectExpr(CC);
449         if (isNE)
450           BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
451         else
452           BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
453         return;
454       }
455     } else { //FP
456       //Any comparison between 2 values should be codegened as an folded
457       //branch, as moving CC to the integer register is very expensive
458       //for a cmp b: c = a - b;
459       //a = b: c = 0
460       //a < b: c < 0
461       //a > b: c > 0
462
463       bool invTest = false;
464       unsigned Tmp3;
465
466       ConstantFPSDNode *CN;
467       if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
468           && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
469         Tmp3 = SelectExpr(CC.getOperand(0));
470       else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
471           && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
472       {
473         Tmp3 = SelectExpr(CC.getOperand(1));
474         invTest = true;
475       }
476       else
477       {
478         unsigned Tmp1 = SelectExpr(CC.getOperand(0));
479         unsigned Tmp2 = SelectExpr(CC.getOperand(1));
480         bool isD = CC.getOperand(0).getValueType() == MVT::f64;
481         Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
482         BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
483           .addReg(Tmp1).addReg(Tmp2);
484       }
485
486       switch (cCode) {
487       default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
488       case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
489       case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
490       case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
491       case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
492       case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
493       case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
494       }
495       BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
496       return;
497     }
498     abort(); //Should never be reached
499   } else {
500     //Giveup and do the stupid thing
501     unsigned Tmp1 = SelectExpr(CC);
502     BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
503     return;
504   }
505   abort(); //Should never be reached
506 }
507
508 unsigned AlphaISel::SelectExpr(SDOperand N) {
509   unsigned Result;
510   unsigned Tmp1, Tmp2 = 0, Tmp3;
511   unsigned Opc = 0;
512   unsigned opcode = N.getOpcode();
513   int64_t SImm = 0;
514   uint64_t UImm;
515
516   SDNode *Node = N.Val;
517   MVT::ValueType DestType = N.getValueType();
518   bool isFP = DestType == MVT::f64 || DestType == MVT::f32;
519
520   unsigned &Reg = ExprMap[N];
521   if (Reg) return Reg;
522
523   switch(N.getOpcode()) {
524   default:
525     Reg = Result = (N.getValueType() != MVT::Other) ?
526       MakeReg(N.getValueType()) : notIn;
527       break;
528   case ISD::AssertSext:
529   case ISD::AssertZext:
530     return Reg = SelectExpr(N.getOperand(0));
531   case ISD::CALL:
532   case ISD::TAILCALL:
533     // If this is a call instruction, make sure to prepare ALL of the result
534     // values as well as the chain.
535     if (Node->getNumValues() == 1)
536       Reg = Result = notIn;  // Void call, just a chain.
537     else {
538       Result = MakeReg(Node->getValueType(0));
539       ExprMap[N.getValue(0)] = Result;
540       for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
541         ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
542       ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
543     }
544     break;
545   }
546
547   switch (opcode) {
548   default:
549     Node->dump();
550     assert(0 && "Node not handled!\n");
551
552   case ISD::READCYCLECOUNTER:
553     Select(N.getOperand(0)); //Select chain
554     BuildMI(BB, Alpha::RPCC, 1, Result).addReg(Alpha::R31);
555     return Result;
556
557   case ISD::CTPOP:
558   case ISD::CTTZ:
559   case ISD::CTLZ:
560     Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
561     (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
562     Tmp1 = SelectExpr(N.getOperand(0));
563     BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
564     return Result;
565
566   case ISD::MULHU:
567     Tmp1 = SelectExpr(N.getOperand(0));
568     Tmp2 = SelectExpr(N.getOperand(1));
569     BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
570     return Result;
571   case ISD::MULHS:
572     {
573       //MULHU - Ra<63>*Rb - Rb<63>*Ra
574       Tmp1 = SelectExpr(N.getOperand(0));
575       Tmp2 = SelectExpr(N.getOperand(1));
576       Tmp3 = MakeReg(MVT::i64);
577       BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
578       unsigned V1 = MakeReg(MVT::i64);
579       unsigned V2 = MakeReg(MVT::i64);
580       BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31)
581         .addReg(Tmp1);
582       BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31)
583         .addReg(Tmp2);
584       unsigned IRes = MakeReg(MVT::i64);
585       BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
586       BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
587       return Result;
588     }
589   case ISD::UNDEF: {
590     BuildMI(BB, Alpha::IDEF, 0, Result);
591     return Result;
592   }
593
594   case ISD::DYNAMIC_STACKALLOC:
595     // Generate both result values.
596     if (Result != notIn)
597       ExprMap[N.getValue(1)] = notIn;   // Generate the token
598     else
599       Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
600
601     // FIXME: We are currently ignoring the requested alignment for handling
602     // greater than the stack alignment.  This will need to be revisited at some
603     // point.  Align = N.getOperand(2);
604
605     if (!isa<ConstantSDNode>(N.getOperand(2)) ||
606         cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
607       std::cerr << "Cannot allocate stack object with greater alignment than"
608                 << " the stack alignment yet!";
609       abort();
610     }
611
612     Select(N.getOperand(0));
613     if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 32767))
614       BuildMI(BB, Alpha::LDA, 2, Alpha::R30).addImm(-SImm).addReg(Alpha::R30);
615     else {
616       Tmp1 = SelectExpr(N.getOperand(1));
617       // Subtract size from stack pointer, thereby allocating some space.
618       BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
619     }
620
621     // Put a pointer to the space into the result register, by copying the stack
622     // pointer.
623     BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
624     return Result;
625
626   case ISD::ConstantPool:
627     Tmp1 = BB->getParent()->getConstantPool()->
628        getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
629     AlphaLowering.restoreGP(BB);
630     Tmp2 = MakeReg(MVT::i64);
631     BuildMI(BB, Alpha::LDAHr, 2, Tmp2).addConstantPoolIndex(Tmp1)
632       .addReg(Alpha::R29);
633     BuildMI(BB, Alpha::LDAr, 2, Result).addConstantPoolIndex(Tmp1)
634       .addReg(Tmp2);
635     return Result;
636
637   case ISD::FrameIndex:
638     BuildMI(BB, Alpha::LDA, 2, Result)
639       .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
640       .addReg(Alpha::F31);
641     return Result;
642
643   case ISD::EXTLOAD:
644   case ISD::ZEXTLOAD:
645   case ISD::SEXTLOAD:
646   case ISD::LOAD:
647     {
648       // Make sure we generate both values.
649       if (Result != notIn)
650         ExprMap[N.getValue(1)] = notIn;   // Generate the token
651       else
652         Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
653
654       SDOperand Chain   = N.getOperand(0);
655       SDOperand Address = N.getOperand(1);
656       Select(Chain);
657
658       bool fpext = true;
659
660       if (opcode == ISD::LOAD)
661         switch (Node->getValueType(0)) {
662         default: Node->dump(); assert(0 && "Bad load!");
663         case MVT::i64: Opc = Alpha::LDQ; break;
664         case MVT::f64: Opc = Alpha::LDT; break;
665         case MVT::f32: Opc = Alpha::LDS; break;
666         }
667       else
668         switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
669         default: Node->dump(); assert(0 && "Bad sign extend!");
670         case MVT::i32: Opc = Alpha::LDL;
671           assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
672         case MVT::i16: Opc = Alpha::LDWU;
673           assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
674         case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
675         case MVT::i8: Opc = Alpha::LDBU;
676           assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
677         }
678
679       int i, j, k;
680       if (EnableAlphaLSMark)
681         getValueInfo(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue(),
682                      i, j, k);
683
684       GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
685       if (GASD && !GASD->getGlobal()->isExternal()) {
686         Tmp1 = MakeReg(MVT::i64);
687         AlphaLowering.restoreGP(BB);
688         BuildMI(BB, Alpha::LDAHr, 2, Tmp1)
689           .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
690         if (EnableAlphaLSMark)
691           BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
692             .addImm(getUID());
693         BuildMI(BB, GetRelVersion(Opc), 2, Result)
694           .addGlobalAddress(GASD->getGlobal()).addReg(Tmp1);
695       } else if (ConstantPoolSDNode *CP =
696                      dyn_cast<ConstantPoolSDNode>(Address)) {
697         unsigned CPIdx = BB->getParent()->getConstantPool()->
698              getConstantPoolIndex(CP->get());
699         AlphaLowering.restoreGP(BB);
700         has_sym = true;
701         Tmp1 = MakeReg(MVT::i64);
702         BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPIdx)
703           .addReg(Alpha::R29);
704         if (EnableAlphaLSMark)
705           BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
706             .addImm(getUID());
707         BuildMI(BB, GetRelVersion(Opc), 2, Result)
708           .addConstantPoolIndex(CPIdx).addReg(Tmp1);
709       } else if(Address.getOpcode() == ISD::FrameIndex) {
710         if (EnableAlphaLSMark)
711           BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
712             .addImm(getUID());
713         BuildMI(BB, Opc, 2, Result)
714           .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
715           .addReg(Alpha::F31);
716       } else {
717         long offset;
718         SelectAddr(Address, Tmp1, offset);
719         if (EnableAlphaLSMark)
720           BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
721             .addImm(getUID());
722         BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
723       }
724       return Result;
725     }
726
727   case ISD::GlobalAddress:
728     AlphaLowering.restoreGP(BB);
729     has_sym = true;
730
731     Reg = Result = MakeReg(MVT::i64);
732
733     if (EnableAlphaLSMark)
734       BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
735         .addImm(getUID());
736
737     BuildMI(BB, Alpha::LDQl, 2, Result)
738       .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal())
739       .addReg(Alpha::R29);
740     return Result;
741
742   case ISD::ExternalSymbol:
743     AlphaLowering.restoreGP(BB);
744     has_sym = true;
745
746     Reg = Result = MakeReg(MVT::i64);
747
748     if (EnableAlphaLSMark)
749       BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
750         .addImm(getUID());
751
752     BuildMI(BB, Alpha::LDQl, 2, Result)
753       .addExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol())
754       .addReg(Alpha::R29);
755     return Result;
756
757   case ISD::TAILCALL:
758   case ISD::CALL:
759     {
760       Select(N.getOperand(0));
761
762       // The chain for this call is now lowered.
763       ExprMap[N.getValue(Node->getNumValues()-1)] = notIn;
764
765       //grab the arguments
766       std::vector<unsigned> argvregs;
767       //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
768       for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
769         argvregs.push_back(SelectExpr(N.getOperand(i)));
770
771       //in reg args
772       for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
773       {
774         unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
775                                Alpha::R19, Alpha::R20, Alpha::R21};
776         unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
777                                  Alpha::F19, Alpha::F20, Alpha::F21};
778         switch(N.getOperand(i+2).getValueType()) {
779         default:
780           Node->dump();
781           N.getOperand(i).Val->dump();
782           std::cerr << "Type for " << i << " is: " <<
783             N.getOperand(i+2).getValueType() << "\n";
784           assert(0 && "Unknown value type for call");
785         case MVT::i1:
786         case MVT::i8:
787         case MVT::i16:
788         case MVT::i32:
789         case MVT::i64:
790           BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i])
791             .addReg(argvregs[i]);
792           break;
793         case MVT::f32:
794           BuildMI(BB, Alpha::CPYSS, 2, args_float[i]).addReg(argvregs[i])
795             .addReg(argvregs[i]);
796           break;
797         case MVT::f64:
798           BuildMI(BB, Alpha::CPYST, 2, args_float[i]).addReg(argvregs[i])
799             .addReg(argvregs[i]);
800           break;
801         }
802       }
803       //in mem args
804       for (int i = 6, e = argvregs.size(); i < e; ++i)
805       {
806         switch(N.getOperand(i+2).getValueType()) {
807         default:
808           Node->dump();
809           N.getOperand(i).Val->dump();
810           std::cerr << "Type for " << i << " is: " <<
811             N.getOperand(i+2).getValueType() << "\n";
812           assert(0 && "Unknown value type for call");
813         case MVT::i1:
814         case MVT::i8:
815         case MVT::i16:
816         case MVT::i32:
817         case MVT::i64:
818           BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
819             .addReg(Alpha::R30);
820           break;
821         case MVT::f32:
822           BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
823             .addReg(Alpha::R30);
824           break;
825         case MVT::f64:
826           BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
827             .addReg(Alpha::R30);
828           break;
829         }
830       }
831       //build the right kind of call
832       GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(N.getOperand(1));
833       if (GASD && !GASD->getGlobal()->isExternal()) {
834         //use PC relative branch call
835         AlphaLowering.restoreGP(BB);
836         BuildMI(BB, Alpha::BSR, 1, Alpha::R26)
837           .addGlobalAddress(GASD->getGlobal(),true);
838       } else {
839         //no need to restore GP as we are doing an indirect call
840         Tmp1 = SelectExpr(N.getOperand(1));
841         BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
842         BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
843       }
844
845       //push the result into a virtual register
846
847       switch (Node->getValueType(0)) {
848       default: Node->dump(); assert(0 && "Unknown value type for call result!");
849       case MVT::Other: return notIn;
850       case MVT::i64:
851         BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
852         break;
853       case MVT::f32:
854         BuildMI(BB, Alpha::CPYSS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
855         break;
856        case MVT::f64:
857         BuildMI(BB, Alpha::CPYST, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
858         break;
859       }
860       return Result+N.ResNo;
861     }
862
863   case ISD::SIGN_EXTEND_INREG:
864     {
865       //do SDIV opt for all levels of ints if not dividing by a constant
866       if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
867           && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
868       {
869         unsigned Tmp4 = MakeReg(MVT::f64);
870         unsigned Tmp5 = MakeReg(MVT::f64);
871         unsigned Tmp6 = MakeReg(MVT::f64);
872         unsigned Tmp7 = MakeReg(MVT::f64);
873         unsigned Tmp8 = MakeReg(MVT::f64);
874         unsigned Tmp9 = MakeReg(MVT::f64);
875
876         Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
877         Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
878         MoveInt2FP(Tmp1, Tmp4, true);
879         MoveInt2FP(Tmp2, Tmp5, true);
880         BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Alpha::F31).addReg(Tmp4);
881         BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Alpha::F31).addReg(Tmp5);
882         BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
883         BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Alpha::F31).addReg(Tmp8);
884         MoveFP2Int(Tmp9, Result, true);
885         return Result;
886       }
887
888       //Alpha has instructions for a bunch of signed 32 bit stuff
889       if(cast<VTSDNode>(Node->getOperand(1))->getVT() == MVT::i32) {
890         switch (N.getOperand(0).getOpcode()) {
891         case ISD::ADD:
892         case ISD::SUB:
893         case ISD::MUL:
894           {
895             bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
896             bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
897             //FIXME: first check for Scaled Adds and Subs!
898             if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
899                isSIntImmediateBounded(N.getOperand(0).getOperand(0).getOperand(1), SImm, 2, 3))
900             {
901               bool use4 = SImm == 2;
902               Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
903               Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
904               BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
905                       2,Result).addReg(Tmp1).addReg(Tmp2);
906             }
907             else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
908                     isSIntImmediateBounded(N.getOperand(0).getOperand(1).getOperand(1), SImm, 2, 3))
909             {
910               bool use4 = SImm == 2;
911               Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
912               Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
913               BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
914             }
915             else if(isSIntImmediateBounded(N.getOperand(0).getOperand(1), SImm, 0, 255)) 
916             { //Normal imm add/sub
917               Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
918               Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
919               BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
920             }
921             else if(!isMul && isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
922                     (((SImm << 32) >> 32) >= -255) && (((SImm << 32) >> 32) <= 0))
923             { //handle canonicalization
924               Opc = isAdd ? Alpha::SUBLi : Alpha::ADDLi;
925               Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
926               SImm = 0 - ((SImm << 32) >> 32);
927               assert(SImm >= 0 && SImm <= 255);
928               BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
929             }
930             else
931             { //Normal add/sub
932               Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
933               Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
934               Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
935               BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
936             }
937             return Result;
938           }
939         default: break; //Fall Though;
940         }
941       } //Every thing else fall though too, including unhandled opcodes above
942       Tmp1 = SelectExpr(N.getOperand(0));
943       //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
944       switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
945       default:
946         Node->dump();
947         assert(0 && "Sign Extend InReg not there yet");
948         break;
949       case MVT::i32:
950         {
951           BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
952           break;
953         }
954       case MVT::i16:
955         BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
956         break;
957       case MVT::i8:
958         BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
959         break;
960       case MVT::i1:
961         Tmp2 = MakeReg(MVT::i64);
962         BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
963         BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
964         break;
965       }
966       return Result;
967     }
968
969   case ISD::SETCC:
970     {
971       ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(2))->get();
972       if (MVT::isInteger(N.getOperand(0).getValueType())) {
973         bool isConst = false;
974         int dir;
975
976         //Tmp1 = SelectExpr(N.getOperand(0));
977         if(isSIntImmediate(N.getOperand(1), SImm) && SImm <= 255 && SImm >= 0)
978           isConst = true;
979
980         switch (CC) {
981         default: Node->dump(); assert(0 && "Unknown integer comparison!");
982         case ISD::SETEQ:
983           Opc = isConst ? Alpha::CMPEQi : Alpha::CMPEQ; dir=1; break;
984         case ISD::SETLT:
985           Opc = isConst ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
986         case ISD::SETLE:
987           Opc = isConst ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
988         case ISD::SETGT: Opc = Alpha::CMPLT; dir = 2; break;
989         case ISD::SETGE: Opc = Alpha::CMPLE; dir = 2; break;
990         case ISD::SETULT:
991           Opc = isConst ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
992         case ISD::SETUGT: Opc = Alpha::CMPULT; dir = 2; break;
993         case ISD::SETULE:
994           Opc = isConst ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
995         case ISD::SETUGE: Opc = Alpha::CMPULE; dir = 2; break;
996         case ISD::SETNE: {//Handle this one special
997           //std::cerr << "Alpha does not have a setne.\n";
998           //abort();
999           Tmp1 = SelectExpr(N.getOperand(0));
1000           Tmp2 = SelectExpr(N.getOperand(1));
1001           Tmp3 = MakeReg(MVT::i64);
1002           BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1003           //Remeber we have the Inv for this CC
1004           CCInvMap[N] = Tmp3;
1005           //and invert
1006           BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
1007           return Result;
1008         }
1009         }
1010         if (dir == 1) {
1011           Tmp1 = SelectExpr(N.getOperand(0));
1012           if (isConst) {
1013             BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
1014           } else {
1015             Tmp2 = SelectExpr(N.getOperand(1));
1016             BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1017           }
1018         } else { //if (dir == 2) {
1019           Tmp1 = SelectExpr(N.getOperand(1));
1020           Tmp2 = SelectExpr(N.getOperand(0));
1021           BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1022         }
1023       } else {
1024         //do the comparison
1025         Tmp1 = MakeReg(MVT::f64);
1026         bool inv = SelectFPSetCC(N, Tmp1);
1027
1028         //now arrange for Result (int) to have a 1 or 0
1029         Tmp2 = MakeReg(MVT::i64);
1030         BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
1031         Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
1032         BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
1033       }
1034       return Result;
1035     }
1036
1037   case ISD::CopyFromReg:
1038     {
1039       ++count_ins;
1040
1041       // Make sure we generate both values.
1042       if (Result != notIn)
1043         ExprMap[N.getValue(1)] = notIn;   // Generate the token
1044       else
1045         Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1046
1047       SDOperand Chain   = N.getOperand(0);
1048
1049       Select(Chain);
1050       unsigned r = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1051       //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1052       switch(N.getValue(0).getValueType()) {
1053       case MVT::f32:
1054         BuildMI(BB, Alpha::CPYSS, 2, Result).addReg(r).addReg(r);
1055         break;
1056       case MVT::f64:
1057         BuildMI(BB, Alpha::CPYST, 2, Result).addReg(r).addReg(r);
1058         break;
1059       default:
1060         BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1061         break;
1062       }
1063       return Result;
1064     }
1065
1066     //Most of the plain arithmetic and logic share the same form, and the same
1067     //constant immediate test
1068   case ISD::XOR:
1069     //Match Not
1070     if (isSIntImmediate(N.getOperand(1), SImm) && SImm == -1) {
1071       Tmp1 = SelectExpr(N.getOperand(0));
1072       BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1073       return Result;
1074     }
1075     //Fall through
1076   case ISD::AND:
1077     //handle zap
1078     if (opcode == ISD::AND && isUIntImmediate(N.getOperand(1), UImm))
1079     {
1080       unsigned int build = 0;
1081       for(int i = 0; i < 8; ++i)
1082       {
1083         if ((UImm & 0x00FF) == 0x00FF)
1084           build |= 1 << i;
1085         else if ((UImm & 0x00FF) != 0)
1086         { build = 0; break; }
1087         UImm >>= 8;
1088       }
1089       if (build)
1090       {
1091         Tmp1 = SelectExpr(N.getOperand(0));
1092         BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1093         return Result;
1094       }
1095     }
1096   case ISD::OR:
1097     //Check operand(0) == Not
1098     if (N.getOperand(0).getOpcode() == ISD::XOR &&
1099         isSIntImmediate(N.getOperand(0).getOperand(1), SImm) && SImm == -1) {
1100       switch(opcode) {
1101         case ISD::AND: Opc = Alpha::BIC; break;
1102         case ISD::OR:  Opc = Alpha::ORNOT; break;
1103         case ISD::XOR: Opc = Alpha::EQV; break;
1104       }
1105       Tmp1 = SelectExpr(N.getOperand(1));
1106       Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1107       BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1108       return Result;
1109     }
1110     //Check operand(1) == Not
1111     if (N.getOperand(1).getOpcode() == ISD::XOR &&
1112         isSIntImmediate(N.getOperand(1).getOperand(1), SImm) && SImm == -1) {
1113       switch(opcode) {
1114         case ISD::AND: Opc = Alpha::BIC; break;
1115         case ISD::OR:  Opc = Alpha::ORNOT; break;
1116         case ISD::XOR: Opc = Alpha::EQV; break;
1117       }
1118       Tmp1 = SelectExpr(N.getOperand(0));
1119       Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1120       BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1121       return Result;
1122     }
1123     //Fall through
1124   case ISD::SHL:
1125   case ISD::SRL:
1126   case ISD::SRA:
1127   case ISD::MUL:
1128     if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255)) {
1129       switch(opcode) {
1130       case ISD::AND: Opc = Alpha::ANDi; break;
1131       case ISD::OR:  Opc = Alpha::BISi; break;
1132       case ISD::XOR: Opc = Alpha::XORi; break;
1133       case ISD::SHL: Opc = Alpha::SLi; break;
1134       case ISD::SRL: Opc = Alpha::SRLi; break;
1135       case ISD::SRA: Opc = Alpha::SRAi; break;
1136       case ISD::MUL: Opc = Alpha::MULQi; break;
1137       };
1138       Tmp1 = SelectExpr(N.getOperand(0));
1139       BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
1140     } else {
1141       switch(opcode) {
1142       case ISD::AND: Opc = Alpha::AND; break;
1143       case ISD::OR:  Opc = Alpha::BIS; break;
1144       case ISD::XOR: Opc = Alpha::XOR; break;
1145       case ISD::SHL: Opc = Alpha::SL; break;
1146       case ISD::SRL: Opc = Alpha::SRL; break;
1147       case ISD::SRA: Opc = Alpha::SRA; break;
1148       case ISD::MUL: Opc = Alpha::MULQ; break;
1149       };
1150       Tmp1 = SelectExpr(N.getOperand(0));
1151       Tmp2 = SelectExpr(N.getOperand(1));
1152       BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1153     }
1154     return Result;
1155
1156   case ISD::ADD:
1157   case ISD::SUB:
1158     {
1159       bool isAdd = opcode == ISD::ADD;
1160
1161       //first check for Scaled Adds and Subs!
1162       //Valid for add and sub
1163       if(N.getOperand(0).getOpcode() == ISD::SHL && 
1164          isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
1165          (SImm == 2 || SImm == 3)) {
1166         bool use4 = SImm == 2;
1167         Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1168         if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
1169           BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1170                   2, Result).addReg(Tmp2).addImm(SImm);
1171         else {
1172           Tmp1 = SelectExpr(N.getOperand(1));
1173           BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1174                   2, Result).addReg(Tmp2).addReg(Tmp1);
1175         }
1176       }
1177       //Position prevents subs
1178       else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
1179               isSIntImmediate(N.getOperand(1).getOperand(1), SImm) &&
1180               (SImm == 2 || SImm == 3)) {
1181         bool use4 = SImm == 2;
1182         Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1183         if (isSIntImmediateBounded(N.getOperand(0), SImm, 0, 255))
1184           BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2).addImm(SImm);
1185         else {
1186           Tmp1 = SelectExpr(N.getOperand(0));
1187           BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1188         }
1189       }
1190       //small addi
1191       else if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
1192       { //Normal imm add/sub
1193         Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1194         Tmp1 = SelectExpr(N.getOperand(0));
1195         BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
1196       }
1197       else if(isSIntImmediateBounded(N.getOperand(1), SImm, -255, 0))
1198       { //inverted imm add/sub
1199         Opc = isAdd ? Alpha::SUBQi : Alpha::ADDQi;
1200         Tmp1 = SelectExpr(N.getOperand(0));
1201         BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(-SImm);
1202       }
1203       //larger addi
1204       else if(isSIntImmediateBounded(N.getOperand(1), SImm, -32767, 32767))
1205       { //LDA
1206         Tmp1 = SelectExpr(N.getOperand(0));
1207         if (!isAdd)
1208           SImm = -SImm;
1209         BuildMI(BB, Alpha::LDA, 2, Result).addImm(SImm).addReg(Tmp1);
1210       }
1211       //give up and do the operation
1212       else {
1213         //Normal add/sub
1214         Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1215         Tmp1 = SelectExpr(N.getOperand(0));
1216         Tmp2 = SelectExpr(N.getOperand(1));
1217         BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1218       }
1219       return Result;
1220     }
1221   case ISD::FADD:
1222   case ISD::FSUB:
1223   case ISD::FMUL:
1224   case ISD::FDIV: {
1225     if (opcode == ISD::FADD)
1226       Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS;
1227     else if (opcode == ISD::FSUB)
1228       Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS;
1229     else if (opcode == ISD::FMUL)
1230       Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS;
1231     else
1232       Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS;
1233     Tmp1 = SelectExpr(N.getOperand(0));
1234     Tmp2 = SelectExpr(N.getOperand(1));
1235     BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1236     return Result;
1237   }
1238   case ISD::SDIV:
1239     {
1240       //check if we can convert into a shift!
1241       if (isSIntImmediate(N.getOperand(1), SImm) &&
1242           SImm != 0 && isPowerOf2_64(llabs(SImm))) {
1243         unsigned k = Log2_64(llabs(SImm));
1244         Tmp1 = SelectExpr(N.getOperand(0));
1245         if (k == 1)
1246           Tmp2 = Tmp1;
1247         else
1248         {
1249           Tmp2 = MakeReg(MVT::i64);
1250           BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1251         }
1252         Tmp3 = MakeReg(MVT::i64);
1253         BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1254         unsigned Tmp4 = MakeReg(MVT::i64);
1255         BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
1256         if (SImm > 0)
1257           BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1258         else
1259         {
1260           unsigned Tmp5 = MakeReg(MVT::i64);
1261           BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1262           BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1263         }
1264         return Result;
1265       }
1266     }
1267     //Else fall through
1268   case ISD::UDIV:
1269     //else fall though
1270   case ISD::UREM:
1271   case ISD::SREM: {
1272     const char* opstr = 0;
1273     switch(opcode) {
1274     case ISD::UREM: opstr = "__remqu"; break;
1275     case ISD::SREM: opstr = "__remq";  break;
1276     case ISD::UDIV: opstr = "__divqu"; break;
1277     case ISD::SDIV: opstr = "__divq";  break;
1278     }
1279     Tmp1 = SelectExpr(N.getOperand(0));
1280     Tmp2 = SelectExpr(N.getOperand(1));
1281     SDOperand Addr =
1282       ISelDAG->getExternalSymbol(opstr, AlphaLowering.getPointerTy());
1283     Tmp3 = SelectExpr(Addr);
1284     //set up regs explicitly (helps Reg alloc)
1285     BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
1286     BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
1287     BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp3).addReg(Tmp3);
1288     BuildMI(BB, Alpha::JSRs, 2, Alpha::R23).addReg(Alpha::R27).addImm(0);
1289     BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
1290     return Result;
1291   }
1292
1293   case ISD::FP_TO_UINT:
1294   case ISD::FP_TO_SINT:
1295     {
1296       assert (DestType == MVT::i64 && "only quads can be loaded to");
1297       MVT::ValueType SrcType = N.getOperand(0).getValueType();
1298       assert (SrcType == MVT::f32 || SrcType == MVT::f64);
1299       Tmp1 = SelectExpr(N.getOperand(0));  // Get the operand register
1300       if (SrcType == MVT::f32)
1301         {
1302           Tmp2 = MakeReg(MVT::f64);
1303           BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1304           Tmp1 = Tmp2;
1305         }
1306       Tmp2 = MakeReg(MVT::f64);
1307       BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
1308       MoveFP2Int(Tmp2, Result, true);
1309
1310       return Result;
1311     }
1312
1313   case ISD::SELECT:
1314     if (isFP) {
1315       //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1316       unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1317       unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1318
1319       SDOperand CC = N.getOperand(0);
1320
1321       if (CC.getOpcode() == ISD::SETCC &&
1322           !MVT::isInteger(CC.getOperand(0).getValueType())) {
1323         //FP Setcc -> Select yay!
1324
1325
1326         //for a cmp b: c = a - b;
1327         //a = b: c = 0
1328         //a < b: c < 0
1329         //a > b: c > 0
1330
1331         bool invTest = false;
1332         unsigned Tmp3;
1333
1334         ConstantFPSDNode *CN;
1335         if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
1336             && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1337           Tmp3 = SelectExpr(CC.getOperand(0));
1338         else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
1339                  && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1340         {
1341           Tmp3 = SelectExpr(CC.getOperand(1));
1342           invTest = true;
1343         }
1344         else
1345         {
1346           unsigned Tmp1 = SelectExpr(CC.getOperand(0));
1347           unsigned Tmp2 = SelectExpr(CC.getOperand(1));
1348           bool isD = CC.getOperand(0).getValueType() == MVT::f64;
1349           Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1350           BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1351             .addReg(Tmp1).addReg(Tmp2);
1352         }
1353
1354         switch (cast<CondCodeSDNode>(CC.getOperand(2))->get()) {
1355         default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1356         case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1357         case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1358         case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1359         case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1360         case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1361         case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1362         }
1363         BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
1364         return Result;
1365       }
1366       else
1367       {
1368         Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1369         BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV)
1370           .addReg(Tmp1);
1371 //         // Spill the cond to memory and reload it from there.
1372 //         unsigned Tmp4 = MakeReg(MVT::f64);
1373 //         MoveIntFP(Tmp1, Tmp4, true);
1374 //         //now ideally, we don't have to do anything to the flag...
1375 //         // Get the condition into the zero flag.
1376 //         BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
1377         return Result;
1378       }
1379     } else {
1380       //FIXME: look at parent to decide if intCC can be folded, or if setCC(FP)
1381       //and can save stack use
1382       //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1383       //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1384       //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1385       // Get the condition into the zero flag.
1386       //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1387
1388       SDOperand CC = N.getOperand(0);
1389
1390       if (CC.getOpcode() == ISD::SETCC &&
1391           !MVT::isInteger(CC.getOperand(0).getValueType()))
1392       { //FP Setcc -> Int Select
1393         Tmp1 = MakeReg(MVT::f64);
1394         Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1395         Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1396         bool inv = SelectFPSetCC(CC, Tmp1);
1397         BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
1398           .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1399         return Result;
1400       }
1401       if (CC.getOpcode() == ISD::SETCC) {
1402         //Int SetCC -> Select
1403         //Dropping the CC is only useful if we are comparing to 0
1404         if(isSIntImmediateBounded(CC.getOperand(1), SImm, 0, 0)) {
1405           //figure out a few things
1406           bool useImm = isSIntImmediateBounded(N.getOperand(2), SImm, 0, 255);
1407
1408           //Fix up CC
1409           ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
1410           if (useImm) //Invert sense to get Imm field right
1411             cCode = ISD::getSetCCInverse(cCode, true);
1412
1413           //Choose the CMOV
1414           switch (cCode) {
1415           default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
1416           case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ;     break;
1417           case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT;     break;
1418           case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE;     break;
1419           case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT;     break;
1420           case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE;     break;
1421           case ISD::SETULT: assert(0 && "unsigned < 0 is never true"); break;
1422           case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE;    break;
1423           //Technically you could have this CC
1424           case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ;    break;
1425           case ISD::SETUGE: assert(0 && "unsgined >= 0 is always true"); break;
1426           case ISD::SETNE:  Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE;    break;
1427           }
1428           Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
1429
1430           if (useImm) {
1431             Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
1432             BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addImm(SImm).addReg(Tmp1);
1433           } else {
1434             Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1435             Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1436             BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
1437           }
1438           return Result;
1439         }
1440         //Otherwise, fall though
1441       }
1442       Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1443       Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1444       Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1445       BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3)
1446         .addReg(Tmp1);
1447
1448       return Result;
1449     }
1450
1451   case ISD::Constant:
1452     {
1453       int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
1454       int zero_extend_top = 0;
1455       if (val > 0 && (val & 0xFFFFFFFF00000000ULL) == 0 &&
1456           ((int32_t)val < 0)) {
1457         //try a small load and zero extend
1458         val = (int32_t)val;
1459         zero_extend_top = 15;
1460       }
1461
1462       if (val <= IMM_HIGH && val >= IMM_LOW) {
1463         if(!zero_extend_top)
1464           BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
1465         else {
1466           Tmp1 = MakeReg(MVT::i64);
1467           BuildMI(BB, Alpha::LDA, 2, Tmp1).addImm(val).addReg(Alpha::R31);
1468           BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp1).addImm(zero_extend_top);
1469         }
1470       }
1471       else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
1472                val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
1473         Tmp1 = MakeReg(MVT::i64);
1474         BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val))
1475           .addReg(Alpha::R31);
1476         if (!zero_extend_top)
1477           BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
1478         else {
1479           Tmp3 = MakeReg(MVT::i64);
1480           BuildMI(BB, Alpha::LDA, 2, Tmp3).addImm(getLower16(val)).addReg(Tmp1);
1481           BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp3).addImm(zero_extend_top);
1482         }
1483       }
1484       else {
1485         //re-get the val since we are going to mem anyway
1486         val = (int64_t)cast<ConstantSDNode>(N)->getValue();
1487         MachineConstantPool *CP = BB->getParent()->getConstantPool();
1488         ConstantUInt *C =
1489           ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
1490         unsigned CPI = CP->getConstantPoolIndex(C);
1491         AlphaLowering.restoreGP(BB);
1492         has_sym = true;
1493         Tmp1 = MakeReg(MVT::i64);
1494         BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPI)
1495           .addReg(Alpha::R29);
1496         if (EnableAlphaLSMark)
1497           BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
1498             .addImm(getUID());
1499         BuildMI(BB, Alpha::LDQr, 2, Result).addConstantPoolIndex(CPI)
1500           .addReg(Tmp1);
1501       }
1502       return Result;
1503     }
1504   case ISD::FNEG:
1505     if(ISD::FABS == N.getOperand(0).getOpcode())
1506       {
1507         Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1508         BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYSNT : Alpha::CPYSNS, 
1509                 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1510       } else {
1511         Tmp1 = SelectExpr(N.getOperand(0));
1512         BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYSNT : Alpha::CPYSNS
1513                 , 2, Result).addReg(Tmp1).addReg(Tmp1);
1514       }
1515     return Result;
1516
1517   case ISD::FABS:
1518     Tmp1 = SelectExpr(N.getOperand(0));
1519     BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYST : Alpha::CPYSS, 2, Result)
1520       .addReg(Alpha::F31).addReg(Tmp1);
1521     return Result;
1522
1523   case ISD::FP_ROUND:
1524     assert (DestType == MVT::f32 &&
1525             N.getOperand(0).getValueType() == MVT::f64 &&
1526             "only f64 to f32 conversion supported here");
1527     Tmp1 = SelectExpr(N.getOperand(0));
1528     BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Alpha::F31).addReg(Tmp1);
1529     return Result;
1530
1531   case ISD::FP_EXTEND:
1532     assert (DestType == MVT::f64 &&
1533             N.getOperand(0).getValueType() == MVT::f32 &&
1534             "only f32 to f64 conversion supported here");
1535     Tmp1 = SelectExpr(N.getOperand(0));
1536     BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
1537     return Result;
1538
1539   case ISD::ConstantFP:
1540     if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1541       if (CN->isExactlyValue(+0.0)) {
1542         BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYST : Alpha::CPYSS
1543                 , 2, Result).addReg(Alpha::F31)
1544           .addReg(Alpha::F31);
1545       } else if ( CN->isExactlyValue(-0.0)) {
1546         BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYSNT : Alpha::CPYSNS,
1547                 2, Result).addReg(Alpha::F31)
1548           .addReg(Alpha::F31);
1549       } else {
1550         abort();
1551       }
1552     }
1553     return Result;
1554
1555   case ISD::SINT_TO_FP:
1556     {
1557       assert (N.getOperand(0).getValueType() == MVT::i64
1558               && "only quads can be loaded from");
1559       Tmp1 = SelectExpr(N.getOperand(0));  // Get the operand register
1560       Tmp2 = MakeReg(MVT::f64);
1561       MoveInt2FP(Tmp1, Tmp2, true);
1562       Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1563       BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1564       return Result;
1565     }
1566
1567   case ISD::AssertSext:
1568   case ISD::AssertZext:
1569     return SelectExpr(N.getOperand(0));
1570
1571   }
1572
1573   return 0;
1574 }
1575
1576 void AlphaISel::Select(SDOperand N) {
1577   unsigned Tmp1, Tmp2, Opc;
1578   unsigned opcode = N.getOpcode();
1579
1580   if (!ExprMap.insert(std::make_pair(N, notIn)).second)
1581     return;  // Already selected.
1582
1583   SDNode *Node = N.Val;
1584
1585   switch (opcode) {
1586
1587   default:
1588     Node->dump(); std::cerr << "\n";
1589     assert(0 && "Node not handled yet!");
1590
1591   case ISD::BRCOND: {
1592     SelectBranchCC(N);
1593     return;
1594   }
1595
1596   case ISD::BR: {
1597     MachineBasicBlock *Dest =
1598       cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1599
1600     Select(N.getOperand(0));
1601     BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1602     return;
1603   }
1604
1605   case ISD::ImplicitDef:
1606     ++count_ins;
1607     Select(N.getOperand(0));
1608     BuildMI(BB, Alpha::IDEF, 0,
1609             cast<RegisterSDNode>(N.getOperand(1))->getReg());
1610     return;
1611
1612   case ISD::EntryToken: return;  // Noop
1613
1614   case ISD::TokenFactor:
1615     for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1616       Select(Node->getOperand(i));
1617
1618     //N.Val->dump(); std::cerr << "\n";
1619     //assert(0 && "Node not handled yet!");
1620
1621     return;
1622
1623   case ISD::CopyToReg:
1624     ++count_outs;
1625     Select(N.getOperand(0));
1626     Tmp1 = SelectExpr(N.getOperand(2));
1627     Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
1628
1629     if (Tmp1 != Tmp2) {
1630       switch(N.getOperand(2).getValueType()) {
1631       case MVT::f64:
1632         BuildMI(BB, Alpha::CPYST, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1633         break;
1634       case MVT::f32:
1635         BuildMI(BB, Alpha::CPYSS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1636         break;
1637       default:
1638         BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1639         break;
1640       }
1641     }
1642     return;
1643
1644   case ISD::RET:
1645     ++count_outs;
1646     switch (N.getNumOperands()) {
1647     default:
1648       std::cerr << N.getNumOperands() << "\n";
1649       for (unsigned i = 0; i < N.getNumOperands(); ++i)
1650         std::cerr << N.getOperand(i).getValueType() << "\n";
1651       Node->dump();
1652       assert(0 && "Unknown return instruction!");
1653     case 2:
1654       Select(N.getOperand(0));
1655       Tmp1 = SelectExpr(N.getOperand(1));
1656       switch (N.getOperand(1).getValueType()) {
1657       default: Node->dump();
1658         assert(0 && "All other types should have been promoted!!");
1659       case MVT::f64:
1660         BuildMI(BB, Alpha::CPYST, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1661         break;
1662       case MVT::f32:
1663         BuildMI(BB, Alpha::CPYSS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1664         break;
1665       case MVT::i32:
1666       case MVT::i64:
1667         BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1668         break;
1669       }
1670       break;
1671     case 1:
1672       Select(N.getOperand(0));
1673       break;
1674     }
1675     // Just emit a 'ret' instruction
1676     AlphaLowering.restoreRA(BB);
1677     BuildMI(BB, Alpha::RET, 2, Alpha::R31).addReg(Alpha::R26).addImm(1);
1678     return;
1679
1680   case ISD::TRUNCSTORE:
1681   case ISD::STORE:
1682     {
1683       SDOperand Chain   = N.getOperand(0);
1684       SDOperand Value = N.getOperand(1);
1685       SDOperand Address = N.getOperand(2);
1686       Select(Chain);
1687
1688       Tmp1 = SelectExpr(Value); //value
1689
1690       if (opcode == ISD::STORE) {
1691         switch(Value.getValueType()) {
1692         default: assert(0 && "unknown Type in store");
1693         case MVT::i64: Opc = Alpha::STQ; break;
1694         case MVT::f64: Opc = Alpha::STT; break;
1695         case MVT::f32: Opc = Alpha::STS; break;
1696         }
1697       } else { //ISD::TRUNCSTORE
1698         switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
1699         default: assert(0 && "unknown Type in store");
1700         case MVT::i8: Opc = Alpha::STB; break;
1701         case MVT::i16: Opc = Alpha::STW; break;
1702         case MVT::i32: Opc = Alpha::STL; break;
1703         }
1704       }
1705
1706       int i, j, k;
1707       if (EnableAlphaLSMark)
1708         getValueInfo(cast<SrcValueSDNode>(N.getOperand(3))->getValue(),
1709                      i, j, k);
1710
1711       GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
1712       if (GASD && !GASD->getGlobal()->isExternal()) {
1713         Tmp2 = MakeReg(MVT::i64);
1714         AlphaLowering.restoreGP(BB);
1715         BuildMI(BB, Alpha::LDAHr, 2, Tmp2)
1716           .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
1717         if (EnableAlphaLSMark)
1718           BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1719             .addImm(getUID());
1720         BuildMI(BB, GetRelVersion(Opc), 3).addReg(Tmp1)
1721           .addGlobalAddress(GASD->getGlobal()).addReg(Tmp2);
1722       } else if(Address.getOpcode() == ISD::FrameIndex) {
1723         if (EnableAlphaLSMark)
1724           BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1725             .addImm(getUID());
1726         BuildMI(BB, Opc, 3).addReg(Tmp1)
1727           .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1728           .addReg(Alpha::F31);
1729       } else {
1730         long offset;
1731         SelectAddr(Address, Tmp2, offset);
1732         if (EnableAlphaLSMark)
1733           BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1734             .addImm(getUID());
1735         BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1736       }
1737       return;
1738     }
1739
1740   case ISD::EXTLOAD:
1741   case ISD::SEXTLOAD:
1742   case ISD::ZEXTLOAD:
1743   case ISD::LOAD:
1744   case ISD::CopyFromReg:
1745   case ISD::TAILCALL:
1746   case ISD::CALL:
1747   case ISD::DYNAMIC_STACKALLOC:
1748     ExprMap.erase(N);
1749     SelectExpr(N);
1750     return;
1751
1752   case ISD::CALLSEQ_START:
1753   case ISD::CALLSEQ_END:
1754     Select(N.getOperand(0));
1755     Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1756
1757     Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
1758       Alpha::ADJUSTSTACKUP;
1759     BuildMI(BB, Opc, 1).addImm(Tmp1);
1760     return;
1761
1762   case ISD::PCMARKER:
1763     Select(N.getOperand(0)); //Chain
1764     BuildMI(BB, Alpha::PCLABEL, 2)
1765       .addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
1766     return;
1767   }
1768   assert(0 && "Should not be reached!");
1769 }
1770
1771
1772 /// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1773 /// into a machine code representation using pattern matching and a machine
1774 /// description file.
1775 ///
1776 FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
1777   return new AlphaISel(TM);
1778 }
1779