blob: ffcdbc1180b7ac6fca651c066ec9c807e709316b [file] [log] [blame]
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001//===-- ARMISelDAGToDAG.cpp - A dag to dag inst selector for ARM ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an instruction selector for the ARM target.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ARM.h"
15#include "ARMTargetMachine.h"
Rafael Espindola84b19be2006-07-16 01:02:57 +000016#include "llvm/CallingConv.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000017#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
Rafael Espindola7246d332006-09-21 11:29:52 +000019#include "llvm/Constants.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000020#include "llvm/Intrinsics.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/SelectionDAG.h"
25#include "llvm/CodeGen/SelectionDAGISel.h"
26#include "llvm/CodeGen/SSARegMap.h"
27#include "llvm/Target/TargetLowering.h"
28#include "llvm/Support/Debug.h"
29#include <iostream>
Rafael Espindolaa2845842006-10-05 16:48:49 +000030#include <vector>
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000031using namespace llvm;
32
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000033namespace {
34 class ARMTargetLowering : public TargetLowering {
Rafael Espindola755be9b2006-08-25 17:55:16 +000035 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000036 public:
37 ARMTargetLowering(TargetMachine &TM);
38 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
Rafael Espindola84b19be2006-07-16 01:02:57 +000039 virtual const char *getTargetNodeName(unsigned Opcode) const;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000040 };
41
42}
43
44ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
45 : TargetLowering(TM) {
Rafael Espindola3717ca92006-08-20 01:49:49 +000046 addRegisterClass(MVT::i32, ARM::IntRegsRegisterClass);
Rafael Espindola27185192006-09-29 21:20:16 +000047 addRegisterClass(MVT::f32, ARM::FPRegsRegisterClass);
48 addRegisterClass(MVT::f64, ARM::DFPRegsRegisterClass);
Rafael Espindola3717ca92006-08-20 01:49:49 +000049
Rafael Espindola27185192006-09-29 21:20:16 +000050 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Rafael Espindola3717ca92006-08-20 01:49:49 +000051
Rafael Espindola06c1e7e2006-08-01 12:58:43 +000052 setOperationAction(ISD::RET, MVT::Other, Custom);
53 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
54 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Rafael Espindola341b8642006-08-04 12:48:42 +000055
Rafael Espindola3c000bf2006-08-21 22:00:32 +000056 setOperationAction(ISD::SETCC, MVT::i32, Expand);
57 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
Rafael Espindola687bc492006-08-24 13:45:55 +000058 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
Rafael Espindola3c000bf2006-08-21 22:00:32 +000059
Rafael Espindola755be9b2006-08-25 17:55:16 +000060 setOperationAction(ISD::VASTART, MVT::Other, Custom);
61 setOperationAction(ISD::VAEND, MVT::Other, Expand);
62
Rafael Espindolacd71da52006-10-03 17:27:58 +000063 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
64 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
65
Rafael Espindola341b8642006-08-04 12:48:42 +000066 setSchedulingPreference(SchedulingForRegPressure);
Rafael Espindola3717ca92006-08-20 01:49:49 +000067 computeRegisterProperties();
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000068}
69
Rafael Espindola84b19be2006-07-16 01:02:57 +000070namespace llvm {
71 namespace ARMISD {
72 enum NodeType {
73 // Start the numbering where the builting ops and target ops leave off.
74 FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
75 /// CALL - A direct function call.
Rafael Espindolaf4fda802006-08-03 17:02:20 +000076 CALL,
77
78 /// Return with a flag operand.
Rafael Espindola3c000bf2006-08-21 22:00:32 +000079 RET_FLAG,
80
81 CMP,
82
Rafael Espindola687bc492006-08-24 13:45:55 +000083 SELECT,
84
Rafael Espindola27185192006-09-29 21:20:16 +000085 BR,
86
Rafael Espindola9e071f02006-10-02 19:30:56 +000087 FSITOS,
88
89 FSITOD,
90
Rafael Espindolaa2845842006-10-05 16:48:49 +000091 FMRRD,
92
93 FMDRR
Rafael Espindola84b19be2006-07-16 01:02:57 +000094 };
95 }
96}
97
Rafael Espindola6f602de2006-08-24 16:13:15 +000098/// DAGCCToARMCC - Convert a DAG integer condition code to an ARM CC
99static ARMCC::CondCodes DAGCCToARMCC(ISD::CondCode CC) {
100 switch (CC) {
Rafael Espindolaebdabda2006-09-21 13:06:26 +0000101 default:
102 std::cerr << "CC = " << CC << "\n";
103 assert(0 && "Unknown condition code!");
104 case ISD::SETUGT: return ARMCC::HI;
105 case ISD::SETULE: return ARMCC::LS;
106 case ISD::SETLE: return ARMCC::LE;
107 case ISD::SETLT: return ARMCC::LT;
108 case ISD::SETGT: return ARMCC::GT;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000109 case ISD::SETNE: return ARMCC::NE;
Rafael Espindolacdda88c2006-08-24 17:19:08 +0000110 case ISD::SETEQ: return ARMCC::EQ;
Rafael Espindola5f450d22006-09-02 20:24:25 +0000111 case ISD::SETGE: return ARMCC::GE;
112 case ISD::SETUGE: return ARMCC::CS;
Rafael Espindolabc4cec92006-09-03 13:19:16 +0000113 case ISD::SETULT: return ARMCC::CC;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000114 }
115}
116
Rafael Espindola84b19be2006-07-16 01:02:57 +0000117const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
118 switch (Opcode) {
119 default: return 0;
120 case ARMISD::CALL: return "ARMISD::CALL";
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000121 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000122 case ARMISD::SELECT: return "ARMISD::SELECT";
123 case ARMISD::CMP: return "ARMISD::CMP";
Rafael Espindola687bc492006-08-24 13:45:55 +0000124 case ARMISD::BR: return "ARMISD::BR";
Rafael Espindola27185192006-09-29 21:20:16 +0000125 case ARMISD::FSITOS: return "ARMISD::FSITOS";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000126 case ARMISD::FSITOD: return "ARMISD::FSITOD";
127 case ARMISD::FMRRD: return "ARMISD::FMRRD";
Rafael Espindolaa2845842006-10-05 16:48:49 +0000128 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Rafael Espindola84b19be2006-07-16 01:02:57 +0000129 }
130}
131
Rafael Espindolaa2845842006-10-05 16:48:49 +0000132class ArgumentLayout {
133 std::vector<bool> is_reg;
134 std::vector<unsigned> pos;
135 std::vector<MVT::ValueType> types;
136public:
137 ArgumentLayout(std::vector<MVT::ValueType> Types) {
138 types = Types;
139
140 unsigned RegNum = 0;
141 unsigned StackOffset = 0;
142 for(std::vector<MVT::ValueType>::iterator I = Types.begin();
143 I != Types.end();
144 ++I) {
145 MVT::ValueType VT = *I;
146 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
147 unsigned size = MVT::getSizeInBits(VT)/32;
148
149 RegNum = ((RegNum + size - 1) / size) * size;
150 if (RegNum < 4) {
151 pos.push_back(RegNum);
152 is_reg.push_back(true);
153 RegNum += size;
154 } else {
155 unsigned bytes = size * 32/8;
156 StackOffset = ((StackOffset + bytes - 1) / bytes) * bytes;
157 pos.push_back(StackOffset);
158 is_reg.push_back(false);
159 StackOffset += bytes;
160 }
161 }
162 }
163 unsigned getRegisterNum(unsigned argNum) {
164 assert(isRegister(argNum));
165 return pos[argNum];
166 }
167 unsigned getOffset(unsigned argNum) {
168 assert(isOffset(argNum));
169 return pos[argNum];
170 }
171 unsigned isRegister(unsigned argNum) {
172 assert(argNum < is_reg.size());
173 return is_reg[argNum];
174 }
175 unsigned isOffset(unsigned argNum) {
176 return !isRegister(argNum);
177 }
178 MVT::ValueType getType(unsigned argNum) {
179 assert(argNum < types.size());
180 return types[argNum];
181 }
182 unsigned getStackSize(void) {
183 int last = is_reg.size() - 1;
184 if (isRegister(last))
185 return 0;
186 return getOffset(last) + MVT::getSizeInBits(getType(last))/8;
187 }
188 int lastRegArg(void) {
189 int size = is_reg.size();
190 int last = 0;
191 while(last < size && isRegister(last))
192 last++;
193 last--;
194 return last;
195 }
196 unsigned lastRegNum(void) {
197 int l = lastRegArg();
198 if (l < 0)
199 return -1;
200 unsigned r = getRegisterNum(l);
201 MVT::ValueType t = getType(l);
202 assert(t == MVT::i32 || t == MVT::f32 || t == MVT::f64);
203 if (t == MVT::f64)
204 return r + 1;
205 return r;
206 }
207};
208
Rafael Espindola84b19be2006-07-16 01:02:57 +0000209// This transforms a ISD::CALL node into a
210// callseq_star <- ARMISD:CALL <- callseq_end
211// chain
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000212static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola84b19be2006-07-16 01:02:57 +0000213 SDOperand Chain = Op.getOperand(0);
214 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
215 assert(CallConv == CallingConv::C && "unknown calling convention");
216 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000217 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
218 assert(isTailCall == false && "tail call not supported");
219 SDOperand Callee = Op.getOperand(4);
220 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Rafael Espindola1a009462006-08-08 13:02:29 +0000221 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000222 static const unsigned regs[] = {
Rafael Espindolafac00a92006-07-25 20:17:20 +0000223 ARM::R0, ARM::R1, ARM::R2, ARM::R3
224 };
225
Rafael Espindolaa2845842006-10-05 16:48:49 +0000226 std::vector<MVT::ValueType> Types;
227 for (unsigned i = 0; i < NumOps; ++i) {
228 MVT::ValueType VT = Op.getOperand(5+2*i).getValueType();
229 Types.push_back(VT);
230 }
231 ArgumentLayout Layout(Types);
Rafael Espindolafac00a92006-07-25 20:17:20 +0000232
Rafael Espindolaa2845842006-10-05 16:48:49 +0000233 unsigned NumBytes = Layout.getStackSize();
234
235 Chain = DAG.getCALLSEQ_START(Chain,
236 DAG.getConstant(NumBytes, MVT::i32));
237
238 //Build a sequence of stores
239 std::vector<SDOperand> MemOpChains;
240 for (unsigned i = Layout.lastRegArg() + 1; i < NumOps; ++i) {
241 SDOperand Arg = Op.getOperand(5+2*i);
242 unsigned ArgOffset = Layout.getOffset(i);
243 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
244 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
245 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
246 Arg, PtrOff, DAG.getSrcValue(NULL)));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000247 }
Rafael Espindola1a009462006-08-08 13:02:29 +0000248 if (!MemOpChains.empty())
Chris Lattnere2199452006-08-11 17:38:39 +0000249 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
250 &MemOpChains[0], MemOpChains.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000251
Rafael Espindola84b19be2006-07-16 01:02:57 +0000252 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
253 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
254 // node so that legalize doesn't hack it.
255 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
256 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
257
258 // If this is a direct call, pass the chain and the callee.
259 assert (Callee.Val);
260 std::vector<SDOperand> Ops;
261 Ops.push_back(Chain);
262 Ops.push_back(Callee);
263
Rafael Espindolaa2845842006-10-05 16:48:49 +0000264 // Build a sequence of copy-to-reg nodes chained together with token chain
265 // and flag operands which copy the outgoing args into the appropriate regs.
266 SDOperand InFlag;
267 for (unsigned i = 0, e = Layout.lastRegArg(); i <= e; ++i) {
268 SDOperand Arg = Op.getOperand(5+2*i);
269 unsigned Reg = regs[Layout.getRegisterNum(i)];
270 assert(Layout.getType(i) == Arg.getValueType());
271 assert(Layout.getType(i) == MVT::i32);
272 Chain = DAG.getCopyToReg(Chain, Reg, Arg, InFlag);
273 InFlag = Chain.getValue(1);
274
275 // Add argument register to the end of the list so that it is known live
276 // into the call.
277 Ops.push_back(DAG.getRegister(Reg, Arg.getValueType()));
278 }
279
280 std::vector<MVT::ValueType> NodeTys;
281 NodeTys.push_back(MVT::Other); // Returns a chain
282 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Rafael Espindola7a53bd02006-08-09 16:41:12 +0000283
Rafael Espindola84b19be2006-07-16 01:02:57 +0000284 unsigned CallOpc = ARMISD::CALL;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000285 if (InFlag.Val)
286 Ops.push_back(InFlag);
Chris Lattner87428672006-08-11 17:22:35 +0000287 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000288 InFlag = Chain.getValue(1);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000289
Rafael Espindolafac00a92006-07-25 20:17:20 +0000290 std::vector<SDOperand> ResultVals;
291 NodeTys.clear();
292
293 // If the call has results, copy the values out of the ret val registers.
294 switch (Op.Val->getValueType(0)) {
295 default: assert(0 && "Unexpected ret value!");
296 case MVT::Other:
297 break;
298 case MVT::i32:
299 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
300 ResultVals.push_back(Chain.getValue(0));
301 NodeTys.push_back(MVT::i32);
302 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000303
304 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
305 DAG.getConstant(NumBytes, MVT::i32));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000306 NodeTys.push_back(MVT::Other);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000307
Rafael Espindolafac00a92006-07-25 20:17:20 +0000308 if (ResultVals.empty())
309 return Chain;
310
311 ResultVals.push_back(Chain);
Chris Lattner87428672006-08-11 17:22:35 +0000312 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
313 ResultVals.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000314 return Res.getValue(Op.ResNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000315}
316
317static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
318 SDOperand Copy;
Rafael Espindola4b023672006-06-05 22:26:14 +0000319 SDOperand Chain = Op.getOperand(0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000320 SDOperand R0 = DAG.getRegister(ARM::R0, MVT::i32);
321 SDOperand R1 = DAG.getRegister(ARM::R1, MVT::i32);
322
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000323 switch(Op.getNumOperands()) {
324 default:
325 assert(0 && "Do not know how to return this many arguments!");
326 abort();
Rafael Espindola4b023672006-06-05 22:26:14 +0000327 case 1: {
328 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
Rafael Espindola6312da02006-08-03 22:50:11 +0000329 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
Rafael Espindola4b023672006-06-05 22:26:14 +0000330 }
Rafael Espindola27185192006-09-29 21:20:16 +0000331 case 3: {
332 SDOperand Val = Op.getOperand(1);
333 assert(Val.getValueType() == MVT::i32 ||
Rafael Espindola9e071f02006-10-02 19:30:56 +0000334 Val.getValueType() == MVT::f32 ||
335 Val.getValueType() == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000336
Rafael Espindola9e071f02006-10-02 19:30:56 +0000337 if (Val.getValueType() == MVT::f64) {
338 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
339 SDOperand Ops[] = {Chain, R0, R1, Val};
340 Copy = DAG.getNode(ARMISD::FMRRD, VTs, Ops, 4);
341 } else {
342 if (Val.getValueType() == MVT::f32)
343 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
344 Copy = DAG.getCopyToReg(Chain, R0, Val, SDOperand());
345 }
346
347 if (DAG.getMachineFunction().liveout_empty()) {
Rafael Espindola4b023672006-06-05 22:26:14 +0000348 DAG.getMachineFunction().addLiveOut(ARM::R0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000349 if (Val.getValueType() == MVT::f64)
350 DAG.getMachineFunction().addLiveOut(ARM::R1);
351 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000352 break;
Rafael Espindola27185192006-09-29 21:20:16 +0000353 }
Rafael Espindola3a02f022006-09-04 19:05:01 +0000354 case 5:
355 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
356 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
357 // If we haven't noted the R0+R1 are live out, do so now.
358 if (DAG.getMachineFunction().liveout_empty()) {
359 DAG.getMachineFunction().addLiveOut(ARM::R0);
360 DAG.getMachineFunction().addLiveOut(ARM::R1);
361 }
362 break;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000363 }
Rafael Espindola4b023672006-06-05 22:26:14 +0000364
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000365 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
366 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000367}
368
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000369static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
370 MVT::ValueType PtrVT = Op.getValueType();
371 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengc356a572006-09-12 21:04:05 +0000372 Constant *C = CP->getConstVal();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000373 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
374
375 return CPI;
376}
377
378static SDOperand LowerGlobalAddress(SDOperand Op,
379 SelectionDAG &DAG) {
380 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Rafael Espindola61369da2006-08-14 19:01:24 +0000381 int alignment = 2;
382 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000383 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr,
384 DAG.getSrcValue(NULL));
385}
386
Rafael Espindola755be9b2006-08-25 17:55:16 +0000387static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
388 unsigned VarArgsFrameIndex) {
389 // vastart just stores the address of the VarArgsFrameIndex slot into the
390 // memory location argument.
391 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
392 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
393 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
394 Op.getOperand(1), Op.getOperand(2));
395}
396
397static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
398 int &VarArgsFrameIndex) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000399 MachineFunction &MF = DAG.getMachineFunction();
400 MachineFrameInfo *MFI = MF.getFrameInfo();
401 SSARegMap *RegMap = MF.getSSARegMap();
402 unsigned NumArgs = Op.Val->getNumValues()-1;
403 SDOperand Root = Op.getOperand(0);
404 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
405 static const unsigned REGS[] = {
406 ARM::R0, ARM::R1, ARM::R2, ARM::R3
407 };
408
409 std::vector<MVT::ValueType> Types(Op.Val->value_begin(), Op.Val->value_end() - 1);
410 ArgumentLayout Layout(Types);
411
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000412 std::vector<SDOperand> ArgValues;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000413 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000414 MVT::ValueType VT = Types[ArgNo];
Rafael Espindola4b442b52006-05-23 02:48:20 +0000415
Rafael Espindolaa2845842006-10-05 16:48:49 +0000416 SDOperand Value;
417 if (Layout.isRegister(ArgNo)) {
418 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
419 unsigned RegNum = Layout.getRegisterNum(ArgNo);
420 unsigned Reg1 = REGS[RegNum];
421 unsigned VReg1 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
422 SDOperand Value1 = DAG.getCopyFromReg(Root, VReg1, MVT::i32);
423 MF.addLiveIn(Reg1, VReg1);
424 if (VT == MVT::f64) {
425 unsigned Reg2 = REGS[RegNum + 1];
426 unsigned VReg2 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
427 SDOperand Value2 = DAG.getCopyFromReg(Root, VReg2, MVT::i32);
428 MF.addLiveIn(Reg2, VReg2);
429 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
430 } else {
431 Value = Value1;
432 if (VT == MVT::f32)
433 Value = DAG.getNode(ISD::BIT_CONVERT, VT, Value);
434 }
435 } else {
436 // If the argument is actually used, emit a load from the right stack
437 // slot.
438 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
439 unsigned Offset = Layout.getOffset(ArgNo);
440 unsigned Size = MVT::getSizeInBits(VT)/8;
441 int FI = MFI->CreateFixedObject(Size, Offset);
442 SDOperand FIN = DAG.getFrameIndex(FI, VT);
443 Value = DAG.getLoad(VT, Root, FIN, DAG.getSrcValue(NULL));
444 } else {
445 Value = DAG.getNode(ISD::UNDEF, VT);
446 }
447 }
448 ArgValues.push_back(Value);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000449 }
450
Rafael Espindolaa2845842006-10-05 16:48:49 +0000451 unsigned NextRegNum = Layout.lastRegNum() + 1;
452
Rafael Espindola755be9b2006-08-25 17:55:16 +0000453 if (isVarArg) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000454 //If this function is vararg we must store the remaing
455 //registers so that they can be acessed with va_start
Rafael Espindola755be9b2006-08-25 17:55:16 +0000456 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000457 -16 + NextRegNum * 4);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000458
Rafael Espindola755be9b2006-08-25 17:55:16 +0000459 SmallVector<SDOperand, 4> MemOps;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000460 for (unsigned RegNo = NextRegNum; RegNo < 4; ++RegNo) {
461 int RegOffset = - (4 - RegNo) * 4;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000462 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000463 RegOffset);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000464 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
465
Rafael Espindolaa2845842006-10-05 16:48:49 +0000466 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
467 MF.addLiveIn(REGS[RegNo], VReg);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000468
469 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
470 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
471 Val, FIN, DAG.getSrcValue(NULL));
472 MemOps.push_back(Store);
473 }
474 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
475 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000476
477 ArgValues.push_back(Root);
478
479 // Return the new list of results.
480 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
481 Op.Val->value_end());
Chris Lattner87428672006-08-11 17:22:35 +0000482 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
Rafael Espindoladc124a22006-05-18 21:45:49 +0000483}
484
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000485static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
486 SDOperand LHS = Op.getOperand(0);
487 SDOperand RHS = Op.getOperand(1);
488 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
489 SDOperand TrueVal = Op.getOperand(2);
490 SDOperand FalseVal = Op.getOperand(3);
Rafael Espindolacdda88c2006-08-24 17:19:08 +0000491 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000492
493 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000494 return DAG.getNode(ARMISD::SELECT, MVT::i32, TrueVal, FalseVal, ARMCC, Cmp);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000495}
496
Rafael Espindola687bc492006-08-24 13:45:55 +0000497static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
498 SDOperand Chain = Op.getOperand(0);
499 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
500 SDOperand LHS = Op.getOperand(2);
501 SDOperand RHS = Op.getOperand(3);
502 SDOperand Dest = Op.getOperand(4);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000503 SDOperand ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
Rafael Espindola687bc492006-08-24 13:45:55 +0000504
505 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000506 return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, ARMCC, Cmp);
Rafael Espindola687bc492006-08-24 13:45:55 +0000507}
508
Rafael Espindola27185192006-09-29 21:20:16 +0000509static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola9e071f02006-10-02 19:30:56 +0000510 SDOperand IntVal = Op.getOperand(0);
Rafael Espindola27185192006-09-29 21:20:16 +0000511 assert(IntVal.getValueType() == MVT::i32);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000512 MVT::ValueType vt = Op.getValueType();
513 assert(vt == MVT::f32 ||
514 vt == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000515
516 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000517 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FSITOS : ARMISD::FSITOD;
518 return DAG.getNode(op, vt, Tmp);
Rafael Espindola27185192006-09-29 21:20:16 +0000519}
520
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000521SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
522 switch (Op.getOpcode()) {
523 default:
524 assert(0 && "Should not custom lower this!");
Rafael Espindola1c8f0532006-05-15 22:34:39 +0000525 abort();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000526 case ISD::ConstantPool:
527 return LowerConstantPool(Op, DAG);
528 case ISD::GlobalAddress:
529 return LowerGlobalAddress(Op, DAG);
Rafael Espindola27185192006-09-29 21:20:16 +0000530 case ISD::SINT_TO_FP:
531 return LowerSINT_TO_FP(Op, DAG);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000532 case ISD::FORMAL_ARGUMENTS:
Rafael Espindola755be9b2006-08-25 17:55:16 +0000533 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000534 case ISD::CALL:
535 return LowerCALL(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000536 case ISD::RET:
537 return LowerRET(Op, DAG);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000538 case ISD::SELECT_CC:
539 return LowerSELECT_CC(Op, DAG);
Rafael Espindola687bc492006-08-24 13:45:55 +0000540 case ISD::BR_CC:
541 return LowerBR_CC(Op, DAG);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000542 case ISD::VASTART:
543 return LowerVASTART(Op, DAG, VarArgsFrameIndex);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000544 }
545}
546
547//===----------------------------------------------------------------------===//
548// Instruction Selector Implementation
549//===----------------------------------------------------------------------===//
550
551//===--------------------------------------------------------------------===//
552/// ARMDAGToDAGISel - ARM specific code to select ARM machine
553/// instructions for SelectionDAG operations.
554///
555namespace {
556class ARMDAGToDAGISel : public SelectionDAGISel {
557 ARMTargetLowering Lowering;
558
559public:
560 ARMDAGToDAGISel(TargetMachine &TM)
561 : SelectionDAGISel(Lowering), Lowering(TM) {
562 }
563
Evan Cheng9ade2182006-08-26 05:34:46 +0000564 SDNode *Select(SDOperand Op);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000565 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000566 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000567 bool SelectAddrMode1(SDOperand N, SDOperand &Arg, SDOperand &Shift,
568 SDOperand &ShiftType);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000569
570 // Include the pieces autogenerated from the target description.
571#include "ARMGenDAGISel.inc"
572};
573
574void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
575 DEBUG(BB->dump());
576
577 DAG.setRoot(SelectRoot(DAG.getRoot()));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000578 DAG.RemoveDeadNodes();
579
580 ScheduleAndEmitDAG(DAG);
581}
582
Rafael Espindola61369da2006-08-14 19:01:24 +0000583static bool isInt12Immediate(SDNode *N, short &Imm) {
584 if (N->getOpcode() != ISD::Constant)
585 return false;
586
587 int32_t t = cast<ConstantSDNode>(N)->getValue();
Rafael Espindola7246d332006-09-21 11:29:52 +0000588 int max = 1<<12;
Rafael Espindola61369da2006-08-14 19:01:24 +0000589 int min = -max;
590 if (t > min && t < max) {
591 Imm = t;
592 return true;
593 }
594 else
595 return false;
596}
597
598static bool isInt12Immediate(SDOperand Op, short &Imm) {
599 return isInt12Immediate(Op.Val, Imm);
600}
601
Rafael Espindola7246d332006-09-21 11:29:52 +0000602static uint32_t rotateL(uint32_t x) {
603 uint32_t bit31 = (x & (1 << 31)) >> 31;
604 uint32_t t = x << 1;
605 return t | bit31;
606}
607
608static bool isUInt8Immediate(uint32_t x) {
609 return x < (1 << 8);
610}
611
612static bool isRotInt8Immediate(uint32_t x) {
613 int r;
614 for (r = 0; r < 16; r++) {
615 if (isUInt8Immediate(x))
616 return true;
617 x = rotateL(rotateL(x));
618 }
619 return false;
620}
621
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000622bool ARMDAGToDAGISel::SelectAddrMode1(SDOperand N,
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000623 SDOperand &Arg,
624 SDOperand &Shift,
625 SDOperand &ShiftType) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000626 switch(N.getOpcode()) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000627 case ISD::Constant: {
Rafael Espindola7246d332006-09-21 11:29:52 +0000628 uint32_t val = cast<ConstantSDNode>(N)->getValue();
629 if(!isRotInt8Immediate(val)) {
630 const Type *t = MVT::getTypeForValueType(MVT::i32);
631 Constant *C = ConstantUInt::get(t, val);
632 int alignment = 2;
633 SDOperand Addr = CurDAG->getTargetConstantPool(C, MVT::i32, alignment);
634 SDOperand Z = CurDAG->getTargetConstant(0, MVT::i32);
635 SDNode *n = CurDAG->getTargetNode(ARM::ldr, MVT::i32, Z, Addr);
636 Arg = SDOperand(n, 0);
637 } else
638 Arg = CurDAG->getTargetConstant(val, MVT::i32);
639
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000640 Shift = CurDAG->getTargetConstant(0, MVT::i32);
641 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000642 return true;
643 }
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000644 case ISD::SRA:
645 Arg = N.getOperand(0);
646 Shift = N.getOperand(1);
647 ShiftType = CurDAG->getTargetConstant(ARMShift::ASR, MVT::i32);
648 return true;
649 case ISD::SRL:
650 Arg = N.getOperand(0);
651 Shift = N.getOperand(1);
652 ShiftType = CurDAG->getTargetConstant(ARMShift::LSR, MVT::i32);
653 return true;
654 case ISD::SHL:
655 Arg = N.getOperand(0);
656 Shift = N.getOperand(1);
657 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
658 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000659 }
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000660
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000661 Arg = N;
662 Shift = CurDAG->getTargetConstant(0, MVT::i32);
663 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000664 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000665}
666
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000667//register plus/minus 12 bit offset
668bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
669 SDOperand &Base) {
Rafael Espindolaf3a335c2006-08-17 17:09:40 +0000670 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
671 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
672 Offset = CurDAG->getTargetConstant(0, MVT::i32);
673 return true;
674 }
Rafael Espindola61369da2006-08-14 19:01:24 +0000675 if (N.getOpcode() == ISD::ADD) {
676 short imm = 0;
677 if (isInt12Immediate(N.getOperand(1), imm)) {
678 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
679 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
680 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
681 } else {
682 Base = N.getOperand(0);
683 }
684 return true; // [r+i]
685 }
686 }
687
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000688 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000689 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
690 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
691 }
692 else
693 Base = N;
694 return true; //any address fits in a register
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000695}
696
Evan Cheng9ade2182006-08-26 05:34:46 +0000697SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000698 SDNode *N = Op.Val;
699
700 switch (N->getOpcode()) {
701 default:
Evan Cheng9ade2182006-08-26 05:34:46 +0000702 return SelectCode(Op);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000703 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000704 }
Evan Cheng64a752f2006-08-11 09:08:15 +0000705 return NULL;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000706}
707
708} // end anonymous namespace
709
710/// createARMISelDag - This pass converts a legalized DAG into a
711/// ARM-specific DAG, ready for instruction scheduling.
712///
713FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
714 return new ARMDAGToDAGISel(TM);
715}