blob: 9c64445d395c7bd544f209ebef8b991a6c604c20 [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 Espindolaad557f92006-10-09 14:13:40 +000050 setLoadXAction(ISD::EXTLOAD, MVT::f32, Expand);
51
Rafael Espindola27185192006-09-29 21:20:16 +000052 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Rafael Espindola3717ca92006-08-20 01:49:49 +000053
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +000054 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
55
Rafael Espindola06c1e7e2006-08-01 12:58:43 +000056 setOperationAction(ISD::RET, MVT::Other, Custom);
57 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
58 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Rafael Espindola341b8642006-08-04 12:48:42 +000059
Rafael Espindola3c000bf2006-08-21 22:00:32 +000060 setOperationAction(ISD::SETCC, MVT::i32, Expand);
61 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
Rafael Espindola687bc492006-08-24 13:45:55 +000062 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
Rafael Espindola3c000bf2006-08-21 22:00:32 +000063
Rafael Espindola755be9b2006-08-25 17:55:16 +000064 setOperationAction(ISD::VASTART, MVT::Other, Custom);
65 setOperationAction(ISD::VAEND, MVT::Other, Expand);
66
Rafael Espindolacd71da52006-10-03 17:27:58 +000067 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
68 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
69
Rafael Espindola341b8642006-08-04 12:48:42 +000070 setSchedulingPreference(SchedulingForRegPressure);
Rafael Espindola3717ca92006-08-20 01:49:49 +000071 computeRegisterProperties();
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000072}
73
Rafael Espindola84b19be2006-07-16 01:02:57 +000074namespace llvm {
75 namespace ARMISD {
76 enum NodeType {
77 // Start the numbering where the builting ops and target ops leave off.
78 FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
79 /// CALL - A direct function call.
Rafael Espindolaf4fda802006-08-03 17:02:20 +000080 CALL,
81
82 /// Return with a flag operand.
Rafael Espindola3c000bf2006-08-21 22:00:32 +000083 RET_FLAG,
84
85 CMP,
86
Rafael Espindola687bc492006-08-24 13:45:55 +000087 SELECT,
88
Rafael Espindola27185192006-09-29 21:20:16 +000089 BR,
90
Rafael Espindola9e071f02006-10-02 19:30:56 +000091 FSITOS,
92
93 FSITOD,
94
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +000095 FUITOS,
96
97 FUITOD,
98
Rafael Espindolaa2845842006-10-05 16:48:49 +000099 FMRRD,
100
101 FMDRR
Rafael Espindola84b19be2006-07-16 01:02:57 +0000102 };
103 }
104}
105
Rafael Espindola6f602de2006-08-24 16:13:15 +0000106/// DAGCCToARMCC - Convert a DAG integer condition code to an ARM CC
107static ARMCC::CondCodes DAGCCToARMCC(ISD::CondCode CC) {
108 switch (CC) {
Rafael Espindolaebdabda2006-09-21 13:06:26 +0000109 default:
110 std::cerr << "CC = " << CC << "\n";
111 assert(0 && "Unknown condition code!");
112 case ISD::SETUGT: return ARMCC::HI;
113 case ISD::SETULE: return ARMCC::LS;
114 case ISD::SETLE: return ARMCC::LE;
115 case ISD::SETLT: return ARMCC::LT;
116 case ISD::SETGT: return ARMCC::GT;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000117 case ISD::SETNE: return ARMCC::NE;
Rafael Espindolacdda88c2006-08-24 17:19:08 +0000118 case ISD::SETEQ: return ARMCC::EQ;
Rafael Espindola5f450d22006-09-02 20:24:25 +0000119 case ISD::SETGE: return ARMCC::GE;
120 case ISD::SETUGE: return ARMCC::CS;
Rafael Espindolabc4cec92006-09-03 13:19:16 +0000121 case ISD::SETULT: return ARMCC::CC;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000122 }
123}
124
Rafael Espindola84b19be2006-07-16 01:02:57 +0000125const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
126 switch (Opcode) {
127 default: return 0;
128 case ARMISD::CALL: return "ARMISD::CALL";
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000129 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000130 case ARMISD::SELECT: return "ARMISD::SELECT";
131 case ARMISD::CMP: return "ARMISD::CMP";
Rafael Espindola687bc492006-08-24 13:45:55 +0000132 case ARMISD::BR: return "ARMISD::BR";
Rafael Espindola27185192006-09-29 21:20:16 +0000133 case ARMISD::FSITOS: return "ARMISD::FSITOS";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000134 case ARMISD::FSITOD: return "ARMISD::FSITOD";
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000135 case ARMISD::FUITOS: return "ARMISD::FUITOS";
136 case ARMISD::FUITOD: return "ARMISD::FUITOD";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000137 case ARMISD::FMRRD: return "ARMISD::FMRRD";
Rafael Espindolaa2845842006-10-05 16:48:49 +0000138 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Rafael Espindola84b19be2006-07-16 01:02:57 +0000139 }
140}
141
Rafael Espindolaa2845842006-10-05 16:48:49 +0000142class ArgumentLayout {
143 std::vector<bool> is_reg;
144 std::vector<unsigned> pos;
145 std::vector<MVT::ValueType> types;
146public:
Rafael Espindola39b5a212006-10-05 17:46:48 +0000147 ArgumentLayout(const std::vector<MVT::ValueType> &Types) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000148 types = Types;
149
150 unsigned RegNum = 0;
151 unsigned StackOffset = 0;
Rafael Espindola39b5a212006-10-05 17:46:48 +0000152 for(std::vector<MVT::ValueType>::const_iterator I = Types.begin();
Rafael Espindolaa2845842006-10-05 16:48:49 +0000153 I != Types.end();
154 ++I) {
155 MVT::ValueType VT = *I;
156 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
157 unsigned size = MVT::getSizeInBits(VT)/32;
158
159 RegNum = ((RegNum + size - 1) / size) * size;
160 if (RegNum < 4) {
161 pos.push_back(RegNum);
162 is_reg.push_back(true);
163 RegNum += size;
164 } else {
165 unsigned bytes = size * 32/8;
166 StackOffset = ((StackOffset + bytes - 1) / bytes) * bytes;
167 pos.push_back(StackOffset);
168 is_reg.push_back(false);
169 StackOffset += bytes;
170 }
171 }
172 }
173 unsigned getRegisterNum(unsigned argNum) {
174 assert(isRegister(argNum));
175 return pos[argNum];
176 }
177 unsigned getOffset(unsigned argNum) {
178 assert(isOffset(argNum));
179 return pos[argNum];
180 }
181 unsigned isRegister(unsigned argNum) {
182 assert(argNum < is_reg.size());
183 return is_reg[argNum];
184 }
185 unsigned isOffset(unsigned argNum) {
186 return !isRegister(argNum);
187 }
188 MVT::ValueType getType(unsigned argNum) {
189 assert(argNum < types.size());
190 return types[argNum];
191 }
192 unsigned getStackSize(void) {
193 int last = is_reg.size() - 1;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000194 if (last < 0)
195 return 0;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000196 if (isRegister(last))
197 return 0;
198 return getOffset(last) + MVT::getSizeInBits(getType(last))/8;
199 }
200 int lastRegArg(void) {
201 int size = is_reg.size();
202 int last = 0;
203 while(last < size && isRegister(last))
204 last++;
205 last--;
206 return last;
207 }
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000208 int lastRegNum(void) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000209 int l = lastRegArg();
210 if (l < 0)
211 return -1;
212 unsigned r = getRegisterNum(l);
213 MVT::ValueType t = getType(l);
214 assert(t == MVT::i32 || t == MVT::f32 || t == MVT::f64);
215 if (t == MVT::f64)
216 return r + 1;
217 return r;
218 }
219};
220
Rafael Espindola84b19be2006-07-16 01:02:57 +0000221// This transforms a ISD::CALL node into a
222// callseq_star <- ARMISD:CALL <- callseq_end
223// chain
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000224static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola84b19be2006-07-16 01:02:57 +0000225 SDOperand Chain = Op.getOperand(0);
226 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
227 assert(CallConv == CallingConv::C && "unknown calling convention");
228 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000229 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
230 assert(isTailCall == false && "tail call not supported");
231 SDOperand Callee = Op.getOperand(4);
232 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Rafael Espindola1a009462006-08-08 13:02:29 +0000233 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000234 static const unsigned regs[] = {
Rafael Espindolafac00a92006-07-25 20:17:20 +0000235 ARM::R0, ARM::R1, ARM::R2, ARM::R3
236 };
237
Rafael Espindolaa2845842006-10-05 16:48:49 +0000238 std::vector<MVT::ValueType> Types;
239 for (unsigned i = 0; i < NumOps; ++i) {
240 MVT::ValueType VT = Op.getOperand(5+2*i).getValueType();
241 Types.push_back(VT);
242 }
243 ArgumentLayout Layout(Types);
Rafael Espindolafac00a92006-07-25 20:17:20 +0000244
Rafael Espindolaa2845842006-10-05 16:48:49 +0000245 unsigned NumBytes = Layout.getStackSize();
246
247 Chain = DAG.getCALLSEQ_START(Chain,
248 DAG.getConstant(NumBytes, MVT::i32));
249
250 //Build a sequence of stores
251 std::vector<SDOperand> MemOpChains;
252 for (unsigned i = Layout.lastRegArg() + 1; i < NumOps; ++i) {
253 SDOperand Arg = Op.getOperand(5+2*i);
254 unsigned ArgOffset = Layout.getOffset(i);
255 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
256 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Evan Cheng786225a2006-10-05 23:01:46 +0000257 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff,
258 DAG.getSrcValue(NULL)));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000259 }
Rafael Espindola1a009462006-08-08 13:02:29 +0000260 if (!MemOpChains.empty())
Chris Lattnere2199452006-08-11 17:38:39 +0000261 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
262 &MemOpChains[0], MemOpChains.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000263
Rafael Espindola84b19be2006-07-16 01:02:57 +0000264 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
265 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
266 // node so that legalize doesn't hack it.
267 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
268 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
269
270 // If this is a direct call, pass the chain and the callee.
271 assert (Callee.Val);
272 std::vector<SDOperand> Ops;
273 Ops.push_back(Chain);
274 Ops.push_back(Callee);
275
Rafael Espindolaa2845842006-10-05 16:48:49 +0000276 // Build a sequence of copy-to-reg nodes chained together with token chain
277 // and flag operands which copy the outgoing args into the appropriate regs.
278 SDOperand InFlag;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000279 for (int i = 0, e = Layout.lastRegArg(); i <= e; ++i) {
Rafael Espindola4a408d42006-10-06 12:50:22 +0000280 SDOperand Arg = Op.getOperand(5+2*i);
281 unsigned RegNum = Layout.getRegisterNum(i);
282 unsigned Reg1 = regs[RegNum];
283 MVT::ValueType VT = Layout.getType(i);
284 assert(VT == Arg.getValueType());
285 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000286
287 // Add argument register to the end of the list so that it is known live
288 // into the call.
Rafael Espindola4a408d42006-10-06 12:50:22 +0000289 Ops.push_back(DAG.getRegister(Reg1, MVT::i32));
290 if (VT == MVT::f64) {
291 unsigned Reg2 = regs[RegNum + 1];
292 SDOperand SDReg1 = DAG.getRegister(Reg1, MVT::i32);
293 SDOperand SDReg2 = DAG.getRegister(Reg2, MVT::i32);
294
295 Ops.push_back(DAG.getRegister(Reg2, MVT::i32));
296 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola935b1f82006-10-06 20:33:26 +0000297 SDOperand Ops[] = {Chain, SDReg1, SDReg2, Arg, InFlag};
298 Chain = DAG.getNode(ARMISD::FMRRD, VTs, Ops, InFlag.Val ? 5 : 4);
Rafael Espindola4a408d42006-10-06 12:50:22 +0000299 } else {
300 if (VT == MVT::f32)
301 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg);
302 Chain = DAG.getCopyToReg(Chain, Reg1, Arg, InFlag);
303 }
304 InFlag = Chain.getValue(1);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000305 }
306
307 std::vector<MVT::ValueType> NodeTys;
308 NodeTys.push_back(MVT::Other); // Returns a chain
309 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Rafael Espindola7a53bd02006-08-09 16:41:12 +0000310
Rafael Espindola84b19be2006-07-16 01:02:57 +0000311 unsigned CallOpc = ARMISD::CALL;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000312 if (InFlag.Val)
313 Ops.push_back(InFlag);
Chris Lattner87428672006-08-11 17:22:35 +0000314 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000315 InFlag = Chain.getValue(1);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000316
Rafael Espindolafac00a92006-07-25 20:17:20 +0000317 std::vector<SDOperand> ResultVals;
318 NodeTys.clear();
319
320 // If the call has results, copy the values out of the ret val registers.
Rafael Espindola614057b2006-10-06 19:10:05 +0000321 MVT::ValueType VT = Op.Val->getValueType(0);
322 if (VT != MVT::Other) {
323 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
324 SDOperand Value;
325
326 SDOperand Value1 = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
327 Chain = Value1.getValue(1);
328 InFlag = Value1.getValue(2);
329 if (VT == MVT::i32)
330 Value = Value1;
331 if (VT == MVT::f32)
332 Value = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Value1);
333 if (VT == MVT::f64) {
334 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
335 Chain = Value2.getValue(1);
336 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
337 }
338 ResultVals.push_back(Value);
339 NodeTys.push_back(VT);
Rafael Espindolafac00a92006-07-25 20:17:20 +0000340 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000341
342 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
343 DAG.getConstant(NumBytes, MVT::i32));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000344 NodeTys.push_back(MVT::Other);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000345
Rafael Espindolafac00a92006-07-25 20:17:20 +0000346 if (ResultVals.empty())
347 return Chain;
348
349 ResultVals.push_back(Chain);
Chris Lattner87428672006-08-11 17:22:35 +0000350 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
351 ResultVals.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000352 return Res.getValue(Op.ResNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000353}
354
355static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
356 SDOperand Copy;
Rafael Espindola4b023672006-06-05 22:26:14 +0000357 SDOperand Chain = Op.getOperand(0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000358 SDOperand R0 = DAG.getRegister(ARM::R0, MVT::i32);
359 SDOperand R1 = DAG.getRegister(ARM::R1, MVT::i32);
360
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000361 switch(Op.getNumOperands()) {
362 default:
363 assert(0 && "Do not know how to return this many arguments!");
364 abort();
Rafael Espindola4b023672006-06-05 22:26:14 +0000365 case 1: {
366 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
Rafael Espindola6312da02006-08-03 22:50:11 +0000367 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
Rafael Espindola4b023672006-06-05 22:26:14 +0000368 }
Rafael Espindola27185192006-09-29 21:20:16 +0000369 case 3: {
370 SDOperand Val = Op.getOperand(1);
371 assert(Val.getValueType() == MVT::i32 ||
Rafael Espindola9e071f02006-10-02 19:30:56 +0000372 Val.getValueType() == MVT::f32 ||
373 Val.getValueType() == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000374
Rafael Espindola9e071f02006-10-02 19:30:56 +0000375 if (Val.getValueType() == MVT::f64) {
376 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
377 SDOperand Ops[] = {Chain, R0, R1, Val};
378 Copy = DAG.getNode(ARMISD::FMRRD, VTs, Ops, 4);
379 } else {
380 if (Val.getValueType() == MVT::f32)
381 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
382 Copy = DAG.getCopyToReg(Chain, R0, Val, SDOperand());
383 }
384
385 if (DAG.getMachineFunction().liveout_empty()) {
Rafael Espindola4b023672006-06-05 22:26:14 +0000386 DAG.getMachineFunction().addLiveOut(ARM::R0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000387 if (Val.getValueType() == MVT::f64)
388 DAG.getMachineFunction().addLiveOut(ARM::R1);
389 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000390 break;
Rafael Espindola27185192006-09-29 21:20:16 +0000391 }
Rafael Espindola3a02f022006-09-04 19:05:01 +0000392 case 5:
393 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
394 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
395 // If we haven't noted the R0+R1 are live out, do so now.
396 if (DAG.getMachineFunction().liveout_empty()) {
397 DAG.getMachineFunction().addLiveOut(ARM::R0);
398 DAG.getMachineFunction().addLiveOut(ARM::R1);
399 }
400 break;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000401 }
Rafael Espindola4b023672006-06-05 22:26:14 +0000402
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000403 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
404 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000405}
406
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000407static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
408 MVT::ValueType PtrVT = Op.getValueType();
409 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengc356a572006-09-12 21:04:05 +0000410 Constant *C = CP->getConstVal();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000411 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
412
413 return CPI;
414}
415
416static SDOperand LowerGlobalAddress(SDOperand Op,
417 SelectionDAG &DAG) {
418 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Rafael Espindola61369da2006-08-14 19:01:24 +0000419 int alignment = 2;
420 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000421 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr,
422 DAG.getSrcValue(NULL));
423}
424
Rafael Espindola755be9b2006-08-25 17:55:16 +0000425static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
426 unsigned VarArgsFrameIndex) {
427 // vastart just stores the address of the VarArgsFrameIndex slot into the
428 // memory location argument.
429 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
430 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Evan Cheng786225a2006-10-05 23:01:46 +0000431 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), Op.getOperand(2));
Rafael Espindola755be9b2006-08-25 17:55:16 +0000432}
433
434static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
435 int &VarArgsFrameIndex) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000436 MachineFunction &MF = DAG.getMachineFunction();
437 MachineFrameInfo *MFI = MF.getFrameInfo();
438 SSARegMap *RegMap = MF.getSSARegMap();
439 unsigned NumArgs = Op.Val->getNumValues()-1;
440 SDOperand Root = Op.getOperand(0);
441 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
442 static const unsigned REGS[] = {
443 ARM::R0, ARM::R1, ARM::R2, ARM::R3
444 };
445
446 std::vector<MVT::ValueType> Types(Op.Val->value_begin(), Op.Val->value_end() - 1);
447 ArgumentLayout Layout(Types);
448
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000449 std::vector<SDOperand> ArgValues;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000450 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000451 MVT::ValueType VT = Types[ArgNo];
Rafael Espindola4b442b52006-05-23 02:48:20 +0000452
Rafael Espindolaa2845842006-10-05 16:48:49 +0000453 SDOperand Value;
454 if (Layout.isRegister(ArgNo)) {
455 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
456 unsigned RegNum = Layout.getRegisterNum(ArgNo);
457 unsigned Reg1 = REGS[RegNum];
458 unsigned VReg1 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
459 SDOperand Value1 = DAG.getCopyFromReg(Root, VReg1, MVT::i32);
460 MF.addLiveIn(Reg1, VReg1);
461 if (VT == MVT::f64) {
462 unsigned Reg2 = REGS[RegNum + 1];
463 unsigned VReg2 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
464 SDOperand Value2 = DAG.getCopyFromReg(Root, VReg2, MVT::i32);
465 MF.addLiveIn(Reg2, VReg2);
466 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
467 } else {
468 Value = Value1;
469 if (VT == MVT::f32)
470 Value = DAG.getNode(ISD::BIT_CONVERT, VT, Value);
471 }
472 } else {
473 // If the argument is actually used, emit a load from the right stack
474 // slot.
475 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
476 unsigned Offset = Layout.getOffset(ArgNo);
477 unsigned Size = MVT::getSizeInBits(VT)/8;
478 int FI = MFI->CreateFixedObject(Size, Offset);
479 SDOperand FIN = DAG.getFrameIndex(FI, VT);
480 Value = DAG.getLoad(VT, Root, FIN, DAG.getSrcValue(NULL));
481 } else {
482 Value = DAG.getNode(ISD::UNDEF, VT);
483 }
484 }
485 ArgValues.push_back(Value);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000486 }
487
Rafael Espindolaa2845842006-10-05 16:48:49 +0000488 unsigned NextRegNum = Layout.lastRegNum() + 1;
489
Rafael Espindola755be9b2006-08-25 17:55:16 +0000490 if (isVarArg) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000491 //If this function is vararg we must store the remaing
492 //registers so that they can be acessed with va_start
Rafael Espindola755be9b2006-08-25 17:55:16 +0000493 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000494 -16 + NextRegNum * 4);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000495
Rafael Espindola755be9b2006-08-25 17:55:16 +0000496 SmallVector<SDOperand, 4> MemOps;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000497 for (unsigned RegNo = NextRegNum; RegNo < 4; ++RegNo) {
498 int RegOffset = - (4 - RegNo) * 4;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000499 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000500 RegOffset);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000501 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
502
Rafael Espindolaa2845842006-10-05 16:48:49 +0000503 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
504 MF.addLiveIn(REGS[RegNo], VReg);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000505
506 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
Evan Cheng786225a2006-10-05 23:01:46 +0000507 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN,
508 DAG.getSrcValue(NULL));
Rafael Espindola755be9b2006-08-25 17:55:16 +0000509 MemOps.push_back(Store);
510 }
511 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
512 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000513
514 ArgValues.push_back(Root);
515
516 // Return the new list of results.
517 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
518 Op.Val->value_end());
Chris Lattner87428672006-08-11 17:22:35 +0000519 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
Rafael Espindoladc124a22006-05-18 21:45:49 +0000520}
521
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000522static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
523 SDOperand LHS = Op.getOperand(0);
524 SDOperand RHS = Op.getOperand(1);
525 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
526 SDOperand TrueVal = Op.getOperand(2);
527 SDOperand FalseVal = Op.getOperand(3);
Rafael Espindolacdda88c2006-08-24 17:19:08 +0000528 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000529
530 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000531 return DAG.getNode(ARMISD::SELECT, MVT::i32, TrueVal, FalseVal, ARMCC, Cmp);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000532}
533
Rafael Espindola687bc492006-08-24 13:45:55 +0000534static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
535 SDOperand Chain = Op.getOperand(0);
536 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
537 SDOperand LHS = Op.getOperand(2);
538 SDOperand RHS = Op.getOperand(3);
539 SDOperand Dest = Op.getOperand(4);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000540 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
Rafael Espindola687bc492006-08-24 13:45:55 +0000541
542 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000543 return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, ARMCC, Cmp);
Rafael Espindola687bc492006-08-24 13:45:55 +0000544}
545
Rafael Espindola27185192006-09-29 21:20:16 +0000546static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola9e071f02006-10-02 19:30:56 +0000547 SDOperand IntVal = Op.getOperand(0);
Rafael Espindola27185192006-09-29 21:20:16 +0000548 assert(IntVal.getValueType() == MVT::i32);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000549 MVT::ValueType vt = Op.getValueType();
550 assert(vt == MVT::f32 ||
551 vt == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000552
553 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000554 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FSITOS : ARMISD::FSITOD;
555 return DAG.getNode(op, vt, Tmp);
Rafael Espindola27185192006-09-29 21:20:16 +0000556}
557
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000558static SDOperand LowerUINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
559 SDOperand IntVal = Op.getOperand(0);
560 assert(IntVal.getValueType() == MVT::i32);
561 MVT::ValueType vt = Op.getValueType();
562 assert(vt == MVT::f32 ||
563 vt == MVT::f64);
564
565 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
566 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FUITOS : ARMISD::FUITOD;
567 return DAG.getNode(op, vt, Tmp);
568}
569
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000570SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
571 switch (Op.getOpcode()) {
572 default:
573 assert(0 && "Should not custom lower this!");
Rafael Espindola1c8f0532006-05-15 22:34:39 +0000574 abort();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000575 case ISD::ConstantPool:
576 return LowerConstantPool(Op, DAG);
577 case ISD::GlobalAddress:
578 return LowerGlobalAddress(Op, DAG);
Rafael Espindola27185192006-09-29 21:20:16 +0000579 case ISD::SINT_TO_FP:
580 return LowerSINT_TO_FP(Op, DAG);
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000581 case ISD::UINT_TO_FP:
582 return LowerUINT_TO_FP(Op, DAG);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000583 case ISD::FORMAL_ARGUMENTS:
Rafael Espindola755be9b2006-08-25 17:55:16 +0000584 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000585 case ISD::CALL:
586 return LowerCALL(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000587 case ISD::RET:
588 return LowerRET(Op, DAG);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000589 case ISD::SELECT_CC:
590 return LowerSELECT_CC(Op, DAG);
Rafael Espindola687bc492006-08-24 13:45:55 +0000591 case ISD::BR_CC:
592 return LowerBR_CC(Op, DAG);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000593 case ISD::VASTART:
594 return LowerVASTART(Op, DAG, VarArgsFrameIndex);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000595 }
596}
597
598//===----------------------------------------------------------------------===//
599// Instruction Selector Implementation
600//===----------------------------------------------------------------------===//
601
602//===--------------------------------------------------------------------===//
603/// ARMDAGToDAGISel - ARM specific code to select ARM machine
604/// instructions for SelectionDAG operations.
605///
606namespace {
607class ARMDAGToDAGISel : public SelectionDAGISel {
608 ARMTargetLowering Lowering;
609
610public:
611 ARMDAGToDAGISel(TargetMachine &TM)
612 : SelectionDAGISel(Lowering), Lowering(TM) {
613 }
614
Evan Cheng9ade2182006-08-26 05:34:46 +0000615 SDNode *Select(SDOperand Op);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000616 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000617 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000618 bool SelectAddrMode1(SDOperand N, SDOperand &Arg, SDOperand &Shift,
619 SDOperand &ShiftType);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000620
621 // Include the pieces autogenerated from the target description.
622#include "ARMGenDAGISel.inc"
623};
624
625void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
626 DEBUG(BB->dump());
627
628 DAG.setRoot(SelectRoot(DAG.getRoot()));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000629 DAG.RemoveDeadNodes();
630
631 ScheduleAndEmitDAG(DAG);
632}
633
Rafael Espindola61369da2006-08-14 19:01:24 +0000634static bool isInt12Immediate(SDNode *N, short &Imm) {
635 if (N->getOpcode() != ISD::Constant)
636 return false;
637
638 int32_t t = cast<ConstantSDNode>(N)->getValue();
Rafael Espindola7246d332006-09-21 11:29:52 +0000639 int max = 1<<12;
Rafael Espindola61369da2006-08-14 19:01:24 +0000640 int min = -max;
641 if (t > min && t < max) {
642 Imm = t;
643 return true;
644 }
645 else
646 return false;
647}
648
649static bool isInt12Immediate(SDOperand Op, short &Imm) {
650 return isInt12Immediate(Op.Val, Imm);
651}
652
Rafael Espindola7246d332006-09-21 11:29:52 +0000653static uint32_t rotateL(uint32_t x) {
654 uint32_t bit31 = (x & (1 << 31)) >> 31;
655 uint32_t t = x << 1;
656 return t | bit31;
657}
658
659static bool isUInt8Immediate(uint32_t x) {
660 return x < (1 << 8);
661}
662
663static bool isRotInt8Immediate(uint32_t x) {
664 int r;
665 for (r = 0; r < 16; r++) {
666 if (isUInt8Immediate(x))
667 return true;
668 x = rotateL(rotateL(x));
669 }
670 return false;
671}
672
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000673bool ARMDAGToDAGISel::SelectAddrMode1(SDOperand N,
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000674 SDOperand &Arg,
675 SDOperand &Shift,
676 SDOperand &ShiftType) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000677 switch(N.getOpcode()) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000678 case ISD::Constant: {
Rafael Espindola7246d332006-09-21 11:29:52 +0000679 uint32_t val = cast<ConstantSDNode>(N)->getValue();
680 if(!isRotInt8Immediate(val)) {
681 const Type *t = MVT::getTypeForValueType(MVT::i32);
682 Constant *C = ConstantUInt::get(t, val);
683 int alignment = 2;
684 SDOperand Addr = CurDAG->getTargetConstantPool(C, MVT::i32, alignment);
685 SDOperand Z = CurDAG->getTargetConstant(0, MVT::i32);
686 SDNode *n = CurDAG->getTargetNode(ARM::ldr, MVT::i32, Z, Addr);
687 Arg = SDOperand(n, 0);
688 } else
689 Arg = CurDAG->getTargetConstant(val, MVT::i32);
690
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000691 Shift = CurDAG->getTargetConstant(0, MVT::i32);
692 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000693 return true;
694 }
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000695 case ISD::SRA:
696 Arg = N.getOperand(0);
697 Shift = N.getOperand(1);
698 ShiftType = CurDAG->getTargetConstant(ARMShift::ASR, MVT::i32);
699 return true;
700 case ISD::SRL:
701 Arg = N.getOperand(0);
702 Shift = N.getOperand(1);
703 ShiftType = CurDAG->getTargetConstant(ARMShift::LSR, MVT::i32);
704 return true;
705 case ISD::SHL:
706 Arg = N.getOperand(0);
707 Shift = N.getOperand(1);
708 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
709 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000710 }
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000711
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000712 Arg = N;
713 Shift = CurDAG->getTargetConstant(0, MVT::i32);
714 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000715 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000716}
717
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000718//register plus/minus 12 bit offset
719bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
720 SDOperand &Base) {
Rafael Espindolaf3a335c2006-08-17 17:09:40 +0000721 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
722 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
723 Offset = CurDAG->getTargetConstant(0, MVT::i32);
724 return true;
725 }
Rafael Espindola61369da2006-08-14 19:01:24 +0000726 if (N.getOpcode() == ISD::ADD) {
727 short imm = 0;
728 if (isInt12Immediate(N.getOperand(1), imm)) {
729 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
730 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
731 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
732 } else {
733 Base = N.getOperand(0);
734 }
735 return true; // [r+i]
736 }
737 }
738
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000739 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000740 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
741 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
742 }
743 else
744 Base = N;
745 return true; //any address fits in a register
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000746}
747
Evan Cheng9ade2182006-08-26 05:34:46 +0000748SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000749 SDNode *N = Op.Val;
750
751 switch (N->getOpcode()) {
752 default:
Evan Cheng9ade2182006-08-26 05:34:46 +0000753 return SelectCode(Op);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000754 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000755 }
Evan Cheng64a752f2006-08-11 09:08:15 +0000756 return NULL;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000757}
758
759} // end anonymous namespace
760
761/// createARMISelDag - This pass converts a legalized DAG into a
762/// ARM-specific DAG, ready for instruction scheduling.
763///
764FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
765 return new ARMDAGToDAGISel(TM);
766}