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/Intrinsics.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/SelectionDAG.h"
24 #include "llvm/CodeGen/SelectionDAGISel.h"
25 #include "llvm/CodeGen/SSARegMap.h"
26 #include "llvm/Target/TargetLowering.h"
27 #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);
48 //LLVM requires that a register class supports MVT::f64!
49 addRegisterClass(MVT::f64, ARM::IntRegsRegisterClass);
51 setOperationAction(ISD::RET, MVT::Other, Custom);
52 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
53 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
55 setOperationAction(ISD::SETCC, MVT::i32, Expand);
56 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
57 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
59 setOperationAction(ISD::VASTART, MVT::Other, Custom);
60 setOperationAction(ISD::VAEND, MVT::Other, Expand);
62 setSchedulingPreference(SchedulingForRegPressure);
63 computeRegisterProperties();
69 // Start the numbering where the builting ops and target ops leave off.
70 FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
71 /// CALL - A direct function call.
74 /// Return with a flag operand.
86 /// DAGCCToARMCC - Convert a DAG integer condition code to an ARM CC
87 static ARMCC::CondCodes DAGCCToARMCC(ISD::CondCode CC) {
89 default: assert(0 && "Unknown condition code!");
90 case ISD::SETNE: return ARMCC::NE;
91 case ISD::SETEQ: return ARMCC::EQ;
92 case ISD::SETGE: return ARMCC::GE;
93 case ISD::SETUGE: return ARMCC::CS;
94 case ISD::SETULT: return ARMCC::CC;
98 const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
101 case ARMISD::CALL: return "ARMISD::CALL";
102 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
103 case ARMISD::SELECT: return "ARMISD::SELECT";
104 case ARMISD::CMP: return "ARMISD::CMP";
105 case ARMISD::BR: return "ARMISD::BR";
109 // This transforms a ISD::CALL node into a
110 // callseq_star <- ARMISD:CALL <- callseq_end
112 static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
113 SDOperand Chain = Op.getOperand(0);
114 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
115 assert(CallConv == CallingConv::C && "unknown calling convention");
116 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
117 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
118 assert(isTailCall == false && "tail call not supported");
119 SDOperand Callee = Op.getOperand(4);
120 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
122 // Count how many bytes are to be pushed on the stack.
123 unsigned NumBytes = 0;
125 // Add up all the space actually used.
126 for (unsigned i = 4; i < NumOps; ++i)
127 NumBytes += MVT::getSizeInBits(Op.getOperand(5+2*i).getValueType())/8;
129 // Adjust the stack pointer for the new arguments...
130 // These operations are automatically eliminated by the prolog/epilog pass
131 Chain = DAG.getCALLSEQ_START(Chain,
132 DAG.getConstant(NumBytes, MVT::i32));
134 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
136 static const unsigned int num_regs = 4;
137 static const unsigned regs[num_regs] = {
138 ARM::R0, ARM::R1, ARM::R2, ARM::R3
141 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
142 std::vector<SDOperand> MemOpChains;
144 for (unsigned i = 0; i != NumOps; ++i) {
145 SDOperand Arg = Op.getOperand(5+2*i);
146 assert(Arg.getValueType() == MVT::i32);
148 RegsToPass.push_back(std::make_pair(regs[i], Arg));
150 unsigned ArgOffset = (i - num_regs) * 4;
151 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
152 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
153 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
154 Arg, PtrOff, DAG.getSrcValue(NULL)));
157 if (!MemOpChains.empty())
158 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
159 &MemOpChains[0], MemOpChains.size());
161 // Build a sequence of copy-to-reg nodes chained together with token chain
162 // and flag operands which copy the outgoing args into the appropriate regs.
164 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
165 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
167 InFlag = Chain.getValue(1);
170 std::vector<MVT::ValueType> NodeTys;
171 NodeTys.push_back(MVT::Other); // Returns a chain
172 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
174 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
175 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
176 // node so that legalize doesn't hack it.
177 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
178 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
180 // If this is a direct call, pass the chain and the callee.
182 std::vector<SDOperand> Ops;
183 Ops.push_back(Chain);
184 Ops.push_back(Callee);
186 // Add argument registers to the end of the list so that they are known live
188 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
189 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
190 RegsToPass[i].second.getValueType()));
192 unsigned CallOpc = ARMISD::CALL;
194 Ops.push_back(InFlag);
195 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
196 InFlag = Chain.getValue(1);
198 std::vector<SDOperand> ResultVals;
201 // If the call has results, copy the values out of the ret val registers.
202 switch (Op.Val->getValueType(0)) {
203 default: assert(0 && "Unexpected ret value!");
207 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
208 ResultVals.push_back(Chain.getValue(0));
209 NodeTys.push_back(MVT::i32);
212 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
213 DAG.getConstant(NumBytes, MVT::i32));
214 NodeTys.push_back(MVT::Other);
216 if (ResultVals.empty())
219 ResultVals.push_back(Chain);
220 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
222 return Res.getValue(Op.ResNo);
225 static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
227 SDOperand Chain = Op.getOperand(0);
228 switch(Op.getNumOperands()) {
230 assert(0 && "Do not know how to return this many arguments!");
233 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
234 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
237 Copy = DAG.getCopyToReg(Chain, ARM::R0, Op.getOperand(1), SDOperand());
238 if (DAG.getMachineFunction().liveout_empty())
239 DAG.getMachineFunction().addLiveOut(ARM::R0);
242 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
243 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
244 // If we haven't noted the R0+R1 are live out, do so now.
245 if (DAG.getMachineFunction().liveout_empty()) {
246 DAG.getMachineFunction().addLiveOut(ARM::R0);
247 DAG.getMachineFunction().addLiveOut(ARM::R1);
252 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
253 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
256 static SDOperand LowerFORMAL_ARGUMENT(SDOperand Op, SelectionDAG &DAG,
259 MachineFunction &MF = DAG.getMachineFunction();
260 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
261 assert (ObjectVT == MVT::i32);
262 SDOperand Root = Op.getOperand(0);
263 SSARegMap *RegMap = MF.getSSARegMap();
265 unsigned num_regs = 4;
266 static const unsigned REGS[] = {
267 ARM::R0, ARM::R1, ARM::R2, ARM::R3
270 if(ArgNo < num_regs) {
271 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
272 MF.addLiveIn(REGS[ArgNo], VReg);
274 return DAG.getCopyFromReg(Root, VReg, MVT::i32);
276 // If the argument is actually used, emit a load from the right stack
278 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
279 unsigned ArgOffset = (ArgNo - num_regs) * 4;
281 MachineFrameInfo *MFI = MF.getFrameInfo();
282 unsigned ObjSize = MVT::getSizeInBits(ObjectVT)/8;
283 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
284 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
285 return DAG.getLoad(ObjectVT, Root, FIN,
286 DAG.getSrcValue(NULL));
288 // Don't emit a dead load.
289 return DAG.getNode(ISD::UNDEF, ObjectVT);
294 static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
295 MVT::ValueType PtrVT = Op.getValueType();
296 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
297 Constant *C = CP->getConstVal();
298 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
303 static SDOperand LowerGlobalAddress(SDOperand Op,
305 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
307 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
308 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr,
309 DAG.getSrcValue(NULL));
312 static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
313 unsigned VarArgsFrameIndex) {
314 // vastart just stores the address of the VarArgsFrameIndex slot into the
315 // memory location argument.
316 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
317 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
318 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
319 Op.getOperand(1), Op.getOperand(2));
322 static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
323 int &VarArgsFrameIndex) {
324 std::vector<SDOperand> ArgValues;
325 SDOperand Root = Op.getOperand(0);
328 unsigned NumArgs = Op.Val->getNumValues()-1;
329 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) {
330 SDOperand ArgVal = LowerFORMAL_ARGUMENT(Op, DAG, VRegs, ArgNo);
332 ArgValues.push_back(ArgVal);
335 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
337 MachineFunction &MF = DAG.getMachineFunction();
338 SSARegMap *RegMap = MF.getSSARegMap();
339 MachineFrameInfo *MFI = MF.getFrameInfo();
340 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
344 static const unsigned REGS[] = {
345 ARM::R0, ARM::R1, ARM::R2, ARM::R3
347 // If this function is vararg, store r0-r3 to their spots on the stack
348 // so that they may be loaded by deferencing the result of va_next.
349 SmallVector<SDOperand, 4> MemOps;
350 for (unsigned ArgNo = 0; ArgNo < 4; ++ArgNo) {
351 int ArgOffset = - (4 - ArgNo) * 4;
352 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
354 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
360 VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
361 if (ArgNo >= NumArgs)
362 MF.addLiveIn(REGS[ArgNo], VReg);
364 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
365 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
366 Val, FIN, DAG.getSrcValue(NULL));
367 MemOps.push_back(Store);
369 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
372 ArgValues.push_back(Root);
374 // Return the new list of results.
375 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
376 Op.Val->value_end());
377 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
380 static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
381 SDOperand LHS = Op.getOperand(0);
382 SDOperand RHS = Op.getOperand(1);
383 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
384 SDOperand TrueVal = Op.getOperand(2);
385 SDOperand FalseVal = Op.getOperand(3);
386 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
388 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
389 return DAG.getNode(ARMISD::SELECT, MVT::i32, TrueVal, FalseVal, ARMCC, Cmp);
392 static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
393 SDOperand Chain = Op.getOperand(0);
394 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
395 SDOperand LHS = Op.getOperand(2);
396 SDOperand RHS = Op.getOperand(3);
397 SDOperand Dest = Op.getOperand(4);
398 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
400 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
401 return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, ARMCC, Cmp);
404 SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
405 switch (Op.getOpcode()) {
407 assert(0 && "Should not custom lower this!");
409 case ISD::ConstantPool:
410 return LowerConstantPool(Op, DAG);
411 case ISD::GlobalAddress:
412 return LowerGlobalAddress(Op, DAG);
413 case ISD::FORMAL_ARGUMENTS:
414 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
416 return LowerCALL(Op, DAG);
418 return LowerRET(Op, DAG);
420 return LowerSELECT_CC(Op, DAG);
422 return LowerBR_CC(Op, DAG);
424 return LowerVASTART(Op, DAG, VarArgsFrameIndex);
428 //===----------------------------------------------------------------------===//
429 // Instruction Selector Implementation
430 //===----------------------------------------------------------------------===//
432 //===--------------------------------------------------------------------===//
433 /// ARMDAGToDAGISel - ARM specific code to select ARM machine
434 /// instructions for SelectionDAG operations.
437 class ARMDAGToDAGISel : public SelectionDAGISel {
438 ARMTargetLowering Lowering;
441 ARMDAGToDAGISel(TargetMachine &TM)
442 : SelectionDAGISel(Lowering), Lowering(TM) {
445 SDNode *Select(SDOperand Op);
446 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
447 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
448 bool SelectAddrMode1(SDOperand N, SDOperand &Arg);
450 // Include the pieces autogenerated from the target description.
451 #include "ARMGenDAGISel.inc"
454 void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
457 DAG.setRoot(SelectRoot(DAG.getRoot()));
458 DAG.RemoveDeadNodes();
460 ScheduleAndEmitDAG(DAG);
463 static bool isInt12Immediate(SDNode *N, short &Imm) {
464 if (N->getOpcode() != ISD::Constant)
467 int32_t t = cast<ConstantSDNode>(N)->getValue();
470 if (t > min && t < max) {
478 static bool isInt12Immediate(SDOperand Op, short &Imm) {
479 return isInt12Immediate(Op.Val, Imm);
482 bool ARMDAGToDAGISel::SelectAddrMode1(SDOperand N,
484 switch(N.getOpcode()) {
485 case ISD::Constant: {
486 //TODO:check that we have a valid constant
487 int32_t t = cast<ConstantSDNode>(N)->getValue();
488 Arg = CurDAG->getTargetConstant(t, MVT::i32);
497 //register plus/minus 12 bit offset
498 bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
500 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
501 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
502 Offset = CurDAG->getTargetConstant(0, MVT::i32);
505 if (N.getOpcode() == ISD::ADD) {
507 if (isInt12Immediate(N.getOperand(1), imm)) {
508 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
509 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
510 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
512 Base = N.getOperand(0);
514 return true; // [r+i]
518 Offset = CurDAG->getTargetConstant(0, MVT::i32);
519 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
520 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
524 return true; //any address fits in a register
527 SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
530 switch (N->getOpcode()) {
532 return SelectCode(Op);
538 } // end anonymous namespace
540 /// createARMISelDag - This pass converts a legalized DAG into a
541 /// ARM-specific DAG, ready for instruction scheduling.
543 FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
544 return new ARMDAGToDAGISel(TM);