eliminate the TargetLowering::UsesGlobalOffsetTable bool, which is
[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 is distributed under the University of Illinois Open Source
6 // 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/CallingConvLower.h"
17 #include "llvm/CodeGen/MachineFrameInfo.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/CodeGen/SelectionDAG.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/CodeGen/PseudoSourceValue.h"
24 #include "llvm/Target/TargetLoweringObjectFile.h"
25 #include "llvm/Constants.h"
26 #include "llvm/Function.h"
27 #include "llvm/Module.h"
28 #include "llvm/Intrinsics.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/raw_ostream.h"
32 using namespace llvm;
33
34 /// AddLiveIn - This helper function adds the specified physical register to the
35 /// MachineFunction as a live in value.  It also creates a corresponding virtual
36 /// register for it.
37 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
38                           TargetRegisterClass *RC) {
39   assert(RC->contains(PReg) && "Not the correct regclass!");
40   unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
41   MF.getRegInfo().addLiveIn(PReg, VReg);
42   return VReg;
43 }
44
45 AlphaTargetLowering::AlphaTargetLowering(TargetMachine &TM)
46   : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
47   // Set up the TargetLowering object.
48   //I am having problems with shr n i8 1
49   setShiftAmountType(MVT::i64);
50   setBooleanContents(ZeroOrOneBooleanContent);
51
52   addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
53   addRegisterClass(MVT::f64, Alpha::F8RCRegisterClass);
54   addRegisterClass(MVT::f32, Alpha::F4RCRegisterClass);
55
56   // We want to custom lower some of our intrinsics.
57   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
58
59   setLoadExtAction(ISD::EXTLOAD, MVT::i1,  Promote);
60   setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
61
62   setLoadExtAction(ISD::ZEXTLOAD, MVT::i1,  Promote);
63   setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Expand);
64
65   setLoadExtAction(ISD::SEXTLOAD, MVT::i1,  Promote);
66   setLoadExtAction(ISD::SEXTLOAD, MVT::i8,  Expand);
67   setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Expand);
68
69   setTruncStoreAction(MVT::f64, MVT::f32, Expand);
70
71   //  setOperationAction(ISD::BRIND,        MVT::Other,   Expand);
72   setOperationAction(ISD::BR_JT,        MVT::Other, Expand);
73   setOperationAction(ISD::BR_CC,        MVT::Other, Expand);
74   setOperationAction(ISD::SELECT_CC,    MVT::Other, Expand);
75
76   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
77
78   setOperationAction(ISD::FREM, MVT::f32, Expand);
79   setOperationAction(ISD::FREM, MVT::f64, Expand);
80
81   setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
82   setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
83   setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
84   setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
85
86   if (!TM.getSubtarget<AlphaSubtarget>().hasCT()) {
87     setOperationAction(ISD::CTPOP    , MVT::i64  , Expand);
88     setOperationAction(ISD::CTTZ     , MVT::i64  , Expand);
89     setOperationAction(ISD::CTLZ     , MVT::i64  , Expand);
90   }
91   setOperationAction(ISD::BSWAP    , MVT::i64, Expand);
92   setOperationAction(ISD::ROTL     , MVT::i64, Expand);
93   setOperationAction(ISD::ROTR     , MVT::i64, Expand);
94
95   setOperationAction(ISD::SREM     , MVT::i64, Custom);
96   setOperationAction(ISD::UREM     , MVT::i64, Custom);
97   setOperationAction(ISD::SDIV     , MVT::i64, Custom);
98   setOperationAction(ISD::UDIV     , MVT::i64, Custom);
99
100   setOperationAction(ISD::ADDC     , MVT::i64, Expand);
101   setOperationAction(ISD::ADDE     , MVT::i64, Expand);
102   setOperationAction(ISD::SUBC     , MVT::i64, Expand);
103   setOperationAction(ISD::SUBE     , MVT::i64, Expand);
104
105   setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
106   setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
107
108   setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
109   setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
110   setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
111
112   // We don't support sin/cos/sqrt/pow
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::FPOW , MVT::f32, Expand);
122   setOperationAction(ISD::FPOW , MVT::f64, Expand);
123
124   setOperationAction(ISD::SETCC, MVT::f32, Promote);
125
126   setOperationAction(ISD::BIT_CONVERT, MVT::f32, Promote);
127
128   setOperationAction(ISD::EH_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   setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
142
143   setOperationAction(ISD::VASTART, MVT::Other, Custom);
144   setOperationAction(ISD::VAEND,   MVT::Other, Expand);
145   setOperationAction(ISD::VACOPY,  MVT::Other, Custom);
146   setOperationAction(ISD::VAARG,   MVT::Other, Custom);
147   setOperationAction(ISD::VAARG,   MVT::i32,   Custom);
148
149   setOperationAction(ISD::JumpTable, MVT::i64, Custom);
150   setOperationAction(ISD::JumpTable, MVT::i32, Custom);
151
152   setStackPointerRegisterToSaveRestore(Alpha::R30);
153
154   setJumpBufSize(272);
155   setJumpBufAlignment(16);
156
157   computeRegisterProperties();
158 }
159
160 MVT::SimpleValueType AlphaTargetLowering::getSetCCResultType(EVT VT) const {
161   return MVT::i64;
162 }
163
164 const char *AlphaTargetLowering::getTargetNodeName(unsigned Opcode) const {
165   switch (Opcode) {
166   default: return 0;
167   case AlphaISD::CVTQT_: return "Alpha::CVTQT_";
168   case AlphaISD::CVTQS_: return "Alpha::CVTQS_";
169   case AlphaISD::CVTTQ_: return "Alpha::CVTTQ_";
170   case AlphaISD::GPRelHi: return "Alpha::GPRelHi";
171   case AlphaISD::GPRelLo: return "Alpha::GPRelLo";
172   case AlphaISD::RelLit: return "Alpha::RelLit";
173   case AlphaISD::GlobalRetAddr: return "Alpha::GlobalRetAddr";
174   case AlphaISD::CALL:   return "Alpha::CALL";
175   case AlphaISD::DivCall: return "Alpha::DivCall";
176   case AlphaISD::RET_FLAG: return "Alpha::RET_FLAG";
177   case AlphaISD::COND_BRANCH_I: return "Alpha::COND_BRANCH_I";
178   case AlphaISD::COND_BRANCH_F: return "Alpha::COND_BRANCH_F";
179   }
180 }
181
182 /// getFunctionAlignment - Return the Log2 alignment of this function.
183 unsigned AlphaTargetLowering::getFunctionAlignment(const Function *F) const {
184   return 4;
185 }
186
187 static SDValue LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
188   EVT PtrVT = Op.getValueType();
189   JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
190   SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
191   // FIXME there isn't really any debug info here
192   DebugLoc dl = Op.getDebugLoc();
193
194   SDValue Hi = DAG.getNode(AlphaISD::GPRelHi,  dl, MVT::i64, JTI,
195                              DAG.getGLOBAL_OFFSET_TABLE(MVT::i64));
196   SDValue Lo = DAG.getNode(AlphaISD::GPRelLo, dl, MVT::i64, JTI, Hi);
197   return Lo;
198 }
199
200 //http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/
201 //AA-PY8AC-TET1_html/callCH3.html#BLOCK21
202
203 //For now, just use variable size stack frame format
204
205 //In a standard call, the first six items are passed in registers $16
206 //- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
207 //of argument-to-register correspondence.) The remaining items are
208 //collected in a memory argument list that is a naturally aligned
209 //array of quadwords. In a standard call, this list, if present, must
210 //be passed at 0(SP).
211 //7 ... n         0(SP) ... (n-7)*8(SP)
212
213 // //#define FP    $15
214 // //#define RA    $26
215 // //#define PV    $27
216 // //#define GP    $29
217 // //#define SP    $30
218
219 #include "AlphaGenCallingConv.inc"
220
221 SDValue
222 AlphaTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
223                                CallingConv::ID CallConv, bool isVarArg,
224                                bool isTailCall,
225                                const SmallVectorImpl<ISD::OutputArg> &Outs,
226                                const SmallVectorImpl<ISD::InputArg> &Ins,
227                                DebugLoc dl, SelectionDAG &DAG,
228                                SmallVectorImpl<SDValue> &InVals) {
229
230   // Analyze operands of the call, assigning locations to each operand.
231   SmallVector<CCValAssign, 16> ArgLocs;
232   CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
233                  ArgLocs, *DAG.getContext());
234
235   CCInfo.AnalyzeCallOperands(Outs, CC_Alpha);
236
237     // Get a count of how many bytes are to be pushed on the stack.
238   unsigned NumBytes = CCInfo.getNextStackOffset();
239
240   Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(NumBytes,
241                                                       getPointerTy(), true));
242
243   SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
244   SmallVector<SDValue, 12> MemOpChains;
245   SDValue StackPtr;
246
247   // Walk the register/memloc assignments, inserting copies/loads.
248   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
249     CCValAssign &VA = ArgLocs[i];
250
251     SDValue Arg = Outs[i].Val;
252
253     // Promote the value if needed.
254     switch (VA.getLocInfo()) {
255       default: assert(0 && "Unknown loc info!");
256       case CCValAssign::Full: break;
257       case CCValAssign::SExt:
258         Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
259         break;
260       case CCValAssign::ZExt:
261         Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
262         break;
263       case CCValAssign::AExt:
264         Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
265         break;
266     }
267
268     // Arguments that can be passed on register must be kept at RegsToPass
269     // vector
270     if (VA.isRegLoc()) {
271       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
272     } else {
273       assert(VA.isMemLoc());
274
275       if (StackPtr.getNode() == 0)
276         StackPtr = DAG.getCopyFromReg(Chain, dl, Alpha::R30, MVT::i64);
277
278       SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
279                                    StackPtr,
280                                    DAG.getIntPtrConstant(VA.getLocMemOffset()));
281
282       MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
283                                          PseudoSourceValue::getStack(), 0));
284     }
285   }
286
287   // Transform all store nodes into one single node because all store nodes are
288   // independent of each other.
289   if (!MemOpChains.empty())
290     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
291                         &MemOpChains[0], MemOpChains.size());
292
293   // Build a sequence of copy-to-reg nodes chained together with token chain and
294   // flag operands which copy the outgoing args into registers.  The InFlag in
295   // necessary since all emited instructions must be stuck together.
296   SDValue InFlag;
297   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
298     Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
299                              RegsToPass[i].second, InFlag);
300     InFlag = Chain.getValue(1);
301   }
302
303   // Returns a chain & a flag for retval copy to use.
304   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
305   SmallVector<SDValue, 8> Ops;
306   Ops.push_back(Chain);
307   Ops.push_back(Callee);
308
309   // Add argument registers to the end of the list so that they are
310   // known live into the call.
311   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
312     Ops.push_back(DAG.getRegister(RegsToPass[i].first,
313                                   RegsToPass[i].second.getValueType()));
314
315   if (InFlag.getNode())
316     Ops.push_back(InFlag);
317
318   Chain = DAG.getNode(AlphaISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
319   InFlag = Chain.getValue(1);
320
321   // Create the CALLSEQ_END node.
322   Chain = DAG.getCALLSEQ_END(Chain,
323                              DAG.getConstant(NumBytes, getPointerTy(), true),
324                              DAG.getConstant(0, getPointerTy(), true),
325                              InFlag);
326   InFlag = Chain.getValue(1);
327
328   // Handle result values, copying them out of physregs into vregs that we
329   // return.
330   return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
331                          Ins, dl, DAG, InVals);
332 }
333
334 /// LowerCallResult - Lower the result values of a call into the
335 /// appropriate copies out of appropriate physical registers.
336 ///
337 SDValue
338 AlphaTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
339                                      CallingConv::ID CallConv, bool isVarArg,
340                                      const SmallVectorImpl<ISD::InputArg> &Ins,
341                                      DebugLoc dl, SelectionDAG &DAG,
342                                      SmallVectorImpl<SDValue> &InVals) {
343
344   // Assign locations to each value returned by this call.
345   SmallVector<CCValAssign, 16> RVLocs;
346   CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
347                  *DAG.getContext());
348
349   CCInfo.AnalyzeCallResult(Ins, RetCC_Alpha);
350
351   // Copy all of the result registers out of their specified physreg.
352   for (unsigned i = 0; i != RVLocs.size(); ++i) {
353     CCValAssign &VA = RVLocs[i];
354
355     Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
356                                VA.getLocVT(), InFlag).getValue(1);
357     SDValue RetValue = Chain.getValue(0);
358     InFlag = Chain.getValue(2);
359
360     // If this is an 8/16/32-bit value, it is really passed promoted to 64
361     // bits. Insert an assert[sz]ext to capture this, then truncate to the
362     // right size.
363     if (VA.getLocInfo() == CCValAssign::SExt)
364       RetValue = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), RetValue,
365                              DAG.getValueType(VA.getValVT()));
366     else if (VA.getLocInfo() == CCValAssign::ZExt)
367       RetValue = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), RetValue,
368                              DAG.getValueType(VA.getValVT()));
369
370     if (VA.getLocInfo() != CCValAssign::Full)
371       RetValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), RetValue);
372
373     InVals.push_back(RetValue);
374   }
375
376   return Chain;
377 }
378
379 SDValue
380 AlphaTargetLowering::LowerFormalArguments(SDValue Chain,
381                                           CallingConv::ID CallConv, bool isVarArg,
382                                           const SmallVectorImpl<ISD::InputArg>
383                                             &Ins,
384                                           DebugLoc dl, SelectionDAG &DAG,
385                                           SmallVectorImpl<SDValue> &InVals) {
386
387   MachineFunction &MF = DAG.getMachineFunction();
388   MachineFrameInfo *MFI = MF.getFrameInfo();
389
390   unsigned args_int[] = {
391     Alpha::R16, Alpha::R17, Alpha::R18, Alpha::R19, Alpha::R20, Alpha::R21};
392   unsigned args_float[] = {
393     Alpha::F16, Alpha::F17, Alpha::F18, Alpha::F19, Alpha::F20, Alpha::F21};
394
395   for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
396     SDValue argt;
397     EVT ObjectVT = Ins[ArgNo].VT;
398     SDValue ArgVal;
399
400     if (ArgNo  < 6) {
401       switch (ObjectVT.getSimpleVT().SimpleTy) {
402       default:
403         assert(false && "Invalid value type!");
404       case MVT::f64:
405         args_float[ArgNo] = AddLiveIn(MF, args_float[ArgNo],
406                                       &Alpha::F8RCRegClass);
407         ArgVal = DAG.getCopyFromReg(Chain, dl, args_float[ArgNo], ObjectVT);
408         break;
409       case MVT::f32:
410         args_float[ArgNo] = AddLiveIn(MF, args_float[ArgNo],
411                                       &Alpha::F4RCRegClass);
412         ArgVal = DAG.getCopyFromReg(Chain, dl, args_float[ArgNo], ObjectVT);
413         break;
414       case MVT::i64:
415         args_int[ArgNo] = AddLiveIn(MF, args_int[ArgNo],
416                                     &Alpha::GPRCRegClass);
417         ArgVal = DAG.getCopyFromReg(Chain, dl, args_int[ArgNo], MVT::i64);
418         break;
419       }
420     } else { //more args
421       // Create the frame index object for this incoming parameter...
422       int FI = MFI->CreateFixedObject(8, 8 * (ArgNo - 6), true, false);
423
424       // Create the SelectionDAG nodes corresponding to a load
425       //from this parameter
426       SDValue FIN = DAG.getFrameIndex(FI, MVT::i64);
427       ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, NULL, 0);
428     }
429     InVals.push_back(ArgVal);
430   }
431
432   // If the functions takes variable number of arguments, copy all regs to stack
433   if (isVarArg) {
434     VarArgsOffset = Ins.size() * 8;
435     std::vector<SDValue> LS;
436     for (int i = 0; i < 6; ++i) {
437       if (TargetRegisterInfo::isPhysicalRegister(args_int[i]))
438         args_int[i] = AddLiveIn(MF, args_int[i], &Alpha::GPRCRegClass);
439       SDValue argt = DAG.getCopyFromReg(Chain, dl, args_int[i], MVT::i64);
440       int FI = MFI->CreateFixedObject(8, -8 * (6 - i), true, false);
441       if (i == 0) VarArgsBase = FI;
442       SDValue SDFI = DAG.getFrameIndex(FI, MVT::i64);
443       LS.push_back(DAG.getStore(Chain, dl, argt, SDFI, NULL, 0));
444
445       if (TargetRegisterInfo::isPhysicalRegister(args_float[i]))
446         args_float[i] = AddLiveIn(MF, args_float[i], &Alpha::F8RCRegClass);
447       argt = DAG.getCopyFromReg(Chain, dl, args_float[i], MVT::f64);
448       FI = MFI->CreateFixedObject(8, - 8 * (12 - i), true, false);
449       SDFI = DAG.getFrameIndex(FI, MVT::i64);
450       LS.push_back(DAG.getStore(Chain, dl, argt, SDFI, NULL, 0));
451     }
452
453     //Set up a token factor with all the stack traffic
454     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &LS[0], LS.size());
455   }
456
457   return Chain;
458 }
459
460 SDValue
461 AlphaTargetLowering::LowerReturn(SDValue Chain,
462                                  CallingConv::ID CallConv, bool isVarArg,
463                                  const SmallVectorImpl<ISD::OutputArg> &Outs,
464                                  DebugLoc dl, SelectionDAG &DAG) {
465
466   SDValue Copy = DAG.getCopyToReg(Chain, dl, Alpha::R26,
467                                   DAG.getNode(AlphaISD::GlobalRetAddr,
468                                               DebugLoc::getUnknownLoc(),
469                                               MVT::i64),
470                                   SDValue());
471   switch (Outs.size()) {
472   default:
473     llvm_unreachable("Do not know how to return this many arguments!");
474   case 0:
475     break;
476     //return SDValue(); // ret void is legal
477   case 1: {
478     EVT ArgVT = Outs[0].Val.getValueType();
479     unsigned ArgReg;
480     if (ArgVT.isInteger())
481       ArgReg = Alpha::R0;
482     else {
483       assert(ArgVT.isFloatingPoint());
484       ArgReg = Alpha::F0;
485     }
486     Copy = DAG.getCopyToReg(Copy, dl, ArgReg,
487                             Outs[0].Val, Copy.getValue(1));
488     if (DAG.getMachineFunction().getRegInfo().liveout_empty())
489       DAG.getMachineFunction().getRegInfo().addLiveOut(ArgReg);
490     break;
491   }
492   case 2: {
493     EVT ArgVT = Outs[0].Val.getValueType();
494     unsigned ArgReg1, ArgReg2;
495     if (ArgVT.isInteger()) {
496       ArgReg1 = Alpha::R0;
497       ArgReg2 = Alpha::R1;
498     } else {
499       assert(ArgVT.isFloatingPoint());
500       ArgReg1 = Alpha::F0;
501       ArgReg2 = Alpha::F1;
502     }
503     Copy = DAG.getCopyToReg(Copy, dl, ArgReg1,
504                             Outs[0].Val, Copy.getValue(1));
505     if (std::find(DAG.getMachineFunction().getRegInfo().liveout_begin(),
506                   DAG.getMachineFunction().getRegInfo().liveout_end(), ArgReg1)
507         == DAG.getMachineFunction().getRegInfo().liveout_end())
508       DAG.getMachineFunction().getRegInfo().addLiveOut(ArgReg1);
509     Copy = DAG.getCopyToReg(Copy, dl, ArgReg2,
510                             Outs[1].Val, Copy.getValue(1));
511     if (std::find(DAG.getMachineFunction().getRegInfo().liveout_begin(),
512                    DAG.getMachineFunction().getRegInfo().liveout_end(), ArgReg2)
513         == DAG.getMachineFunction().getRegInfo().liveout_end())
514       DAG.getMachineFunction().getRegInfo().addLiveOut(ArgReg2);
515     break;
516   }
517   }
518   return DAG.getNode(AlphaISD::RET_FLAG, dl,
519                      MVT::Other, Copy, Copy.getValue(1));
520 }
521
522 void AlphaTargetLowering::LowerVAARG(SDNode *N, SDValue &Chain,
523                                      SDValue &DataPtr, SelectionDAG &DAG) {
524   Chain = N->getOperand(0);
525   SDValue VAListP = N->getOperand(1);
526   const Value *VAListS = cast<SrcValueSDNode>(N->getOperand(2))->getValue();
527   DebugLoc dl = N->getDebugLoc();
528
529   SDValue Base = DAG.getLoad(MVT::i64, dl, Chain, VAListP, VAListS, 0);
530   SDValue Tmp = DAG.getNode(ISD::ADD, dl, MVT::i64, VAListP,
531                               DAG.getConstant(8, MVT::i64));
532   SDValue Offset = DAG.getExtLoad(ISD::SEXTLOAD, dl, MVT::i64, Base.getValue(1),
533                                     Tmp, NULL, 0, MVT::i32);
534   DataPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Base, Offset);
535   if (N->getValueType(0).isFloatingPoint())
536   {
537     //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
538     SDValue FPDataPtr = DAG.getNode(ISD::SUB, dl, MVT::i64, DataPtr,
539                                       DAG.getConstant(8*6, MVT::i64));
540     SDValue CC = DAG.getSetCC(dl, MVT::i64, Offset,
541                                 DAG.getConstant(8*6, MVT::i64), ISD::SETLT);
542     DataPtr = DAG.getNode(ISD::SELECT, dl, MVT::i64, CC, FPDataPtr, DataPtr);
543   }
544
545   SDValue NewOffset = DAG.getNode(ISD::ADD, dl, MVT::i64, Offset,
546                                     DAG.getConstant(8, MVT::i64));
547   Chain = DAG.getTruncStore(Offset.getValue(1), dl, NewOffset, Tmp, NULL, 0,
548                             MVT::i32);
549 }
550
551 /// LowerOperation - Provide custom lowering hooks for some operations.
552 ///
553 SDValue AlphaTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
554   DebugLoc dl = Op.getDebugLoc();
555   switch (Op.getOpcode()) {
556   default: llvm_unreachable("Wasn't expecting to be able to lower this!");
557   case ISD::JumpTable: return LowerJumpTable(Op, DAG);
558
559   case ISD::INTRINSIC_WO_CHAIN: {
560     unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
561     switch (IntNo) {
562     default: break;    // Don't custom lower most intrinsics.
563     case Intrinsic::alpha_umulh:
564       return DAG.getNode(ISD::MULHU, dl, MVT::i64,
565                          Op.getOperand(1), Op.getOperand(2));
566     }
567   }
568
569   case ISD::SRL_PARTS: {
570     SDValue ShOpLo = Op.getOperand(0);
571     SDValue ShOpHi = Op.getOperand(1);
572     SDValue ShAmt  = Op.getOperand(2);
573     SDValue bm = DAG.getNode(ISD::SUB, dl, MVT::i64,
574                              DAG.getConstant(64, MVT::i64), ShAmt);
575     SDValue BMCC = DAG.getSetCC(dl, MVT::i64, bm,
576                                 DAG.getConstant(0, MVT::i64), ISD::SETLE);
577     // if 64 - shAmt <= 0
578     SDValue Hi_Neg = DAG.getConstant(0, MVT::i64);
579     SDValue ShAmt_Neg = DAG.getNode(ISD::SUB, dl, MVT::i64,
580                                     DAG.getConstant(0, MVT::i64), bm);
581     SDValue Lo_Neg = DAG.getNode(ISD::SRL, dl, MVT::i64, ShOpHi, ShAmt_Neg);
582     // else
583     SDValue carries = DAG.getNode(ISD::SHL, dl, MVT::i64, ShOpHi, bm);
584     SDValue Hi_Pos =  DAG.getNode(ISD::SRL, dl, MVT::i64, ShOpHi, ShAmt);
585     SDValue Lo_Pos = DAG.getNode(ISD::SRL, dl, MVT::i64, ShOpLo, ShAmt);
586     Lo_Pos = DAG.getNode(ISD::OR, dl, MVT::i64, Lo_Pos, carries);
587     // Merge
588     SDValue Hi = DAG.getNode(ISD::SELECT, dl, MVT::i64, BMCC, Hi_Neg, Hi_Pos);
589     SDValue Lo = DAG.getNode(ISD::SELECT, dl, MVT::i64, BMCC, Lo_Neg, Lo_Pos);
590     SDValue Ops[2] = { Lo, Hi };
591     return DAG.getMergeValues(Ops, 2, dl);
592   }
593     //  case ISD::SRA_PARTS:
594
595     //  case ISD::SHL_PARTS:
596
597
598   case ISD::SINT_TO_FP: {
599     assert(Op.getOperand(0).getValueType() == MVT::i64 &&
600            "Unhandled SINT_TO_FP type in custom expander!");
601     SDValue LD;
602     bool isDouble = Op.getValueType() == MVT::f64;
603     LD = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op.getOperand(0));
604     SDValue FP = DAG.getNode(isDouble?AlphaISD::CVTQT_:AlphaISD::CVTQS_, dl,
605                                isDouble?MVT::f64:MVT::f32, LD);
606     return FP;
607   }
608   case ISD::FP_TO_SINT: {
609     bool isDouble = Op.getOperand(0).getValueType() == MVT::f64;
610     SDValue src = Op.getOperand(0);
611
612     if (!isDouble) //Promote
613       src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, src);
614
615     src = DAG.getNode(AlphaISD::CVTTQ_, dl, MVT::f64, src);
616
617     return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, src);
618   }
619   case ISD::ConstantPool: {
620     ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
621     Constant *C = CP->getConstVal();
622     SDValue CPI = DAG.getTargetConstantPool(C, MVT::i64, CP->getAlignment());
623     // FIXME there isn't really any debug info here
624
625     SDValue Hi = DAG.getNode(AlphaISD::GPRelHi,  dl, MVT::i64, CPI,
626                                DAG.getGLOBAL_OFFSET_TABLE(MVT::i64));
627     SDValue Lo = DAG.getNode(AlphaISD::GPRelLo, dl, MVT::i64, CPI, Hi);
628     return Lo;
629   }
630   case ISD::GlobalTLSAddress:
631     llvm_unreachable("TLS not implemented for Alpha.");
632   case ISD::GlobalAddress: {
633     GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
634     GlobalValue *GV = GSDN->getGlobal();
635     SDValue GA = DAG.getTargetGlobalAddress(GV, MVT::i64, GSDN->getOffset());
636     // FIXME there isn't really any debug info here
637
638     //    if (!GV->hasWeakLinkage() && !GV->isDeclaration() && !GV->hasLinkOnceLinkage()) {
639     if (GV->hasLocalLinkage()) {
640       SDValue Hi = DAG.getNode(AlphaISD::GPRelHi,  dl, MVT::i64, GA,
641                                 DAG.getGLOBAL_OFFSET_TABLE(MVT::i64));
642       SDValue Lo = DAG.getNode(AlphaISD::GPRelLo, dl, MVT::i64, GA, Hi);
643       return Lo;
644     } else
645       return DAG.getNode(AlphaISD::RelLit, dl, MVT::i64, GA,
646                          DAG.getGLOBAL_OFFSET_TABLE(MVT::i64));
647   }
648   case ISD::ExternalSymbol: {
649     return DAG.getNode(AlphaISD::RelLit, dl, MVT::i64,
650                        DAG.getTargetExternalSymbol(cast<ExternalSymbolSDNode>(Op)
651                                                    ->getSymbol(), MVT::i64),
652                        DAG.getGLOBAL_OFFSET_TABLE(MVT::i64));
653   }
654
655   case ISD::UREM:
656   case ISD::SREM:
657     //Expand only on constant case
658     if (Op.getOperand(1).getOpcode() == ISD::Constant) {
659       EVT VT = Op.getNode()->getValueType(0);
660       SDValue Tmp1 = Op.getNode()->getOpcode() == ISD::UREM ?
661         BuildUDIV(Op.getNode(), DAG, NULL) :
662         BuildSDIV(Op.getNode(), DAG, NULL);
663       Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Op.getOperand(1));
664       Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Op.getOperand(0), Tmp1);
665       return Tmp1;
666     }
667     //fall through
668   case ISD::SDIV:
669   case ISD::UDIV:
670     if (Op.getValueType().isInteger()) {
671       if (Op.getOperand(1).getOpcode() == ISD::Constant)
672         return Op.getOpcode() == ISD::SDIV ? BuildSDIV(Op.getNode(), DAG, NULL)
673           : BuildUDIV(Op.getNode(), DAG, NULL);
674       const char* opstr = 0;
675       switch (Op.getOpcode()) {
676       case ISD::UREM: opstr = "__remqu"; break;
677       case ISD::SREM: opstr = "__remq";  break;
678       case ISD::UDIV: opstr = "__divqu"; break;
679       case ISD::SDIV: opstr = "__divq";  break;
680       }
681       SDValue Tmp1 = Op.getOperand(0),
682         Tmp2 = Op.getOperand(1),
683         Addr = DAG.getExternalSymbol(opstr, MVT::i64);
684       return DAG.getNode(AlphaISD::DivCall, dl, MVT::i64, Addr, Tmp1, Tmp2);
685     }
686     break;
687
688   case ISD::VAARG: {
689     SDValue Chain, DataPtr;
690     LowerVAARG(Op.getNode(), Chain, DataPtr, DAG);
691
692     SDValue Result;
693     if (Op.getValueType() == MVT::i32)
694       Result = DAG.getExtLoad(ISD::SEXTLOAD, dl, MVT::i64, Chain, DataPtr,
695                               NULL, 0, MVT::i32);
696     else
697       Result = DAG.getLoad(Op.getValueType(), dl, Chain, DataPtr, NULL, 0);
698     return Result;
699   }
700   case ISD::VACOPY: {
701     SDValue Chain = Op.getOperand(0);
702     SDValue DestP = Op.getOperand(1);
703     SDValue SrcP = Op.getOperand(2);
704     const Value *DestS = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
705     const Value *SrcS = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
706
707     SDValue Val = DAG.getLoad(getPointerTy(), dl, Chain, SrcP, SrcS, 0);
708     SDValue Result = DAG.getStore(Val.getValue(1), dl, Val, DestP, DestS, 0);
709     SDValue NP = DAG.getNode(ISD::ADD, dl, MVT::i64, SrcP,
710                                DAG.getConstant(8, MVT::i64));
711     Val = DAG.getExtLoad(ISD::SEXTLOAD, dl, MVT::i64, Result,
712                          NP, NULL,0, MVT::i32);
713     SDValue NPD = DAG.getNode(ISD::ADD, dl, MVT::i64, DestP,
714                                 DAG.getConstant(8, MVT::i64));
715     return DAG.getTruncStore(Val.getValue(1), dl, Val, NPD, NULL, 0, MVT::i32);
716   }
717   case ISD::VASTART: {
718     SDValue Chain = Op.getOperand(0);
719     SDValue VAListP = Op.getOperand(1);
720     const Value *VAListS = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
721
722     // vastart stores the address of the VarArgsBase and VarArgsOffset
723     SDValue FR  = DAG.getFrameIndex(VarArgsBase, MVT::i64);
724     SDValue S1  = DAG.getStore(Chain, dl, FR, VAListP, VAListS, 0);
725     SDValue SA2 = DAG.getNode(ISD::ADD, dl, MVT::i64, VAListP,
726                                 DAG.getConstant(8, MVT::i64));
727     return DAG.getTruncStore(S1, dl, DAG.getConstant(VarArgsOffset, MVT::i64),
728                              SA2, NULL, 0, MVT::i32);
729   }
730   case ISD::RETURNADDR:
731     return DAG.getNode(AlphaISD::GlobalRetAddr, DebugLoc::getUnknownLoc(),
732                        MVT::i64);
733       //FIXME: implement
734   case ISD::FRAMEADDR:          break;
735   }
736
737   return SDValue();
738 }
739
740 void AlphaTargetLowering::ReplaceNodeResults(SDNode *N,
741                                              SmallVectorImpl<SDValue>&Results,
742                                              SelectionDAG &DAG) {
743   DebugLoc dl = N->getDebugLoc();
744   assert(N->getValueType(0) == MVT::i32 &&
745          N->getOpcode() == ISD::VAARG &&
746          "Unknown node to custom promote!");
747
748   SDValue Chain, DataPtr;
749   LowerVAARG(N, Chain, DataPtr, DAG);
750   SDValue Res = DAG.getLoad(N->getValueType(0), dl, Chain, DataPtr, NULL, 0);
751   Results.push_back(Res);
752   Results.push_back(SDValue(Res.getNode(), 1));
753 }
754
755
756 //Inline Asm
757
758 /// getConstraintType - Given a constraint letter, return the type of
759 /// constraint it is for this target.
760 AlphaTargetLowering::ConstraintType
761 AlphaTargetLowering::getConstraintType(const std::string &Constraint) const {
762   if (Constraint.size() == 1) {
763     switch (Constraint[0]) {
764     default: break;
765     case 'f':
766     case 'r':
767       return C_RegisterClass;
768     }
769   }
770   return TargetLowering::getConstraintType(Constraint);
771 }
772
773 std::vector<unsigned> AlphaTargetLowering::
774 getRegClassForInlineAsmConstraint(const std::string &Constraint,
775                                   EVT VT) const {
776   if (Constraint.size() == 1) {
777     switch (Constraint[0]) {
778     default: break;  // Unknown constriant letter
779     case 'f':
780       return make_vector<unsigned>(Alpha::F0 , Alpha::F1 , Alpha::F2 ,
781                                    Alpha::F3 , Alpha::F4 , Alpha::F5 ,
782                                    Alpha::F6 , Alpha::F7 , Alpha::F8 ,
783                                    Alpha::F9 , Alpha::F10, Alpha::F11,
784                                    Alpha::F12, Alpha::F13, Alpha::F14,
785                                    Alpha::F15, Alpha::F16, Alpha::F17,
786                                    Alpha::F18, Alpha::F19, Alpha::F20,
787                                    Alpha::F21, Alpha::F22, Alpha::F23,
788                                    Alpha::F24, Alpha::F25, Alpha::F26,
789                                    Alpha::F27, Alpha::F28, Alpha::F29,
790                                    Alpha::F30, Alpha::F31, 0);
791     case 'r':
792       return make_vector<unsigned>(Alpha::R0 , Alpha::R1 , Alpha::R2 ,
793                                    Alpha::R3 , Alpha::R4 , Alpha::R5 ,
794                                    Alpha::R6 , Alpha::R7 , Alpha::R8 ,
795                                    Alpha::R9 , Alpha::R10, Alpha::R11,
796                                    Alpha::R12, Alpha::R13, Alpha::R14,
797                                    Alpha::R15, Alpha::R16, Alpha::R17,
798                                    Alpha::R18, Alpha::R19, Alpha::R20,
799                                    Alpha::R21, Alpha::R22, Alpha::R23,
800                                    Alpha::R24, Alpha::R25, Alpha::R26,
801                                    Alpha::R27, Alpha::R28, Alpha::R29,
802                                    Alpha::R30, Alpha::R31, 0);
803     }
804   }
805
806   return std::vector<unsigned>();
807 }
808 //===----------------------------------------------------------------------===//
809 //  Other Lowering Code
810 //===----------------------------------------------------------------------===//
811
812 MachineBasicBlock *
813 AlphaTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
814                                                  MachineBasicBlock *BB,
815                    DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
816   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
817   assert((MI->getOpcode() == Alpha::CAS32 ||
818           MI->getOpcode() == Alpha::CAS64 ||
819           MI->getOpcode() == Alpha::LAS32 ||
820           MI->getOpcode() == Alpha::LAS64 ||
821           MI->getOpcode() == Alpha::SWAP32 ||
822           MI->getOpcode() == Alpha::SWAP64) &&
823          "Unexpected instr type to insert");
824
825   bool is32 = MI->getOpcode() == Alpha::CAS32 ||
826     MI->getOpcode() == Alpha::LAS32 ||
827     MI->getOpcode() == Alpha::SWAP32;
828
829   //Load locked store conditional for atomic ops take on the same form
830   //start:
831   //ll
832   //do stuff (maybe branch to exit)
833   //sc
834   //test sc and maybe branck to start
835   //exit:
836   const BasicBlock *LLVM_BB = BB->getBasicBlock();
837   DebugLoc dl = MI->getDebugLoc();
838   MachineFunction::iterator It = BB;
839   ++It;
840
841   MachineBasicBlock *thisMBB = BB;
842   MachineFunction *F = BB->getParent();
843   MachineBasicBlock *llscMBB = F->CreateMachineBasicBlock(LLVM_BB);
844   MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
845
846   // Inform sdisel of the edge changes.
847   for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
848          E = BB->succ_end(); I != E; ++I)
849     EM->insert(std::make_pair(*I, sinkMBB));
850
851   sinkMBB->transferSuccessors(thisMBB);
852
853   F->insert(It, llscMBB);
854   F->insert(It, sinkMBB);
855
856   BuildMI(thisMBB, dl, TII->get(Alpha::BR)).addMBB(llscMBB);
857
858   unsigned reg_res = MI->getOperand(0).getReg(),
859     reg_ptr = MI->getOperand(1).getReg(),
860     reg_v2 = MI->getOperand(2).getReg(),
861     reg_store = F->getRegInfo().createVirtualRegister(&Alpha::GPRCRegClass);
862
863   BuildMI(llscMBB, dl, TII->get(is32 ? Alpha::LDL_L : Alpha::LDQ_L),
864           reg_res).addImm(0).addReg(reg_ptr);
865   switch (MI->getOpcode()) {
866   case Alpha::CAS32:
867   case Alpha::CAS64: {
868     unsigned reg_cmp
869       = F->getRegInfo().createVirtualRegister(&Alpha::GPRCRegClass);
870     BuildMI(llscMBB, dl, TII->get(Alpha::CMPEQ), reg_cmp)
871       .addReg(reg_v2).addReg(reg_res);
872     BuildMI(llscMBB, dl, TII->get(Alpha::BEQ))
873       .addImm(0).addReg(reg_cmp).addMBB(sinkMBB);
874     BuildMI(llscMBB, dl, TII->get(Alpha::BISr), reg_store)
875       .addReg(Alpha::R31).addReg(MI->getOperand(3).getReg());
876     break;
877   }
878   case Alpha::LAS32:
879   case Alpha::LAS64: {
880     BuildMI(llscMBB, dl,TII->get(is32 ? Alpha::ADDLr : Alpha::ADDQr), reg_store)
881       .addReg(reg_res).addReg(reg_v2);
882     break;
883   }
884   case Alpha::SWAP32:
885   case Alpha::SWAP64: {
886     BuildMI(llscMBB, dl, TII->get(Alpha::BISr), reg_store)
887       .addReg(reg_v2).addReg(reg_v2);
888     break;
889   }
890   }
891   BuildMI(llscMBB, dl, TII->get(is32 ? Alpha::STL_C : Alpha::STQ_C), reg_store)
892     .addReg(reg_store).addImm(0).addReg(reg_ptr);
893   BuildMI(llscMBB, dl, TII->get(Alpha::BEQ))
894     .addImm(0).addReg(reg_store).addMBB(llscMBB);
895   BuildMI(llscMBB, dl, TII->get(Alpha::BR)).addMBB(sinkMBB);
896
897   thisMBB->addSuccessor(llscMBB);
898   llscMBB->addSuccessor(llscMBB);
899   llscMBB->addSuccessor(sinkMBB);
900   F->DeleteMachineInstr(MI);   // The pseudo instruction is gone now.
901
902   return sinkMBB;
903 }
904
905 bool
906 AlphaTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
907   // The Alpha target isn't yet aware of offsets.
908   return false;
909 }
910
911 bool AlphaTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
912   if (VT != MVT::f32 && VT != MVT::f64)
913     return false;
914   // +0.0   F31
915   // +0.0f  F31
916   // -0.0  -F31
917   // -0.0f -F31
918   return Imm.isZero() || Imm.isNegZero();
919 }