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