blob: 1ef5bf48ca69280095cceaa39adb2cfbc294d21d [file] [log] [blame]
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001//===-- ARMISelDAGToDAG.cpp - A dag to dag inst selector for ARM ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an instruction selector for the ARM target.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ARM.h"
15#include "ARMTargetMachine.h"
Rafael Espindola84b19be2006-07-16 01:02:57 +000016#include "llvm/CallingConv.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000017#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"
28#include <iostream>
Evan Cheng2ef88a02006-08-07 22:28:20 +000029#include <queue>
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000030#include <set>
31using namespace llvm;
32
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000033namespace {
34 class ARMTargetLowering : public TargetLowering {
35 public:
36 ARMTargetLowering(TargetMachine &TM);
37 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
Rafael Espindola84b19be2006-07-16 01:02:57 +000038 virtual const char *getTargetNodeName(unsigned Opcode) const;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000039 };
40
41}
42
43ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
44 : TargetLowering(TM) {
Rafael Espindola06c1e7e2006-08-01 12:58:43 +000045 setOperationAction(ISD::RET, MVT::Other, Custom);
46 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
47 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Rafael Espindola341b8642006-08-04 12:48:42 +000048
49 setSchedulingPreference(SchedulingForRegPressure);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000050}
51
Rafael Espindola84b19be2006-07-16 01:02:57 +000052namespace llvm {
53 namespace ARMISD {
54 enum NodeType {
55 // Start the numbering where the builting ops and target ops leave off.
56 FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
57 /// CALL - A direct function call.
Rafael Espindolaf4fda802006-08-03 17:02:20 +000058 CALL,
59
60 /// Return with a flag operand.
61 RET_FLAG
Rafael Espindola84b19be2006-07-16 01:02:57 +000062 };
63 }
64}
65
66const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
67 switch (Opcode) {
68 default: return 0;
69 case ARMISD::CALL: return "ARMISD::CALL";
Rafael Espindolaf4fda802006-08-03 17:02:20 +000070 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Rafael Espindola84b19be2006-07-16 01:02:57 +000071 }
72}
73
74// This transforms a ISD::CALL node into a
75// callseq_star <- ARMISD:CALL <- callseq_end
76// chain
Rafael Espindolac3c1a862006-05-25 11:00:18 +000077static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola84b19be2006-07-16 01:02:57 +000078 SDOperand Chain = Op.getOperand(0);
79 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
80 assert(CallConv == CallingConv::C && "unknown calling convention");
81 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +000082 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
83 assert(isTailCall == false && "tail call not supported");
84 SDOperand Callee = Op.getOperand(4);
85 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Rafael Espindola84b19be2006-07-16 01:02:57 +000086
87 // Count how many bytes are to be pushed on the stack. Initially
88 // only the link register.
89 unsigned NumBytes = 4;
90
Rafael Espindola1a009462006-08-08 13:02:29 +000091 // Add up all the space actually used.
92 for (unsigned i = 4; i < NumOps; ++i)
93 NumBytes += MVT::getSizeInBits(Op.getOperand(5+2*i).getValueType())/8;
Rafael Espindolafac00a92006-07-25 20:17:20 +000094
Rafael Espindola84b19be2006-07-16 01:02:57 +000095 // Adjust the stack pointer for the new arguments...
96 // These operations are automatically eliminated by the prolog/epilog pass
97 Chain = DAG.getCALLSEQ_START(Chain,
98 DAG.getConstant(NumBytes, MVT::i32));
99
Rafael Espindola1a009462006-08-08 13:02:29 +0000100 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
101
102 static const unsigned int num_regs = 4;
103 static const unsigned regs[num_regs] = {
Rafael Espindolafac00a92006-07-25 20:17:20 +0000104 ARM::R0, ARM::R1, ARM::R2, ARM::R3
105 };
106
107 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
Rafael Espindola1a009462006-08-08 13:02:29 +0000108 std::vector<SDOperand> MemOpChains;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000109
110 for (unsigned i = 0; i != NumOps; ++i) {
111 SDOperand Arg = Op.getOperand(5+2*i);
Rafael Espindola1a009462006-08-08 13:02:29 +0000112 assert(Arg.getValueType() == MVT::i32);
113 if (i < num_regs)
114 RegsToPass.push_back(std::make_pair(regs[i], Arg));
115 else {
116 unsigned ArgOffset = (i - num_regs) * 4;
117 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
118 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
119 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
120 Arg, PtrOff, DAG.getSrcValue(NULL)));
121 }
Rafael Espindolafac00a92006-07-25 20:17:20 +0000122 }
Rafael Espindola1a009462006-08-08 13:02:29 +0000123 if (!MemOpChains.empty())
124 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOpChains);
Rafael Espindolafac00a92006-07-25 20:17:20 +0000125
126 // Build a sequence of copy-to-reg nodes chained together with token chain
127 // and flag operands which copy the outgoing args into the appropriate regs.
128 SDOperand InFlag;
129 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
130 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
131 InFlag);
132 InFlag = Chain.getValue(1);
133 }
134
Rafael Espindola84b19be2006-07-16 01:02:57 +0000135 std::vector<MVT::ValueType> NodeTys;
136 NodeTys.push_back(MVT::Other); // Returns a chain
137 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
138
139 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
140 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
141 // node so that legalize doesn't hack it.
142 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
143 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
144
145 // If this is a direct call, pass the chain and the callee.
146 assert (Callee.Val);
147 std::vector<SDOperand> Ops;
148 Ops.push_back(Chain);
149 Ops.push_back(Callee);
150
151 unsigned CallOpc = ARMISD::CALL;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000152 if (InFlag.Val)
153 Ops.push_back(InFlag);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000154 Chain = DAG.getNode(CallOpc, NodeTys, Ops);
Rafael Espindolafac00a92006-07-25 20:17:20 +0000155 InFlag = Chain.getValue(1);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000156
Rafael Espindolafac00a92006-07-25 20:17:20 +0000157 std::vector<SDOperand> ResultVals;
158 NodeTys.clear();
159
160 // If the call has results, copy the values out of the ret val registers.
161 switch (Op.Val->getValueType(0)) {
162 default: assert(0 && "Unexpected ret value!");
163 case MVT::Other:
164 break;
165 case MVT::i32:
166 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
167 ResultVals.push_back(Chain.getValue(0));
168 NodeTys.push_back(MVT::i32);
169 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000170
171 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
172 DAG.getConstant(NumBytes, MVT::i32));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000173 NodeTys.push_back(MVT::Other);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000174
Rafael Espindolafac00a92006-07-25 20:17:20 +0000175 if (ResultVals.empty())
176 return Chain;
177
178 ResultVals.push_back(Chain);
179 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, ResultVals);
180 return Res.getValue(Op.ResNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000181}
182
183static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
184 SDOperand Copy;
Rafael Espindola4b023672006-06-05 22:26:14 +0000185 SDOperand Chain = Op.getOperand(0);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000186 switch(Op.getNumOperands()) {
187 default:
188 assert(0 && "Do not know how to return this many arguments!");
189 abort();
Rafael Espindola4b023672006-06-05 22:26:14 +0000190 case 1: {
191 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
Rafael Espindola6312da02006-08-03 22:50:11 +0000192 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
Rafael Espindola4b023672006-06-05 22:26:14 +0000193 }
Evan Cheng6848be12006-05-26 23:10:12 +0000194 case 3:
Rafael Espindola4b023672006-06-05 22:26:14 +0000195 Copy = DAG.getCopyToReg(Chain, ARM::R0, Op.getOperand(1), SDOperand());
196 if (DAG.getMachineFunction().liveout_empty())
197 DAG.getMachineFunction().addLiveOut(ARM::R0);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000198 break;
199 }
Rafael Espindola4b023672006-06-05 22:26:14 +0000200
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000201 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
202 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000203}
204
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000205static SDOperand LowerFORMAL_ARGUMENT(SDOperand Op, SelectionDAG &DAG,
206 unsigned ArgNo) {
Rafael Espindola4b442b52006-05-23 02:48:20 +0000207 MachineFunction &MF = DAG.getMachineFunction();
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000208 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
209 assert (ObjectVT == MVT::i32);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000210 SDOperand Root = Op.getOperand(0);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000211 SSARegMap *RegMap = MF.getSSARegMap();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000212
Rafael Espindola4b442b52006-05-23 02:48:20 +0000213 unsigned num_regs = 4;
Rafael Espindola4b442b52006-05-23 02:48:20 +0000214 static const unsigned REGS[] = {
215 ARM::R0, ARM::R1, ARM::R2, ARM::R3
216 };
217
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000218 if(ArgNo < num_regs) {
Rafael Espindola4b442b52006-05-23 02:48:20 +0000219 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000220 MF.addLiveIn(REGS[ArgNo], VReg);
221 return DAG.getCopyFromReg(Root, VReg, MVT::i32);
222 } else {
223 // If the argument is actually used, emit a load from the right stack
224 // slot.
225 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000226 unsigned ArgOffset = (ArgNo - num_regs) * 4;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000227
228 MachineFrameInfo *MFI = MF.getFrameInfo();
229 unsigned ObjSize = MVT::getSizeInBits(ObjectVT)/8;
230 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
231 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
232 return DAG.getLoad(ObjectVT, Root, FIN,
233 DAG.getSrcValue(NULL));
234 } else {
235 // Don't emit a dead load.
236 return DAG.getNode(ISD::UNDEF, ObjectVT);
237 }
238 }
239}
240
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000241static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
242 MVT::ValueType PtrVT = Op.getValueType();
243 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
244 Constant *C = CP->get();
245 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
246
247 return CPI;
248}
249
250static SDOperand LowerGlobalAddress(SDOperand Op,
251 SelectionDAG &DAG) {
252 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
253 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, 2);
254 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr,
255 DAG.getSrcValue(NULL));
256}
257
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000258static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
259 std::vector<SDOperand> ArgValues;
260 SDOperand Root = Op.getOperand(0);
261
262 for (unsigned ArgNo = 0, e = Op.Val->getNumValues()-1; ArgNo != e; ++ArgNo) {
263 SDOperand ArgVal = LowerFORMAL_ARGUMENT(Op, DAG, ArgNo);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000264
265 ArgValues.push_back(ArgVal);
266 }
267
268 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
269 assert(!isVarArg);
270
271 ArgValues.push_back(Root);
272
273 // Return the new list of results.
274 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
275 Op.Val->value_end());
276 return DAG.getNode(ISD::MERGE_VALUES, RetVT, ArgValues);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000277}
278
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000279SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
280 switch (Op.getOpcode()) {
281 default:
282 assert(0 && "Should not custom lower this!");
Rafael Espindola1c8f0532006-05-15 22:34:39 +0000283 abort();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000284 case ISD::ConstantPool:
285 return LowerConstantPool(Op, DAG);
286 case ISD::GlobalAddress:
287 return LowerGlobalAddress(Op, DAG);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000288 case ISD::FORMAL_ARGUMENTS:
289 return LowerFORMAL_ARGUMENTS(Op, DAG);
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000290 case ISD::CALL:
291 return LowerCALL(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000292 case ISD::RET:
293 return LowerRET(Op, DAG);
294 }
295}
296
297//===----------------------------------------------------------------------===//
298// Instruction Selector Implementation
299//===----------------------------------------------------------------------===//
300
301//===--------------------------------------------------------------------===//
302/// ARMDAGToDAGISel - ARM specific code to select ARM machine
303/// instructions for SelectionDAG operations.
304///
305namespace {
306class ARMDAGToDAGISel : public SelectionDAGISel {
307 ARMTargetLowering Lowering;
308
309public:
310 ARMDAGToDAGISel(TargetMachine &TM)
311 : SelectionDAGISel(Lowering), Lowering(TM) {
312 }
313
314 void Select(SDOperand &Result, SDOperand Op);
315 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000316 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000317
318 // Include the pieces autogenerated from the target description.
319#include "ARMGenDAGISel.inc"
320};
321
322void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
323 DEBUG(BB->dump());
324
325 DAG.setRoot(SelectRoot(DAG.getRoot()));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000326 DAG.RemoveDeadNodes();
327
328 ScheduleAndEmitDAG(DAG);
329}
330
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000331//register plus/minus 12 bit offset
332bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
333 SDOperand &Base) {
334 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000335 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
336 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
337 }
338 else
339 Base = N;
340 return true; //any address fits in a register
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000341}
342
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000343void ARMDAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000344 SDNode *N = Op.Val;
345
346 switch (N->getOpcode()) {
347 default:
348 SelectCode(Result, Op);
349 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000350 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000351}
352
353} // end anonymous namespace
354
355/// createARMISelDag - This pass converts a legalized DAG into a
356/// ARM-specific DAG, ready for instruction scheduling.
357///
358FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
359 return new ARMDAGToDAGISel(TM);
360}