1 //===-- ARMISelDAGToDAG.cpp - A dag to dag inst selector for ARM ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines an instruction selector for the ARM target.
12 //===----------------------------------------------------------------------===//
15 #include "ARMTargetMachine.h"
16 #include "llvm/CallingConv.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Function.h"
19 #include "llvm/Constants.h"
20 #include "llvm/Intrinsics.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/SelectionDAG.h"
25 #include "llvm/CodeGen/SelectionDAGISel.h"
26 #include "llvm/CodeGen/SSARegMap.h"
27 #include "llvm/Target/TargetLowering.h"
28 #include "llvm/Support/Debug.h"
34 class ARMTargetLowering : public TargetLowering {
35 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
37 ARMTargetLowering(TargetMachine &TM);
38 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
39 virtual const char *getTargetNodeName(unsigned Opcode) const;
44 ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
45 : TargetLowering(TM) {
46 addRegisterClass(MVT::i32, ARM::IntRegsRegisterClass);
47 addRegisterClass(MVT::f32, ARM::FPRegsRegisterClass);
48 addRegisterClass(MVT::f64, ARM::DFPRegsRegisterClass);
50 setLoadXAction(ISD::EXTLOAD, MVT::f32, Expand);
52 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
53 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
55 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
56 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
58 setOperationAction(ISD::RET, MVT::Other, Custom);
59 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
60 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
62 setOperationAction(ISD::SELECT, MVT::i32, Expand);
64 setOperationAction(ISD::SETCC, MVT::i32, Expand);
65 setOperationAction(ISD::SETCC, MVT::f32, Expand);
66 setOperationAction(ISD::SETCC, MVT::f64, Expand);
68 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
69 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
70 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
71 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
73 setOperationAction(ISD::VASTART, MVT::Other, Custom);
74 setOperationAction(ISD::VAEND, MVT::Other, Expand);
76 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
77 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
79 setSchedulingPreference(SchedulingForRegPressure);
80 computeRegisterProperties();
86 // Start the numbering where the builting ops and target ops leave off.
87 FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
88 /// CALL - A direct function call.
91 /// Return with a flag operand.
121 /// DAGCCToARMCC - Convert a DAG integer condition code to an ARM CC
122 //Note: ARM doesn't have condition codes corresponding to the ordered
123 //condition codes of LLVM. We use exception raising instructions so
124 //that we can be sure that V == 0 and test only the rest of the expression.
125 static ARMCC::CondCodes DAGCCToARMCC(ISD::CondCode CC) {
128 std::cerr << "CC = " << CC << "\n";
129 assert(0 && "Unknown condition code!");
130 case ISD::SETUGT: return ARMCC::HI;
131 case ISD::SETULE: return ARMCC::LS;
133 case ISD::SETOLE: return ARMCC::LE;
135 case ISD::SETOLT: return ARMCC::LT;
137 case ISD::SETOGT: return ARMCC::GT;
138 case ISD::SETNE: return ARMCC::NE;
140 case ISD::SETOEQ: return ARMCC::EQ;
142 case ISD::SETOGE: return ARMCC::GE;
143 case ISD::SETUGE: return ARMCC::CS;
144 case ISD::SETULT: return ARMCC::CC;
148 const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
151 case ARMISD::CALL: return "ARMISD::CALL";
152 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
153 case ARMISD::SELECT: return "ARMISD::SELECT";
154 case ARMISD::CMP: return "ARMISD::CMP";
155 case ARMISD::BR: return "ARMISD::BR";
156 case ARMISD::FSITOS: return "ARMISD::FSITOS";
157 case ARMISD::FTOSIS: return "ARMISD::FTOSIS";
158 case ARMISD::FSITOD: return "ARMISD::FSITOD";
159 case ARMISD::FTOSID: return "ARMISD::FTOSID";
160 case ARMISD::FUITOS: return "ARMISD::FUITOS";
161 case ARMISD::FTOUIS: return "ARMISD::FTOUIS";
162 case ARMISD::FUITOD: return "ARMISD::FUITOD";
163 case ARMISD::FTOUID: return "ARMISD::FTOUID";
164 case ARMISD::FMRRD: return "ARMISD::FMRRD";
165 case ARMISD::FMDRR: return "ARMISD::FMDRR";
166 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
170 class ArgumentLayout {
171 std::vector<bool> is_reg;
172 std::vector<unsigned> pos;
173 std::vector<MVT::ValueType> types;
175 ArgumentLayout(const std::vector<MVT::ValueType> &Types) {
179 unsigned StackOffset = 0;
180 for(std::vector<MVT::ValueType>::const_iterator I = Types.begin();
183 MVT::ValueType VT = *I;
184 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
185 unsigned size = MVT::getSizeInBits(VT)/32;
187 RegNum = ((RegNum + size - 1) / size) * size;
189 pos.push_back(RegNum);
190 is_reg.push_back(true);
193 unsigned bytes = size * 32/8;
194 StackOffset = ((StackOffset + bytes - 1) / bytes) * bytes;
195 pos.push_back(StackOffset);
196 is_reg.push_back(false);
197 StackOffset += bytes;
201 unsigned getRegisterNum(unsigned argNum) {
202 assert(isRegister(argNum));
205 unsigned getOffset(unsigned argNum) {
206 assert(isOffset(argNum));
209 unsigned isRegister(unsigned argNum) {
210 assert(argNum < is_reg.size());
211 return is_reg[argNum];
213 unsigned isOffset(unsigned argNum) {
214 return !isRegister(argNum);
216 MVT::ValueType getType(unsigned argNum) {
217 assert(argNum < types.size());
218 return types[argNum];
220 unsigned getStackSize(void) {
221 int last = is_reg.size() - 1;
224 if (isRegister(last))
226 return getOffset(last) + MVT::getSizeInBits(getType(last))/8;
228 int lastRegArg(void) {
229 int size = is_reg.size();
231 while(last < size && isRegister(last))
236 int lastRegNum(void) {
237 int l = lastRegArg();
240 unsigned r = getRegisterNum(l);
241 MVT::ValueType t = getType(l);
242 assert(t == MVT::i32 || t == MVT::f32 || t == MVT::f64);
249 // This transforms a ISD::CALL node into a
250 // callseq_star <- ARMISD:CALL <- callseq_end
252 static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
253 SDOperand Chain = Op.getOperand(0);
254 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
255 assert(CallConv == CallingConv::C && "unknown calling convention");
256 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
257 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
258 assert(isTailCall == false && "tail call not supported");
259 SDOperand Callee = Op.getOperand(4);
260 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
261 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
262 static const unsigned regs[] = {
263 ARM::R0, ARM::R1, ARM::R2, ARM::R3
266 std::vector<MVT::ValueType> Types;
267 for (unsigned i = 0; i < NumOps; ++i) {
268 MVT::ValueType VT = Op.getOperand(5+2*i).getValueType();
271 ArgumentLayout Layout(Types);
273 unsigned NumBytes = Layout.getStackSize();
275 Chain = DAG.getCALLSEQ_START(Chain,
276 DAG.getConstant(NumBytes, MVT::i32));
278 //Build a sequence of stores
279 std::vector<SDOperand> MemOpChains;
280 for (unsigned i = Layout.lastRegArg() + 1; i < NumOps; ++i) {
281 SDOperand Arg = Op.getOperand(5+2*i);
282 unsigned ArgOffset = Layout.getOffset(i);
283 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
284 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
285 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff,
286 DAG.getSrcValue(NULL)));
288 if (!MemOpChains.empty())
289 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
290 &MemOpChains[0], MemOpChains.size());
292 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
293 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
294 // node so that legalize doesn't hack it.
295 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
296 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
298 // If this is a direct call, pass the chain and the callee.
300 std::vector<SDOperand> Ops;
301 Ops.push_back(Chain);
302 Ops.push_back(Callee);
304 // Build a sequence of copy-to-reg nodes chained together with token chain
305 // and flag operands which copy the outgoing args into the appropriate regs.
307 for (int i = 0, e = Layout.lastRegArg(); i <= e; ++i) {
308 SDOperand Arg = Op.getOperand(5+2*i);
309 unsigned RegNum = Layout.getRegisterNum(i);
310 unsigned Reg1 = regs[RegNum];
311 MVT::ValueType VT = Layout.getType(i);
312 assert(VT == Arg.getValueType());
313 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
315 // Add argument register to the end of the list so that it is known live
317 Ops.push_back(DAG.getRegister(Reg1, MVT::i32));
318 if (VT == MVT::f64) {
319 unsigned Reg2 = regs[RegNum + 1];
320 SDOperand SDReg1 = DAG.getRegister(Reg1, MVT::i32);
321 SDOperand SDReg2 = DAG.getRegister(Reg2, MVT::i32);
323 Ops.push_back(DAG.getRegister(Reg2, MVT::i32));
324 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
325 SDOperand Ops[] = {Chain, SDReg1, SDReg2, Arg, InFlag};
326 Chain = DAG.getNode(ARMISD::FMRRD, VTs, Ops, InFlag.Val ? 5 : 4);
329 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg);
330 Chain = DAG.getCopyToReg(Chain, Reg1, Arg, InFlag);
332 InFlag = Chain.getValue(1);
335 std::vector<MVT::ValueType> NodeTys;
336 NodeTys.push_back(MVT::Other); // Returns a chain
337 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
339 unsigned CallOpc = ARMISD::CALL;
341 Ops.push_back(InFlag);
342 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
343 InFlag = Chain.getValue(1);
345 std::vector<SDOperand> ResultVals;
348 // If the call has results, copy the values out of the ret val registers.
349 MVT::ValueType VT = Op.Val->getValueType(0);
350 if (VT != MVT::Other) {
351 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
354 SDOperand Value1 = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
355 Chain = Value1.getValue(1);
356 InFlag = Value1.getValue(2);
360 Value = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Value1);
361 if (VT == MVT::f64) {
362 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
363 Chain = Value2.getValue(1);
364 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
366 ResultVals.push_back(Value);
367 NodeTys.push_back(VT);
370 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
371 DAG.getConstant(NumBytes, MVT::i32));
372 NodeTys.push_back(MVT::Other);
374 if (ResultVals.empty())
377 ResultVals.push_back(Chain);
378 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
380 return Res.getValue(Op.ResNo);
383 static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
385 SDOperand Chain = Op.getOperand(0);
386 SDOperand R0 = DAG.getRegister(ARM::R0, MVT::i32);
387 SDOperand R1 = DAG.getRegister(ARM::R1, MVT::i32);
389 switch(Op.getNumOperands()) {
391 assert(0 && "Do not know how to return this many arguments!");
394 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
395 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
398 SDOperand Val = Op.getOperand(1);
399 assert(Val.getValueType() == MVT::i32 ||
400 Val.getValueType() == MVT::f32 ||
401 Val.getValueType() == MVT::f64);
403 if (Val.getValueType() == MVT::f64) {
404 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
405 SDOperand Ops[] = {Chain, R0, R1, Val};
406 Copy = DAG.getNode(ARMISD::FMRRD, VTs, Ops, 4);
408 if (Val.getValueType() == MVT::f32)
409 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
410 Copy = DAG.getCopyToReg(Chain, R0, Val, SDOperand());
413 if (DAG.getMachineFunction().liveout_empty()) {
414 DAG.getMachineFunction().addLiveOut(ARM::R0);
415 if (Val.getValueType() == MVT::f64)
416 DAG.getMachineFunction().addLiveOut(ARM::R1);
421 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
422 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
423 // If we haven't noted the R0+R1 are live out, do so now.
424 if (DAG.getMachineFunction().liveout_empty()) {
425 DAG.getMachineFunction().addLiveOut(ARM::R0);
426 DAG.getMachineFunction().addLiveOut(ARM::R1);
431 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
432 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
435 static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
436 MVT::ValueType PtrVT = Op.getValueType();
437 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
438 Constant *C = CP->getConstVal();
439 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
444 static SDOperand LowerGlobalAddress(SDOperand Op,
446 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
448 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
449 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr, NULL, 0);
452 static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
453 unsigned VarArgsFrameIndex) {
454 // vastart just stores the address of the VarArgsFrameIndex slot into the
455 // memory location argument.
456 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
457 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
458 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), Op.getOperand(2));
461 static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
462 int &VarArgsFrameIndex) {
463 MachineFunction &MF = DAG.getMachineFunction();
464 MachineFrameInfo *MFI = MF.getFrameInfo();
465 SSARegMap *RegMap = MF.getSSARegMap();
466 unsigned NumArgs = Op.Val->getNumValues()-1;
467 SDOperand Root = Op.getOperand(0);
468 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
469 static const unsigned REGS[] = {
470 ARM::R0, ARM::R1, ARM::R2, ARM::R3
473 std::vector<MVT::ValueType> Types(Op.Val->value_begin(), Op.Val->value_end() - 1);
474 ArgumentLayout Layout(Types);
476 std::vector<SDOperand> ArgValues;
477 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) {
478 MVT::ValueType VT = Types[ArgNo];
481 if (Layout.isRegister(ArgNo)) {
482 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
483 unsigned RegNum = Layout.getRegisterNum(ArgNo);
484 unsigned Reg1 = REGS[RegNum];
485 unsigned VReg1 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
486 SDOperand Value1 = DAG.getCopyFromReg(Root, VReg1, MVT::i32);
487 MF.addLiveIn(Reg1, VReg1);
488 if (VT == MVT::f64) {
489 unsigned Reg2 = REGS[RegNum + 1];
490 unsigned VReg2 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
491 SDOperand Value2 = DAG.getCopyFromReg(Root, VReg2, MVT::i32);
492 MF.addLiveIn(Reg2, VReg2);
493 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
497 Value = DAG.getNode(ISD::BIT_CONVERT, VT, Value);
500 // If the argument is actually used, emit a load from the right stack
502 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
503 unsigned Offset = Layout.getOffset(ArgNo);
504 unsigned Size = MVT::getSizeInBits(VT)/8;
505 int FI = MFI->CreateFixedObject(Size, Offset);
506 SDOperand FIN = DAG.getFrameIndex(FI, VT);
507 Value = DAG.getLoad(VT, Root, FIN, NULL, 0);
509 Value = DAG.getNode(ISD::UNDEF, VT);
512 ArgValues.push_back(Value);
515 unsigned NextRegNum = Layout.lastRegNum() + 1;
518 //If this function is vararg we must store the remaing
519 //registers so that they can be acessed with va_start
520 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
521 -16 + NextRegNum * 4);
523 SmallVector<SDOperand, 4> MemOps;
524 for (unsigned RegNo = NextRegNum; RegNo < 4; ++RegNo) {
525 int RegOffset = - (4 - RegNo) * 4;
526 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
528 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
530 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
531 MF.addLiveIn(REGS[RegNo], VReg);
533 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
534 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN,
535 DAG.getSrcValue(NULL));
536 MemOps.push_back(Store);
538 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
541 ArgValues.push_back(Root);
543 // Return the new list of results.
544 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
545 Op.Val->value_end());
546 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
549 static SDOperand GetCMP(ISD::CondCode CC, SDOperand LHS, SDOperand RHS,
551 MVT::ValueType vt = LHS.getValueType();
552 assert(vt == MVT::i32 || vt == MVT::f32 || vt == MVT::f64);
553 //Note: unordered floating point compares should use a non throwing
555 bool isUnorderedFloat = (vt == MVT::f32 || vt == MVT::f64) &&
556 (CC >= ISD::SETUO && CC <= ISD::SETUNE);
557 assert(!isUnorderedFloat && "Unordered float compares are not supported");
559 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
561 Cmp = DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp);
565 static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
566 SDOperand LHS = Op.getOperand(0);
567 SDOperand RHS = Op.getOperand(1);
568 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
569 SDOperand TrueVal = Op.getOperand(2);
570 SDOperand FalseVal = Op.getOperand(3);
571 SDOperand Cmp = GetCMP(CC, LHS, RHS, DAG);
572 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
573 return DAG.getNode(ARMISD::SELECT, MVT::i32, TrueVal, FalseVal, ARMCC, Cmp);
576 static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
577 SDOperand Chain = Op.getOperand(0);
578 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
579 SDOperand LHS = Op.getOperand(2);
580 SDOperand RHS = Op.getOperand(3);
581 SDOperand Dest = Op.getOperand(4);
582 SDOperand Cmp = GetCMP(CC, LHS, RHS, DAG);
583 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
584 return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, ARMCC, Cmp);
587 static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
588 SDOperand IntVal = Op.getOperand(0);
589 assert(IntVal.getValueType() == MVT::i32);
590 MVT::ValueType vt = Op.getValueType();
591 assert(vt == MVT::f32 ||
594 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
595 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FSITOS : ARMISD::FSITOD;
596 return DAG.getNode(op, vt, Tmp);
599 static SDOperand LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
600 assert(Op.getValueType() == MVT::i32);
601 SDOperand FloatVal = Op.getOperand(0);
602 MVT::ValueType vt = FloatVal.getValueType();
603 assert(vt == MVT::f32 || vt == MVT::f64);
605 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FTOSIS : ARMISD::FTOSID;
606 SDOperand Tmp = DAG.getNode(op, MVT::f32, FloatVal);
607 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Tmp);
610 static SDOperand LowerUINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
611 SDOperand IntVal = Op.getOperand(0);
612 assert(IntVal.getValueType() == MVT::i32);
613 MVT::ValueType vt = Op.getValueType();
614 assert(vt == MVT::f32 ||
617 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
618 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FUITOS : ARMISD::FUITOD;
619 return DAG.getNode(op, vt, Tmp);
622 static SDOperand LowerFP_TO_UINT(SDOperand Op, SelectionDAG &DAG) {
623 assert(Op.getValueType() == MVT::i32);
624 SDOperand FloatVal = Op.getOperand(0);
625 MVT::ValueType vt = FloatVal.getValueType();
626 assert(vt == MVT::f32 || vt == MVT::f64);
628 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FTOUIS : ARMISD::FTOUID;
629 SDOperand Tmp = DAG.getNode(op, MVT::f32, FloatVal);
630 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Tmp);
633 SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
634 switch (Op.getOpcode()) {
636 assert(0 && "Should not custom lower this!");
638 case ISD::ConstantPool:
639 return LowerConstantPool(Op, DAG);
640 case ISD::GlobalAddress:
641 return LowerGlobalAddress(Op, DAG);
642 case ISD::FP_TO_SINT:
643 return LowerFP_TO_SINT(Op, DAG);
644 case ISD::SINT_TO_FP:
645 return LowerSINT_TO_FP(Op, DAG);
646 case ISD::FP_TO_UINT:
647 return LowerFP_TO_UINT(Op, DAG);
648 case ISD::UINT_TO_FP:
649 return LowerUINT_TO_FP(Op, DAG);
650 case ISD::FORMAL_ARGUMENTS:
651 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
653 return LowerCALL(Op, DAG);
655 return LowerRET(Op, DAG);
657 return LowerSELECT_CC(Op, DAG);
659 return LowerBR_CC(Op, DAG);
661 return LowerVASTART(Op, DAG, VarArgsFrameIndex);
665 //===----------------------------------------------------------------------===//
666 // Instruction Selector Implementation
667 //===----------------------------------------------------------------------===//
669 //===--------------------------------------------------------------------===//
670 /// ARMDAGToDAGISel - ARM specific code to select ARM machine
671 /// instructions for SelectionDAG operations.
674 class ARMDAGToDAGISel : public SelectionDAGISel {
675 ARMTargetLowering Lowering;
678 ARMDAGToDAGISel(TargetMachine &TM)
679 : SelectionDAGISel(Lowering), Lowering(TM) {
682 SDNode *Select(SDOperand Op);
683 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
684 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
685 bool SelectAddrMode1(SDOperand N, SDOperand &Arg, SDOperand &Shift,
686 SDOperand &ShiftType);
688 // Include the pieces autogenerated from the target description.
689 #include "ARMGenDAGISel.inc"
692 void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
695 DAG.setRoot(SelectRoot(DAG.getRoot()));
696 DAG.RemoveDeadNodes();
698 ScheduleAndEmitDAG(DAG);
701 static bool isInt12Immediate(SDNode *N, short &Imm) {
702 if (N->getOpcode() != ISD::Constant)
705 int32_t t = cast<ConstantSDNode>(N)->getValue();
708 if (t > min && t < max) {
716 static bool isInt12Immediate(SDOperand Op, short &Imm) {
717 return isInt12Immediate(Op.Val, Imm);
720 static uint32_t rotateL(uint32_t x) {
721 uint32_t bit31 = (x & (1 << 31)) >> 31;
726 static bool isUInt8Immediate(uint32_t x) {
730 static bool isRotInt8Immediate(uint32_t x) {
732 for (r = 0; r < 16; r++) {
733 if (isUInt8Immediate(x))
735 x = rotateL(rotateL(x));
740 bool ARMDAGToDAGISel::SelectAddrMode1(SDOperand N,
743 SDOperand &ShiftType) {
744 switch(N.getOpcode()) {
745 case ISD::Constant: {
746 uint32_t val = cast<ConstantSDNode>(N)->getValue();
747 if(!isRotInt8Immediate(val)) {
748 const Type *t = MVT::getTypeForValueType(MVT::i32);
749 Constant *C = ConstantUInt::get(t, val);
751 SDOperand Addr = CurDAG->getTargetConstantPool(C, MVT::i32, alignment);
752 SDOperand Z = CurDAG->getTargetConstant(0, MVT::i32);
753 SDNode *n = CurDAG->getTargetNode(ARM::ldr, MVT::i32, Z, Addr);
754 Arg = SDOperand(n, 0);
756 Arg = CurDAG->getTargetConstant(val, MVT::i32);
758 Shift = CurDAG->getTargetConstant(0, MVT::i32);
759 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
763 Arg = N.getOperand(0);
764 Shift = N.getOperand(1);
765 ShiftType = CurDAG->getTargetConstant(ARMShift::ASR, MVT::i32);
768 Arg = N.getOperand(0);
769 Shift = N.getOperand(1);
770 ShiftType = CurDAG->getTargetConstant(ARMShift::LSR, MVT::i32);
773 Arg = N.getOperand(0);
774 Shift = N.getOperand(1);
775 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
780 Shift = CurDAG->getTargetConstant(0, MVT::i32);
781 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
785 //register plus/minus 12 bit offset
786 bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
788 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
789 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
790 Offset = CurDAG->getTargetConstant(0, MVT::i32);
793 if (N.getOpcode() == ISD::ADD) {
795 if (isInt12Immediate(N.getOperand(1), imm)) {
796 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
797 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
798 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
800 Base = N.getOperand(0);
802 return true; // [r+i]
806 Offset = CurDAG->getTargetConstant(0, MVT::i32);
807 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
808 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
812 return true; //any address fits in a register
815 SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
818 switch (N->getOpcode()) {
820 return SelectCode(Op);
826 } // end anonymous namespace
828 /// createARMISelDag - This pass converts a legalized DAG into a
829 /// ARM-specific DAG, ready for instruction scheduling.
831 FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
832 return new ARMDAGToDAGISel(TM);