blob: 4e417173480340d7c8319afa7c89a819ed95bbbd [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 Espindola06c1e7e2006-08-01 12:58:43 +000052 setOperationAction(ISD::RET, MVT::Other, Custom);
53 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
54 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Rafael Espindola341b8642006-08-04 12:48:42 +000055
Rafael Espindola3c000bf2006-08-21 22:00:32 +000056 setOperationAction(ISD::SETCC, MVT::i32, Expand);
57 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
Rafael Espindola687bc492006-08-24 13:45:55 +000058 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
Rafael Espindola3c000bf2006-08-21 22:00:32 +000059
Rafael Espindola755be9b2006-08-25 17:55:16 +000060 setOperationAction(ISD::VASTART, MVT::Other, Custom);
61 setOperationAction(ISD::VAEND, MVT::Other, Expand);
62
Rafael Espindolacd71da52006-10-03 17:27:58 +000063 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
64 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
65
Rafael Espindola341b8642006-08-04 12:48:42 +000066 setSchedulingPreference(SchedulingForRegPressure);
Rafael Espindola3717ca92006-08-20 01:49:49 +000067 computeRegisterProperties();
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000068}
69
Rafael Espindola84b19be2006-07-16 01:02:57 +000070namespace llvm {
71 namespace ARMISD {
72 enum NodeType {
73 // Start the numbering where the builting ops and target ops leave off.
74 FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
75 /// CALL - A direct function call.
Rafael Espindolaf4fda802006-08-03 17:02:20 +000076 CALL,
77
78 /// Return with a flag operand.
Rafael Espindola3c000bf2006-08-21 22:00:32 +000079 RET_FLAG,
80
81 CMP,
82
Rafael Espindola687bc492006-08-24 13:45:55 +000083 SELECT,
84
Rafael Espindola27185192006-09-29 21:20:16 +000085 BR,
86
Rafael Espindola9e071f02006-10-02 19:30:56 +000087 FSITOS,
88
89 FSITOD,
90
Rafael Espindolaa2845842006-10-05 16:48:49 +000091 FMRRD,
92
93 FMDRR
Rafael Espindola84b19be2006-07-16 01:02:57 +000094 };
95 }
96}
97
Rafael Espindola6f602de2006-08-24 16:13:15 +000098/// DAGCCToARMCC - Convert a DAG integer condition code to an ARM CC
99static ARMCC::CondCodes DAGCCToARMCC(ISD::CondCode CC) {
100 switch (CC) {
Rafael Espindolaebdabda2006-09-21 13:06:26 +0000101 default:
102 std::cerr << "CC = " << CC << "\n";
103 assert(0 && "Unknown condition code!");
104 case ISD::SETUGT: return ARMCC::HI;
105 case ISD::SETULE: return ARMCC::LS;
106 case ISD::SETLE: return ARMCC::LE;
107 case ISD::SETLT: return ARMCC::LT;
108 case ISD::SETGT: return ARMCC::GT;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000109 case ISD::SETNE: return ARMCC::NE;
Rafael Espindolacdda88c2006-08-24 17:19:08 +0000110 case ISD::SETEQ: return ARMCC::EQ;
Rafael Espindola5f450d22006-09-02 20:24:25 +0000111 case ISD::SETGE: return ARMCC::GE;
112 case ISD::SETUGE: return ARMCC::CS;
Rafael Espindolabc4cec92006-09-03 13:19:16 +0000113 case ISD::SETULT: return ARMCC::CC;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000114 }
115}
116
Rafael Espindola84b19be2006-07-16 01:02:57 +0000117const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
118 switch (Opcode) {
119 default: return 0;
120 case ARMISD::CALL: return "ARMISD::CALL";
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000121 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000122 case ARMISD::SELECT: return "ARMISD::SELECT";
123 case ARMISD::CMP: return "ARMISD::CMP";
Rafael Espindola687bc492006-08-24 13:45:55 +0000124 case ARMISD::BR: return "ARMISD::BR";
Rafael Espindola27185192006-09-29 21:20:16 +0000125 case ARMISD::FSITOS: return "ARMISD::FSITOS";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000126 case ARMISD::FSITOD: return "ARMISD::FSITOD";
127 case ARMISD::FMRRD: return "ARMISD::FMRRD";
Rafael Espindolaa2845842006-10-05 16:48:49 +0000128 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Rafael Espindola84b19be2006-07-16 01:02:57 +0000129 }
130}
131
Rafael Espindolaa2845842006-10-05 16:48:49 +0000132class ArgumentLayout {
133 std::vector<bool> is_reg;
134 std::vector<unsigned> pos;
135 std::vector<MVT::ValueType> types;
136public:
Rafael Espindola39b5a212006-10-05 17:46:48 +0000137 ArgumentLayout(const std::vector<MVT::ValueType> &Types) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000138 types = Types;
139
140 unsigned RegNum = 0;
141 unsigned StackOffset = 0;
Rafael Espindola39b5a212006-10-05 17:46:48 +0000142 for(std::vector<MVT::ValueType>::const_iterator I = Types.begin();
Rafael Espindolaa2845842006-10-05 16:48:49 +0000143 I != Types.end();
144 ++I) {
145 MVT::ValueType VT = *I;
146 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
147 unsigned size = MVT::getSizeInBits(VT)/32;
148
149 RegNum = ((RegNum + size - 1) / size) * size;
150 if (RegNum < 4) {
151 pos.push_back(RegNum);
152 is_reg.push_back(true);
153 RegNum += size;
154 } else {
155 unsigned bytes = size * 32/8;
156 StackOffset = ((StackOffset + bytes - 1) / bytes) * bytes;
157 pos.push_back(StackOffset);
158 is_reg.push_back(false);
159 StackOffset += bytes;
160 }
161 }
162 }
163 unsigned getRegisterNum(unsigned argNum) {
164 assert(isRegister(argNum));
165 return pos[argNum];
166 }
167 unsigned getOffset(unsigned argNum) {
168 assert(isOffset(argNum));
169 return pos[argNum];
170 }
171 unsigned isRegister(unsigned argNum) {
172 assert(argNum < is_reg.size());
173 return is_reg[argNum];
174 }
175 unsigned isOffset(unsigned argNum) {
176 return !isRegister(argNum);
177 }
178 MVT::ValueType getType(unsigned argNum) {
179 assert(argNum < types.size());
180 return types[argNum];
181 }
182 unsigned getStackSize(void) {
183 int last = is_reg.size() - 1;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000184 if (last < 0)
185 return 0;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000186 if (isRegister(last))
187 return 0;
188 return getOffset(last) + MVT::getSizeInBits(getType(last))/8;
189 }
190 int lastRegArg(void) {
191 int size = is_reg.size();
192 int last = 0;
193 while(last < size && isRegister(last))
194 last++;
195 last--;
196 return last;
197 }
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000198 int lastRegNum(void) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000199 int l = lastRegArg();
200 if (l < 0)
201 return -1;
202 unsigned r = getRegisterNum(l);
203 MVT::ValueType t = getType(l);
204 assert(t == MVT::i32 || t == MVT::f32 || t == MVT::f64);
205 if (t == MVT::f64)
206 return r + 1;
207 return r;
208 }
209};
210
Rafael Espindola84b19be2006-07-16 01:02:57 +0000211// This transforms a ISD::CALL node into a
212// callseq_star <- ARMISD:CALL <- callseq_end
213// chain
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000214static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola84b19be2006-07-16 01:02:57 +0000215 SDOperand Chain = Op.getOperand(0);
216 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
217 assert(CallConv == CallingConv::C && "unknown calling convention");
218 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000219 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
220 assert(isTailCall == false && "tail call not supported");
221 SDOperand Callee = Op.getOperand(4);
222 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Rafael Espindola1a009462006-08-08 13:02:29 +0000223 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000224 static const unsigned regs[] = {
Rafael Espindolafac00a92006-07-25 20:17:20 +0000225 ARM::R0, ARM::R1, ARM::R2, ARM::R3
226 };
227
Rafael Espindolaa2845842006-10-05 16:48:49 +0000228 std::vector<MVT::ValueType> Types;
229 for (unsigned i = 0; i < NumOps; ++i) {
230 MVT::ValueType VT = Op.getOperand(5+2*i).getValueType();
231 Types.push_back(VT);
232 }
233 ArgumentLayout Layout(Types);
Rafael Espindolafac00a92006-07-25 20:17:20 +0000234
Rafael Espindolaa2845842006-10-05 16:48:49 +0000235 unsigned NumBytes = Layout.getStackSize();
236
237 Chain = DAG.getCALLSEQ_START(Chain,
238 DAG.getConstant(NumBytes, MVT::i32));
239
240 //Build a sequence of stores
241 std::vector<SDOperand> MemOpChains;
242 for (unsigned i = Layout.lastRegArg() + 1; i < NumOps; ++i) {
243 SDOperand Arg = Op.getOperand(5+2*i);
244 unsigned ArgOffset = Layout.getOffset(i);
245 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
246 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Evan Cheng786225a2006-10-05 23:01:46 +0000247 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff,
248 DAG.getSrcValue(NULL)));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000249 }
Rafael Espindola1a009462006-08-08 13:02:29 +0000250 if (!MemOpChains.empty())
Chris Lattnere2199452006-08-11 17:38:39 +0000251 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
252 &MemOpChains[0], MemOpChains.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000253
Rafael Espindola84b19be2006-07-16 01:02:57 +0000254 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
255 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
256 // node so that legalize doesn't hack it.
257 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
258 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
259
260 // If this is a direct call, pass the chain and the callee.
261 assert (Callee.Val);
262 std::vector<SDOperand> Ops;
263 Ops.push_back(Chain);
264 Ops.push_back(Callee);
265
Rafael Espindolaa2845842006-10-05 16:48:49 +0000266 // Build a sequence of copy-to-reg nodes chained together with token chain
267 // and flag operands which copy the outgoing args into the appropriate regs.
268 SDOperand InFlag;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000269 for (int i = 0, e = Layout.lastRegArg(); i <= e; ++i) {
Rafael Espindola4a408d42006-10-06 12:50:22 +0000270 SDOperand Arg = Op.getOperand(5+2*i);
271 unsigned RegNum = Layout.getRegisterNum(i);
272 unsigned Reg1 = regs[RegNum];
273 MVT::ValueType VT = Layout.getType(i);
274 assert(VT == Arg.getValueType());
275 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000276
277 // Add argument register to the end of the list so that it is known live
278 // into the call.
Rafael Espindola4a408d42006-10-06 12:50:22 +0000279 Ops.push_back(DAG.getRegister(Reg1, MVT::i32));
280 if (VT == MVT::f64) {
281 unsigned Reg2 = regs[RegNum + 1];
282 SDOperand SDReg1 = DAG.getRegister(Reg1, MVT::i32);
283 SDOperand SDReg2 = DAG.getRegister(Reg2, MVT::i32);
284
285 Ops.push_back(DAG.getRegister(Reg2, MVT::i32));
286 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola935b1f82006-10-06 20:33:26 +0000287 SDOperand Ops[] = {Chain, SDReg1, SDReg2, Arg, InFlag};
288 Chain = DAG.getNode(ARMISD::FMRRD, VTs, Ops, InFlag.Val ? 5 : 4);
Rafael Espindola4a408d42006-10-06 12:50:22 +0000289 } else {
290 if (VT == MVT::f32)
291 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg);
292 Chain = DAG.getCopyToReg(Chain, Reg1, Arg, InFlag);
293 }
294 InFlag = Chain.getValue(1);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000295 }
296
297 std::vector<MVT::ValueType> NodeTys;
298 NodeTys.push_back(MVT::Other); // Returns a chain
299 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Rafael Espindola7a53bd02006-08-09 16:41:12 +0000300
Rafael Espindola84b19be2006-07-16 01:02:57 +0000301 unsigned CallOpc = ARMISD::CALL;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000302 if (InFlag.Val)
303 Ops.push_back(InFlag);
Chris Lattner87428672006-08-11 17:22:35 +0000304 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000305 InFlag = Chain.getValue(1);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000306
Rafael Espindolafac00a92006-07-25 20:17:20 +0000307 std::vector<SDOperand> ResultVals;
308 NodeTys.clear();
309
310 // If the call has results, copy the values out of the ret val registers.
Rafael Espindola614057b2006-10-06 19:10:05 +0000311 MVT::ValueType VT = Op.Val->getValueType(0);
312 if (VT != MVT::Other) {
313 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
314 SDOperand Value;
315
316 SDOperand Value1 = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
317 Chain = Value1.getValue(1);
318 InFlag = Value1.getValue(2);
319 if (VT == MVT::i32)
320 Value = Value1;
321 if (VT == MVT::f32)
322 Value = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Value1);
323 if (VT == MVT::f64) {
324 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
325 Chain = Value2.getValue(1);
326 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
327 }
328 ResultVals.push_back(Value);
329 NodeTys.push_back(VT);
Rafael Espindolafac00a92006-07-25 20:17:20 +0000330 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000331
332 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
333 DAG.getConstant(NumBytes, MVT::i32));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000334 NodeTys.push_back(MVT::Other);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000335
Rafael Espindolafac00a92006-07-25 20:17:20 +0000336 if (ResultVals.empty())
337 return Chain;
338
339 ResultVals.push_back(Chain);
Chris Lattner87428672006-08-11 17:22:35 +0000340 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
341 ResultVals.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000342 return Res.getValue(Op.ResNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000343}
344
345static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
346 SDOperand Copy;
Rafael Espindola4b023672006-06-05 22:26:14 +0000347 SDOperand Chain = Op.getOperand(0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000348 SDOperand R0 = DAG.getRegister(ARM::R0, MVT::i32);
349 SDOperand R1 = DAG.getRegister(ARM::R1, MVT::i32);
350
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000351 switch(Op.getNumOperands()) {
352 default:
353 assert(0 && "Do not know how to return this many arguments!");
354 abort();
Rafael Espindola4b023672006-06-05 22:26:14 +0000355 case 1: {
356 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
Rafael Espindola6312da02006-08-03 22:50:11 +0000357 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
Rafael Espindola4b023672006-06-05 22:26:14 +0000358 }
Rafael Espindola27185192006-09-29 21:20:16 +0000359 case 3: {
360 SDOperand Val = Op.getOperand(1);
361 assert(Val.getValueType() == MVT::i32 ||
Rafael Espindola9e071f02006-10-02 19:30:56 +0000362 Val.getValueType() == MVT::f32 ||
363 Val.getValueType() == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000364
Rafael Espindola9e071f02006-10-02 19:30:56 +0000365 if (Val.getValueType() == MVT::f64) {
366 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
367 SDOperand Ops[] = {Chain, R0, R1, Val};
368 Copy = DAG.getNode(ARMISD::FMRRD, VTs, Ops, 4);
369 } else {
370 if (Val.getValueType() == MVT::f32)
371 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
372 Copy = DAG.getCopyToReg(Chain, R0, Val, SDOperand());
373 }
374
375 if (DAG.getMachineFunction().liveout_empty()) {
Rafael Espindola4b023672006-06-05 22:26:14 +0000376 DAG.getMachineFunction().addLiveOut(ARM::R0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000377 if (Val.getValueType() == MVT::f64)
378 DAG.getMachineFunction().addLiveOut(ARM::R1);
379 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000380 break;
Rafael Espindola27185192006-09-29 21:20:16 +0000381 }
Rafael Espindola3a02f022006-09-04 19:05:01 +0000382 case 5:
383 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
384 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
385 // If we haven't noted the R0+R1 are live out, do so now.
386 if (DAG.getMachineFunction().liveout_empty()) {
387 DAG.getMachineFunction().addLiveOut(ARM::R0);
388 DAG.getMachineFunction().addLiveOut(ARM::R1);
389 }
390 break;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000391 }
Rafael Espindola4b023672006-06-05 22:26:14 +0000392
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000393 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
394 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000395}
396
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000397static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
398 MVT::ValueType PtrVT = Op.getValueType();
399 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengc356a572006-09-12 21:04:05 +0000400 Constant *C = CP->getConstVal();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000401 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
402
403 return CPI;
404}
405
406static SDOperand LowerGlobalAddress(SDOperand Op,
407 SelectionDAG &DAG) {
408 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Rafael Espindola61369da2006-08-14 19:01:24 +0000409 int alignment = 2;
410 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000411 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr,
412 DAG.getSrcValue(NULL));
413}
414
Rafael Espindola755be9b2006-08-25 17:55:16 +0000415static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
416 unsigned VarArgsFrameIndex) {
417 // vastart just stores the address of the VarArgsFrameIndex slot into the
418 // memory location argument.
419 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
420 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Evan Cheng786225a2006-10-05 23:01:46 +0000421 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), Op.getOperand(2));
Rafael Espindola755be9b2006-08-25 17:55:16 +0000422}
423
424static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
425 int &VarArgsFrameIndex) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000426 MachineFunction &MF = DAG.getMachineFunction();
427 MachineFrameInfo *MFI = MF.getFrameInfo();
428 SSARegMap *RegMap = MF.getSSARegMap();
429 unsigned NumArgs = Op.Val->getNumValues()-1;
430 SDOperand Root = Op.getOperand(0);
431 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
432 static const unsigned REGS[] = {
433 ARM::R0, ARM::R1, ARM::R2, ARM::R3
434 };
435
436 std::vector<MVT::ValueType> Types(Op.Val->value_begin(), Op.Val->value_end() - 1);
437 ArgumentLayout Layout(Types);
438
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000439 std::vector<SDOperand> ArgValues;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000440 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000441 MVT::ValueType VT = Types[ArgNo];
Rafael Espindola4b442b52006-05-23 02:48:20 +0000442
Rafael Espindolaa2845842006-10-05 16:48:49 +0000443 SDOperand Value;
444 if (Layout.isRegister(ArgNo)) {
445 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
446 unsigned RegNum = Layout.getRegisterNum(ArgNo);
447 unsigned Reg1 = REGS[RegNum];
448 unsigned VReg1 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
449 SDOperand Value1 = DAG.getCopyFromReg(Root, VReg1, MVT::i32);
450 MF.addLiveIn(Reg1, VReg1);
451 if (VT == MVT::f64) {
452 unsigned Reg2 = REGS[RegNum + 1];
453 unsigned VReg2 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
454 SDOperand Value2 = DAG.getCopyFromReg(Root, VReg2, MVT::i32);
455 MF.addLiveIn(Reg2, VReg2);
456 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
457 } else {
458 Value = Value1;
459 if (VT == MVT::f32)
460 Value = DAG.getNode(ISD::BIT_CONVERT, VT, Value);
461 }
462 } else {
463 // If the argument is actually used, emit a load from the right stack
464 // slot.
465 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
466 unsigned Offset = Layout.getOffset(ArgNo);
467 unsigned Size = MVT::getSizeInBits(VT)/8;
468 int FI = MFI->CreateFixedObject(Size, Offset);
469 SDOperand FIN = DAG.getFrameIndex(FI, VT);
470 Value = DAG.getLoad(VT, Root, FIN, DAG.getSrcValue(NULL));
471 } else {
472 Value = DAG.getNode(ISD::UNDEF, VT);
473 }
474 }
475 ArgValues.push_back(Value);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000476 }
477
Rafael Espindolaa2845842006-10-05 16:48:49 +0000478 unsigned NextRegNum = Layout.lastRegNum() + 1;
479
Rafael Espindola755be9b2006-08-25 17:55:16 +0000480 if (isVarArg) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000481 //If this function is vararg we must store the remaing
482 //registers so that they can be acessed with va_start
Rafael Espindola755be9b2006-08-25 17:55:16 +0000483 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000484 -16 + NextRegNum * 4);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000485
Rafael Espindola755be9b2006-08-25 17:55:16 +0000486 SmallVector<SDOperand, 4> MemOps;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000487 for (unsigned RegNo = NextRegNum; RegNo < 4; ++RegNo) {
488 int RegOffset = - (4 - RegNo) * 4;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000489 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000490 RegOffset);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000491 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
492
Rafael Espindolaa2845842006-10-05 16:48:49 +0000493 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
494 MF.addLiveIn(REGS[RegNo], VReg);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000495
496 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
Evan Cheng786225a2006-10-05 23:01:46 +0000497 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN,
498 DAG.getSrcValue(NULL));
Rafael Espindola755be9b2006-08-25 17:55:16 +0000499 MemOps.push_back(Store);
500 }
501 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
502 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000503
504 ArgValues.push_back(Root);
505
506 // Return the new list of results.
507 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
508 Op.Val->value_end());
Chris Lattner87428672006-08-11 17:22:35 +0000509 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
Rafael Espindoladc124a22006-05-18 21:45:49 +0000510}
511
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000512static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
513 SDOperand LHS = Op.getOperand(0);
514 SDOperand RHS = Op.getOperand(1);
515 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
516 SDOperand TrueVal = Op.getOperand(2);
517 SDOperand FalseVal = Op.getOperand(3);
Rafael Espindolacdda88c2006-08-24 17:19:08 +0000518 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000519
520 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000521 return DAG.getNode(ARMISD::SELECT, MVT::i32, TrueVal, FalseVal, ARMCC, Cmp);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000522}
523
Rafael Espindola687bc492006-08-24 13:45:55 +0000524static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
525 SDOperand Chain = Op.getOperand(0);
526 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
527 SDOperand LHS = Op.getOperand(2);
528 SDOperand RHS = Op.getOperand(3);
529 SDOperand Dest = Op.getOperand(4);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000530 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
Rafael Espindola687bc492006-08-24 13:45:55 +0000531
532 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000533 return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, ARMCC, Cmp);
Rafael Espindola687bc492006-08-24 13:45:55 +0000534}
535
Rafael Espindola27185192006-09-29 21:20:16 +0000536static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola9e071f02006-10-02 19:30:56 +0000537 SDOperand IntVal = Op.getOperand(0);
Rafael Espindola27185192006-09-29 21:20:16 +0000538 assert(IntVal.getValueType() == MVT::i32);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000539 MVT::ValueType vt = Op.getValueType();
540 assert(vt == MVT::f32 ||
541 vt == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000542
543 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000544 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FSITOS : ARMISD::FSITOD;
545 return DAG.getNode(op, vt, Tmp);
Rafael Espindola27185192006-09-29 21:20:16 +0000546}
547
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000548SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
549 switch (Op.getOpcode()) {
550 default:
551 assert(0 && "Should not custom lower this!");
Rafael Espindola1c8f0532006-05-15 22:34:39 +0000552 abort();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000553 case ISD::ConstantPool:
554 return LowerConstantPool(Op, DAG);
555 case ISD::GlobalAddress:
556 return LowerGlobalAddress(Op, DAG);
Rafael Espindola27185192006-09-29 21:20:16 +0000557 case ISD::SINT_TO_FP:
558 return LowerSINT_TO_FP(Op, DAG);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000559 case ISD::FORMAL_ARGUMENTS:
Rafael Espindola755be9b2006-08-25 17:55:16 +0000560 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000561 case ISD::CALL:
562 return LowerCALL(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000563 case ISD::RET:
564 return LowerRET(Op, DAG);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000565 case ISD::SELECT_CC:
566 return LowerSELECT_CC(Op, DAG);
Rafael Espindola687bc492006-08-24 13:45:55 +0000567 case ISD::BR_CC:
568 return LowerBR_CC(Op, DAG);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000569 case ISD::VASTART:
570 return LowerVASTART(Op, DAG, VarArgsFrameIndex);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000571 }
572}
573
574//===----------------------------------------------------------------------===//
575// Instruction Selector Implementation
576//===----------------------------------------------------------------------===//
577
578//===--------------------------------------------------------------------===//
579/// ARMDAGToDAGISel - ARM specific code to select ARM machine
580/// instructions for SelectionDAG operations.
581///
582namespace {
583class ARMDAGToDAGISel : public SelectionDAGISel {
584 ARMTargetLowering Lowering;
585
586public:
587 ARMDAGToDAGISel(TargetMachine &TM)
588 : SelectionDAGISel(Lowering), Lowering(TM) {
589 }
590
Evan Cheng9ade2182006-08-26 05:34:46 +0000591 SDNode *Select(SDOperand Op);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000592 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000593 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000594 bool SelectAddrMode1(SDOperand N, SDOperand &Arg, SDOperand &Shift,
595 SDOperand &ShiftType);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000596
597 // Include the pieces autogenerated from the target description.
598#include "ARMGenDAGISel.inc"
599};
600
601void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
602 DEBUG(BB->dump());
603
604 DAG.setRoot(SelectRoot(DAG.getRoot()));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000605 DAG.RemoveDeadNodes();
606
607 ScheduleAndEmitDAG(DAG);
608}
609
Rafael Espindola61369da2006-08-14 19:01:24 +0000610static bool isInt12Immediate(SDNode *N, short &Imm) {
611 if (N->getOpcode() != ISD::Constant)
612 return false;
613
614 int32_t t = cast<ConstantSDNode>(N)->getValue();
Rafael Espindola7246d332006-09-21 11:29:52 +0000615 int max = 1<<12;
Rafael Espindola61369da2006-08-14 19:01:24 +0000616 int min = -max;
617 if (t > min && t < max) {
618 Imm = t;
619 return true;
620 }
621 else
622 return false;
623}
624
625static bool isInt12Immediate(SDOperand Op, short &Imm) {
626 return isInt12Immediate(Op.Val, Imm);
627}
628
Rafael Espindola7246d332006-09-21 11:29:52 +0000629static uint32_t rotateL(uint32_t x) {
630 uint32_t bit31 = (x & (1 << 31)) >> 31;
631 uint32_t t = x << 1;
632 return t | bit31;
633}
634
635static bool isUInt8Immediate(uint32_t x) {
636 return x < (1 << 8);
637}
638
639static bool isRotInt8Immediate(uint32_t x) {
640 int r;
641 for (r = 0; r < 16; r++) {
642 if (isUInt8Immediate(x))
643 return true;
644 x = rotateL(rotateL(x));
645 }
646 return false;
647}
648
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000649bool ARMDAGToDAGISel::SelectAddrMode1(SDOperand N,
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000650 SDOperand &Arg,
651 SDOperand &Shift,
652 SDOperand &ShiftType) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000653 switch(N.getOpcode()) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000654 case ISD::Constant: {
Rafael Espindola7246d332006-09-21 11:29:52 +0000655 uint32_t val = cast<ConstantSDNode>(N)->getValue();
656 if(!isRotInt8Immediate(val)) {
657 const Type *t = MVT::getTypeForValueType(MVT::i32);
658 Constant *C = ConstantUInt::get(t, val);
659 int alignment = 2;
660 SDOperand Addr = CurDAG->getTargetConstantPool(C, MVT::i32, alignment);
661 SDOperand Z = CurDAG->getTargetConstant(0, MVT::i32);
662 SDNode *n = CurDAG->getTargetNode(ARM::ldr, MVT::i32, Z, Addr);
663 Arg = SDOperand(n, 0);
664 } else
665 Arg = CurDAG->getTargetConstant(val, MVT::i32);
666
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000667 Shift = CurDAG->getTargetConstant(0, MVT::i32);
668 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000669 return true;
670 }
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000671 case ISD::SRA:
672 Arg = N.getOperand(0);
673 Shift = N.getOperand(1);
674 ShiftType = CurDAG->getTargetConstant(ARMShift::ASR, MVT::i32);
675 return true;
676 case ISD::SRL:
677 Arg = N.getOperand(0);
678 Shift = N.getOperand(1);
679 ShiftType = CurDAG->getTargetConstant(ARMShift::LSR, MVT::i32);
680 return true;
681 case ISD::SHL:
682 Arg = N.getOperand(0);
683 Shift = N.getOperand(1);
684 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
685 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000686 }
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000687
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000688 Arg = N;
689 Shift = CurDAG->getTargetConstant(0, MVT::i32);
690 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000691 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000692}
693
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000694//register plus/minus 12 bit offset
695bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
696 SDOperand &Base) {
Rafael Espindolaf3a335c2006-08-17 17:09:40 +0000697 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
698 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
699 Offset = CurDAG->getTargetConstant(0, MVT::i32);
700 return true;
701 }
Rafael Espindola61369da2006-08-14 19:01:24 +0000702 if (N.getOpcode() == ISD::ADD) {
703 short imm = 0;
704 if (isInt12Immediate(N.getOperand(1), imm)) {
705 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
706 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
707 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
708 } else {
709 Base = N.getOperand(0);
710 }
711 return true; // [r+i]
712 }
713 }
714
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000715 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000716 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
717 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
718 }
719 else
720 Base = N;
721 return true; //any address fits in a register
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000722}
723
Evan Cheng9ade2182006-08-26 05:34:46 +0000724SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000725 SDNode *N = Op.Val;
726
727 switch (N->getOpcode()) {
728 default:
Evan Cheng9ade2182006-08-26 05:34:46 +0000729 return SelectCode(Op);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000730 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000731 }
Evan Cheng64a752f2006-08-11 09:08:15 +0000732 return NULL;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000733}
734
735} // end anonymous namespace
736
737/// createARMISelDag - This pass converts a legalized DAG into a
738/// ARM-specific DAG, ready for instruction scheduling.
739///
740FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
741 return new ARMDAGToDAGISel(TM);
742}