1 //===-- AlphaISelLowering.cpp - Alpha DAG Lowering Implementation ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the AlphaISelLowering class.
12 //===----------------------------------------------------------------------===//
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"
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
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);
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);
52 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
53 addRegisterClass(MVT::f64, Alpha::F8RCRegisterClass);
54 addRegisterClass(MVT::f32, Alpha::F4RCRegisterClass);
56 // We want to custom lower some of our intrinsics.
57 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
59 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
60 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
62 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
63 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Expand);
65 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
66 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
67 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Expand);
69 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
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);
76 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
78 setOperationAction(ISD::FREM, MVT::f32, Expand);
79 setOperationAction(ISD::FREM, MVT::f64, Expand);
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);
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);
91 setOperationAction(ISD::BSWAP , MVT::i64, Expand);
92 setOperationAction(ISD::ROTL , MVT::i64, Expand);
93 setOperationAction(ISD::ROTR , MVT::i64, Expand);
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);
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);
105 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
106 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
108 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
109 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
110 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
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);
118 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
119 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
121 setOperationAction(ISD::FPOW , MVT::f32, Expand);
122 setOperationAction(ISD::FPOW , MVT::f64, Expand);
124 setOperationAction(ISD::SETCC, MVT::f32, Promote);
126 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Promote);
128 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
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);
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);
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);
149 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
150 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
152 setStackPointerRegisterToSaveRestore(Alpha::R30);
155 setJumpBufAlignment(16);
157 computeRegisterProperties();
160 MVT::SimpleValueType AlphaTargetLowering::getSetCCResultType(EVT VT) const {
164 const char *AlphaTargetLowering::getTargetNodeName(unsigned Opcode) const {
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";
182 /// getFunctionAlignment - Return the Log2 alignment of this function.
183 unsigned AlphaTargetLowering::getFunctionAlignment(const Function *F) const {
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();
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);
200 //http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/
201 //AA-PY8AC-TET1_html/callCH3.html#BLOCK21
203 //For now, just use variable size stack frame format
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)
219 #include "AlphaGenCallingConv.inc"
222 AlphaTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
223 CallingConv::ID CallConv, bool isVarArg,
225 const SmallVectorImpl<ISD::OutputArg> &Outs,
226 const SmallVectorImpl<ISD::InputArg> &Ins,
227 DebugLoc dl, SelectionDAG &DAG,
228 SmallVectorImpl<SDValue> &InVals) {
229 // Alpha target does not yet support tail call optimization.
232 // Analyze operands of the call, assigning locations to each operand.
233 SmallVector<CCValAssign, 16> ArgLocs;
234 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
235 ArgLocs, *DAG.getContext());
237 CCInfo.AnalyzeCallOperands(Outs, CC_Alpha);
239 // Get a count of how many bytes are to be pushed on the stack.
240 unsigned NumBytes = CCInfo.getNextStackOffset();
242 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(NumBytes,
243 getPointerTy(), true));
245 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
246 SmallVector<SDValue, 12> MemOpChains;
249 // Walk the register/memloc assignments, inserting copies/loads.
250 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
251 CCValAssign &VA = ArgLocs[i];
253 SDValue Arg = Outs[i].Val;
255 // Promote the value if needed.
256 switch (VA.getLocInfo()) {
257 default: assert(0 && "Unknown loc info!");
258 case CCValAssign::Full: break;
259 case CCValAssign::SExt:
260 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
262 case CCValAssign::ZExt:
263 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
265 case CCValAssign::AExt:
266 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
270 // Arguments that can be passed on register must be kept at RegsToPass
273 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
275 assert(VA.isMemLoc());
277 if (StackPtr.getNode() == 0)
278 StackPtr = DAG.getCopyFromReg(Chain, dl, Alpha::R30, MVT::i64);
280 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
282 DAG.getIntPtrConstant(VA.getLocMemOffset()));
284 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
285 PseudoSourceValue::getStack(), 0));
289 // Transform all store nodes into one single node because all store nodes are
290 // independent of each other.
291 if (!MemOpChains.empty())
292 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
293 &MemOpChains[0], MemOpChains.size());
295 // Build a sequence of copy-to-reg nodes chained together with token chain and
296 // flag operands which copy the outgoing args into registers. The InFlag in
297 // necessary since all emited instructions must be stuck together.
299 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
300 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
301 RegsToPass[i].second, InFlag);
302 InFlag = Chain.getValue(1);
305 // Returns a chain & a flag for retval copy to use.
306 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
307 SmallVector<SDValue, 8> Ops;
308 Ops.push_back(Chain);
309 Ops.push_back(Callee);
311 // Add argument registers to the end of the list so that they are
312 // known live into the call.
313 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
314 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
315 RegsToPass[i].second.getValueType()));
317 if (InFlag.getNode())
318 Ops.push_back(InFlag);
320 Chain = DAG.getNode(AlphaISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
321 InFlag = Chain.getValue(1);
323 // Create the CALLSEQ_END node.
324 Chain = DAG.getCALLSEQ_END(Chain,
325 DAG.getConstant(NumBytes, getPointerTy(), true),
326 DAG.getConstant(0, getPointerTy(), true),
328 InFlag = Chain.getValue(1);
330 // Handle result values, copying them out of physregs into vregs that we
332 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
333 Ins, dl, DAG, InVals);
336 /// LowerCallResult - Lower the result values of a call into the
337 /// appropriate copies out of appropriate physical registers.
340 AlphaTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
341 CallingConv::ID CallConv, bool isVarArg,
342 const SmallVectorImpl<ISD::InputArg> &Ins,
343 DebugLoc dl, SelectionDAG &DAG,
344 SmallVectorImpl<SDValue> &InVals) {
346 // Assign locations to each value returned by this call.
347 SmallVector<CCValAssign, 16> RVLocs;
348 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
351 CCInfo.AnalyzeCallResult(Ins, RetCC_Alpha);
353 // Copy all of the result registers out of their specified physreg.
354 for (unsigned i = 0; i != RVLocs.size(); ++i) {
355 CCValAssign &VA = RVLocs[i];
357 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
358 VA.getLocVT(), InFlag).getValue(1);
359 SDValue RetValue = Chain.getValue(0);
360 InFlag = Chain.getValue(2);
362 // If this is an 8/16/32-bit value, it is really passed promoted to 64
363 // bits. Insert an assert[sz]ext to capture this, then truncate to the
365 if (VA.getLocInfo() == CCValAssign::SExt)
366 RetValue = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), RetValue,
367 DAG.getValueType(VA.getValVT()));
368 else if (VA.getLocInfo() == CCValAssign::ZExt)
369 RetValue = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), RetValue,
370 DAG.getValueType(VA.getValVT()));
372 if (VA.getLocInfo() != CCValAssign::Full)
373 RetValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), RetValue);
375 InVals.push_back(RetValue);
382 AlphaTargetLowering::LowerFormalArguments(SDValue Chain,
383 CallingConv::ID CallConv, bool isVarArg,
384 const SmallVectorImpl<ISD::InputArg>
386 DebugLoc dl, SelectionDAG &DAG,
387 SmallVectorImpl<SDValue> &InVals) {
389 MachineFunction &MF = DAG.getMachineFunction();
390 MachineFrameInfo *MFI = MF.getFrameInfo();
392 unsigned args_int[] = {
393 Alpha::R16, Alpha::R17, Alpha::R18, Alpha::R19, Alpha::R20, Alpha::R21};
394 unsigned args_float[] = {
395 Alpha::F16, Alpha::F17, Alpha::F18, Alpha::F19, Alpha::F20, Alpha::F21};
397 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
399 EVT ObjectVT = Ins[ArgNo].VT;
403 switch (ObjectVT.getSimpleVT().SimpleTy) {
405 assert(false && "Invalid value type!");
407 args_float[ArgNo] = AddLiveIn(MF, args_float[ArgNo],
408 &Alpha::F8RCRegClass);
409 ArgVal = DAG.getCopyFromReg(Chain, dl, args_float[ArgNo], ObjectVT);
412 args_float[ArgNo] = AddLiveIn(MF, args_float[ArgNo],
413 &Alpha::F4RCRegClass);
414 ArgVal = DAG.getCopyFromReg(Chain, dl, args_float[ArgNo], ObjectVT);
417 args_int[ArgNo] = AddLiveIn(MF, args_int[ArgNo],
418 &Alpha::GPRCRegClass);
419 ArgVal = DAG.getCopyFromReg(Chain, dl, args_int[ArgNo], MVT::i64);
423 // Create the frame index object for this incoming parameter...
424 int FI = MFI->CreateFixedObject(8, 8 * (ArgNo - 6), true, false);
426 // Create the SelectionDAG nodes corresponding to a load
427 //from this parameter
428 SDValue FIN = DAG.getFrameIndex(FI, MVT::i64);
429 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, NULL, 0);
431 InVals.push_back(ArgVal);
434 // If the functions takes variable number of arguments, copy all regs to stack
436 VarArgsOffset = Ins.size() * 8;
437 std::vector<SDValue> LS;
438 for (int i = 0; i < 6; ++i) {
439 if (TargetRegisterInfo::isPhysicalRegister(args_int[i]))
440 args_int[i] = AddLiveIn(MF, args_int[i], &Alpha::GPRCRegClass);
441 SDValue argt = DAG.getCopyFromReg(Chain, dl, args_int[i], MVT::i64);
442 int FI = MFI->CreateFixedObject(8, -8 * (6 - i), true, false);
443 if (i == 0) VarArgsBase = FI;
444 SDValue SDFI = DAG.getFrameIndex(FI, MVT::i64);
445 LS.push_back(DAG.getStore(Chain, dl, argt, SDFI, NULL, 0));
447 if (TargetRegisterInfo::isPhysicalRegister(args_float[i]))
448 args_float[i] = AddLiveIn(MF, args_float[i], &Alpha::F8RCRegClass);
449 argt = DAG.getCopyFromReg(Chain, dl, args_float[i], MVT::f64);
450 FI = MFI->CreateFixedObject(8, - 8 * (12 - i), true, false);
451 SDFI = DAG.getFrameIndex(FI, MVT::i64);
452 LS.push_back(DAG.getStore(Chain, dl, argt, SDFI, NULL, 0));
455 //Set up a token factor with all the stack traffic
456 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &LS[0], LS.size());
463 AlphaTargetLowering::LowerReturn(SDValue Chain,
464 CallingConv::ID CallConv, bool isVarArg,
465 const SmallVectorImpl<ISD::OutputArg> &Outs,
466 DebugLoc dl, SelectionDAG &DAG) {
468 SDValue Copy = DAG.getCopyToReg(Chain, dl, Alpha::R26,
469 DAG.getNode(AlphaISD::GlobalRetAddr,
470 DebugLoc::getUnknownLoc(),
473 switch (Outs.size()) {
475 llvm_unreachable("Do not know how to return this many arguments!");
478 //return SDValue(); // ret void is legal
480 EVT ArgVT = Outs[0].Val.getValueType();
482 if (ArgVT.isInteger())
485 assert(ArgVT.isFloatingPoint());
488 Copy = DAG.getCopyToReg(Copy, dl, ArgReg,
489 Outs[0].Val, Copy.getValue(1));
490 if (DAG.getMachineFunction().getRegInfo().liveout_empty())
491 DAG.getMachineFunction().getRegInfo().addLiveOut(ArgReg);
495 EVT ArgVT = Outs[0].Val.getValueType();
496 unsigned ArgReg1, ArgReg2;
497 if (ArgVT.isInteger()) {
501 assert(ArgVT.isFloatingPoint());
505 Copy = DAG.getCopyToReg(Copy, dl, ArgReg1,
506 Outs[0].Val, Copy.getValue(1));
507 if (std::find(DAG.getMachineFunction().getRegInfo().liveout_begin(),
508 DAG.getMachineFunction().getRegInfo().liveout_end(), ArgReg1)
509 == DAG.getMachineFunction().getRegInfo().liveout_end())
510 DAG.getMachineFunction().getRegInfo().addLiveOut(ArgReg1);
511 Copy = DAG.getCopyToReg(Copy, dl, ArgReg2,
512 Outs[1].Val, Copy.getValue(1));
513 if (std::find(DAG.getMachineFunction().getRegInfo().liveout_begin(),
514 DAG.getMachineFunction().getRegInfo().liveout_end(), ArgReg2)
515 == DAG.getMachineFunction().getRegInfo().liveout_end())
516 DAG.getMachineFunction().getRegInfo().addLiveOut(ArgReg2);
520 return DAG.getNode(AlphaISD::RET_FLAG, dl,
521 MVT::Other, Copy, Copy.getValue(1));
524 void AlphaTargetLowering::LowerVAARG(SDNode *N, SDValue &Chain,
525 SDValue &DataPtr, SelectionDAG &DAG) {
526 Chain = N->getOperand(0);
527 SDValue VAListP = N->getOperand(1);
528 const Value *VAListS = cast<SrcValueSDNode>(N->getOperand(2))->getValue();
529 DebugLoc dl = N->getDebugLoc();
531 SDValue Base = DAG.getLoad(MVT::i64, dl, Chain, VAListP, VAListS, 0);
532 SDValue Tmp = DAG.getNode(ISD::ADD, dl, MVT::i64, VAListP,
533 DAG.getConstant(8, MVT::i64));
534 SDValue Offset = DAG.getExtLoad(ISD::SEXTLOAD, dl, MVT::i64, Base.getValue(1),
535 Tmp, NULL, 0, MVT::i32);
536 DataPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Base, Offset);
537 if (N->getValueType(0).isFloatingPoint())
539 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
540 SDValue FPDataPtr = DAG.getNode(ISD::SUB, dl, MVT::i64, DataPtr,
541 DAG.getConstant(8*6, MVT::i64));
542 SDValue CC = DAG.getSetCC(dl, MVT::i64, Offset,
543 DAG.getConstant(8*6, MVT::i64), ISD::SETLT);
544 DataPtr = DAG.getNode(ISD::SELECT, dl, MVT::i64, CC, FPDataPtr, DataPtr);
547 SDValue NewOffset = DAG.getNode(ISD::ADD, dl, MVT::i64, Offset,
548 DAG.getConstant(8, MVT::i64));
549 Chain = DAG.getTruncStore(Offset.getValue(1), dl, NewOffset, Tmp, NULL, 0,
553 /// LowerOperation - Provide custom lowering hooks for some operations.
555 SDValue AlphaTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
556 DebugLoc dl = Op.getDebugLoc();
557 switch (Op.getOpcode()) {
558 default: llvm_unreachable("Wasn't expecting to be able to lower this!");
559 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
561 case ISD::INTRINSIC_WO_CHAIN: {
562 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
564 default: break; // Don't custom lower most intrinsics.
565 case Intrinsic::alpha_umulh:
566 return DAG.getNode(ISD::MULHU, dl, MVT::i64,
567 Op.getOperand(1), Op.getOperand(2));
571 case ISD::SRL_PARTS: {
572 SDValue ShOpLo = Op.getOperand(0);
573 SDValue ShOpHi = Op.getOperand(1);
574 SDValue ShAmt = Op.getOperand(2);
575 SDValue bm = DAG.getNode(ISD::SUB, dl, MVT::i64,
576 DAG.getConstant(64, MVT::i64), ShAmt);
577 SDValue BMCC = DAG.getSetCC(dl, MVT::i64, bm,
578 DAG.getConstant(0, MVT::i64), ISD::SETLE);
579 // if 64 - shAmt <= 0
580 SDValue Hi_Neg = DAG.getConstant(0, MVT::i64);
581 SDValue ShAmt_Neg = DAG.getNode(ISD::SUB, dl, MVT::i64,
582 DAG.getConstant(0, MVT::i64), bm);
583 SDValue Lo_Neg = DAG.getNode(ISD::SRL, dl, MVT::i64, ShOpHi, ShAmt_Neg);
585 SDValue carries = DAG.getNode(ISD::SHL, dl, MVT::i64, ShOpHi, bm);
586 SDValue Hi_Pos = DAG.getNode(ISD::SRL, dl, MVT::i64, ShOpHi, ShAmt);
587 SDValue Lo_Pos = DAG.getNode(ISD::SRL, dl, MVT::i64, ShOpLo, ShAmt);
588 Lo_Pos = DAG.getNode(ISD::OR, dl, MVT::i64, Lo_Pos, carries);
590 SDValue Hi = DAG.getNode(ISD::SELECT, dl, MVT::i64, BMCC, Hi_Neg, Hi_Pos);
591 SDValue Lo = DAG.getNode(ISD::SELECT, dl, MVT::i64, BMCC, Lo_Neg, Lo_Pos);
592 SDValue Ops[2] = { Lo, Hi };
593 return DAG.getMergeValues(Ops, 2, dl);
595 // case ISD::SRA_PARTS:
597 // case ISD::SHL_PARTS:
600 case ISD::SINT_TO_FP: {
601 assert(Op.getOperand(0).getValueType() == MVT::i64 &&
602 "Unhandled SINT_TO_FP type in custom expander!");
604 bool isDouble = Op.getValueType() == MVT::f64;
605 LD = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op.getOperand(0));
606 SDValue FP = DAG.getNode(isDouble?AlphaISD::CVTQT_:AlphaISD::CVTQS_, dl,
607 isDouble?MVT::f64:MVT::f32, LD);
610 case ISD::FP_TO_SINT: {
611 bool isDouble = Op.getOperand(0).getValueType() == MVT::f64;
612 SDValue src = Op.getOperand(0);
614 if (!isDouble) //Promote
615 src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, src);
617 src = DAG.getNode(AlphaISD::CVTTQ_, dl, MVT::f64, src);
619 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, src);
621 case ISD::ConstantPool: {
622 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
623 Constant *C = CP->getConstVal();
624 SDValue CPI = DAG.getTargetConstantPool(C, MVT::i64, CP->getAlignment());
625 // FIXME there isn't really any debug info here
627 SDValue Hi = DAG.getNode(AlphaISD::GPRelHi, dl, MVT::i64, CPI,
628 DAG.getGLOBAL_OFFSET_TABLE(MVT::i64));
629 SDValue Lo = DAG.getNode(AlphaISD::GPRelLo, dl, MVT::i64, CPI, Hi);
632 case ISD::GlobalTLSAddress:
633 llvm_unreachable("TLS not implemented for Alpha.");
634 case ISD::GlobalAddress: {
635 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
636 GlobalValue *GV = GSDN->getGlobal();
637 SDValue GA = DAG.getTargetGlobalAddress(GV, MVT::i64, GSDN->getOffset());
638 // FIXME there isn't really any debug info here
640 // if (!GV->hasWeakLinkage() && !GV->isDeclaration() && !GV->hasLinkOnceLinkage()) {
641 if (GV->hasLocalLinkage()) {
642 SDValue Hi = DAG.getNode(AlphaISD::GPRelHi, dl, MVT::i64, GA,
643 DAG.getGLOBAL_OFFSET_TABLE(MVT::i64));
644 SDValue Lo = DAG.getNode(AlphaISD::GPRelLo, dl, MVT::i64, GA, Hi);
647 return DAG.getNode(AlphaISD::RelLit, dl, MVT::i64, GA,
648 DAG.getGLOBAL_OFFSET_TABLE(MVT::i64));
650 case ISD::ExternalSymbol: {
651 return DAG.getNode(AlphaISD::RelLit, dl, MVT::i64,
652 DAG.getTargetExternalSymbol(cast<ExternalSymbolSDNode>(Op)
653 ->getSymbol(), MVT::i64),
654 DAG.getGLOBAL_OFFSET_TABLE(MVT::i64));
659 //Expand only on constant case
660 if (Op.getOperand(1).getOpcode() == ISD::Constant) {
661 EVT VT = Op.getNode()->getValueType(0);
662 SDValue Tmp1 = Op.getNode()->getOpcode() == ISD::UREM ?
663 BuildUDIV(Op.getNode(), DAG, NULL) :
664 BuildSDIV(Op.getNode(), DAG, NULL);
665 Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Op.getOperand(1));
666 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Op.getOperand(0), Tmp1);
672 if (Op.getValueType().isInteger()) {
673 if (Op.getOperand(1).getOpcode() == ISD::Constant)
674 return Op.getOpcode() == ISD::SDIV ? BuildSDIV(Op.getNode(), DAG, NULL)
675 : BuildUDIV(Op.getNode(), DAG, NULL);
676 const char* opstr = 0;
677 switch (Op.getOpcode()) {
678 case ISD::UREM: opstr = "__remqu"; break;
679 case ISD::SREM: opstr = "__remq"; break;
680 case ISD::UDIV: opstr = "__divqu"; break;
681 case ISD::SDIV: opstr = "__divq"; break;
683 SDValue Tmp1 = Op.getOperand(0),
684 Tmp2 = Op.getOperand(1),
685 Addr = DAG.getExternalSymbol(opstr, MVT::i64);
686 return DAG.getNode(AlphaISD::DivCall, dl, MVT::i64, Addr, Tmp1, Tmp2);
691 SDValue Chain, DataPtr;
692 LowerVAARG(Op.getNode(), Chain, DataPtr, DAG);
695 if (Op.getValueType() == MVT::i32)
696 Result = DAG.getExtLoad(ISD::SEXTLOAD, dl, MVT::i64, Chain, DataPtr,
699 Result = DAG.getLoad(Op.getValueType(), dl, Chain, DataPtr, NULL, 0);
703 SDValue Chain = Op.getOperand(0);
704 SDValue DestP = Op.getOperand(1);
705 SDValue SrcP = Op.getOperand(2);
706 const Value *DestS = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
707 const Value *SrcS = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
709 SDValue Val = DAG.getLoad(getPointerTy(), dl, Chain, SrcP, SrcS, 0);
710 SDValue Result = DAG.getStore(Val.getValue(1), dl, Val, DestP, DestS, 0);
711 SDValue NP = DAG.getNode(ISD::ADD, dl, MVT::i64, SrcP,
712 DAG.getConstant(8, MVT::i64));
713 Val = DAG.getExtLoad(ISD::SEXTLOAD, dl, MVT::i64, Result,
714 NP, NULL,0, MVT::i32);
715 SDValue NPD = DAG.getNode(ISD::ADD, dl, MVT::i64, DestP,
716 DAG.getConstant(8, MVT::i64));
717 return DAG.getTruncStore(Val.getValue(1), dl, Val, NPD, NULL, 0, MVT::i32);
720 SDValue Chain = Op.getOperand(0);
721 SDValue VAListP = Op.getOperand(1);
722 const Value *VAListS = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
724 // vastart stores the address of the VarArgsBase and VarArgsOffset
725 SDValue FR = DAG.getFrameIndex(VarArgsBase, MVT::i64);
726 SDValue S1 = DAG.getStore(Chain, dl, FR, VAListP, VAListS, 0);
727 SDValue SA2 = DAG.getNode(ISD::ADD, dl, MVT::i64, VAListP,
728 DAG.getConstant(8, MVT::i64));
729 return DAG.getTruncStore(S1, dl, DAG.getConstant(VarArgsOffset, MVT::i64),
730 SA2, NULL, 0, MVT::i32);
732 case ISD::RETURNADDR:
733 return DAG.getNode(AlphaISD::GlobalRetAddr, DebugLoc::getUnknownLoc(),
736 case ISD::FRAMEADDR: break;
742 void AlphaTargetLowering::ReplaceNodeResults(SDNode *N,
743 SmallVectorImpl<SDValue>&Results,
745 DebugLoc dl = N->getDebugLoc();
746 assert(N->getValueType(0) == MVT::i32 &&
747 N->getOpcode() == ISD::VAARG &&
748 "Unknown node to custom promote!");
750 SDValue Chain, DataPtr;
751 LowerVAARG(N, Chain, DataPtr, DAG);
752 SDValue Res = DAG.getLoad(N->getValueType(0), dl, Chain, DataPtr, NULL, 0);
753 Results.push_back(Res);
754 Results.push_back(SDValue(Res.getNode(), 1));
760 /// getConstraintType - Given a constraint letter, return the type of
761 /// constraint it is for this target.
762 AlphaTargetLowering::ConstraintType
763 AlphaTargetLowering::getConstraintType(const std::string &Constraint) const {
764 if (Constraint.size() == 1) {
765 switch (Constraint[0]) {
769 return C_RegisterClass;
772 return TargetLowering::getConstraintType(Constraint);
775 std::vector<unsigned> AlphaTargetLowering::
776 getRegClassForInlineAsmConstraint(const std::string &Constraint,
778 if (Constraint.size() == 1) {
779 switch (Constraint[0]) {
780 default: break; // Unknown constriant letter
782 return make_vector<unsigned>(Alpha::F0 , Alpha::F1 , Alpha::F2 ,
783 Alpha::F3 , Alpha::F4 , Alpha::F5 ,
784 Alpha::F6 , Alpha::F7 , Alpha::F8 ,
785 Alpha::F9 , Alpha::F10, Alpha::F11,
786 Alpha::F12, Alpha::F13, Alpha::F14,
787 Alpha::F15, Alpha::F16, Alpha::F17,
788 Alpha::F18, Alpha::F19, Alpha::F20,
789 Alpha::F21, Alpha::F22, Alpha::F23,
790 Alpha::F24, Alpha::F25, Alpha::F26,
791 Alpha::F27, Alpha::F28, Alpha::F29,
792 Alpha::F30, Alpha::F31, 0);
794 return make_vector<unsigned>(Alpha::R0 , Alpha::R1 , Alpha::R2 ,
795 Alpha::R3 , Alpha::R4 , Alpha::R5 ,
796 Alpha::R6 , Alpha::R7 , Alpha::R8 ,
797 Alpha::R9 , Alpha::R10, Alpha::R11,
798 Alpha::R12, Alpha::R13, Alpha::R14,
799 Alpha::R15, Alpha::R16, Alpha::R17,
800 Alpha::R18, Alpha::R19, Alpha::R20,
801 Alpha::R21, Alpha::R22, Alpha::R23,
802 Alpha::R24, Alpha::R25, Alpha::R26,
803 Alpha::R27, Alpha::R28, Alpha::R29,
804 Alpha::R30, Alpha::R31, 0);
808 return std::vector<unsigned>();
810 //===----------------------------------------------------------------------===//
811 // Other Lowering Code
812 //===----------------------------------------------------------------------===//
815 AlphaTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
816 MachineBasicBlock *BB,
817 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
818 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
819 assert((MI->getOpcode() == Alpha::CAS32 ||
820 MI->getOpcode() == Alpha::CAS64 ||
821 MI->getOpcode() == Alpha::LAS32 ||
822 MI->getOpcode() == Alpha::LAS64 ||
823 MI->getOpcode() == Alpha::SWAP32 ||
824 MI->getOpcode() == Alpha::SWAP64) &&
825 "Unexpected instr type to insert");
827 bool is32 = MI->getOpcode() == Alpha::CAS32 ||
828 MI->getOpcode() == Alpha::LAS32 ||
829 MI->getOpcode() == Alpha::SWAP32;
831 //Load locked store conditional for atomic ops take on the same form
834 //do stuff (maybe branch to exit)
836 //test sc and maybe branck to start
838 const BasicBlock *LLVM_BB = BB->getBasicBlock();
839 DebugLoc dl = MI->getDebugLoc();
840 MachineFunction::iterator It = BB;
843 MachineBasicBlock *thisMBB = BB;
844 MachineFunction *F = BB->getParent();
845 MachineBasicBlock *llscMBB = F->CreateMachineBasicBlock(LLVM_BB);
846 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
848 // Inform sdisel of the edge changes.
849 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
850 E = BB->succ_end(); I != E; ++I)
851 EM->insert(std::make_pair(*I, sinkMBB));
853 sinkMBB->transferSuccessors(thisMBB);
855 F->insert(It, llscMBB);
856 F->insert(It, sinkMBB);
858 BuildMI(thisMBB, dl, TII->get(Alpha::BR)).addMBB(llscMBB);
860 unsigned reg_res = MI->getOperand(0).getReg(),
861 reg_ptr = MI->getOperand(1).getReg(),
862 reg_v2 = MI->getOperand(2).getReg(),
863 reg_store = F->getRegInfo().createVirtualRegister(&Alpha::GPRCRegClass);
865 BuildMI(llscMBB, dl, TII->get(is32 ? Alpha::LDL_L : Alpha::LDQ_L),
866 reg_res).addImm(0).addReg(reg_ptr);
867 switch (MI->getOpcode()) {
871 = F->getRegInfo().createVirtualRegister(&Alpha::GPRCRegClass);
872 BuildMI(llscMBB, dl, TII->get(Alpha::CMPEQ), reg_cmp)
873 .addReg(reg_v2).addReg(reg_res);
874 BuildMI(llscMBB, dl, TII->get(Alpha::BEQ))
875 .addImm(0).addReg(reg_cmp).addMBB(sinkMBB);
876 BuildMI(llscMBB, dl, TII->get(Alpha::BISr), reg_store)
877 .addReg(Alpha::R31).addReg(MI->getOperand(3).getReg());
882 BuildMI(llscMBB, dl,TII->get(is32 ? Alpha::ADDLr : Alpha::ADDQr), reg_store)
883 .addReg(reg_res).addReg(reg_v2);
887 case Alpha::SWAP64: {
888 BuildMI(llscMBB, dl, TII->get(Alpha::BISr), reg_store)
889 .addReg(reg_v2).addReg(reg_v2);
893 BuildMI(llscMBB, dl, TII->get(is32 ? Alpha::STL_C : Alpha::STQ_C), reg_store)
894 .addReg(reg_store).addImm(0).addReg(reg_ptr);
895 BuildMI(llscMBB, dl, TII->get(Alpha::BEQ))
896 .addImm(0).addReg(reg_store).addMBB(llscMBB);
897 BuildMI(llscMBB, dl, TII->get(Alpha::BR)).addMBB(sinkMBB);
899 thisMBB->addSuccessor(llscMBB);
900 llscMBB->addSuccessor(llscMBB);
901 llscMBB->addSuccessor(sinkMBB);
902 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
908 AlphaTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
909 // The Alpha target isn't yet aware of offsets.
913 bool AlphaTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
914 if (VT != MVT::f32 && VT != MVT::f64)
920 return Imm.isZero() || Imm.isNegZero();