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