blob: 905d9620e20d9a44d778e6ae809221cbe203e8e2 [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 Espindolad2b56682006-10-14 17:59:54 +000073 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
74
Rafael Espindola0505be02006-10-16 21:10:32 +000075 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
76 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
77 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
78
Rafael Espindola755be9b2006-08-25 17:55:16 +000079 setOperationAction(ISD::VASTART, MVT::Other, Custom);
80 setOperationAction(ISD::VAEND, MVT::Other, Expand);
81
Rafael Espindolacd71da52006-10-03 17:27:58 +000082 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
83 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
84
Rafael Espindola341b8642006-08-04 12:48:42 +000085 setSchedulingPreference(SchedulingForRegPressure);
Rafael Espindola3717ca92006-08-20 01:49:49 +000086 computeRegisterProperties();
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000087}
88
Rafael Espindola84b19be2006-07-16 01:02:57 +000089namespace llvm {
90 namespace ARMISD {
91 enum NodeType {
92 // Start the numbering where the builting ops and target ops leave off.
93 FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
94 /// CALL - A direct function call.
Rafael Espindolaf4fda802006-08-03 17:02:20 +000095 CALL,
96
97 /// Return with a flag operand.
Rafael Espindola3c000bf2006-08-21 22:00:32 +000098 RET_FLAG,
99
100 CMP,
101
Rafael Espindola687bc492006-08-24 13:45:55 +0000102 SELECT,
103
Rafael Espindola27185192006-09-29 21:20:16 +0000104 BR,
105
Rafael Espindola9e071f02006-10-02 19:30:56 +0000106 FSITOS,
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000107 FTOSIS,
Rafael Espindola9e071f02006-10-02 19:30:56 +0000108
109 FSITOD,
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000110 FTOSID,
Rafael Espindola9e071f02006-10-02 19:30:56 +0000111
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000112 FUITOS,
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000113 FTOUIS,
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000114
115 FUITOD,
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000116 FTOUID,
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000117
Rafael Espindolaa2845842006-10-05 16:48:49 +0000118 FMRRD,
119
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000120 FMDRR,
121
122 FMSTAT
Rafael Espindola84b19be2006-07-16 01:02:57 +0000123 };
124 }
125}
126
Rafael Espindola42b62f32006-10-13 13:14:59 +0000127/// DAGFPCCToARMCC - Convert a DAG fp condition code to an ARM CC
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000128// Unordered = !N & !Z & C & V = V
129// Ordered = N | Z | !C | !V = N | Z | !V
Rafael Espindola42b62f32006-10-13 13:14:59 +0000130static ARMCC::CondCodes DAGFPCCToARMCC(ISD::CondCode CC) {
Rafael Espindola6f602de2006-08-24 16:13:15 +0000131 switch (CC) {
Rafael Espindolaebdabda2006-09-21 13:06:26 +0000132 default:
Rafael Espindola42b62f32006-10-13 13:14:59 +0000133 assert(0 && "Unknown fp condition code!");
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000134// SETOEQ = (N | Z | !V) & Z = Z = EQ
135 case ISD::SETEQ:
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000136 case ISD::SETOEQ: return ARMCC::EQ;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000137// SETOGT = (N | Z | !V) & !N & !Z = !V &!N &!Z = (N = V) & !Z = GT
138 case ISD::SETGT:
Rafael Espindola42b62f32006-10-13 13:14:59 +0000139 case ISD::SETOGT: return ARMCC::GT;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000140// SETOGE = (N | Z | !V) & !N = (Z | !V) & !N = !V & !N = GE
141 case ISD::SETGE:
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000142 case ISD::SETOGE: return ARMCC::GE;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000143// SETOLT = (N | Z | !V) & N = N = MI
144 case ISD::SETLT:
145 case ISD::SETOLT: return ARMCC::MI;
146// SETOLE = (N | Z | !V) & (N | Z) = N | Z = !C | Z = LS
147 case ISD::SETLE:
148 case ISD::SETOLE: return ARMCC::LS;
149// SETONE = (N | Z | !V) & !Z = (N | !V) & Z = !V & Z = Z = NE
150 case ISD::SETNE:
Rafael Espindola42b62f32006-10-13 13:14:59 +0000151 case ISD::SETONE: return ARMCC::NE;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000152// SETO = N | Z | !V = Z | !V = !V = VC
153 case ISD::SETO: return ARMCC::VC;
154// SETUO = V = VS
155 case ISD::SETUO: return ARMCC::VS;
156// SETUEQ = V | Z = ??
157// SETUGT = V | (!Z & !N) = !Z & !N = !Z & C = HI
158 case ISD::SETUGT: return ARMCC::HI;
159// SETUGE = V | !N = !N = PL
Rafael Espindola42b62f32006-10-13 13:14:59 +0000160 case ISD::SETUGE: return ARMCC::PL;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000161// SETULT = V | N = ??
162// SETULE = V | Z | N = ??
163// SETUNE = V | !Z = !Z = NE
Rafael Espindola42b62f32006-10-13 13:14:59 +0000164 case ISD::SETUNE: return ARMCC::NE;
165 }
166}
167
168/// DAGIntCCToARMCC - Convert a DAG integer condition code to an ARM CC
169static ARMCC::CondCodes DAGIntCCToARMCC(ISD::CondCode CC) {
170 switch (CC) {
171 default:
172 assert(0 && "Unknown integer condition code!");
173 case ISD::SETEQ: return ARMCC::EQ;
174 case ISD::SETNE: return ARMCC::NE;
175 case ISD::SETLT: return ARMCC::LT;
176 case ISD::SETLE: return ARMCC::LE;
177 case ISD::SETGT: return ARMCC::GT;
178 case ISD::SETGE: return ARMCC::GE;
Rafael Espindolabc4cec92006-09-03 13:19:16 +0000179 case ISD::SETULT: return ARMCC::CC;
Rafael Espindola42b62f32006-10-13 13:14:59 +0000180 case ISD::SETULE: return ARMCC::LS;
181 case ISD::SETUGT: return ARMCC::HI;
182 case ISD::SETUGE: return ARMCC::CS;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000183 }
184}
185
Rafael Espindola84b19be2006-07-16 01:02:57 +0000186const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
187 switch (Opcode) {
188 default: return 0;
189 case ARMISD::CALL: return "ARMISD::CALL";
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000190 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000191 case ARMISD::SELECT: return "ARMISD::SELECT";
192 case ARMISD::CMP: return "ARMISD::CMP";
Rafael Espindola687bc492006-08-24 13:45:55 +0000193 case ARMISD::BR: return "ARMISD::BR";
Rafael Espindola27185192006-09-29 21:20:16 +0000194 case ARMISD::FSITOS: return "ARMISD::FSITOS";
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000195 case ARMISD::FTOSIS: return "ARMISD::FTOSIS";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000196 case ARMISD::FSITOD: return "ARMISD::FSITOD";
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000197 case ARMISD::FTOSID: return "ARMISD::FTOSID";
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000198 case ARMISD::FUITOS: return "ARMISD::FUITOS";
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000199 case ARMISD::FTOUIS: return "ARMISD::FTOUIS";
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000200 case ARMISD::FUITOD: return "ARMISD::FUITOD";
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000201 case ARMISD::FTOUID: return "ARMISD::FTOUID";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000202 case ARMISD::FMRRD: return "ARMISD::FMRRD";
Rafael Espindolaa2845842006-10-05 16:48:49 +0000203 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000204 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Rafael Espindola84b19be2006-07-16 01:02:57 +0000205 }
206}
207
Rafael Espindolaa2845842006-10-05 16:48:49 +0000208class ArgumentLayout {
209 std::vector<bool> is_reg;
210 std::vector<unsigned> pos;
211 std::vector<MVT::ValueType> types;
212public:
Rafael Espindola39b5a212006-10-05 17:46:48 +0000213 ArgumentLayout(const std::vector<MVT::ValueType> &Types) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000214 types = Types;
215
216 unsigned RegNum = 0;
217 unsigned StackOffset = 0;
Rafael Espindola39b5a212006-10-05 17:46:48 +0000218 for(std::vector<MVT::ValueType>::const_iterator I = Types.begin();
Rafael Espindolaa2845842006-10-05 16:48:49 +0000219 I != Types.end();
220 ++I) {
221 MVT::ValueType VT = *I;
222 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
223 unsigned size = MVT::getSizeInBits(VT)/32;
224
225 RegNum = ((RegNum + size - 1) / size) * size;
226 if (RegNum < 4) {
227 pos.push_back(RegNum);
228 is_reg.push_back(true);
229 RegNum += size;
230 } else {
231 unsigned bytes = size * 32/8;
232 StackOffset = ((StackOffset + bytes - 1) / bytes) * bytes;
233 pos.push_back(StackOffset);
234 is_reg.push_back(false);
235 StackOffset += bytes;
236 }
237 }
238 }
239 unsigned getRegisterNum(unsigned argNum) {
240 assert(isRegister(argNum));
241 return pos[argNum];
242 }
243 unsigned getOffset(unsigned argNum) {
244 assert(isOffset(argNum));
245 return pos[argNum];
246 }
247 unsigned isRegister(unsigned argNum) {
248 assert(argNum < is_reg.size());
249 return is_reg[argNum];
250 }
251 unsigned isOffset(unsigned argNum) {
252 return !isRegister(argNum);
253 }
254 MVT::ValueType getType(unsigned argNum) {
255 assert(argNum < types.size());
256 return types[argNum];
257 }
258 unsigned getStackSize(void) {
259 int last = is_reg.size() - 1;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000260 if (last < 0)
261 return 0;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000262 if (isRegister(last))
263 return 0;
264 return getOffset(last) + MVT::getSizeInBits(getType(last))/8;
265 }
266 int lastRegArg(void) {
267 int size = is_reg.size();
268 int last = 0;
269 while(last < size && isRegister(last))
270 last++;
271 last--;
272 return last;
273 }
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000274 int lastRegNum(void) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000275 int l = lastRegArg();
276 if (l < 0)
277 return -1;
278 unsigned r = getRegisterNum(l);
279 MVT::ValueType t = getType(l);
280 assert(t == MVT::i32 || t == MVT::f32 || t == MVT::f64);
281 if (t == MVT::f64)
282 return r + 1;
283 return r;
284 }
285};
286
Rafael Espindola84b19be2006-07-16 01:02:57 +0000287// This transforms a ISD::CALL node into a
288// callseq_star <- ARMISD:CALL <- callseq_end
289// chain
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000290static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola84b19be2006-07-16 01:02:57 +0000291 SDOperand Chain = Op.getOperand(0);
292 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
293 assert(CallConv == CallingConv::C && "unknown calling convention");
294 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000295 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000296 SDOperand Callee = Op.getOperand(4);
297 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Rafael Espindola1a009462006-08-08 13:02:29 +0000298 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000299 static const unsigned regs[] = {
Rafael Espindolafac00a92006-07-25 20:17:20 +0000300 ARM::R0, ARM::R1, ARM::R2, ARM::R3
301 };
302
Rafael Espindolaa2845842006-10-05 16:48:49 +0000303 std::vector<MVT::ValueType> Types;
304 for (unsigned i = 0; i < NumOps; ++i) {
305 MVT::ValueType VT = Op.getOperand(5+2*i).getValueType();
306 Types.push_back(VT);
307 }
308 ArgumentLayout Layout(Types);
Rafael Espindolafac00a92006-07-25 20:17:20 +0000309
Rafael Espindolaa2845842006-10-05 16:48:49 +0000310 unsigned NumBytes = Layout.getStackSize();
311
312 Chain = DAG.getCALLSEQ_START(Chain,
313 DAG.getConstant(NumBytes, MVT::i32));
314
315 //Build a sequence of stores
316 std::vector<SDOperand> MemOpChains;
317 for (unsigned i = Layout.lastRegArg() + 1; i < NumOps; ++i) {
318 SDOperand Arg = Op.getOperand(5+2*i);
319 unsigned ArgOffset = Layout.getOffset(i);
320 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
321 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Evan Cheng8b2794a2006-10-13 21:14:26 +0000322 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000323 }
Rafael Espindola1a009462006-08-08 13:02:29 +0000324 if (!MemOpChains.empty())
Chris Lattnere2199452006-08-11 17:38:39 +0000325 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
326 &MemOpChains[0], MemOpChains.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000327
Rafael Espindola0505be02006-10-16 21:10:32 +0000328 // If the callee is a GlobalAddress node (quite common, every direct call is)
329 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
330 // Likewise ExternalSymbol -> TargetExternalSymbol.
331 assert(Callee.getValueType() == MVT::i32);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000332 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Rafael Espindola0505be02006-10-16 21:10:32 +0000333 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
334 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
335 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000336
337 // If this is a direct call, pass the chain and the callee.
338 assert (Callee.Val);
339 std::vector<SDOperand> Ops;
340 Ops.push_back(Chain);
341 Ops.push_back(Callee);
342
Rafael Espindolaa2845842006-10-05 16:48:49 +0000343 // Build a sequence of copy-to-reg nodes chained together with token chain
344 // and flag operands which copy the outgoing args into the appropriate regs.
345 SDOperand InFlag;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000346 for (int i = 0, e = Layout.lastRegArg(); i <= e; ++i) {
Rafael Espindola4a408d42006-10-06 12:50:22 +0000347 SDOperand Arg = Op.getOperand(5+2*i);
348 unsigned RegNum = Layout.getRegisterNum(i);
349 unsigned Reg1 = regs[RegNum];
350 MVT::ValueType VT = Layout.getType(i);
351 assert(VT == Arg.getValueType());
352 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000353
354 // Add argument register to the end of the list so that it is known live
355 // into the call.
Rafael Espindola4a408d42006-10-06 12:50:22 +0000356 Ops.push_back(DAG.getRegister(Reg1, MVT::i32));
357 if (VT == MVT::f64) {
358 unsigned Reg2 = regs[RegNum + 1];
359 SDOperand SDReg1 = DAG.getRegister(Reg1, MVT::i32);
360 SDOperand SDReg2 = DAG.getRegister(Reg2, MVT::i32);
361
362 Ops.push_back(DAG.getRegister(Reg2, MVT::i32));
363 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola935b1f82006-10-06 20:33:26 +0000364 SDOperand Ops[] = {Chain, SDReg1, SDReg2, Arg, InFlag};
365 Chain = DAG.getNode(ARMISD::FMRRD, VTs, Ops, InFlag.Val ? 5 : 4);
Rafael Espindola4a408d42006-10-06 12:50:22 +0000366 } else {
367 if (VT == MVT::f32)
368 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg);
369 Chain = DAG.getCopyToReg(Chain, Reg1, Arg, InFlag);
370 }
371 InFlag = Chain.getValue(1);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000372 }
373
374 std::vector<MVT::ValueType> NodeTys;
375 NodeTys.push_back(MVT::Other); // Returns a chain
376 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Rafael Espindola7a53bd02006-08-09 16:41:12 +0000377
Rafael Espindola84b19be2006-07-16 01:02:57 +0000378 unsigned CallOpc = ARMISD::CALL;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000379 if (InFlag.Val)
380 Ops.push_back(InFlag);
Chris Lattner87428672006-08-11 17:22:35 +0000381 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000382 InFlag = Chain.getValue(1);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000383
Rafael Espindolafac00a92006-07-25 20:17:20 +0000384 std::vector<SDOperand> ResultVals;
385 NodeTys.clear();
386
387 // If the call has results, copy the values out of the ret val registers.
Rafael Espindola614057b2006-10-06 19:10:05 +0000388 MVT::ValueType VT = Op.Val->getValueType(0);
389 if (VT != MVT::Other) {
390 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
Rafael Espindola614057b2006-10-06 19:10:05 +0000391
392 SDOperand Value1 = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
393 Chain = Value1.getValue(1);
394 InFlag = Value1.getValue(2);
Rafael Espindola26a76d12006-10-13 16:47:22 +0000395 NodeTys.push_back(VT);
396 if (VT == MVT::i32) {
397 ResultVals.push_back(Value1);
398 if (Op.Val->getValueType(1) == MVT::i32) {
399 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
400 Chain = Value2.getValue(1);
401 ResultVals.push_back(Value2);
402 NodeTys.push_back(VT);
403 }
404 }
405 if (VT == MVT::f32) {
406 SDOperand Value = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Value1);
407 ResultVals.push_back(Value);
408 }
Rafael Espindola614057b2006-10-06 19:10:05 +0000409 if (VT == MVT::f64) {
410 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
411 Chain = Value2.getValue(1);
Rafael Espindola26a76d12006-10-13 16:47:22 +0000412 SDOperand Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
413 ResultVals.push_back(Value);
Rafael Espindola614057b2006-10-06 19:10:05 +0000414 }
Rafael Espindolafac00a92006-07-25 20:17:20 +0000415 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000416
417 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
418 DAG.getConstant(NumBytes, MVT::i32));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000419 NodeTys.push_back(MVT::Other);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000420
Rafael Espindolafac00a92006-07-25 20:17:20 +0000421 if (ResultVals.empty())
422 return Chain;
423
424 ResultVals.push_back(Chain);
Chris Lattner87428672006-08-11 17:22:35 +0000425 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
426 ResultVals.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000427 return Res.getValue(Op.ResNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000428}
429
430static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
431 SDOperand Copy;
Rafael Espindola4b023672006-06-05 22:26:14 +0000432 SDOperand Chain = Op.getOperand(0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000433 SDOperand R0 = DAG.getRegister(ARM::R0, MVT::i32);
434 SDOperand R1 = DAG.getRegister(ARM::R1, MVT::i32);
435
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000436 switch(Op.getNumOperands()) {
437 default:
438 assert(0 && "Do not know how to return this many arguments!");
439 abort();
Rafael Espindola4b023672006-06-05 22:26:14 +0000440 case 1: {
441 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
Rafael Espindola6312da02006-08-03 22:50:11 +0000442 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
Rafael Espindola4b023672006-06-05 22:26:14 +0000443 }
Rafael Espindola27185192006-09-29 21:20:16 +0000444 case 3: {
445 SDOperand Val = Op.getOperand(1);
446 assert(Val.getValueType() == MVT::i32 ||
Rafael Espindola9e071f02006-10-02 19:30:56 +0000447 Val.getValueType() == MVT::f32 ||
448 Val.getValueType() == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000449
Rafael Espindola9e071f02006-10-02 19:30:56 +0000450 if (Val.getValueType() == MVT::f64) {
451 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
452 SDOperand Ops[] = {Chain, R0, R1, Val};
453 Copy = DAG.getNode(ARMISD::FMRRD, VTs, Ops, 4);
454 } else {
455 if (Val.getValueType() == MVT::f32)
456 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
457 Copy = DAG.getCopyToReg(Chain, R0, Val, SDOperand());
458 }
459
460 if (DAG.getMachineFunction().liveout_empty()) {
Rafael Espindola4b023672006-06-05 22:26:14 +0000461 DAG.getMachineFunction().addLiveOut(ARM::R0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000462 if (Val.getValueType() == MVT::f64)
463 DAG.getMachineFunction().addLiveOut(ARM::R1);
464 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000465 break;
Rafael Espindola27185192006-09-29 21:20:16 +0000466 }
Rafael Espindola3a02f022006-09-04 19:05:01 +0000467 case 5:
468 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
469 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
470 // If we haven't noted the R0+R1 are live out, do so now.
471 if (DAG.getMachineFunction().liveout_empty()) {
472 DAG.getMachineFunction().addLiveOut(ARM::R0);
473 DAG.getMachineFunction().addLiveOut(ARM::R1);
474 }
475 break;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000476 }
Rafael Espindola4b023672006-06-05 22:26:14 +0000477
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000478 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
479 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000480}
481
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000482static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
483 MVT::ValueType PtrVT = Op.getValueType();
484 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengc356a572006-09-12 21:04:05 +0000485 Constant *C = CP->getConstVal();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000486 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
487
488 return CPI;
489}
490
491static SDOperand LowerGlobalAddress(SDOperand Op,
492 SelectionDAG &DAG) {
493 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Rafael Espindola61369da2006-08-14 19:01:24 +0000494 int alignment = 2;
495 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
Evan Cheng466685d2006-10-09 20:57:25 +0000496 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr, NULL, 0);
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000497}
498
Rafael Espindola755be9b2006-08-25 17:55:16 +0000499static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
500 unsigned VarArgsFrameIndex) {
501 // vastart just stores the address of the VarArgsFrameIndex slot into the
502 // memory location argument.
503 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
504 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +0000505 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
506 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), SV->getValue(),
507 SV->getOffset());
Rafael Espindola755be9b2006-08-25 17:55:16 +0000508}
509
510static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
511 int &VarArgsFrameIndex) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000512 MachineFunction &MF = DAG.getMachineFunction();
513 MachineFrameInfo *MFI = MF.getFrameInfo();
514 SSARegMap *RegMap = MF.getSSARegMap();
515 unsigned NumArgs = Op.Val->getNumValues()-1;
516 SDOperand Root = Op.getOperand(0);
517 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
518 static const unsigned REGS[] = {
519 ARM::R0, ARM::R1, ARM::R2, ARM::R3
520 };
521
522 std::vector<MVT::ValueType> Types(Op.Val->value_begin(), Op.Val->value_end() - 1);
523 ArgumentLayout Layout(Types);
524
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000525 std::vector<SDOperand> ArgValues;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000526 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000527 MVT::ValueType VT = Types[ArgNo];
Rafael Espindola4b442b52006-05-23 02:48:20 +0000528
Rafael Espindolaa2845842006-10-05 16:48:49 +0000529 SDOperand Value;
530 if (Layout.isRegister(ArgNo)) {
531 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
532 unsigned RegNum = Layout.getRegisterNum(ArgNo);
533 unsigned Reg1 = REGS[RegNum];
534 unsigned VReg1 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
535 SDOperand Value1 = DAG.getCopyFromReg(Root, VReg1, MVT::i32);
536 MF.addLiveIn(Reg1, VReg1);
537 if (VT == MVT::f64) {
538 unsigned Reg2 = REGS[RegNum + 1];
539 unsigned VReg2 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
540 SDOperand Value2 = DAG.getCopyFromReg(Root, VReg2, MVT::i32);
541 MF.addLiveIn(Reg2, VReg2);
542 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
543 } else {
544 Value = Value1;
545 if (VT == MVT::f32)
546 Value = DAG.getNode(ISD::BIT_CONVERT, VT, Value);
547 }
548 } else {
549 // If the argument is actually used, emit a load from the right stack
550 // slot.
551 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
552 unsigned Offset = Layout.getOffset(ArgNo);
553 unsigned Size = MVT::getSizeInBits(VT)/8;
554 int FI = MFI->CreateFixedObject(Size, Offset);
555 SDOperand FIN = DAG.getFrameIndex(FI, VT);
Evan Cheng466685d2006-10-09 20:57:25 +0000556 Value = DAG.getLoad(VT, Root, FIN, NULL, 0);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000557 } else {
558 Value = DAG.getNode(ISD::UNDEF, VT);
559 }
560 }
561 ArgValues.push_back(Value);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000562 }
563
Rafael Espindolaa2845842006-10-05 16:48:49 +0000564 unsigned NextRegNum = Layout.lastRegNum() + 1;
565
Rafael Espindola755be9b2006-08-25 17:55:16 +0000566 if (isVarArg) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000567 //If this function is vararg we must store the remaing
568 //registers so that they can be acessed with va_start
Rafael Espindola755be9b2006-08-25 17:55:16 +0000569 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000570 -16 + NextRegNum * 4);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000571
Rafael Espindola755be9b2006-08-25 17:55:16 +0000572 SmallVector<SDOperand, 4> MemOps;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000573 for (unsigned RegNo = NextRegNum; RegNo < 4; ++RegNo) {
574 int RegOffset = - (4 - RegNo) * 4;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000575 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000576 RegOffset);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000577 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
578
Rafael Espindolaa2845842006-10-05 16:48:49 +0000579 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
580 MF.addLiveIn(REGS[RegNo], VReg);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000581
582 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
Evan Cheng8b2794a2006-10-13 21:14:26 +0000583 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000584 MemOps.push_back(Store);
585 }
586 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
587 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000588
589 ArgValues.push_back(Root);
590
591 // Return the new list of results.
592 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
593 Op.Val->value_end());
Chris Lattner87428672006-08-11 17:22:35 +0000594 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
Rafael Espindoladc124a22006-05-18 21:45:49 +0000595}
596
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000597static SDOperand GetCMP(ISD::CondCode CC, SDOperand LHS, SDOperand RHS,
598 SelectionDAG &DAG) {
599 MVT::ValueType vt = LHS.getValueType();
Rafael Espindola0d9fe762006-10-10 16:33:47 +0000600 assert(vt == MVT::i32 || vt == MVT::f32 || vt == MVT::f64);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000601
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000602 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
Rafael Espindola42b62f32006-10-13 13:14:59 +0000603
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000604 if (vt != MVT::i32)
605 Cmp = DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp);
606 return Cmp;
607}
608
Rafael Espindola42b62f32006-10-13 13:14:59 +0000609static SDOperand GetARMCC(ISD::CondCode CC, MVT::ValueType vt,
610 SelectionDAG &DAG) {
611 assert(vt == MVT::i32 || vt == MVT::f32 || vt == MVT::f64);
612 if (vt == MVT::i32)
613 return DAG.getConstant(DAGIntCCToARMCC(CC), MVT::i32);
614 else
615 return DAG.getConstant(DAGFPCCToARMCC(CC), MVT::i32);
616}
617
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000618static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
619 SDOperand LHS = Op.getOperand(0);
620 SDOperand RHS = Op.getOperand(1);
621 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
622 SDOperand TrueVal = Op.getOperand(2);
623 SDOperand FalseVal = Op.getOperand(3);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000624 SDOperand Cmp = GetCMP(CC, LHS, RHS, DAG);
Rafael Espindola42b62f32006-10-13 13:14:59 +0000625 SDOperand ARMCC = GetARMCC(CC, LHS.getValueType(), DAG);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000626 return DAG.getNode(ARMISD::SELECT, MVT::i32, TrueVal, FalseVal, ARMCC, Cmp);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000627}
628
Rafael Espindola687bc492006-08-24 13:45:55 +0000629static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
630 SDOperand Chain = Op.getOperand(0);
631 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
632 SDOperand LHS = Op.getOperand(2);
633 SDOperand RHS = Op.getOperand(3);
634 SDOperand Dest = Op.getOperand(4);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000635 SDOperand Cmp = GetCMP(CC, LHS, RHS, DAG);
Rafael Espindola42b62f32006-10-13 13:14:59 +0000636 SDOperand ARMCC = GetARMCC(CC, LHS.getValueType(), DAG);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000637 return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, ARMCC, Cmp);
Rafael Espindola687bc492006-08-24 13:45:55 +0000638}
639
Rafael Espindola27185192006-09-29 21:20:16 +0000640static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola9e071f02006-10-02 19:30:56 +0000641 SDOperand IntVal = Op.getOperand(0);
Rafael Espindola27185192006-09-29 21:20:16 +0000642 assert(IntVal.getValueType() == MVT::i32);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000643 MVT::ValueType vt = Op.getValueType();
644 assert(vt == MVT::f32 ||
645 vt == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000646
647 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000648 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FSITOS : ARMISD::FSITOD;
649 return DAG.getNode(op, vt, Tmp);
Rafael Espindola27185192006-09-29 21:20:16 +0000650}
651
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000652static SDOperand LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
653 assert(Op.getValueType() == MVT::i32);
654 SDOperand FloatVal = Op.getOperand(0);
655 MVT::ValueType vt = FloatVal.getValueType();
656 assert(vt == MVT::f32 || vt == MVT::f64);
657
658 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FTOSIS : ARMISD::FTOSID;
659 SDOperand Tmp = DAG.getNode(op, MVT::f32, FloatVal);
660 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Tmp);
661}
662
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000663static SDOperand LowerUINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
664 SDOperand IntVal = Op.getOperand(0);
665 assert(IntVal.getValueType() == MVT::i32);
666 MVT::ValueType vt = Op.getValueType();
667 assert(vt == MVT::f32 ||
668 vt == MVT::f64);
669
670 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
671 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FUITOS : ARMISD::FUITOD;
672 return DAG.getNode(op, vt, Tmp);
673}
674
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000675static SDOperand LowerFP_TO_UINT(SDOperand Op, SelectionDAG &DAG) {
676 assert(Op.getValueType() == MVT::i32);
677 SDOperand FloatVal = Op.getOperand(0);
678 MVT::ValueType vt = FloatVal.getValueType();
679 assert(vt == MVT::f32 || vt == MVT::f64);
680
681 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FTOUIS : ARMISD::FTOUID;
682 SDOperand Tmp = DAG.getNode(op, MVT::f32, FloatVal);
683 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Tmp);
684}
685
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000686SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
687 switch (Op.getOpcode()) {
688 default:
689 assert(0 && "Should not custom lower this!");
Rafael Espindola1c8f0532006-05-15 22:34:39 +0000690 abort();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000691 case ISD::ConstantPool:
692 return LowerConstantPool(Op, DAG);
693 case ISD::GlobalAddress:
694 return LowerGlobalAddress(Op, DAG);
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000695 case ISD::FP_TO_SINT:
696 return LowerFP_TO_SINT(Op, DAG);
Rafael Espindola27185192006-09-29 21:20:16 +0000697 case ISD::SINT_TO_FP:
698 return LowerSINT_TO_FP(Op, DAG);
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000699 case ISD::FP_TO_UINT:
700 return LowerFP_TO_UINT(Op, DAG);
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000701 case ISD::UINT_TO_FP:
702 return LowerUINT_TO_FP(Op, DAG);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000703 case ISD::FORMAL_ARGUMENTS:
Rafael Espindola755be9b2006-08-25 17:55:16 +0000704 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000705 case ISD::CALL:
706 return LowerCALL(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000707 case ISD::RET:
708 return LowerRET(Op, DAG);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000709 case ISD::SELECT_CC:
710 return LowerSELECT_CC(Op, DAG);
Rafael Espindola687bc492006-08-24 13:45:55 +0000711 case ISD::BR_CC:
712 return LowerBR_CC(Op, DAG);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000713 case ISD::VASTART:
714 return LowerVASTART(Op, DAG, VarArgsFrameIndex);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000715 }
716}
717
718//===----------------------------------------------------------------------===//
719// Instruction Selector Implementation
720//===----------------------------------------------------------------------===//
721
722//===--------------------------------------------------------------------===//
723/// ARMDAGToDAGISel - ARM specific code to select ARM machine
724/// instructions for SelectionDAG operations.
725///
726namespace {
727class ARMDAGToDAGISel : public SelectionDAGISel {
728 ARMTargetLowering Lowering;
729
730public:
731 ARMDAGToDAGISel(TargetMachine &TM)
732 : SelectionDAGISel(Lowering), Lowering(TM) {
733 }
734
Evan Cheng9ade2182006-08-26 05:34:46 +0000735 SDNode *Select(SDOperand Op);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000736 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000737 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000738 bool SelectAddrMode1(SDOperand N, SDOperand &Arg, SDOperand &Shift,
739 SDOperand &ShiftType);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000740
741 // Include the pieces autogenerated from the target description.
742#include "ARMGenDAGISel.inc"
743};
744
745void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
746 DEBUG(BB->dump());
747
748 DAG.setRoot(SelectRoot(DAG.getRoot()));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000749 DAG.RemoveDeadNodes();
750
751 ScheduleAndEmitDAG(DAG);
752}
753
Rafael Espindola61369da2006-08-14 19:01:24 +0000754static bool isInt12Immediate(SDNode *N, short &Imm) {
755 if (N->getOpcode() != ISD::Constant)
756 return false;
757
758 int32_t t = cast<ConstantSDNode>(N)->getValue();
Rafael Espindola7246d332006-09-21 11:29:52 +0000759 int max = 1<<12;
Rafael Espindola61369da2006-08-14 19:01:24 +0000760 int min = -max;
761 if (t > min && t < max) {
762 Imm = t;
763 return true;
764 }
765 else
766 return false;
767}
768
769static bool isInt12Immediate(SDOperand Op, short &Imm) {
770 return isInt12Immediate(Op.Val, Imm);
771}
772
Rafael Espindola7246d332006-09-21 11:29:52 +0000773static uint32_t rotateL(uint32_t x) {
774 uint32_t bit31 = (x & (1 << 31)) >> 31;
775 uint32_t t = x << 1;
776 return t | bit31;
777}
778
779static bool isUInt8Immediate(uint32_t x) {
780 return x < (1 << 8);
781}
782
783static bool isRotInt8Immediate(uint32_t x) {
784 int r;
785 for (r = 0; r < 16; r++) {
786 if (isUInt8Immediate(x))
787 return true;
788 x = rotateL(rotateL(x));
789 }
790 return false;
791}
792
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000793bool ARMDAGToDAGISel::SelectAddrMode1(SDOperand N,
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000794 SDOperand &Arg,
795 SDOperand &Shift,
796 SDOperand &ShiftType) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000797 switch(N.getOpcode()) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000798 case ISD::Constant: {
Rafael Espindola7246d332006-09-21 11:29:52 +0000799 uint32_t val = cast<ConstantSDNode>(N)->getValue();
800 if(!isRotInt8Immediate(val)) {
801 const Type *t = MVT::getTypeForValueType(MVT::i32);
802 Constant *C = ConstantUInt::get(t, val);
803 int alignment = 2;
804 SDOperand Addr = CurDAG->getTargetConstantPool(C, MVT::i32, alignment);
805 SDOperand Z = CurDAG->getTargetConstant(0, MVT::i32);
806 SDNode *n = CurDAG->getTargetNode(ARM::ldr, MVT::i32, Z, Addr);
807 Arg = SDOperand(n, 0);
808 } else
809 Arg = CurDAG->getTargetConstant(val, MVT::i32);
810
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000811 Shift = CurDAG->getTargetConstant(0, MVT::i32);
812 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000813 return true;
814 }
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000815 case ISD::SRA:
816 Arg = N.getOperand(0);
817 Shift = N.getOperand(1);
818 ShiftType = CurDAG->getTargetConstant(ARMShift::ASR, MVT::i32);
819 return true;
820 case ISD::SRL:
821 Arg = N.getOperand(0);
822 Shift = N.getOperand(1);
823 ShiftType = CurDAG->getTargetConstant(ARMShift::LSR, MVT::i32);
824 return true;
825 case ISD::SHL:
826 Arg = N.getOperand(0);
827 Shift = N.getOperand(1);
828 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
829 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000830 }
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000831
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000832 Arg = N;
833 Shift = CurDAG->getTargetConstant(0, MVT::i32);
834 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000835 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000836}
837
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000838//register plus/minus 12 bit offset
839bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
840 SDOperand &Base) {
Rafael Espindolaf3a335c2006-08-17 17:09:40 +0000841 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
842 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
843 Offset = CurDAG->getTargetConstant(0, MVT::i32);
844 return true;
845 }
Rafael Espindola61369da2006-08-14 19:01:24 +0000846 if (N.getOpcode() == ISD::ADD) {
847 short imm = 0;
848 if (isInt12Immediate(N.getOperand(1), imm)) {
849 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
850 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
851 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
852 } else {
853 Base = N.getOperand(0);
854 }
855 return true; // [r+i]
856 }
857 }
858
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000859 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000860 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
861 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
862 }
863 else
864 Base = N;
865 return true; //any address fits in a register
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000866}
867
Evan Cheng9ade2182006-08-26 05:34:46 +0000868SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000869 SDNode *N = Op.Val;
870
871 switch (N->getOpcode()) {
872 default:
Evan Cheng9ade2182006-08-26 05:34:46 +0000873 return SelectCode(Op);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000874 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000875 }
Evan Cheng64a752f2006-08-11 09:08:15 +0000876 return NULL;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000877}
878
879} // end anonymous namespace
880
881/// createARMISelDag - This pass converts a legalized DAG into a
882/// ARM-specific DAG, ready for instruction scheduling.
883///
884FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
885 return new ARMDAGToDAGISel(TM);
886}