blob: 56d61165c3c3d4793ab3e8133423ad5a76b17b96 [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
Rafael Espindolaec46ea32006-08-16 14:43:33 +000087 // Count how many bytes are to be pushed on the stack.
88 unsigned NumBytes = 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +000089
Rafael Espindola1a009462006-08-08 13:02:29 +000090 // Add up all the space actually used.
91 for (unsigned i = 4; i < NumOps; ++i)
92 NumBytes += MVT::getSizeInBits(Op.getOperand(5+2*i).getValueType())/8;
Rafael Espindolafac00a92006-07-25 20:17:20 +000093
Rafael Espindola84b19be2006-07-16 01:02:57 +000094 // Adjust the stack pointer for the new arguments...
95 // These operations are automatically eliminated by the prolog/epilog pass
96 Chain = DAG.getCALLSEQ_START(Chain,
97 DAG.getConstant(NumBytes, MVT::i32));
98
Rafael Espindola1a009462006-08-08 13:02:29 +000099 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
100
101 static const unsigned int num_regs = 4;
102 static const unsigned regs[num_regs] = {
Rafael Espindolafac00a92006-07-25 20:17:20 +0000103 ARM::R0, ARM::R1, ARM::R2, ARM::R3
104 };
105
106 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
Rafael Espindola1a009462006-08-08 13:02:29 +0000107 std::vector<SDOperand> MemOpChains;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000108
109 for (unsigned i = 0; i != NumOps; ++i) {
110 SDOperand Arg = Op.getOperand(5+2*i);
Rafael Espindola1a009462006-08-08 13:02:29 +0000111 assert(Arg.getValueType() == MVT::i32);
112 if (i < num_regs)
113 RegsToPass.push_back(std::make_pair(regs[i], Arg));
114 else {
115 unsigned ArgOffset = (i - num_regs) * 4;
116 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
117 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
118 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
119 Arg, PtrOff, DAG.getSrcValue(NULL)));
120 }
Rafael Espindolafac00a92006-07-25 20:17:20 +0000121 }
Rafael Espindola1a009462006-08-08 13:02:29 +0000122 if (!MemOpChains.empty())
Chris Lattnere2199452006-08-11 17:38:39 +0000123 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
124 &MemOpChains[0], MemOpChains.size());
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
Rafael Espindola7a53bd02006-08-09 16:41:12 +0000151 // Add argument registers to the end of the list so that they are known live
152 // into the call.
153 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
154 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
155 RegsToPass[i].second.getValueType()));
156
Rafael Espindola84b19be2006-07-16 01:02:57 +0000157 unsigned CallOpc = ARMISD::CALL;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000158 if (InFlag.Val)
159 Ops.push_back(InFlag);
Chris Lattner87428672006-08-11 17:22:35 +0000160 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000161 InFlag = Chain.getValue(1);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000162
Rafael Espindolafac00a92006-07-25 20:17:20 +0000163 std::vector<SDOperand> ResultVals;
164 NodeTys.clear();
165
166 // If the call has results, copy the values out of the ret val registers.
167 switch (Op.Val->getValueType(0)) {
168 default: assert(0 && "Unexpected ret value!");
169 case MVT::Other:
170 break;
171 case MVT::i32:
172 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
173 ResultVals.push_back(Chain.getValue(0));
174 NodeTys.push_back(MVT::i32);
175 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000176
177 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
178 DAG.getConstant(NumBytes, MVT::i32));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000179 NodeTys.push_back(MVT::Other);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000180
Rafael Espindolafac00a92006-07-25 20:17:20 +0000181 if (ResultVals.empty())
182 return Chain;
183
184 ResultVals.push_back(Chain);
Chris Lattner87428672006-08-11 17:22:35 +0000185 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
186 ResultVals.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000187 return Res.getValue(Op.ResNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000188}
189
190static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
191 SDOperand Copy;
Rafael Espindola4b023672006-06-05 22:26:14 +0000192 SDOperand Chain = Op.getOperand(0);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000193 switch(Op.getNumOperands()) {
194 default:
195 assert(0 && "Do not know how to return this many arguments!");
196 abort();
Rafael Espindola4b023672006-06-05 22:26:14 +0000197 case 1: {
198 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
Rafael Espindola6312da02006-08-03 22:50:11 +0000199 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
Rafael Espindola4b023672006-06-05 22:26:14 +0000200 }
Evan Cheng6848be12006-05-26 23:10:12 +0000201 case 3:
Rafael Espindola4b023672006-06-05 22:26:14 +0000202 Copy = DAG.getCopyToReg(Chain, ARM::R0, Op.getOperand(1), SDOperand());
203 if (DAG.getMachineFunction().liveout_empty())
204 DAG.getMachineFunction().addLiveOut(ARM::R0);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000205 break;
206 }
Rafael Espindola4b023672006-06-05 22:26:14 +0000207
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000208 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
209 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000210}
211
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000212static SDOperand LowerFORMAL_ARGUMENT(SDOperand Op, SelectionDAG &DAG,
213 unsigned ArgNo) {
Rafael Espindola4b442b52006-05-23 02:48:20 +0000214 MachineFunction &MF = DAG.getMachineFunction();
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000215 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
216 assert (ObjectVT == MVT::i32);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000217 SDOperand Root = Op.getOperand(0);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000218 SSARegMap *RegMap = MF.getSSARegMap();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000219
Rafael Espindola4b442b52006-05-23 02:48:20 +0000220 unsigned num_regs = 4;
Rafael Espindola4b442b52006-05-23 02:48:20 +0000221 static const unsigned REGS[] = {
222 ARM::R0, ARM::R1, ARM::R2, ARM::R3
223 };
224
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000225 if(ArgNo < num_regs) {
Rafael Espindola4b442b52006-05-23 02:48:20 +0000226 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000227 MF.addLiveIn(REGS[ArgNo], VReg);
228 return DAG.getCopyFromReg(Root, VReg, MVT::i32);
229 } else {
230 // If the argument is actually used, emit a load from the right stack
231 // slot.
232 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000233 unsigned ArgOffset = (ArgNo - num_regs) * 4;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000234
235 MachineFrameInfo *MFI = MF.getFrameInfo();
236 unsigned ObjSize = MVT::getSizeInBits(ObjectVT)/8;
237 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
238 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
239 return DAG.getLoad(ObjectVT, Root, FIN,
240 DAG.getSrcValue(NULL));
241 } else {
242 // Don't emit a dead load.
243 return DAG.getNode(ISD::UNDEF, ObjectVT);
244 }
245 }
246}
247
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000248static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
249 MVT::ValueType PtrVT = Op.getValueType();
250 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
251 Constant *C = CP->get();
252 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
253
254 return CPI;
255}
256
257static SDOperand LowerGlobalAddress(SDOperand Op,
258 SelectionDAG &DAG) {
259 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Rafael Espindola61369da2006-08-14 19:01:24 +0000260 int alignment = 2;
261 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000262 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr,
263 DAG.getSrcValue(NULL));
264}
265
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000266static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
267 std::vector<SDOperand> ArgValues;
268 SDOperand Root = Op.getOperand(0);
269
270 for (unsigned ArgNo = 0, e = Op.Val->getNumValues()-1; ArgNo != e; ++ArgNo) {
271 SDOperand ArgVal = LowerFORMAL_ARGUMENT(Op, DAG, ArgNo);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000272
273 ArgValues.push_back(ArgVal);
274 }
275
276 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
277 assert(!isVarArg);
278
279 ArgValues.push_back(Root);
280
281 // Return the new list of results.
282 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
283 Op.Val->value_end());
Chris Lattner87428672006-08-11 17:22:35 +0000284 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
Rafael Espindoladc124a22006-05-18 21:45:49 +0000285}
286
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000287SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
288 switch (Op.getOpcode()) {
289 default:
290 assert(0 && "Should not custom lower this!");
Rafael Espindola1c8f0532006-05-15 22:34:39 +0000291 abort();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000292 case ISD::ConstantPool:
293 return LowerConstantPool(Op, DAG);
294 case ISD::GlobalAddress:
295 return LowerGlobalAddress(Op, DAG);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000296 case ISD::FORMAL_ARGUMENTS:
297 return LowerFORMAL_ARGUMENTS(Op, DAG);
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000298 case ISD::CALL:
299 return LowerCALL(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000300 case ISD::RET:
301 return LowerRET(Op, DAG);
302 }
303}
304
305//===----------------------------------------------------------------------===//
306// Instruction Selector Implementation
307//===----------------------------------------------------------------------===//
308
309//===--------------------------------------------------------------------===//
310/// ARMDAGToDAGISel - ARM specific code to select ARM machine
311/// instructions for SelectionDAG operations.
312///
313namespace {
314class ARMDAGToDAGISel : public SelectionDAGISel {
315 ARMTargetLowering Lowering;
316
317public:
318 ARMDAGToDAGISel(TargetMachine &TM)
319 : SelectionDAGISel(Lowering), Lowering(TM) {
320 }
321
Evan Cheng64a752f2006-08-11 09:08:15 +0000322 SDNode *Select(SDOperand &Result, SDOperand Op);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000323 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000324 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000325
326 // Include the pieces autogenerated from the target description.
327#include "ARMGenDAGISel.inc"
328};
329
330void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
331 DEBUG(BB->dump());
332
333 DAG.setRoot(SelectRoot(DAG.getRoot()));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000334 DAG.RemoveDeadNodes();
335
336 ScheduleAndEmitDAG(DAG);
337}
338
Rafael Espindola61369da2006-08-14 19:01:24 +0000339static bool isInt12Immediate(SDNode *N, short &Imm) {
340 if (N->getOpcode() != ISD::Constant)
341 return false;
342
343 int32_t t = cast<ConstantSDNode>(N)->getValue();
344 int max = 2<<12 - 1;
345 int min = -max;
346 if (t > min && t < max) {
347 Imm = t;
348 return true;
349 }
350 else
351 return false;
352}
353
354static bool isInt12Immediate(SDOperand Op, short &Imm) {
355 return isInt12Immediate(Op.Val, Imm);
356}
357
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000358//register plus/minus 12 bit offset
359bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
360 SDOperand &Base) {
Rafael Espindolaf3a335c2006-08-17 17:09:40 +0000361 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
362 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
363 Offset = CurDAG->getTargetConstant(0, MVT::i32);
364 return true;
365 }
Rafael Espindola61369da2006-08-14 19:01:24 +0000366 if (N.getOpcode() == ISD::ADD) {
367 short imm = 0;
368 if (isInt12Immediate(N.getOperand(1), imm)) {
369 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
370 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
371 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
372 } else {
373 Base = N.getOperand(0);
374 }
375 return true; // [r+i]
376 }
377 }
378
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000379 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000380 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
381 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
382 }
383 else
384 Base = N;
385 return true; //any address fits in a register
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000386}
387
Evan Cheng64a752f2006-08-11 09:08:15 +0000388SDNode *ARMDAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000389 SDNode *N = Op.Val;
390
391 switch (N->getOpcode()) {
392 default:
Evan Cheng64a752f2006-08-11 09:08:15 +0000393 return SelectCode(Result, Op);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000394 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000395 }
Evan Cheng64a752f2006-08-11 09:08:15 +0000396 return NULL;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000397}
398
399} // end anonymous namespace
400
401/// createARMISelDag - This pass converts a legalized DAG into a
402/// ARM-specific DAG, ready for instruction scheduling.
403///
404FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
405 return new ARMDAGToDAGISel(TM);
406}