blob: 3f3b949464c8b5cd4c38913e0501e55b588f0a95 [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"
Rafael Espindola7246d332006-09-21 11:29:52 +000019#include "llvm/Constants.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000020#include "llvm/Intrinsics.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/SelectionDAG.h"
25#include "llvm/CodeGen/SelectionDAGISel.h"
26#include "llvm/CodeGen/SSARegMap.h"
27#include "llvm/Target/TargetLowering.h"
28#include "llvm/Support/Debug.h"
29#include <iostream>
Rafael Espindolaa2845842006-10-05 16:48:49 +000030#include <vector>
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000031using namespace llvm;
32
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000033namespace {
34 class ARMTargetLowering : public TargetLowering {
Rafael Espindola755be9b2006-08-25 17:55:16 +000035 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000036 public:
37 ARMTargetLowering(TargetMachine &TM);
38 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
Rafael Espindola84b19be2006-07-16 01:02:57 +000039 virtual const char *getTargetNodeName(unsigned Opcode) const;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000040 };
41
42}
43
44ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
45 : TargetLowering(TM) {
Rafael Espindola3717ca92006-08-20 01:49:49 +000046 addRegisterClass(MVT::i32, ARM::IntRegsRegisterClass);
Rafael Espindola27185192006-09-29 21:20:16 +000047 addRegisterClass(MVT::f32, ARM::FPRegsRegisterClass);
48 addRegisterClass(MVT::f64, ARM::DFPRegsRegisterClass);
Rafael Espindola3717ca92006-08-20 01:49:49 +000049
Rafael Espindola27185192006-09-29 21:20:16 +000050 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Rafael Espindola3717ca92006-08-20 01:49:49 +000051
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +000052 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
53
Rafael Espindola06c1e7e2006-08-01 12:58:43 +000054 setOperationAction(ISD::RET, MVT::Other, Custom);
55 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
56 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Rafael Espindola341b8642006-08-04 12:48:42 +000057
Rafael Espindola3c000bf2006-08-21 22:00:32 +000058 setOperationAction(ISD::SETCC, MVT::i32, Expand);
59 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
Rafael Espindola687bc492006-08-24 13:45:55 +000060 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
Rafael Espindola3c000bf2006-08-21 22:00:32 +000061
Rafael Espindola755be9b2006-08-25 17:55:16 +000062 setOperationAction(ISD::VASTART, MVT::Other, Custom);
63 setOperationAction(ISD::VAEND, MVT::Other, Expand);
64
Rafael Espindolacd71da52006-10-03 17:27:58 +000065 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
66 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
67
Rafael Espindola341b8642006-08-04 12:48:42 +000068 setSchedulingPreference(SchedulingForRegPressure);
Rafael Espindola3717ca92006-08-20 01:49:49 +000069 computeRegisterProperties();
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000070}
71
Rafael Espindola84b19be2006-07-16 01:02:57 +000072namespace llvm {
73 namespace ARMISD {
74 enum NodeType {
75 // Start the numbering where the builting ops and target ops leave off.
76 FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
77 /// CALL - A direct function call.
Rafael Espindolaf4fda802006-08-03 17:02:20 +000078 CALL,
79
80 /// Return with a flag operand.
Rafael Espindola3c000bf2006-08-21 22:00:32 +000081 RET_FLAG,
82
83 CMP,
84
Rafael Espindola687bc492006-08-24 13:45:55 +000085 SELECT,
86
Rafael Espindola27185192006-09-29 21:20:16 +000087 BR,
88
Rafael Espindola9e071f02006-10-02 19:30:56 +000089 FSITOS,
90
91 FSITOD,
92
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +000093 FUITOS,
94
95 FUITOD,
96
Rafael Espindolaa2845842006-10-05 16:48:49 +000097 FMRRD,
98
99 FMDRR
Rafael Espindola84b19be2006-07-16 01:02:57 +0000100 };
101 }
102}
103
Rafael Espindola6f602de2006-08-24 16:13:15 +0000104/// DAGCCToARMCC - Convert a DAG integer condition code to an ARM CC
105static ARMCC::CondCodes DAGCCToARMCC(ISD::CondCode CC) {
106 switch (CC) {
Rafael Espindolaebdabda2006-09-21 13:06:26 +0000107 default:
108 std::cerr << "CC = " << CC << "\n";
109 assert(0 && "Unknown condition code!");
110 case ISD::SETUGT: return ARMCC::HI;
111 case ISD::SETULE: return ARMCC::LS;
112 case ISD::SETLE: return ARMCC::LE;
113 case ISD::SETLT: return ARMCC::LT;
114 case ISD::SETGT: return ARMCC::GT;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000115 case ISD::SETNE: return ARMCC::NE;
Rafael Espindolacdda88c2006-08-24 17:19:08 +0000116 case ISD::SETEQ: return ARMCC::EQ;
Rafael Espindola5f450d22006-09-02 20:24:25 +0000117 case ISD::SETGE: return ARMCC::GE;
118 case ISD::SETUGE: return ARMCC::CS;
Rafael Espindolabc4cec92006-09-03 13:19:16 +0000119 case ISD::SETULT: return ARMCC::CC;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000120 }
121}
122
Rafael Espindola84b19be2006-07-16 01:02:57 +0000123const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
124 switch (Opcode) {
125 default: return 0;
126 case ARMISD::CALL: return "ARMISD::CALL";
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000127 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000128 case ARMISD::SELECT: return "ARMISD::SELECT";
129 case ARMISD::CMP: return "ARMISD::CMP";
Rafael Espindola687bc492006-08-24 13:45:55 +0000130 case ARMISD::BR: return "ARMISD::BR";
Rafael Espindola27185192006-09-29 21:20:16 +0000131 case ARMISD::FSITOS: return "ARMISD::FSITOS";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000132 case ARMISD::FSITOD: return "ARMISD::FSITOD";
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000133 case ARMISD::FUITOS: return "ARMISD::FUITOS";
134 case ARMISD::FUITOD: return "ARMISD::FUITOD";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000135 case ARMISD::FMRRD: return "ARMISD::FMRRD";
Rafael Espindolaa2845842006-10-05 16:48:49 +0000136 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Rafael Espindola84b19be2006-07-16 01:02:57 +0000137 }
138}
139
Rafael Espindolaa2845842006-10-05 16:48:49 +0000140class ArgumentLayout {
141 std::vector<bool> is_reg;
142 std::vector<unsigned> pos;
143 std::vector<MVT::ValueType> types;
144public:
Rafael Espindola39b5a212006-10-05 17:46:48 +0000145 ArgumentLayout(const std::vector<MVT::ValueType> &Types) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000146 types = Types;
147
148 unsigned RegNum = 0;
149 unsigned StackOffset = 0;
Rafael Espindola39b5a212006-10-05 17:46:48 +0000150 for(std::vector<MVT::ValueType>::const_iterator I = Types.begin();
Rafael Espindolaa2845842006-10-05 16:48:49 +0000151 I != Types.end();
152 ++I) {
153 MVT::ValueType VT = *I;
154 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
155 unsigned size = MVT::getSizeInBits(VT)/32;
156
157 RegNum = ((RegNum + size - 1) / size) * size;
158 if (RegNum < 4) {
159 pos.push_back(RegNum);
160 is_reg.push_back(true);
161 RegNum += size;
162 } else {
163 unsigned bytes = size * 32/8;
164 StackOffset = ((StackOffset + bytes - 1) / bytes) * bytes;
165 pos.push_back(StackOffset);
166 is_reg.push_back(false);
167 StackOffset += bytes;
168 }
169 }
170 }
171 unsigned getRegisterNum(unsigned argNum) {
172 assert(isRegister(argNum));
173 return pos[argNum];
174 }
175 unsigned getOffset(unsigned argNum) {
176 assert(isOffset(argNum));
177 return pos[argNum];
178 }
179 unsigned isRegister(unsigned argNum) {
180 assert(argNum < is_reg.size());
181 return is_reg[argNum];
182 }
183 unsigned isOffset(unsigned argNum) {
184 return !isRegister(argNum);
185 }
186 MVT::ValueType getType(unsigned argNum) {
187 assert(argNum < types.size());
188 return types[argNum];
189 }
190 unsigned getStackSize(void) {
191 int last = is_reg.size() - 1;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000192 if (last < 0)
193 return 0;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000194 if (isRegister(last))
195 return 0;
196 return getOffset(last) + MVT::getSizeInBits(getType(last))/8;
197 }
198 int lastRegArg(void) {
199 int size = is_reg.size();
200 int last = 0;
201 while(last < size && isRegister(last))
202 last++;
203 last--;
204 return last;
205 }
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000206 int lastRegNum(void) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000207 int l = lastRegArg();
208 if (l < 0)
209 return -1;
210 unsigned r = getRegisterNum(l);
211 MVT::ValueType t = getType(l);
212 assert(t == MVT::i32 || t == MVT::f32 || t == MVT::f64);
213 if (t == MVT::f64)
214 return r + 1;
215 return r;
216 }
217};
218
Rafael Espindola84b19be2006-07-16 01:02:57 +0000219// This transforms a ISD::CALL node into a
220// callseq_star <- ARMISD:CALL <- callseq_end
221// chain
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000222static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola84b19be2006-07-16 01:02:57 +0000223 SDOperand Chain = Op.getOperand(0);
224 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
225 assert(CallConv == CallingConv::C && "unknown calling convention");
226 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000227 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
228 assert(isTailCall == false && "tail call not supported");
229 SDOperand Callee = Op.getOperand(4);
230 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Rafael Espindola1a009462006-08-08 13:02:29 +0000231 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000232 static const unsigned regs[] = {
Rafael Espindolafac00a92006-07-25 20:17:20 +0000233 ARM::R0, ARM::R1, ARM::R2, ARM::R3
234 };
235
Rafael Espindolaa2845842006-10-05 16:48:49 +0000236 std::vector<MVT::ValueType> Types;
237 for (unsigned i = 0; i < NumOps; ++i) {
238 MVT::ValueType VT = Op.getOperand(5+2*i).getValueType();
239 Types.push_back(VT);
240 }
241 ArgumentLayout Layout(Types);
Rafael Espindolafac00a92006-07-25 20:17:20 +0000242
Rafael Espindolaa2845842006-10-05 16:48:49 +0000243 unsigned NumBytes = Layout.getStackSize();
244
245 Chain = DAG.getCALLSEQ_START(Chain,
246 DAG.getConstant(NumBytes, MVT::i32));
247
248 //Build a sequence of stores
249 std::vector<SDOperand> MemOpChains;
250 for (unsigned i = Layout.lastRegArg() + 1; i < NumOps; ++i) {
251 SDOperand Arg = Op.getOperand(5+2*i);
252 unsigned ArgOffset = Layout.getOffset(i);
253 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
254 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Evan Cheng786225a2006-10-05 23:01:46 +0000255 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff,
256 DAG.getSrcValue(NULL)));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000257 }
Rafael Espindola1a009462006-08-08 13:02:29 +0000258 if (!MemOpChains.empty())
Chris Lattnere2199452006-08-11 17:38:39 +0000259 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
260 &MemOpChains[0], MemOpChains.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000261
Rafael Espindola84b19be2006-07-16 01:02:57 +0000262 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
263 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
264 // node so that legalize doesn't hack it.
265 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
266 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
267
268 // If this is a direct call, pass the chain and the callee.
269 assert (Callee.Val);
270 std::vector<SDOperand> Ops;
271 Ops.push_back(Chain);
272 Ops.push_back(Callee);
273
Rafael Espindolaa2845842006-10-05 16:48:49 +0000274 // Build a sequence of copy-to-reg nodes chained together with token chain
275 // and flag operands which copy the outgoing args into the appropriate regs.
276 SDOperand InFlag;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000277 for (int i = 0, e = Layout.lastRegArg(); i <= e; ++i) {
Rafael Espindola4a408d42006-10-06 12:50:22 +0000278 SDOperand Arg = Op.getOperand(5+2*i);
279 unsigned RegNum = Layout.getRegisterNum(i);
280 unsigned Reg1 = regs[RegNum];
281 MVT::ValueType VT = Layout.getType(i);
282 assert(VT == Arg.getValueType());
283 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000284
285 // Add argument register to the end of the list so that it is known live
286 // into the call.
Rafael Espindola4a408d42006-10-06 12:50:22 +0000287 Ops.push_back(DAG.getRegister(Reg1, MVT::i32));
288 if (VT == MVT::f64) {
289 unsigned Reg2 = regs[RegNum + 1];
290 SDOperand SDReg1 = DAG.getRegister(Reg1, MVT::i32);
291 SDOperand SDReg2 = DAG.getRegister(Reg2, MVT::i32);
292
293 Ops.push_back(DAG.getRegister(Reg2, MVT::i32));
294 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola935b1f82006-10-06 20:33:26 +0000295 SDOperand Ops[] = {Chain, SDReg1, SDReg2, Arg, InFlag};
296 Chain = DAG.getNode(ARMISD::FMRRD, VTs, Ops, InFlag.Val ? 5 : 4);
Rafael Espindola4a408d42006-10-06 12:50:22 +0000297 } else {
298 if (VT == MVT::f32)
299 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg);
300 Chain = DAG.getCopyToReg(Chain, Reg1, Arg, InFlag);
301 }
302 InFlag = Chain.getValue(1);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000303 }
304
305 std::vector<MVT::ValueType> NodeTys;
306 NodeTys.push_back(MVT::Other); // Returns a chain
307 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Rafael Espindola7a53bd02006-08-09 16:41:12 +0000308
Rafael Espindola84b19be2006-07-16 01:02:57 +0000309 unsigned CallOpc = ARMISD::CALL;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000310 if (InFlag.Val)
311 Ops.push_back(InFlag);
Chris Lattner87428672006-08-11 17:22:35 +0000312 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000313 InFlag = Chain.getValue(1);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000314
Rafael Espindolafac00a92006-07-25 20:17:20 +0000315 std::vector<SDOperand> ResultVals;
316 NodeTys.clear();
317
318 // If the call has results, copy the values out of the ret val registers.
Rafael Espindola614057b2006-10-06 19:10:05 +0000319 MVT::ValueType VT = Op.Val->getValueType(0);
320 if (VT != MVT::Other) {
321 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
322 SDOperand Value;
323
324 SDOperand Value1 = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
325 Chain = Value1.getValue(1);
326 InFlag = Value1.getValue(2);
327 if (VT == MVT::i32)
328 Value = Value1;
329 if (VT == MVT::f32)
330 Value = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Value1);
331 if (VT == MVT::f64) {
332 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
333 Chain = Value2.getValue(1);
334 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
335 }
336 ResultVals.push_back(Value);
337 NodeTys.push_back(VT);
Rafael Espindolafac00a92006-07-25 20:17:20 +0000338 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000339
340 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
341 DAG.getConstant(NumBytes, MVT::i32));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000342 NodeTys.push_back(MVT::Other);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000343
Rafael Espindolafac00a92006-07-25 20:17:20 +0000344 if (ResultVals.empty())
345 return Chain;
346
347 ResultVals.push_back(Chain);
Chris Lattner87428672006-08-11 17:22:35 +0000348 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
349 ResultVals.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000350 return Res.getValue(Op.ResNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000351}
352
353static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
354 SDOperand Copy;
Rafael Espindola4b023672006-06-05 22:26:14 +0000355 SDOperand Chain = Op.getOperand(0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000356 SDOperand R0 = DAG.getRegister(ARM::R0, MVT::i32);
357 SDOperand R1 = DAG.getRegister(ARM::R1, MVT::i32);
358
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000359 switch(Op.getNumOperands()) {
360 default:
361 assert(0 && "Do not know how to return this many arguments!");
362 abort();
Rafael Espindola4b023672006-06-05 22:26:14 +0000363 case 1: {
364 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
Rafael Espindola6312da02006-08-03 22:50:11 +0000365 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
Rafael Espindola4b023672006-06-05 22:26:14 +0000366 }
Rafael Espindola27185192006-09-29 21:20:16 +0000367 case 3: {
368 SDOperand Val = Op.getOperand(1);
369 assert(Val.getValueType() == MVT::i32 ||
Rafael Espindola9e071f02006-10-02 19:30:56 +0000370 Val.getValueType() == MVT::f32 ||
371 Val.getValueType() == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000372
Rafael Espindola9e071f02006-10-02 19:30:56 +0000373 if (Val.getValueType() == MVT::f64) {
374 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
375 SDOperand Ops[] = {Chain, R0, R1, Val};
376 Copy = DAG.getNode(ARMISD::FMRRD, VTs, Ops, 4);
377 } else {
378 if (Val.getValueType() == MVT::f32)
379 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
380 Copy = DAG.getCopyToReg(Chain, R0, Val, SDOperand());
381 }
382
383 if (DAG.getMachineFunction().liveout_empty()) {
Rafael Espindola4b023672006-06-05 22:26:14 +0000384 DAG.getMachineFunction().addLiveOut(ARM::R0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000385 if (Val.getValueType() == MVT::f64)
386 DAG.getMachineFunction().addLiveOut(ARM::R1);
387 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000388 break;
Rafael Espindola27185192006-09-29 21:20:16 +0000389 }
Rafael Espindola3a02f022006-09-04 19:05:01 +0000390 case 5:
391 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
392 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
393 // If we haven't noted the R0+R1 are live out, do so now.
394 if (DAG.getMachineFunction().liveout_empty()) {
395 DAG.getMachineFunction().addLiveOut(ARM::R0);
396 DAG.getMachineFunction().addLiveOut(ARM::R1);
397 }
398 break;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000399 }
Rafael Espindola4b023672006-06-05 22:26:14 +0000400
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000401 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
402 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000403}
404
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000405static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
406 MVT::ValueType PtrVT = Op.getValueType();
407 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengc356a572006-09-12 21:04:05 +0000408 Constant *C = CP->getConstVal();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000409 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
410
411 return CPI;
412}
413
414static SDOperand LowerGlobalAddress(SDOperand Op,
415 SelectionDAG &DAG) {
416 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Rafael Espindola61369da2006-08-14 19:01:24 +0000417 int alignment = 2;
418 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000419 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr,
420 DAG.getSrcValue(NULL));
421}
422
Rafael Espindola755be9b2006-08-25 17:55:16 +0000423static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
424 unsigned VarArgsFrameIndex) {
425 // vastart just stores the address of the VarArgsFrameIndex slot into the
426 // memory location argument.
427 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
428 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Evan Cheng786225a2006-10-05 23:01:46 +0000429 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), Op.getOperand(2));
Rafael Espindola755be9b2006-08-25 17:55:16 +0000430}
431
432static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
433 int &VarArgsFrameIndex) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000434 MachineFunction &MF = DAG.getMachineFunction();
435 MachineFrameInfo *MFI = MF.getFrameInfo();
436 SSARegMap *RegMap = MF.getSSARegMap();
437 unsigned NumArgs = Op.Val->getNumValues()-1;
438 SDOperand Root = Op.getOperand(0);
439 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
440 static const unsigned REGS[] = {
441 ARM::R0, ARM::R1, ARM::R2, ARM::R3
442 };
443
444 std::vector<MVT::ValueType> Types(Op.Val->value_begin(), Op.Val->value_end() - 1);
445 ArgumentLayout Layout(Types);
446
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000447 std::vector<SDOperand> ArgValues;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000448 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000449 MVT::ValueType VT = Types[ArgNo];
Rafael Espindola4b442b52006-05-23 02:48:20 +0000450
Rafael Espindolaa2845842006-10-05 16:48:49 +0000451 SDOperand Value;
452 if (Layout.isRegister(ArgNo)) {
453 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
454 unsigned RegNum = Layout.getRegisterNum(ArgNo);
455 unsigned Reg1 = REGS[RegNum];
456 unsigned VReg1 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
457 SDOperand Value1 = DAG.getCopyFromReg(Root, VReg1, MVT::i32);
458 MF.addLiveIn(Reg1, VReg1);
459 if (VT == MVT::f64) {
460 unsigned Reg2 = REGS[RegNum + 1];
461 unsigned VReg2 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
462 SDOperand Value2 = DAG.getCopyFromReg(Root, VReg2, MVT::i32);
463 MF.addLiveIn(Reg2, VReg2);
464 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
465 } else {
466 Value = Value1;
467 if (VT == MVT::f32)
468 Value = DAG.getNode(ISD::BIT_CONVERT, VT, Value);
469 }
470 } else {
471 // If the argument is actually used, emit a load from the right stack
472 // slot.
473 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
474 unsigned Offset = Layout.getOffset(ArgNo);
475 unsigned Size = MVT::getSizeInBits(VT)/8;
476 int FI = MFI->CreateFixedObject(Size, Offset);
477 SDOperand FIN = DAG.getFrameIndex(FI, VT);
478 Value = DAG.getLoad(VT, Root, FIN, DAG.getSrcValue(NULL));
479 } else {
480 Value = DAG.getNode(ISD::UNDEF, VT);
481 }
482 }
483 ArgValues.push_back(Value);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000484 }
485
Rafael Espindolaa2845842006-10-05 16:48:49 +0000486 unsigned NextRegNum = Layout.lastRegNum() + 1;
487
Rafael Espindola755be9b2006-08-25 17:55:16 +0000488 if (isVarArg) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000489 //If this function is vararg we must store the remaing
490 //registers so that they can be acessed with va_start
Rafael Espindola755be9b2006-08-25 17:55:16 +0000491 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000492 -16 + NextRegNum * 4);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000493
Rafael Espindola755be9b2006-08-25 17:55:16 +0000494 SmallVector<SDOperand, 4> MemOps;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000495 for (unsigned RegNo = NextRegNum; RegNo < 4; ++RegNo) {
496 int RegOffset = - (4 - RegNo) * 4;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000497 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000498 RegOffset);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000499 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
500
Rafael Espindolaa2845842006-10-05 16:48:49 +0000501 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
502 MF.addLiveIn(REGS[RegNo], VReg);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000503
504 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
Evan Cheng786225a2006-10-05 23:01:46 +0000505 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN,
506 DAG.getSrcValue(NULL));
Rafael Espindola755be9b2006-08-25 17:55:16 +0000507 MemOps.push_back(Store);
508 }
509 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
510 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000511
512 ArgValues.push_back(Root);
513
514 // Return the new list of results.
515 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
516 Op.Val->value_end());
Chris Lattner87428672006-08-11 17:22:35 +0000517 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
Rafael Espindoladc124a22006-05-18 21:45:49 +0000518}
519
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000520static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
521 SDOperand LHS = Op.getOperand(0);
522 SDOperand RHS = Op.getOperand(1);
523 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
524 SDOperand TrueVal = Op.getOperand(2);
525 SDOperand FalseVal = Op.getOperand(3);
Rafael Espindolacdda88c2006-08-24 17:19:08 +0000526 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000527
528 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000529 return DAG.getNode(ARMISD::SELECT, MVT::i32, TrueVal, FalseVal, ARMCC, Cmp);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000530}
531
Rafael Espindola687bc492006-08-24 13:45:55 +0000532static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
533 SDOperand Chain = Op.getOperand(0);
534 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
535 SDOperand LHS = Op.getOperand(2);
536 SDOperand RHS = Op.getOperand(3);
537 SDOperand Dest = Op.getOperand(4);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000538 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
Rafael Espindola687bc492006-08-24 13:45:55 +0000539
540 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000541 return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, ARMCC, Cmp);
Rafael Espindola687bc492006-08-24 13:45:55 +0000542}
543
Rafael Espindola27185192006-09-29 21:20:16 +0000544static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola9e071f02006-10-02 19:30:56 +0000545 SDOperand IntVal = Op.getOperand(0);
Rafael Espindola27185192006-09-29 21:20:16 +0000546 assert(IntVal.getValueType() == MVT::i32);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000547 MVT::ValueType vt = Op.getValueType();
548 assert(vt == MVT::f32 ||
549 vt == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000550
551 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000552 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FSITOS : ARMISD::FSITOD;
553 return DAG.getNode(op, vt, Tmp);
Rafael Espindola27185192006-09-29 21:20:16 +0000554}
555
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000556static SDOperand LowerUINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
557 SDOperand IntVal = Op.getOperand(0);
558 assert(IntVal.getValueType() == MVT::i32);
559 MVT::ValueType vt = Op.getValueType();
560 assert(vt == MVT::f32 ||
561 vt == MVT::f64);
562
563 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
564 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FUITOS : ARMISD::FUITOD;
565 return DAG.getNode(op, vt, Tmp);
566}
567
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000568SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
569 switch (Op.getOpcode()) {
570 default:
571 assert(0 && "Should not custom lower this!");
Rafael Espindola1c8f0532006-05-15 22:34:39 +0000572 abort();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000573 case ISD::ConstantPool:
574 return LowerConstantPool(Op, DAG);
575 case ISD::GlobalAddress:
576 return LowerGlobalAddress(Op, DAG);
Rafael Espindola27185192006-09-29 21:20:16 +0000577 case ISD::SINT_TO_FP:
578 return LowerSINT_TO_FP(Op, DAG);
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000579 case ISD::UINT_TO_FP:
580 return LowerUINT_TO_FP(Op, DAG);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000581 case ISD::FORMAL_ARGUMENTS:
Rafael Espindola755be9b2006-08-25 17:55:16 +0000582 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000583 case ISD::CALL:
584 return LowerCALL(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000585 case ISD::RET:
586 return LowerRET(Op, DAG);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000587 case ISD::SELECT_CC:
588 return LowerSELECT_CC(Op, DAG);
Rafael Espindola687bc492006-08-24 13:45:55 +0000589 case ISD::BR_CC:
590 return LowerBR_CC(Op, DAG);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000591 case ISD::VASTART:
592 return LowerVASTART(Op, DAG, VarArgsFrameIndex);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000593 }
594}
595
596//===----------------------------------------------------------------------===//
597// Instruction Selector Implementation
598//===----------------------------------------------------------------------===//
599
600//===--------------------------------------------------------------------===//
601/// ARMDAGToDAGISel - ARM specific code to select ARM machine
602/// instructions for SelectionDAG operations.
603///
604namespace {
605class ARMDAGToDAGISel : public SelectionDAGISel {
606 ARMTargetLowering Lowering;
607
608public:
609 ARMDAGToDAGISel(TargetMachine &TM)
610 : SelectionDAGISel(Lowering), Lowering(TM) {
611 }
612
Evan Cheng9ade2182006-08-26 05:34:46 +0000613 SDNode *Select(SDOperand Op);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000614 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000615 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000616 bool SelectAddrMode1(SDOperand N, SDOperand &Arg, SDOperand &Shift,
617 SDOperand &ShiftType);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000618
619 // Include the pieces autogenerated from the target description.
620#include "ARMGenDAGISel.inc"
621};
622
623void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
624 DEBUG(BB->dump());
625
626 DAG.setRoot(SelectRoot(DAG.getRoot()));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000627 DAG.RemoveDeadNodes();
628
629 ScheduleAndEmitDAG(DAG);
630}
631
Rafael Espindola61369da2006-08-14 19:01:24 +0000632static bool isInt12Immediate(SDNode *N, short &Imm) {
633 if (N->getOpcode() != ISD::Constant)
634 return false;
635
636 int32_t t = cast<ConstantSDNode>(N)->getValue();
Rafael Espindola7246d332006-09-21 11:29:52 +0000637 int max = 1<<12;
Rafael Espindola61369da2006-08-14 19:01:24 +0000638 int min = -max;
639 if (t > min && t < max) {
640 Imm = t;
641 return true;
642 }
643 else
644 return false;
645}
646
647static bool isInt12Immediate(SDOperand Op, short &Imm) {
648 return isInt12Immediate(Op.Val, Imm);
649}
650
Rafael Espindola7246d332006-09-21 11:29:52 +0000651static uint32_t rotateL(uint32_t x) {
652 uint32_t bit31 = (x & (1 << 31)) >> 31;
653 uint32_t t = x << 1;
654 return t | bit31;
655}
656
657static bool isUInt8Immediate(uint32_t x) {
658 return x < (1 << 8);
659}
660
661static bool isRotInt8Immediate(uint32_t x) {
662 int r;
663 for (r = 0; r < 16; r++) {
664 if (isUInt8Immediate(x))
665 return true;
666 x = rotateL(rotateL(x));
667 }
668 return false;
669}
670
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000671bool ARMDAGToDAGISel::SelectAddrMode1(SDOperand N,
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000672 SDOperand &Arg,
673 SDOperand &Shift,
674 SDOperand &ShiftType) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000675 switch(N.getOpcode()) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000676 case ISD::Constant: {
Rafael Espindola7246d332006-09-21 11:29:52 +0000677 uint32_t val = cast<ConstantSDNode>(N)->getValue();
678 if(!isRotInt8Immediate(val)) {
679 const Type *t = MVT::getTypeForValueType(MVT::i32);
680 Constant *C = ConstantUInt::get(t, val);
681 int alignment = 2;
682 SDOperand Addr = CurDAG->getTargetConstantPool(C, MVT::i32, alignment);
683 SDOperand Z = CurDAG->getTargetConstant(0, MVT::i32);
684 SDNode *n = CurDAG->getTargetNode(ARM::ldr, MVT::i32, Z, Addr);
685 Arg = SDOperand(n, 0);
686 } else
687 Arg = CurDAG->getTargetConstant(val, MVT::i32);
688
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000689 Shift = CurDAG->getTargetConstant(0, MVT::i32);
690 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000691 return true;
692 }
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000693 case ISD::SRA:
694 Arg = N.getOperand(0);
695 Shift = N.getOperand(1);
696 ShiftType = CurDAG->getTargetConstant(ARMShift::ASR, MVT::i32);
697 return true;
698 case ISD::SRL:
699 Arg = N.getOperand(0);
700 Shift = N.getOperand(1);
701 ShiftType = CurDAG->getTargetConstant(ARMShift::LSR, MVT::i32);
702 return true;
703 case ISD::SHL:
704 Arg = N.getOperand(0);
705 Shift = N.getOperand(1);
706 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
707 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000708 }
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000709
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000710 Arg = N;
711 Shift = CurDAG->getTargetConstant(0, MVT::i32);
712 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000713 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000714}
715
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000716//register plus/minus 12 bit offset
717bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
718 SDOperand &Base) {
Rafael Espindolaf3a335c2006-08-17 17:09:40 +0000719 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
720 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
721 Offset = CurDAG->getTargetConstant(0, MVT::i32);
722 return true;
723 }
Rafael Espindola61369da2006-08-14 19:01:24 +0000724 if (N.getOpcode() == ISD::ADD) {
725 short imm = 0;
726 if (isInt12Immediate(N.getOperand(1), imm)) {
727 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
728 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
729 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
730 } else {
731 Base = N.getOperand(0);
732 }
733 return true; // [r+i]
734 }
735 }
736
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000737 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000738 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
739 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
740 }
741 else
742 Base = N;
743 return true; //any address fits in a register
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000744}
745
Evan Cheng9ade2182006-08-26 05:34:46 +0000746SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000747 SDNode *N = Op.Val;
748
749 switch (N->getOpcode()) {
750 default:
Evan Cheng9ade2182006-08-26 05:34:46 +0000751 return SelectCode(Op);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000752 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000753 }
Evan Cheng64a752f2006-08-11 09:08:15 +0000754 return NULL;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000755}
756
757} // end anonymous namespace
758
759/// createARMISelDag - This pass converts a legalized DAG into a
760/// ARM-specific DAG, ready for instruction scheduling.
761///
762FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
763 return new ARMDAGToDAGISel(TM);
764}