blob: 8076757580761a560c67a7979c84d2addc94fd16 [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 Espindola42b62f32006-10-13 13:14:59 +0000121/// DAGFPCCToARMCC - Convert a DAG fp condition code to an ARM CC
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000122// Unordered = !N & !Z & C & V = V
123// Ordered = N | Z | !C | !V = N | Z | !V
Rafael Espindola42b62f32006-10-13 13:14:59 +0000124static ARMCC::CondCodes DAGFPCCToARMCC(ISD::CondCode CC) {
Rafael Espindola6f602de2006-08-24 16:13:15 +0000125 switch (CC) {
Rafael Espindolaebdabda2006-09-21 13:06:26 +0000126 default:
Rafael Espindola42b62f32006-10-13 13:14:59 +0000127 assert(0 && "Unknown fp condition code!");
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000128// SETOEQ = (N | Z | !V) & Z = Z = EQ
129 case ISD::SETEQ:
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000130 case ISD::SETOEQ: return ARMCC::EQ;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000131// SETOGT = (N | Z | !V) & !N & !Z = !V &!N &!Z = (N = V) & !Z = GT
132 case ISD::SETGT:
Rafael Espindola42b62f32006-10-13 13:14:59 +0000133 case ISD::SETOGT: return ARMCC::GT;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000134// SETOGE = (N | Z | !V) & !N = (Z | !V) & !N = !V & !N = GE
135 case ISD::SETGE:
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000136 case ISD::SETOGE: return ARMCC::GE;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000137// SETOLT = (N | Z | !V) & N = N = MI
138 case ISD::SETLT:
139 case ISD::SETOLT: return ARMCC::MI;
140// SETOLE = (N | Z | !V) & (N | Z) = N | Z = !C | Z = LS
141 case ISD::SETLE:
142 case ISD::SETOLE: return ARMCC::LS;
143// SETONE = (N | Z | !V) & !Z = (N | !V) & Z = !V & Z = Z = NE
144 case ISD::SETNE:
Rafael Espindola42b62f32006-10-13 13:14:59 +0000145 case ISD::SETONE: return ARMCC::NE;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000146// SETO = N | Z | !V = Z | !V = !V = VC
147 case ISD::SETO: return ARMCC::VC;
148// SETUO = V = VS
149 case ISD::SETUO: return ARMCC::VS;
150// SETUEQ = V | Z = ??
151// SETUGT = V | (!Z & !N) = !Z & !N = !Z & C = HI
152 case ISD::SETUGT: return ARMCC::HI;
153// SETUGE = V | !N = !N = PL
Rafael Espindola42b62f32006-10-13 13:14:59 +0000154 case ISD::SETUGE: return ARMCC::PL;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000155// SETULT = V | N = ??
156// SETULE = V | Z | N = ??
157// SETUNE = V | !Z = !Z = NE
Rafael Espindola42b62f32006-10-13 13:14:59 +0000158 case ISD::SETUNE: return ARMCC::NE;
159 }
160}
161
162/// DAGIntCCToARMCC - Convert a DAG integer condition code to an ARM CC
163static ARMCC::CondCodes DAGIntCCToARMCC(ISD::CondCode CC) {
164 switch (CC) {
165 default:
166 assert(0 && "Unknown integer condition code!");
167 case ISD::SETEQ: return ARMCC::EQ;
168 case ISD::SETNE: return ARMCC::NE;
169 case ISD::SETLT: return ARMCC::LT;
170 case ISD::SETLE: return ARMCC::LE;
171 case ISD::SETGT: return ARMCC::GT;
172 case ISD::SETGE: return ARMCC::GE;
Rafael Espindolabc4cec92006-09-03 13:19:16 +0000173 case ISD::SETULT: return ARMCC::CC;
Rafael Espindola42b62f32006-10-13 13:14:59 +0000174 case ISD::SETULE: return ARMCC::LS;
175 case ISD::SETUGT: return ARMCC::HI;
176 case ISD::SETUGE: return ARMCC::CS;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000177 }
178}
179
Rafael Espindola84b19be2006-07-16 01:02:57 +0000180const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
181 switch (Opcode) {
182 default: return 0;
183 case ARMISD::CALL: return "ARMISD::CALL";
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000184 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000185 case ARMISD::SELECT: return "ARMISD::SELECT";
186 case ARMISD::CMP: return "ARMISD::CMP";
Rafael Espindola687bc492006-08-24 13:45:55 +0000187 case ARMISD::BR: return "ARMISD::BR";
Rafael Espindola27185192006-09-29 21:20:16 +0000188 case ARMISD::FSITOS: return "ARMISD::FSITOS";
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000189 case ARMISD::FTOSIS: return "ARMISD::FTOSIS";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000190 case ARMISD::FSITOD: return "ARMISD::FSITOD";
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000191 case ARMISD::FTOSID: return "ARMISD::FTOSID";
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000192 case ARMISD::FUITOS: return "ARMISD::FUITOS";
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000193 case ARMISD::FTOUIS: return "ARMISD::FTOUIS";
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000194 case ARMISD::FUITOD: return "ARMISD::FUITOD";
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000195 case ARMISD::FTOUID: return "ARMISD::FTOUID";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000196 case ARMISD::FMRRD: return "ARMISD::FMRRD";
Rafael Espindolaa2845842006-10-05 16:48:49 +0000197 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000198 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Rafael Espindola84b19be2006-07-16 01:02:57 +0000199 }
200}
201
Rafael Espindolaa2845842006-10-05 16:48:49 +0000202class ArgumentLayout {
203 std::vector<bool> is_reg;
204 std::vector<unsigned> pos;
205 std::vector<MVT::ValueType> types;
206public:
Rafael Espindola39b5a212006-10-05 17:46:48 +0000207 ArgumentLayout(const std::vector<MVT::ValueType> &Types) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000208 types = Types;
209
210 unsigned RegNum = 0;
211 unsigned StackOffset = 0;
Rafael Espindola39b5a212006-10-05 17:46:48 +0000212 for(std::vector<MVT::ValueType>::const_iterator I = Types.begin();
Rafael Espindolaa2845842006-10-05 16:48:49 +0000213 I != Types.end();
214 ++I) {
215 MVT::ValueType VT = *I;
216 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
217 unsigned size = MVT::getSizeInBits(VT)/32;
218
219 RegNum = ((RegNum + size - 1) / size) * size;
220 if (RegNum < 4) {
221 pos.push_back(RegNum);
222 is_reg.push_back(true);
223 RegNum += size;
224 } else {
225 unsigned bytes = size * 32/8;
226 StackOffset = ((StackOffset + bytes - 1) / bytes) * bytes;
227 pos.push_back(StackOffset);
228 is_reg.push_back(false);
229 StackOffset += bytes;
230 }
231 }
232 }
233 unsigned getRegisterNum(unsigned argNum) {
234 assert(isRegister(argNum));
235 return pos[argNum];
236 }
237 unsigned getOffset(unsigned argNum) {
238 assert(isOffset(argNum));
239 return pos[argNum];
240 }
241 unsigned isRegister(unsigned argNum) {
242 assert(argNum < is_reg.size());
243 return is_reg[argNum];
244 }
245 unsigned isOffset(unsigned argNum) {
246 return !isRegister(argNum);
247 }
248 MVT::ValueType getType(unsigned argNum) {
249 assert(argNum < types.size());
250 return types[argNum];
251 }
252 unsigned getStackSize(void) {
253 int last = is_reg.size() - 1;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000254 if (last < 0)
255 return 0;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000256 if (isRegister(last))
257 return 0;
258 return getOffset(last) + MVT::getSizeInBits(getType(last))/8;
259 }
260 int lastRegArg(void) {
261 int size = is_reg.size();
262 int last = 0;
263 while(last < size && isRegister(last))
264 last++;
265 last--;
266 return last;
267 }
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000268 int lastRegNum(void) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000269 int l = lastRegArg();
270 if (l < 0)
271 return -1;
272 unsigned r = getRegisterNum(l);
273 MVT::ValueType t = getType(l);
274 assert(t == MVT::i32 || t == MVT::f32 || t == MVT::f64);
275 if (t == MVT::f64)
276 return r + 1;
277 return r;
278 }
279};
280
Rafael Espindola84b19be2006-07-16 01:02:57 +0000281// This transforms a ISD::CALL node into a
282// callseq_star <- ARMISD:CALL <- callseq_end
283// chain
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000284static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola84b19be2006-07-16 01:02:57 +0000285 SDOperand Chain = Op.getOperand(0);
286 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
287 assert(CallConv == CallingConv::C && "unknown calling convention");
288 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000289 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000290 SDOperand Callee = Op.getOperand(4);
291 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Rafael Espindola1a009462006-08-08 13:02:29 +0000292 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000293 static const unsigned regs[] = {
Rafael Espindolafac00a92006-07-25 20:17:20 +0000294 ARM::R0, ARM::R1, ARM::R2, ARM::R3
295 };
296
Rafael Espindolaa2845842006-10-05 16:48:49 +0000297 std::vector<MVT::ValueType> Types;
298 for (unsigned i = 0; i < NumOps; ++i) {
299 MVT::ValueType VT = Op.getOperand(5+2*i).getValueType();
300 Types.push_back(VT);
301 }
302 ArgumentLayout Layout(Types);
Rafael Espindolafac00a92006-07-25 20:17:20 +0000303
Rafael Espindolaa2845842006-10-05 16:48:49 +0000304 unsigned NumBytes = Layout.getStackSize();
305
306 Chain = DAG.getCALLSEQ_START(Chain,
307 DAG.getConstant(NumBytes, MVT::i32));
308
309 //Build a sequence of stores
310 std::vector<SDOperand> MemOpChains;
311 for (unsigned i = Layout.lastRegArg() + 1; i < NumOps; ++i) {
312 SDOperand Arg = Op.getOperand(5+2*i);
313 unsigned ArgOffset = Layout.getOffset(i);
314 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
315 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Evan Cheng8b2794a2006-10-13 21:14:26 +0000316 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000317 }
Rafael Espindola1a009462006-08-08 13:02:29 +0000318 if (!MemOpChains.empty())
Chris Lattnere2199452006-08-11 17:38:39 +0000319 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
320 &MemOpChains[0], MemOpChains.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000321
Rafael Espindola84b19be2006-07-16 01:02:57 +0000322 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
323 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
324 // node so that legalize doesn't hack it.
325 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
326 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
327
328 // If this is a direct call, pass the chain and the callee.
329 assert (Callee.Val);
330 std::vector<SDOperand> Ops;
331 Ops.push_back(Chain);
332 Ops.push_back(Callee);
333
Rafael Espindolaa2845842006-10-05 16:48:49 +0000334 // Build a sequence of copy-to-reg nodes chained together with token chain
335 // and flag operands which copy the outgoing args into the appropriate regs.
336 SDOperand InFlag;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000337 for (int i = 0, e = Layout.lastRegArg(); i <= e; ++i) {
Rafael Espindola4a408d42006-10-06 12:50:22 +0000338 SDOperand Arg = Op.getOperand(5+2*i);
339 unsigned RegNum = Layout.getRegisterNum(i);
340 unsigned Reg1 = regs[RegNum];
341 MVT::ValueType VT = Layout.getType(i);
342 assert(VT == Arg.getValueType());
343 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000344
345 // Add argument register to the end of the list so that it is known live
346 // into the call.
Rafael Espindola4a408d42006-10-06 12:50:22 +0000347 Ops.push_back(DAG.getRegister(Reg1, MVT::i32));
348 if (VT == MVT::f64) {
349 unsigned Reg2 = regs[RegNum + 1];
350 SDOperand SDReg1 = DAG.getRegister(Reg1, MVT::i32);
351 SDOperand SDReg2 = DAG.getRegister(Reg2, MVT::i32);
352
353 Ops.push_back(DAG.getRegister(Reg2, MVT::i32));
354 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola935b1f82006-10-06 20:33:26 +0000355 SDOperand Ops[] = {Chain, SDReg1, SDReg2, Arg, InFlag};
356 Chain = DAG.getNode(ARMISD::FMRRD, VTs, Ops, InFlag.Val ? 5 : 4);
Rafael Espindola4a408d42006-10-06 12:50:22 +0000357 } else {
358 if (VT == MVT::f32)
359 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg);
360 Chain = DAG.getCopyToReg(Chain, Reg1, Arg, InFlag);
361 }
362 InFlag = Chain.getValue(1);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000363 }
364
365 std::vector<MVT::ValueType> NodeTys;
366 NodeTys.push_back(MVT::Other); // Returns a chain
367 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Rafael Espindola7a53bd02006-08-09 16:41:12 +0000368
Rafael Espindola84b19be2006-07-16 01:02:57 +0000369 unsigned CallOpc = ARMISD::CALL;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000370 if (InFlag.Val)
371 Ops.push_back(InFlag);
Chris Lattner87428672006-08-11 17:22:35 +0000372 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000373 InFlag = Chain.getValue(1);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000374
Rafael Espindolafac00a92006-07-25 20:17:20 +0000375 std::vector<SDOperand> ResultVals;
376 NodeTys.clear();
377
378 // If the call has results, copy the values out of the ret val registers.
Rafael Espindola614057b2006-10-06 19:10:05 +0000379 MVT::ValueType VT = Op.Val->getValueType(0);
380 if (VT != MVT::Other) {
381 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
Rafael Espindola614057b2006-10-06 19:10:05 +0000382
383 SDOperand Value1 = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
384 Chain = Value1.getValue(1);
385 InFlag = Value1.getValue(2);
Rafael Espindola26a76d12006-10-13 16:47:22 +0000386 NodeTys.push_back(VT);
387 if (VT == MVT::i32) {
388 ResultVals.push_back(Value1);
389 if (Op.Val->getValueType(1) == MVT::i32) {
390 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
391 Chain = Value2.getValue(1);
392 ResultVals.push_back(Value2);
393 NodeTys.push_back(VT);
394 }
395 }
396 if (VT == MVT::f32) {
397 SDOperand Value = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Value1);
398 ResultVals.push_back(Value);
399 }
Rafael Espindola614057b2006-10-06 19:10:05 +0000400 if (VT == MVT::f64) {
401 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
402 Chain = Value2.getValue(1);
Rafael Espindola26a76d12006-10-13 16:47:22 +0000403 SDOperand Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
404 ResultVals.push_back(Value);
Rafael Espindola614057b2006-10-06 19:10:05 +0000405 }
Rafael Espindolafac00a92006-07-25 20:17:20 +0000406 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000407
408 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
409 DAG.getConstant(NumBytes, MVT::i32));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000410 NodeTys.push_back(MVT::Other);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000411
Rafael Espindolafac00a92006-07-25 20:17:20 +0000412 if (ResultVals.empty())
413 return Chain;
414
415 ResultVals.push_back(Chain);
Chris Lattner87428672006-08-11 17:22:35 +0000416 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
417 ResultVals.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000418 return Res.getValue(Op.ResNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000419}
420
421static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
422 SDOperand Copy;
Rafael Espindola4b023672006-06-05 22:26:14 +0000423 SDOperand Chain = Op.getOperand(0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000424 SDOperand R0 = DAG.getRegister(ARM::R0, MVT::i32);
425 SDOperand R1 = DAG.getRegister(ARM::R1, MVT::i32);
426
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000427 switch(Op.getNumOperands()) {
428 default:
429 assert(0 && "Do not know how to return this many arguments!");
430 abort();
Rafael Espindola4b023672006-06-05 22:26:14 +0000431 case 1: {
432 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
Rafael Espindola6312da02006-08-03 22:50:11 +0000433 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
Rafael Espindola4b023672006-06-05 22:26:14 +0000434 }
Rafael Espindola27185192006-09-29 21:20:16 +0000435 case 3: {
436 SDOperand Val = Op.getOperand(1);
437 assert(Val.getValueType() == MVT::i32 ||
Rafael Espindola9e071f02006-10-02 19:30:56 +0000438 Val.getValueType() == MVT::f32 ||
439 Val.getValueType() == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000440
Rafael Espindola9e071f02006-10-02 19:30:56 +0000441 if (Val.getValueType() == MVT::f64) {
442 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
443 SDOperand Ops[] = {Chain, R0, R1, Val};
444 Copy = DAG.getNode(ARMISD::FMRRD, VTs, Ops, 4);
445 } else {
446 if (Val.getValueType() == MVT::f32)
447 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
448 Copy = DAG.getCopyToReg(Chain, R0, Val, SDOperand());
449 }
450
451 if (DAG.getMachineFunction().liveout_empty()) {
Rafael Espindola4b023672006-06-05 22:26:14 +0000452 DAG.getMachineFunction().addLiveOut(ARM::R0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000453 if (Val.getValueType() == MVT::f64)
454 DAG.getMachineFunction().addLiveOut(ARM::R1);
455 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000456 break;
Rafael Espindola27185192006-09-29 21:20:16 +0000457 }
Rafael Espindola3a02f022006-09-04 19:05:01 +0000458 case 5:
459 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
460 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
461 // If we haven't noted the R0+R1 are live out, do so now.
462 if (DAG.getMachineFunction().liveout_empty()) {
463 DAG.getMachineFunction().addLiveOut(ARM::R0);
464 DAG.getMachineFunction().addLiveOut(ARM::R1);
465 }
466 break;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000467 }
Rafael Espindola4b023672006-06-05 22:26:14 +0000468
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000469 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
470 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000471}
472
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000473static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
474 MVT::ValueType PtrVT = Op.getValueType();
475 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengc356a572006-09-12 21:04:05 +0000476 Constant *C = CP->getConstVal();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000477 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
478
479 return CPI;
480}
481
482static SDOperand LowerGlobalAddress(SDOperand Op,
483 SelectionDAG &DAG) {
484 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Rafael Espindola61369da2006-08-14 19:01:24 +0000485 int alignment = 2;
486 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
Evan Cheng466685d2006-10-09 20:57:25 +0000487 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr, NULL, 0);
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000488}
489
Rafael Espindola755be9b2006-08-25 17:55:16 +0000490static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
491 unsigned VarArgsFrameIndex) {
492 // vastart just stores the address of the VarArgsFrameIndex slot into the
493 // memory location argument.
494 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
495 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +0000496 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
497 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), SV->getValue(),
498 SV->getOffset());
Rafael Espindola755be9b2006-08-25 17:55:16 +0000499}
500
501static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
502 int &VarArgsFrameIndex) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000503 MachineFunction &MF = DAG.getMachineFunction();
504 MachineFrameInfo *MFI = MF.getFrameInfo();
505 SSARegMap *RegMap = MF.getSSARegMap();
506 unsigned NumArgs = Op.Val->getNumValues()-1;
507 SDOperand Root = Op.getOperand(0);
508 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
509 static const unsigned REGS[] = {
510 ARM::R0, ARM::R1, ARM::R2, ARM::R3
511 };
512
513 std::vector<MVT::ValueType> Types(Op.Val->value_begin(), Op.Val->value_end() - 1);
514 ArgumentLayout Layout(Types);
515
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000516 std::vector<SDOperand> ArgValues;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000517 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000518 MVT::ValueType VT = Types[ArgNo];
Rafael Espindola4b442b52006-05-23 02:48:20 +0000519
Rafael Espindolaa2845842006-10-05 16:48:49 +0000520 SDOperand Value;
521 if (Layout.isRegister(ArgNo)) {
522 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
523 unsigned RegNum = Layout.getRegisterNum(ArgNo);
524 unsigned Reg1 = REGS[RegNum];
525 unsigned VReg1 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
526 SDOperand Value1 = DAG.getCopyFromReg(Root, VReg1, MVT::i32);
527 MF.addLiveIn(Reg1, VReg1);
528 if (VT == MVT::f64) {
529 unsigned Reg2 = REGS[RegNum + 1];
530 unsigned VReg2 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
531 SDOperand Value2 = DAG.getCopyFromReg(Root, VReg2, MVT::i32);
532 MF.addLiveIn(Reg2, VReg2);
533 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
534 } else {
535 Value = Value1;
536 if (VT == MVT::f32)
537 Value = DAG.getNode(ISD::BIT_CONVERT, VT, Value);
538 }
539 } else {
540 // If the argument is actually used, emit a load from the right stack
541 // slot.
542 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
543 unsigned Offset = Layout.getOffset(ArgNo);
544 unsigned Size = MVT::getSizeInBits(VT)/8;
545 int FI = MFI->CreateFixedObject(Size, Offset);
546 SDOperand FIN = DAG.getFrameIndex(FI, VT);
Evan Cheng466685d2006-10-09 20:57:25 +0000547 Value = DAG.getLoad(VT, Root, FIN, NULL, 0);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000548 } else {
549 Value = DAG.getNode(ISD::UNDEF, VT);
550 }
551 }
552 ArgValues.push_back(Value);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000553 }
554
Rafael Espindolaa2845842006-10-05 16:48:49 +0000555 unsigned NextRegNum = Layout.lastRegNum() + 1;
556
Rafael Espindola755be9b2006-08-25 17:55:16 +0000557 if (isVarArg) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000558 //If this function is vararg we must store the remaing
559 //registers so that they can be acessed with va_start
Rafael Espindola755be9b2006-08-25 17:55:16 +0000560 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000561 -16 + NextRegNum * 4);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000562
Rafael Espindola755be9b2006-08-25 17:55:16 +0000563 SmallVector<SDOperand, 4> MemOps;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000564 for (unsigned RegNo = NextRegNum; RegNo < 4; ++RegNo) {
565 int RegOffset = - (4 - RegNo) * 4;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000566 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000567 RegOffset);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000568 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
569
Rafael Espindolaa2845842006-10-05 16:48:49 +0000570 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
571 MF.addLiveIn(REGS[RegNo], VReg);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000572
573 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
Evan Cheng8b2794a2006-10-13 21:14:26 +0000574 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000575 MemOps.push_back(Store);
576 }
577 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
578 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000579
580 ArgValues.push_back(Root);
581
582 // Return the new list of results.
583 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
584 Op.Val->value_end());
Chris Lattner87428672006-08-11 17:22:35 +0000585 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
Rafael Espindoladc124a22006-05-18 21:45:49 +0000586}
587
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000588static SDOperand GetCMP(ISD::CondCode CC, SDOperand LHS, SDOperand RHS,
589 SelectionDAG &DAG) {
590 MVT::ValueType vt = LHS.getValueType();
Rafael Espindola0d9fe762006-10-10 16:33:47 +0000591 assert(vt == MVT::i32 || vt == MVT::f32 || vt == MVT::f64);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000592
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000593 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
Rafael Espindola42b62f32006-10-13 13:14:59 +0000594
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000595 if (vt != MVT::i32)
596 Cmp = DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp);
597 return Cmp;
598}
599
Rafael Espindola42b62f32006-10-13 13:14:59 +0000600static SDOperand GetARMCC(ISD::CondCode CC, MVT::ValueType vt,
601 SelectionDAG &DAG) {
602 assert(vt == MVT::i32 || vt == MVT::f32 || vt == MVT::f64);
603 if (vt == MVT::i32)
604 return DAG.getConstant(DAGIntCCToARMCC(CC), MVT::i32);
605 else
606 return DAG.getConstant(DAGFPCCToARMCC(CC), MVT::i32);
607}
608
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000609static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
610 SDOperand LHS = Op.getOperand(0);
611 SDOperand RHS = Op.getOperand(1);
612 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
613 SDOperand TrueVal = Op.getOperand(2);
614 SDOperand FalseVal = Op.getOperand(3);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000615 SDOperand Cmp = GetCMP(CC, LHS, RHS, DAG);
Rafael Espindola42b62f32006-10-13 13:14:59 +0000616 SDOperand ARMCC = GetARMCC(CC, LHS.getValueType(), DAG);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000617 return DAG.getNode(ARMISD::SELECT, MVT::i32, TrueVal, FalseVal, ARMCC, Cmp);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000618}
619
Rafael Espindola687bc492006-08-24 13:45:55 +0000620static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
621 SDOperand Chain = Op.getOperand(0);
622 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
623 SDOperand LHS = Op.getOperand(2);
624 SDOperand RHS = Op.getOperand(3);
625 SDOperand Dest = Op.getOperand(4);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000626 SDOperand Cmp = GetCMP(CC, LHS, RHS, DAG);
Rafael Espindola42b62f32006-10-13 13:14:59 +0000627 SDOperand ARMCC = GetARMCC(CC, LHS.getValueType(), DAG);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000628 return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, ARMCC, Cmp);
Rafael Espindola687bc492006-08-24 13:45:55 +0000629}
630
Rafael Espindola27185192006-09-29 21:20:16 +0000631static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola9e071f02006-10-02 19:30:56 +0000632 SDOperand IntVal = Op.getOperand(0);
Rafael Espindola27185192006-09-29 21:20:16 +0000633 assert(IntVal.getValueType() == MVT::i32);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000634 MVT::ValueType vt = Op.getValueType();
635 assert(vt == MVT::f32 ||
636 vt == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000637
638 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000639 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FSITOS : ARMISD::FSITOD;
640 return DAG.getNode(op, vt, Tmp);
Rafael Espindola27185192006-09-29 21:20:16 +0000641}
642
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000643static SDOperand LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
644 assert(Op.getValueType() == MVT::i32);
645 SDOperand FloatVal = Op.getOperand(0);
646 MVT::ValueType vt = FloatVal.getValueType();
647 assert(vt == MVT::f32 || vt == MVT::f64);
648
649 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FTOSIS : ARMISD::FTOSID;
650 SDOperand Tmp = DAG.getNode(op, MVT::f32, FloatVal);
651 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Tmp);
652}
653
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000654static SDOperand LowerUINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
655 SDOperand IntVal = Op.getOperand(0);
656 assert(IntVal.getValueType() == MVT::i32);
657 MVT::ValueType vt = Op.getValueType();
658 assert(vt == MVT::f32 ||
659 vt == MVT::f64);
660
661 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
662 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FUITOS : ARMISD::FUITOD;
663 return DAG.getNode(op, vt, Tmp);
664}
665
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000666static SDOperand LowerFP_TO_UINT(SDOperand Op, SelectionDAG &DAG) {
667 assert(Op.getValueType() == MVT::i32);
668 SDOperand FloatVal = Op.getOperand(0);
669 MVT::ValueType vt = FloatVal.getValueType();
670 assert(vt == MVT::f32 || vt == MVT::f64);
671
672 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FTOUIS : ARMISD::FTOUID;
673 SDOperand Tmp = DAG.getNode(op, MVT::f32, FloatVal);
674 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Tmp);
675}
676
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000677SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
678 switch (Op.getOpcode()) {
679 default:
680 assert(0 && "Should not custom lower this!");
Rafael Espindola1c8f0532006-05-15 22:34:39 +0000681 abort();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000682 case ISD::ConstantPool:
683 return LowerConstantPool(Op, DAG);
684 case ISD::GlobalAddress:
685 return LowerGlobalAddress(Op, DAG);
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000686 case ISD::FP_TO_SINT:
687 return LowerFP_TO_SINT(Op, DAG);
Rafael Espindola27185192006-09-29 21:20:16 +0000688 case ISD::SINT_TO_FP:
689 return LowerSINT_TO_FP(Op, DAG);
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000690 case ISD::FP_TO_UINT:
691 return LowerFP_TO_UINT(Op, DAG);
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000692 case ISD::UINT_TO_FP:
693 return LowerUINT_TO_FP(Op, DAG);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000694 case ISD::FORMAL_ARGUMENTS:
Rafael Espindola755be9b2006-08-25 17:55:16 +0000695 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000696 case ISD::CALL:
697 return LowerCALL(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000698 case ISD::RET:
699 return LowerRET(Op, DAG);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000700 case ISD::SELECT_CC:
701 return LowerSELECT_CC(Op, DAG);
Rafael Espindola687bc492006-08-24 13:45:55 +0000702 case ISD::BR_CC:
703 return LowerBR_CC(Op, DAG);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000704 case ISD::VASTART:
705 return LowerVASTART(Op, DAG, VarArgsFrameIndex);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000706 }
707}
708
709//===----------------------------------------------------------------------===//
710// Instruction Selector Implementation
711//===----------------------------------------------------------------------===//
712
713//===--------------------------------------------------------------------===//
714/// ARMDAGToDAGISel - ARM specific code to select ARM machine
715/// instructions for SelectionDAG operations.
716///
717namespace {
718class ARMDAGToDAGISel : public SelectionDAGISel {
719 ARMTargetLowering Lowering;
720
721public:
722 ARMDAGToDAGISel(TargetMachine &TM)
723 : SelectionDAGISel(Lowering), Lowering(TM) {
724 }
725
Evan Cheng9ade2182006-08-26 05:34:46 +0000726 SDNode *Select(SDOperand Op);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000727 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000728 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000729 bool SelectAddrMode1(SDOperand N, SDOperand &Arg, SDOperand &Shift,
730 SDOperand &ShiftType);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000731
732 // Include the pieces autogenerated from the target description.
733#include "ARMGenDAGISel.inc"
734};
735
736void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
737 DEBUG(BB->dump());
738
739 DAG.setRoot(SelectRoot(DAG.getRoot()));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000740 DAG.RemoveDeadNodes();
741
742 ScheduleAndEmitDAG(DAG);
743}
744
Rafael Espindola61369da2006-08-14 19:01:24 +0000745static bool isInt12Immediate(SDNode *N, short &Imm) {
746 if (N->getOpcode() != ISD::Constant)
747 return false;
748
749 int32_t t = cast<ConstantSDNode>(N)->getValue();
Rafael Espindola7246d332006-09-21 11:29:52 +0000750 int max = 1<<12;
Rafael Espindola61369da2006-08-14 19:01:24 +0000751 int min = -max;
752 if (t > min && t < max) {
753 Imm = t;
754 return true;
755 }
756 else
757 return false;
758}
759
760static bool isInt12Immediate(SDOperand Op, short &Imm) {
761 return isInt12Immediate(Op.Val, Imm);
762}
763
Rafael Espindola7246d332006-09-21 11:29:52 +0000764static uint32_t rotateL(uint32_t x) {
765 uint32_t bit31 = (x & (1 << 31)) >> 31;
766 uint32_t t = x << 1;
767 return t | bit31;
768}
769
770static bool isUInt8Immediate(uint32_t x) {
771 return x < (1 << 8);
772}
773
774static bool isRotInt8Immediate(uint32_t x) {
775 int r;
776 for (r = 0; r < 16; r++) {
777 if (isUInt8Immediate(x))
778 return true;
779 x = rotateL(rotateL(x));
780 }
781 return false;
782}
783
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000784bool ARMDAGToDAGISel::SelectAddrMode1(SDOperand N,
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000785 SDOperand &Arg,
786 SDOperand &Shift,
787 SDOperand &ShiftType) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000788 switch(N.getOpcode()) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000789 case ISD::Constant: {
Rafael Espindola7246d332006-09-21 11:29:52 +0000790 uint32_t val = cast<ConstantSDNode>(N)->getValue();
791 if(!isRotInt8Immediate(val)) {
792 const Type *t = MVT::getTypeForValueType(MVT::i32);
793 Constant *C = ConstantUInt::get(t, val);
794 int alignment = 2;
795 SDOperand Addr = CurDAG->getTargetConstantPool(C, MVT::i32, alignment);
796 SDOperand Z = CurDAG->getTargetConstant(0, MVT::i32);
797 SDNode *n = CurDAG->getTargetNode(ARM::ldr, MVT::i32, Z, Addr);
798 Arg = SDOperand(n, 0);
799 } else
800 Arg = CurDAG->getTargetConstant(val, MVT::i32);
801
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000802 Shift = CurDAG->getTargetConstant(0, MVT::i32);
803 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000804 return true;
805 }
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000806 case ISD::SRA:
807 Arg = N.getOperand(0);
808 Shift = N.getOperand(1);
809 ShiftType = CurDAG->getTargetConstant(ARMShift::ASR, MVT::i32);
810 return true;
811 case ISD::SRL:
812 Arg = N.getOperand(0);
813 Shift = N.getOperand(1);
814 ShiftType = CurDAG->getTargetConstant(ARMShift::LSR, MVT::i32);
815 return true;
816 case ISD::SHL:
817 Arg = N.getOperand(0);
818 Shift = N.getOperand(1);
819 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
820 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000821 }
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000822
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000823 Arg = N;
824 Shift = CurDAG->getTargetConstant(0, MVT::i32);
825 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000826 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000827}
828
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000829//register plus/minus 12 bit offset
830bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
831 SDOperand &Base) {
Rafael Espindolaf3a335c2006-08-17 17:09:40 +0000832 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
833 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
834 Offset = CurDAG->getTargetConstant(0, MVT::i32);
835 return true;
836 }
Rafael Espindola61369da2006-08-14 19:01:24 +0000837 if (N.getOpcode() == ISD::ADD) {
838 short imm = 0;
839 if (isInt12Immediate(N.getOperand(1), imm)) {
840 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
841 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
842 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
843 } else {
844 Base = N.getOperand(0);
845 }
846 return true; // [r+i]
847 }
848 }
849
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000850 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000851 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
852 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
853 }
854 else
855 Base = N;
856 return true; //any address fits in a register
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000857}
858
Evan Cheng9ade2182006-08-26 05:34:46 +0000859SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000860 SDNode *N = Op.Val;
861
862 switch (N->getOpcode()) {
863 default:
Evan Cheng9ade2182006-08-26 05:34:46 +0000864 return SelectCode(Op);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000865 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000866 }
Evan Cheng64a752f2006-08-11 09:08:15 +0000867 return NULL;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000868}
869
870} // end anonymous namespace
871
872/// createARMISelDag - This pass converts a legalized DAG into a
873/// ARM-specific DAG, ready for instruction scheduling.
874///
875FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
876 return new ARMDAGToDAGISel(TM);
877}