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::SIGN_EXTEND_INREG, MVT::i16, Expand);
63 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
64 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
66 setOperationAction(ISD::SELECT, MVT::i32, Expand);
68 setOperationAction(ISD::SETCC, MVT::i32, Expand);
69 setOperationAction(ISD::SETCC, MVT::f32, Expand);
70 setOperationAction(ISD::SETCC, MVT::f64, Expand);
72 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
73 setOperationAction(ISD::BRIND, MVT::i32, Expand);
74 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
75 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
76 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
78 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
80 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
81 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
82 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
83 setOperationAction(ISD::SDIV, MVT::i32, Expand);
84 setOperationAction(ISD::UDIV, MVT::i32, Expand);
85 setOperationAction(ISD::SREM, MVT::i32, Expand);
86 setOperationAction(ISD::UREM, MVT::i32, Expand);
88 setOperationAction(ISD::VASTART, MVT::Other, Custom);
89 setOperationAction(ISD::VAEND, MVT::Other, Expand);
91 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
92 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
94 setSchedulingPreference(SchedulingForRegPressure);
95 computeRegisterProperties();
101 // Start the numbering where the builting ops and target ops leave off.
102 FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
103 /// CALL - A direct function call.
106 /// Return with a flag operand.
136 /// DAGFPCCToARMCC - Convert a DAG fp condition code to an ARM CC
137 // Unordered = !N & !Z & C & V = V
138 // Ordered = N | Z | !C | !V = N | Z | !V
139 static ARMCC::CondCodes DAGFPCCToARMCC(ISD::CondCode CC) {
142 assert(0 && "Unknown fp condition code!");
143 // SETOEQ = (N | Z | !V) & Z = Z = EQ
145 case ISD::SETOEQ: return ARMCC::EQ;
146 // SETOGT = (N | Z | !V) & !N & !Z = !V &!N &!Z = (N = V) & !Z = GT
148 case ISD::SETOGT: return ARMCC::GT;
149 // SETOGE = (N | Z | !V) & !N = (Z | !V) & !N = !V & !N = GE
151 case ISD::SETOGE: return ARMCC::GE;
152 // SETOLT = (N | Z | !V) & N = N = MI
154 case ISD::SETOLT: return ARMCC::MI;
155 // SETOLE = (N | Z | !V) & (N | Z) = N | Z = !C | Z = LS
157 case ISD::SETOLE: return ARMCC::LS;
158 // SETONE = (N | Z | !V) & !Z = (N | !V) & Z = !V & Z = Z = NE
160 case ISD::SETONE: return ARMCC::NE;
161 // SETO = N | Z | !V = Z | !V = !V = VC
162 case ISD::SETO: return ARMCC::VC;
164 case ISD::SETUO: return ARMCC::VS;
165 // SETUEQ = V | Z = ??
166 // SETUGT = V | (!Z & !N) = !Z & !N = !Z & C = HI
167 case ISD::SETUGT: return ARMCC::HI;
168 // SETUGE = V | !N = !N = PL
169 case ISD::SETUGE: return ARMCC::PL;
170 // SETULT = V | N = ??
171 // SETULE = V | Z | N = ??
172 // SETUNE = V | !Z = !Z = NE
173 case ISD::SETUNE: return ARMCC::NE;
177 /// DAGIntCCToARMCC - Convert a DAG integer condition code to an ARM CC
178 static ARMCC::CondCodes DAGIntCCToARMCC(ISD::CondCode CC) {
181 assert(0 && "Unknown integer condition code!");
182 case ISD::SETEQ: return ARMCC::EQ;
183 case ISD::SETNE: return ARMCC::NE;
184 case ISD::SETLT: return ARMCC::LT;
185 case ISD::SETLE: return ARMCC::LE;
186 case ISD::SETGT: return ARMCC::GT;
187 case ISD::SETGE: return ARMCC::GE;
188 case ISD::SETULT: return ARMCC::CC;
189 case ISD::SETULE: return ARMCC::LS;
190 case ISD::SETUGT: return ARMCC::HI;
191 case ISD::SETUGE: return ARMCC::CS;
195 const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
198 case ARMISD::CALL: return "ARMISD::CALL";
199 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
200 case ARMISD::SELECT: return "ARMISD::SELECT";
201 case ARMISD::CMP: return "ARMISD::CMP";
202 case ARMISD::BR: return "ARMISD::BR";
203 case ARMISD::FSITOS: return "ARMISD::FSITOS";
204 case ARMISD::FTOSIS: return "ARMISD::FTOSIS";
205 case ARMISD::FSITOD: return "ARMISD::FSITOD";
206 case ARMISD::FTOSID: return "ARMISD::FTOSID";
207 case ARMISD::FUITOS: return "ARMISD::FUITOS";
208 case ARMISD::FTOUIS: return "ARMISD::FTOUIS";
209 case ARMISD::FUITOD: return "ARMISD::FUITOD";
210 case ARMISD::FTOUID: return "ARMISD::FTOUID";
211 case ARMISD::FMRRD: return "ARMISD::FMRRD";
212 case ARMISD::FMDRR: return "ARMISD::FMDRR";
213 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
217 class ArgumentLayout {
218 std::vector<bool> is_reg;
219 std::vector<unsigned> pos;
220 std::vector<MVT::ValueType> types;
222 ArgumentLayout(const std::vector<MVT::ValueType> &Types) {
226 unsigned StackOffset = 0;
227 for(std::vector<MVT::ValueType>::const_iterator I = Types.begin();
230 MVT::ValueType VT = *I;
231 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
232 unsigned size = MVT::getSizeInBits(VT)/32;
234 RegNum = ((RegNum + size - 1) / size) * size;
236 pos.push_back(RegNum);
237 is_reg.push_back(true);
240 unsigned bytes = size * 32/8;
241 StackOffset = ((StackOffset + bytes - 1) / bytes) * bytes;
242 pos.push_back(StackOffset);
243 is_reg.push_back(false);
244 StackOffset += bytes;
248 unsigned getRegisterNum(unsigned argNum) {
249 assert(isRegister(argNum));
252 unsigned getOffset(unsigned argNum) {
253 assert(isOffset(argNum));
256 unsigned isRegister(unsigned argNum) {
257 assert(argNum < is_reg.size());
258 return is_reg[argNum];
260 unsigned isOffset(unsigned argNum) {
261 return !isRegister(argNum);
263 MVT::ValueType getType(unsigned argNum) {
264 assert(argNum < types.size());
265 return types[argNum];
267 unsigned getStackSize(void) {
268 int last = is_reg.size() - 1;
271 if (isRegister(last))
273 return getOffset(last) + MVT::getSizeInBits(getType(last))/8;
275 int lastRegArg(void) {
276 int size = is_reg.size();
278 while(last < size && isRegister(last))
283 int lastRegNum(void) {
284 int l = lastRegArg();
287 unsigned r = getRegisterNum(l);
288 MVT::ValueType t = getType(l);
289 assert(t == MVT::i32 || t == MVT::f32 || t == MVT::f64);
296 // This transforms a ISD::CALL node into a
297 // callseq_star <- ARMISD:CALL <- callseq_end
299 static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
300 SDOperand Chain = Op.getOperand(0);
301 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
302 assert((CallConv == CallingConv::C ||
303 CallConv == CallingConv::Fast)
304 && "unknown calling convention");
305 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
306 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
307 SDOperand Callee = Op.getOperand(4);
308 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
309 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
310 static const unsigned regs[] = {
311 ARM::R0, ARM::R1, ARM::R2, ARM::R3
314 std::vector<MVT::ValueType> Types;
315 for (unsigned i = 0; i < NumOps; ++i) {
316 MVT::ValueType VT = Op.getOperand(5+2*i).getValueType();
319 ArgumentLayout Layout(Types);
321 unsigned NumBytes = Layout.getStackSize();
323 Chain = DAG.getCALLSEQ_START(Chain,
324 DAG.getConstant(NumBytes, MVT::i32));
326 //Build a sequence of stores
327 std::vector<SDOperand> MemOpChains;
328 for (unsigned i = Layout.lastRegArg() + 1; i < NumOps; ++i) {
329 SDOperand Arg = Op.getOperand(5+2*i);
330 unsigned ArgOffset = Layout.getOffset(i);
331 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
332 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
333 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
335 if (!MemOpChains.empty())
336 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
337 &MemOpChains[0], MemOpChains.size());
339 // If the callee is a GlobalAddress node (quite common, every direct call is)
340 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
341 // Likewise ExternalSymbol -> TargetExternalSymbol.
342 assert(Callee.getValueType() == MVT::i32);
343 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
344 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
345 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
346 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
348 // If this is a direct call, pass the chain and the callee.
350 std::vector<SDOperand> Ops;
351 Ops.push_back(Chain);
352 Ops.push_back(Callee);
354 // Build a sequence of copy-to-reg nodes chained together with token chain
355 // and flag operands which copy the outgoing args into the appropriate regs.
357 for (int i = 0, e = Layout.lastRegArg(); i <= e; ++i) {
358 SDOperand Arg = Op.getOperand(5+2*i);
359 unsigned RegNum = Layout.getRegisterNum(i);
360 unsigned Reg1 = regs[RegNum];
361 MVT::ValueType VT = Layout.getType(i);
362 assert(VT == Arg.getValueType());
363 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
365 // Add argument register to the end of the list so that it is known live
367 Ops.push_back(DAG.getRegister(Reg1, MVT::i32));
368 if (VT == MVT::f64) {
369 unsigned Reg2 = regs[RegNum + 1];
370 SDOperand SDReg1 = DAG.getRegister(Reg1, MVT::i32);
371 SDOperand SDReg2 = DAG.getRegister(Reg2, MVT::i32);
373 Ops.push_back(DAG.getRegister(Reg2, MVT::i32));
374 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
375 SDOperand Ops[] = {Chain, SDReg1, SDReg2, Arg, InFlag};
376 Chain = DAG.getNode(ARMISD::FMRRD, VTs, Ops, InFlag.Val ? 5 : 4);
379 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg);
380 Chain = DAG.getCopyToReg(Chain, Reg1, Arg, InFlag);
382 InFlag = Chain.getValue(1);
385 std::vector<MVT::ValueType> NodeTys;
386 NodeTys.push_back(MVT::Other); // Returns a chain
387 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
389 unsigned CallOpc = ARMISD::CALL;
391 Ops.push_back(InFlag);
392 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
393 InFlag = Chain.getValue(1);
395 std::vector<SDOperand> ResultVals;
398 // If the call has results, copy the values out of the ret val registers.
399 MVT::ValueType VT = Op.Val->getValueType(0);
400 if (VT != MVT::Other) {
401 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
403 SDOperand Value1 = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
404 Chain = Value1.getValue(1);
405 InFlag = Value1.getValue(2);
406 NodeTys.push_back(VT);
407 if (VT == MVT::i32) {
408 ResultVals.push_back(Value1);
409 if (Op.Val->getValueType(1) == MVT::i32) {
410 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
411 Chain = Value2.getValue(1);
412 ResultVals.push_back(Value2);
413 NodeTys.push_back(VT);
416 if (VT == MVT::f32) {
417 SDOperand Value = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Value1);
418 ResultVals.push_back(Value);
420 if (VT == MVT::f64) {
421 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
422 Chain = Value2.getValue(1);
423 SDOperand Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
424 ResultVals.push_back(Value);
428 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
429 DAG.getConstant(NumBytes, MVT::i32));
430 NodeTys.push_back(MVT::Other);
432 if (ResultVals.empty())
435 ResultVals.push_back(Chain);
436 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
438 return Res.getValue(Op.ResNo);
441 static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
443 SDOperand Chain = Op.getOperand(0);
444 SDOperand R0 = DAG.getRegister(ARM::R0, MVT::i32);
445 SDOperand R1 = DAG.getRegister(ARM::R1, MVT::i32);
447 switch(Op.getNumOperands()) {
449 assert(0 && "Do not know how to return this many arguments!");
452 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
453 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
456 SDOperand Val = Op.getOperand(1);
457 assert(Val.getValueType() == MVT::i32 ||
458 Val.getValueType() == MVT::f32 ||
459 Val.getValueType() == MVT::f64);
461 if (Val.getValueType() == MVT::f64) {
462 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
463 SDOperand Ops[] = {Chain, R0, R1, Val};
464 Copy = DAG.getNode(ARMISD::FMRRD, VTs, Ops, 4);
466 if (Val.getValueType() == MVT::f32)
467 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
468 Copy = DAG.getCopyToReg(Chain, R0, Val, SDOperand());
471 if (DAG.getMachineFunction().liveout_empty()) {
472 DAG.getMachineFunction().addLiveOut(ARM::R0);
473 if (Val.getValueType() == MVT::f64)
474 DAG.getMachineFunction().addLiveOut(ARM::R1);
479 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
480 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
481 // If we haven't noted the R0+R1 are live out, do so now.
482 if (DAG.getMachineFunction().liveout_empty()) {
483 DAG.getMachineFunction().addLiveOut(ARM::R0);
484 DAG.getMachineFunction().addLiveOut(ARM::R1);
489 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
490 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
493 static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
494 MVT::ValueType PtrVT = Op.getValueType();
495 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
496 Constant *C = CP->getConstVal();
497 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
502 static SDOperand LowerGlobalAddress(SDOperand Op,
504 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
506 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
507 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr, NULL, 0);
510 static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
511 unsigned VarArgsFrameIndex) {
512 // vastart just stores the address of the VarArgsFrameIndex slot into the
513 // memory location argument.
514 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
515 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
516 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
517 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), SV->getValue(),
521 static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
522 int &VarArgsFrameIndex) {
523 MachineFunction &MF = DAG.getMachineFunction();
524 MachineFrameInfo *MFI = MF.getFrameInfo();
525 SSARegMap *RegMap = MF.getSSARegMap();
526 unsigned NumArgs = Op.Val->getNumValues()-1;
527 SDOperand Root = Op.getOperand(0);
528 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
529 static const unsigned REGS[] = {
530 ARM::R0, ARM::R1, ARM::R2, ARM::R3
533 std::vector<MVT::ValueType> Types(Op.Val->value_begin(), Op.Val->value_end() - 1);
534 ArgumentLayout Layout(Types);
536 std::vector<SDOperand> ArgValues;
537 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) {
538 MVT::ValueType VT = Types[ArgNo];
541 if (Layout.isRegister(ArgNo)) {
542 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
543 unsigned RegNum = Layout.getRegisterNum(ArgNo);
544 unsigned Reg1 = REGS[RegNum];
545 unsigned VReg1 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
546 SDOperand Value1 = DAG.getCopyFromReg(Root, VReg1, MVT::i32);
547 MF.addLiveIn(Reg1, VReg1);
548 if (VT == MVT::f64) {
549 unsigned Reg2 = REGS[RegNum + 1];
550 unsigned VReg2 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
551 SDOperand Value2 = DAG.getCopyFromReg(Root, VReg2, MVT::i32);
552 MF.addLiveIn(Reg2, VReg2);
553 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
557 Value = DAG.getNode(ISD::BIT_CONVERT, VT, Value);
560 // If the argument is actually used, emit a load from the right stack
562 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
563 unsigned Offset = Layout.getOffset(ArgNo);
564 unsigned Size = MVT::getSizeInBits(VT)/8;
565 int FI = MFI->CreateFixedObject(Size, Offset);
566 SDOperand FIN = DAG.getFrameIndex(FI, VT);
567 Value = DAG.getLoad(VT, Root, FIN, NULL, 0);
569 Value = DAG.getNode(ISD::UNDEF, VT);
572 ArgValues.push_back(Value);
575 unsigned NextRegNum = Layout.lastRegNum() + 1;
578 //If this function is vararg we must store the remaing
579 //registers so that they can be acessed with va_start
580 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
581 -16 + NextRegNum * 4);
583 SmallVector<SDOperand, 4> MemOps;
584 for (unsigned RegNo = NextRegNum; RegNo < 4; ++RegNo) {
585 int RegOffset = - (4 - RegNo) * 4;
586 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
588 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
590 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
591 MF.addLiveIn(REGS[RegNo], VReg);
593 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
594 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
595 MemOps.push_back(Store);
597 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
600 ArgValues.push_back(Root);
602 // Return the new list of results.
603 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
604 Op.Val->value_end());
605 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
608 static SDOperand GetCMP(ISD::CondCode CC, SDOperand LHS, SDOperand RHS,
610 MVT::ValueType vt = LHS.getValueType();
611 assert(vt == MVT::i32 || vt == MVT::f32 || vt == MVT::f64);
613 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
616 Cmp = DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp);
620 static SDOperand GetARMCC(ISD::CondCode CC, MVT::ValueType vt,
622 assert(vt == MVT::i32 || vt == MVT::f32 || vt == MVT::f64);
624 return DAG.getConstant(DAGIntCCToARMCC(CC), MVT::i32);
626 return DAG.getConstant(DAGFPCCToARMCC(CC), MVT::i32);
629 static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
630 SDOperand LHS = Op.getOperand(0);
631 SDOperand RHS = Op.getOperand(1);
632 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
633 SDOperand TrueVal = Op.getOperand(2);
634 SDOperand FalseVal = Op.getOperand(3);
635 SDOperand Cmp = GetCMP(CC, LHS, RHS, DAG);
636 SDOperand ARMCC = GetARMCC(CC, LHS.getValueType(), DAG);
637 return DAG.getNode(ARMISD::SELECT, MVT::i32, TrueVal, FalseVal, ARMCC, Cmp);
640 static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
641 SDOperand Chain = Op.getOperand(0);
642 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
643 SDOperand LHS = Op.getOperand(2);
644 SDOperand RHS = Op.getOperand(3);
645 SDOperand Dest = Op.getOperand(4);
646 SDOperand Cmp = GetCMP(CC, LHS, RHS, DAG);
647 SDOperand ARMCC = GetARMCC(CC, LHS.getValueType(), DAG);
648 return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, ARMCC, Cmp);
651 static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
652 SDOperand IntVal = Op.getOperand(0);
653 assert(IntVal.getValueType() == MVT::i32);
654 MVT::ValueType vt = Op.getValueType();
655 assert(vt == MVT::f32 ||
658 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
659 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FSITOS : ARMISD::FSITOD;
660 return DAG.getNode(op, vt, Tmp);
663 static SDOperand LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
664 assert(Op.getValueType() == MVT::i32);
665 SDOperand FloatVal = Op.getOperand(0);
666 MVT::ValueType vt = FloatVal.getValueType();
667 assert(vt == MVT::f32 || vt == MVT::f64);
669 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FTOSIS : ARMISD::FTOSID;
670 SDOperand Tmp = DAG.getNode(op, MVT::f32, FloatVal);
671 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Tmp);
674 static SDOperand LowerUINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
675 SDOperand IntVal = Op.getOperand(0);
676 assert(IntVal.getValueType() == MVT::i32);
677 MVT::ValueType vt = Op.getValueType();
678 assert(vt == MVT::f32 ||
681 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
682 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FUITOS : ARMISD::FUITOD;
683 return DAG.getNode(op, vt, Tmp);
686 static SDOperand LowerFP_TO_UINT(SDOperand Op, SelectionDAG &DAG) {
687 assert(Op.getValueType() == MVT::i32);
688 SDOperand FloatVal = Op.getOperand(0);
689 MVT::ValueType vt = FloatVal.getValueType();
690 assert(vt == MVT::f32 || vt == MVT::f64);
692 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FTOUIS : ARMISD::FTOUID;
693 SDOperand Tmp = DAG.getNode(op, MVT::f32, FloatVal);
694 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Tmp);
697 SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
698 switch (Op.getOpcode()) {
700 assert(0 && "Should not custom lower this!");
702 case ISD::ConstantPool:
703 return LowerConstantPool(Op, DAG);
704 case ISD::GlobalAddress:
705 return LowerGlobalAddress(Op, DAG);
706 case ISD::FP_TO_SINT:
707 return LowerFP_TO_SINT(Op, DAG);
708 case ISD::SINT_TO_FP:
709 return LowerSINT_TO_FP(Op, DAG);
710 case ISD::FP_TO_UINT:
711 return LowerFP_TO_UINT(Op, DAG);
712 case ISD::UINT_TO_FP:
713 return LowerUINT_TO_FP(Op, DAG);
714 case ISD::FORMAL_ARGUMENTS:
715 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
717 return LowerCALL(Op, DAG);
719 return LowerRET(Op, DAG);
721 return LowerSELECT_CC(Op, DAG);
723 return LowerBR_CC(Op, DAG);
725 return LowerVASTART(Op, DAG, VarArgsFrameIndex);
729 //===----------------------------------------------------------------------===//
730 // Instruction Selector Implementation
731 //===----------------------------------------------------------------------===//
733 //===--------------------------------------------------------------------===//
734 /// ARMDAGToDAGISel - ARM specific code to select ARM machine
735 /// instructions for SelectionDAG operations.
738 class ARMDAGToDAGISel : public SelectionDAGISel {
739 ARMTargetLowering Lowering;
742 ARMDAGToDAGISel(TargetMachine &TM)
743 : SelectionDAGISel(Lowering), Lowering(TM) {
746 SDNode *Select(SDOperand Op);
747 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
748 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
749 bool SelectAddrMode1(SDOperand N, SDOperand &Arg, SDOperand &Shift,
750 SDOperand &ShiftType);
751 bool SelectAddrMode5(SDOperand N, SDOperand &Arg, SDOperand &Offset);
753 // Include the pieces autogenerated from the target description.
754 #include "ARMGenDAGISel.inc"
757 void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
760 DAG.setRoot(SelectRoot(DAG.getRoot()));
761 DAG.RemoveDeadNodes();
763 ScheduleAndEmitDAG(DAG);
766 static bool isInt12Immediate(SDNode *N, short &Imm) {
767 if (N->getOpcode() != ISD::Constant)
770 int32_t t = cast<ConstantSDNode>(N)->getValue();
773 if (t > min && t < max) {
781 static bool isInt12Immediate(SDOperand Op, short &Imm) {
782 return isInt12Immediate(Op.Val, Imm);
785 static uint32_t rotateL(uint32_t x) {
786 uint32_t bit31 = (x & (1 << 31)) >> 31;
791 static bool isUInt8Immediate(uint32_t x) {
795 static bool isRotInt8Immediate(uint32_t x) {
797 for (r = 0; r < 16; r++) {
798 if (isUInt8Immediate(x))
800 x = rotateL(rotateL(x));
805 bool ARMDAGToDAGISel::SelectAddrMode1(SDOperand N,
808 SDOperand &ShiftType) {
809 switch(N.getOpcode()) {
810 case ISD::Constant: {
811 uint32_t val = cast<ConstantSDNode>(N)->getValue();
812 if(!isRotInt8Immediate(val)) {
813 const Type *t = MVT::getTypeForValueType(MVT::i32);
814 Constant *C = ConstantUInt::get(t, val);
816 SDOperand Addr = CurDAG->getTargetConstantPool(C, MVT::i32, alignment);
817 SDOperand Z = CurDAG->getTargetConstant(0, MVT::i32);
818 SDNode *n = CurDAG->getTargetNode(ARM::ldr, MVT::i32, Z, Addr);
819 Arg = SDOperand(n, 0);
821 Arg = CurDAG->getTargetConstant(val, MVT::i32);
823 Shift = CurDAG->getTargetConstant(0, MVT::i32);
824 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
828 Arg = N.getOperand(0);
829 Shift = N.getOperand(1);
830 ShiftType = CurDAG->getTargetConstant(ARMShift::ASR, MVT::i32);
833 Arg = N.getOperand(0);
834 Shift = N.getOperand(1);
835 ShiftType = CurDAG->getTargetConstant(ARMShift::LSR, MVT::i32);
838 Arg = N.getOperand(0);
839 Shift = N.getOperand(1);
840 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
845 Shift = CurDAG->getTargetConstant(0, MVT::i32);
846 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
850 bool ARMDAGToDAGISel::SelectAddrMode5(SDOperand N, SDOperand &Arg,
852 //TODO: detect offset
853 Offset = CurDAG->getTargetConstant(0, MVT::i32);
858 //register plus/minus 12 bit offset
859 bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
861 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
862 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
863 Offset = CurDAG->getTargetConstant(0, MVT::i32);
866 if (N.getOpcode() == ISD::ADD) {
868 if (isInt12Immediate(N.getOperand(1), imm)) {
869 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
870 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
871 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
873 Base = N.getOperand(0);
875 return true; // [r+i]
879 Offset = CurDAG->getTargetConstant(0, MVT::i32);
880 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
881 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
885 return true; //any address fits in a register
888 SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
891 switch (N->getOpcode()) {
893 return SelectCode(Op);
899 } // end anonymous namespace
901 /// createARMISelDag - This pass converts a legalized DAG into a
902 /// ARM-specific DAG, ready for instruction scheduling.
904 FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
905 return new ARMDAGToDAGISel(TM);