blob: 7221818d1752784286a6697916232fafca274c37 [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 Espindola6495bdd2006-10-19 12:06:50 +000062 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
63 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
64 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
65
Rafael Espindola48bc9fb2006-10-09 16:28:33 +000066 setOperationAction(ISD::SELECT, MVT::i32, Expand);
67
Rafael Espindola3c000bf2006-08-21 22:00:32 +000068 setOperationAction(ISD::SETCC, MVT::i32, Expand);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +000069 setOperationAction(ISD::SETCC, MVT::f32, Expand);
70 setOperationAction(ISD::SETCC, MVT::f64, Expand);
71
Rafael Espindola3c000bf2006-08-21 22:00:32 +000072 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
Rafael Espindolad8ed7f82006-10-23 20:08:22 +000073
74 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
75
Rafael Espindola4749aa42006-10-19 10:56:43 +000076 setOperationAction(ISD::BRIND, MVT::i32, Expand);
Rafael Espindola687bc492006-08-24 13:45:55 +000077 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +000078 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
79 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Rafael Espindola3c000bf2006-08-21 22:00:32 +000080
Rafael Espindolad2b56682006-10-14 17:59:54 +000081 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
82
Rafael Espindola0505be02006-10-16 21:10:32 +000083 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
84 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
85 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Rafael Espindola226f8bc2006-10-17 21:05:33 +000086 setOperationAction(ISD::SDIV, MVT::i32, Expand);
87 setOperationAction(ISD::UDIV, MVT::i32, Expand);
88 setOperationAction(ISD::SREM, MVT::i32, Expand);
89 setOperationAction(ISD::UREM, MVT::i32, Expand);
Rafael Espindola0505be02006-10-16 21:10:32 +000090
Rafael Espindola755be9b2006-08-25 17:55:16 +000091 setOperationAction(ISD::VASTART, MVT::Other, Custom);
Rafael Espindola0e5e3aa2006-10-24 20:15:21 +000092 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
Rafael Espindola755be9b2006-08-25 17:55:16 +000093 setOperationAction(ISD::VAEND, MVT::Other, Expand);
Rafael Espindola7ae68ab2006-10-26 13:31:26 +000094 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Rafael Espindola755be9b2006-08-25 17:55:16 +000095
Rafael Espindolacd71da52006-10-03 17:27:58 +000096 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
97 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
98
Rafael Espindola7ae68ab2006-10-26 13:31:26 +000099 setStackPointerRegisterToSaveRestore(ARM::R13);
100
Rafael Espindola341b8642006-08-04 12:48:42 +0000101 setSchedulingPreference(SchedulingForRegPressure);
Rafael Espindola3717ca92006-08-20 01:49:49 +0000102 computeRegisterProperties();
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000103}
104
Rafael Espindola84b19be2006-07-16 01:02:57 +0000105namespace llvm {
106 namespace ARMISD {
107 enum NodeType {
108 // Start the numbering where the builting ops and target ops leave off.
109 FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
110 /// CALL - A direct function call.
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000111 CALL,
112
113 /// Return with a flag operand.
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000114 RET_FLAG,
115
116 CMP,
117
Rafael Espindola687bc492006-08-24 13:45:55 +0000118 SELECT,
119
Rafael Espindola27185192006-09-29 21:20:16 +0000120 BR,
121
Rafael Espindola9e071f02006-10-02 19:30:56 +0000122 FSITOS,
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000123 FTOSIS,
Rafael Espindola9e071f02006-10-02 19:30:56 +0000124
125 FSITOD,
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000126 FTOSID,
Rafael Espindola9e071f02006-10-02 19:30:56 +0000127
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000128 FUITOS,
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000129 FTOUIS,
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000130
131 FUITOD,
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000132 FTOUID,
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000133
Rafael Espindolaa2845842006-10-05 16:48:49 +0000134 FMRRD,
135
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000136 FMDRR,
137
138 FMSTAT
Rafael Espindola84b19be2006-07-16 01:02:57 +0000139 };
140 }
141}
142
Rafael Espindola42b62f32006-10-13 13:14:59 +0000143/// DAGFPCCToARMCC - Convert a DAG fp condition code to an ARM CC
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000144// Unordered = !N & !Z & C & V = V
145// Ordered = N | Z | !C | !V = N | Z | !V
Rafael Espindola42b62f32006-10-13 13:14:59 +0000146static ARMCC::CondCodes DAGFPCCToARMCC(ISD::CondCode CC) {
Rafael Espindola6f602de2006-08-24 16:13:15 +0000147 switch (CC) {
Rafael Espindolaebdabda2006-09-21 13:06:26 +0000148 default:
Rafael Espindola42b62f32006-10-13 13:14:59 +0000149 assert(0 && "Unknown fp condition code!");
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000150// SETOEQ = (N | Z | !V) & Z = Z = EQ
151 case ISD::SETEQ:
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000152 case ISD::SETOEQ: return ARMCC::EQ;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000153// SETOGT = (N | Z | !V) & !N & !Z = !V &!N &!Z = (N = V) & !Z = GT
154 case ISD::SETGT:
Rafael Espindola42b62f32006-10-13 13:14:59 +0000155 case ISD::SETOGT: return ARMCC::GT;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000156// SETOGE = (N | Z | !V) & !N = (Z | !V) & !N = !V & !N = GE
157 case ISD::SETGE:
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000158 case ISD::SETOGE: return ARMCC::GE;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000159// SETOLT = (N | Z | !V) & N = N = MI
160 case ISD::SETLT:
161 case ISD::SETOLT: return ARMCC::MI;
162// SETOLE = (N | Z | !V) & (N | Z) = N | Z = !C | Z = LS
163 case ISD::SETLE:
164 case ISD::SETOLE: return ARMCC::LS;
165// SETONE = (N | Z | !V) & !Z = (N | !V) & Z = !V & Z = Z = NE
166 case ISD::SETNE:
Rafael Espindola42b62f32006-10-13 13:14:59 +0000167 case ISD::SETONE: return ARMCC::NE;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000168// SETO = N | Z | !V = Z | !V = !V = VC
169 case ISD::SETO: return ARMCC::VC;
170// SETUO = V = VS
171 case ISD::SETUO: return ARMCC::VS;
172// SETUEQ = V | Z = ??
173// SETUGT = V | (!Z & !N) = !Z & !N = !Z & C = HI
174 case ISD::SETUGT: return ARMCC::HI;
175// SETUGE = V | !N = !N = PL
Rafael Espindola42b62f32006-10-13 13:14:59 +0000176 case ISD::SETUGE: return ARMCC::PL;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000177// SETULT = V | N = ??
178// SETULE = V | Z | N = ??
179// SETUNE = V | !Z = !Z = NE
Rafael Espindola42b62f32006-10-13 13:14:59 +0000180 case ISD::SETUNE: return ARMCC::NE;
181 }
182}
183
184/// DAGIntCCToARMCC - Convert a DAG integer condition code to an ARM CC
185static ARMCC::CondCodes DAGIntCCToARMCC(ISD::CondCode CC) {
186 switch (CC) {
187 default:
188 assert(0 && "Unknown integer condition code!");
189 case ISD::SETEQ: return ARMCC::EQ;
190 case ISD::SETNE: return ARMCC::NE;
191 case ISD::SETLT: return ARMCC::LT;
192 case ISD::SETLE: return ARMCC::LE;
193 case ISD::SETGT: return ARMCC::GT;
194 case ISD::SETGE: return ARMCC::GE;
Rafael Espindolabc4cec92006-09-03 13:19:16 +0000195 case ISD::SETULT: return ARMCC::CC;
Rafael Espindola42b62f32006-10-13 13:14:59 +0000196 case ISD::SETULE: return ARMCC::LS;
197 case ISD::SETUGT: return ARMCC::HI;
198 case ISD::SETUGE: return ARMCC::CS;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000199 }
200}
201
Rafael Espindola84b19be2006-07-16 01:02:57 +0000202const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
203 switch (Opcode) {
204 default: return 0;
205 case ARMISD::CALL: return "ARMISD::CALL";
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000206 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000207 case ARMISD::SELECT: return "ARMISD::SELECT";
208 case ARMISD::CMP: return "ARMISD::CMP";
Rafael Espindola687bc492006-08-24 13:45:55 +0000209 case ARMISD::BR: return "ARMISD::BR";
Rafael Espindola27185192006-09-29 21:20:16 +0000210 case ARMISD::FSITOS: return "ARMISD::FSITOS";
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000211 case ARMISD::FTOSIS: return "ARMISD::FTOSIS";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000212 case ARMISD::FSITOD: return "ARMISD::FSITOD";
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000213 case ARMISD::FTOSID: return "ARMISD::FTOSID";
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000214 case ARMISD::FUITOS: return "ARMISD::FUITOS";
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000215 case ARMISD::FTOUIS: return "ARMISD::FTOUIS";
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000216 case ARMISD::FUITOD: return "ARMISD::FUITOD";
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000217 case ARMISD::FTOUID: return "ARMISD::FTOUID";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000218 case ARMISD::FMRRD: return "ARMISD::FMRRD";
Rafael Espindolaa2845842006-10-05 16:48:49 +0000219 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000220 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Rafael Espindola84b19be2006-07-16 01:02:57 +0000221 }
222}
223
Rafael Espindolaa2845842006-10-05 16:48:49 +0000224class ArgumentLayout {
225 std::vector<bool> is_reg;
226 std::vector<unsigned> pos;
227 std::vector<MVT::ValueType> types;
228public:
Rafael Espindola39b5a212006-10-05 17:46:48 +0000229 ArgumentLayout(const std::vector<MVT::ValueType> &Types) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000230 types = Types;
231
232 unsigned RegNum = 0;
233 unsigned StackOffset = 0;
Rafael Espindola39b5a212006-10-05 17:46:48 +0000234 for(std::vector<MVT::ValueType>::const_iterator I = Types.begin();
Rafael Espindolaa2845842006-10-05 16:48:49 +0000235 I != Types.end();
236 ++I) {
237 MVT::ValueType VT = *I;
238 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
239 unsigned size = MVT::getSizeInBits(VT)/32;
240
241 RegNum = ((RegNum + size - 1) / size) * size;
242 if (RegNum < 4) {
243 pos.push_back(RegNum);
244 is_reg.push_back(true);
245 RegNum += size;
246 } else {
247 unsigned bytes = size * 32/8;
248 StackOffset = ((StackOffset + bytes - 1) / bytes) * bytes;
249 pos.push_back(StackOffset);
250 is_reg.push_back(false);
251 StackOffset += bytes;
252 }
253 }
254 }
255 unsigned getRegisterNum(unsigned argNum) {
256 assert(isRegister(argNum));
257 return pos[argNum];
258 }
259 unsigned getOffset(unsigned argNum) {
260 assert(isOffset(argNum));
261 return pos[argNum];
262 }
263 unsigned isRegister(unsigned argNum) {
264 assert(argNum < is_reg.size());
265 return is_reg[argNum];
266 }
267 unsigned isOffset(unsigned argNum) {
268 return !isRegister(argNum);
269 }
270 MVT::ValueType getType(unsigned argNum) {
271 assert(argNum < types.size());
272 return types[argNum];
273 }
274 unsigned getStackSize(void) {
275 int last = is_reg.size() - 1;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000276 if (last < 0)
277 return 0;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000278 if (isRegister(last))
279 return 0;
280 return getOffset(last) + MVT::getSizeInBits(getType(last))/8;
281 }
282 int lastRegArg(void) {
283 int size = is_reg.size();
284 int last = 0;
285 while(last < size && isRegister(last))
286 last++;
287 last--;
288 return last;
289 }
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000290 int lastRegNum(void) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000291 int l = lastRegArg();
292 if (l < 0)
293 return -1;
294 unsigned r = getRegisterNum(l);
295 MVT::ValueType t = getType(l);
296 assert(t == MVT::i32 || t == MVT::f32 || t == MVT::f64);
297 if (t == MVT::f64)
298 return r + 1;
299 return r;
300 }
301};
302
Rafael Espindola84b19be2006-07-16 01:02:57 +0000303// This transforms a ISD::CALL node into a
304// callseq_star <- ARMISD:CALL <- callseq_end
305// chain
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000306static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola84b19be2006-07-16 01:02:57 +0000307 SDOperand Chain = Op.getOperand(0);
308 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Rafael Espindola5f1b6982006-10-18 12:03:07 +0000309 assert((CallConv == CallingConv::C ||
310 CallConv == CallingConv::Fast)
311 && "unknown calling convention");
Rafael Espindola84b19be2006-07-16 01:02:57 +0000312 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000313 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000314 SDOperand Callee = Op.getOperand(4);
315 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Rafael Espindola1a009462006-08-08 13:02:29 +0000316 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000317 static const unsigned regs[] = {
Rafael Espindolafac00a92006-07-25 20:17:20 +0000318 ARM::R0, ARM::R1, ARM::R2, ARM::R3
319 };
320
Rafael Espindolaa2845842006-10-05 16:48:49 +0000321 std::vector<MVT::ValueType> Types;
322 for (unsigned i = 0; i < NumOps; ++i) {
323 MVT::ValueType VT = Op.getOperand(5+2*i).getValueType();
324 Types.push_back(VT);
325 }
326 ArgumentLayout Layout(Types);
Rafael Espindolafac00a92006-07-25 20:17:20 +0000327
Rafael Espindolaa2845842006-10-05 16:48:49 +0000328 unsigned NumBytes = Layout.getStackSize();
329
330 Chain = DAG.getCALLSEQ_START(Chain,
331 DAG.getConstant(NumBytes, MVT::i32));
332
333 //Build a sequence of stores
334 std::vector<SDOperand> MemOpChains;
335 for (unsigned i = Layout.lastRegArg() + 1; i < NumOps; ++i) {
336 SDOperand Arg = Op.getOperand(5+2*i);
337 unsigned ArgOffset = Layout.getOffset(i);
338 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
339 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Evan Cheng8b2794a2006-10-13 21:14:26 +0000340 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000341 }
Rafael Espindola1a009462006-08-08 13:02:29 +0000342 if (!MemOpChains.empty())
Chris Lattnere2199452006-08-11 17:38:39 +0000343 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
344 &MemOpChains[0], MemOpChains.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000345
Rafael Espindola0505be02006-10-16 21:10:32 +0000346 // If the callee is a GlobalAddress node (quite common, every direct call is)
347 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
348 // Likewise ExternalSymbol -> TargetExternalSymbol.
349 assert(Callee.getValueType() == MVT::i32);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000350 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Rafael Espindola0505be02006-10-16 21:10:32 +0000351 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
352 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
353 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000354
355 // If this is a direct call, pass the chain and the callee.
356 assert (Callee.Val);
357 std::vector<SDOperand> Ops;
358 Ops.push_back(Chain);
359 Ops.push_back(Callee);
360
Rafael Espindolaa2845842006-10-05 16:48:49 +0000361 // Build a sequence of copy-to-reg nodes chained together with token chain
362 // and flag operands which copy the outgoing args into the appropriate regs.
363 SDOperand InFlag;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000364 for (int i = 0, e = Layout.lastRegArg(); i <= e; ++i) {
Rafael Espindola4a408d42006-10-06 12:50:22 +0000365 SDOperand Arg = Op.getOperand(5+2*i);
366 unsigned RegNum = Layout.getRegisterNum(i);
367 unsigned Reg1 = regs[RegNum];
368 MVT::ValueType VT = Layout.getType(i);
369 assert(VT == Arg.getValueType());
370 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000371
372 // Add argument register to the end of the list so that it is known live
373 // into the call.
Rafael Espindola4a408d42006-10-06 12:50:22 +0000374 Ops.push_back(DAG.getRegister(Reg1, MVT::i32));
375 if (VT == MVT::f64) {
376 unsigned Reg2 = regs[RegNum + 1];
377 SDOperand SDReg1 = DAG.getRegister(Reg1, MVT::i32);
378 SDOperand SDReg2 = DAG.getRegister(Reg2, MVT::i32);
379
380 Ops.push_back(DAG.getRegister(Reg2, MVT::i32));
381 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola935b1f82006-10-06 20:33:26 +0000382 SDOperand Ops[] = {Chain, SDReg1, SDReg2, Arg, InFlag};
383 Chain = DAG.getNode(ARMISD::FMRRD, VTs, Ops, InFlag.Val ? 5 : 4);
Rafael Espindola4a408d42006-10-06 12:50:22 +0000384 } else {
385 if (VT == MVT::f32)
386 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg);
387 Chain = DAG.getCopyToReg(Chain, Reg1, Arg, InFlag);
388 }
389 InFlag = Chain.getValue(1);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000390 }
391
392 std::vector<MVT::ValueType> NodeTys;
393 NodeTys.push_back(MVT::Other); // Returns a chain
394 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Rafael Espindola7a53bd02006-08-09 16:41:12 +0000395
Rafael Espindola84b19be2006-07-16 01:02:57 +0000396 unsigned CallOpc = ARMISD::CALL;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000397 if (InFlag.Val)
398 Ops.push_back(InFlag);
Chris Lattner87428672006-08-11 17:22:35 +0000399 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000400 InFlag = Chain.getValue(1);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000401
Rafael Espindolafac00a92006-07-25 20:17:20 +0000402 std::vector<SDOperand> ResultVals;
403 NodeTys.clear();
404
405 // If the call has results, copy the values out of the ret val registers.
Rafael Espindola614057b2006-10-06 19:10:05 +0000406 MVT::ValueType VT = Op.Val->getValueType(0);
407 if (VT != MVT::Other) {
408 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
Rafael Espindola614057b2006-10-06 19:10:05 +0000409
410 SDOperand Value1 = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
411 Chain = Value1.getValue(1);
412 InFlag = Value1.getValue(2);
Rafael Espindola26a76d12006-10-13 16:47:22 +0000413 NodeTys.push_back(VT);
414 if (VT == MVT::i32) {
415 ResultVals.push_back(Value1);
416 if (Op.Val->getValueType(1) == MVT::i32) {
417 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
418 Chain = Value2.getValue(1);
419 ResultVals.push_back(Value2);
420 NodeTys.push_back(VT);
421 }
422 }
423 if (VT == MVT::f32) {
424 SDOperand Value = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Value1);
425 ResultVals.push_back(Value);
426 }
Rafael Espindola614057b2006-10-06 19:10:05 +0000427 if (VT == MVT::f64) {
428 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
429 Chain = Value2.getValue(1);
Rafael Espindola26a76d12006-10-13 16:47:22 +0000430 SDOperand Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
431 ResultVals.push_back(Value);
Rafael Espindola614057b2006-10-06 19:10:05 +0000432 }
Rafael Espindolafac00a92006-07-25 20:17:20 +0000433 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000434
435 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
436 DAG.getConstant(NumBytes, MVT::i32));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000437 NodeTys.push_back(MVT::Other);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000438
Rafael Espindolafac00a92006-07-25 20:17:20 +0000439 if (ResultVals.empty())
440 return Chain;
441
442 ResultVals.push_back(Chain);
Chris Lattner87428672006-08-11 17:22:35 +0000443 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
444 ResultVals.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000445 return Res.getValue(Op.ResNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000446}
447
448static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
449 SDOperand Copy;
Rafael Espindola4b023672006-06-05 22:26:14 +0000450 SDOperand Chain = Op.getOperand(0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000451 SDOperand R0 = DAG.getRegister(ARM::R0, MVT::i32);
452 SDOperand R1 = DAG.getRegister(ARM::R1, MVT::i32);
453
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000454 switch(Op.getNumOperands()) {
455 default:
456 assert(0 && "Do not know how to return this many arguments!");
457 abort();
Rafael Espindola4b023672006-06-05 22:26:14 +0000458 case 1: {
459 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
Rafael Espindola6312da02006-08-03 22:50:11 +0000460 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
Rafael Espindola4b023672006-06-05 22:26:14 +0000461 }
Rafael Espindola27185192006-09-29 21:20:16 +0000462 case 3: {
463 SDOperand Val = Op.getOperand(1);
464 assert(Val.getValueType() == MVT::i32 ||
Rafael Espindola9e071f02006-10-02 19:30:56 +0000465 Val.getValueType() == MVT::f32 ||
466 Val.getValueType() == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000467
Rafael Espindola9e071f02006-10-02 19:30:56 +0000468 if (Val.getValueType() == MVT::f64) {
469 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
470 SDOperand Ops[] = {Chain, R0, R1, Val};
471 Copy = DAG.getNode(ARMISD::FMRRD, VTs, Ops, 4);
472 } else {
473 if (Val.getValueType() == MVT::f32)
474 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
475 Copy = DAG.getCopyToReg(Chain, R0, Val, SDOperand());
476 }
477
478 if (DAG.getMachineFunction().liveout_empty()) {
Rafael Espindola4b023672006-06-05 22:26:14 +0000479 DAG.getMachineFunction().addLiveOut(ARM::R0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000480 if (Val.getValueType() == MVT::f64)
481 DAG.getMachineFunction().addLiveOut(ARM::R1);
482 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000483 break;
Rafael Espindola27185192006-09-29 21:20:16 +0000484 }
Rafael Espindola3a02f022006-09-04 19:05:01 +0000485 case 5:
486 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
487 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
488 // If we haven't noted the R0+R1 are live out, do so now.
489 if (DAG.getMachineFunction().liveout_empty()) {
490 DAG.getMachineFunction().addLiveOut(ARM::R0);
491 DAG.getMachineFunction().addLiveOut(ARM::R1);
492 }
493 break;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000494 }
Rafael Espindola4b023672006-06-05 22:26:14 +0000495
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000496 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
497 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000498}
499
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000500static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
501 MVT::ValueType PtrVT = Op.getValueType();
502 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengc356a572006-09-12 21:04:05 +0000503 Constant *C = CP->getConstVal();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000504 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
505
506 return CPI;
507}
508
509static SDOperand LowerGlobalAddress(SDOperand Op,
510 SelectionDAG &DAG) {
511 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Rafael Espindola61369da2006-08-14 19:01:24 +0000512 int alignment = 2;
513 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
Evan Cheng466685d2006-10-09 20:57:25 +0000514 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr, NULL, 0);
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000515}
516
Rafael Espindola755be9b2006-08-25 17:55:16 +0000517static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
518 unsigned VarArgsFrameIndex) {
519 // vastart just stores the address of the VarArgsFrameIndex slot into the
520 // memory location argument.
521 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
522 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +0000523 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
524 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), SV->getValue(),
525 SV->getOffset());
Rafael Espindola755be9b2006-08-25 17:55:16 +0000526}
527
528static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
529 int &VarArgsFrameIndex) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000530 MachineFunction &MF = DAG.getMachineFunction();
531 MachineFrameInfo *MFI = MF.getFrameInfo();
532 SSARegMap *RegMap = MF.getSSARegMap();
533 unsigned NumArgs = Op.Val->getNumValues()-1;
534 SDOperand Root = Op.getOperand(0);
535 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
536 static const unsigned REGS[] = {
537 ARM::R0, ARM::R1, ARM::R2, ARM::R3
538 };
539
540 std::vector<MVT::ValueType> Types(Op.Val->value_begin(), Op.Val->value_end() - 1);
541 ArgumentLayout Layout(Types);
542
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000543 std::vector<SDOperand> ArgValues;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000544 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000545 MVT::ValueType VT = Types[ArgNo];
Rafael Espindola4b442b52006-05-23 02:48:20 +0000546
Rafael Espindolaa2845842006-10-05 16:48:49 +0000547 SDOperand Value;
548 if (Layout.isRegister(ArgNo)) {
549 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
550 unsigned RegNum = Layout.getRegisterNum(ArgNo);
551 unsigned Reg1 = REGS[RegNum];
552 unsigned VReg1 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
553 SDOperand Value1 = DAG.getCopyFromReg(Root, VReg1, MVT::i32);
554 MF.addLiveIn(Reg1, VReg1);
555 if (VT == MVT::f64) {
556 unsigned Reg2 = REGS[RegNum + 1];
557 unsigned VReg2 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
558 SDOperand Value2 = DAG.getCopyFromReg(Root, VReg2, MVT::i32);
559 MF.addLiveIn(Reg2, VReg2);
560 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
561 } else {
562 Value = Value1;
563 if (VT == MVT::f32)
564 Value = DAG.getNode(ISD::BIT_CONVERT, VT, Value);
565 }
566 } else {
567 // If the argument is actually used, emit a load from the right stack
568 // slot.
569 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
570 unsigned Offset = Layout.getOffset(ArgNo);
571 unsigned Size = MVT::getSizeInBits(VT)/8;
572 int FI = MFI->CreateFixedObject(Size, Offset);
573 SDOperand FIN = DAG.getFrameIndex(FI, VT);
Evan Cheng466685d2006-10-09 20:57:25 +0000574 Value = DAG.getLoad(VT, Root, FIN, NULL, 0);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000575 } else {
576 Value = DAG.getNode(ISD::UNDEF, VT);
577 }
578 }
579 ArgValues.push_back(Value);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000580 }
581
Rafael Espindolaa2845842006-10-05 16:48:49 +0000582 unsigned NextRegNum = Layout.lastRegNum() + 1;
583
Rafael Espindola755be9b2006-08-25 17:55:16 +0000584 if (isVarArg) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000585 //If this function is vararg we must store the remaing
586 //registers so that they can be acessed with va_start
Rafael Espindola755be9b2006-08-25 17:55:16 +0000587 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000588 -16 + NextRegNum * 4);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000589
Rafael Espindola755be9b2006-08-25 17:55:16 +0000590 SmallVector<SDOperand, 4> MemOps;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000591 for (unsigned RegNo = NextRegNum; RegNo < 4; ++RegNo) {
592 int RegOffset = - (4 - RegNo) * 4;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000593 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000594 RegOffset);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000595 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
596
Rafael Espindolaa2845842006-10-05 16:48:49 +0000597 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
598 MF.addLiveIn(REGS[RegNo], VReg);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000599
600 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
Evan Cheng8b2794a2006-10-13 21:14:26 +0000601 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000602 MemOps.push_back(Store);
603 }
604 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
605 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000606
607 ArgValues.push_back(Root);
608
609 // Return the new list of results.
610 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
611 Op.Val->value_end());
Chris Lattner87428672006-08-11 17:22:35 +0000612 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
Rafael Espindoladc124a22006-05-18 21:45:49 +0000613}
614
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000615static SDOperand GetCMP(ISD::CondCode CC, SDOperand LHS, SDOperand RHS,
616 SelectionDAG &DAG) {
617 MVT::ValueType vt = LHS.getValueType();
Rafael Espindola0d9fe762006-10-10 16:33:47 +0000618 assert(vt == MVT::i32 || vt == MVT::f32 || vt == MVT::f64);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000619
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000620 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
Rafael Espindola42b62f32006-10-13 13:14:59 +0000621
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000622 if (vt != MVT::i32)
623 Cmp = DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp);
624 return Cmp;
625}
626
Rafael Espindola42b62f32006-10-13 13:14:59 +0000627static SDOperand GetARMCC(ISD::CondCode CC, MVT::ValueType vt,
628 SelectionDAG &DAG) {
629 assert(vt == MVT::i32 || vt == MVT::f32 || vt == MVT::f64);
630 if (vt == MVT::i32)
631 return DAG.getConstant(DAGIntCCToARMCC(CC), MVT::i32);
632 else
633 return DAG.getConstant(DAGFPCCToARMCC(CC), MVT::i32);
634}
635
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000636static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
637 SDOperand LHS = Op.getOperand(0);
638 SDOperand RHS = Op.getOperand(1);
639 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
640 SDOperand TrueVal = Op.getOperand(2);
641 SDOperand FalseVal = Op.getOperand(3);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000642 SDOperand Cmp = GetCMP(CC, LHS, RHS, DAG);
Rafael Espindola42b62f32006-10-13 13:14:59 +0000643 SDOperand ARMCC = GetARMCC(CC, LHS.getValueType(), DAG);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000644 return DAG.getNode(ARMISD::SELECT, MVT::i32, TrueVal, FalseVal, ARMCC, Cmp);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000645}
646
Rafael Espindola687bc492006-08-24 13:45:55 +0000647static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
648 SDOperand Chain = Op.getOperand(0);
649 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
650 SDOperand LHS = Op.getOperand(2);
651 SDOperand RHS = Op.getOperand(3);
652 SDOperand Dest = Op.getOperand(4);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000653 SDOperand Cmp = GetCMP(CC, LHS, RHS, DAG);
Rafael Espindola42b62f32006-10-13 13:14:59 +0000654 SDOperand ARMCC = GetARMCC(CC, LHS.getValueType(), DAG);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000655 return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, ARMCC, Cmp);
Rafael Espindola687bc492006-08-24 13:45:55 +0000656}
657
Rafael Espindola27185192006-09-29 21:20:16 +0000658static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola9e071f02006-10-02 19:30:56 +0000659 SDOperand IntVal = Op.getOperand(0);
Rafael Espindola27185192006-09-29 21:20:16 +0000660 assert(IntVal.getValueType() == MVT::i32);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000661 MVT::ValueType vt = Op.getValueType();
662 assert(vt == MVT::f32 ||
663 vt == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000664
665 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000666 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FSITOS : ARMISD::FSITOD;
667 return DAG.getNode(op, vt, Tmp);
Rafael Espindola27185192006-09-29 21:20:16 +0000668}
669
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000670static SDOperand LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
671 assert(Op.getValueType() == MVT::i32);
672 SDOperand FloatVal = Op.getOperand(0);
673 MVT::ValueType vt = FloatVal.getValueType();
674 assert(vt == MVT::f32 || vt == MVT::f64);
675
676 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FTOSIS : ARMISD::FTOSID;
677 SDOperand Tmp = DAG.getNode(op, MVT::f32, FloatVal);
678 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Tmp);
679}
680
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000681static SDOperand LowerUINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
682 SDOperand IntVal = Op.getOperand(0);
683 assert(IntVal.getValueType() == MVT::i32);
684 MVT::ValueType vt = Op.getValueType();
685 assert(vt == MVT::f32 ||
686 vt == MVT::f64);
687
688 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
689 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FUITOS : ARMISD::FUITOD;
690 return DAG.getNode(op, vt, Tmp);
691}
692
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000693static SDOperand LowerFP_TO_UINT(SDOperand Op, SelectionDAG &DAG) {
694 assert(Op.getValueType() == MVT::i32);
695 SDOperand FloatVal = Op.getOperand(0);
696 MVT::ValueType vt = FloatVal.getValueType();
697 assert(vt == MVT::f32 || vt == MVT::f64);
698
699 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FTOUIS : ARMISD::FTOUID;
700 SDOperand Tmp = DAG.getNode(op, MVT::f32, FloatVal);
701 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Tmp);
702}
703
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000704SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
705 switch (Op.getOpcode()) {
706 default:
707 assert(0 && "Should not custom lower this!");
Rafael Espindola1c8f0532006-05-15 22:34:39 +0000708 abort();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000709 case ISD::ConstantPool:
710 return LowerConstantPool(Op, DAG);
711 case ISD::GlobalAddress:
712 return LowerGlobalAddress(Op, DAG);
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000713 case ISD::FP_TO_SINT:
714 return LowerFP_TO_SINT(Op, DAG);
Rafael Espindola27185192006-09-29 21:20:16 +0000715 case ISD::SINT_TO_FP:
716 return LowerSINT_TO_FP(Op, DAG);
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000717 case ISD::FP_TO_UINT:
718 return LowerFP_TO_UINT(Op, DAG);
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000719 case ISD::UINT_TO_FP:
720 return LowerUINT_TO_FP(Op, DAG);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000721 case ISD::FORMAL_ARGUMENTS:
Rafael Espindola755be9b2006-08-25 17:55:16 +0000722 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000723 case ISD::CALL:
724 return LowerCALL(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000725 case ISD::RET:
726 return LowerRET(Op, DAG);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000727 case ISD::SELECT_CC:
728 return LowerSELECT_CC(Op, DAG);
Rafael Espindola687bc492006-08-24 13:45:55 +0000729 case ISD::BR_CC:
730 return LowerBR_CC(Op, DAG);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000731 case ISD::VASTART:
732 return LowerVASTART(Op, DAG, VarArgsFrameIndex);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000733 }
734}
735
736//===----------------------------------------------------------------------===//
737// Instruction Selector Implementation
738//===----------------------------------------------------------------------===//
739
740//===--------------------------------------------------------------------===//
741/// ARMDAGToDAGISel - ARM specific code to select ARM machine
742/// instructions for SelectionDAG operations.
743///
744namespace {
745class ARMDAGToDAGISel : public SelectionDAGISel {
746 ARMTargetLowering Lowering;
747
748public:
749 ARMDAGToDAGISel(TargetMachine &TM)
750 : SelectionDAGISel(Lowering), Lowering(TM) {
751 }
752
Evan Cheng9ade2182006-08-26 05:34:46 +0000753 SDNode *Select(SDOperand Op);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000754 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000755 bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000756 bool SelectAddrMode1(SDOperand N, SDOperand &Arg, SDOperand &Shift,
757 SDOperand &ShiftType);
Rafael Espindola32bd5f42006-10-17 18:04:53 +0000758 bool SelectAddrMode5(SDOperand N, SDOperand &Arg, SDOperand &Offset);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000759
760 // Include the pieces autogenerated from the target description.
761#include "ARMGenDAGISel.inc"
762};
763
764void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
765 DEBUG(BB->dump());
766
767 DAG.setRoot(SelectRoot(DAG.getRoot()));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000768 DAG.RemoveDeadNodes();
769
770 ScheduleAndEmitDAG(DAG);
771}
772
Rafael Espindola61369da2006-08-14 19:01:24 +0000773static bool isInt12Immediate(SDNode *N, short &Imm) {
774 if (N->getOpcode() != ISD::Constant)
775 return false;
776
777 int32_t t = cast<ConstantSDNode>(N)->getValue();
Rafael Espindola7246d332006-09-21 11:29:52 +0000778 int max = 1<<12;
Rafael Espindola61369da2006-08-14 19:01:24 +0000779 int min = -max;
780 if (t > min && t < max) {
781 Imm = t;
782 return true;
783 }
784 else
785 return false;
786}
787
788static bool isInt12Immediate(SDOperand Op, short &Imm) {
789 return isInt12Immediate(Op.Val, Imm);
790}
791
Rafael Espindola7246d332006-09-21 11:29:52 +0000792static uint32_t rotateL(uint32_t x) {
793 uint32_t bit31 = (x & (1 << 31)) >> 31;
794 uint32_t t = x << 1;
795 return t | bit31;
796}
797
798static bool isUInt8Immediate(uint32_t x) {
799 return x < (1 << 8);
800}
801
802static bool isRotInt8Immediate(uint32_t x) {
803 int r;
804 for (r = 0; r < 16; r++) {
805 if (isUInt8Immediate(x))
806 return true;
807 x = rotateL(rotateL(x));
808 }
809 return false;
810}
811
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000812bool ARMDAGToDAGISel::SelectAddrMode1(SDOperand N,
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000813 SDOperand &Arg,
814 SDOperand &Shift,
815 SDOperand &ShiftType) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000816 switch(N.getOpcode()) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000817 case ISD::Constant: {
Rafael Espindola7246d332006-09-21 11:29:52 +0000818 uint32_t val = cast<ConstantSDNode>(N)->getValue();
819 if(!isRotInt8Immediate(val)) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000820 Constant *C = ConstantInt::get(Type::UIntTy, val);
Rafael Espindola7246d332006-09-21 11:29:52 +0000821 int alignment = 2;
822 SDOperand Addr = CurDAG->getTargetConstantPool(C, MVT::i32, alignment);
823 SDOperand Z = CurDAG->getTargetConstant(0, MVT::i32);
824 SDNode *n = CurDAG->getTargetNode(ARM::ldr, MVT::i32, Z, Addr);
825 Arg = SDOperand(n, 0);
826 } else
827 Arg = CurDAG->getTargetConstant(val, MVT::i32);
828
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000829 Shift = CurDAG->getTargetConstant(0, MVT::i32);
830 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000831 return true;
832 }
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000833 case ISD::SRA:
834 Arg = N.getOperand(0);
835 Shift = N.getOperand(1);
836 ShiftType = CurDAG->getTargetConstant(ARMShift::ASR, MVT::i32);
837 return true;
838 case ISD::SRL:
839 Arg = N.getOperand(0);
840 Shift = N.getOperand(1);
841 ShiftType = CurDAG->getTargetConstant(ARMShift::LSR, MVT::i32);
842 return true;
843 case ISD::SHL:
844 Arg = N.getOperand(0);
845 Shift = N.getOperand(1);
846 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
847 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000848 }
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000849
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000850 Arg = N;
851 Shift = CurDAG->getTargetConstant(0, MVT::i32);
852 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000853 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000854}
855
Rafael Espindola32bd5f42006-10-17 18:04:53 +0000856bool ARMDAGToDAGISel::SelectAddrMode5(SDOperand N, SDOperand &Arg,
857 SDOperand &Offset) {
858 //TODO: detect offset
859 Offset = CurDAG->getTargetConstant(0, MVT::i32);
860 Arg = N;
861 return true;
862}
863
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000864//register plus/minus 12 bit offset
865bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
866 SDOperand &Base) {
Rafael Espindolaf3a335c2006-08-17 17:09:40 +0000867 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
868 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
869 Offset = CurDAG->getTargetConstant(0, MVT::i32);
870 return true;
871 }
Rafael Espindola61369da2006-08-14 19:01:24 +0000872 if (N.getOpcode() == ISD::ADD) {
873 short imm = 0;
874 if (isInt12Immediate(N.getOperand(1), imm)) {
875 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
876 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
877 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
878 } else {
879 Base = N.getOperand(0);
880 }
881 return true; // [r+i]
882 }
883 }
884
Rafael Espindolaa4e64352006-07-11 11:36:48 +0000885 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Rafael Espindolaaefe1422006-07-10 01:41:35 +0000886 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
887 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
888 }
889 else
890 Base = N;
891 return true; //any address fits in a register
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000892}
893
Evan Cheng9ade2182006-08-26 05:34:46 +0000894SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000895 SDNode *N = Op.Val;
896
897 switch (N->getOpcode()) {
898 default:
Evan Cheng9ade2182006-08-26 05:34:46 +0000899 return SelectCode(Op);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000900 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000901 }
Evan Cheng64a752f2006-08-11 09:08:15 +0000902 return NULL;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000903}
904
905} // end anonymous namespace
906
907/// createARMISelDag - This pass converts a legalized DAG into a
908/// ARM-specific DAG, ready for instruction scheduling.
909///
910FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
911 return new ARMDAGToDAGISel(TM);
912}