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::BRIND, MVT::i32, Expand);
70 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
71 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
72 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
74 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
76 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
77 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
78 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
79 setOperationAction(ISD::SDIV, MVT::i32, Expand);
80 setOperationAction(ISD::UDIV, MVT::i32, Expand);
81 setOperationAction(ISD::SREM, MVT::i32, Expand);
82 setOperationAction(ISD::UREM, MVT::i32, Expand);
84 setOperationAction(ISD::VASTART, MVT::Other, Custom);
85 setOperationAction(ISD::VAEND, MVT::Other, Expand);
87 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
88 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
90 setSchedulingPreference(SchedulingForRegPressure);
91 computeRegisterProperties();
97 // Start the numbering where the builting ops and target ops leave off.
98 FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
99 /// CALL - A direct function call.
102 /// Return with a flag operand.
132 /// DAGFPCCToARMCC - Convert a DAG fp condition code to an ARM CC
133 // Unordered = !N & !Z & C & V = V
134 // Ordered = N | Z | !C | !V = N | Z | !V
135 static ARMCC::CondCodes DAGFPCCToARMCC(ISD::CondCode CC) {
138 assert(0 && "Unknown fp condition code!");
139 // SETOEQ = (N | Z | !V) & Z = Z = EQ
141 case ISD::SETOEQ: return ARMCC::EQ;
142 // SETOGT = (N | Z | !V) & !N & !Z = !V &!N &!Z = (N = V) & !Z = GT
144 case ISD::SETOGT: return ARMCC::GT;
145 // SETOGE = (N | Z | !V) & !N = (Z | !V) & !N = !V & !N = GE
147 case ISD::SETOGE: return ARMCC::GE;
148 // SETOLT = (N | Z | !V) & N = N = MI
150 case ISD::SETOLT: return ARMCC::MI;
151 // SETOLE = (N | Z | !V) & (N | Z) = N | Z = !C | Z = LS
153 case ISD::SETOLE: return ARMCC::LS;
154 // SETONE = (N | Z | !V) & !Z = (N | !V) & Z = !V & Z = Z = NE
156 case ISD::SETONE: return ARMCC::NE;
157 // SETO = N | Z | !V = Z | !V = !V = VC
158 case ISD::SETO: return ARMCC::VC;
160 case ISD::SETUO: return ARMCC::VS;
161 // SETUEQ = V | Z = ??
162 // SETUGT = V | (!Z & !N) = !Z & !N = !Z & C = HI
163 case ISD::SETUGT: return ARMCC::HI;
164 // SETUGE = V | !N = !N = PL
165 case ISD::SETUGE: return ARMCC::PL;
166 // SETULT = V | N = ??
167 // SETULE = V | Z | N = ??
168 // SETUNE = V | !Z = !Z = NE
169 case ISD::SETUNE: return ARMCC::NE;
173 /// DAGIntCCToARMCC - Convert a DAG integer condition code to an ARM CC
174 static ARMCC::CondCodes DAGIntCCToARMCC(ISD::CondCode CC) {
177 assert(0 && "Unknown integer condition code!");
178 case ISD::SETEQ: return ARMCC::EQ;
179 case ISD::SETNE: return ARMCC::NE;
180 case ISD::SETLT: return ARMCC::LT;
181 case ISD::SETLE: return ARMCC::LE;
182 case ISD::SETGT: return ARMCC::GT;
183 case ISD::SETGE: return ARMCC::GE;
184 case ISD::SETULT: return ARMCC::CC;
185 case ISD::SETULE: return ARMCC::LS;
186 case ISD::SETUGT: return ARMCC::HI;
187 case ISD::SETUGE: return ARMCC::CS;
191 const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
194 case ARMISD::CALL: return "ARMISD::CALL";
195 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
196 case ARMISD::SELECT: return "ARMISD::SELECT";
197 case ARMISD::CMP: return "ARMISD::CMP";
198 case ARMISD::BR: return "ARMISD::BR";
199 case ARMISD::FSITOS: return "ARMISD::FSITOS";
200 case ARMISD::FTOSIS: return "ARMISD::FTOSIS";
201 case ARMISD::FSITOD: return "ARMISD::FSITOD";
202 case ARMISD::FTOSID: return "ARMISD::FTOSID";
203 case ARMISD::FUITOS: return "ARMISD::FUITOS";
204 case ARMISD::FTOUIS: return "ARMISD::FTOUIS";
205 case ARMISD::FUITOD: return "ARMISD::FUITOD";
206 case ARMISD::FTOUID: return "ARMISD::FTOUID";
207 case ARMISD::FMRRD: return "ARMISD::FMRRD";
208 case ARMISD::FMDRR: return "ARMISD::FMDRR";
209 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
213 class ArgumentLayout {
214 std::vector<bool> is_reg;
215 std::vector<unsigned> pos;
216 std::vector<MVT::ValueType> types;
218 ArgumentLayout(const std::vector<MVT::ValueType> &Types) {
222 unsigned StackOffset = 0;
223 for(std::vector<MVT::ValueType>::const_iterator I = Types.begin();
226 MVT::ValueType VT = *I;
227 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
228 unsigned size = MVT::getSizeInBits(VT)/32;
230 RegNum = ((RegNum + size - 1) / size) * size;
232 pos.push_back(RegNum);
233 is_reg.push_back(true);
236 unsigned bytes = size * 32/8;
237 StackOffset = ((StackOffset + bytes - 1) / bytes) * bytes;
238 pos.push_back(StackOffset);
239 is_reg.push_back(false);
240 StackOffset += bytes;
244 unsigned getRegisterNum(unsigned argNum) {
245 assert(isRegister(argNum));
248 unsigned getOffset(unsigned argNum) {
249 assert(isOffset(argNum));
252 unsigned isRegister(unsigned argNum) {
253 assert(argNum < is_reg.size());
254 return is_reg[argNum];
256 unsigned isOffset(unsigned argNum) {
257 return !isRegister(argNum);
259 MVT::ValueType getType(unsigned argNum) {
260 assert(argNum < types.size());
261 return types[argNum];
263 unsigned getStackSize(void) {
264 int last = is_reg.size() - 1;
267 if (isRegister(last))
269 return getOffset(last) + MVT::getSizeInBits(getType(last))/8;
271 int lastRegArg(void) {
272 int size = is_reg.size();
274 while(last < size && isRegister(last))
279 int lastRegNum(void) {
280 int l = lastRegArg();
283 unsigned r = getRegisterNum(l);
284 MVT::ValueType t = getType(l);
285 assert(t == MVT::i32 || t == MVT::f32 || t == MVT::f64);
292 // This transforms a ISD::CALL node into a
293 // callseq_star <- ARMISD:CALL <- callseq_end
295 static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
296 SDOperand Chain = Op.getOperand(0);
297 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
298 assert((CallConv == CallingConv::C ||
299 CallConv == CallingConv::Fast)
300 && "unknown calling convention");
301 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
302 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
303 SDOperand Callee = Op.getOperand(4);
304 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
305 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
306 static const unsigned regs[] = {
307 ARM::R0, ARM::R1, ARM::R2, ARM::R3
310 std::vector<MVT::ValueType> Types;
311 for (unsigned i = 0; i < NumOps; ++i) {
312 MVT::ValueType VT = Op.getOperand(5+2*i).getValueType();
315 ArgumentLayout Layout(Types);
317 unsigned NumBytes = Layout.getStackSize();
319 Chain = DAG.getCALLSEQ_START(Chain,
320 DAG.getConstant(NumBytes, MVT::i32));
322 //Build a sequence of stores
323 std::vector<SDOperand> MemOpChains;
324 for (unsigned i = Layout.lastRegArg() + 1; i < NumOps; ++i) {
325 SDOperand Arg = Op.getOperand(5+2*i);
326 unsigned ArgOffset = Layout.getOffset(i);
327 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
328 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
329 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
331 if (!MemOpChains.empty())
332 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
333 &MemOpChains[0], MemOpChains.size());
335 // If the callee is a GlobalAddress node (quite common, every direct call is)
336 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
337 // Likewise ExternalSymbol -> TargetExternalSymbol.
338 assert(Callee.getValueType() == MVT::i32);
339 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
340 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
341 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
342 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
344 // If this is a direct call, pass the chain and the callee.
346 std::vector<SDOperand> Ops;
347 Ops.push_back(Chain);
348 Ops.push_back(Callee);
350 // Build a sequence of copy-to-reg nodes chained together with token chain
351 // and flag operands which copy the outgoing args into the appropriate regs.
353 for (int i = 0, e = Layout.lastRegArg(); i <= e; ++i) {
354 SDOperand Arg = Op.getOperand(5+2*i);
355 unsigned RegNum = Layout.getRegisterNum(i);
356 unsigned Reg1 = regs[RegNum];
357 MVT::ValueType VT = Layout.getType(i);
358 assert(VT == Arg.getValueType());
359 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
361 // Add argument register to the end of the list so that it is known live
363 Ops.push_back(DAG.getRegister(Reg1, MVT::i32));
364 if (VT == MVT::f64) {
365 unsigned Reg2 = regs[RegNum + 1];
366 SDOperand SDReg1 = DAG.getRegister(Reg1, MVT::i32);
367 SDOperand SDReg2 = DAG.getRegister(Reg2, MVT::i32);
369 Ops.push_back(DAG.getRegister(Reg2, MVT::i32));
370 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
371 SDOperand Ops[] = {Chain, SDReg1, SDReg2, Arg, InFlag};
372 Chain = DAG.getNode(ARMISD::FMRRD, VTs, Ops, InFlag.Val ? 5 : 4);
375 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg);
376 Chain = DAG.getCopyToReg(Chain, Reg1, Arg, InFlag);
378 InFlag = Chain.getValue(1);
381 std::vector<MVT::ValueType> NodeTys;
382 NodeTys.push_back(MVT::Other); // Returns a chain
383 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
385 unsigned CallOpc = ARMISD::CALL;
387 Ops.push_back(InFlag);
388 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
389 InFlag = Chain.getValue(1);
391 std::vector<SDOperand> ResultVals;
394 // If the call has results, copy the values out of the ret val registers.
395 MVT::ValueType VT = Op.Val->getValueType(0);
396 if (VT != MVT::Other) {
397 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
399 SDOperand Value1 = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
400 Chain = Value1.getValue(1);
401 InFlag = Value1.getValue(2);
402 NodeTys.push_back(VT);
403 if (VT == MVT::i32) {
404 ResultVals.push_back(Value1);
405 if (Op.Val->getValueType(1) == MVT::i32) {
406 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
407 Chain = Value2.getValue(1);
408 ResultVals.push_back(Value2);
409 NodeTys.push_back(VT);
412 if (VT == MVT::f32) {
413 SDOperand Value = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Value1);
414 ResultVals.push_back(Value);
416 if (VT == MVT::f64) {
417 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
418 Chain = Value2.getValue(1);
419 SDOperand Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
420 ResultVals.push_back(Value);
424 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
425 DAG.getConstant(NumBytes, MVT::i32));
426 NodeTys.push_back(MVT::Other);
428 if (ResultVals.empty())
431 ResultVals.push_back(Chain);
432 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
434 return Res.getValue(Op.ResNo);
437 static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
439 SDOperand Chain = Op.getOperand(0);
440 SDOperand R0 = DAG.getRegister(ARM::R0, MVT::i32);
441 SDOperand R1 = DAG.getRegister(ARM::R1, MVT::i32);
443 switch(Op.getNumOperands()) {
445 assert(0 && "Do not know how to return this many arguments!");
448 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
449 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
452 SDOperand Val = Op.getOperand(1);
453 assert(Val.getValueType() == MVT::i32 ||
454 Val.getValueType() == MVT::f32 ||
455 Val.getValueType() == MVT::f64);
457 if (Val.getValueType() == MVT::f64) {
458 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
459 SDOperand Ops[] = {Chain, R0, R1, Val};
460 Copy = DAG.getNode(ARMISD::FMRRD, VTs, Ops, 4);
462 if (Val.getValueType() == MVT::f32)
463 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
464 Copy = DAG.getCopyToReg(Chain, R0, Val, SDOperand());
467 if (DAG.getMachineFunction().liveout_empty()) {
468 DAG.getMachineFunction().addLiveOut(ARM::R0);
469 if (Val.getValueType() == MVT::f64)
470 DAG.getMachineFunction().addLiveOut(ARM::R1);
475 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
476 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
477 // If we haven't noted the R0+R1 are live out, do so now.
478 if (DAG.getMachineFunction().liveout_empty()) {
479 DAG.getMachineFunction().addLiveOut(ARM::R0);
480 DAG.getMachineFunction().addLiveOut(ARM::R1);
485 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
486 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
489 static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
490 MVT::ValueType PtrVT = Op.getValueType();
491 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
492 Constant *C = CP->getConstVal();
493 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
498 static SDOperand LowerGlobalAddress(SDOperand Op,
500 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
502 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
503 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr, NULL, 0);
506 static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
507 unsigned VarArgsFrameIndex) {
508 // vastart just stores the address of the VarArgsFrameIndex slot into the
509 // memory location argument.
510 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
511 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
512 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
513 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), SV->getValue(),
517 static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
518 int &VarArgsFrameIndex) {
519 MachineFunction &MF = DAG.getMachineFunction();
520 MachineFrameInfo *MFI = MF.getFrameInfo();
521 SSARegMap *RegMap = MF.getSSARegMap();
522 unsigned NumArgs = Op.Val->getNumValues()-1;
523 SDOperand Root = Op.getOperand(0);
524 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
525 static const unsigned REGS[] = {
526 ARM::R0, ARM::R1, ARM::R2, ARM::R3
529 std::vector<MVT::ValueType> Types(Op.Val->value_begin(), Op.Val->value_end() - 1);
530 ArgumentLayout Layout(Types);
532 std::vector<SDOperand> ArgValues;
533 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) {
534 MVT::ValueType VT = Types[ArgNo];
537 if (Layout.isRegister(ArgNo)) {
538 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
539 unsigned RegNum = Layout.getRegisterNum(ArgNo);
540 unsigned Reg1 = REGS[RegNum];
541 unsigned VReg1 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
542 SDOperand Value1 = DAG.getCopyFromReg(Root, VReg1, MVT::i32);
543 MF.addLiveIn(Reg1, VReg1);
544 if (VT == MVT::f64) {
545 unsigned Reg2 = REGS[RegNum + 1];
546 unsigned VReg2 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
547 SDOperand Value2 = DAG.getCopyFromReg(Root, VReg2, MVT::i32);
548 MF.addLiveIn(Reg2, VReg2);
549 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
553 Value = DAG.getNode(ISD::BIT_CONVERT, VT, Value);
556 // If the argument is actually used, emit a load from the right stack
558 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
559 unsigned Offset = Layout.getOffset(ArgNo);
560 unsigned Size = MVT::getSizeInBits(VT)/8;
561 int FI = MFI->CreateFixedObject(Size, Offset);
562 SDOperand FIN = DAG.getFrameIndex(FI, VT);
563 Value = DAG.getLoad(VT, Root, FIN, NULL, 0);
565 Value = DAG.getNode(ISD::UNDEF, VT);
568 ArgValues.push_back(Value);
571 unsigned NextRegNum = Layout.lastRegNum() + 1;
574 //If this function is vararg we must store the remaing
575 //registers so that they can be acessed with va_start
576 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
577 -16 + NextRegNum * 4);
579 SmallVector<SDOperand, 4> MemOps;
580 for (unsigned RegNo = NextRegNum; RegNo < 4; ++RegNo) {
581 int RegOffset = - (4 - RegNo) * 4;
582 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
584 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
586 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
587 MF.addLiveIn(REGS[RegNo], VReg);
589 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
590 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
591 MemOps.push_back(Store);
593 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
596 ArgValues.push_back(Root);
598 // Return the new list of results.
599 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
600 Op.Val->value_end());
601 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
604 static SDOperand GetCMP(ISD::CondCode CC, SDOperand LHS, SDOperand RHS,
606 MVT::ValueType vt = LHS.getValueType();
607 assert(vt == MVT::i32 || vt == MVT::f32 || vt == MVT::f64);
609 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
612 Cmp = DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp);
616 static SDOperand GetARMCC(ISD::CondCode CC, MVT::ValueType vt,
618 assert(vt == MVT::i32 || vt == MVT::f32 || vt == MVT::f64);
620 return DAG.getConstant(DAGIntCCToARMCC(CC), MVT::i32);
622 return DAG.getConstant(DAGFPCCToARMCC(CC), MVT::i32);
625 static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
626 SDOperand LHS = Op.getOperand(0);
627 SDOperand RHS = Op.getOperand(1);
628 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
629 SDOperand TrueVal = Op.getOperand(2);
630 SDOperand FalseVal = Op.getOperand(3);
631 SDOperand Cmp = GetCMP(CC, LHS, RHS, DAG);
632 SDOperand ARMCC = GetARMCC(CC, LHS.getValueType(), DAG);
633 return DAG.getNode(ARMISD::SELECT, MVT::i32, TrueVal, FalseVal, ARMCC, Cmp);
636 static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
637 SDOperand Chain = Op.getOperand(0);
638 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
639 SDOperand LHS = Op.getOperand(2);
640 SDOperand RHS = Op.getOperand(3);
641 SDOperand Dest = Op.getOperand(4);
642 SDOperand Cmp = GetCMP(CC, LHS, RHS, DAG);
643 SDOperand ARMCC = GetARMCC(CC, LHS.getValueType(), DAG);
644 return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, ARMCC, Cmp);
647 static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
648 SDOperand IntVal = Op.getOperand(0);
649 assert(IntVal.getValueType() == MVT::i32);
650 MVT::ValueType vt = Op.getValueType();
651 assert(vt == MVT::f32 ||
654 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
655 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FSITOS : ARMISD::FSITOD;
656 return DAG.getNode(op, vt, Tmp);
659 static SDOperand LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
660 assert(Op.getValueType() == MVT::i32);
661 SDOperand FloatVal = Op.getOperand(0);
662 MVT::ValueType vt = FloatVal.getValueType();
663 assert(vt == MVT::f32 || vt == MVT::f64);
665 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FTOSIS : ARMISD::FTOSID;
666 SDOperand Tmp = DAG.getNode(op, MVT::f32, FloatVal);
667 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Tmp);
670 static SDOperand LowerUINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
671 SDOperand IntVal = Op.getOperand(0);
672 assert(IntVal.getValueType() == MVT::i32);
673 MVT::ValueType vt = Op.getValueType();
674 assert(vt == MVT::f32 ||
677 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
678 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FUITOS : ARMISD::FUITOD;
679 return DAG.getNode(op, vt, Tmp);
682 static SDOperand LowerFP_TO_UINT(SDOperand Op, SelectionDAG &DAG) {
683 assert(Op.getValueType() == MVT::i32);
684 SDOperand FloatVal = Op.getOperand(0);
685 MVT::ValueType vt = FloatVal.getValueType();
686 assert(vt == MVT::f32 || vt == MVT::f64);
688 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FTOUIS : ARMISD::FTOUID;
689 SDOperand Tmp = DAG.getNode(op, MVT::f32, FloatVal);
690 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Tmp);
693 SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
694 switch (Op.getOpcode()) {
696 assert(0 && "Should not custom lower this!");
698 case ISD::ConstantPool:
699 return LowerConstantPool(Op, DAG);
700 case ISD::GlobalAddress:
701 return LowerGlobalAddress(Op, DAG);
702 case ISD::FP_TO_SINT:
703 return LowerFP_TO_SINT(Op, DAG);
704 case ISD::SINT_TO_FP:
705 return LowerSINT_TO_FP(Op, DAG);
706 case ISD::FP_TO_UINT:
707 return LowerFP_TO_UINT(Op, DAG);
708 case ISD::UINT_TO_FP:
709 return LowerUINT_TO_FP(Op, DAG);
710 case ISD::FORMAL_ARGUMENTS:
711 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
713 return LowerCALL(Op, DAG);
715 return LowerRET(Op, DAG);
717 return LowerSELECT_CC(Op, DAG);
719 return LowerBR_CC(Op, DAG);
721 return LowerVASTART(Op, DAG, VarArgsFrameIndex);
725 //===----------------------------------------------------------------------===//
726 // Instruction Selector Implementation
727 //===----------------------------------------------------------------------===//
729 //===--------------------------------------------------------------------===//
730 /// ARMDAGToDAGISel - ARM specific code to select ARM machine
731 /// instructions for SelectionDAG operations.
734 class ARMDAGToDAGISel : public SelectionDAGISel {
735 ARMTargetLowering Lowering;
738 ARMDAGToDAGISel(TargetMachine &TM)
739 : SelectionDAGISel(Lowering), Lowering(TM) {
742 SDNode *Select(SDOperand Op);
743 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
744 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
745 bool SelectAddrMode1(SDOperand N, SDOperand &Arg, SDOperand &Shift,
746 SDOperand &ShiftType);
747 bool SelectAddrMode5(SDOperand N, SDOperand &Arg, SDOperand &Offset);
749 // Include the pieces autogenerated from the target description.
750 #include "ARMGenDAGISel.inc"
753 void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
756 DAG.setRoot(SelectRoot(DAG.getRoot()));
757 DAG.RemoveDeadNodes();
759 ScheduleAndEmitDAG(DAG);
762 static bool isInt12Immediate(SDNode *N, short &Imm) {
763 if (N->getOpcode() != ISD::Constant)
766 int32_t t = cast<ConstantSDNode>(N)->getValue();
769 if (t > min && t < max) {
777 static bool isInt12Immediate(SDOperand Op, short &Imm) {
778 return isInt12Immediate(Op.Val, Imm);
781 static uint32_t rotateL(uint32_t x) {
782 uint32_t bit31 = (x & (1 << 31)) >> 31;
787 static bool isUInt8Immediate(uint32_t x) {
791 static bool isRotInt8Immediate(uint32_t x) {
793 for (r = 0; r < 16; r++) {
794 if (isUInt8Immediate(x))
796 x = rotateL(rotateL(x));
801 bool ARMDAGToDAGISel::SelectAddrMode1(SDOperand N,
804 SDOperand &ShiftType) {
805 switch(N.getOpcode()) {
806 case ISD::Constant: {
807 uint32_t val = cast<ConstantSDNode>(N)->getValue();
808 if(!isRotInt8Immediate(val)) {
809 const Type *t = MVT::getTypeForValueType(MVT::i32);
810 Constant *C = ConstantUInt::get(t, val);
812 SDOperand Addr = CurDAG->getTargetConstantPool(C, MVT::i32, alignment);
813 SDOperand Z = CurDAG->getTargetConstant(0, MVT::i32);
814 SDNode *n = CurDAG->getTargetNode(ARM::ldr, MVT::i32, Z, Addr);
815 Arg = SDOperand(n, 0);
817 Arg = CurDAG->getTargetConstant(val, MVT::i32);
819 Shift = CurDAG->getTargetConstant(0, MVT::i32);
820 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
824 Arg = N.getOperand(0);
825 Shift = N.getOperand(1);
826 ShiftType = CurDAG->getTargetConstant(ARMShift::ASR, MVT::i32);
829 Arg = N.getOperand(0);
830 Shift = N.getOperand(1);
831 ShiftType = CurDAG->getTargetConstant(ARMShift::LSR, MVT::i32);
834 Arg = N.getOperand(0);
835 Shift = N.getOperand(1);
836 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
841 Shift = CurDAG->getTargetConstant(0, MVT::i32);
842 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
846 bool ARMDAGToDAGISel::SelectAddrMode5(SDOperand N, SDOperand &Arg,
848 //TODO: detect offset
849 Offset = CurDAG->getTargetConstant(0, MVT::i32);
854 //register plus/minus 12 bit offset
855 bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
857 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
858 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
859 Offset = CurDAG->getTargetConstant(0, MVT::i32);
862 if (N.getOpcode() == ISD::ADD) {
864 if (isInt12Immediate(N.getOperand(1), imm)) {
865 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
866 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
867 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
869 Base = N.getOperand(0);
871 return true; // [r+i]
875 Offset = CurDAG->getTargetConstant(0, MVT::i32);
876 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
877 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
881 return true; //any address fits in a register
884 SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
887 switch (N->getOpcode()) {
889 return SelectCode(Op);
895 } // end anonymous namespace
897 /// createARMISelDag - This pass converts a legalized DAG into a
898 /// ARM-specific DAG, ready for instruction scheduling.
900 FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
901 return new ARMDAGToDAGISel(TM);