blob: 559873389021dfdca78bc49fba784a14f38ec2a2 [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 Espindola48bc9fb2006-10-09 16:28:33 +000060 setOperationAction(ISD::SELECT, MVT::i32, Expand);
61
Rafael Espindola3c000bf2006-08-21 22:00:32 +000062 setOperationAction(ISD::SETCC, MVT::i32, Expand);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +000063 setOperationAction(ISD::SETCC, MVT::f32, Expand);
64 setOperationAction(ISD::SETCC, MVT::f64, Expand);
65
Rafael Espindola3c000bf2006-08-21 22:00:32 +000066 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
Rafael Espindola687bc492006-08-24 13:45:55 +000067 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +000068 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
69 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Rafael Espindola3c000bf2006-08-21 22:00:32 +000070
Rafael Espindola755be9b2006-08-25 17:55:16 +000071 setOperationAction(ISD::VASTART, MVT::Other, Custom);
72 setOperationAction(ISD::VAEND, MVT::Other, Expand);
73
Rafael Espindolacd71da52006-10-03 17:27:58 +000074 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
75 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
76
Rafael Espindola341b8642006-08-04 12:48:42 +000077 setSchedulingPreference(SchedulingForRegPressure);
Rafael Espindola3717ca92006-08-20 01:49:49 +000078 computeRegisterProperties();
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000079}
80
Rafael Espindola84b19be2006-07-16 01:02:57 +000081namespace llvm {
82 namespace ARMISD {
83 enum NodeType {
84 // Start the numbering where the builting ops and target ops leave off.
85 FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
86 /// CALL - A direct function call.
Rafael Espindolaf4fda802006-08-03 17:02:20 +000087 CALL,
88
89 /// Return with a flag operand.
Rafael Espindola3c000bf2006-08-21 22:00:32 +000090 RET_FLAG,
91
92 CMP,
93
Rafael Espindola687bc492006-08-24 13:45:55 +000094 SELECT,
95
Rafael Espindola27185192006-09-29 21:20:16 +000096 BR,
97
Rafael Espindola9e071f02006-10-02 19:30:56 +000098 FSITOS,
99
100 FSITOD,
101
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000102 FUITOS,
103
104 FUITOD,
105
Rafael Espindolaa2845842006-10-05 16:48:49 +0000106 FMRRD,
107
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000108 FMDRR,
109
110 FMSTAT
Rafael Espindola84b19be2006-07-16 01:02:57 +0000111 };
112 }
113}
114
Rafael Espindola6f602de2006-08-24 16:13:15 +0000115/// DAGCCToARMCC - Convert a DAG integer condition code to an ARM CC
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000116//Note: ARM doesn't have condition codes corresponding to the ordered
117//condition codes of LLVM. We use exception raising instructions so
118//that we can be sure that V == 0 and test only the rest of the expression.
Rafael Espindola6f602de2006-08-24 16:13:15 +0000119static ARMCC::CondCodes DAGCCToARMCC(ISD::CondCode CC) {
120 switch (CC) {
Rafael Espindolaebdabda2006-09-21 13:06:26 +0000121 default:
122 std::cerr << "CC = " << CC << "\n";
123 assert(0 && "Unknown condition code!");
124 case ISD::SETUGT: return ARMCC::HI;
125 case ISD::SETULE: return ARMCC::LS;
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000126 case ISD::SETLE:
127 case ISD::SETOLE: return ARMCC::LE;
128 case ISD::SETLT:
129 case ISD::SETOLT: return ARMCC::LT;
130 case ISD::SETGT:
131 case ISD::SETOGT: return ARMCC::GT;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000132 case ISD::SETNE: return ARMCC::NE;
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000133 case ISD::SETEQ:
134 case ISD::SETOEQ: return ARMCC::EQ;
135 case ISD::SETGE:
136 case ISD::SETOGE: return ARMCC::GE;
Rafael Espindola5f450d22006-09-02 20:24:25 +0000137 case ISD::SETUGE: return ARMCC::CS;
Rafael Espindolabc4cec92006-09-03 13:19:16 +0000138 case ISD::SETULT: return ARMCC::CC;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000139 }
140}
141
Rafael Espindola84b19be2006-07-16 01:02:57 +0000142const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
143 switch (Opcode) {
144 default: return 0;
145 case ARMISD::CALL: return "ARMISD::CALL";
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000146 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000147 case ARMISD::SELECT: return "ARMISD::SELECT";
148 case ARMISD::CMP: return "ARMISD::CMP";
Rafael Espindola687bc492006-08-24 13:45:55 +0000149 case ARMISD::BR: return "ARMISD::BR";
Rafael Espindola27185192006-09-29 21:20:16 +0000150 case ARMISD::FSITOS: return "ARMISD::FSITOS";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000151 case ARMISD::FSITOD: return "ARMISD::FSITOD";
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000152 case ARMISD::FUITOS: return "ARMISD::FUITOS";
153 case ARMISD::FUITOD: return "ARMISD::FUITOD";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000154 case ARMISD::FMRRD: return "ARMISD::FMRRD";
Rafael Espindolaa2845842006-10-05 16:48:49 +0000155 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000156 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Rafael Espindola84b19be2006-07-16 01:02:57 +0000157 }
158}
159
Rafael Espindolaa2845842006-10-05 16:48:49 +0000160class ArgumentLayout {
161 std::vector<bool> is_reg;
162 std::vector<unsigned> pos;
163 std::vector<MVT::ValueType> types;
164public:
Rafael Espindola39b5a212006-10-05 17:46:48 +0000165 ArgumentLayout(const std::vector<MVT::ValueType> &Types) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000166 types = Types;
167
168 unsigned RegNum = 0;
169 unsigned StackOffset = 0;
Rafael Espindola39b5a212006-10-05 17:46:48 +0000170 for(std::vector<MVT::ValueType>::const_iterator I = Types.begin();
Rafael Espindolaa2845842006-10-05 16:48:49 +0000171 I != Types.end();
172 ++I) {
173 MVT::ValueType VT = *I;
174 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
175 unsigned size = MVT::getSizeInBits(VT)/32;
176
177 RegNum = ((RegNum + size - 1) / size) * size;
178 if (RegNum < 4) {
179 pos.push_back(RegNum);
180 is_reg.push_back(true);
181 RegNum += size;
182 } else {
183 unsigned bytes = size * 32/8;
184 StackOffset = ((StackOffset + bytes - 1) / bytes) * bytes;
185 pos.push_back(StackOffset);
186 is_reg.push_back(false);
187 StackOffset += bytes;
188 }
189 }
190 }
191 unsigned getRegisterNum(unsigned argNum) {
192 assert(isRegister(argNum));
193 return pos[argNum];
194 }
195 unsigned getOffset(unsigned argNum) {
196 assert(isOffset(argNum));
197 return pos[argNum];
198 }
199 unsigned isRegister(unsigned argNum) {
200 assert(argNum < is_reg.size());
201 return is_reg[argNum];
202 }
203 unsigned isOffset(unsigned argNum) {
204 return !isRegister(argNum);
205 }
206 MVT::ValueType getType(unsigned argNum) {
207 assert(argNum < types.size());
208 return types[argNum];
209 }
210 unsigned getStackSize(void) {
211 int last = is_reg.size() - 1;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000212 if (last < 0)
213 return 0;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000214 if (isRegister(last))
215 return 0;
216 return getOffset(last) + MVT::getSizeInBits(getType(last))/8;
217 }
218 int lastRegArg(void) {
219 int size = is_reg.size();
220 int last = 0;
221 while(last < size && isRegister(last))
222 last++;
223 last--;
224 return last;
225 }
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000226 int lastRegNum(void) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000227 int l = lastRegArg();
228 if (l < 0)
229 return -1;
230 unsigned r = getRegisterNum(l);
231 MVT::ValueType t = getType(l);
232 assert(t == MVT::i32 || t == MVT::f32 || t == MVT::f64);
233 if (t == MVT::f64)
234 return r + 1;
235 return r;
236 }
237};
238
Rafael Espindola84b19be2006-07-16 01:02:57 +0000239// This transforms a ISD::CALL node into a
240// callseq_star <- ARMISD:CALL <- callseq_end
241// chain
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000242static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola84b19be2006-07-16 01:02:57 +0000243 SDOperand Chain = Op.getOperand(0);
244 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
245 assert(CallConv == CallingConv::C && "unknown calling convention");
246 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000247 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
248 assert(isTailCall == false && "tail call not supported");
249 SDOperand Callee = Op.getOperand(4);
250 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Rafael Espindola1a009462006-08-08 13:02:29 +0000251 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000252 static const unsigned regs[] = {
Rafael Espindolafac00a92006-07-25 20:17:20 +0000253 ARM::R0, ARM::R1, ARM::R2, ARM::R3
254 };
255
Rafael Espindolaa2845842006-10-05 16:48:49 +0000256 std::vector<MVT::ValueType> Types;
257 for (unsigned i = 0; i < NumOps; ++i) {
258 MVT::ValueType VT = Op.getOperand(5+2*i).getValueType();
259 Types.push_back(VT);
260 }
261 ArgumentLayout Layout(Types);
Rafael Espindolafac00a92006-07-25 20:17:20 +0000262
Rafael Espindolaa2845842006-10-05 16:48:49 +0000263 unsigned NumBytes = Layout.getStackSize();
264
265 Chain = DAG.getCALLSEQ_START(Chain,
266 DAG.getConstant(NumBytes, MVT::i32));
267
268 //Build a sequence of stores
269 std::vector<SDOperand> MemOpChains;
270 for (unsigned i = Layout.lastRegArg() + 1; i < NumOps; ++i) {
271 SDOperand Arg = Op.getOperand(5+2*i);
272 unsigned ArgOffset = Layout.getOffset(i);
273 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
274 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Evan Cheng786225a2006-10-05 23:01:46 +0000275 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff,
276 DAG.getSrcValue(NULL)));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000277 }
Rafael Espindola1a009462006-08-08 13:02:29 +0000278 if (!MemOpChains.empty())
Chris Lattnere2199452006-08-11 17:38:39 +0000279 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
280 &MemOpChains[0], MemOpChains.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000281
Rafael Espindola84b19be2006-07-16 01:02:57 +0000282 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
283 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
284 // node so that legalize doesn't hack it.
285 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
286 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
287
288 // If this is a direct call, pass the chain and the callee.
289 assert (Callee.Val);
290 std::vector<SDOperand> Ops;
291 Ops.push_back(Chain);
292 Ops.push_back(Callee);
293
Rafael Espindolaa2845842006-10-05 16:48:49 +0000294 // Build a sequence of copy-to-reg nodes chained together with token chain
295 // and flag operands which copy the outgoing args into the appropriate regs.
296 SDOperand InFlag;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000297 for (int i = 0, e = Layout.lastRegArg(); i <= e; ++i) {
Rafael Espindola4a408d42006-10-06 12:50:22 +0000298 SDOperand Arg = Op.getOperand(5+2*i);
299 unsigned RegNum = Layout.getRegisterNum(i);
300 unsigned Reg1 = regs[RegNum];
301 MVT::ValueType VT = Layout.getType(i);
302 assert(VT == Arg.getValueType());
303 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000304
305 // Add argument register to the end of the list so that it is known live
306 // into the call.
Rafael Espindola4a408d42006-10-06 12:50:22 +0000307 Ops.push_back(DAG.getRegister(Reg1, MVT::i32));
308 if (VT == MVT::f64) {
309 unsigned Reg2 = regs[RegNum + 1];
310 SDOperand SDReg1 = DAG.getRegister(Reg1, MVT::i32);
311 SDOperand SDReg2 = DAG.getRegister(Reg2, MVT::i32);
312
313 Ops.push_back(DAG.getRegister(Reg2, MVT::i32));
314 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola935b1f82006-10-06 20:33:26 +0000315 SDOperand Ops[] = {Chain, SDReg1, SDReg2, Arg, InFlag};
316 Chain = DAG.getNode(ARMISD::FMRRD, VTs, Ops, InFlag.Val ? 5 : 4);
Rafael Espindola4a408d42006-10-06 12:50:22 +0000317 } else {
318 if (VT == MVT::f32)
319 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg);
320 Chain = DAG.getCopyToReg(Chain, Reg1, Arg, InFlag);
321 }
322 InFlag = Chain.getValue(1);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000323 }
324
325 std::vector<MVT::ValueType> NodeTys;
326 NodeTys.push_back(MVT::Other); // Returns a chain
327 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Rafael Espindola7a53bd02006-08-09 16:41:12 +0000328
Rafael Espindola84b19be2006-07-16 01:02:57 +0000329 unsigned CallOpc = ARMISD::CALL;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000330 if (InFlag.Val)
331 Ops.push_back(InFlag);
Chris Lattner87428672006-08-11 17:22:35 +0000332 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000333 InFlag = Chain.getValue(1);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000334
Rafael Espindolafac00a92006-07-25 20:17:20 +0000335 std::vector<SDOperand> ResultVals;
336 NodeTys.clear();
337
338 // If the call has results, copy the values out of the ret val registers.
Rafael Espindola614057b2006-10-06 19:10:05 +0000339 MVT::ValueType VT = Op.Val->getValueType(0);
340 if (VT != MVT::Other) {
341 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
342 SDOperand Value;
343
344 SDOperand Value1 = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
345 Chain = Value1.getValue(1);
346 InFlag = Value1.getValue(2);
347 if (VT == MVT::i32)
348 Value = Value1;
349 if (VT == MVT::f32)
350 Value = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Value1);
351 if (VT == MVT::f64) {
352 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
353 Chain = Value2.getValue(1);
354 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
355 }
356 ResultVals.push_back(Value);
357 NodeTys.push_back(VT);
Rafael Espindolafac00a92006-07-25 20:17:20 +0000358 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000359
360 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
361 DAG.getConstant(NumBytes, MVT::i32));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000362 NodeTys.push_back(MVT::Other);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000363
Rafael Espindolafac00a92006-07-25 20:17:20 +0000364 if (ResultVals.empty())
365 return Chain;
366
367 ResultVals.push_back(Chain);
Chris Lattner87428672006-08-11 17:22:35 +0000368 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
369 ResultVals.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000370 return Res.getValue(Op.ResNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000371}
372
373static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
374 SDOperand Copy;
Rafael Espindola4b023672006-06-05 22:26:14 +0000375 SDOperand Chain = Op.getOperand(0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000376 SDOperand R0 = DAG.getRegister(ARM::R0, MVT::i32);
377 SDOperand R1 = DAG.getRegister(ARM::R1, MVT::i32);
378
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000379 switch(Op.getNumOperands()) {
380 default:
381 assert(0 && "Do not know how to return this many arguments!");
382 abort();
Rafael Espindola4b023672006-06-05 22:26:14 +0000383 case 1: {
384 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
Rafael Espindola6312da02006-08-03 22:50:11 +0000385 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
Rafael Espindola4b023672006-06-05 22:26:14 +0000386 }
Rafael Espindola27185192006-09-29 21:20:16 +0000387 case 3: {
388 SDOperand Val = Op.getOperand(1);
389 assert(Val.getValueType() == MVT::i32 ||
Rafael Espindola9e071f02006-10-02 19:30:56 +0000390 Val.getValueType() == MVT::f32 ||
391 Val.getValueType() == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000392
Rafael Espindola9e071f02006-10-02 19:30:56 +0000393 if (Val.getValueType() == MVT::f64) {
394 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
395 SDOperand Ops[] = {Chain, R0, R1, Val};
396 Copy = DAG.getNode(ARMISD::FMRRD, VTs, Ops, 4);
397 } else {
398 if (Val.getValueType() == MVT::f32)
399 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
400 Copy = DAG.getCopyToReg(Chain, R0, Val, SDOperand());
401 }
402
403 if (DAG.getMachineFunction().liveout_empty()) {
Rafael Espindola4b023672006-06-05 22:26:14 +0000404 DAG.getMachineFunction().addLiveOut(ARM::R0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000405 if (Val.getValueType() == MVT::f64)
406 DAG.getMachineFunction().addLiveOut(ARM::R1);
407 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000408 break;
Rafael Espindola27185192006-09-29 21:20:16 +0000409 }
Rafael Espindola3a02f022006-09-04 19:05:01 +0000410 case 5:
411 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
412 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
413 // If we haven't noted the R0+R1 are live out, do so now.
414 if (DAG.getMachineFunction().liveout_empty()) {
415 DAG.getMachineFunction().addLiveOut(ARM::R0);
416 DAG.getMachineFunction().addLiveOut(ARM::R1);
417 }
418 break;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000419 }
Rafael Espindola4b023672006-06-05 22:26:14 +0000420
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000421 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
422 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000423}
424
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000425static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
426 MVT::ValueType PtrVT = Op.getValueType();
427 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengc356a572006-09-12 21:04:05 +0000428 Constant *C = CP->getConstVal();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000429 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
430
431 return CPI;
432}
433
434static SDOperand LowerGlobalAddress(SDOperand Op,
435 SelectionDAG &DAG) {
436 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Rafael Espindola61369da2006-08-14 19:01:24 +0000437 int alignment = 2;
438 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
Evan Cheng466685d2006-10-09 20:57:25 +0000439 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr, NULL, 0);
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000440}
441
Rafael Espindola755be9b2006-08-25 17:55:16 +0000442static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
443 unsigned VarArgsFrameIndex) {
444 // vastart just stores the address of the VarArgsFrameIndex slot into the
445 // memory location argument.
446 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
447 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Evan Cheng786225a2006-10-05 23:01:46 +0000448 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), Op.getOperand(2));
Rafael Espindola755be9b2006-08-25 17:55:16 +0000449}
450
451static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
452 int &VarArgsFrameIndex) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000453 MachineFunction &MF = DAG.getMachineFunction();
454 MachineFrameInfo *MFI = MF.getFrameInfo();
455 SSARegMap *RegMap = MF.getSSARegMap();
456 unsigned NumArgs = Op.Val->getNumValues()-1;
457 SDOperand Root = Op.getOperand(0);
458 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
459 static const unsigned REGS[] = {
460 ARM::R0, ARM::R1, ARM::R2, ARM::R3
461 };
462
463 std::vector<MVT::ValueType> Types(Op.Val->value_begin(), Op.Val->value_end() - 1);
464 ArgumentLayout Layout(Types);
465
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000466 std::vector<SDOperand> ArgValues;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000467 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000468 MVT::ValueType VT = Types[ArgNo];
Rafael Espindola4b442b52006-05-23 02:48:20 +0000469
Rafael Espindolaa2845842006-10-05 16:48:49 +0000470 SDOperand Value;
471 if (Layout.isRegister(ArgNo)) {
472 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
473 unsigned RegNum = Layout.getRegisterNum(ArgNo);
474 unsigned Reg1 = REGS[RegNum];
475 unsigned VReg1 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
476 SDOperand Value1 = DAG.getCopyFromReg(Root, VReg1, MVT::i32);
477 MF.addLiveIn(Reg1, VReg1);
478 if (VT == MVT::f64) {
479 unsigned Reg2 = REGS[RegNum + 1];
480 unsigned VReg2 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
481 SDOperand Value2 = DAG.getCopyFromReg(Root, VReg2, MVT::i32);
482 MF.addLiveIn(Reg2, VReg2);
483 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
484 } else {
485 Value = Value1;
486 if (VT == MVT::f32)
487 Value = DAG.getNode(ISD::BIT_CONVERT, VT, Value);
488 }
489 } else {
490 // If the argument is actually used, emit a load from the right stack
491 // slot.
492 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
493 unsigned Offset = Layout.getOffset(ArgNo);
494 unsigned Size = MVT::getSizeInBits(VT)/8;
495 int FI = MFI->CreateFixedObject(Size, Offset);
496 SDOperand FIN = DAG.getFrameIndex(FI, VT);
Evan Cheng466685d2006-10-09 20:57:25 +0000497 Value = DAG.getLoad(VT, Root, FIN, NULL, 0);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000498 } else {
499 Value = DAG.getNode(ISD::UNDEF, VT);
500 }
501 }
502 ArgValues.push_back(Value);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000503 }
504
Rafael Espindolaa2845842006-10-05 16:48:49 +0000505 unsigned NextRegNum = Layout.lastRegNum() + 1;
506
Rafael Espindola755be9b2006-08-25 17:55:16 +0000507 if (isVarArg) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000508 //If this function is vararg we must store the remaing
509 //registers so that they can be acessed with va_start
Rafael Espindola755be9b2006-08-25 17:55:16 +0000510 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000511 -16 + NextRegNum * 4);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000512
Rafael Espindola755be9b2006-08-25 17:55:16 +0000513 SmallVector<SDOperand, 4> MemOps;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000514 for (unsigned RegNo = NextRegNum; RegNo < 4; ++RegNo) {
515 int RegOffset = - (4 - RegNo) * 4;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000516 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000517 RegOffset);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000518 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
519
Rafael Espindolaa2845842006-10-05 16:48:49 +0000520 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
521 MF.addLiveIn(REGS[RegNo], VReg);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000522
523 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
Evan Cheng786225a2006-10-05 23:01:46 +0000524 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN,
525 DAG.getSrcValue(NULL));
Rafael Espindola755be9b2006-08-25 17:55:16 +0000526 MemOps.push_back(Store);
527 }
528 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
529 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000530
531 ArgValues.push_back(Root);
532
533 // Return the new list of results.
534 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
535 Op.Val->value_end());
Chris Lattner87428672006-08-11 17:22:35 +0000536 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
Rafael Espindoladc124a22006-05-18 21:45:49 +0000537}
538
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000539static SDOperand GetCMP(ISD::CondCode CC, SDOperand LHS, SDOperand RHS,
540 SelectionDAG &DAG) {
541 MVT::ValueType vt = LHS.getValueType();
542 assert(vt == MVT::i32 || vt == MVT::f32);
543 //Note: unordered floating point compares should use a non throwing
544 //compare.
545 bool isUnorderedFloat = vt == MVT::f32 &&
546 (CC >= ISD::SETUO && CC <= ISD::SETUNE);
547 assert(!isUnorderedFloat && "Unordered float compares are not supported");
548
549 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
550 if (vt != MVT::i32)
551 Cmp = DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp);
552 return Cmp;
553}
554
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000555static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
556 SDOperand LHS = Op.getOperand(0);
557 SDOperand RHS = Op.getOperand(1);
558 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
559 SDOperand TrueVal = Op.getOperand(2);
560 SDOperand FalseVal = Op.getOperand(3);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000561 SDOperand Cmp = GetCMP(CC, LHS, RHS, DAG);
Rafael Espindolacdda88c2006-08-24 17:19:08 +0000562 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000563 return DAG.getNode(ARMISD::SELECT, MVT::i32, TrueVal, FalseVal, ARMCC, Cmp);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000564}
565
Rafael Espindola687bc492006-08-24 13:45:55 +0000566static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
567 SDOperand Chain = Op.getOperand(0);
568 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
569 SDOperand LHS = Op.getOperand(2);
570 SDOperand RHS = Op.getOperand(3);
571 SDOperand Dest = Op.getOperand(4);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000572 SDOperand Cmp = GetCMP(CC, LHS, RHS, DAG);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000573 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000574 return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, ARMCC, Cmp);
Rafael Espindola687bc492006-08-24 13:45:55 +0000575}
576
Rafael Espindola27185192006-09-29 21:20:16 +0000577static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola9e071f02006-10-02 19:30:56 +0000578 SDOperand IntVal = Op.getOperand(0);
Rafael Espindola27185192006-09-29 21:20:16 +0000579 assert(IntVal.getValueType() == MVT::i32);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000580 MVT::ValueType vt = Op.getValueType();
581 assert(vt == MVT::f32 ||
582 vt == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000583
584 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000585 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FSITOS : ARMISD::FSITOD;
586 return DAG.getNode(op, vt, Tmp);
Rafael Espindola27185192006-09-29 21:20:16 +0000587}
588
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000589static SDOperand LowerUINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
590 SDOperand IntVal = Op.getOperand(0);
591 assert(IntVal.getValueType() == MVT::i32);
592 MVT::ValueType vt = Op.getValueType();
593 assert(vt == MVT::f32 ||
594 vt == MVT::f64);
595
596 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
597 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FUITOS : ARMISD::FUITOD;
598 return DAG.getNode(op, vt, Tmp);
599}
600
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000601SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
602 switch (Op.getOpcode()) {
603 default:
604 assert(0 && "Should not custom lower this!");
Rafael Espindola1c8f0532006-05-15 22:34:39 +0000605 abort();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000606 case ISD::ConstantPool:
607 return LowerConstantPool(Op, DAG);
608 case ISD::GlobalAddress:
609 return LowerGlobalAddress(Op, DAG);
Rafael Espindola27185192006-09-29 21:20:16 +0000610 case ISD::SINT_TO_FP:
611 return LowerSINT_TO_FP(Op, DAG);
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000612 case ISD::UINT_TO_FP:
613 return LowerUINT_TO_FP(Op, DAG);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000614 case ISD::FORMAL_ARGUMENTS:
Rafael Espindola755be9b2006-08-25 17:55:16 +0000615 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000616 case ISD::CALL:
617 return LowerCALL(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000618 case ISD::RET:
619 return LowerRET(Op, DAG);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000620 case ISD::SELECT_CC:
621 return LowerSELECT_CC(Op, DAG);
Rafael Espindola687bc492006-08-24 13:45:55 +0000622 case ISD::BR_CC:
623 return LowerBR_CC(Op, DAG);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000624 case ISD::VASTART:
625 return LowerVASTART(Op, DAG, VarArgsFrameIndex);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000626 }
627}
628
629//===----------------------------------------------------------------------===//
630// Instruction Selector Implementation
631//===----------------------------------------------------------------------===//
632
633//===--------------------------------------------------------------------===//
634/// ARMDAGToDAGISel - ARM specific code to select ARM machine
635/// instructions for SelectionDAG operations.
636///
637namespace {
638class ARMDAGToDAGISel : public SelectionDAGISel {
639 ARMTargetLowering Lowering;
640
641public:
642 ARMDAGToDAGISel(TargetMachine &TM)
643 : SelectionDAGISel(Lowering), Lowering(TM) {
644 }
645
Evan Cheng9ade2182006-08-26 05:34:46 +0000646 SDNode *Select(SDOperand Op);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000647 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000648 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000649 bool SelectAddrMode1(SDOperand N, SDOperand &Arg, SDOperand &Shift,
650 SDOperand &ShiftType);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000651
652 // Include the pieces autogenerated from the target description.
653#include "ARMGenDAGISel.inc"
654};
655
656void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
657 DEBUG(BB->dump());
658
659 DAG.setRoot(SelectRoot(DAG.getRoot()));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000660 DAG.RemoveDeadNodes();
661
662 ScheduleAndEmitDAG(DAG);
663}
664
Rafael Espindola61369da2006-08-14 19:01:24 +0000665static bool isInt12Immediate(SDNode *N, short &Imm) {
666 if (N->getOpcode() != ISD::Constant)
667 return false;
668
669 int32_t t = cast<ConstantSDNode>(N)->getValue();
Rafael Espindola7246d332006-09-21 11:29:52 +0000670 int max = 1<<12;
Rafael Espindola61369da2006-08-14 19:01:24 +0000671 int min = -max;
672 if (t > min && t < max) {
673 Imm = t;
674 return true;
675 }
676 else
677 return false;
678}
679
680static bool isInt12Immediate(SDOperand Op, short &Imm) {
681 return isInt12Immediate(Op.Val, Imm);
682}
683
Rafael Espindola7246d332006-09-21 11:29:52 +0000684static uint32_t rotateL(uint32_t x) {
685 uint32_t bit31 = (x & (1 << 31)) >> 31;
686 uint32_t t = x << 1;
687 return t | bit31;
688}
689
690static bool isUInt8Immediate(uint32_t x) {
691 return x < (1 << 8);
692}
693
694static bool isRotInt8Immediate(uint32_t x) {
695 int r;
696 for (r = 0; r < 16; r++) {
697 if (isUInt8Immediate(x))
698 return true;
699 x = rotateL(rotateL(x));
700 }
701 return false;
702}
703
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000704bool ARMDAGToDAGISel::SelectAddrMode1(SDOperand N,
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000705 SDOperand &Arg,
706 SDOperand &Shift,
707 SDOperand &ShiftType) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000708 switch(N.getOpcode()) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000709 case ISD::Constant: {
Rafael Espindola7246d332006-09-21 11:29:52 +0000710 uint32_t val = cast<ConstantSDNode>(N)->getValue();
711 if(!isRotInt8Immediate(val)) {
712 const Type *t = MVT::getTypeForValueType(MVT::i32);
713 Constant *C = ConstantUInt::get(t, val);
714 int alignment = 2;
715 SDOperand Addr = CurDAG->getTargetConstantPool(C, MVT::i32, alignment);
716 SDOperand Z = CurDAG->getTargetConstant(0, MVT::i32);
717 SDNode *n = CurDAG->getTargetNode(ARM::ldr, MVT::i32, Z, Addr);
718 Arg = SDOperand(n, 0);
719 } else
720 Arg = CurDAG->getTargetConstant(val, MVT::i32);
721
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000722 Shift = CurDAG->getTargetConstant(0, MVT::i32);
723 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000724 return true;
725 }
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000726 case ISD::SRA:
727 Arg = N.getOperand(0);
728 Shift = N.getOperand(1);
729 ShiftType = CurDAG->getTargetConstant(ARMShift::ASR, MVT::i32);
730 return true;
731 case ISD::SRL:
732 Arg = N.getOperand(0);
733 Shift = N.getOperand(1);
734 ShiftType = CurDAG->getTargetConstant(ARMShift::LSR, MVT::i32);
735 return true;
736 case ISD::SHL:
737 Arg = N.getOperand(0);
738 Shift = N.getOperand(1);
739 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
740 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000741 }
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000742
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000743 Arg = N;
744 Shift = CurDAG->getTargetConstant(0, MVT::i32);
745 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000746 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000747}
748
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000749//register plus/minus 12 bit offset
750bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
751 SDOperand &Base) {
Rafael Espindolaf3a335c2006-08-17 17:09:40 +0000752 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
753 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
754 Offset = CurDAG->getTargetConstant(0, MVT::i32);
755 return true;
756 }
Rafael Espindola61369da2006-08-14 19:01:24 +0000757 if (N.getOpcode() == ISD::ADD) {
758 short imm = 0;
759 if (isInt12Immediate(N.getOperand(1), imm)) {
760 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
761 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
762 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
763 } else {
764 Base = N.getOperand(0);
765 }
766 return true; // [r+i]
767 }
768 }
769
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000770 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000771 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
772 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
773 }
774 else
775 Base = N;
776 return true; //any address fits in a register
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000777}
778
Evan Cheng9ade2182006-08-26 05:34:46 +0000779SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000780 SDNode *N = Op.Val;
781
782 switch (N->getOpcode()) {
783 default:
Evan Cheng9ade2182006-08-26 05:34:46 +0000784 return SelectCode(Op);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000785 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000786 }
Evan Cheng64a752f2006-08-11 09:08:15 +0000787 return NULL;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000788}
789
790} // end anonymous namespace
791
792/// createARMISelDag - This pass converts a legalized DAG into a
793/// ARM-specific DAG, ready for instruction scheduling.
794///
795FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
796 return new ARMDAGToDAGISel(TM);
797}