blob: 34d972ffa76b6d6b37c0124b221e1692ac7b3054 [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>
29#include <set>
30using namespace llvm;
31
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000032namespace {
33 class ARMTargetLowering : public TargetLowering {
34 public:
35 ARMTargetLowering(TargetMachine &TM);
36 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
Rafael Espindola84b19be2006-07-16 01:02:57 +000037 virtual const char *getTargetNodeName(unsigned Opcode) const;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000038 };
39
40}
41
42ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
43 : TargetLowering(TM) {
Rafael Espindola06c1e7e2006-08-01 12:58:43 +000044 setOperationAction(ISD::RET, MVT::Other, Custom);
45 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
46 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Rafael Espindola341b8642006-08-04 12:48:42 +000047
48 setSchedulingPreference(SchedulingForRegPressure);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000049}
50
Rafael Espindola84b19be2006-07-16 01:02:57 +000051namespace llvm {
52 namespace ARMISD {
53 enum NodeType {
54 // Start the numbering where the builting ops and target ops leave off.
55 FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
56 /// CALL - A direct function call.
Rafael Espindolaf4fda802006-08-03 17:02:20 +000057 CALL,
58
59 /// Return with a flag operand.
60 RET_FLAG
Rafael Espindola84b19be2006-07-16 01:02:57 +000061 };
62 }
63}
64
65const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
66 switch (Opcode) {
67 default: return 0;
68 case ARMISD::CALL: return "ARMISD::CALL";
Rafael Espindolaf4fda802006-08-03 17:02:20 +000069 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Rafael Espindola84b19be2006-07-16 01:02:57 +000070 }
71}
72
73// This transforms a ISD::CALL node into a
74// callseq_star <- ARMISD:CALL <- callseq_end
75// chain
Rafael Espindolac3c1a862006-05-25 11:00:18 +000076static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola84b19be2006-07-16 01:02:57 +000077 SDOperand Chain = Op.getOperand(0);
78 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
79 assert(CallConv == CallingConv::C && "unknown calling convention");
80 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
81 assert(isVarArg == false && "VarArg not supported");
82 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 Espindolafac00a92006-07-25 20:17:20 +000091 assert(NumOps <= 4); //no args on the stack
92
Rafael Espindola84b19be2006-07-16 01:02:57 +000093 // Adjust the stack pointer for the new arguments...
94 // These operations are automatically eliminated by the prolog/epilog pass
95 Chain = DAG.getCALLSEQ_START(Chain,
96 DAG.getConstant(NumBytes, MVT::i32));
97
Rafael Espindolafac00a92006-07-25 20:17:20 +000098 static const unsigned regs[] = {
99 ARM::R0, ARM::R1, ARM::R2, ARM::R3
100 };
101
102 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
103
104 for (unsigned i = 0; i != NumOps; ++i) {
105 SDOperand Arg = Op.getOperand(5+2*i);
106 RegsToPass.push_back(std::make_pair(regs[i], Arg));
107 }
108
109 // Build a sequence of copy-to-reg nodes chained together with token chain
110 // and flag operands which copy the outgoing args into the appropriate regs.
111 SDOperand InFlag;
112 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
113 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
114 InFlag);
115 InFlag = Chain.getValue(1);
116 }
117
Rafael Espindola84b19be2006-07-16 01:02:57 +0000118 std::vector<MVT::ValueType> NodeTys;
119 NodeTys.push_back(MVT::Other); // Returns a chain
120 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
121
122 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
123 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
124 // node so that legalize doesn't hack it.
125 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
126 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
127
128 // If this is a direct call, pass the chain and the callee.
129 assert (Callee.Val);
130 std::vector<SDOperand> Ops;
131 Ops.push_back(Chain);
132 Ops.push_back(Callee);
133
134 unsigned CallOpc = ARMISD::CALL;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000135 if (InFlag.Val)
136 Ops.push_back(InFlag);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000137 Chain = DAG.getNode(CallOpc, NodeTys, Ops);
Rafael Espindolafac00a92006-07-25 20:17:20 +0000138 InFlag = Chain.getValue(1);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000139
Rafael Espindolafac00a92006-07-25 20:17:20 +0000140 std::vector<SDOperand> ResultVals;
141 NodeTys.clear();
142
143 // If the call has results, copy the values out of the ret val registers.
144 switch (Op.Val->getValueType(0)) {
145 default: assert(0 && "Unexpected ret value!");
146 case MVT::Other:
147 break;
148 case MVT::i32:
149 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
150 ResultVals.push_back(Chain.getValue(0));
151 NodeTys.push_back(MVT::i32);
152 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000153
154 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
155 DAG.getConstant(NumBytes, MVT::i32));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000156 NodeTys.push_back(MVT::Other);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000157
Rafael Espindolafac00a92006-07-25 20:17:20 +0000158 if (ResultVals.empty())
159 return Chain;
160
161 ResultVals.push_back(Chain);
162 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, ResultVals);
163 return Res.getValue(Op.ResNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000164}
165
166static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
167 SDOperand Copy;
Rafael Espindola4b023672006-06-05 22:26:14 +0000168 SDOperand Chain = Op.getOperand(0);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000169 switch(Op.getNumOperands()) {
170 default:
171 assert(0 && "Do not know how to return this many arguments!");
172 abort();
Rafael Espindola4b023672006-06-05 22:26:14 +0000173 case 1: {
174 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
Rafael Espindola6312da02006-08-03 22:50:11 +0000175 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
Rafael Espindola4b023672006-06-05 22:26:14 +0000176 }
Evan Cheng6848be12006-05-26 23:10:12 +0000177 case 3:
Rafael Espindola4b023672006-06-05 22:26:14 +0000178 Copy = DAG.getCopyToReg(Chain, ARM::R0, Op.getOperand(1), SDOperand());
179 if (DAG.getMachineFunction().liveout_empty())
180 DAG.getMachineFunction().addLiveOut(ARM::R0);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000181 break;
182 }
Rafael Espindola4b023672006-06-05 22:26:14 +0000183
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000184 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
185 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000186}
187
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000188static SDOperand LowerFORMAL_ARGUMENT(SDOperand Op, SelectionDAG &DAG,
189 unsigned ArgNo) {
Rafael Espindola4b442b52006-05-23 02:48:20 +0000190 MachineFunction &MF = DAG.getMachineFunction();
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000191 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
192 assert (ObjectVT == MVT::i32);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000193 SDOperand Root = Op.getOperand(0);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000194 SSARegMap *RegMap = MF.getSSARegMap();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000195
Rafael Espindola4b442b52006-05-23 02:48:20 +0000196 unsigned num_regs = 4;
Rafael Espindola4b442b52006-05-23 02:48:20 +0000197 static const unsigned REGS[] = {
198 ARM::R0, ARM::R1, ARM::R2, ARM::R3
199 };
200
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000201 if(ArgNo < num_regs) {
Rafael Espindola4b442b52006-05-23 02:48:20 +0000202 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000203 MF.addLiveIn(REGS[ArgNo], VReg);
204 return DAG.getCopyFromReg(Root, VReg, MVT::i32);
205 } else {
206 // If the argument is actually used, emit a load from the right stack
207 // slot.
208 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000209 unsigned ArgOffset = (ArgNo - num_regs) * 4;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000210
211 MachineFrameInfo *MFI = MF.getFrameInfo();
212 unsigned ObjSize = MVT::getSizeInBits(ObjectVT)/8;
213 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
214 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
215 return DAG.getLoad(ObjectVT, Root, FIN,
216 DAG.getSrcValue(NULL));
217 } else {
218 // Don't emit a dead load.
219 return DAG.getNode(ISD::UNDEF, ObjectVT);
220 }
221 }
222}
223
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000224static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
225 MVT::ValueType PtrVT = Op.getValueType();
226 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
227 Constant *C = CP->get();
228 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
229
230 return CPI;
231}
232
233static SDOperand LowerGlobalAddress(SDOperand Op,
234 SelectionDAG &DAG) {
235 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
236 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, 2);
237 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr,
238 DAG.getSrcValue(NULL));
239}
240
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000241static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
242 std::vector<SDOperand> ArgValues;
243 SDOperand Root = Op.getOperand(0);
244
245 for (unsigned ArgNo = 0, e = Op.Val->getNumValues()-1; ArgNo != e; ++ArgNo) {
246 SDOperand ArgVal = LowerFORMAL_ARGUMENT(Op, DAG, ArgNo);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000247
248 ArgValues.push_back(ArgVal);
249 }
250
251 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
252 assert(!isVarArg);
253
254 ArgValues.push_back(Root);
255
256 // Return the new list of results.
257 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
258 Op.Val->value_end());
259 return DAG.getNode(ISD::MERGE_VALUES, RetVT, ArgValues);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000260}
261
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000262SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
263 switch (Op.getOpcode()) {
264 default:
265 assert(0 && "Should not custom lower this!");
Rafael Espindola1c8f0532006-05-15 22:34:39 +0000266 abort();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000267 case ISD::ConstantPool:
268 return LowerConstantPool(Op, DAG);
269 case ISD::GlobalAddress:
270 return LowerGlobalAddress(Op, DAG);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000271 case ISD::FORMAL_ARGUMENTS:
272 return LowerFORMAL_ARGUMENTS(Op, DAG);
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000273 case ISD::CALL:
274 return LowerCALL(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000275 case ISD::RET:
276 return LowerRET(Op, DAG);
277 }
278}
279
280//===----------------------------------------------------------------------===//
281// Instruction Selector Implementation
282//===----------------------------------------------------------------------===//
283
284//===--------------------------------------------------------------------===//
285/// ARMDAGToDAGISel - ARM specific code to select ARM machine
286/// instructions for SelectionDAG operations.
287///
288namespace {
289class ARMDAGToDAGISel : public SelectionDAGISel {
290 ARMTargetLowering Lowering;
291
292public:
293 ARMDAGToDAGISel(TargetMachine &TM)
294 : SelectionDAGISel(Lowering), Lowering(TM) {
295 }
296
297 void Select(SDOperand &Result, SDOperand Op);
298 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000299 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000300
301 // Include the pieces autogenerated from the target description.
302#include "ARMGenDAGISel.inc"
303};
304
305void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
306 DEBUG(BB->dump());
307
308 DAG.setRoot(SelectRoot(DAG.getRoot()));
309 CodeGenMap.clear();
Evan Chengafe358e2006-05-24 20:46:25 +0000310 HandleMap.clear();
311 ReplaceMap.clear();
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000312 DAG.RemoveDeadNodes();
313
314 ScheduleAndEmitDAG(DAG);
315}
316
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000317//register plus/minus 12 bit offset
318bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
319 SDOperand &Base) {
320 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000321 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
322 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
323 }
324 else
325 Base = N;
326 return true; //any address fits in a register
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000327}
328
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000329void ARMDAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000330 SDNode *N = Op.Val;
331
332 switch (N->getOpcode()) {
333 default:
334 SelectCode(Result, Op);
335 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000336 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000337}
338
339} // end anonymous namespace
340
341/// createARMISelDag - This pass converts a legalized DAG into a
342/// ARM-specific DAG, ready for instruction scheduling.
343///
344FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
345 return new ARMDAGToDAGISel(TM);
346}