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::SINT_TO_FP, MVT::i32, Custom);
54 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
56 setOperationAction(ISD::RET, MVT::Other, Custom);
57 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
58 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
60 setOperationAction(ISD::SELECT, MVT::i32, Expand);
62 setOperationAction(ISD::SETCC, MVT::i32, Expand);
63 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
64 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
66 setOperationAction(ISD::VASTART, MVT::Other, Custom);
67 setOperationAction(ISD::VAEND, MVT::Other, Expand);
69 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
70 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
72 setSchedulingPreference(SchedulingForRegPressure);
73 computeRegisterProperties();
79 // Start the numbering where the builting ops and target ops leave off.
80 FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
81 /// CALL - A direct function call.
84 /// Return with a flag operand.
108 /// DAGCCToARMCC - Convert a DAG integer condition code to an ARM CC
109 static ARMCC::CondCodes DAGCCToARMCC(ISD::CondCode CC) {
112 std::cerr << "CC = " << CC << "\n";
113 assert(0 && "Unknown condition code!");
114 case ISD::SETUGT: return ARMCC::HI;
115 case ISD::SETULE: return ARMCC::LS;
116 case ISD::SETLE: return ARMCC::LE;
117 case ISD::SETLT: return ARMCC::LT;
118 case ISD::SETGT: return ARMCC::GT;
119 case ISD::SETNE: return ARMCC::NE;
120 case ISD::SETEQ: return ARMCC::EQ;
121 case ISD::SETGE: return ARMCC::GE;
122 case ISD::SETUGE: return ARMCC::CS;
123 case ISD::SETULT: return ARMCC::CC;
127 const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
130 case ARMISD::CALL: return "ARMISD::CALL";
131 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
132 case ARMISD::SELECT: return "ARMISD::SELECT";
133 case ARMISD::CMP: return "ARMISD::CMP";
134 case ARMISD::BR: return "ARMISD::BR";
135 case ARMISD::FSITOS: return "ARMISD::FSITOS";
136 case ARMISD::FSITOD: return "ARMISD::FSITOD";
137 case ARMISD::FUITOS: return "ARMISD::FUITOS";
138 case ARMISD::FUITOD: return "ARMISD::FUITOD";
139 case ARMISD::FMRRD: return "ARMISD::FMRRD";
140 case ARMISD::FMDRR: return "ARMISD::FMDRR";
144 class ArgumentLayout {
145 std::vector<bool> is_reg;
146 std::vector<unsigned> pos;
147 std::vector<MVT::ValueType> types;
149 ArgumentLayout(const std::vector<MVT::ValueType> &Types) {
153 unsigned StackOffset = 0;
154 for(std::vector<MVT::ValueType>::const_iterator I = Types.begin();
157 MVT::ValueType VT = *I;
158 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
159 unsigned size = MVT::getSizeInBits(VT)/32;
161 RegNum = ((RegNum + size - 1) / size) * size;
163 pos.push_back(RegNum);
164 is_reg.push_back(true);
167 unsigned bytes = size * 32/8;
168 StackOffset = ((StackOffset + bytes - 1) / bytes) * bytes;
169 pos.push_back(StackOffset);
170 is_reg.push_back(false);
171 StackOffset += bytes;
175 unsigned getRegisterNum(unsigned argNum) {
176 assert(isRegister(argNum));
179 unsigned getOffset(unsigned argNum) {
180 assert(isOffset(argNum));
183 unsigned isRegister(unsigned argNum) {
184 assert(argNum < is_reg.size());
185 return is_reg[argNum];
187 unsigned isOffset(unsigned argNum) {
188 return !isRegister(argNum);
190 MVT::ValueType getType(unsigned argNum) {
191 assert(argNum < types.size());
192 return types[argNum];
194 unsigned getStackSize(void) {
195 int last = is_reg.size() - 1;
198 if (isRegister(last))
200 return getOffset(last) + MVT::getSizeInBits(getType(last))/8;
202 int lastRegArg(void) {
203 int size = is_reg.size();
205 while(last < size && isRegister(last))
210 int lastRegNum(void) {
211 int l = lastRegArg();
214 unsigned r = getRegisterNum(l);
215 MVT::ValueType t = getType(l);
216 assert(t == MVT::i32 || t == MVT::f32 || t == MVT::f64);
223 // This transforms a ISD::CALL node into a
224 // callseq_star <- ARMISD:CALL <- callseq_end
226 static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
227 SDOperand Chain = Op.getOperand(0);
228 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
229 assert(CallConv == CallingConv::C && "unknown calling convention");
230 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
231 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
232 assert(isTailCall == false && "tail call not supported");
233 SDOperand Callee = Op.getOperand(4);
234 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
235 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
236 static const unsigned regs[] = {
237 ARM::R0, ARM::R1, ARM::R2, ARM::R3
240 std::vector<MVT::ValueType> Types;
241 for (unsigned i = 0; i < NumOps; ++i) {
242 MVT::ValueType VT = Op.getOperand(5+2*i).getValueType();
245 ArgumentLayout Layout(Types);
247 unsigned NumBytes = Layout.getStackSize();
249 Chain = DAG.getCALLSEQ_START(Chain,
250 DAG.getConstant(NumBytes, MVT::i32));
252 //Build a sequence of stores
253 std::vector<SDOperand> MemOpChains;
254 for (unsigned i = Layout.lastRegArg() + 1; i < NumOps; ++i) {
255 SDOperand Arg = Op.getOperand(5+2*i);
256 unsigned ArgOffset = Layout.getOffset(i);
257 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
258 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
259 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff,
260 DAG.getSrcValue(NULL)));
262 if (!MemOpChains.empty())
263 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
264 &MemOpChains[0], MemOpChains.size());
266 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
267 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
268 // node so that legalize doesn't hack it.
269 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
270 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
272 // If this is a direct call, pass the chain and the callee.
274 std::vector<SDOperand> Ops;
275 Ops.push_back(Chain);
276 Ops.push_back(Callee);
278 // Build a sequence of copy-to-reg nodes chained together with token chain
279 // and flag operands which copy the outgoing args into the appropriate regs.
281 for (int i = 0, e = Layout.lastRegArg(); i <= e; ++i) {
282 SDOperand Arg = Op.getOperand(5+2*i);
283 unsigned RegNum = Layout.getRegisterNum(i);
284 unsigned Reg1 = regs[RegNum];
285 MVT::ValueType VT = Layout.getType(i);
286 assert(VT == Arg.getValueType());
287 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
289 // Add argument register to the end of the list so that it is known live
291 Ops.push_back(DAG.getRegister(Reg1, MVT::i32));
292 if (VT == MVT::f64) {
293 unsigned Reg2 = regs[RegNum + 1];
294 SDOperand SDReg1 = DAG.getRegister(Reg1, MVT::i32);
295 SDOperand SDReg2 = DAG.getRegister(Reg2, MVT::i32);
297 Ops.push_back(DAG.getRegister(Reg2, MVT::i32));
298 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
299 SDOperand Ops[] = {Chain, SDReg1, SDReg2, Arg, InFlag};
300 Chain = DAG.getNode(ARMISD::FMRRD, VTs, Ops, InFlag.Val ? 5 : 4);
303 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg);
304 Chain = DAG.getCopyToReg(Chain, Reg1, Arg, InFlag);
306 InFlag = Chain.getValue(1);
309 std::vector<MVT::ValueType> NodeTys;
310 NodeTys.push_back(MVT::Other); // Returns a chain
311 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
313 unsigned CallOpc = ARMISD::CALL;
315 Ops.push_back(InFlag);
316 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
317 InFlag = Chain.getValue(1);
319 std::vector<SDOperand> ResultVals;
322 // If the call has results, copy the values out of the ret val registers.
323 MVT::ValueType VT = Op.Val->getValueType(0);
324 if (VT != MVT::Other) {
325 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
328 SDOperand Value1 = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
329 Chain = Value1.getValue(1);
330 InFlag = Value1.getValue(2);
334 Value = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Value1);
335 if (VT == MVT::f64) {
336 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
337 Chain = Value2.getValue(1);
338 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
340 ResultVals.push_back(Value);
341 NodeTys.push_back(VT);
344 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
345 DAG.getConstant(NumBytes, MVT::i32));
346 NodeTys.push_back(MVT::Other);
348 if (ResultVals.empty())
351 ResultVals.push_back(Chain);
352 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
354 return Res.getValue(Op.ResNo);
357 static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
359 SDOperand Chain = Op.getOperand(0);
360 SDOperand R0 = DAG.getRegister(ARM::R0, MVT::i32);
361 SDOperand R1 = DAG.getRegister(ARM::R1, MVT::i32);
363 switch(Op.getNumOperands()) {
365 assert(0 && "Do not know how to return this many arguments!");
368 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
369 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
372 SDOperand Val = Op.getOperand(1);
373 assert(Val.getValueType() == MVT::i32 ||
374 Val.getValueType() == MVT::f32 ||
375 Val.getValueType() == MVT::f64);
377 if (Val.getValueType() == MVT::f64) {
378 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
379 SDOperand Ops[] = {Chain, R0, R1, Val};
380 Copy = DAG.getNode(ARMISD::FMRRD, VTs, Ops, 4);
382 if (Val.getValueType() == MVT::f32)
383 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
384 Copy = DAG.getCopyToReg(Chain, R0, Val, SDOperand());
387 if (DAG.getMachineFunction().liveout_empty()) {
388 DAG.getMachineFunction().addLiveOut(ARM::R0);
389 if (Val.getValueType() == MVT::f64)
390 DAG.getMachineFunction().addLiveOut(ARM::R1);
395 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
396 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
397 // If we haven't noted the R0+R1 are live out, do so now.
398 if (DAG.getMachineFunction().liveout_empty()) {
399 DAG.getMachineFunction().addLiveOut(ARM::R0);
400 DAG.getMachineFunction().addLiveOut(ARM::R1);
405 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
406 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
409 static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
410 MVT::ValueType PtrVT = Op.getValueType();
411 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
412 Constant *C = CP->getConstVal();
413 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
418 static SDOperand LowerGlobalAddress(SDOperand Op,
420 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
422 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
423 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr, NULL, 0);
426 static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
427 unsigned VarArgsFrameIndex) {
428 // vastart just stores the address of the VarArgsFrameIndex slot into the
429 // memory location argument.
430 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
431 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
432 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), Op.getOperand(2));
435 static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
436 int &VarArgsFrameIndex) {
437 MachineFunction &MF = DAG.getMachineFunction();
438 MachineFrameInfo *MFI = MF.getFrameInfo();
439 SSARegMap *RegMap = MF.getSSARegMap();
440 unsigned NumArgs = Op.Val->getNumValues()-1;
441 SDOperand Root = Op.getOperand(0);
442 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
443 static const unsigned REGS[] = {
444 ARM::R0, ARM::R1, ARM::R2, ARM::R3
447 std::vector<MVT::ValueType> Types(Op.Val->value_begin(), Op.Val->value_end() - 1);
448 ArgumentLayout Layout(Types);
450 std::vector<SDOperand> ArgValues;
451 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) {
452 MVT::ValueType VT = Types[ArgNo];
455 if (Layout.isRegister(ArgNo)) {
456 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
457 unsigned RegNum = Layout.getRegisterNum(ArgNo);
458 unsigned Reg1 = REGS[RegNum];
459 unsigned VReg1 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
460 SDOperand Value1 = DAG.getCopyFromReg(Root, VReg1, MVT::i32);
461 MF.addLiveIn(Reg1, VReg1);
462 if (VT == MVT::f64) {
463 unsigned Reg2 = REGS[RegNum + 1];
464 unsigned VReg2 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
465 SDOperand Value2 = DAG.getCopyFromReg(Root, VReg2, MVT::i32);
466 MF.addLiveIn(Reg2, VReg2);
467 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
471 Value = DAG.getNode(ISD::BIT_CONVERT, VT, Value);
474 // If the argument is actually used, emit a load from the right stack
476 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
477 unsigned Offset = Layout.getOffset(ArgNo);
478 unsigned Size = MVT::getSizeInBits(VT)/8;
479 int FI = MFI->CreateFixedObject(Size, Offset);
480 SDOperand FIN = DAG.getFrameIndex(FI, VT);
481 Value = DAG.getLoad(VT, Root, FIN, NULL, 0);
483 Value = DAG.getNode(ISD::UNDEF, VT);
486 ArgValues.push_back(Value);
489 unsigned NextRegNum = Layout.lastRegNum() + 1;
492 //If this function is vararg we must store the remaing
493 //registers so that they can be acessed with va_start
494 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
495 -16 + NextRegNum * 4);
497 SmallVector<SDOperand, 4> MemOps;
498 for (unsigned RegNo = NextRegNum; RegNo < 4; ++RegNo) {
499 int RegOffset = - (4 - RegNo) * 4;
500 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
502 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
504 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
505 MF.addLiveIn(REGS[RegNo], VReg);
507 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
508 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN,
509 DAG.getSrcValue(NULL));
510 MemOps.push_back(Store);
512 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
515 ArgValues.push_back(Root);
517 // Return the new list of results.
518 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
519 Op.Val->value_end());
520 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
523 static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
524 SDOperand LHS = Op.getOperand(0);
525 SDOperand RHS = Op.getOperand(1);
526 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
527 SDOperand TrueVal = Op.getOperand(2);
528 SDOperand FalseVal = Op.getOperand(3);
529 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
531 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
532 return DAG.getNode(ARMISD::SELECT, MVT::i32, TrueVal, FalseVal, ARMCC, Cmp);
535 static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
536 SDOperand Chain = Op.getOperand(0);
537 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
538 SDOperand LHS = Op.getOperand(2);
539 SDOperand RHS = Op.getOperand(3);
540 SDOperand Dest = Op.getOperand(4);
541 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
543 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
544 return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, ARMCC, Cmp);
547 static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
548 SDOperand IntVal = Op.getOperand(0);
549 assert(IntVal.getValueType() == MVT::i32);
550 MVT::ValueType vt = Op.getValueType();
551 assert(vt == MVT::f32 ||
554 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
555 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FSITOS : ARMISD::FSITOD;
556 return DAG.getNode(op, vt, Tmp);
559 static SDOperand LowerUINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
560 SDOperand IntVal = Op.getOperand(0);
561 assert(IntVal.getValueType() == MVT::i32);
562 MVT::ValueType vt = Op.getValueType();
563 assert(vt == MVT::f32 ||
566 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
567 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FUITOS : ARMISD::FUITOD;
568 return DAG.getNode(op, vt, Tmp);
571 SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
572 switch (Op.getOpcode()) {
574 assert(0 && "Should not custom lower this!");
576 case ISD::ConstantPool:
577 return LowerConstantPool(Op, DAG);
578 case ISD::GlobalAddress:
579 return LowerGlobalAddress(Op, DAG);
580 case ISD::SINT_TO_FP:
581 return LowerSINT_TO_FP(Op, DAG);
582 case ISD::UINT_TO_FP:
583 return LowerUINT_TO_FP(Op, DAG);
584 case ISD::FORMAL_ARGUMENTS:
585 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
587 return LowerCALL(Op, DAG);
589 return LowerRET(Op, DAG);
591 return LowerSELECT_CC(Op, DAG);
593 return LowerBR_CC(Op, DAG);
595 return LowerVASTART(Op, DAG, VarArgsFrameIndex);
599 //===----------------------------------------------------------------------===//
600 // Instruction Selector Implementation
601 //===----------------------------------------------------------------------===//
603 //===--------------------------------------------------------------------===//
604 /// ARMDAGToDAGISel - ARM specific code to select ARM machine
605 /// instructions for SelectionDAG operations.
608 class ARMDAGToDAGISel : public SelectionDAGISel {
609 ARMTargetLowering Lowering;
612 ARMDAGToDAGISel(TargetMachine &TM)
613 : SelectionDAGISel(Lowering), Lowering(TM) {
616 SDNode *Select(SDOperand Op);
617 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
618 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
619 bool SelectAddrMode1(SDOperand N, SDOperand &Arg, SDOperand &Shift,
620 SDOperand &ShiftType);
622 // Include the pieces autogenerated from the target description.
623 #include "ARMGenDAGISel.inc"
626 void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
629 DAG.setRoot(SelectRoot(DAG.getRoot()));
630 DAG.RemoveDeadNodes();
632 ScheduleAndEmitDAG(DAG);
635 static bool isInt12Immediate(SDNode *N, short &Imm) {
636 if (N->getOpcode() != ISD::Constant)
639 int32_t t = cast<ConstantSDNode>(N)->getValue();
642 if (t > min && t < max) {
650 static bool isInt12Immediate(SDOperand Op, short &Imm) {
651 return isInt12Immediate(Op.Val, Imm);
654 static uint32_t rotateL(uint32_t x) {
655 uint32_t bit31 = (x & (1 << 31)) >> 31;
660 static bool isUInt8Immediate(uint32_t x) {
664 static bool isRotInt8Immediate(uint32_t x) {
666 for (r = 0; r < 16; r++) {
667 if (isUInt8Immediate(x))
669 x = rotateL(rotateL(x));
674 bool ARMDAGToDAGISel::SelectAddrMode1(SDOperand N,
677 SDOperand &ShiftType) {
678 switch(N.getOpcode()) {
679 case ISD::Constant: {
680 uint32_t val = cast<ConstantSDNode>(N)->getValue();
681 if(!isRotInt8Immediate(val)) {
682 const Type *t = MVT::getTypeForValueType(MVT::i32);
683 Constant *C = ConstantUInt::get(t, val);
685 SDOperand Addr = CurDAG->getTargetConstantPool(C, MVT::i32, alignment);
686 SDOperand Z = CurDAG->getTargetConstant(0, MVT::i32);
687 SDNode *n = CurDAG->getTargetNode(ARM::ldr, MVT::i32, Z, Addr);
688 Arg = SDOperand(n, 0);
690 Arg = CurDAG->getTargetConstant(val, MVT::i32);
692 Shift = CurDAG->getTargetConstant(0, MVT::i32);
693 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
697 Arg = N.getOperand(0);
698 Shift = N.getOperand(1);
699 ShiftType = CurDAG->getTargetConstant(ARMShift::ASR, MVT::i32);
702 Arg = N.getOperand(0);
703 Shift = N.getOperand(1);
704 ShiftType = CurDAG->getTargetConstant(ARMShift::LSR, MVT::i32);
707 Arg = N.getOperand(0);
708 Shift = N.getOperand(1);
709 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
714 Shift = CurDAG->getTargetConstant(0, MVT::i32);
715 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
719 //register plus/minus 12 bit offset
720 bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
722 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
723 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
724 Offset = CurDAG->getTargetConstant(0, MVT::i32);
727 if (N.getOpcode() == ISD::ADD) {
729 if (isInt12Immediate(N.getOperand(1), imm)) {
730 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
731 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
732 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
734 Base = N.getOperand(0);
736 return true; // [r+i]
740 Offset = CurDAG->getTargetConstant(0, MVT::i32);
741 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
742 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
746 return true; //any address fits in a register
749 SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
752 switch (N->getOpcode()) {
754 return SelectCode(Op);
760 } // end anonymous namespace
762 /// createARMISelDag - This pass converts a legalized DAG into a
763 /// ARM-specific DAG, ready for instruction scheduling.
765 FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
766 return new ARMDAGToDAGISel(TM);