Switch to AlphaISD::CALL instead of ISD::CALL
[oota-llvm.git] / lib / Target / Alpha / AlphaISelLowering.cpp
1 //===-- AlphaISelLowering.cpp - Alpha DAG Lowering Implementation ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Andrew Lenharth and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the AlphaISelLowering class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "AlphaISelLowering.h"
15 #include "AlphaTargetMachine.h"
16 #include "llvm/CodeGen/MachineFrameInfo.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/SelectionDAG.h"
20 #include "llvm/CodeGen/SSARegMap.h"
21 #include "llvm/Constants.h"
22 #include "llvm/Function.h"
23 #include "llvm/Module.h"
24 #include "llvm/Support/CommandLine.h"
25 #include <iostream>
26
27 using namespace llvm;
28
29 namespace llvm {
30   cl::opt<bool> EnableAlphaLSMark("enable-alpha-lsmark",
31     cl::desc("Emit symbols to correlate Mem ops to LLVM Values"),
32     cl::Hidden);
33 }
34
35 /// AddLiveIn - This helper function adds the specified physical register to the
36 /// MachineFunction as a live in value.  It also creates a corresponding virtual
37 /// register for it.
38 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
39                           TargetRegisterClass *RC) {
40   assert(RC->contains(PReg) && "Not the correct regclass!");
41   unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
42   MF.addLiveIn(PReg, VReg);
43   return VReg;
44 }
45
46 AlphaTargetLowering::AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
47   // Set up the TargetLowering object.
48   //I am having problems with shr n ubyte 1
49   setShiftAmountType(MVT::i64);
50   setSetCCResultType(MVT::i64);
51   setSetCCResultContents(ZeroOrOneSetCCResult);
52   
53   addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
54   addRegisterClass(MVT::f64, Alpha::F8RCRegisterClass);
55   addRegisterClass(MVT::f32, Alpha::F4RCRegisterClass);
56   
57   setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
58   setOperationAction(ISD::BRTWOWAY_CC,  MVT::Other, Expand);
59   
60   setOperationAction(ISD::EXTLOAD, MVT::i1,  Promote);
61   setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
62   
63   setOperationAction(ISD::ZEXTLOAD, MVT::i1,  Promote);
64   setOperationAction(ISD::ZEXTLOAD, MVT::i32, Expand);
65   
66   setOperationAction(ISD::SEXTLOAD, MVT::i1,  Promote);
67   setOperationAction(ISD::SEXTLOAD, MVT::i8,  Expand);
68   setOperationAction(ISD::SEXTLOAD, MVT::i16, Expand);
69   
70   setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
71
72   if (EnableAlphaLSMark) {
73     setOperationAction(ISD::LOAD, MVT::i64, Custom);
74     setOperationAction(ISD::LOAD, MVT::f64, Custom);
75     setOperationAction(ISD::LOAD, MVT::f32, Custom);
76
77     setOperationAction(ISD::ZEXTLOAD, MVT::i8,  Custom);
78     setOperationAction(ISD::ZEXTLOAD, MVT::i16, Custom);
79     setOperationAction(ISD::SEXTLOAD, MVT::i32, Custom);
80
81     setOperationAction(ISD::EXTLOAD, MVT::i8,  Custom);
82     setOperationAction(ISD::EXTLOAD, MVT::i16, Custom);
83     setOperationAction(ISD::EXTLOAD, MVT::i32, Custom);
84   }
85
86   setOperationAction(ISD::FREM, MVT::f32, Expand);
87   setOperationAction(ISD::FREM, MVT::f64, Expand);
88   
89   setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
90   setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
91   setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
92   setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
93
94   if (!TM.getSubtarget<AlphaSubtarget>().hasCT()) {
95     setOperationAction(ISD::CTPOP    , MVT::i64  , Expand);
96     setOperationAction(ISD::CTTZ     , MVT::i64  , Expand);
97     setOperationAction(ISD::CTLZ     , MVT::i64  , Expand);
98   }
99   setOperationAction(ISD::BSWAP    , MVT::i64, Expand);
100   setOperationAction(ISD::ROTL     , MVT::i64, Expand);
101   setOperationAction(ISD::ROTR     , MVT::i64, Expand);
102   
103   setOperationAction(ISD::SREM     , MVT::i64, Custom);
104   setOperationAction(ISD::UREM     , MVT::i64, Custom);
105   setOperationAction(ISD::SDIV     , MVT::i64, Custom);
106   setOperationAction(ISD::UDIV     , MVT::i64, Custom);
107   
108   setOperationAction(ISD::MEMMOVE  , MVT::Other, Expand);
109   setOperationAction(ISD::MEMSET   , MVT::Other, Expand);
110   setOperationAction(ISD::MEMCPY   , MVT::Other, Expand);
111   
112   // We don't support sin/cos/sqrt
113   setOperationAction(ISD::FSIN , MVT::f64, Expand);
114   setOperationAction(ISD::FCOS , MVT::f64, Expand);
115   setOperationAction(ISD::FSIN , MVT::f32, Expand);
116   setOperationAction(ISD::FCOS , MVT::f32, Expand);
117
118   setOperationAction(ISD::FSQRT, MVT::f64, Expand);
119   setOperationAction(ISD::FSQRT, MVT::f32, Expand);
120
121   setOperationAction(ISD::SETCC, MVT::f32, Promote);
122
123   // We don't have line number support yet.
124   setOperationAction(ISD::LOCATION, MVT::Other, Expand);
125   setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
126   setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
127
128   // Not implemented yet.
129   setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); 
130   setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
131   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
132
133   // We want to legalize GlobalAddress and ConstantPool and
134   // ExternalSymbols nodes into the appropriate instructions to
135   // materialize the address.
136   setOperationAction(ISD::GlobalAddress,  MVT::i64, Custom);
137   setOperationAction(ISD::ConstantPool,   MVT::i64, Custom);
138   setOperationAction(ISD::ExternalSymbol, MVT::i64, Custom);
139
140   setOperationAction(ISD::VASTART, MVT::Other, Custom);
141   setOperationAction(ISD::VAEND,   MVT::Other, Expand);
142   setOperationAction(ISD::VACOPY,  MVT::Other, Custom);
143   setOperationAction(ISD::VAARG,   MVT::Other, Custom);
144
145   setStackPointerRegisterToSaveRestore(Alpha::R30);
146
147   addLegalFPImmediate(+0.0); //F31
148   addLegalFPImmediate(-0.0); //-F31
149
150   computeRegisterProperties();
151
152   useITOF = TM.getSubtarget<AlphaSubtarget>().hasF2I();
153 }
154
155 const char *AlphaTargetLowering::getTargetNodeName(unsigned Opcode) const {
156   switch (Opcode) {
157   default: return 0;
158   case AlphaISD::ITOFT_: return "Alpha::ITOFT_";
159   case AlphaISD::FTOIT_: return "Alpha::FTOIT_";
160   case AlphaISD::CVTQT_: return "Alpha::CVTQT_";
161   case AlphaISD::CVTQS_: return "Alpha::CVTQS_";
162   case AlphaISD::CVTTQ_: return "Alpha::CVTTQ_";
163   case AlphaISD::GPRelHi: return "Alpha::GPRelHi";
164   case AlphaISD::GPRelLo: return "Alpha::GPRelLo";
165   case AlphaISD::RelLit: return "Alpha::RelLit";
166   case AlphaISD::GlobalBaseReg: return "Alpha::GlobalBaseReg";
167   case AlphaISD::CALL:   return "Alpha::CALL";
168   case AlphaISD::DivCall: return "Alpha::DivCall";
169   case AlphaISD::LDQ_: return "Alpha::LDQ_";
170   case AlphaISD::LDT_: return "Alpha::LDT_";
171   case AlphaISD::LDS_: return "Alpha::LDS_";
172   case AlphaISD::LDL_: return "Alpha::LDL_";
173   case AlphaISD::LDWU_: return "Alpha::LDWU_";
174   case AlphaISD::LDBU_:  return "Alpha::LDBU_";
175   case AlphaISD::STQ_: return "Alpha::STQ_";
176   case AlphaISD::STT_: return "Alpha::STT_";
177   case AlphaISD::STS_: return "Alpha::STS_";
178   case AlphaISD::STL_: return "Alpha::STL_";
179   case AlphaISD::STW_: return "Alpha::STW_";
180   case AlphaISD::STB_:  return "Alpha::STB_";
181   }
182 }
183
184 //http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
185
186 //For now, just use variable size stack frame format
187
188 //In a standard call, the first six items are passed in registers $16
189 //- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
190 //of argument-to-register correspondence.) The remaining items are
191 //collected in a memory argument list that is a naturally aligned
192 //array of quadwords. In a standard call, this list, if present, must
193 //be passed at 0(SP).
194 //7 ... n         0(SP) ... (n-7)*8(SP)
195
196 // //#define FP    $15
197 // //#define RA    $26
198 // //#define PV    $27
199 // //#define GP    $29
200 // //#define SP    $30
201
202 std::vector<SDOperand>
203 AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
204 {
205   MachineFunction &MF = DAG.getMachineFunction();
206   MachineFrameInfo *MFI = MF.getFrameInfo();
207   MachineBasicBlock& BB = MF.front();
208   std::vector<SDOperand> ArgValues;
209
210   unsigned args_int[] = {
211     Alpha::R16, Alpha::R17, Alpha::R18, Alpha::R19, Alpha::R20, Alpha::R21};
212   unsigned args_float[] = {
213     Alpha::F16, Alpha::F17, Alpha::F18, Alpha::F19, Alpha::F20, Alpha::F21};
214
215   int count = 0;
216
217   GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
218   RA = AddLiveIn(MF, Alpha::R26, getRegClassFor(MVT::i64));
219
220   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
221   {
222     SDOperand argt;
223     if (count  < 6) {
224       unsigned Vreg;
225       MVT::ValueType VT = getValueType(I->getType());
226       switch (VT) {
227       default:
228         std::cerr << "Unknown Type " << VT << "\n";
229         abort();
230       case MVT::f64:
231       case MVT::f32:
232         args_float[count] = AddLiveIn(MF, args_float[count], getRegClassFor(VT));
233         argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[count], VT);
234         DAG.setRoot(argt.getValue(1));
235         break;
236       case MVT::i1:
237       case MVT::i8:
238       case MVT::i16:
239       case MVT::i32:
240       case MVT::i64:
241         args_int[count] = AddLiveIn(MF, args_int[count], getRegClassFor(MVT::i64));
242         argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[count], MVT::i64);
243         DAG.setRoot(argt.getValue(1));
244         if (VT != MVT::i64) {
245           unsigned AssertOp = 
246             I->getType()->isSigned() ? ISD::AssertSext : ISD::AssertZext;
247           argt = DAG.getNode(AssertOp, MVT::i64, argt, 
248                              DAG.getValueType(VT));
249           argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
250         }
251         break;
252       }
253     } else { //more args
254       // Create the frame index object for this incoming parameter...
255       int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
256
257       // Create the SelectionDAG nodes corresponding to a load
258       //from this parameter
259       SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
260       argt = DAG.getLoad(getValueType(I->getType()),
261                          DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
262     }
263     ++count;
264     ArgValues.push_back(argt);
265   }
266
267   // If the functions takes variable number of arguments, copy all regs to stack
268   if (F.isVarArg()) {
269     VarArgsOffset = count * 8;
270     std::vector<SDOperand> LS;
271     for (int i = 0; i < 6; ++i) {
272       if (MRegisterInfo::isPhysicalRegister(args_int[i]))
273         args_int[i] = AddLiveIn(MF, args_int[i], getRegClassFor(MVT::i64));
274       SDOperand argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[i], MVT::i64);
275       int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
276       if (i == 0) VarArgsBase = FI;
277       SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
278       LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
279                                SDFI, DAG.getSrcValue(NULL)));
280
281       if (MRegisterInfo::isPhysicalRegister(args_float[i]))
282         args_float[i] = AddLiveIn(MF, args_float[i], getRegClassFor(MVT::f64));
283       argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[i], MVT::f64);
284       FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
285       SDFI = DAG.getFrameIndex(FI, MVT::i64);
286       LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
287                                SDFI, DAG.getSrcValue(NULL)));
288     }
289
290     //Set up a token factor with all the stack traffic
291     DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
292   }
293
294   // Finally, inform the code generator which regs we return values in.
295   switch (getValueType(F.getReturnType())) {
296   default: assert(0 && "Unknown type!");
297   case MVT::isVoid: break;
298   case MVT::i1:
299   case MVT::i8:
300   case MVT::i16:
301   case MVT::i32:
302   case MVT::i64:
303     MF.addLiveOut(Alpha::R0);
304     break;
305   case MVT::f32:
306   case MVT::f64:
307     MF.addLiveOut(Alpha::F0);
308     break;
309   }
310
311   //return the arguments
312   return ArgValues;
313 }
314
315 std::pair<SDOperand, SDOperand>
316 AlphaTargetLowering::LowerCallTo(SDOperand Chain,
317                                  const Type *RetTy, bool isVarArg,
318                                  unsigned CallingConv, bool isTailCall,
319                                  SDOperand Callee, ArgListTy &Args,
320                                  SelectionDAG &DAG) {
321   int NumBytes = 0;
322   if (Args.size() > 6)
323     NumBytes = (Args.size() - 6) * 8;
324
325   Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
326                       DAG.getConstant(NumBytes, getPointerTy()));
327   std::vector<SDOperand> args_to_use;
328   for (unsigned i = 0, e = Args.size(); i != e; ++i)
329   {
330     switch (getValueType(Args[i].second)) {
331     default: assert(0 && "Unexpected ValueType for argument!");
332     case MVT::i1:
333     case MVT::i8:
334     case MVT::i16:
335     case MVT::i32:
336       // Promote the integer to 64 bits.  If the input type is signed use a
337       // sign extend, otherwise use a zero extend.
338       if (Args[i].second->isSigned())
339         Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
340       else
341         Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
342       break;
343     case MVT::i64:
344     case MVT::f64:
345     case MVT::f32:
346       break;
347     }
348     args_to_use.push_back(Args[i].first);
349   }
350
351   std::vector<MVT::ValueType> RetVals;
352   MVT::ValueType RetTyVT = getValueType(RetTy);
353   MVT::ValueType ActualRetTyVT = RetTyVT;
354   if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i32)
355     ActualRetTyVT = MVT::i64;
356
357   if (RetTyVT != MVT::isVoid)
358     RetVals.push_back(ActualRetTyVT);
359   RetVals.push_back(MVT::Other);
360
361   std::vector<SDOperand> Ops;
362   Ops.push_back(Chain);
363   Ops.push_back(Callee);
364   Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
365   SDOperand TheCall = DAG.getNode(AlphaISD::CALL, RetVals, Ops);
366   Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
367   Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
368                       DAG.getConstant(NumBytes, getPointerTy()));
369   SDOperand RetVal = TheCall;
370
371   if (RetTyVT != ActualRetTyVT) {
372     RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
373                          MVT::i64, RetVal, DAG.getValueType(RetTyVT));
374     RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
375   }
376
377   return std::make_pair(RetVal, Chain);
378 }
379
380 void AlphaTargetLowering::restoreGP(MachineBasicBlock* BB)
381 {
382   BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
383 }
384 void AlphaTargetLowering::restoreRA(MachineBasicBlock* BB)
385 {
386   BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(RA).addReg(RA);
387 }
388
389
390
391 static void getValueInfo(const Value* v, int& type, int& fun, int& offset)
392 {
393   fun = type = offset = 0;
394   if (v == NULL) {
395     type = 0;
396   } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(v)) {
397     type = 1;
398     const Module* M = GV->getParent();
399     for(Module::const_global_iterator ii = M->global_begin(); &*ii != GV; ++ii)
400       ++offset;
401   } else if (const Argument* Arg = dyn_cast<Argument>(v)) {
402     type = 2;
403     const Function* F = Arg->getParent();
404     const Module* M = F->getParent();
405     for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
406       ++fun;
407     for(Function::const_arg_iterator ii = F->arg_begin(); &*ii != Arg; ++ii)
408       ++offset;
409   } else if (const Instruction* I = dyn_cast<Instruction>(v)) {
410     assert(dyn_cast<PointerType>(I->getType()));
411     type = 3;
412     const BasicBlock* bb = I->getParent();
413     const Function* F = bb->getParent();
414     const Module* M = F->getParent();
415     for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
416       ++fun;
417     for(Function::const_iterator ii = F->begin(); &*ii != bb; ++ii)
418       offset += ii->size();
419     for(BasicBlock::const_iterator ii = bb->begin(); &*ii != I; ++ii)
420       ++offset;
421   } else if (const Constant* C = dyn_cast<Constant>(v)) {
422     //Don't know how to look these up yet
423     type = 0;
424   } else {
425     assert(0 && "Error in value marking");
426   }
427   //type = 4: register spilling
428   //type = 5: global address loading or constant loading
429 }
430
431 static int getUID()
432 {
433   static int id = 0;
434   return ++id;
435 }
436
437 /// LowerOperation - Provide custom lowering hooks for some operations.
438 ///
439 SDOperand AlphaTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
440   switch (Op.getOpcode()) {
441   default: assert(0 && "Wasn't expecting to be able to lower this!"); 
442   case ISD::SINT_TO_FP: {
443     assert(MVT::i64 == Op.getOperand(0).getValueType() && 
444            "Unhandled SINT_TO_FP type in custom expander!");
445     SDOperand LD;
446     bool isDouble = MVT::f64 == Op.getValueType();
447     if (useITOF) {
448       LD = DAG.getNode(AlphaISD::ITOFT_, MVT::f64, Op.getOperand(0));
449     } else {
450       int FrameIdx =
451         DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
452       SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i64);
453       SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
454                                  Op.getOperand(0), FI, DAG.getSrcValue(0));
455       LD = DAG.getLoad(MVT::f64, ST, FI, DAG.getSrcValue(0));
456       }
457     SDOperand FP = DAG.getNode(isDouble?AlphaISD::CVTQT_:AlphaISD::CVTQS_,
458                                isDouble?MVT::f64:MVT::f32, LD);
459     return FP;
460   }
461   case ISD::FP_TO_SINT: {
462     bool isDouble = MVT::f64 == Op.getOperand(0).getValueType();
463     SDOperand src = Op.getOperand(0);
464
465     if (!isDouble) //Promote
466       src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, src);
467     
468     src = DAG.getNode(AlphaISD::CVTTQ_, MVT::f64, src);
469
470     if (useITOF) {
471       return DAG.getNode(AlphaISD::FTOIT_, MVT::i64, src);
472     } else {
473       int FrameIdx =
474         DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
475       SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i64);
476       SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
477                                  src, FI, DAG.getSrcValue(0));
478       return DAG.getLoad(MVT::i64, ST, FI, DAG.getSrcValue(0));
479       }
480   }
481   case ISD::ConstantPool: {
482     Constant *C = cast<ConstantPoolSDNode>(Op)->get();
483     SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i64);
484     
485     SDOperand Hi = DAG.getNode(AlphaISD::GPRelHi,  MVT::i64, CPI,
486                                DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
487     SDOperand Lo = DAG.getNode(AlphaISD::GPRelLo, MVT::i64, CPI, Hi);
488     return Lo;
489   }
490   case ISD::GlobalAddress: {
491     GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
492     GlobalValue *GV = GSDN->getGlobal();
493     SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i64, GSDN->getOffset());
494
495     if (!GV->hasWeakLinkage() && !GV->isExternal()) {
496       SDOperand Hi = DAG.getNode(AlphaISD::GPRelHi,  MVT::i64, GA,
497                                  DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
498       SDOperand Lo = DAG.getNode(AlphaISD::GPRelLo, MVT::i64, GA, Hi);
499       return Lo;
500     } else
501       return DAG.getNode(AlphaISD::RelLit, MVT::i64, GA, DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
502   }
503   case ISD::ExternalSymbol: {
504     return DAG.getNode(AlphaISD::RelLit, MVT::i64, 
505                        DAG.getTargetExternalSymbol(cast<ExternalSymbolSDNode>(Op)->getSymbol(), MVT::i64),
506                        DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
507   }
508
509   case ISD::SDIV:
510   case ISD::UDIV:
511   case ISD::UREM:
512   case ISD::SREM:
513     if (MVT::isInteger(Op.getValueType())) {
514       const char* opstr = 0;
515       switch(Op.getOpcode()) {
516       case ISD::UREM: opstr = "__remqu"; break;
517       case ISD::SREM: opstr = "__remq";  break;
518       case ISD::UDIV: opstr = "__divqu"; break;
519       case ISD::SDIV: opstr = "__divq";  break;
520       }
521       SDOperand Tmp1 = Op.getOperand(0),
522         Tmp2 = Op.getOperand(1),
523         Addr = DAG.getExternalSymbol(opstr, MVT::i64);
524       return DAG.getNode(AlphaISD::DivCall, MVT::i64, Addr, Tmp1, Tmp2);
525     }
526     break;
527
528   case ISD::LOAD:
529   case ISD::SEXTLOAD:
530   case ISD::ZEXTLOAD:
531   case ISD::EXTLOAD:
532     {
533       SDOperand Chain   = Op.getOperand(0);
534       SDOperand Address = Op.getOperand(1);
535
536       unsigned Opc;
537       unsigned opcode = Op.getOpcode();
538
539       if (opcode == ISD::LOAD)
540         switch (Op.Val->getValueType(0)) {
541         default: Op.Val->dump(); assert(0 && "Bad load!");
542         case MVT::i64: Opc = AlphaISD::LDQ_; break;
543         case MVT::f64: Opc = AlphaISD::LDT_; break;
544         case MVT::f32: Opc = AlphaISD::LDS_; break;
545         }
546       else
547         switch (cast<VTSDNode>(Op.getOperand(3))->getVT()) {
548         default: Op.Val->dump(); assert(0 && "Bad sign extend!");
549         case MVT::i32: Opc = AlphaISD::LDL_;
550           assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
551         case MVT::i16: Opc = AlphaISD::LDWU_;
552           assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
553         case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
554         case MVT::i8: Opc = AlphaISD::LDBU_;
555           assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
556         }
557
558       int i, j, k;
559       getValueInfo(dyn_cast<SrcValueSDNode>(Op.getOperand(2))->getValue(), i, j, k);
560
561       SDOperand Zero = DAG.getConstant(0, MVT::i64);
562       std::vector<MVT::ValueType> VTS;
563       VTS.push_back(Op.Val->getValueType(0));
564       VTS.push_back(MVT::Other);
565       std::vector<SDOperand> ARGS;
566       ARGS.push_back(Chain);
567       ARGS.push_back(Zero);
568       ARGS.push_back(Address);
569       ARGS.push_back(DAG.getConstant(i, MVT::i64));
570       ARGS.push_back(DAG.getConstant(j, MVT::i64));
571       ARGS.push_back(DAG.getConstant(k, MVT::i64));
572       ARGS.push_back(DAG.getConstant(getUID(), MVT::i64));
573       return DAG.getNode(Opc, VTS, ARGS);
574     }
575
576   case ISD::TRUNCSTORE:
577   case ISD::STORE:
578     {
579       SDOperand Chain   = Op.getOperand(0);
580       SDOperand Value = Op.getOperand(1);
581       SDOperand Address = Op.getOperand(2);
582
583       unsigned Opc;
584       unsigned opcode = Op.getOpcode();
585
586       if (opcode == ISD::STORE) {
587         switch(Value.getValueType()) {
588         default: assert(0 && "unknown Type in store");
589         case MVT::i64: Opc = AlphaISD::STQ_; break;
590         case MVT::f64: Opc = AlphaISD::STT_; break;
591         case MVT::f32: Opc = AlphaISD::STS_; break;
592         }
593       } else { //ISD::TRUNCSTORE
594         switch(cast<VTSDNode>(Op.getOperand(4))->getVT()) {
595         default: assert(0 && "unknown Type in store");
596         case MVT::i8: Opc = AlphaISD::STB_; break;
597         case MVT::i16: Opc = AlphaISD::STW_; break;
598         case MVT::i32: Opc = AlphaISD::STL_; break;
599         }
600       }
601
602       int i, j, k;
603       getValueInfo(cast<SrcValueSDNode>(Op.getOperand(3))->getValue(), i, j, k);
604
605       SDOperand Zero = DAG.getConstant(0, MVT::i64);
606       std::vector<MVT::ValueType> VTS;
607       VTS.push_back(MVT::Other);
608       std::vector<SDOperand> ARGS;
609       ARGS.push_back(Chain);
610       ARGS.push_back(Value);
611       ARGS.push_back(Zero);
612       ARGS.push_back(Address);
613       ARGS.push_back(DAG.getConstant(i, MVT::i64));
614       ARGS.push_back(DAG.getConstant(j, MVT::i64));
615       ARGS.push_back(DAG.getConstant(k, MVT::i64));
616       ARGS.push_back(DAG.getConstant(getUID(), MVT::i64));
617       return DAG.getNode(Opc, VTS, ARGS);
618     }
619   case ISD::VAARG: {
620     SDOperand Chain = Op.getOperand(0);
621     SDOperand VAListP = Op.getOperand(1);
622     SDOperand VAListS = Op.getOperand(2);
623     
624     SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAListP, VAListS);
625     SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
626                                 DAG.getConstant(8, MVT::i64));
627     SDOperand Offset = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Base.getValue(1),
628                                       Tmp, DAG.getSrcValue(0), MVT::i32);
629     SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
630     if (MVT::isFloatingPoint(Op.getValueType()))
631     {
632       //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
633       SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
634                                         DAG.getConstant(8*6, MVT::i64));
635       SDOperand CC = DAG.getSetCC(MVT::i64, Offset,
636                                   DAG.getConstant(8*6, MVT::i64), ISD::SETLT);
637       DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
638     }
639
640     SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
641                                       DAG.getConstant(8, MVT::i64));
642     SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other,
643                                    Offset.getValue(1), NewOffset,
644                                    Tmp, DAG.getSrcValue(0),
645                                    DAG.getValueType(MVT::i32));
646     
647     SDOperand Result;
648     if (Op.getValueType() == MVT::i32)
649       Result = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Update, DataPtr,
650                               DAG.getSrcValue(0), MVT::i32);
651     else
652       Result = DAG.getLoad(Op.getValueType(), Update, DataPtr, 
653                            DAG.getSrcValue(0));
654     return Result;
655   }
656   case ISD::VACOPY: {
657     SDOperand Chain = Op.getOperand(0);
658     SDOperand DestP = Op.getOperand(1);
659     SDOperand SrcP = Op.getOperand(2);
660     SDOperand DestS = Op.getOperand(3);
661     SDOperand SrcS = Op.getOperand(4);
662     
663     SDOperand Val = DAG.getLoad(getPointerTy(), Chain, SrcP, SrcS);
664     SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1), Val,
665                                    DestP, DestS);
666     SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, SrcP, 
667                                DAG.getConstant(8, MVT::i64));
668     Val = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Result, NP,
669                          DAG.getSrcValue(0), MVT::i32);
670     SDOperand NPD = DAG.getNode(ISD::ADD, MVT::i64, DestP,
671                                 DAG.getConstant(8, MVT::i64));
672     return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
673                        Val, NPD, DAG.getSrcValue(0),DAG.getValueType(MVT::i32));
674   }
675   case ISD::VASTART: {
676     SDOperand Chain = Op.getOperand(0);
677     SDOperand VAListP = Op.getOperand(1);
678     SDOperand VAListS = Op.getOperand(2);
679     
680     // vastart stores the address of the VarArgsBase and VarArgsOffset
681     SDOperand FR  = DAG.getFrameIndex(VarArgsBase, MVT::i64);
682     SDOperand S1  = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
683                                 VAListS);
684     SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
685                                 DAG.getConstant(8, MVT::i64));
686     return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
687                        DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
688                        DAG.getSrcValue(0), DAG.getValueType(MVT::i32));
689   }
690   }
691
692   return SDOperand();
693 }