blob: 915684ad17e717e6ad8e208565807c9f62a17644 [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;
82 assert(isVarArg == false && "VarArg not supported");
83 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
84 assert(isTailCall == false && "tail call not supported");
85 SDOperand Callee = Op.getOperand(4);
86 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Rafael Espindola84b19be2006-07-16 01:02:57 +000087
88 // Count how many bytes are to be pushed on the stack. Initially
89 // only the link register.
90 unsigned NumBytes = 4;
91
Rafael Espindolafac00a92006-07-25 20:17:20 +000092 assert(NumOps <= 4); //no args on the stack
93
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 Espindolafac00a92006-07-25 20:17:20 +000099 static const unsigned regs[] = {
100 ARM::R0, ARM::R1, ARM::R2, ARM::R3
101 };
102
103 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
104
105 for (unsigned i = 0; i != NumOps; ++i) {
106 SDOperand Arg = Op.getOperand(5+2*i);
107 RegsToPass.push_back(std::make_pair(regs[i], Arg));
108 }
109
110 // Build a sequence of copy-to-reg nodes chained together with token chain
111 // and flag operands which copy the outgoing args into the appropriate regs.
112 SDOperand InFlag;
113 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
114 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
115 InFlag);
116 InFlag = Chain.getValue(1);
117 }
118
Rafael Espindola84b19be2006-07-16 01:02:57 +0000119 std::vector<MVT::ValueType> NodeTys;
120 NodeTys.push_back(MVT::Other); // Returns a chain
121 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
122
123 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
124 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
125 // node so that legalize doesn't hack it.
126 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
127 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
128
129 // If this is a direct call, pass the chain and the callee.
130 assert (Callee.Val);
131 std::vector<SDOperand> Ops;
132 Ops.push_back(Chain);
133 Ops.push_back(Callee);
134
135 unsigned CallOpc = ARMISD::CALL;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000136 if (InFlag.Val)
137 Ops.push_back(InFlag);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000138 Chain = DAG.getNode(CallOpc, NodeTys, Ops);
Rafael Espindolafac00a92006-07-25 20:17:20 +0000139 InFlag = Chain.getValue(1);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000140
Rafael Espindolafac00a92006-07-25 20:17:20 +0000141 std::vector<SDOperand> ResultVals;
142 NodeTys.clear();
143
144 // If the call has results, copy the values out of the ret val registers.
145 switch (Op.Val->getValueType(0)) {
146 default: assert(0 && "Unexpected ret value!");
147 case MVT::Other:
148 break;
149 case MVT::i32:
150 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
151 ResultVals.push_back(Chain.getValue(0));
152 NodeTys.push_back(MVT::i32);
153 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000154
155 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
156 DAG.getConstant(NumBytes, MVT::i32));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000157 NodeTys.push_back(MVT::Other);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000158
Rafael Espindolafac00a92006-07-25 20:17:20 +0000159 if (ResultVals.empty())
160 return Chain;
161
162 ResultVals.push_back(Chain);
163 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, ResultVals);
164 return Res.getValue(Op.ResNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000165}
166
167static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
168 SDOperand Copy;
Rafael Espindola4b023672006-06-05 22:26:14 +0000169 SDOperand Chain = Op.getOperand(0);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000170 switch(Op.getNumOperands()) {
171 default:
172 assert(0 && "Do not know how to return this many arguments!");
173 abort();
Rafael Espindola4b023672006-06-05 22:26:14 +0000174 case 1: {
175 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
Rafael Espindola6312da02006-08-03 22:50:11 +0000176 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
Rafael Espindola4b023672006-06-05 22:26:14 +0000177 }
Evan Cheng6848be12006-05-26 23:10:12 +0000178 case 3:
Rafael Espindola4b023672006-06-05 22:26:14 +0000179 Copy = DAG.getCopyToReg(Chain, ARM::R0, Op.getOperand(1), SDOperand());
180 if (DAG.getMachineFunction().liveout_empty())
181 DAG.getMachineFunction().addLiveOut(ARM::R0);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000182 break;
183 }
Rafael Espindola4b023672006-06-05 22:26:14 +0000184
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000185 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
186 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000187}
188
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000189static SDOperand LowerFORMAL_ARGUMENT(SDOperand Op, SelectionDAG &DAG,
190 unsigned ArgNo) {
Rafael Espindola4b442b52006-05-23 02:48:20 +0000191 MachineFunction &MF = DAG.getMachineFunction();
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000192 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
193 assert (ObjectVT == MVT::i32);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000194 SDOperand Root = Op.getOperand(0);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000195 SSARegMap *RegMap = MF.getSSARegMap();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000196
Rafael Espindola4b442b52006-05-23 02:48:20 +0000197 unsigned num_regs = 4;
Rafael Espindola4b442b52006-05-23 02:48:20 +0000198 static const unsigned REGS[] = {
199 ARM::R0, ARM::R1, ARM::R2, ARM::R3
200 };
201
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000202 if(ArgNo < num_regs) {
Rafael Espindola4b442b52006-05-23 02:48:20 +0000203 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000204 MF.addLiveIn(REGS[ArgNo], VReg);
205 return DAG.getCopyFromReg(Root, VReg, MVT::i32);
206 } else {
207 // If the argument is actually used, emit a load from the right stack
208 // slot.
209 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000210 unsigned ArgOffset = (ArgNo - num_regs) * 4;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000211
212 MachineFrameInfo *MFI = MF.getFrameInfo();
213 unsigned ObjSize = MVT::getSizeInBits(ObjectVT)/8;
214 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
215 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
216 return DAG.getLoad(ObjectVT, Root, FIN,
217 DAG.getSrcValue(NULL));
218 } else {
219 // Don't emit a dead load.
220 return DAG.getNode(ISD::UNDEF, ObjectVT);
221 }
222 }
223}
224
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000225static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
226 MVT::ValueType PtrVT = Op.getValueType();
227 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
228 Constant *C = CP->get();
229 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
230
231 return CPI;
232}
233
234static SDOperand LowerGlobalAddress(SDOperand Op,
235 SelectionDAG &DAG) {
236 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
237 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, 2);
238 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr,
239 DAG.getSrcValue(NULL));
240}
241
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000242static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
243 std::vector<SDOperand> ArgValues;
244 SDOperand Root = Op.getOperand(0);
245
246 for (unsigned ArgNo = 0, e = Op.Val->getNumValues()-1; ArgNo != e; ++ArgNo) {
247 SDOperand ArgVal = LowerFORMAL_ARGUMENT(Op, DAG, ArgNo);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000248
249 ArgValues.push_back(ArgVal);
250 }
251
252 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
253 assert(!isVarArg);
254
255 ArgValues.push_back(Root);
256
257 // Return the new list of results.
258 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
259 Op.Val->value_end());
260 return DAG.getNode(ISD::MERGE_VALUES, RetVT, ArgValues);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000261}
262
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000263SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
264 switch (Op.getOpcode()) {
265 default:
266 assert(0 && "Should not custom lower this!");
Rafael Espindola1c8f0532006-05-15 22:34:39 +0000267 abort();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000268 case ISD::ConstantPool:
269 return LowerConstantPool(Op, DAG);
270 case ISD::GlobalAddress:
271 return LowerGlobalAddress(Op, DAG);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000272 case ISD::FORMAL_ARGUMENTS:
273 return LowerFORMAL_ARGUMENTS(Op, DAG);
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000274 case ISD::CALL:
275 return LowerCALL(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000276 case ISD::RET:
277 return LowerRET(Op, DAG);
278 }
279}
280
281//===----------------------------------------------------------------------===//
282// Instruction Selector Implementation
283//===----------------------------------------------------------------------===//
284
285//===--------------------------------------------------------------------===//
286/// ARMDAGToDAGISel - ARM specific code to select ARM machine
287/// instructions for SelectionDAG operations.
288///
289namespace {
290class ARMDAGToDAGISel : public SelectionDAGISel {
291 ARMTargetLowering Lowering;
292
293public:
294 ARMDAGToDAGISel(TargetMachine &TM)
295 : SelectionDAGISel(Lowering), Lowering(TM) {
296 }
297
298 void Select(SDOperand &Result, SDOperand Op);
299 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000300 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000301
302 // Include the pieces autogenerated from the target description.
303#include "ARMGenDAGISel.inc"
304};
305
306void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
307 DEBUG(BB->dump());
308
309 DAG.setRoot(SelectRoot(DAG.getRoot()));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000310 DAG.RemoveDeadNodes();
311
312 ScheduleAndEmitDAG(DAG);
313}
314
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000315//register plus/minus 12 bit offset
316bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
317 SDOperand &Base) {
318 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000319 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
320 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
321 }
322 else
323 Base = N;
324 return true; //any address fits in a register
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000325}
326
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000327void ARMDAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000328 SDNode *N = Op.Val;
329
330 switch (N->getOpcode()) {
331 default:
332 SelectCode(Result, Op);
333 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000334 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000335}
336
337} // end anonymous namespace
338
339/// createARMISelDag - This pass converts a legalized DAG into a
340/// ARM-specific DAG, ready for instruction scheduling.
341///
342FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
343 return new ARMDAGToDAGISel(TM);
344}