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"
35 class ARMTargetLowering : public TargetLowering {
36 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
38 ARMTargetLowering(TargetMachine &TM);
39 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
40 virtual const char *getTargetNodeName(unsigned Opcode) const;
45 ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
46 : TargetLowering(TM) {
47 addRegisterClass(MVT::i32, ARM::IntRegsRegisterClass);
48 addRegisterClass(MVT::f32, ARM::FPRegsRegisterClass);
49 addRegisterClass(MVT::f64, ARM::DFPRegsRegisterClass);
51 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
53 setOperationAction(ISD::RET, MVT::Other, Custom);
54 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
55 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
57 setOperationAction(ISD::SETCC, MVT::i32, Expand);
58 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
59 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
61 setOperationAction(ISD::VASTART, MVT::Other, Custom);
62 setOperationAction(ISD::VAEND, MVT::Other, Expand);
64 setSchedulingPreference(SchedulingForRegPressure);
65 computeRegisterProperties();
71 // Start the numbering where the builting ops and target ops leave off.
72 FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
73 /// CALL - A direct function call.
76 /// Return with a flag operand.
90 /// DAGCCToARMCC - Convert a DAG integer condition code to an ARM CC
91 static ARMCC::CondCodes DAGCCToARMCC(ISD::CondCode CC) {
94 std::cerr << "CC = " << CC << "\n";
95 assert(0 && "Unknown condition code!");
96 case ISD::SETUGT: return ARMCC::HI;
97 case ISD::SETULE: return ARMCC::LS;
98 case ISD::SETLE: return ARMCC::LE;
99 case ISD::SETLT: return ARMCC::LT;
100 case ISD::SETGT: return ARMCC::GT;
101 case ISD::SETNE: return ARMCC::NE;
102 case ISD::SETEQ: return ARMCC::EQ;
103 case ISD::SETGE: return ARMCC::GE;
104 case ISD::SETUGE: return ARMCC::CS;
105 case ISD::SETULT: return ARMCC::CC;
109 const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
112 case ARMISD::CALL: return "ARMISD::CALL";
113 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
114 case ARMISD::SELECT: return "ARMISD::SELECT";
115 case ARMISD::CMP: return "ARMISD::CMP";
116 case ARMISD::BR: return "ARMISD::BR";
117 case ARMISD::FSITOS: return "ARMISD::FSITOS";
121 // This transforms a ISD::CALL node into a
122 // callseq_star <- ARMISD:CALL <- callseq_end
124 static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
125 SDOperand Chain = Op.getOperand(0);
126 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
127 assert(CallConv == CallingConv::C && "unknown calling convention");
128 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
129 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
130 assert(isTailCall == false && "tail call not supported");
131 SDOperand Callee = Op.getOperand(4);
132 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
134 // Count how many bytes are to be pushed on the stack.
135 unsigned NumBytes = 0;
137 // Add up all the space actually used.
138 for (unsigned i = 4; i < NumOps; ++i)
139 NumBytes += MVT::getSizeInBits(Op.getOperand(5+2*i).getValueType())/8;
141 // Adjust the stack pointer for the new arguments...
142 // These operations are automatically eliminated by the prolog/epilog pass
143 Chain = DAG.getCALLSEQ_START(Chain,
144 DAG.getConstant(NumBytes, MVT::i32));
146 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
148 static const unsigned int num_regs = 4;
149 static const unsigned regs[num_regs] = {
150 ARM::R0, ARM::R1, ARM::R2, ARM::R3
153 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
154 std::vector<SDOperand> MemOpChains;
156 for (unsigned i = 0; i != NumOps; ++i) {
157 SDOperand Arg = Op.getOperand(5+2*i);
158 assert(Arg.getValueType() == MVT::i32);
160 RegsToPass.push_back(std::make_pair(regs[i], Arg));
162 unsigned ArgOffset = (i - num_regs) * 4;
163 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
164 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
165 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
166 Arg, PtrOff, DAG.getSrcValue(NULL)));
169 if (!MemOpChains.empty())
170 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
171 &MemOpChains[0], MemOpChains.size());
173 // Build a sequence of copy-to-reg nodes chained together with token chain
174 // and flag operands which copy the outgoing args into the appropriate regs.
176 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
177 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
179 InFlag = Chain.getValue(1);
182 std::vector<MVT::ValueType> NodeTys;
183 NodeTys.push_back(MVT::Other); // Returns a chain
184 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
186 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
187 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
188 // node so that legalize doesn't hack it.
189 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
190 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
192 // If this is a direct call, pass the chain and the callee.
194 std::vector<SDOperand> Ops;
195 Ops.push_back(Chain);
196 Ops.push_back(Callee);
198 // Add argument registers to the end of the list so that they are known live
200 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
201 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
202 RegsToPass[i].second.getValueType()));
204 unsigned CallOpc = ARMISD::CALL;
206 Ops.push_back(InFlag);
207 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
208 InFlag = Chain.getValue(1);
210 std::vector<SDOperand> ResultVals;
213 // If the call has results, copy the values out of the ret val registers.
214 switch (Op.Val->getValueType(0)) {
215 default: assert(0 && "Unexpected ret value!");
219 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
220 ResultVals.push_back(Chain.getValue(0));
221 NodeTys.push_back(MVT::i32);
224 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
225 DAG.getConstant(NumBytes, MVT::i32));
226 NodeTys.push_back(MVT::Other);
228 if (ResultVals.empty())
231 ResultVals.push_back(Chain);
232 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
234 return Res.getValue(Op.ResNo);
237 static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
239 SDOperand Chain = Op.getOperand(0);
240 switch(Op.getNumOperands()) {
242 assert(0 && "Do not know how to return this many arguments!");
245 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
246 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
249 SDOperand Val = Op.getOperand(1);
250 assert(Val.getValueType() == MVT::i32 ||
251 Val.getValueType() == MVT::f32);
253 if (Val.getValueType() == MVT::f32)
254 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
255 Copy = DAG.getCopyToReg(Chain, ARM::R0, Val, SDOperand());
256 if (DAG.getMachineFunction().liveout_empty())
257 DAG.getMachineFunction().addLiveOut(ARM::R0);
261 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
262 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
263 // If we haven't noted the R0+R1 are live out, do so now.
264 if (DAG.getMachineFunction().liveout_empty()) {
265 DAG.getMachineFunction().addLiveOut(ARM::R0);
266 DAG.getMachineFunction().addLiveOut(ARM::R1);
271 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
272 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
275 static SDOperand LowerFORMAL_ARGUMENT(SDOperand Op, SelectionDAG &DAG,
278 MachineFunction &MF = DAG.getMachineFunction();
279 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
280 assert (ObjectVT == MVT::i32);
281 SDOperand Root = Op.getOperand(0);
282 SSARegMap *RegMap = MF.getSSARegMap();
284 unsigned num_regs = 4;
285 static const unsigned REGS[] = {
286 ARM::R0, ARM::R1, ARM::R2, ARM::R3
289 if(ArgNo < num_regs) {
290 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
291 MF.addLiveIn(REGS[ArgNo], VReg);
293 return DAG.getCopyFromReg(Root, VReg, MVT::i32);
295 // If the argument is actually used, emit a load from the right stack
297 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
298 unsigned ArgOffset = (ArgNo - num_regs) * 4;
300 MachineFrameInfo *MFI = MF.getFrameInfo();
301 unsigned ObjSize = MVT::getSizeInBits(ObjectVT)/8;
302 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
303 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
304 return DAG.getLoad(ObjectVT, Root, FIN,
305 DAG.getSrcValue(NULL));
307 // Don't emit a dead load.
308 return DAG.getNode(ISD::UNDEF, ObjectVT);
313 static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
314 MVT::ValueType PtrVT = Op.getValueType();
315 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
316 Constant *C = CP->getConstVal();
317 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
322 static SDOperand LowerGlobalAddress(SDOperand Op,
324 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
326 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
327 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr,
328 DAG.getSrcValue(NULL));
331 static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
332 unsigned VarArgsFrameIndex) {
333 // vastart just stores the address of the VarArgsFrameIndex slot into the
334 // memory location argument.
335 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
336 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
337 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
338 Op.getOperand(1), Op.getOperand(2));
341 static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
342 int &VarArgsFrameIndex) {
343 std::vector<SDOperand> ArgValues;
344 SDOperand Root = Op.getOperand(0);
347 unsigned NumArgs = Op.Val->getNumValues()-1;
348 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) {
349 SDOperand ArgVal = LowerFORMAL_ARGUMENT(Op, DAG, VRegs, ArgNo);
351 ArgValues.push_back(ArgVal);
354 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
356 MachineFunction &MF = DAG.getMachineFunction();
357 SSARegMap *RegMap = MF.getSSARegMap();
358 MachineFrameInfo *MFI = MF.getFrameInfo();
359 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
363 static const unsigned REGS[] = {
364 ARM::R0, ARM::R1, ARM::R2, ARM::R3
366 // If this function is vararg, store r0-r3 to their spots on the stack
367 // so that they may be loaded by deferencing the result of va_next.
368 SmallVector<SDOperand, 4> MemOps;
369 for (unsigned ArgNo = 0; ArgNo < 4; ++ArgNo) {
370 int ArgOffset = - (4 - ArgNo) * 4;
371 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
373 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
379 VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
380 if (ArgNo >= NumArgs)
381 MF.addLiveIn(REGS[ArgNo], VReg);
383 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
384 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
385 Val, FIN, DAG.getSrcValue(NULL));
386 MemOps.push_back(Store);
388 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
391 ArgValues.push_back(Root);
393 // Return the new list of results.
394 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
395 Op.Val->value_end());
396 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
399 static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
400 SDOperand LHS = Op.getOperand(0);
401 SDOperand RHS = Op.getOperand(1);
402 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
403 SDOperand TrueVal = Op.getOperand(2);
404 SDOperand FalseVal = Op.getOperand(3);
405 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
407 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
408 return DAG.getNode(ARMISD::SELECT, MVT::i32, TrueVal, FalseVal, ARMCC, Cmp);
411 static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
412 SDOperand Chain = Op.getOperand(0);
413 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
414 SDOperand LHS = Op.getOperand(2);
415 SDOperand RHS = Op.getOperand(3);
416 SDOperand Dest = Op.getOperand(4);
417 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
419 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
420 return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, ARMCC, Cmp);
423 static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
424 SDOperand IntVal = Op.getOperand(0);
425 assert(IntVal.getValueType() == MVT::i32);
426 assert(Op.getValueType() == MVT::f32);
428 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
429 return DAG.getNode(ARMISD::FSITOS, MVT::f32, Tmp);
432 SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
433 switch (Op.getOpcode()) {
435 assert(0 && "Should not custom lower this!");
437 case ISD::ConstantPool:
438 return LowerConstantPool(Op, DAG);
439 case ISD::GlobalAddress:
440 return LowerGlobalAddress(Op, DAG);
441 case ISD::SINT_TO_FP:
442 return LowerSINT_TO_FP(Op, DAG);
443 case ISD::FORMAL_ARGUMENTS:
444 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
446 return LowerCALL(Op, DAG);
448 return LowerRET(Op, DAG);
450 return LowerSELECT_CC(Op, DAG);
452 return LowerBR_CC(Op, DAG);
454 return LowerVASTART(Op, DAG, VarArgsFrameIndex);
458 //===----------------------------------------------------------------------===//
459 // Instruction Selector Implementation
460 //===----------------------------------------------------------------------===//
462 //===--------------------------------------------------------------------===//
463 /// ARMDAGToDAGISel - ARM specific code to select ARM machine
464 /// instructions for SelectionDAG operations.
467 class ARMDAGToDAGISel : public SelectionDAGISel {
468 ARMTargetLowering Lowering;
471 ARMDAGToDAGISel(TargetMachine &TM)
472 : SelectionDAGISel(Lowering), Lowering(TM) {
475 SDNode *Select(SDOperand Op);
476 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
477 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
478 bool SelectAddrMode1(SDOperand N, SDOperand &Arg, SDOperand &Shift,
479 SDOperand &ShiftType);
481 // Include the pieces autogenerated from the target description.
482 #include "ARMGenDAGISel.inc"
485 void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
488 DAG.setRoot(SelectRoot(DAG.getRoot()));
489 DAG.RemoveDeadNodes();
491 ScheduleAndEmitDAG(DAG);
494 static bool isInt12Immediate(SDNode *N, short &Imm) {
495 if (N->getOpcode() != ISD::Constant)
498 int32_t t = cast<ConstantSDNode>(N)->getValue();
501 if (t > min && t < max) {
509 static bool isInt12Immediate(SDOperand Op, short &Imm) {
510 return isInt12Immediate(Op.Val, Imm);
513 static uint32_t rotateL(uint32_t x) {
514 uint32_t bit31 = (x & (1 << 31)) >> 31;
519 static bool isUInt8Immediate(uint32_t x) {
523 static bool isRotInt8Immediate(uint32_t x) {
525 for (r = 0; r < 16; r++) {
526 if (isUInt8Immediate(x))
528 x = rotateL(rotateL(x));
533 bool ARMDAGToDAGISel::SelectAddrMode1(SDOperand N,
536 SDOperand &ShiftType) {
537 switch(N.getOpcode()) {
538 case ISD::Constant: {
539 uint32_t val = cast<ConstantSDNode>(N)->getValue();
540 if(!isRotInt8Immediate(val)) {
541 const Type *t = MVT::getTypeForValueType(MVT::i32);
542 Constant *C = ConstantUInt::get(t, val);
544 SDOperand Addr = CurDAG->getTargetConstantPool(C, MVT::i32, alignment);
545 SDOperand Z = CurDAG->getTargetConstant(0, MVT::i32);
546 SDNode *n = CurDAG->getTargetNode(ARM::ldr, MVT::i32, Z, Addr);
547 Arg = SDOperand(n, 0);
549 Arg = CurDAG->getTargetConstant(val, MVT::i32);
551 Shift = CurDAG->getTargetConstant(0, MVT::i32);
552 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
556 Arg = N.getOperand(0);
557 Shift = N.getOperand(1);
558 ShiftType = CurDAG->getTargetConstant(ARMShift::ASR, MVT::i32);
561 Arg = N.getOperand(0);
562 Shift = N.getOperand(1);
563 ShiftType = CurDAG->getTargetConstant(ARMShift::LSR, MVT::i32);
566 Arg = N.getOperand(0);
567 Shift = N.getOperand(1);
568 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
573 Shift = CurDAG->getTargetConstant(0, MVT::i32);
574 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
578 //register plus/minus 12 bit offset
579 bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
581 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
582 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
583 Offset = CurDAG->getTargetConstant(0, MVT::i32);
586 if (N.getOpcode() == ISD::ADD) {
588 if (isInt12Immediate(N.getOperand(1), imm)) {
589 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
590 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
591 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
593 Base = N.getOperand(0);
595 return true; // [r+i]
599 Offset = CurDAG->getTargetConstant(0, MVT::i32);
600 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
601 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
605 return true; //any address fits in a register
608 SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
611 switch (N->getOpcode()) {
613 return SelectCode(Op);
619 } // end anonymous namespace
621 /// createARMISelDag - This pass converts a legalized DAG into a
622 /// ARM-specific DAG, ready for instruction scheduling.
624 FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
625 return new ARMDAGToDAGISel(TM);