blob: cf02b8b3e4a0cf2be61c9598a4e7fa20bfedb59c [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"
Rafael Espindola462af9a2006-12-05 17:37:31 +000021#include "llvm/ADT/VectorExtras.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
27#include "llvm/CodeGen/SSARegMap.h"
28#include "llvm/Target/TargetLowering.h"
29#include "llvm/Support/Debug.h"
30#include <iostream>
Rafael Espindolaa2845842006-10-05 16:48:49 +000031#include <vector>
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000032using namespace llvm;
33
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000034namespace {
35 class ARMTargetLowering : public TargetLowering {
Rafael Espindola755be9b2006-08-25 17:55:16 +000036 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000037 public:
38 ARMTargetLowering(TargetMachine &TM);
39 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
Rafael Espindola84b19be2006-07-16 01:02:57 +000040 virtual const char *getTargetNodeName(unsigned Opcode) const;
Rafael Espindola462af9a2006-12-05 17:37:31 +000041 std::vector<unsigned>
42 getRegClassForInlineAsmConstraint(const std::string &Constraint,
43 MVT::ValueType VT) const;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000044 };
45
46}
47
48ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
49 : TargetLowering(TM) {
Rafael Espindola3717ca92006-08-20 01:49:49 +000050 addRegisterClass(MVT::i32, ARM::IntRegsRegisterClass);
Rafael Espindola27185192006-09-29 21:20:16 +000051 addRegisterClass(MVT::f32, ARM::FPRegsRegisterClass);
52 addRegisterClass(MVT::f64, ARM::DFPRegsRegisterClass);
Rafael Espindola3717ca92006-08-20 01:49:49 +000053
Rafael Espindolaad557f92006-10-09 14:13:40 +000054 setLoadXAction(ISD::EXTLOAD, MVT::f32, Expand);
55
Rafael Espindolab47e1d02006-10-10 18:55:14 +000056 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Rafael Espindola27185192006-09-29 21:20:16 +000057 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Rafael Espindola3717ca92006-08-20 01:49:49 +000058
Rafael Espindola493a7fc2006-10-10 20:38:57 +000059 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +000060 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
61
Rafael Espindola06c1e7e2006-08-01 12:58:43 +000062 setOperationAction(ISD::RET, MVT::Other, Custom);
63 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
64 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Rafael Espindola341b8642006-08-04 12:48:42 +000065
Rafael Espindola6495bdd2006-10-19 12:06:50 +000066 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
67 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
68 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
69
Rafael Espindola48bc9fb2006-10-09 16:28:33 +000070 setOperationAction(ISD::SELECT, MVT::i32, Expand);
71
Rafael Espindola3c000bf2006-08-21 22:00:32 +000072 setOperationAction(ISD::SETCC, MVT::i32, Expand);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +000073 setOperationAction(ISD::SETCC, MVT::f32, Expand);
74 setOperationAction(ISD::SETCC, MVT::f64, Expand);
75
Rafael Espindola3c000bf2006-08-21 22:00:32 +000076 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
Rafael Espindolad8ed7f82006-10-23 20:08:22 +000077
Rafael Espindola97815c62006-12-05 17:57:23 +000078 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
Rafael Espindolad8ed7f82006-10-23 20:08:22 +000079 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
Rafael Espindola97815c62006-12-05 17:57:23 +000080 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
Rafael Espindolad8ed7f82006-10-23 20:08:22 +000081
Evan Chengc35497f2006-10-30 08:02:39 +000082 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
83 setOperationAction(ISD::BRIND, MVT::Other, Expand);
Rafael Espindola687bc492006-08-24 13:45:55 +000084 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +000085 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
86 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Rafael Espindola3c000bf2006-08-21 22:00:32 +000087
Rafael Espindolad2b56682006-10-14 17:59:54 +000088 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
89
Rafael Espindola0505be02006-10-16 21:10:32 +000090 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
91 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
92 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Rafael Espindola226f8bc2006-10-17 21:05:33 +000093 setOperationAction(ISD::SDIV, MVT::i32, Expand);
94 setOperationAction(ISD::UDIV, MVT::i32, Expand);
95 setOperationAction(ISD::SREM, MVT::i32, Expand);
96 setOperationAction(ISD::UREM, MVT::i32, Expand);
Rafael Espindola0505be02006-10-16 21:10:32 +000097
Rafael Espindola755be9b2006-08-25 17:55:16 +000098 setOperationAction(ISD::VASTART, MVT::Other, Custom);
Rafael Espindola0e5e3aa2006-10-24 20:15:21 +000099 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000100 setOperationAction(ISD::VAEND, MVT::Other, Expand);
Rafael Espindola7ae68ab2006-10-26 13:31:26 +0000101 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000102
Rafael Espindolacd71da52006-10-03 17:27:58 +0000103 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
104 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
105
Rafael Espindola7ae68ab2006-10-26 13:31:26 +0000106 setStackPointerRegisterToSaveRestore(ARM::R13);
107
Rafael Espindola341b8642006-08-04 12:48:42 +0000108 setSchedulingPreference(SchedulingForRegPressure);
Rafael Espindola3717ca92006-08-20 01:49:49 +0000109 computeRegisterProperties();
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000110}
111
Rafael Espindola84b19be2006-07-16 01:02:57 +0000112namespace llvm {
113 namespace ARMISD {
114 enum NodeType {
115 // Start the numbering where the builting ops and target ops leave off.
116 FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
117 /// CALL - A direct function call.
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000118 CALL,
119
120 /// Return with a flag operand.
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000121 RET_FLAG,
122
123 CMP,
124
Rafael Espindola687bc492006-08-24 13:45:55 +0000125 SELECT,
126
Rafael Espindola27185192006-09-29 21:20:16 +0000127 BR,
128
Rafael Espindola9e071f02006-10-02 19:30:56 +0000129 FSITOS,
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000130 FTOSIS,
Rafael Espindola9e071f02006-10-02 19:30:56 +0000131
132 FSITOD,
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000133 FTOSID,
Rafael Espindola9e071f02006-10-02 19:30:56 +0000134
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000135 FUITOS,
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000136 FTOUIS,
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000137
138 FUITOD,
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000139 FTOUID,
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000140
Rafael Espindolaa2845842006-10-05 16:48:49 +0000141 FMRRD,
142
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000143 FMDRR,
144
145 FMSTAT
Rafael Espindola84b19be2006-07-16 01:02:57 +0000146 };
147 }
148}
149
Rafael Espindola42b62f32006-10-13 13:14:59 +0000150/// DAGFPCCToARMCC - Convert a DAG fp condition code to an ARM CC
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000151// Unordered = !N & !Z & C & V = V
152// Ordered = N | Z | !C | !V = N | Z | !V
Rafael Espindola42b62f32006-10-13 13:14:59 +0000153static ARMCC::CondCodes DAGFPCCToARMCC(ISD::CondCode CC) {
Rafael Espindola6f602de2006-08-24 16:13:15 +0000154 switch (CC) {
Rafael Espindolaebdabda2006-09-21 13:06:26 +0000155 default:
Rafael Espindola42b62f32006-10-13 13:14:59 +0000156 assert(0 && "Unknown fp condition code!");
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000157// SETOEQ = (N | Z | !V) & Z = Z = EQ
158 case ISD::SETEQ:
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000159 case ISD::SETOEQ: return ARMCC::EQ;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000160// SETOGT = (N | Z | !V) & !N & !Z = !V &!N &!Z = (N = V) & !Z = GT
161 case ISD::SETGT:
Rafael Espindola42b62f32006-10-13 13:14:59 +0000162 case ISD::SETOGT: return ARMCC::GT;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000163// SETOGE = (N | Z | !V) & !N = (Z | !V) & !N = !V & !N = GE
164 case ISD::SETGE:
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000165 case ISD::SETOGE: return ARMCC::GE;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000166// SETOLT = (N | Z | !V) & N = N = MI
167 case ISD::SETLT:
168 case ISD::SETOLT: return ARMCC::MI;
169// SETOLE = (N | Z | !V) & (N | Z) = N | Z = !C | Z = LS
170 case ISD::SETLE:
171 case ISD::SETOLE: return ARMCC::LS;
172// SETONE = (N | Z | !V) & !Z = (N | !V) & Z = !V & Z = Z = NE
173 case ISD::SETNE:
Rafael Espindola42b62f32006-10-13 13:14:59 +0000174 case ISD::SETONE: return ARMCC::NE;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000175// SETO = N | Z | !V = Z | !V = !V = VC
176 case ISD::SETO: return ARMCC::VC;
177// SETUO = V = VS
178 case ISD::SETUO: return ARMCC::VS;
179// SETUEQ = V | Z = ??
180// SETUGT = V | (!Z & !N) = !Z & !N = !Z & C = HI
181 case ISD::SETUGT: return ARMCC::HI;
182// SETUGE = V | !N = !N = PL
Rafael Espindola42b62f32006-10-13 13:14:59 +0000183 case ISD::SETUGE: return ARMCC::PL;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000184// SETULT = V | N = ??
185// SETULE = V | Z | N = ??
186// SETUNE = V | !Z = !Z = NE
Rafael Espindola42b62f32006-10-13 13:14:59 +0000187 case ISD::SETUNE: return ARMCC::NE;
188 }
189}
190
191/// DAGIntCCToARMCC - Convert a DAG integer condition code to an ARM CC
192static ARMCC::CondCodes DAGIntCCToARMCC(ISD::CondCode CC) {
193 switch (CC) {
194 default:
195 assert(0 && "Unknown integer condition code!");
196 case ISD::SETEQ: return ARMCC::EQ;
197 case ISD::SETNE: return ARMCC::NE;
198 case ISD::SETLT: return ARMCC::LT;
199 case ISD::SETLE: return ARMCC::LE;
200 case ISD::SETGT: return ARMCC::GT;
201 case ISD::SETGE: return ARMCC::GE;
Rafael Espindolabc4cec92006-09-03 13:19:16 +0000202 case ISD::SETULT: return ARMCC::CC;
Rafael Espindola42b62f32006-10-13 13:14:59 +0000203 case ISD::SETULE: return ARMCC::LS;
204 case ISD::SETUGT: return ARMCC::HI;
205 case ISD::SETUGE: return ARMCC::CS;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000206 }
207}
208
Rafael Espindola462af9a2006-12-05 17:37:31 +0000209std::vector<unsigned> ARMTargetLowering::
210getRegClassForInlineAsmConstraint(const std::string &Constraint,
211 MVT::ValueType VT) const {
212 if (Constraint.size() == 1) {
213 // FIXME: handling only r regs
214 switch (Constraint[0]) {
215 default: break; // Unknown constraint letter
216
217 case 'r': // GENERAL_REGS
218 case 'R': // LEGACY_REGS
219 if (VT == MVT::i32)
220 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
221 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
222 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
223 ARM::R12, ARM::R13, ARM::R14, 0);
224 break;
225
226 }
227 }
228
229 return std::vector<unsigned>();
230}
231
Rafael Espindola84b19be2006-07-16 01:02:57 +0000232const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
233 switch (Opcode) {
234 default: return 0;
235 case ARMISD::CALL: return "ARMISD::CALL";
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000236 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000237 case ARMISD::SELECT: return "ARMISD::SELECT";
238 case ARMISD::CMP: return "ARMISD::CMP";
Rafael Espindola687bc492006-08-24 13:45:55 +0000239 case ARMISD::BR: return "ARMISD::BR";
Rafael Espindola27185192006-09-29 21:20:16 +0000240 case ARMISD::FSITOS: return "ARMISD::FSITOS";
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000241 case ARMISD::FTOSIS: return "ARMISD::FTOSIS";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000242 case ARMISD::FSITOD: return "ARMISD::FSITOD";
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000243 case ARMISD::FTOSID: return "ARMISD::FTOSID";
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000244 case ARMISD::FUITOS: return "ARMISD::FUITOS";
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000245 case ARMISD::FTOUIS: return "ARMISD::FTOUIS";
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000246 case ARMISD::FUITOD: return "ARMISD::FUITOD";
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000247 case ARMISD::FTOUID: return "ARMISD::FTOUID";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000248 case ARMISD::FMRRD: return "ARMISD::FMRRD";
Rafael Espindolaa2845842006-10-05 16:48:49 +0000249 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000250 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Rafael Espindola84b19be2006-07-16 01:02:57 +0000251 }
252}
253
Rafael Espindolaa2845842006-10-05 16:48:49 +0000254class ArgumentLayout {
255 std::vector<bool> is_reg;
256 std::vector<unsigned> pos;
257 std::vector<MVT::ValueType> types;
258public:
Rafael Espindola39b5a212006-10-05 17:46:48 +0000259 ArgumentLayout(const std::vector<MVT::ValueType> &Types) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000260 types = Types;
261
262 unsigned RegNum = 0;
263 unsigned StackOffset = 0;
Rafael Espindola39b5a212006-10-05 17:46:48 +0000264 for(std::vector<MVT::ValueType>::const_iterator I = Types.begin();
Rafael Espindolaa2845842006-10-05 16:48:49 +0000265 I != Types.end();
266 ++I) {
267 MVT::ValueType VT = *I;
268 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
269 unsigned size = MVT::getSizeInBits(VT)/32;
270
271 RegNum = ((RegNum + size - 1) / size) * size;
272 if (RegNum < 4) {
273 pos.push_back(RegNum);
274 is_reg.push_back(true);
275 RegNum += size;
276 } else {
277 unsigned bytes = size * 32/8;
278 StackOffset = ((StackOffset + bytes - 1) / bytes) * bytes;
279 pos.push_back(StackOffset);
280 is_reg.push_back(false);
281 StackOffset += bytes;
282 }
283 }
284 }
285 unsigned getRegisterNum(unsigned argNum) {
286 assert(isRegister(argNum));
287 return pos[argNum];
288 }
289 unsigned getOffset(unsigned argNum) {
290 assert(isOffset(argNum));
291 return pos[argNum];
292 }
293 unsigned isRegister(unsigned argNum) {
294 assert(argNum < is_reg.size());
295 return is_reg[argNum];
296 }
297 unsigned isOffset(unsigned argNum) {
298 return !isRegister(argNum);
299 }
300 MVT::ValueType getType(unsigned argNum) {
301 assert(argNum < types.size());
302 return types[argNum];
303 }
304 unsigned getStackSize(void) {
305 int last = is_reg.size() - 1;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000306 if (last < 0)
307 return 0;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000308 if (isRegister(last))
309 return 0;
310 return getOffset(last) + MVT::getSizeInBits(getType(last))/8;
311 }
312 int lastRegArg(void) {
313 int size = is_reg.size();
314 int last = 0;
315 while(last < size && isRegister(last))
316 last++;
317 last--;
318 return last;
319 }
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000320 int lastRegNum(void) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000321 int l = lastRegArg();
322 if (l < 0)
323 return -1;
324 unsigned r = getRegisterNum(l);
325 MVT::ValueType t = getType(l);
326 assert(t == MVT::i32 || t == MVT::f32 || t == MVT::f64);
327 if (t == MVT::f64)
328 return r + 1;
329 return r;
330 }
331};
332
Rafael Espindola84b19be2006-07-16 01:02:57 +0000333// This transforms a ISD::CALL node into a
334// callseq_star <- ARMISD:CALL <- callseq_end
335// chain
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000336static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola84b19be2006-07-16 01:02:57 +0000337 SDOperand Chain = Op.getOperand(0);
338 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Rafael Espindola5f1b6982006-10-18 12:03:07 +0000339 assert((CallConv == CallingConv::C ||
340 CallConv == CallingConv::Fast)
341 && "unknown calling convention");
Rafael Espindola84b19be2006-07-16 01:02:57 +0000342 SDOperand Callee = Op.getOperand(4);
343 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Rafael Espindola1a009462006-08-08 13:02:29 +0000344 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000345 static const unsigned regs[] = {
Rafael Espindolafac00a92006-07-25 20:17:20 +0000346 ARM::R0, ARM::R1, ARM::R2, ARM::R3
347 };
348
Rafael Espindolaa2845842006-10-05 16:48:49 +0000349 std::vector<MVT::ValueType> Types;
350 for (unsigned i = 0; i < NumOps; ++i) {
351 MVT::ValueType VT = Op.getOperand(5+2*i).getValueType();
352 Types.push_back(VT);
353 }
354 ArgumentLayout Layout(Types);
Rafael Espindolafac00a92006-07-25 20:17:20 +0000355
Rafael Espindolaa2845842006-10-05 16:48:49 +0000356 unsigned NumBytes = Layout.getStackSize();
357
358 Chain = DAG.getCALLSEQ_START(Chain,
359 DAG.getConstant(NumBytes, MVT::i32));
360
361 //Build a sequence of stores
362 std::vector<SDOperand> MemOpChains;
363 for (unsigned i = Layout.lastRegArg() + 1; i < NumOps; ++i) {
364 SDOperand Arg = Op.getOperand(5+2*i);
365 unsigned ArgOffset = Layout.getOffset(i);
366 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
367 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Evan Cheng8b2794a2006-10-13 21:14:26 +0000368 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000369 }
Rafael Espindola1a009462006-08-08 13:02:29 +0000370 if (!MemOpChains.empty())
Chris Lattnere2199452006-08-11 17:38:39 +0000371 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
372 &MemOpChains[0], MemOpChains.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000373
Rafael Espindola0505be02006-10-16 21:10:32 +0000374 // If the callee is a GlobalAddress node (quite common, every direct call is)
375 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
376 // Likewise ExternalSymbol -> TargetExternalSymbol.
377 assert(Callee.getValueType() == MVT::i32);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000378 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Rafael Espindola0505be02006-10-16 21:10:32 +0000379 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
380 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
381 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000382
383 // If this is a direct call, pass the chain and the callee.
384 assert (Callee.Val);
385 std::vector<SDOperand> Ops;
386 Ops.push_back(Chain);
387 Ops.push_back(Callee);
388
Rafael Espindolaa2845842006-10-05 16:48:49 +0000389 // Build a sequence of copy-to-reg nodes chained together with token chain
390 // and flag operands which copy the outgoing args into the appropriate regs.
391 SDOperand InFlag;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000392 for (int i = 0, e = Layout.lastRegArg(); i <= e; ++i) {
Rafael Espindola4a408d42006-10-06 12:50:22 +0000393 SDOperand Arg = Op.getOperand(5+2*i);
394 unsigned RegNum = Layout.getRegisterNum(i);
395 unsigned Reg1 = regs[RegNum];
396 MVT::ValueType VT = Layout.getType(i);
397 assert(VT == Arg.getValueType());
398 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000399
400 // Add argument register to the end of the list so that it is known live
401 // into the call.
Rafael Espindola4a408d42006-10-06 12:50:22 +0000402 Ops.push_back(DAG.getRegister(Reg1, MVT::i32));
403 if (VT == MVT::f64) {
404 unsigned Reg2 = regs[RegNum + 1];
405 SDOperand SDReg1 = DAG.getRegister(Reg1, MVT::i32);
406 SDOperand SDReg2 = DAG.getRegister(Reg2, MVT::i32);
407
408 Ops.push_back(DAG.getRegister(Reg2, MVT::i32));
409 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola935b1f82006-10-06 20:33:26 +0000410 SDOperand Ops[] = {Chain, SDReg1, SDReg2, Arg, InFlag};
411 Chain = DAG.getNode(ARMISD::FMRRD, VTs, Ops, InFlag.Val ? 5 : 4);
Rafael Espindola4a408d42006-10-06 12:50:22 +0000412 } else {
413 if (VT == MVT::f32)
414 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg);
415 Chain = DAG.getCopyToReg(Chain, Reg1, Arg, InFlag);
416 }
417 InFlag = Chain.getValue(1);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000418 }
419
420 std::vector<MVT::ValueType> NodeTys;
421 NodeTys.push_back(MVT::Other); // Returns a chain
422 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Rafael Espindola7a53bd02006-08-09 16:41:12 +0000423
Rafael Espindola84b19be2006-07-16 01:02:57 +0000424 unsigned CallOpc = ARMISD::CALL;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000425 if (InFlag.Val)
426 Ops.push_back(InFlag);
Chris Lattner87428672006-08-11 17:22:35 +0000427 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000428 InFlag = Chain.getValue(1);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000429
Rafael Espindolafac00a92006-07-25 20:17:20 +0000430 std::vector<SDOperand> ResultVals;
431 NodeTys.clear();
432
433 // If the call has results, copy the values out of the ret val registers.
Rafael Espindola614057b2006-10-06 19:10:05 +0000434 MVT::ValueType VT = Op.Val->getValueType(0);
435 if (VT != MVT::Other) {
436 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
Rafael Espindola614057b2006-10-06 19:10:05 +0000437
438 SDOperand Value1 = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
439 Chain = Value1.getValue(1);
440 InFlag = Value1.getValue(2);
Rafael Espindola26a76d12006-10-13 16:47:22 +0000441 NodeTys.push_back(VT);
442 if (VT == MVT::i32) {
443 ResultVals.push_back(Value1);
444 if (Op.Val->getValueType(1) == MVT::i32) {
445 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
446 Chain = Value2.getValue(1);
447 ResultVals.push_back(Value2);
448 NodeTys.push_back(VT);
449 }
450 }
451 if (VT == MVT::f32) {
452 SDOperand Value = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Value1);
453 ResultVals.push_back(Value);
454 }
Rafael Espindola614057b2006-10-06 19:10:05 +0000455 if (VT == MVT::f64) {
456 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
457 Chain = Value2.getValue(1);
Rafael Espindola26a76d12006-10-13 16:47:22 +0000458 SDOperand Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
459 ResultVals.push_back(Value);
Rafael Espindola614057b2006-10-06 19:10:05 +0000460 }
Rafael Espindolafac00a92006-07-25 20:17:20 +0000461 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000462
463 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
464 DAG.getConstant(NumBytes, MVT::i32));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000465 NodeTys.push_back(MVT::Other);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000466
Rafael Espindolafac00a92006-07-25 20:17:20 +0000467 if (ResultVals.empty())
468 return Chain;
469
470 ResultVals.push_back(Chain);
Chris Lattner87428672006-08-11 17:22:35 +0000471 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
472 ResultVals.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000473 return Res.getValue(Op.ResNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000474}
475
476static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
477 SDOperand Copy;
Rafael Espindola4b023672006-06-05 22:26:14 +0000478 SDOperand Chain = Op.getOperand(0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000479 SDOperand R0 = DAG.getRegister(ARM::R0, MVT::i32);
480 SDOperand R1 = DAG.getRegister(ARM::R1, MVT::i32);
481
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000482 switch(Op.getNumOperands()) {
483 default:
484 assert(0 && "Do not know how to return this many arguments!");
485 abort();
Rafael Espindola4b023672006-06-05 22:26:14 +0000486 case 1: {
487 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
Rafael Espindola6312da02006-08-03 22:50:11 +0000488 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
Rafael Espindola4b023672006-06-05 22:26:14 +0000489 }
Rafael Espindola27185192006-09-29 21:20:16 +0000490 case 3: {
491 SDOperand Val = Op.getOperand(1);
492 assert(Val.getValueType() == MVT::i32 ||
Rafael Espindola9e071f02006-10-02 19:30:56 +0000493 Val.getValueType() == MVT::f32 ||
494 Val.getValueType() == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000495
Rafael Espindola9e071f02006-10-02 19:30:56 +0000496 if (Val.getValueType() == MVT::f64) {
497 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
498 SDOperand Ops[] = {Chain, R0, R1, Val};
499 Copy = DAG.getNode(ARMISD::FMRRD, VTs, Ops, 4);
500 } else {
501 if (Val.getValueType() == MVT::f32)
502 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
503 Copy = DAG.getCopyToReg(Chain, R0, Val, SDOperand());
504 }
505
506 if (DAG.getMachineFunction().liveout_empty()) {
Rafael Espindola4b023672006-06-05 22:26:14 +0000507 DAG.getMachineFunction().addLiveOut(ARM::R0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000508 if (Val.getValueType() == MVT::f64)
509 DAG.getMachineFunction().addLiveOut(ARM::R1);
510 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000511 break;
Rafael Espindola27185192006-09-29 21:20:16 +0000512 }
Rafael Espindola3a02f022006-09-04 19:05:01 +0000513 case 5:
514 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
515 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
516 // If we haven't noted the R0+R1 are live out, do so now.
517 if (DAG.getMachineFunction().liveout_empty()) {
518 DAG.getMachineFunction().addLiveOut(ARM::R0);
519 DAG.getMachineFunction().addLiveOut(ARM::R1);
520 }
521 break;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000522 }
Rafael Espindola4b023672006-06-05 22:26:14 +0000523
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000524 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
525 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000526}
527
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000528static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
529 MVT::ValueType PtrVT = Op.getValueType();
530 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengc356a572006-09-12 21:04:05 +0000531 Constant *C = CP->getConstVal();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000532 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
533
534 return CPI;
535}
536
537static SDOperand LowerGlobalAddress(SDOperand Op,
538 SelectionDAG &DAG) {
539 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Rafael Espindola61369da2006-08-14 19:01:24 +0000540 int alignment = 2;
541 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
Evan Cheng466685d2006-10-09 20:57:25 +0000542 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr, NULL, 0);
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000543}
544
Rafael Espindola755be9b2006-08-25 17:55:16 +0000545static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
546 unsigned VarArgsFrameIndex) {
547 // vastart just stores the address of the VarArgsFrameIndex slot into the
548 // memory location argument.
549 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
550 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +0000551 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
552 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), SV->getValue(),
553 SV->getOffset());
Rafael Espindola755be9b2006-08-25 17:55:16 +0000554}
555
556static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
557 int &VarArgsFrameIndex) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000558 MachineFunction &MF = DAG.getMachineFunction();
559 MachineFrameInfo *MFI = MF.getFrameInfo();
560 SSARegMap *RegMap = MF.getSSARegMap();
561 unsigned NumArgs = Op.Val->getNumValues()-1;
562 SDOperand Root = Op.getOperand(0);
563 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
564 static const unsigned REGS[] = {
565 ARM::R0, ARM::R1, ARM::R2, ARM::R3
566 };
567
568 std::vector<MVT::ValueType> Types(Op.Val->value_begin(), Op.Val->value_end() - 1);
569 ArgumentLayout Layout(Types);
570
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000571 std::vector<SDOperand> ArgValues;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000572 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000573 MVT::ValueType VT = Types[ArgNo];
Rafael Espindola4b442b52006-05-23 02:48:20 +0000574
Rafael Espindolaa2845842006-10-05 16:48:49 +0000575 SDOperand Value;
576 if (Layout.isRegister(ArgNo)) {
577 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
578 unsigned RegNum = Layout.getRegisterNum(ArgNo);
579 unsigned Reg1 = REGS[RegNum];
580 unsigned VReg1 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
581 SDOperand Value1 = DAG.getCopyFromReg(Root, VReg1, MVT::i32);
582 MF.addLiveIn(Reg1, VReg1);
583 if (VT == MVT::f64) {
584 unsigned Reg2 = REGS[RegNum + 1];
585 unsigned VReg2 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
586 SDOperand Value2 = DAG.getCopyFromReg(Root, VReg2, MVT::i32);
587 MF.addLiveIn(Reg2, VReg2);
588 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
589 } else {
590 Value = Value1;
591 if (VT == MVT::f32)
592 Value = DAG.getNode(ISD::BIT_CONVERT, VT, Value);
593 }
594 } else {
595 // If the argument is actually used, emit a load from the right stack
596 // slot.
597 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
598 unsigned Offset = Layout.getOffset(ArgNo);
599 unsigned Size = MVT::getSizeInBits(VT)/8;
600 int FI = MFI->CreateFixedObject(Size, Offset);
601 SDOperand FIN = DAG.getFrameIndex(FI, VT);
Evan Cheng466685d2006-10-09 20:57:25 +0000602 Value = DAG.getLoad(VT, Root, FIN, NULL, 0);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000603 } else {
604 Value = DAG.getNode(ISD::UNDEF, VT);
605 }
606 }
607 ArgValues.push_back(Value);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000608 }
609
Rafael Espindolaa2845842006-10-05 16:48:49 +0000610 unsigned NextRegNum = Layout.lastRegNum() + 1;
611
Rafael Espindola755be9b2006-08-25 17:55:16 +0000612 if (isVarArg) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000613 //If this function is vararg we must store the remaing
614 //registers so that they can be acessed with va_start
Rafael Espindola755be9b2006-08-25 17:55:16 +0000615 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000616 -16 + NextRegNum * 4);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000617
Rafael Espindola755be9b2006-08-25 17:55:16 +0000618 SmallVector<SDOperand, 4> MemOps;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000619 for (unsigned RegNo = NextRegNum; RegNo < 4; ++RegNo) {
620 int RegOffset = - (4 - RegNo) * 4;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000621 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000622 RegOffset);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000623 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
624
Rafael Espindolaa2845842006-10-05 16:48:49 +0000625 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
626 MF.addLiveIn(REGS[RegNo], VReg);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000627
628 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
Evan Cheng8b2794a2006-10-13 21:14:26 +0000629 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000630 MemOps.push_back(Store);
631 }
632 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
633 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000634
635 ArgValues.push_back(Root);
636
637 // Return the new list of results.
638 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
639 Op.Val->value_end());
Chris Lattner87428672006-08-11 17:22:35 +0000640 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
Rafael Espindoladc124a22006-05-18 21:45:49 +0000641}
642
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000643static SDOperand GetCMP(ISD::CondCode CC, SDOperand LHS, SDOperand RHS,
644 SelectionDAG &DAG) {
645 MVT::ValueType vt = LHS.getValueType();
Rafael Espindola0d9fe762006-10-10 16:33:47 +0000646 assert(vt == MVT::i32 || vt == MVT::f32 || vt == MVT::f64);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000647
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000648 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
Rafael Espindola42b62f32006-10-13 13:14:59 +0000649
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000650 if (vt != MVT::i32)
651 Cmp = DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp);
652 return Cmp;
653}
654
Rafael Espindola42b62f32006-10-13 13:14:59 +0000655static SDOperand GetARMCC(ISD::CondCode CC, MVT::ValueType vt,
656 SelectionDAG &DAG) {
657 assert(vt == MVT::i32 || vt == MVT::f32 || vt == MVT::f64);
658 if (vt == MVT::i32)
659 return DAG.getConstant(DAGIntCCToARMCC(CC), MVT::i32);
660 else
661 return DAG.getConstant(DAGFPCCToARMCC(CC), MVT::i32);
662}
663
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000664static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
665 SDOperand LHS = Op.getOperand(0);
666 SDOperand RHS = Op.getOperand(1);
667 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
668 SDOperand TrueVal = Op.getOperand(2);
669 SDOperand FalseVal = Op.getOperand(3);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000670 SDOperand Cmp = GetCMP(CC, LHS, RHS, DAG);
Rafael Espindola42b62f32006-10-13 13:14:59 +0000671 SDOperand ARMCC = GetARMCC(CC, LHS.getValueType(), DAG);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000672 return DAG.getNode(ARMISD::SELECT, MVT::i32, TrueVal, FalseVal, ARMCC, Cmp);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000673}
674
Rafael Espindola687bc492006-08-24 13:45:55 +0000675static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
676 SDOperand Chain = Op.getOperand(0);
677 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
678 SDOperand LHS = Op.getOperand(2);
679 SDOperand RHS = Op.getOperand(3);
680 SDOperand Dest = Op.getOperand(4);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000681 SDOperand Cmp = GetCMP(CC, LHS, RHS, DAG);
Rafael Espindola42b62f32006-10-13 13:14:59 +0000682 SDOperand ARMCC = GetARMCC(CC, LHS.getValueType(), DAG);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000683 return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, ARMCC, Cmp);
Rafael Espindola687bc492006-08-24 13:45:55 +0000684}
685
Rafael Espindola27185192006-09-29 21:20:16 +0000686static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola9e071f02006-10-02 19:30:56 +0000687 SDOperand IntVal = Op.getOperand(0);
Rafael Espindola27185192006-09-29 21:20:16 +0000688 assert(IntVal.getValueType() == MVT::i32);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000689 MVT::ValueType vt = Op.getValueType();
690 assert(vt == MVT::f32 ||
691 vt == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000692
693 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000694 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FSITOS : ARMISD::FSITOD;
695 return DAG.getNode(op, vt, Tmp);
Rafael Espindola27185192006-09-29 21:20:16 +0000696}
697
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000698static SDOperand LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
699 assert(Op.getValueType() == MVT::i32);
700 SDOperand FloatVal = Op.getOperand(0);
701 MVT::ValueType vt = FloatVal.getValueType();
702 assert(vt == MVT::f32 || vt == MVT::f64);
703
704 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FTOSIS : ARMISD::FTOSID;
705 SDOperand Tmp = DAG.getNode(op, MVT::f32, FloatVal);
706 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Tmp);
707}
708
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000709static SDOperand LowerUINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
710 SDOperand IntVal = Op.getOperand(0);
711 assert(IntVal.getValueType() == MVT::i32);
712 MVT::ValueType vt = Op.getValueType();
713 assert(vt == MVT::f32 ||
714 vt == MVT::f64);
715
716 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
717 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FUITOS : ARMISD::FUITOD;
718 return DAG.getNode(op, vt, Tmp);
719}
720
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000721static SDOperand LowerFP_TO_UINT(SDOperand Op, SelectionDAG &DAG) {
722 assert(Op.getValueType() == MVT::i32);
723 SDOperand FloatVal = Op.getOperand(0);
724 MVT::ValueType vt = FloatVal.getValueType();
725 assert(vt == MVT::f32 || vt == MVT::f64);
726
727 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FTOUIS : ARMISD::FTOUID;
728 SDOperand Tmp = DAG.getNode(op, MVT::f32, FloatVal);
729 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Tmp);
730}
731
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000732SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
733 switch (Op.getOpcode()) {
734 default:
735 assert(0 && "Should not custom lower this!");
Rafael Espindola1c8f0532006-05-15 22:34:39 +0000736 abort();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000737 case ISD::ConstantPool:
738 return LowerConstantPool(Op, DAG);
739 case ISD::GlobalAddress:
740 return LowerGlobalAddress(Op, DAG);
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000741 case ISD::FP_TO_SINT:
742 return LowerFP_TO_SINT(Op, DAG);
Rafael Espindola27185192006-09-29 21:20:16 +0000743 case ISD::SINT_TO_FP:
744 return LowerSINT_TO_FP(Op, DAG);
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000745 case ISD::FP_TO_UINT:
746 return LowerFP_TO_UINT(Op, DAG);
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000747 case ISD::UINT_TO_FP:
748 return LowerUINT_TO_FP(Op, DAG);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000749 case ISD::FORMAL_ARGUMENTS:
Rafael Espindola755be9b2006-08-25 17:55:16 +0000750 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000751 case ISD::CALL:
752 return LowerCALL(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000753 case ISD::RET:
754 return LowerRET(Op, DAG);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000755 case ISD::SELECT_CC:
756 return LowerSELECT_CC(Op, DAG);
Rafael Espindola687bc492006-08-24 13:45:55 +0000757 case ISD::BR_CC:
758 return LowerBR_CC(Op, DAG);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000759 case ISD::VASTART:
760 return LowerVASTART(Op, DAG, VarArgsFrameIndex);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000761 }
762}
763
764//===----------------------------------------------------------------------===//
765// Instruction Selector Implementation
766//===----------------------------------------------------------------------===//
767
768//===--------------------------------------------------------------------===//
769/// ARMDAGToDAGISel - ARM specific code to select ARM machine
770/// instructions for SelectionDAG operations.
771///
772namespace {
773class ARMDAGToDAGISel : public SelectionDAGISel {
774 ARMTargetLowering Lowering;
775
776public:
777 ARMDAGToDAGISel(TargetMachine &TM)
778 : SelectionDAGISel(Lowering), Lowering(TM) {
779 }
780
Evan Cheng9ade2182006-08-26 05:34:46 +0000781 SDNode *Select(SDOperand Op);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000782 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Evan Cheng0d538262006-11-08 20:34:28 +0000783 bool SelectAddrMode1(SDOperand Op, SDOperand N, SDOperand &Arg,
784 SDOperand &Shift, SDOperand &ShiftType);
785 bool SelectAddrMode2(SDOperand Op, SDOperand N, SDOperand &Arg,
786 SDOperand &Offset);
787 bool SelectAddrMode5(SDOperand Op, SDOperand N, SDOperand &Arg,
788 SDOperand &Offset);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000789
790 // Include the pieces autogenerated from the target description.
791#include "ARMGenDAGISel.inc"
792};
793
794void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
795 DEBUG(BB->dump());
796
797 DAG.setRoot(SelectRoot(DAG.getRoot()));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000798 DAG.RemoveDeadNodes();
799
800 ScheduleAndEmitDAG(DAG);
801}
802
Rafael Espindola61369da2006-08-14 19:01:24 +0000803static bool isInt12Immediate(SDNode *N, short &Imm) {
804 if (N->getOpcode() != ISD::Constant)
805 return false;
806
807 int32_t t = cast<ConstantSDNode>(N)->getValue();
Rafael Espindola7246d332006-09-21 11:29:52 +0000808 int max = 1<<12;
Rafael Espindola61369da2006-08-14 19:01:24 +0000809 int min = -max;
810 if (t > min && t < max) {
811 Imm = t;
812 return true;
813 }
814 else
815 return false;
816}
817
818static bool isInt12Immediate(SDOperand Op, short &Imm) {
819 return isInt12Immediate(Op.Val, Imm);
820}
821
Rafael Espindola7246d332006-09-21 11:29:52 +0000822static uint32_t rotateL(uint32_t x) {
823 uint32_t bit31 = (x & (1 << 31)) >> 31;
824 uint32_t t = x << 1;
825 return t | bit31;
826}
827
828static bool isUInt8Immediate(uint32_t x) {
829 return x < (1 << 8);
830}
831
832static bool isRotInt8Immediate(uint32_t x) {
833 int r;
834 for (r = 0; r < 16; r++) {
835 if (isUInt8Immediate(x))
836 return true;
837 x = rotateL(rotateL(x));
838 }
839 return false;
840}
841
Evan Cheng0d538262006-11-08 20:34:28 +0000842bool ARMDAGToDAGISel::SelectAddrMode1(SDOperand Op,
843 SDOperand N,
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000844 SDOperand &Arg,
845 SDOperand &Shift,
846 SDOperand &ShiftType) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000847 switch(N.getOpcode()) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000848 case ISD::Constant: {
Rafael Espindola7246d332006-09-21 11:29:52 +0000849 uint32_t val = cast<ConstantSDNode>(N)->getValue();
850 if(!isRotInt8Immediate(val)) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000851 Constant *C = ConstantInt::get(Type::UIntTy, val);
Rafael Espindola7246d332006-09-21 11:29:52 +0000852 int alignment = 2;
853 SDOperand Addr = CurDAG->getTargetConstantPool(C, MVT::i32, alignment);
854 SDOperand Z = CurDAG->getTargetConstant(0, MVT::i32);
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000855 SDNode *n = CurDAG->getTargetNode(ARM::LDR, MVT::i32, Addr, Z);
Rafael Espindola7246d332006-09-21 11:29:52 +0000856 Arg = SDOperand(n, 0);
857 } else
858 Arg = CurDAG->getTargetConstant(val, MVT::i32);
859
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000860 Shift = CurDAG->getTargetConstant(0, MVT::i32);
861 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000862 return true;
863 }
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000864 case ISD::SRA:
865 Arg = N.getOperand(0);
866 Shift = N.getOperand(1);
867 ShiftType = CurDAG->getTargetConstant(ARMShift::ASR, MVT::i32);
868 return true;
869 case ISD::SRL:
870 Arg = N.getOperand(0);
871 Shift = N.getOperand(1);
872 ShiftType = CurDAG->getTargetConstant(ARMShift::LSR, MVT::i32);
873 return true;
874 case ISD::SHL:
875 Arg = N.getOperand(0);
876 Shift = N.getOperand(1);
877 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
878 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000879 }
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000880
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000881 Arg = N;
882 Shift = CurDAG->getTargetConstant(0, MVT::i32);
883 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000884 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000885}
886
Evan Cheng0d538262006-11-08 20:34:28 +0000887bool ARMDAGToDAGISel::SelectAddrMode2(SDOperand Op, SDOperand N,
888 SDOperand &Arg, SDOperand &Offset) {
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000889 //TODO: complete and cleanup!
890 SDOperand Zero = CurDAG->getTargetConstant(0, MVT::i32);
891 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
892 Arg = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
893 Offset = Zero;
894 return true;
895 }
896 if (N.getOpcode() == ISD::ADD) {
897 short imm = 0;
898 if (isInt12Immediate(N.getOperand(1), imm)) {
899 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
900 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
901 Arg = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
902 } else {
903 Arg = N.getOperand(0);
904 }
905 return true; // [r+i]
906 }
907 }
908 Offset = Zero;
909 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
910 Arg = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
911 else
912 Arg = N;
913 return true;
914}
915
Evan Cheng0d538262006-11-08 20:34:28 +0000916bool ARMDAGToDAGISel::SelectAddrMode5(SDOperand Op,
917 SDOperand N, SDOperand &Arg,
Rafael Espindola32bd5f42006-10-17 18:04:53 +0000918 SDOperand &Offset) {
919 //TODO: detect offset
920 Offset = CurDAG->getTargetConstant(0, MVT::i32);
921 Arg = N;
922 return true;
923}
924
Evan Cheng9ade2182006-08-26 05:34:46 +0000925SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000926 SDNode *N = Op.Val;
927
928 switch (N->getOpcode()) {
929 default:
Evan Cheng9ade2182006-08-26 05:34:46 +0000930 return SelectCode(Op);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000931 break;
Rafael Espindolaf819a492006-11-09 13:58:55 +0000932 case ISD::FrameIndex: {
933 int FI = cast<FrameIndexSDNode>(N)->getIndex();
934 SDOperand Ops[] = {CurDAG->getTargetFrameIndex(FI, MVT::i32),
935 CurDAG->getTargetConstant(0, MVT::i32),
936 CurDAG->getTargetConstant(0, MVT::i32),
937 CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32)};
938
939 return CurDAG->SelectNodeTo(N, ARM::ADD, MVT::i32, Ops,
940 sizeof(Ops)/sizeof(SDOperand));
941 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000942 }
Rafael Espindolaf819a492006-11-09 13:58:55 +0000943 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000944}
945
946} // end anonymous namespace
947
948/// createARMISelDag - This pass converts a legalized DAG into a
949/// ARM-specific DAG, ready for instruction scheduling.
950///
951FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
952 return new ARMDAGToDAGISel(TM);
953}