blob: f219b2aaddd044ae973f961600f187426232ed62 [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 Espindolae5bbd6d2006-10-07 14:24:52 +000055 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
56
Rafael Espindola06c1e7e2006-08-01 12:58:43 +000057 setOperationAction(ISD::RET, MVT::Other, Custom);
58 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
59 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Rafael Espindola341b8642006-08-04 12:48:42 +000060
Rafael Espindola48bc9fb2006-10-09 16:28:33 +000061 setOperationAction(ISD::SELECT, MVT::i32, Expand);
62
Rafael Espindola3c000bf2006-08-21 22:00:32 +000063 setOperationAction(ISD::SETCC, MVT::i32, Expand);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +000064 setOperationAction(ISD::SETCC, MVT::f32, Expand);
65 setOperationAction(ISD::SETCC, MVT::f64, Expand);
66
Rafael Espindola3c000bf2006-08-21 22:00:32 +000067 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
Rafael Espindola687bc492006-08-24 13:45:55 +000068 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +000069 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
70 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Rafael Espindola3c000bf2006-08-21 22:00:32 +000071
Rafael Espindola755be9b2006-08-25 17:55:16 +000072 setOperationAction(ISD::VASTART, MVT::Other, Custom);
73 setOperationAction(ISD::VAEND, MVT::Other, Expand);
74
Rafael Espindolacd71da52006-10-03 17:27:58 +000075 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
76 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
77
Rafael Espindola341b8642006-08-04 12:48:42 +000078 setSchedulingPreference(SchedulingForRegPressure);
Rafael Espindola3717ca92006-08-20 01:49:49 +000079 computeRegisterProperties();
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000080}
81
Rafael Espindola84b19be2006-07-16 01:02:57 +000082namespace llvm {
83 namespace ARMISD {
84 enum NodeType {
85 // Start the numbering where the builting ops and target ops leave off.
86 FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
87 /// CALL - A direct function call.
Rafael Espindolaf4fda802006-08-03 17:02:20 +000088 CALL,
89
90 /// Return with a flag operand.
Rafael Espindola3c000bf2006-08-21 22:00:32 +000091 RET_FLAG,
92
93 CMP,
94
Rafael Espindola687bc492006-08-24 13:45:55 +000095 SELECT,
96
Rafael Espindola27185192006-09-29 21:20:16 +000097 BR,
98
Rafael Espindola9e071f02006-10-02 19:30:56 +000099 FSITOS,
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000100 FTOSIS,
Rafael Espindola9e071f02006-10-02 19:30:56 +0000101
102 FSITOD,
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000103 FTOSID,
Rafael Espindola9e071f02006-10-02 19:30:56 +0000104
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000105 FUITOS,
106
107 FUITOD,
108
Rafael Espindolaa2845842006-10-05 16:48:49 +0000109 FMRRD,
110
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000111 FMDRR,
112
113 FMSTAT
Rafael Espindola84b19be2006-07-16 01:02:57 +0000114 };
115 }
116}
117
Rafael Espindola6f602de2006-08-24 16:13:15 +0000118/// DAGCCToARMCC - Convert a DAG integer condition code to an ARM CC
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000119//Note: ARM doesn't have condition codes corresponding to the ordered
120//condition codes of LLVM. We use exception raising instructions so
121//that we can be sure that V == 0 and test only the rest of the expression.
Rafael Espindola6f602de2006-08-24 16:13:15 +0000122static ARMCC::CondCodes DAGCCToARMCC(ISD::CondCode CC) {
123 switch (CC) {
Rafael Espindolaebdabda2006-09-21 13:06:26 +0000124 default:
125 std::cerr << "CC = " << CC << "\n";
126 assert(0 && "Unknown condition code!");
127 case ISD::SETUGT: return ARMCC::HI;
128 case ISD::SETULE: return ARMCC::LS;
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000129 case ISD::SETLE:
130 case ISD::SETOLE: return ARMCC::LE;
131 case ISD::SETLT:
132 case ISD::SETOLT: return ARMCC::LT;
133 case ISD::SETGT:
134 case ISD::SETOGT: return ARMCC::GT;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000135 case ISD::SETNE: return ARMCC::NE;
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000136 case ISD::SETEQ:
137 case ISD::SETOEQ: return ARMCC::EQ;
138 case ISD::SETGE:
139 case ISD::SETOGE: return ARMCC::GE;
Rafael Espindola5f450d22006-09-02 20:24:25 +0000140 case ISD::SETUGE: return ARMCC::CS;
Rafael Espindolabc4cec92006-09-03 13:19:16 +0000141 case ISD::SETULT: return ARMCC::CC;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000142 }
143}
144
Rafael Espindola84b19be2006-07-16 01:02:57 +0000145const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
146 switch (Opcode) {
147 default: return 0;
148 case ARMISD::CALL: return "ARMISD::CALL";
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000149 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000150 case ARMISD::SELECT: return "ARMISD::SELECT";
151 case ARMISD::CMP: return "ARMISD::CMP";
Rafael Espindola687bc492006-08-24 13:45:55 +0000152 case ARMISD::BR: return "ARMISD::BR";
Rafael Espindola27185192006-09-29 21:20:16 +0000153 case ARMISD::FSITOS: return "ARMISD::FSITOS";
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000154 case ARMISD::FTOSIS: return "ARMISD::FTOSIS";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000155 case ARMISD::FSITOD: return "ARMISD::FSITOD";
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000156 case ARMISD::FTOSID: return "ARMISD::FTOSID";
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000157 case ARMISD::FUITOS: return "ARMISD::FUITOS";
158 case ARMISD::FUITOD: return "ARMISD::FUITOD";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000159 case ARMISD::FMRRD: return "ARMISD::FMRRD";
Rafael Espindolaa2845842006-10-05 16:48:49 +0000160 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000161 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Rafael Espindola84b19be2006-07-16 01:02:57 +0000162 }
163}
164
Rafael Espindolaa2845842006-10-05 16:48:49 +0000165class ArgumentLayout {
166 std::vector<bool> is_reg;
167 std::vector<unsigned> pos;
168 std::vector<MVT::ValueType> types;
169public:
Rafael Espindola39b5a212006-10-05 17:46:48 +0000170 ArgumentLayout(const std::vector<MVT::ValueType> &Types) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000171 types = Types;
172
173 unsigned RegNum = 0;
174 unsigned StackOffset = 0;
Rafael Espindola39b5a212006-10-05 17:46:48 +0000175 for(std::vector<MVT::ValueType>::const_iterator I = Types.begin();
Rafael Espindolaa2845842006-10-05 16:48:49 +0000176 I != Types.end();
177 ++I) {
178 MVT::ValueType VT = *I;
179 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
180 unsigned size = MVT::getSizeInBits(VT)/32;
181
182 RegNum = ((RegNum + size - 1) / size) * size;
183 if (RegNum < 4) {
184 pos.push_back(RegNum);
185 is_reg.push_back(true);
186 RegNum += size;
187 } else {
188 unsigned bytes = size * 32/8;
189 StackOffset = ((StackOffset + bytes - 1) / bytes) * bytes;
190 pos.push_back(StackOffset);
191 is_reg.push_back(false);
192 StackOffset += bytes;
193 }
194 }
195 }
196 unsigned getRegisterNum(unsigned argNum) {
197 assert(isRegister(argNum));
198 return pos[argNum];
199 }
200 unsigned getOffset(unsigned argNum) {
201 assert(isOffset(argNum));
202 return pos[argNum];
203 }
204 unsigned isRegister(unsigned argNum) {
205 assert(argNum < is_reg.size());
206 return is_reg[argNum];
207 }
208 unsigned isOffset(unsigned argNum) {
209 return !isRegister(argNum);
210 }
211 MVT::ValueType getType(unsigned argNum) {
212 assert(argNum < types.size());
213 return types[argNum];
214 }
215 unsigned getStackSize(void) {
216 int last = is_reg.size() - 1;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000217 if (last < 0)
218 return 0;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000219 if (isRegister(last))
220 return 0;
221 return getOffset(last) + MVT::getSizeInBits(getType(last))/8;
222 }
223 int lastRegArg(void) {
224 int size = is_reg.size();
225 int last = 0;
226 while(last < size && isRegister(last))
227 last++;
228 last--;
229 return last;
230 }
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000231 int lastRegNum(void) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000232 int l = lastRegArg();
233 if (l < 0)
234 return -1;
235 unsigned r = getRegisterNum(l);
236 MVT::ValueType t = getType(l);
237 assert(t == MVT::i32 || t == MVT::f32 || t == MVT::f64);
238 if (t == MVT::f64)
239 return r + 1;
240 return r;
241 }
242};
243
Rafael Espindola84b19be2006-07-16 01:02:57 +0000244// This transforms a ISD::CALL node into a
245// callseq_star <- ARMISD:CALL <- callseq_end
246// chain
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000247static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola84b19be2006-07-16 01:02:57 +0000248 SDOperand Chain = Op.getOperand(0);
249 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
250 assert(CallConv == CallingConv::C && "unknown calling convention");
251 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000252 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
253 assert(isTailCall == false && "tail call not supported");
254 SDOperand Callee = Op.getOperand(4);
255 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Rafael Espindola1a009462006-08-08 13:02:29 +0000256 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000257 static const unsigned regs[] = {
Rafael Espindolafac00a92006-07-25 20:17:20 +0000258 ARM::R0, ARM::R1, ARM::R2, ARM::R3
259 };
260
Rafael Espindolaa2845842006-10-05 16:48:49 +0000261 std::vector<MVT::ValueType> Types;
262 for (unsigned i = 0; i < NumOps; ++i) {
263 MVT::ValueType VT = Op.getOperand(5+2*i).getValueType();
264 Types.push_back(VT);
265 }
266 ArgumentLayout Layout(Types);
Rafael Espindolafac00a92006-07-25 20:17:20 +0000267
Rafael Espindolaa2845842006-10-05 16:48:49 +0000268 unsigned NumBytes = Layout.getStackSize();
269
270 Chain = DAG.getCALLSEQ_START(Chain,
271 DAG.getConstant(NumBytes, MVT::i32));
272
273 //Build a sequence of stores
274 std::vector<SDOperand> MemOpChains;
275 for (unsigned i = Layout.lastRegArg() + 1; i < NumOps; ++i) {
276 SDOperand Arg = Op.getOperand(5+2*i);
277 unsigned ArgOffset = Layout.getOffset(i);
278 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
279 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Evan Cheng786225a2006-10-05 23:01:46 +0000280 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff,
281 DAG.getSrcValue(NULL)));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000282 }
Rafael Espindola1a009462006-08-08 13:02:29 +0000283 if (!MemOpChains.empty())
Chris Lattnere2199452006-08-11 17:38:39 +0000284 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
285 &MemOpChains[0], MemOpChains.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000286
Rafael Espindola84b19be2006-07-16 01:02:57 +0000287 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
288 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
289 // node so that legalize doesn't hack it.
290 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
291 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
292
293 // If this is a direct call, pass the chain and the callee.
294 assert (Callee.Val);
295 std::vector<SDOperand> Ops;
296 Ops.push_back(Chain);
297 Ops.push_back(Callee);
298
Rafael Espindolaa2845842006-10-05 16:48:49 +0000299 // Build a sequence of copy-to-reg nodes chained together with token chain
300 // and flag operands which copy the outgoing args into the appropriate regs.
301 SDOperand InFlag;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000302 for (int i = 0, e = Layout.lastRegArg(); i <= e; ++i) {
Rafael Espindola4a408d42006-10-06 12:50:22 +0000303 SDOperand Arg = Op.getOperand(5+2*i);
304 unsigned RegNum = Layout.getRegisterNum(i);
305 unsigned Reg1 = regs[RegNum];
306 MVT::ValueType VT = Layout.getType(i);
307 assert(VT == Arg.getValueType());
308 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000309
310 // Add argument register to the end of the list so that it is known live
311 // into the call.
Rafael Espindola4a408d42006-10-06 12:50:22 +0000312 Ops.push_back(DAG.getRegister(Reg1, MVT::i32));
313 if (VT == MVT::f64) {
314 unsigned Reg2 = regs[RegNum + 1];
315 SDOperand SDReg1 = DAG.getRegister(Reg1, MVT::i32);
316 SDOperand SDReg2 = DAG.getRegister(Reg2, MVT::i32);
317
318 Ops.push_back(DAG.getRegister(Reg2, MVT::i32));
319 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola935b1f82006-10-06 20:33:26 +0000320 SDOperand Ops[] = {Chain, SDReg1, SDReg2, Arg, InFlag};
321 Chain = DAG.getNode(ARMISD::FMRRD, VTs, Ops, InFlag.Val ? 5 : 4);
Rafael Espindola4a408d42006-10-06 12:50:22 +0000322 } else {
323 if (VT == MVT::f32)
324 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg);
325 Chain = DAG.getCopyToReg(Chain, Reg1, Arg, InFlag);
326 }
327 InFlag = Chain.getValue(1);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000328 }
329
330 std::vector<MVT::ValueType> NodeTys;
331 NodeTys.push_back(MVT::Other); // Returns a chain
332 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Rafael Espindola7a53bd02006-08-09 16:41:12 +0000333
Rafael Espindola84b19be2006-07-16 01:02:57 +0000334 unsigned CallOpc = ARMISD::CALL;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000335 if (InFlag.Val)
336 Ops.push_back(InFlag);
Chris Lattner87428672006-08-11 17:22:35 +0000337 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000338 InFlag = Chain.getValue(1);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000339
Rafael Espindolafac00a92006-07-25 20:17:20 +0000340 std::vector<SDOperand> ResultVals;
341 NodeTys.clear();
342
343 // If the call has results, copy the values out of the ret val registers.
Rafael Espindola614057b2006-10-06 19:10:05 +0000344 MVT::ValueType VT = Op.Val->getValueType(0);
345 if (VT != MVT::Other) {
346 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
347 SDOperand Value;
348
349 SDOperand Value1 = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
350 Chain = Value1.getValue(1);
351 InFlag = Value1.getValue(2);
352 if (VT == MVT::i32)
353 Value = Value1;
354 if (VT == MVT::f32)
355 Value = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Value1);
356 if (VT == MVT::f64) {
357 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
358 Chain = Value2.getValue(1);
359 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
360 }
361 ResultVals.push_back(Value);
362 NodeTys.push_back(VT);
Rafael Espindolafac00a92006-07-25 20:17:20 +0000363 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000364
365 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
366 DAG.getConstant(NumBytes, MVT::i32));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000367 NodeTys.push_back(MVT::Other);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000368
Rafael Espindolafac00a92006-07-25 20:17:20 +0000369 if (ResultVals.empty())
370 return Chain;
371
372 ResultVals.push_back(Chain);
Chris Lattner87428672006-08-11 17:22:35 +0000373 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
374 ResultVals.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000375 return Res.getValue(Op.ResNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000376}
377
378static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
379 SDOperand Copy;
Rafael Espindola4b023672006-06-05 22:26:14 +0000380 SDOperand Chain = Op.getOperand(0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000381 SDOperand R0 = DAG.getRegister(ARM::R0, MVT::i32);
382 SDOperand R1 = DAG.getRegister(ARM::R1, MVT::i32);
383
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000384 switch(Op.getNumOperands()) {
385 default:
386 assert(0 && "Do not know how to return this many arguments!");
387 abort();
Rafael Espindola4b023672006-06-05 22:26:14 +0000388 case 1: {
389 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
Rafael Espindola6312da02006-08-03 22:50:11 +0000390 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
Rafael Espindola4b023672006-06-05 22:26:14 +0000391 }
Rafael Espindola27185192006-09-29 21:20:16 +0000392 case 3: {
393 SDOperand Val = Op.getOperand(1);
394 assert(Val.getValueType() == MVT::i32 ||
Rafael Espindola9e071f02006-10-02 19:30:56 +0000395 Val.getValueType() == MVT::f32 ||
396 Val.getValueType() == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000397
Rafael Espindola9e071f02006-10-02 19:30:56 +0000398 if (Val.getValueType() == MVT::f64) {
399 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
400 SDOperand Ops[] = {Chain, R0, R1, Val};
401 Copy = DAG.getNode(ARMISD::FMRRD, VTs, Ops, 4);
402 } else {
403 if (Val.getValueType() == MVT::f32)
404 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
405 Copy = DAG.getCopyToReg(Chain, R0, Val, SDOperand());
406 }
407
408 if (DAG.getMachineFunction().liveout_empty()) {
Rafael Espindola4b023672006-06-05 22:26:14 +0000409 DAG.getMachineFunction().addLiveOut(ARM::R0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000410 if (Val.getValueType() == MVT::f64)
411 DAG.getMachineFunction().addLiveOut(ARM::R1);
412 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000413 break;
Rafael Espindola27185192006-09-29 21:20:16 +0000414 }
Rafael Espindola3a02f022006-09-04 19:05:01 +0000415 case 5:
416 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
417 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
418 // If we haven't noted the R0+R1 are live out, do so now.
419 if (DAG.getMachineFunction().liveout_empty()) {
420 DAG.getMachineFunction().addLiveOut(ARM::R0);
421 DAG.getMachineFunction().addLiveOut(ARM::R1);
422 }
423 break;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000424 }
Rafael Espindola4b023672006-06-05 22:26:14 +0000425
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000426 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
427 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000428}
429
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000430static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
431 MVT::ValueType PtrVT = Op.getValueType();
432 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengc356a572006-09-12 21:04:05 +0000433 Constant *C = CP->getConstVal();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000434 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
435
436 return CPI;
437}
438
439static SDOperand LowerGlobalAddress(SDOperand Op,
440 SelectionDAG &DAG) {
441 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Rafael Espindola61369da2006-08-14 19:01:24 +0000442 int alignment = 2;
443 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
Evan Cheng466685d2006-10-09 20:57:25 +0000444 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr, NULL, 0);
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000445}
446
Rafael Espindola755be9b2006-08-25 17:55:16 +0000447static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
448 unsigned VarArgsFrameIndex) {
449 // vastart just stores the address of the VarArgsFrameIndex slot into the
450 // memory location argument.
451 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
452 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Evan Cheng786225a2006-10-05 23:01:46 +0000453 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), Op.getOperand(2));
Rafael Espindola755be9b2006-08-25 17:55:16 +0000454}
455
456static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
457 int &VarArgsFrameIndex) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000458 MachineFunction &MF = DAG.getMachineFunction();
459 MachineFrameInfo *MFI = MF.getFrameInfo();
460 SSARegMap *RegMap = MF.getSSARegMap();
461 unsigned NumArgs = Op.Val->getNumValues()-1;
462 SDOperand Root = Op.getOperand(0);
463 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
464 static const unsigned REGS[] = {
465 ARM::R0, ARM::R1, ARM::R2, ARM::R3
466 };
467
468 std::vector<MVT::ValueType> Types(Op.Val->value_begin(), Op.Val->value_end() - 1);
469 ArgumentLayout Layout(Types);
470
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000471 std::vector<SDOperand> ArgValues;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000472 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000473 MVT::ValueType VT = Types[ArgNo];
Rafael Espindola4b442b52006-05-23 02:48:20 +0000474
Rafael Espindolaa2845842006-10-05 16:48:49 +0000475 SDOperand Value;
476 if (Layout.isRegister(ArgNo)) {
477 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
478 unsigned RegNum = Layout.getRegisterNum(ArgNo);
479 unsigned Reg1 = REGS[RegNum];
480 unsigned VReg1 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
481 SDOperand Value1 = DAG.getCopyFromReg(Root, VReg1, MVT::i32);
482 MF.addLiveIn(Reg1, VReg1);
483 if (VT == MVT::f64) {
484 unsigned Reg2 = REGS[RegNum + 1];
485 unsigned VReg2 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
486 SDOperand Value2 = DAG.getCopyFromReg(Root, VReg2, MVT::i32);
487 MF.addLiveIn(Reg2, VReg2);
488 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
489 } else {
490 Value = Value1;
491 if (VT == MVT::f32)
492 Value = DAG.getNode(ISD::BIT_CONVERT, VT, Value);
493 }
494 } else {
495 // If the argument is actually used, emit a load from the right stack
496 // slot.
497 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
498 unsigned Offset = Layout.getOffset(ArgNo);
499 unsigned Size = MVT::getSizeInBits(VT)/8;
500 int FI = MFI->CreateFixedObject(Size, Offset);
501 SDOperand FIN = DAG.getFrameIndex(FI, VT);
Evan Cheng466685d2006-10-09 20:57:25 +0000502 Value = DAG.getLoad(VT, Root, FIN, NULL, 0);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000503 } else {
504 Value = DAG.getNode(ISD::UNDEF, VT);
505 }
506 }
507 ArgValues.push_back(Value);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000508 }
509
Rafael Espindolaa2845842006-10-05 16:48:49 +0000510 unsigned NextRegNum = Layout.lastRegNum() + 1;
511
Rafael Espindola755be9b2006-08-25 17:55:16 +0000512 if (isVarArg) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000513 //If this function is vararg we must store the remaing
514 //registers so that they can be acessed with va_start
Rafael Espindola755be9b2006-08-25 17:55:16 +0000515 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000516 -16 + NextRegNum * 4);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000517
Rafael Espindola755be9b2006-08-25 17:55:16 +0000518 SmallVector<SDOperand, 4> MemOps;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000519 for (unsigned RegNo = NextRegNum; RegNo < 4; ++RegNo) {
520 int RegOffset = - (4 - RegNo) * 4;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000521 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000522 RegOffset);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000523 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
524
Rafael Espindolaa2845842006-10-05 16:48:49 +0000525 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
526 MF.addLiveIn(REGS[RegNo], VReg);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000527
528 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
Evan Cheng786225a2006-10-05 23:01:46 +0000529 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN,
530 DAG.getSrcValue(NULL));
Rafael Espindola755be9b2006-08-25 17:55:16 +0000531 MemOps.push_back(Store);
532 }
533 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
534 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000535
536 ArgValues.push_back(Root);
537
538 // Return the new list of results.
539 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
540 Op.Val->value_end());
Chris Lattner87428672006-08-11 17:22:35 +0000541 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
Rafael Espindoladc124a22006-05-18 21:45:49 +0000542}
543
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000544static SDOperand GetCMP(ISD::CondCode CC, SDOperand LHS, SDOperand RHS,
545 SelectionDAG &DAG) {
546 MVT::ValueType vt = LHS.getValueType();
Rafael Espindola0d9fe762006-10-10 16:33:47 +0000547 assert(vt == MVT::i32 || vt == MVT::f32 || vt == MVT::f64);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000548 //Note: unordered floating point compares should use a non throwing
549 //compare.
Rafael Espindola0d9fe762006-10-10 16:33:47 +0000550 bool isUnorderedFloat = (vt == MVT::f32 || vt == MVT::f64) &&
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000551 (CC >= ISD::SETUO && CC <= ISD::SETUNE);
552 assert(!isUnorderedFloat && "Unordered float compares are not supported");
553
554 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
555 if (vt != MVT::i32)
556 Cmp = DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp);
557 return Cmp;
558}
559
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000560static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
561 SDOperand LHS = Op.getOperand(0);
562 SDOperand RHS = Op.getOperand(1);
563 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
564 SDOperand TrueVal = Op.getOperand(2);
565 SDOperand FalseVal = Op.getOperand(3);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000566 SDOperand Cmp = GetCMP(CC, LHS, RHS, DAG);
Rafael Espindolacdda88c2006-08-24 17:19:08 +0000567 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000568 return DAG.getNode(ARMISD::SELECT, MVT::i32, TrueVal, FalseVal, ARMCC, Cmp);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000569}
570
Rafael Espindola687bc492006-08-24 13:45:55 +0000571static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
572 SDOperand Chain = Op.getOperand(0);
573 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
574 SDOperand LHS = Op.getOperand(2);
575 SDOperand RHS = Op.getOperand(3);
576 SDOperand Dest = Op.getOperand(4);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000577 SDOperand Cmp = GetCMP(CC, LHS, RHS, DAG);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000578 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000579 return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, ARMCC, Cmp);
Rafael Espindola687bc492006-08-24 13:45:55 +0000580}
581
Rafael Espindola27185192006-09-29 21:20:16 +0000582static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola9e071f02006-10-02 19:30:56 +0000583 SDOperand IntVal = Op.getOperand(0);
Rafael Espindola27185192006-09-29 21:20:16 +0000584 assert(IntVal.getValueType() == MVT::i32);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000585 MVT::ValueType vt = Op.getValueType();
586 assert(vt == MVT::f32 ||
587 vt == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000588
589 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000590 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FSITOS : ARMISD::FSITOD;
591 return DAG.getNode(op, vt, Tmp);
Rafael Espindola27185192006-09-29 21:20:16 +0000592}
593
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000594static SDOperand LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
595 assert(Op.getValueType() == MVT::i32);
596 SDOperand FloatVal = Op.getOperand(0);
597 MVT::ValueType vt = FloatVal.getValueType();
598 assert(vt == MVT::f32 || vt == MVT::f64);
599
600 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FTOSIS : ARMISD::FTOSID;
601 SDOperand Tmp = DAG.getNode(op, MVT::f32, FloatVal);
602 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Tmp);
603}
604
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000605static SDOperand LowerUINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
606 SDOperand IntVal = Op.getOperand(0);
607 assert(IntVal.getValueType() == MVT::i32);
608 MVT::ValueType vt = Op.getValueType();
609 assert(vt == MVT::f32 ||
610 vt == MVT::f64);
611
612 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
613 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FUITOS : ARMISD::FUITOD;
614 return DAG.getNode(op, vt, Tmp);
615}
616
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000617SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
618 switch (Op.getOpcode()) {
619 default:
620 assert(0 && "Should not custom lower this!");
Rafael Espindola1c8f0532006-05-15 22:34:39 +0000621 abort();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000622 case ISD::ConstantPool:
623 return LowerConstantPool(Op, DAG);
624 case ISD::GlobalAddress:
625 return LowerGlobalAddress(Op, DAG);
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000626 case ISD::FP_TO_SINT:
627 return LowerFP_TO_SINT(Op, DAG);
Rafael Espindola27185192006-09-29 21:20:16 +0000628 case ISD::SINT_TO_FP:
629 return LowerSINT_TO_FP(Op, DAG);
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000630 case ISD::UINT_TO_FP:
631 return LowerUINT_TO_FP(Op, DAG);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000632 case ISD::FORMAL_ARGUMENTS:
Rafael Espindola755be9b2006-08-25 17:55:16 +0000633 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000634 case ISD::CALL:
635 return LowerCALL(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000636 case ISD::RET:
637 return LowerRET(Op, DAG);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000638 case ISD::SELECT_CC:
639 return LowerSELECT_CC(Op, DAG);
Rafael Espindola687bc492006-08-24 13:45:55 +0000640 case ISD::BR_CC:
641 return LowerBR_CC(Op, DAG);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000642 case ISD::VASTART:
643 return LowerVASTART(Op, DAG, VarArgsFrameIndex);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000644 }
645}
646
647//===----------------------------------------------------------------------===//
648// Instruction Selector Implementation
649//===----------------------------------------------------------------------===//
650
651//===--------------------------------------------------------------------===//
652/// ARMDAGToDAGISel - ARM specific code to select ARM machine
653/// instructions for SelectionDAG operations.
654///
655namespace {
656class ARMDAGToDAGISel : public SelectionDAGISel {
657 ARMTargetLowering Lowering;
658
659public:
660 ARMDAGToDAGISel(TargetMachine &TM)
661 : SelectionDAGISel(Lowering), Lowering(TM) {
662 }
663
Evan Cheng9ade2182006-08-26 05:34:46 +0000664 SDNode *Select(SDOperand Op);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000665 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000666 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000667 bool SelectAddrMode1(SDOperand N, SDOperand &Arg, SDOperand &Shift,
668 SDOperand &ShiftType);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000669
670 // Include the pieces autogenerated from the target description.
671#include "ARMGenDAGISel.inc"
672};
673
674void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
675 DEBUG(BB->dump());
676
677 DAG.setRoot(SelectRoot(DAG.getRoot()));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000678 DAG.RemoveDeadNodes();
679
680 ScheduleAndEmitDAG(DAG);
681}
682
Rafael Espindola61369da2006-08-14 19:01:24 +0000683static bool isInt12Immediate(SDNode *N, short &Imm) {
684 if (N->getOpcode() != ISD::Constant)
685 return false;
686
687 int32_t t = cast<ConstantSDNode>(N)->getValue();
Rafael Espindola7246d332006-09-21 11:29:52 +0000688 int max = 1<<12;
Rafael Espindola61369da2006-08-14 19:01:24 +0000689 int min = -max;
690 if (t > min && t < max) {
691 Imm = t;
692 return true;
693 }
694 else
695 return false;
696}
697
698static bool isInt12Immediate(SDOperand Op, short &Imm) {
699 return isInt12Immediate(Op.Val, Imm);
700}
701
Rafael Espindola7246d332006-09-21 11:29:52 +0000702static uint32_t rotateL(uint32_t x) {
703 uint32_t bit31 = (x & (1 << 31)) >> 31;
704 uint32_t t = x << 1;
705 return t | bit31;
706}
707
708static bool isUInt8Immediate(uint32_t x) {
709 return x < (1 << 8);
710}
711
712static bool isRotInt8Immediate(uint32_t x) {
713 int r;
714 for (r = 0; r < 16; r++) {
715 if (isUInt8Immediate(x))
716 return true;
717 x = rotateL(rotateL(x));
718 }
719 return false;
720}
721
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000722bool ARMDAGToDAGISel::SelectAddrMode1(SDOperand N,
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000723 SDOperand &Arg,
724 SDOperand &Shift,
725 SDOperand &ShiftType) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000726 switch(N.getOpcode()) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000727 case ISD::Constant: {
Rafael Espindola7246d332006-09-21 11:29:52 +0000728 uint32_t val = cast<ConstantSDNode>(N)->getValue();
729 if(!isRotInt8Immediate(val)) {
730 const Type *t = MVT::getTypeForValueType(MVT::i32);
731 Constant *C = ConstantUInt::get(t, val);
732 int alignment = 2;
733 SDOperand Addr = CurDAG->getTargetConstantPool(C, MVT::i32, alignment);
734 SDOperand Z = CurDAG->getTargetConstant(0, MVT::i32);
735 SDNode *n = CurDAG->getTargetNode(ARM::ldr, MVT::i32, Z, Addr);
736 Arg = SDOperand(n, 0);
737 } else
738 Arg = CurDAG->getTargetConstant(val, MVT::i32);
739
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000740 Shift = CurDAG->getTargetConstant(0, MVT::i32);
741 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000742 return true;
743 }
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000744 case ISD::SRA:
745 Arg = N.getOperand(0);
746 Shift = N.getOperand(1);
747 ShiftType = CurDAG->getTargetConstant(ARMShift::ASR, MVT::i32);
748 return true;
749 case ISD::SRL:
750 Arg = N.getOperand(0);
751 Shift = N.getOperand(1);
752 ShiftType = CurDAG->getTargetConstant(ARMShift::LSR, MVT::i32);
753 return true;
754 case ISD::SHL:
755 Arg = N.getOperand(0);
756 Shift = N.getOperand(1);
757 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
758 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000759 }
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000760
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000761 Arg = N;
762 Shift = CurDAG->getTargetConstant(0, MVT::i32);
763 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000764 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000765}
766
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000767//register plus/minus 12 bit offset
768bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
769 SDOperand &Base) {
Rafael Espindolaf3a335c2006-08-17 17:09:40 +0000770 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
771 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
772 Offset = CurDAG->getTargetConstant(0, MVT::i32);
773 return true;
774 }
Rafael Espindola61369da2006-08-14 19:01:24 +0000775 if (N.getOpcode() == ISD::ADD) {
776 short imm = 0;
777 if (isInt12Immediate(N.getOperand(1), imm)) {
778 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
779 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
780 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
781 } else {
782 Base = N.getOperand(0);
783 }
784 return true; // [r+i]
785 }
786 }
787
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000788 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000789 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
790 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
791 }
792 else
793 Base = N;
794 return true; //any address fits in a register
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000795}
796
Evan Cheng9ade2182006-08-26 05:34:46 +0000797SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000798 SDNode *N = Op.Val;
799
800 switch (N->getOpcode()) {
801 default:
Evan Cheng9ade2182006-08-26 05:34:46 +0000802 return SelectCode(Op);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000803 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000804 }
Evan Cheng64a752f2006-08-11 09:08:15 +0000805 return NULL;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000806}
807
808} // end anonymous namespace
809
810/// createARMISelDag - This pass converts a legalized DAG into a
811/// ARM-specific DAG, ready for instruction scheduling.
812///
813FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
814 return new ARMDAGToDAGISel(TM);
815}