blob: f629bc3f80c4c73c1ab25ff6ca1cfc98b6469a0c [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,
95
Rafael Espindola687bc492006-08-24 13:45:55 +000096 SELECT,
97
Rafael Espindola27185192006-09-29 21:20:16 +000098 BR,
99
Rafael Espindola9e071f02006-10-02 19:30:56 +0000100 FSITOS,
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000101 FTOSIS,
Rafael Espindola9e071f02006-10-02 19:30:56 +0000102
103 FSITOD,
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000104 FTOSID,
Rafael Espindola9e071f02006-10-02 19:30:56 +0000105
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000106 FUITOS,
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000107 FTOUIS,
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000108
109 FUITOD,
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000110 FTOUID,
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000111
Rafael Espindolaa2845842006-10-05 16:48:49 +0000112 FMRRD,
113
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000114 FMDRR,
115
116 FMSTAT
Rafael Espindola84b19be2006-07-16 01:02:57 +0000117 };
118 }
119}
120
Rafael Espindola6f602de2006-08-24 16:13:15 +0000121/// DAGCCToARMCC - Convert a DAG integer condition code to an ARM CC
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000122//Note: ARM doesn't have condition codes corresponding to the ordered
123//condition codes of LLVM. We use exception raising instructions so
124//that we can be sure that V == 0 and test only the rest of the expression.
Rafael Espindola6f602de2006-08-24 16:13:15 +0000125static ARMCC::CondCodes DAGCCToARMCC(ISD::CondCode CC) {
126 switch (CC) {
Rafael Espindolaebdabda2006-09-21 13:06:26 +0000127 default:
128 std::cerr << "CC = " << CC << "\n";
129 assert(0 && "Unknown condition code!");
130 case ISD::SETUGT: return ARMCC::HI;
131 case ISD::SETULE: return ARMCC::LS;
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000132 case ISD::SETLE:
133 case ISD::SETOLE: return ARMCC::LE;
134 case ISD::SETLT:
135 case ISD::SETOLT: return ARMCC::LT;
136 case ISD::SETGT:
137 case ISD::SETOGT: return ARMCC::GT;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000138 case ISD::SETNE: return ARMCC::NE;
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000139 case ISD::SETEQ:
140 case ISD::SETOEQ: return ARMCC::EQ;
141 case ISD::SETGE:
142 case ISD::SETOGE: return ARMCC::GE;
Rafael Espindola5f450d22006-09-02 20:24:25 +0000143 case ISD::SETUGE: return ARMCC::CS;
Rafael Espindolabc4cec92006-09-03 13:19:16 +0000144 case ISD::SETULT: return ARMCC::CC;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000145 }
146}
147
Rafael Espindola84b19be2006-07-16 01:02:57 +0000148const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
149 switch (Opcode) {
150 default: return 0;
151 case ARMISD::CALL: return "ARMISD::CALL";
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000152 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000153 case ARMISD::SELECT: return "ARMISD::SELECT";
154 case ARMISD::CMP: return "ARMISD::CMP";
Rafael Espindola687bc492006-08-24 13:45:55 +0000155 case ARMISD::BR: return "ARMISD::BR";
Rafael Espindola27185192006-09-29 21:20:16 +0000156 case ARMISD::FSITOS: return "ARMISD::FSITOS";
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000157 case ARMISD::FTOSIS: return "ARMISD::FTOSIS";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000158 case ARMISD::FSITOD: return "ARMISD::FSITOD";
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000159 case ARMISD::FTOSID: return "ARMISD::FTOSID";
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000160 case ARMISD::FUITOS: return "ARMISD::FUITOS";
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000161 case ARMISD::FTOUIS: return "ARMISD::FTOUIS";
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000162 case ARMISD::FUITOD: return "ARMISD::FUITOD";
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000163 case ARMISD::FTOUID: return "ARMISD::FTOUID";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000164 case ARMISD::FMRRD: return "ARMISD::FMRRD";
Rafael Espindolaa2845842006-10-05 16:48:49 +0000165 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000166 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Rafael Espindola84b19be2006-07-16 01:02:57 +0000167 }
168}
169
Rafael Espindolaa2845842006-10-05 16:48:49 +0000170class ArgumentLayout {
171 std::vector<bool> is_reg;
172 std::vector<unsigned> pos;
173 std::vector<MVT::ValueType> types;
174public:
Rafael Espindola39b5a212006-10-05 17:46:48 +0000175 ArgumentLayout(const std::vector<MVT::ValueType> &Types) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000176 types = Types;
177
178 unsigned RegNum = 0;
179 unsigned StackOffset = 0;
Rafael Espindola39b5a212006-10-05 17:46:48 +0000180 for(std::vector<MVT::ValueType>::const_iterator I = Types.begin();
Rafael Espindolaa2845842006-10-05 16:48:49 +0000181 I != Types.end();
182 ++I) {
183 MVT::ValueType VT = *I;
184 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
185 unsigned size = MVT::getSizeInBits(VT)/32;
186
187 RegNum = ((RegNum + size - 1) / size) * size;
188 if (RegNum < 4) {
189 pos.push_back(RegNum);
190 is_reg.push_back(true);
191 RegNum += size;
192 } else {
193 unsigned bytes = size * 32/8;
194 StackOffset = ((StackOffset + bytes - 1) / bytes) * bytes;
195 pos.push_back(StackOffset);
196 is_reg.push_back(false);
197 StackOffset += bytes;
198 }
199 }
200 }
201 unsigned getRegisterNum(unsigned argNum) {
202 assert(isRegister(argNum));
203 return pos[argNum];
204 }
205 unsigned getOffset(unsigned argNum) {
206 assert(isOffset(argNum));
207 return pos[argNum];
208 }
209 unsigned isRegister(unsigned argNum) {
210 assert(argNum < is_reg.size());
211 return is_reg[argNum];
212 }
213 unsigned isOffset(unsigned argNum) {
214 return !isRegister(argNum);
215 }
216 MVT::ValueType getType(unsigned argNum) {
217 assert(argNum < types.size());
218 return types[argNum];
219 }
220 unsigned getStackSize(void) {
221 int last = is_reg.size() - 1;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000222 if (last < 0)
223 return 0;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000224 if (isRegister(last))
225 return 0;
226 return getOffset(last) + MVT::getSizeInBits(getType(last))/8;
227 }
228 int lastRegArg(void) {
229 int size = is_reg.size();
230 int last = 0;
231 while(last < size && isRegister(last))
232 last++;
233 last--;
234 return last;
235 }
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000236 int lastRegNum(void) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000237 int l = lastRegArg();
238 if (l < 0)
239 return -1;
240 unsigned r = getRegisterNum(l);
241 MVT::ValueType t = getType(l);
242 assert(t == MVT::i32 || t == MVT::f32 || t == MVT::f64);
243 if (t == MVT::f64)
244 return r + 1;
245 return r;
246 }
247};
248
Rafael Espindola84b19be2006-07-16 01:02:57 +0000249// This transforms a ISD::CALL node into a
250// callseq_star <- ARMISD:CALL <- callseq_end
251// chain
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000252static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola84b19be2006-07-16 01:02:57 +0000253 SDOperand Chain = Op.getOperand(0);
254 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
255 assert(CallConv == CallingConv::C && "unknown calling convention");
256 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000257 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
258 assert(isTailCall == false && "tail call not supported");
259 SDOperand Callee = Op.getOperand(4);
260 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Rafael Espindola1a009462006-08-08 13:02:29 +0000261 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000262 static const unsigned regs[] = {
Rafael Espindolafac00a92006-07-25 20:17:20 +0000263 ARM::R0, ARM::R1, ARM::R2, ARM::R3
264 };
265
Rafael Espindolaa2845842006-10-05 16:48:49 +0000266 std::vector<MVT::ValueType> Types;
267 for (unsigned i = 0; i < NumOps; ++i) {
268 MVT::ValueType VT = Op.getOperand(5+2*i).getValueType();
269 Types.push_back(VT);
270 }
271 ArgumentLayout Layout(Types);
Rafael Espindolafac00a92006-07-25 20:17:20 +0000272
Rafael Espindolaa2845842006-10-05 16:48:49 +0000273 unsigned NumBytes = Layout.getStackSize();
274
275 Chain = DAG.getCALLSEQ_START(Chain,
276 DAG.getConstant(NumBytes, MVT::i32));
277
278 //Build a sequence of stores
279 std::vector<SDOperand> MemOpChains;
280 for (unsigned i = Layout.lastRegArg() + 1; i < NumOps; ++i) {
281 SDOperand Arg = Op.getOperand(5+2*i);
282 unsigned ArgOffset = Layout.getOffset(i);
283 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
284 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Evan Cheng786225a2006-10-05 23:01:46 +0000285 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff,
286 DAG.getSrcValue(NULL)));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000287 }
Rafael Espindola1a009462006-08-08 13:02:29 +0000288 if (!MemOpChains.empty())
Chris Lattnere2199452006-08-11 17:38:39 +0000289 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
290 &MemOpChains[0], MemOpChains.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000291
Rafael Espindola84b19be2006-07-16 01:02:57 +0000292 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
293 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
294 // node so that legalize doesn't hack it.
295 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
296 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
297
298 // If this is a direct call, pass the chain and the callee.
299 assert (Callee.Val);
300 std::vector<SDOperand> Ops;
301 Ops.push_back(Chain);
302 Ops.push_back(Callee);
303
Rafael Espindolaa2845842006-10-05 16:48:49 +0000304 // Build a sequence of copy-to-reg nodes chained together with token chain
305 // and flag operands which copy the outgoing args into the appropriate regs.
306 SDOperand InFlag;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000307 for (int i = 0, e = Layout.lastRegArg(); i <= e; ++i) {
Rafael Espindola4a408d42006-10-06 12:50:22 +0000308 SDOperand Arg = Op.getOperand(5+2*i);
309 unsigned RegNum = Layout.getRegisterNum(i);
310 unsigned Reg1 = regs[RegNum];
311 MVT::ValueType VT = Layout.getType(i);
312 assert(VT == Arg.getValueType());
313 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000314
315 // Add argument register to the end of the list so that it is known live
316 // into the call.
Rafael Espindola4a408d42006-10-06 12:50:22 +0000317 Ops.push_back(DAG.getRegister(Reg1, MVT::i32));
318 if (VT == MVT::f64) {
319 unsigned Reg2 = regs[RegNum + 1];
320 SDOperand SDReg1 = DAG.getRegister(Reg1, MVT::i32);
321 SDOperand SDReg2 = DAG.getRegister(Reg2, MVT::i32);
322
323 Ops.push_back(DAG.getRegister(Reg2, MVT::i32));
324 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola935b1f82006-10-06 20:33:26 +0000325 SDOperand Ops[] = {Chain, SDReg1, SDReg2, Arg, InFlag};
326 Chain = DAG.getNode(ARMISD::FMRRD, VTs, Ops, InFlag.Val ? 5 : 4);
Rafael Espindola4a408d42006-10-06 12:50:22 +0000327 } else {
328 if (VT == MVT::f32)
329 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg);
330 Chain = DAG.getCopyToReg(Chain, Reg1, Arg, InFlag);
331 }
332 InFlag = Chain.getValue(1);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000333 }
334
335 std::vector<MVT::ValueType> NodeTys;
336 NodeTys.push_back(MVT::Other); // Returns a chain
337 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Rafael Espindola7a53bd02006-08-09 16:41:12 +0000338
Rafael Espindola84b19be2006-07-16 01:02:57 +0000339 unsigned CallOpc = ARMISD::CALL;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000340 if (InFlag.Val)
341 Ops.push_back(InFlag);
Chris Lattner87428672006-08-11 17:22:35 +0000342 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000343 InFlag = Chain.getValue(1);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000344
Rafael Espindolafac00a92006-07-25 20:17:20 +0000345 std::vector<SDOperand> ResultVals;
346 NodeTys.clear();
347
348 // If the call has results, copy the values out of the ret val registers.
Rafael Espindola614057b2006-10-06 19:10:05 +0000349 MVT::ValueType VT = Op.Val->getValueType(0);
350 if (VT != MVT::Other) {
351 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
352 SDOperand Value;
353
354 SDOperand Value1 = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
355 Chain = Value1.getValue(1);
356 InFlag = Value1.getValue(2);
357 if (VT == MVT::i32)
358 Value = Value1;
359 if (VT == MVT::f32)
360 Value = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Value1);
361 if (VT == MVT::f64) {
362 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
363 Chain = Value2.getValue(1);
364 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
365 }
366 ResultVals.push_back(Value);
367 NodeTys.push_back(VT);
Rafael Espindolafac00a92006-07-25 20:17:20 +0000368 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000369
370 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
371 DAG.getConstant(NumBytes, MVT::i32));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000372 NodeTys.push_back(MVT::Other);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000373
Rafael Espindolafac00a92006-07-25 20:17:20 +0000374 if (ResultVals.empty())
375 return Chain;
376
377 ResultVals.push_back(Chain);
Chris Lattner87428672006-08-11 17:22:35 +0000378 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
379 ResultVals.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000380 return Res.getValue(Op.ResNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000381}
382
383static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
384 SDOperand Copy;
Rafael Espindola4b023672006-06-05 22:26:14 +0000385 SDOperand Chain = Op.getOperand(0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000386 SDOperand R0 = DAG.getRegister(ARM::R0, MVT::i32);
387 SDOperand R1 = DAG.getRegister(ARM::R1, MVT::i32);
388
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000389 switch(Op.getNumOperands()) {
390 default:
391 assert(0 && "Do not know how to return this many arguments!");
392 abort();
Rafael Espindola4b023672006-06-05 22:26:14 +0000393 case 1: {
394 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
Rafael Espindola6312da02006-08-03 22:50:11 +0000395 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
Rafael Espindola4b023672006-06-05 22:26:14 +0000396 }
Rafael Espindola27185192006-09-29 21:20:16 +0000397 case 3: {
398 SDOperand Val = Op.getOperand(1);
399 assert(Val.getValueType() == MVT::i32 ||
Rafael Espindola9e071f02006-10-02 19:30:56 +0000400 Val.getValueType() == MVT::f32 ||
401 Val.getValueType() == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000402
Rafael Espindola9e071f02006-10-02 19:30:56 +0000403 if (Val.getValueType() == MVT::f64) {
404 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
405 SDOperand Ops[] = {Chain, R0, R1, Val};
406 Copy = DAG.getNode(ARMISD::FMRRD, VTs, Ops, 4);
407 } else {
408 if (Val.getValueType() == MVT::f32)
409 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
410 Copy = DAG.getCopyToReg(Chain, R0, Val, SDOperand());
411 }
412
413 if (DAG.getMachineFunction().liveout_empty()) {
Rafael Espindola4b023672006-06-05 22:26:14 +0000414 DAG.getMachineFunction().addLiveOut(ARM::R0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000415 if (Val.getValueType() == MVT::f64)
416 DAG.getMachineFunction().addLiveOut(ARM::R1);
417 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000418 break;
Rafael Espindola27185192006-09-29 21:20:16 +0000419 }
Rafael Espindola3a02f022006-09-04 19:05:01 +0000420 case 5:
421 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
422 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
423 // If we haven't noted the R0+R1 are live out, do so now.
424 if (DAG.getMachineFunction().liveout_empty()) {
425 DAG.getMachineFunction().addLiveOut(ARM::R0);
426 DAG.getMachineFunction().addLiveOut(ARM::R1);
427 }
428 break;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000429 }
Rafael Espindola4b023672006-06-05 22:26:14 +0000430
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000431 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
432 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000433}
434
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000435static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
436 MVT::ValueType PtrVT = Op.getValueType();
437 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengc356a572006-09-12 21:04:05 +0000438 Constant *C = CP->getConstVal();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000439 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
440
441 return CPI;
442}
443
444static SDOperand LowerGlobalAddress(SDOperand Op,
445 SelectionDAG &DAG) {
446 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Rafael Espindola61369da2006-08-14 19:01:24 +0000447 int alignment = 2;
448 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
Evan Cheng466685d2006-10-09 20:57:25 +0000449 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr, NULL, 0);
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000450}
451
Rafael Espindola755be9b2006-08-25 17:55:16 +0000452static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
453 unsigned VarArgsFrameIndex) {
454 // vastart just stores the address of the VarArgsFrameIndex slot into the
455 // memory location argument.
456 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
457 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Evan Cheng786225a2006-10-05 23:01:46 +0000458 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), Op.getOperand(2));
Rafael Espindola755be9b2006-08-25 17:55:16 +0000459}
460
461static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
462 int &VarArgsFrameIndex) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000463 MachineFunction &MF = DAG.getMachineFunction();
464 MachineFrameInfo *MFI = MF.getFrameInfo();
465 SSARegMap *RegMap = MF.getSSARegMap();
466 unsigned NumArgs = Op.Val->getNumValues()-1;
467 SDOperand Root = Op.getOperand(0);
468 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
469 static const unsigned REGS[] = {
470 ARM::R0, ARM::R1, ARM::R2, ARM::R3
471 };
472
473 std::vector<MVT::ValueType> Types(Op.Val->value_begin(), Op.Val->value_end() - 1);
474 ArgumentLayout Layout(Types);
475
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000476 std::vector<SDOperand> ArgValues;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000477 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000478 MVT::ValueType VT = Types[ArgNo];
Rafael Espindola4b442b52006-05-23 02:48:20 +0000479
Rafael Espindolaa2845842006-10-05 16:48:49 +0000480 SDOperand Value;
481 if (Layout.isRegister(ArgNo)) {
482 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
483 unsigned RegNum = Layout.getRegisterNum(ArgNo);
484 unsigned Reg1 = REGS[RegNum];
485 unsigned VReg1 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
486 SDOperand Value1 = DAG.getCopyFromReg(Root, VReg1, MVT::i32);
487 MF.addLiveIn(Reg1, VReg1);
488 if (VT == MVT::f64) {
489 unsigned Reg2 = REGS[RegNum + 1];
490 unsigned VReg2 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
491 SDOperand Value2 = DAG.getCopyFromReg(Root, VReg2, MVT::i32);
492 MF.addLiveIn(Reg2, VReg2);
493 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
494 } else {
495 Value = Value1;
496 if (VT == MVT::f32)
497 Value = DAG.getNode(ISD::BIT_CONVERT, VT, Value);
498 }
499 } else {
500 // If the argument is actually used, emit a load from the right stack
501 // slot.
502 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
503 unsigned Offset = Layout.getOffset(ArgNo);
504 unsigned Size = MVT::getSizeInBits(VT)/8;
505 int FI = MFI->CreateFixedObject(Size, Offset);
506 SDOperand FIN = DAG.getFrameIndex(FI, VT);
Evan Cheng466685d2006-10-09 20:57:25 +0000507 Value = DAG.getLoad(VT, Root, FIN, NULL, 0);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000508 } else {
509 Value = DAG.getNode(ISD::UNDEF, VT);
510 }
511 }
512 ArgValues.push_back(Value);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000513 }
514
Rafael Espindolaa2845842006-10-05 16:48:49 +0000515 unsigned NextRegNum = Layout.lastRegNum() + 1;
516
Rafael Espindola755be9b2006-08-25 17:55:16 +0000517 if (isVarArg) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000518 //If this function is vararg we must store the remaing
519 //registers so that they can be acessed with va_start
Rafael Espindola755be9b2006-08-25 17:55:16 +0000520 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000521 -16 + NextRegNum * 4);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000522
Rafael Espindola755be9b2006-08-25 17:55:16 +0000523 SmallVector<SDOperand, 4> MemOps;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000524 for (unsigned RegNo = NextRegNum; RegNo < 4; ++RegNo) {
525 int RegOffset = - (4 - RegNo) * 4;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000526 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000527 RegOffset);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000528 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
529
Rafael Espindolaa2845842006-10-05 16:48:49 +0000530 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
531 MF.addLiveIn(REGS[RegNo], VReg);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000532
533 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
Evan Cheng786225a2006-10-05 23:01:46 +0000534 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN,
535 DAG.getSrcValue(NULL));
Rafael Espindola755be9b2006-08-25 17:55:16 +0000536 MemOps.push_back(Store);
537 }
538 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
539 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000540
541 ArgValues.push_back(Root);
542
543 // Return the new list of results.
544 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
545 Op.Val->value_end());
Chris Lattner87428672006-08-11 17:22:35 +0000546 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
Rafael Espindoladc124a22006-05-18 21:45:49 +0000547}
548
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000549static SDOperand GetCMP(ISD::CondCode CC, SDOperand LHS, SDOperand RHS,
550 SelectionDAG &DAG) {
551 MVT::ValueType vt = LHS.getValueType();
Rafael Espindola0d9fe762006-10-10 16:33:47 +0000552 assert(vt == MVT::i32 || vt == MVT::f32 || vt == MVT::f64);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000553 //Note: unordered floating point compares should use a non throwing
554 //compare.
Rafael Espindola0d9fe762006-10-10 16:33:47 +0000555 bool isUnorderedFloat = (vt == MVT::f32 || vt == MVT::f64) &&
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000556 (CC >= ISD::SETUO && CC <= ISD::SETUNE);
557 assert(!isUnorderedFloat && "Unordered float compares are not supported");
558
559 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
560 if (vt != MVT::i32)
561 Cmp = DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp);
562 return Cmp;
563}
564
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000565static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
566 SDOperand LHS = Op.getOperand(0);
567 SDOperand RHS = Op.getOperand(1);
568 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
569 SDOperand TrueVal = Op.getOperand(2);
570 SDOperand FalseVal = Op.getOperand(3);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000571 SDOperand Cmp = GetCMP(CC, LHS, RHS, DAG);
Rafael Espindolacdda88c2006-08-24 17:19:08 +0000572 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000573 return DAG.getNode(ARMISD::SELECT, MVT::i32, TrueVal, FalseVal, ARMCC, Cmp);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000574}
575
Rafael Espindola687bc492006-08-24 13:45:55 +0000576static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
577 SDOperand Chain = Op.getOperand(0);
578 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
579 SDOperand LHS = Op.getOperand(2);
580 SDOperand RHS = Op.getOperand(3);
581 SDOperand Dest = Op.getOperand(4);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000582 SDOperand Cmp = GetCMP(CC, LHS, RHS, DAG);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000583 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000584 return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, ARMCC, Cmp);
Rafael Espindola687bc492006-08-24 13:45:55 +0000585}
586
Rafael Espindola27185192006-09-29 21:20:16 +0000587static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola9e071f02006-10-02 19:30:56 +0000588 SDOperand IntVal = Op.getOperand(0);
Rafael Espindola27185192006-09-29 21:20:16 +0000589 assert(IntVal.getValueType() == MVT::i32);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000590 MVT::ValueType vt = Op.getValueType();
591 assert(vt == MVT::f32 ||
592 vt == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000593
594 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000595 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FSITOS : ARMISD::FSITOD;
596 return DAG.getNode(op, vt, Tmp);
Rafael Espindola27185192006-09-29 21:20:16 +0000597}
598
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000599static SDOperand LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
600 assert(Op.getValueType() == MVT::i32);
601 SDOperand FloatVal = Op.getOperand(0);
602 MVT::ValueType vt = FloatVal.getValueType();
603 assert(vt == MVT::f32 || vt == MVT::f64);
604
605 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FTOSIS : ARMISD::FTOSID;
606 SDOperand Tmp = DAG.getNode(op, MVT::f32, FloatVal);
607 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Tmp);
608}
609
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000610static SDOperand LowerUINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
611 SDOperand IntVal = Op.getOperand(0);
612 assert(IntVal.getValueType() == MVT::i32);
613 MVT::ValueType vt = Op.getValueType();
614 assert(vt == MVT::f32 ||
615 vt == MVT::f64);
616
617 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
618 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FUITOS : ARMISD::FUITOD;
619 return DAG.getNode(op, vt, Tmp);
620}
621
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000622static SDOperand LowerFP_TO_UINT(SDOperand Op, SelectionDAG &DAG) {
623 assert(Op.getValueType() == MVT::i32);
624 SDOperand FloatVal = Op.getOperand(0);
625 MVT::ValueType vt = FloatVal.getValueType();
626 assert(vt == MVT::f32 || vt == MVT::f64);
627
628 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FTOUIS : ARMISD::FTOUID;
629 SDOperand Tmp = DAG.getNode(op, MVT::f32, FloatVal);
630 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Tmp);
631}
632
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000633SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
634 switch (Op.getOpcode()) {
635 default:
636 assert(0 && "Should not custom lower this!");
Rafael Espindola1c8f0532006-05-15 22:34:39 +0000637 abort();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000638 case ISD::ConstantPool:
639 return LowerConstantPool(Op, DAG);
640 case ISD::GlobalAddress:
641 return LowerGlobalAddress(Op, DAG);
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000642 case ISD::FP_TO_SINT:
643 return LowerFP_TO_SINT(Op, DAG);
Rafael Espindola27185192006-09-29 21:20:16 +0000644 case ISD::SINT_TO_FP:
645 return LowerSINT_TO_FP(Op, DAG);
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000646 case ISD::FP_TO_UINT:
647 return LowerFP_TO_UINT(Op, DAG);
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000648 case ISD::UINT_TO_FP:
649 return LowerUINT_TO_FP(Op, DAG);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000650 case ISD::FORMAL_ARGUMENTS:
Rafael Espindola755be9b2006-08-25 17:55:16 +0000651 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000652 case ISD::CALL:
653 return LowerCALL(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000654 case ISD::RET:
655 return LowerRET(Op, DAG);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000656 case ISD::SELECT_CC:
657 return LowerSELECT_CC(Op, DAG);
Rafael Espindola687bc492006-08-24 13:45:55 +0000658 case ISD::BR_CC:
659 return LowerBR_CC(Op, DAG);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000660 case ISD::VASTART:
661 return LowerVASTART(Op, DAG, VarArgsFrameIndex);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000662 }
663}
664
665//===----------------------------------------------------------------------===//
666// Instruction Selector Implementation
667//===----------------------------------------------------------------------===//
668
669//===--------------------------------------------------------------------===//
670/// ARMDAGToDAGISel - ARM specific code to select ARM machine
671/// instructions for SelectionDAG operations.
672///
673namespace {
674class ARMDAGToDAGISel : public SelectionDAGISel {
675 ARMTargetLowering Lowering;
676
677public:
678 ARMDAGToDAGISel(TargetMachine &TM)
679 : SelectionDAGISel(Lowering), Lowering(TM) {
680 }
681
Evan Cheng9ade2182006-08-26 05:34:46 +0000682 SDNode *Select(SDOperand Op);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000683 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000684 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000685 bool SelectAddrMode1(SDOperand N, SDOperand &Arg, SDOperand &Shift,
686 SDOperand &ShiftType);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000687
688 // Include the pieces autogenerated from the target description.
689#include "ARMGenDAGISel.inc"
690};
691
692void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
693 DEBUG(BB->dump());
694
695 DAG.setRoot(SelectRoot(DAG.getRoot()));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000696 DAG.RemoveDeadNodes();
697
698 ScheduleAndEmitDAG(DAG);
699}
700
Rafael Espindola61369da2006-08-14 19:01:24 +0000701static bool isInt12Immediate(SDNode *N, short &Imm) {
702 if (N->getOpcode() != ISD::Constant)
703 return false;
704
705 int32_t t = cast<ConstantSDNode>(N)->getValue();
Rafael Espindola7246d332006-09-21 11:29:52 +0000706 int max = 1<<12;
Rafael Espindola61369da2006-08-14 19:01:24 +0000707 int min = -max;
708 if (t > min && t < max) {
709 Imm = t;
710 return true;
711 }
712 else
713 return false;
714}
715
716static bool isInt12Immediate(SDOperand Op, short &Imm) {
717 return isInt12Immediate(Op.Val, Imm);
718}
719
Rafael Espindola7246d332006-09-21 11:29:52 +0000720static uint32_t rotateL(uint32_t x) {
721 uint32_t bit31 = (x & (1 << 31)) >> 31;
722 uint32_t t = x << 1;
723 return t | bit31;
724}
725
726static bool isUInt8Immediate(uint32_t x) {
727 return x < (1 << 8);
728}
729
730static bool isRotInt8Immediate(uint32_t x) {
731 int r;
732 for (r = 0; r < 16; r++) {
733 if (isUInt8Immediate(x))
734 return true;
735 x = rotateL(rotateL(x));
736 }
737 return false;
738}
739
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000740bool ARMDAGToDAGISel::SelectAddrMode1(SDOperand N,
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000741 SDOperand &Arg,
742 SDOperand &Shift,
743 SDOperand &ShiftType) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000744 switch(N.getOpcode()) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000745 case ISD::Constant: {
Rafael Espindola7246d332006-09-21 11:29:52 +0000746 uint32_t val = cast<ConstantSDNode>(N)->getValue();
747 if(!isRotInt8Immediate(val)) {
748 const Type *t = MVT::getTypeForValueType(MVT::i32);
749 Constant *C = ConstantUInt::get(t, val);
750 int alignment = 2;
751 SDOperand Addr = CurDAG->getTargetConstantPool(C, MVT::i32, alignment);
752 SDOperand Z = CurDAG->getTargetConstant(0, MVT::i32);
753 SDNode *n = CurDAG->getTargetNode(ARM::ldr, MVT::i32, Z, Addr);
754 Arg = SDOperand(n, 0);
755 } else
756 Arg = CurDAG->getTargetConstant(val, MVT::i32);
757
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000758 Shift = CurDAG->getTargetConstant(0, MVT::i32);
759 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000760 return true;
761 }
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000762 case ISD::SRA:
763 Arg = N.getOperand(0);
764 Shift = N.getOperand(1);
765 ShiftType = CurDAG->getTargetConstant(ARMShift::ASR, MVT::i32);
766 return true;
767 case ISD::SRL:
768 Arg = N.getOperand(0);
769 Shift = N.getOperand(1);
770 ShiftType = CurDAG->getTargetConstant(ARMShift::LSR, MVT::i32);
771 return true;
772 case ISD::SHL:
773 Arg = N.getOperand(0);
774 Shift = N.getOperand(1);
775 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
776 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000777 }
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000778
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000779 Arg = N;
780 Shift = CurDAG->getTargetConstant(0, MVT::i32);
781 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000782 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000783}
784
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000785//register plus/minus 12 bit offset
786bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
787 SDOperand &Base) {
Rafael Espindolaf3a335c2006-08-17 17:09:40 +0000788 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
789 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
790 Offset = CurDAG->getTargetConstant(0, MVT::i32);
791 return true;
792 }
Rafael Espindola61369da2006-08-14 19:01:24 +0000793 if (N.getOpcode() == ISD::ADD) {
794 short imm = 0;
795 if (isInt12Immediate(N.getOperand(1), imm)) {
796 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
797 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
798 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
799 } else {
800 Base = N.getOperand(0);
801 }
802 return true; // [r+i]
803 }
804 }
805
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000806 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000807 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
808 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
809 }
810 else
811 Base = N;
812 return true; //any address fits in a register
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000813}
814
Evan Cheng9ade2182006-08-26 05:34:46 +0000815SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000816 SDNode *N = Op.Val;
817
818 switch (N->getOpcode()) {
819 default:
Evan Cheng9ade2182006-08-26 05:34:46 +0000820 return SelectCode(Op);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000821 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000822 }
Evan Cheng64a752f2006-08-11 09:08:15 +0000823 return NULL;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000824}
825
826} // end anonymous namespace
827
828/// createARMISelDag - This pass converts a legalized DAG into a
829/// ARM-specific DAG, ready for instruction scheduling.
830///
831FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
832 return new ARMDAGToDAGISel(TM);
833}