blob: 0423cc8c6ccf4306d800c03e3806beff0253fbd4 [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 Espindolab47e1d02006-10-10 18:55:14 +000052 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Rafael Espindola27185192006-09-29 21:20:16 +000053 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Rafael Espindola3717ca92006-08-20 01:49:49 +000054
Rafael Espindola493a7fc2006-10-10 20:38:57 +000055 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +000056 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
57
Rafael Espindola06c1e7e2006-08-01 12:58:43 +000058 setOperationAction(ISD::RET, MVT::Other, Custom);
59 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
60 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Rafael Espindola341b8642006-08-04 12:48:42 +000061
Rafael Espindola48bc9fb2006-10-09 16:28:33 +000062 setOperationAction(ISD::SELECT, MVT::i32, Expand);
63
Rafael Espindola3c000bf2006-08-21 22:00:32 +000064 setOperationAction(ISD::SETCC, MVT::i32, Expand);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +000065 setOperationAction(ISD::SETCC, MVT::f32, Expand);
66 setOperationAction(ISD::SETCC, MVT::f64, Expand);
67
Rafael Espindola3c000bf2006-08-21 22:00:32 +000068 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
Rafael Espindola687bc492006-08-24 13:45:55 +000069 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +000070 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
71 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Rafael Espindola3c000bf2006-08-21 22:00:32 +000072
Rafael Espindola755be9b2006-08-25 17:55:16 +000073 setOperationAction(ISD::VASTART, MVT::Other, Custom);
74 setOperationAction(ISD::VAEND, MVT::Other, Expand);
75
Rafael Espindolacd71da52006-10-03 17:27:58 +000076 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
77 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
78
Rafael Espindola341b8642006-08-04 12:48:42 +000079 setSchedulingPreference(SchedulingForRegPressure);
Rafael Espindola3717ca92006-08-20 01:49:49 +000080 computeRegisterProperties();
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000081}
82
Rafael Espindola84b19be2006-07-16 01:02:57 +000083namespace llvm {
84 namespace ARMISD {
85 enum NodeType {
86 // Start the numbering where the builting ops and target ops leave off.
87 FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
88 /// CALL - A direct function call.
Rafael Espindolaf4fda802006-08-03 17:02:20 +000089 CALL,
90
91 /// Return with a flag operand.
Rafael Espindola3c000bf2006-08-21 22:00:32 +000092 RET_FLAG,
93
94 CMP,
Rafael Espindola42b62f32006-10-13 13:14:59 +000095 CMPE,
Rafael Espindola3c000bf2006-08-21 22:00:32 +000096
Rafael Espindola687bc492006-08-24 13:45:55 +000097 SELECT,
98
Rafael Espindola27185192006-09-29 21:20:16 +000099 BR,
100
Rafael Espindola9e071f02006-10-02 19:30:56 +0000101 FSITOS,
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000102 FTOSIS,
Rafael Espindola9e071f02006-10-02 19:30:56 +0000103
104 FSITOD,
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000105 FTOSID,
Rafael Espindola9e071f02006-10-02 19:30:56 +0000106
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000107 FUITOS,
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000108 FTOUIS,
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000109
110 FUITOD,
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000111 FTOUID,
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000112
Rafael Espindolaa2845842006-10-05 16:48:49 +0000113 FMRRD,
114
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000115 FMDRR,
116
117 FMSTAT
Rafael Espindola84b19be2006-07-16 01:02:57 +0000118 };
119 }
120}
121
Rafael Espindola42b62f32006-10-13 13:14:59 +0000122/// DAGFPCCToARMCC - Convert a DAG fp condition code to an ARM CC
123static ARMCC::CondCodes DAGFPCCToARMCC(ISD::CondCode CC) {
Rafael Espindola6f602de2006-08-24 16:13:15 +0000124 switch (CC) {
Rafael Espindolaebdabda2006-09-21 13:06:26 +0000125 default:
Rafael Espindola42b62f32006-10-13 13:14:59 +0000126 assert(0 && "Unknown fp condition code!");
127// For the following conditions we use a comparison that throws exceptions,
128// so we may assume that V=0
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000129 case ISD::SETOEQ: return ARMCC::EQ;
Rafael Espindola42b62f32006-10-13 13:14:59 +0000130 case ISD::SETOGT: return ARMCC::GT;
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000131 case ISD::SETOGE: return ARMCC::GE;
Rafael Espindola42b62f32006-10-13 13:14:59 +0000132 case ISD::SETOLT: return ARMCC::LT;
133 case ISD::SETOLE: return ARMCC::LE;
134 case ISD::SETONE: return ARMCC::NE;
135// For the following conditions the result is undefined in case of a nan,
136// so we may assume that V=0
137 case ISD::SETEQ: return ARMCC::EQ;
138 case ISD::SETGT: return ARMCC::GT;
139 case ISD::SETGE: return ARMCC::GE;
140 case ISD::SETLT: return ARMCC::LT;
141 case ISD::SETLE: return ARMCC::LE;
142 case ISD::SETNE: return ARMCC::NE;
143// For the following we may not assume anything
144// SETO = N | Z | !C | !V = ???
145// SETUO = (!N & !Z & C & V) = ???
146// SETUEQ = (!N & !Z & C & V) | Z = ???
147// SETUGT = (!N & !Z & C & V) | (!Z & !N) = ???
148// SETUGE = (!N & !Z & C & V) | !N = !N = PL
149 case ISD::SETUGE: return ARMCC::PL;
150// SETULT = (!N & !Z & C & V) | N = ???
151// SETULE = (!N & !Z & C & V) | Z | N = ???
152// SETUNE = (!N & !Z & C & V) | !Z = !Z = NE
153 case ISD::SETUNE: return ARMCC::NE;
154 }
155}
156
157/// DAGIntCCToARMCC - Convert a DAG integer condition code to an ARM CC
158static ARMCC::CondCodes DAGIntCCToARMCC(ISD::CondCode CC) {
159 switch (CC) {
160 default:
161 assert(0 && "Unknown integer condition code!");
162 case ISD::SETEQ: return ARMCC::EQ;
163 case ISD::SETNE: return ARMCC::NE;
164 case ISD::SETLT: return ARMCC::LT;
165 case ISD::SETLE: return ARMCC::LE;
166 case ISD::SETGT: return ARMCC::GT;
167 case ISD::SETGE: return ARMCC::GE;
Rafael Espindolabc4cec92006-09-03 13:19:16 +0000168 case ISD::SETULT: return ARMCC::CC;
Rafael Espindola42b62f32006-10-13 13:14:59 +0000169 case ISD::SETULE: return ARMCC::LS;
170 case ISD::SETUGT: return ARMCC::HI;
171 case ISD::SETUGE: return ARMCC::CS;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000172 }
173}
174
Rafael Espindola84b19be2006-07-16 01:02:57 +0000175const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
176 switch (Opcode) {
177 default: return 0;
178 case ARMISD::CALL: return "ARMISD::CALL";
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000179 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000180 case ARMISD::SELECT: return "ARMISD::SELECT";
181 case ARMISD::CMP: return "ARMISD::CMP";
Rafael Espindola42b62f32006-10-13 13:14:59 +0000182 case ARMISD::CMPE: return "ARMISD::CMPE";
Rafael Espindola687bc492006-08-24 13:45:55 +0000183 case ARMISD::BR: return "ARMISD::BR";
Rafael Espindola27185192006-09-29 21:20:16 +0000184 case ARMISD::FSITOS: return "ARMISD::FSITOS";
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000185 case ARMISD::FTOSIS: return "ARMISD::FTOSIS";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000186 case ARMISD::FSITOD: return "ARMISD::FSITOD";
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000187 case ARMISD::FTOSID: return "ARMISD::FTOSID";
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000188 case ARMISD::FUITOS: return "ARMISD::FUITOS";
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000189 case ARMISD::FTOUIS: return "ARMISD::FTOUIS";
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000190 case ARMISD::FUITOD: return "ARMISD::FUITOD";
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000191 case ARMISD::FTOUID: return "ARMISD::FTOUID";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000192 case ARMISD::FMRRD: return "ARMISD::FMRRD";
Rafael Espindolaa2845842006-10-05 16:48:49 +0000193 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000194 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Rafael Espindola84b19be2006-07-16 01:02:57 +0000195 }
196}
197
Rafael Espindolaa2845842006-10-05 16:48:49 +0000198class ArgumentLayout {
199 std::vector<bool> is_reg;
200 std::vector<unsigned> pos;
201 std::vector<MVT::ValueType> types;
202public:
Rafael Espindola39b5a212006-10-05 17:46:48 +0000203 ArgumentLayout(const std::vector<MVT::ValueType> &Types) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000204 types = Types;
205
206 unsigned RegNum = 0;
207 unsigned StackOffset = 0;
Rafael Espindola39b5a212006-10-05 17:46:48 +0000208 for(std::vector<MVT::ValueType>::const_iterator I = Types.begin();
Rafael Espindolaa2845842006-10-05 16:48:49 +0000209 I != Types.end();
210 ++I) {
211 MVT::ValueType VT = *I;
212 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
213 unsigned size = MVT::getSizeInBits(VT)/32;
214
215 RegNum = ((RegNum + size - 1) / size) * size;
216 if (RegNum < 4) {
217 pos.push_back(RegNum);
218 is_reg.push_back(true);
219 RegNum += size;
220 } else {
221 unsigned bytes = size * 32/8;
222 StackOffset = ((StackOffset + bytes - 1) / bytes) * bytes;
223 pos.push_back(StackOffset);
224 is_reg.push_back(false);
225 StackOffset += bytes;
226 }
227 }
228 }
229 unsigned getRegisterNum(unsigned argNum) {
230 assert(isRegister(argNum));
231 return pos[argNum];
232 }
233 unsigned getOffset(unsigned argNum) {
234 assert(isOffset(argNum));
235 return pos[argNum];
236 }
237 unsigned isRegister(unsigned argNum) {
238 assert(argNum < is_reg.size());
239 return is_reg[argNum];
240 }
241 unsigned isOffset(unsigned argNum) {
242 return !isRegister(argNum);
243 }
244 MVT::ValueType getType(unsigned argNum) {
245 assert(argNum < types.size());
246 return types[argNum];
247 }
248 unsigned getStackSize(void) {
249 int last = is_reg.size() - 1;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000250 if (last < 0)
251 return 0;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000252 if (isRegister(last))
253 return 0;
254 return getOffset(last) + MVT::getSizeInBits(getType(last))/8;
255 }
256 int lastRegArg(void) {
257 int size = is_reg.size();
258 int last = 0;
259 while(last < size && isRegister(last))
260 last++;
261 last--;
262 return last;
263 }
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000264 int lastRegNum(void) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000265 int l = lastRegArg();
266 if (l < 0)
267 return -1;
268 unsigned r = getRegisterNum(l);
269 MVT::ValueType t = getType(l);
270 assert(t == MVT::i32 || t == MVT::f32 || t == MVT::f64);
271 if (t == MVT::f64)
272 return r + 1;
273 return r;
274 }
275};
276
Rafael Espindola84b19be2006-07-16 01:02:57 +0000277// This transforms a ISD::CALL node into a
278// callseq_star <- ARMISD:CALL <- callseq_end
279// chain
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000280static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola84b19be2006-07-16 01:02:57 +0000281 SDOperand Chain = Op.getOperand(0);
282 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
283 assert(CallConv == CallingConv::C && "unknown calling convention");
284 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000285 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000286 SDOperand Callee = Op.getOperand(4);
287 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Rafael Espindola1a009462006-08-08 13:02:29 +0000288 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000289 static const unsigned regs[] = {
Rafael Espindolafac00a92006-07-25 20:17:20 +0000290 ARM::R0, ARM::R1, ARM::R2, ARM::R3
291 };
292
Rafael Espindolaa2845842006-10-05 16:48:49 +0000293 std::vector<MVT::ValueType> Types;
294 for (unsigned i = 0; i < NumOps; ++i) {
295 MVT::ValueType VT = Op.getOperand(5+2*i).getValueType();
296 Types.push_back(VT);
297 }
298 ArgumentLayout Layout(Types);
Rafael Espindolafac00a92006-07-25 20:17:20 +0000299
Rafael Espindolaa2845842006-10-05 16:48:49 +0000300 unsigned NumBytes = Layout.getStackSize();
301
302 Chain = DAG.getCALLSEQ_START(Chain,
303 DAG.getConstant(NumBytes, MVT::i32));
304
305 //Build a sequence of stores
306 std::vector<SDOperand> MemOpChains;
307 for (unsigned i = Layout.lastRegArg() + 1; i < NumOps; ++i) {
308 SDOperand Arg = Op.getOperand(5+2*i);
309 unsigned ArgOffset = Layout.getOffset(i);
310 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
311 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Evan Cheng786225a2006-10-05 23:01:46 +0000312 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff,
313 DAG.getSrcValue(NULL)));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000314 }
Rafael Espindola1a009462006-08-08 13:02:29 +0000315 if (!MemOpChains.empty())
Chris Lattnere2199452006-08-11 17:38:39 +0000316 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
317 &MemOpChains[0], MemOpChains.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000318
Rafael Espindola84b19be2006-07-16 01:02:57 +0000319 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
320 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
321 // node so that legalize doesn't hack it.
322 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
323 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
324
325 // If this is a direct call, pass the chain and the callee.
326 assert (Callee.Val);
327 std::vector<SDOperand> Ops;
328 Ops.push_back(Chain);
329 Ops.push_back(Callee);
330
Rafael Espindolaa2845842006-10-05 16:48:49 +0000331 // Build a sequence of copy-to-reg nodes chained together with token chain
332 // and flag operands which copy the outgoing args into the appropriate regs.
333 SDOperand InFlag;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000334 for (int i = 0, e = Layout.lastRegArg(); i <= e; ++i) {
Rafael Espindola4a408d42006-10-06 12:50:22 +0000335 SDOperand Arg = Op.getOperand(5+2*i);
336 unsigned RegNum = Layout.getRegisterNum(i);
337 unsigned Reg1 = regs[RegNum];
338 MVT::ValueType VT = Layout.getType(i);
339 assert(VT == Arg.getValueType());
340 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000341
342 // Add argument register to the end of the list so that it is known live
343 // into the call.
Rafael Espindola4a408d42006-10-06 12:50:22 +0000344 Ops.push_back(DAG.getRegister(Reg1, MVT::i32));
345 if (VT == MVT::f64) {
346 unsigned Reg2 = regs[RegNum + 1];
347 SDOperand SDReg1 = DAG.getRegister(Reg1, MVT::i32);
348 SDOperand SDReg2 = DAG.getRegister(Reg2, MVT::i32);
349
350 Ops.push_back(DAG.getRegister(Reg2, MVT::i32));
351 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola935b1f82006-10-06 20:33:26 +0000352 SDOperand Ops[] = {Chain, SDReg1, SDReg2, Arg, InFlag};
353 Chain = DAG.getNode(ARMISD::FMRRD, VTs, Ops, InFlag.Val ? 5 : 4);
Rafael Espindola4a408d42006-10-06 12:50:22 +0000354 } else {
355 if (VT == MVT::f32)
356 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg);
357 Chain = DAG.getCopyToReg(Chain, Reg1, Arg, InFlag);
358 }
359 InFlag = Chain.getValue(1);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000360 }
361
362 std::vector<MVT::ValueType> NodeTys;
363 NodeTys.push_back(MVT::Other); // Returns a chain
364 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Rafael Espindola7a53bd02006-08-09 16:41:12 +0000365
Rafael Espindola84b19be2006-07-16 01:02:57 +0000366 unsigned CallOpc = ARMISD::CALL;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000367 if (InFlag.Val)
368 Ops.push_back(InFlag);
Chris Lattner87428672006-08-11 17:22:35 +0000369 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000370 InFlag = Chain.getValue(1);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000371
Rafael Espindolafac00a92006-07-25 20:17:20 +0000372 std::vector<SDOperand> ResultVals;
373 NodeTys.clear();
374
375 // If the call has results, copy the values out of the ret val registers.
Rafael Espindola614057b2006-10-06 19:10:05 +0000376 MVT::ValueType VT = Op.Val->getValueType(0);
377 if (VT != MVT::Other) {
378 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
Rafael Espindola614057b2006-10-06 19:10:05 +0000379
380 SDOperand Value1 = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
381 Chain = Value1.getValue(1);
382 InFlag = Value1.getValue(2);
Rafael Espindola26a76d12006-10-13 16:47:22 +0000383 NodeTys.push_back(VT);
384 if (VT == MVT::i32) {
385 ResultVals.push_back(Value1);
386 if (Op.Val->getValueType(1) == MVT::i32) {
387 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
388 Chain = Value2.getValue(1);
389 ResultVals.push_back(Value2);
390 NodeTys.push_back(VT);
391 }
392 }
393 if (VT == MVT::f32) {
394 SDOperand Value = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Value1);
395 ResultVals.push_back(Value);
396 }
Rafael Espindola614057b2006-10-06 19:10:05 +0000397 if (VT == MVT::f64) {
398 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
399 Chain = Value2.getValue(1);
Rafael Espindola26a76d12006-10-13 16:47:22 +0000400 SDOperand Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
401 ResultVals.push_back(Value);
Rafael Espindola614057b2006-10-06 19:10:05 +0000402 }
Rafael Espindolafac00a92006-07-25 20:17:20 +0000403 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000404
405 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
406 DAG.getConstant(NumBytes, MVT::i32));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000407 NodeTys.push_back(MVT::Other);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000408
Rafael Espindolafac00a92006-07-25 20:17:20 +0000409 if (ResultVals.empty())
410 return Chain;
411
412 ResultVals.push_back(Chain);
Chris Lattner87428672006-08-11 17:22:35 +0000413 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
414 ResultVals.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000415 return Res.getValue(Op.ResNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000416}
417
418static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
419 SDOperand Copy;
Rafael Espindola4b023672006-06-05 22:26:14 +0000420 SDOperand Chain = Op.getOperand(0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000421 SDOperand R0 = DAG.getRegister(ARM::R0, MVT::i32);
422 SDOperand R1 = DAG.getRegister(ARM::R1, MVT::i32);
423
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000424 switch(Op.getNumOperands()) {
425 default:
426 assert(0 && "Do not know how to return this many arguments!");
427 abort();
Rafael Espindola4b023672006-06-05 22:26:14 +0000428 case 1: {
429 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
Rafael Espindola6312da02006-08-03 22:50:11 +0000430 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
Rafael Espindola4b023672006-06-05 22:26:14 +0000431 }
Rafael Espindola27185192006-09-29 21:20:16 +0000432 case 3: {
433 SDOperand Val = Op.getOperand(1);
434 assert(Val.getValueType() == MVT::i32 ||
Rafael Espindola9e071f02006-10-02 19:30:56 +0000435 Val.getValueType() == MVT::f32 ||
436 Val.getValueType() == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000437
Rafael Espindola9e071f02006-10-02 19:30:56 +0000438 if (Val.getValueType() == MVT::f64) {
439 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
440 SDOperand Ops[] = {Chain, R0, R1, Val};
441 Copy = DAG.getNode(ARMISD::FMRRD, VTs, Ops, 4);
442 } else {
443 if (Val.getValueType() == MVT::f32)
444 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
445 Copy = DAG.getCopyToReg(Chain, R0, Val, SDOperand());
446 }
447
448 if (DAG.getMachineFunction().liveout_empty()) {
Rafael Espindola4b023672006-06-05 22:26:14 +0000449 DAG.getMachineFunction().addLiveOut(ARM::R0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000450 if (Val.getValueType() == MVT::f64)
451 DAG.getMachineFunction().addLiveOut(ARM::R1);
452 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000453 break;
Rafael Espindola27185192006-09-29 21:20:16 +0000454 }
Rafael Espindola3a02f022006-09-04 19:05:01 +0000455 case 5:
456 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
457 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
458 // If we haven't noted the R0+R1 are live out, do so now.
459 if (DAG.getMachineFunction().liveout_empty()) {
460 DAG.getMachineFunction().addLiveOut(ARM::R0);
461 DAG.getMachineFunction().addLiveOut(ARM::R1);
462 }
463 break;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000464 }
Rafael Espindola4b023672006-06-05 22:26:14 +0000465
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000466 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
467 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000468}
469
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000470static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
471 MVT::ValueType PtrVT = Op.getValueType();
472 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengc356a572006-09-12 21:04:05 +0000473 Constant *C = CP->getConstVal();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000474 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
475
476 return CPI;
477}
478
479static SDOperand LowerGlobalAddress(SDOperand Op,
480 SelectionDAG &DAG) {
481 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Rafael Espindola61369da2006-08-14 19:01:24 +0000482 int alignment = 2;
483 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
Evan Cheng466685d2006-10-09 20:57:25 +0000484 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr, NULL, 0);
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000485}
486
Rafael Espindola755be9b2006-08-25 17:55:16 +0000487static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
488 unsigned VarArgsFrameIndex) {
489 // vastart just stores the address of the VarArgsFrameIndex slot into the
490 // memory location argument.
491 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
492 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Evan Cheng786225a2006-10-05 23:01:46 +0000493 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), Op.getOperand(2));
Rafael Espindola755be9b2006-08-25 17:55:16 +0000494}
495
496static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
497 int &VarArgsFrameIndex) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000498 MachineFunction &MF = DAG.getMachineFunction();
499 MachineFrameInfo *MFI = MF.getFrameInfo();
500 SSARegMap *RegMap = MF.getSSARegMap();
501 unsigned NumArgs = Op.Val->getNumValues()-1;
502 SDOperand Root = Op.getOperand(0);
503 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
504 static const unsigned REGS[] = {
505 ARM::R0, ARM::R1, ARM::R2, ARM::R3
506 };
507
508 std::vector<MVT::ValueType> Types(Op.Val->value_begin(), Op.Val->value_end() - 1);
509 ArgumentLayout Layout(Types);
510
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000511 std::vector<SDOperand> ArgValues;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000512 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000513 MVT::ValueType VT = Types[ArgNo];
Rafael Espindola4b442b52006-05-23 02:48:20 +0000514
Rafael Espindolaa2845842006-10-05 16:48:49 +0000515 SDOperand Value;
516 if (Layout.isRegister(ArgNo)) {
517 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
518 unsigned RegNum = Layout.getRegisterNum(ArgNo);
519 unsigned Reg1 = REGS[RegNum];
520 unsigned VReg1 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
521 SDOperand Value1 = DAG.getCopyFromReg(Root, VReg1, MVT::i32);
522 MF.addLiveIn(Reg1, VReg1);
523 if (VT == MVT::f64) {
524 unsigned Reg2 = REGS[RegNum + 1];
525 unsigned VReg2 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
526 SDOperand Value2 = DAG.getCopyFromReg(Root, VReg2, MVT::i32);
527 MF.addLiveIn(Reg2, VReg2);
528 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
529 } else {
530 Value = Value1;
531 if (VT == MVT::f32)
532 Value = DAG.getNode(ISD::BIT_CONVERT, VT, Value);
533 }
534 } else {
535 // If the argument is actually used, emit a load from the right stack
536 // slot.
537 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
538 unsigned Offset = Layout.getOffset(ArgNo);
539 unsigned Size = MVT::getSizeInBits(VT)/8;
540 int FI = MFI->CreateFixedObject(Size, Offset);
541 SDOperand FIN = DAG.getFrameIndex(FI, VT);
Evan Cheng466685d2006-10-09 20:57:25 +0000542 Value = DAG.getLoad(VT, Root, FIN, NULL, 0);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000543 } else {
544 Value = DAG.getNode(ISD::UNDEF, VT);
545 }
546 }
547 ArgValues.push_back(Value);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000548 }
549
Rafael Espindolaa2845842006-10-05 16:48:49 +0000550 unsigned NextRegNum = Layout.lastRegNum() + 1;
551
Rafael Espindola755be9b2006-08-25 17:55:16 +0000552 if (isVarArg) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000553 //If this function is vararg we must store the remaing
554 //registers so that they can be acessed with va_start
Rafael Espindola755be9b2006-08-25 17:55:16 +0000555 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000556 -16 + NextRegNum * 4);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000557
Rafael Espindola755be9b2006-08-25 17:55:16 +0000558 SmallVector<SDOperand, 4> MemOps;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000559 for (unsigned RegNo = NextRegNum; RegNo < 4; ++RegNo) {
560 int RegOffset = - (4 - RegNo) * 4;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000561 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000562 RegOffset);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000563 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
564
Rafael Espindolaa2845842006-10-05 16:48:49 +0000565 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
566 MF.addLiveIn(REGS[RegNo], VReg);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000567
568 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
Evan Cheng786225a2006-10-05 23:01:46 +0000569 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN,
570 DAG.getSrcValue(NULL));
Rafael Espindola755be9b2006-08-25 17:55:16 +0000571 MemOps.push_back(Store);
572 }
573 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
574 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000575
576 ArgValues.push_back(Root);
577
578 // Return the new list of results.
579 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
580 Op.Val->value_end());
Chris Lattner87428672006-08-11 17:22:35 +0000581 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
Rafael Espindoladc124a22006-05-18 21:45:49 +0000582}
583
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000584static SDOperand GetCMP(ISD::CondCode CC, SDOperand LHS, SDOperand RHS,
585 SelectionDAG &DAG) {
586 MVT::ValueType vt = LHS.getValueType();
Rafael Espindola0d9fe762006-10-10 16:33:47 +0000587 assert(vt == MVT::i32 || vt == MVT::f32 || vt == MVT::f64);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000588
Rafael Espindola42b62f32006-10-13 13:14:59 +0000589 bool isOrderedFloat = (vt == MVT::f32 || vt == MVT::f64) &&
590 (CC >= ISD::SETOEQ && CC <= ISD::SETONE);
591
592 SDOperand Cmp;
593 if (isOrderedFloat) {
594 Cmp = DAG.getNode(ARMISD::CMPE, MVT::Flag, LHS, RHS);
595 } else {
596 Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
597 }
598
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000599 if (vt != MVT::i32)
600 Cmp = DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp);
601 return Cmp;
602}
603
Rafael Espindola42b62f32006-10-13 13:14:59 +0000604static SDOperand GetARMCC(ISD::CondCode CC, MVT::ValueType vt,
605 SelectionDAG &DAG) {
606 assert(vt == MVT::i32 || vt == MVT::f32 || vt == MVT::f64);
607 if (vt == MVT::i32)
608 return DAG.getConstant(DAGIntCCToARMCC(CC), MVT::i32);
609 else
610 return DAG.getConstant(DAGFPCCToARMCC(CC), MVT::i32);
611}
612
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000613static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
614 SDOperand LHS = Op.getOperand(0);
615 SDOperand RHS = Op.getOperand(1);
616 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
617 SDOperand TrueVal = Op.getOperand(2);
618 SDOperand FalseVal = Op.getOperand(3);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000619 SDOperand Cmp = GetCMP(CC, LHS, RHS, DAG);
Rafael Espindola42b62f32006-10-13 13:14:59 +0000620 SDOperand ARMCC = GetARMCC(CC, LHS.getValueType(), DAG);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000621 return DAG.getNode(ARMISD::SELECT, MVT::i32, TrueVal, FalseVal, ARMCC, Cmp);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000622}
623
Rafael Espindola687bc492006-08-24 13:45:55 +0000624static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
625 SDOperand Chain = Op.getOperand(0);
626 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
627 SDOperand LHS = Op.getOperand(2);
628 SDOperand RHS = Op.getOperand(3);
629 SDOperand Dest = Op.getOperand(4);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000630 SDOperand Cmp = GetCMP(CC, LHS, RHS, DAG);
Rafael Espindola42b62f32006-10-13 13:14:59 +0000631 SDOperand ARMCC = GetARMCC(CC, LHS.getValueType(), DAG);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000632 return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, ARMCC, Cmp);
Rafael Espindola687bc492006-08-24 13:45:55 +0000633}
634
Rafael Espindola27185192006-09-29 21:20:16 +0000635static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola9e071f02006-10-02 19:30:56 +0000636 SDOperand IntVal = Op.getOperand(0);
Rafael Espindola27185192006-09-29 21:20:16 +0000637 assert(IntVal.getValueType() == MVT::i32);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000638 MVT::ValueType vt = Op.getValueType();
639 assert(vt == MVT::f32 ||
640 vt == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000641
642 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000643 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FSITOS : ARMISD::FSITOD;
644 return DAG.getNode(op, vt, Tmp);
Rafael Espindola27185192006-09-29 21:20:16 +0000645}
646
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000647static SDOperand LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
648 assert(Op.getValueType() == MVT::i32);
649 SDOperand FloatVal = Op.getOperand(0);
650 MVT::ValueType vt = FloatVal.getValueType();
651 assert(vt == MVT::f32 || vt == MVT::f64);
652
653 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FTOSIS : ARMISD::FTOSID;
654 SDOperand Tmp = DAG.getNode(op, MVT::f32, FloatVal);
655 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Tmp);
656}
657
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000658static SDOperand LowerUINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
659 SDOperand IntVal = Op.getOperand(0);
660 assert(IntVal.getValueType() == MVT::i32);
661 MVT::ValueType vt = Op.getValueType();
662 assert(vt == MVT::f32 ||
663 vt == MVT::f64);
664
665 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
666 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FUITOS : ARMISD::FUITOD;
667 return DAG.getNode(op, vt, Tmp);
668}
669
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000670static SDOperand LowerFP_TO_UINT(SDOperand Op, SelectionDAG &DAG) {
671 assert(Op.getValueType() == MVT::i32);
672 SDOperand FloatVal = Op.getOperand(0);
673 MVT::ValueType vt = FloatVal.getValueType();
674 assert(vt == MVT::f32 || vt == MVT::f64);
675
676 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FTOUIS : ARMISD::FTOUID;
677 SDOperand Tmp = DAG.getNode(op, MVT::f32, FloatVal);
678 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Tmp);
679}
680
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000681SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
682 switch (Op.getOpcode()) {
683 default:
684 assert(0 && "Should not custom lower this!");
Rafael Espindola1c8f0532006-05-15 22:34:39 +0000685 abort();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000686 case ISD::ConstantPool:
687 return LowerConstantPool(Op, DAG);
688 case ISD::GlobalAddress:
689 return LowerGlobalAddress(Op, DAG);
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000690 case ISD::FP_TO_SINT:
691 return LowerFP_TO_SINT(Op, DAG);
Rafael Espindola27185192006-09-29 21:20:16 +0000692 case ISD::SINT_TO_FP:
693 return LowerSINT_TO_FP(Op, DAG);
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000694 case ISD::FP_TO_UINT:
695 return LowerFP_TO_UINT(Op, DAG);
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000696 case ISD::UINT_TO_FP:
697 return LowerUINT_TO_FP(Op, DAG);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000698 case ISD::FORMAL_ARGUMENTS:
Rafael Espindola755be9b2006-08-25 17:55:16 +0000699 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000700 case ISD::CALL:
701 return LowerCALL(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000702 case ISD::RET:
703 return LowerRET(Op, DAG);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000704 case ISD::SELECT_CC:
705 return LowerSELECT_CC(Op, DAG);
Rafael Espindola687bc492006-08-24 13:45:55 +0000706 case ISD::BR_CC:
707 return LowerBR_CC(Op, DAG);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000708 case ISD::VASTART:
709 return LowerVASTART(Op, DAG, VarArgsFrameIndex);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000710 }
711}
712
713//===----------------------------------------------------------------------===//
714// Instruction Selector Implementation
715//===----------------------------------------------------------------------===//
716
717//===--------------------------------------------------------------------===//
718/// ARMDAGToDAGISel - ARM specific code to select ARM machine
719/// instructions for SelectionDAG operations.
720///
721namespace {
722class ARMDAGToDAGISel : public SelectionDAGISel {
723 ARMTargetLowering Lowering;
724
725public:
726 ARMDAGToDAGISel(TargetMachine &TM)
727 : SelectionDAGISel(Lowering), Lowering(TM) {
728 }
729
Evan Cheng9ade2182006-08-26 05:34:46 +0000730 SDNode *Select(SDOperand Op);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000731 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000732 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000733 bool SelectAddrMode1(SDOperand N, SDOperand &Arg, SDOperand &Shift,
734 SDOperand &ShiftType);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000735
736 // Include the pieces autogenerated from the target description.
737#include "ARMGenDAGISel.inc"
738};
739
740void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
741 DEBUG(BB->dump());
742
743 DAG.setRoot(SelectRoot(DAG.getRoot()));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000744 DAG.RemoveDeadNodes();
745
746 ScheduleAndEmitDAG(DAG);
747}
748
Rafael Espindola61369da2006-08-14 19:01:24 +0000749static bool isInt12Immediate(SDNode *N, short &Imm) {
750 if (N->getOpcode() != ISD::Constant)
751 return false;
752
753 int32_t t = cast<ConstantSDNode>(N)->getValue();
Rafael Espindola7246d332006-09-21 11:29:52 +0000754 int max = 1<<12;
Rafael Espindola61369da2006-08-14 19:01:24 +0000755 int min = -max;
756 if (t > min && t < max) {
757 Imm = t;
758 return true;
759 }
760 else
761 return false;
762}
763
764static bool isInt12Immediate(SDOperand Op, short &Imm) {
765 return isInt12Immediate(Op.Val, Imm);
766}
767
Rafael Espindola7246d332006-09-21 11:29:52 +0000768static uint32_t rotateL(uint32_t x) {
769 uint32_t bit31 = (x & (1 << 31)) >> 31;
770 uint32_t t = x << 1;
771 return t | bit31;
772}
773
774static bool isUInt8Immediate(uint32_t x) {
775 return x < (1 << 8);
776}
777
778static bool isRotInt8Immediate(uint32_t x) {
779 int r;
780 for (r = 0; r < 16; r++) {
781 if (isUInt8Immediate(x))
782 return true;
783 x = rotateL(rotateL(x));
784 }
785 return false;
786}
787
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000788bool ARMDAGToDAGISel::SelectAddrMode1(SDOperand N,
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000789 SDOperand &Arg,
790 SDOperand &Shift,
791 SDOperand &ShiftType) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000792 switch(N.getOpcode()) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000793 case ISD::Constant: {
Rafael Espindola7246d332006-09-21 11:29:52 +0000794 uint32_t val = cast<ConstantSDNode>(N)->getValue();
795 if(!isRotInt8Immediate(val)) {
796 const Type *t = MVT::getTypeForValueType(MVT::i32);
797 Constant *C = ConstantUInt::get(t, val);
798 int alignment = 2;
799 SDOperand Addr = CurDAG->getTargetConstantPool(C, MVT::i32, alignment);
800 SDOperand Z = CurDAG->getTargetConstant(0, MVT::i32);
801 SDNode *n = CurDAG->getTargetNode(ARM::ldr, MVT::i32, Z, Addr);
802 Arg = SDOperand(n, 0);
803 } else
804 Arg = CurDAG->getTargetConstant(val, MVT::i32);
805
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000806 Shift = CurDAG->getTargetConstant(0, MVT::i32);
807 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000808 return true;
809 }
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000810 case ISD::SRA:
811 Arg = N.getOperand(0);
812 Shift = N.getOperand(1);
813 ShiftType = CurDAG->getTargetConstant(ARMShift::ASR, MVT::i32);
814 return true;
815 case ISD::SRL:
816 Arg = N.getOperand(0);
817 Shift = N.getOperand(1);
818 ShiftType = CurDAG->getTargetConstant(ARMShift::LSR, MVT::i32);
819 return true;
820 case ISD::SHL:
821 Arg = N.getOperand(0);
822 Shift = N.getOperand(1);
823 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
824 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000825 }
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000826
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000827 Arg = N;
828 Shift = CurDAG->getTargetConstant(0, MVT::i32);
829 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000830 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000831}
832
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000833//register plus/minus 12 bit offset
834bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
835 SDOperand &Base) {
Rafael Espindolaf3a335c2006-08-17 17:09:40 +0000836 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
837 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
838 Offset = CurDAG->getTargetConstant(0, MVT::i32);
839 return true;
840 }
Rafael Espindola61369da2006-08-14 19:01:24 +0000841 if (N.getOpcode() == ISD::ADD) {
842 short imm = 0;
843 if (isInt12Immediate(N.getOperand(1), imm)) {
844 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
845 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
846 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
847 } else {
848 Base = N.getOperand(0);
849 }
850 return true; // [r+i]
851 }
852 }
853
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000854 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000855 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
856 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
857 }
858 else
859 Base = N;
860 return true; //any address fits in a register
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000861}
862
Evan Cheng9ade2182006-08-26 05:34:46 +0000863SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000864 SDNode *N = Op.Val;
865
866 switch (N->getOpcode()) {
867 default:
Evan Cheng9ade2182006-08-26 05:34:46 +0000868 return SelectCode(Op);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000869 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000870 }
Evan Cheng64a752f2006-08-11 09:08:15 +0000871 return NULL;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000872}
873
874} // end anonymous namespace
875
876/// createARMISelDag - This pass converts a legalized DAG into a
877/// ARM-specific DAG, ready for instruction scheduling.
878///
879FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
880 return new ARMDAGToDAGISel(TM);
881}