blob: 48d918c1d6c52185cf2d85a40b8b47d2fd12f76f [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"
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 Espindola462af9a2006-12-05 17:37:31 +000040 std::vector<unsigned>
41 getRegClassForInlineAsmConstraint(const std::string &Constraint,
42 MVT::ValueType VT) const;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000043 };
44
45}
46
47ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
48 : TargetLowering(TM) {
Rafael Espindola3717ca92006-08-20 01:49:49 +000049 addRegisterClass(MVT::i32, ARM::IntRegsRegisterClass);
Rafael Espindola27185192006-09-29 21:20:16 +000050 addRegisterClass(MVT::f32, ARM::FPRegsRegisterClass);
51 addRegisterClass(MVT::f64, ARM::DFPRegsRegisterClass);
Rafael Espindola3717ca92006-08-20 01:49:49 +000052
Rafael Espindolaad557f92006-10-09 14:13:40 +000053 setLoadXAction(ISD::EXTLOAD, MVT::f32, Expand);
54
Rafael Espindolab47e1d02006-10-10 18:55:14 +000055 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Rafael Espindola27185192006-09-29 21:20:16 +000056 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Rafael Espindola3717ca92006-08-20 01:49:49 +000057
Rafael Espindola493a7fc2006-10-10 20:38:57 +000058 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +000059 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
60
Rafael Espindola06c1e7e2006-08-01 12:58:43 +000061 setOperationAction(ISD::RET, MVT::Other, Custom);
62 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
63 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Rafael Espindola341b8642006-08-04 12:48:42 +000064
Rafael Espindola6495bdd2006-10-19 12:06:50 +000065 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
66 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
67 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
68
Rafael Espindola48bc9fb2006-10-09 16:28:33 +000069 setOperationAction(ISD::SELECT, MVT::i32, Expand);
70
Rafael Espindola3c000bf2006-08-21 22:00:32 +000071 setOperationAction(ISD::SETCC, MVT::i32, Expand);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +000072 setOperationAction(ISD::SETCC, MVT::f32, Expand);
73 setOperationAction(ISD::SETCC, MVT::f64, Expand);
74
Rafael Espindola3c000bf2006-08-21 22:00:32 +000075 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
Rafael Espindolad8ed7f82006-10-23 20:08:22 +000076
Rafael Espindola97815c62006-12-05 17:57:23 +000077 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
Rafael Espindolad8ed7f82006-10-23 20:08:22 +000078 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
Rafael Espindola97815c62006-12-05 17:57:23 +000079 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
Rafael Espindolad8ed7f82006-10-23 20:08:22 +000080
Evan Chengc35497f2006-10-30 08:02:39 +000081 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
82 setOperationAction(ISD::BRIND, MVT::Other, Expand);
Rafael Espindola687bc492006-08-24 13:45:55 +000083 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +000084 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
85 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Rafael Espindola3c000bf2006-08-21 22:00:32 +000086
Rafael Espindolad2b56682006-10-14 17:59:54 +000087 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
88
Rafael Espindola0505be02006-10-16 21:10:32 +000089 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
90 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
91 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Rafael Espindola226f8bc2006-10-17 21:05:33 +000092 setOperationAction(ISD::SDIV, MVT::i32, Expand);
93 setOperationAction(ISD::UDIV, MVT::i32, Expand);
94 setOperationAction(ISD::SREM, MVT::i32, Expand);
95 setOperationAction(ISD::UREM, MVT::i32, Expand);
Rafael Espindola0505be02006-10-16 21:10:32 +000096
Rafael Espindola755be9b2006-08-25 17:55:16 +000097 setOperationAction(ISD::VASTART, MVT::Other, Custom);
Rafael Espindola0e5e3aa2006-10-24 20:15:21 +000098 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
Rafael Espindola755be9b2006-08-25 17:55:16 +000099 setOperationAction(ISD::VAEND, MVT::Other, Expand);
Rafael Espindola7ae68ab2006-10-26 13:31:26 +0000100 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000101
Rafael Espindolacd71da52006-10-03 17:27:58 +0000102 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
103 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
104
Rafael Espindola7ae68ab2006-10-26 13:31:26 +0000105 setStackPointerRegisterToSaveRestore(ARM::R13);
106
Rafael Espindola341b8642006-08-04 12:48:42 +0000107 setSchedulingPreference(SchedulingForRegPressure);
Rafael Espindola3717ca92006-08-20 01:49:49 +0000108 computeRegisterProperties();
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000109}
110
Rafael Espindola84b19be2006-07-16 01:02:57 +0000111namespace llvm {
112 namespace ARMISD {
113 enum NodeType {
114 // Start the numbering where the builting ops and target ops leave off.
115 FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
116 /// CALL - A direct function call.
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000117 CALL,
118
119 /// Return with a flag operand.
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000120 RET_FLAG,
121
122 CMP,
123
Rafael Espindola687bc492006-08-24 13:45:55 +0000124 SELECT,
125
Rafael Espindola27185192006-09-29 21:20:16 +0000126 BR,
127
Rafael Espindola9e071f02006-10-02 19:30:56 +0000128 FSITOS,
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000129 FTOSIS,
Rafael Espindola9e071f02006-10-02 19:30:56 +0000130
131 FSITOD,
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000132 FTOSID,
Rafael Espindola9e071f02006-10-02 19:30:56 +0000133
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000134 FUITOS,
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000135 FTOUIS,
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000136
137 FUITOD,
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000138 FTOUID,
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000139
Rafael Espindolaa2845842006-10-05 16:48:49 +0000140 FMRRD,
141
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000142 FMDRR,
143
144 FMSTAT
Rafael Espindola84b19be2006-07-16 01:02:57 +0000145 };
146 }
147}
148
Rafael Espindola42b62f32006-10-13 13:14:59 +0000149/// DAGFPCCToARMCC - Convert a DAG fp condition code to an ARM CC
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000150// Unordered = !N & !Z & C & V = V
151// Ordered = N | Z | !C | !V = N | Z | !V
Rafael Espindola42b62f32006-10-13 13:14:59 +0000152static ARMCC::CondCodes DAGFPCCToARMCC(ISD::CondCode CC) {
Rafael Espindola6f602de2006-08-24 16:13:15 +0000153 switch (CC) {
Rafael Espindolaebdabda2006-09-21 13:06:26 +0000154 default:
Rafael Espindola42b62f32006-10-13 13:14:59 +0000155 assert(0 && "Unknown fp condition code!");
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000156// SETOEQ = (N | Z | !V) & Z = Z = EQ
157 case ISD::SETEQ:
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000158 case ISD::SETOEQ: return ARMCC::EQ;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000159// SETOGT = (N | Z | !V) & !N & !Z = !V &!N &!Z = (N = V) & !Z = GT
160 case ISD::SETGT:
Rafael Espindola42b62f32006-10-13 13:14:59 +0000161 case ISD::SETOGT: return ARMCC::GT;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000162// SETOGE = (N | Z | !V) & !N = (Z | !V) & !N = !V & !N = GE
163 case ISD::SETGE:
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000164 case ISD::SETOGE: return ARMCC::GE;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000165// SETOLT = (N | Z | !V) & N = N = MI
166 case ISD::SETLT:
167 case ISD::SETOLT: return ARMCC::MI;
168// SETOLE = (N | Z | !V) & (N | Z) = N | Z = !C | Z = LS
169 case ISD::SETLE:
170 case ISD::SETOLE: return ARMCC::LS;
171// SETONE = (N | Z | !V) & !Z = (N | !V) & Z = !V & Z = Z = NE
172 case ISD::SETNE:
Rafael Espindola42b62f32006-10-13 13:14:59 +0000173 case ISD::SETONE: return ARMCC::NE;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000174// SETO = N | Z | !V = Z | !V = !V = VC
175 case ISD::SETO: return ARMCC::VC;
176// SETUO = V = VS
177 case ISD::SETUO: return ARMCC::VS;
178// SETUEQ = V | Z = ??
179// SETUGT = V | (!Z & !N) = !Z & !N = !Z & C = HI
180 case ISD::SETUGT: return ARMCC::HI;
181// SETUGE = V | !N = !N = PL
Rafael Espindola42b62f32006-10-13 13:14:59 +0000182 case ISD::SETUGE: return ARMCC::PL;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000183// SETULT = V | N = ??
184// SETULE = V | Z | N = ??
185// SETUNE = V | !Z = !Z = NE
Rafael Espindola42b62f32006-10-13 13:14:59 +0000186 case ISD::SETUNE: return ARMCC::NE;
187 }
188}
189
190/// DAGIntCCToARMCC - Convert a DAG integer condition code to an ARM CC
191static ARMCC::CondCodes DAGIntCCToARMCC(ISD::CondCode CC) {
192 switch (CC) {
193 default:
194 assert(0 && "Unknown integer condition code!");
195 case ISD::SETEQ: return ARMCC::EQ;
196 case ISD::SETNE: return ARMCC::NE;
197 case ISD::SETLT: return ARMCC::LT;
198 case ISD::SETLE: return ARMCC::LE;
199 case ISD::SETGT: return ARMCC::GT;
200 case ISD::SETGE: return ARMCC::GE;
Rafael Espindolabc4cec92006-09-03 13:19:16 +0000201 case ISD::SETULT: return ARMCC::CC;
Rafael Espindola42b62f32006-10-13 13:14:59 +0000202 case ISD::SETULE: return ARMCC::LS;
203 case ISD::SETUGT: return ARMCC::HI;
204 case ISD::SETUGE: return ARMCC::CS;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000205 }
206}
207
Rafael Espindola462af9a2006-12-05 17:37:31 +0000208std::vector<unsigned> ARMTargetLowering::
209getRegClassForInlineAsmConstraint(const std::string &Constraint,
210 MVT::ValueType VT) const {
211 if (Constraint.size() == 1) {
212 // FIXME: handling only r regs
213 switch (Constraint[0]) {
214 default: break; // Unknown constraint letter
215
216 case 'r': // GENERAL_REGS
217 case 'R': // LEGACY_REGS
218 if (VT == MVT::i32)
219 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
220 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
221 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
222 ARM::R12, ARM::R13, ARM::R14, 0);
223 break;
224
225 }
226 }
227
228 return std::vector<unsigned>();
229}
230
Rafael Espindola84b19be2006-07-16 01:02:57 +0000231const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
232 switch (Opcode) {
233 default: return 0;
234 case ARMISD::CALL: return "ARMISD::CALL";
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000235 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000236 case ARMISD::SELECT: return "ARMISD::SELECT";
237 case ARMISD::CMP: return "ARMISD::CMP";
Rafael Espindola687bc492006-08-24 13:45:55 +0000238 case ARMISD::BR: return "ARMISD::BR";
Rafael Espindola27185192006-09-29 21:20:16 +0000239 case ARMISD::FSITOS: return "ARMISD::FSITOS";
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000240 case ARMISD::FTOSIS: return "ARMISD::FTOSIS";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000241 case ARMISD::FSITOD: return "ARMISD::FSITOD";
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000242 case ARMISD::FTOSID: return "ARMISD::FTOSID";
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000243 case ARMISD::FUITOS: return "ARMISD::FUITOS";
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000244 case ARMISD::FTOUIS: return "ARMISD::FTOUIS";
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000245 case ARMISD::FUITOD: return "ARMISD::FUITOD";
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000246 case ARMISD::FTOUID: return "ARMISD::FTOUID";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000247 case ARMISD::FMRRD: return "ARMISD::FMRRD";
Rafael Espindolaa2845842006-10-05 16:48:49 +0000248 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000249 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Rafael Espindola84b19be2006-07-16 01:02:57 +0000250 }
251}
252
Rafael Espindolaa2845842006-10-05 16:48:49 +0000253class ArgumentLayout {
254 std::vector<bool> is_reg;
255 std::vector<unsigned> pos;
256 std::vector<MVT::ValueType> types;
257public:
Rafael Espindola39b5a212006-10-05 17:46:48 +0000258 ArgumentLayout(const std::vector<MVT::ValueType> &Types) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000259 types = Types;
260
261 unsigned RegNum = 0;
262 unsigned StackOffset = 0;
Rafael Espindola39b5a212006-10-05 17:46:48 +0000263 for(std::vector<MVT::ValueType>::const_iterator I = Types.begin();
Rafael Espindolaa2845842006-10-05 16:48:49 +0000264 I != Types.end();
265 ++I) {
266 MVT::ValueType VT = *I;
267 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
268 unsigned size = MVT::getSizeInBits(VT)/32;
269
270 RegNum = ((RegNum + size - 1) / size) * size;
271 if (RegNum < 4) {
272 pos.push_back(RegNum);
273 is_reg.push_back(true);
274 RegNum += size;
275 } else {
276 unsigned bytes = size * 32/8;
277 StackOffset = ((StackOffset + bytes - 1) / bytes) * bytes;
278 pos.push_back(StackOffset);
279 is_reg.push_back(false);
280 StackOffset += bytes;
281 }
282 }
283 }
284 unsigned getRegisterNum(unsigned argNum) {
285 assert(isRegister(argNum));
286 return pos[argNum];
287 }
288 unsigned getOffset(unsigned argNum) {
289 assert(isOffset(argNum));
290 return pos[argNum];
291 }
292 unsigned isRegister(unsigned argNum) {
293 assert(argNum < is_reg.size());
294 return is_reg[argNum];
295 }
296 unsigned isOffset(unsigned argNum) {
297 return !isRegister(argNum);
298 }
299 MVT::ValueType getType(unsigned argNum) {
300 assert(argNum < types.size());
301 return types[argNum];
302 }
303 unsigned getStackSize(void) {
304 int last = is_reg.size() - 1;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000305 if (last < 0)
306 return 0;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000307 if (isRegister(last))
308 return 0;
309 return getOffset(last) + MVT::getSizeInBits(getType(last))/8;
310 }
311 int lastRegArg(void) {
312 int size = is_reg.size();
313 int last = 0;
314 while(last < size && isRegister(last))
315 last++;
316 last--;
317 return last;
318 }
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000319 int lastRegNum(void) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000320 int l = lastRegArg();
321 if (l < 0)
322 return -1;
323 unsigned r = getRegisterNum(l);
324 MVT::ValueType t = getType(l);
325 assert(t == MVT::i32 || t == MVT::f32 || t == MVT::f64);
326 if (t == MVT::f64)
327 return r + 1;
328 return r;
329 }
330};
331
Rafael Espindola84b19be2006-07-16 01:02:57 +0000332// This transforms a ISD::CALL node into a
333// callseq_star <- ARMISD:CALL <- callseq_end
334// chain
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000335static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola84b19be2006-07-16 01:02:57 +0000336 SDOperand Chain = Op.getOperand(0);
337 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Rafael Espindola5f1b6982006-10-18 12:03:07 +0000338 assert((CallConv == CallingConv::C ||
339 CallConv == CallingConv::Fast)
340 && "unknown calling convention");
Rafael Espindola84b19be2006-07-16 01:02:57 +0000341 SDOperand Callee = Op.getOperand(4);
342 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Rafael Espindola1a009462006-08-08 13:02:29 +0000343 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000344 static const unsigned regs[] = {
Rafael Espindolafac00a92006-07-25 20:17:20 +0000345 ARM::R0, ARM::R1, ARM::R2, ARM::R3
346 };
347
Rafael Espindolaa2845842006-10-05 16:48:49 +0000348 std::vector<MVT::ValueType> Types;
349 for (unsigned i = 0; i < NumOps; ++i) {
350 MVT::ValueType VT = Op.getOperand(5+2*i).getValueType();
351 Types.push_back(VT);
352 }
353 ArgumentLayout Layout(Types);
Rafael Espindolafac00a92006-07-25 20:17:20 +0000354
Rafael Espindolaa2845842006-10-05 16:48:49 +0000355 unsigned NumBytes = Layout.getStackSize();
356
357 Chain = DAG.getCALLSEQ_START(Chain,
358 DAG.getConstant(NumBytes, MVT::i32));
359
360 //Build a sequence of stores
361 std::vector<SDOperand> MemOpChains;
362 for (unsigned i = Layout.lastRegArg() + 1; i < NumOps; ++i) {
363 SDOperand Arg = Op.getOperand(5+2*i);
364 unsigned ArgOffset = Layout.getOffset(i);
365 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
366 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Evan Cheng8b2794a2006-10-13 21:14:26 +0000367 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000368 }
Rafael Espindola1a009462006-08-08 13:02:29 +0000369 if (!MemOpChains.empty())
Chris Lattnere2199452006-08-11 17:38:39 +0000370 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
371 &MemOpChains[0], MemOpChains.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000372
Rafael Espindola0505be02006-10-16 21:10:32 +0000373 // If the callee is a GlobalAddress node (quite common, every direct call is)
374 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
375 // Likewise ExternalSymbol -> TargetExternalSymbol.
376 assert(Callee.getValueType() == MVT::i32);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000377 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Rafael Espindola0505be02006-10-16 21:10:32 +0000378 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
379 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
380 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000381
382 // If this is a direct call, pass the chain and the callee.
383 assert (Callee.Val);
384 std::vector<SDOperand> Ops;
385 Ops.push_back(Chain);
386 Ops.push_back(Callee);
387
Rafael Espindolaa2845842006-10-05 16:48:49 +0000388 // Build a sequence of copy-to-reg nodes chained together with token chain
389 // and flag operands which copy the outgoing args into the appropriate regs.
390 SDOperand InFlag;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000391 for (int i = 0, e = Layout.lastRegArg(); i <= e; ++i) {
Rafael Espindola4a408d42006-10-06 12:50:22 +0000392 SDOperand Arg = Op.getOperand(5+2*i);
393 unsigned RegNum = Layout.getRegisterNum(i);
394 unsigned Reg1 = regs[RegNum];
395 MVT::ValueType VT = Layout.getType(i);
396 assert(VT == Arg.getValueType());
397 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000398
399 // Add argument register to the end of the list so that it is known live
400 // into the call.
Rafael Espindola4a408d42006-10-06 12:50:22 +0000401 Ops.push_back(DAG.getRegister(Reg1, MVT::i32));
402 if (VT == MVT::f64) {
403 unsigned Reg2 = regs[RegNum + 1];
404 SDOperand SDReg1 = DAG.getRegister(Reg1, MVT::i32);
405 SDOperand SDReg2 = DAG.getRegister(Reg2, MVT::i32);
406
407 Ops.push_back(DAG.getRegister(Reg2, MVT::i32));
408 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola935b1f82006-10-06 20:33:26 +0000409 SDOperand Ops[] = {Chain, SDReg1, SDReg2, Arg, InFlag};
410 Chain = DAG.getNode(ARMISD::FMRRD, VTs, Ops, InFlag.Val ? 5 : 4);
Rafael Espindola4a408d42006-10-06 12:50:22 +0000411 } else {
412 if (VT == MVT::f32)
413 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg);
414 Chain = DAG.getCopyToReg(Chain, Reg1, Arg, InFlag);
415 }
416 InFlag = Chain.getValue(1);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000417 }
418
419 std::vector<MVT::ValueType> NodeTys;
420 NodeTys.push_back(MVT::Other); // Returns a chain
421 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Rafael Espindola7a53bd02006-08-09 16:41:12 +0000422
Rafael Espindola84b19be2006-07-16 01:02:57 +0000423 unsigned CallOpc = ARMISD::CALL;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000424 if (InFlag.Val)
425 Ops.push_back(InFlag);
Chris Lattner87428672006-08-11 17:22:35 +0000426 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000427 InFlag = Chain.getValue(1);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000428
Rafael Espindolafac00a92006-07-25 20:17:20 +0000429 std::vector<SDOperand> ResultVals;
430 NodeTys.clear();
431
432 // If the call has results, copy the values out of the ret val registers.
Rafael Espindola614057b2006-10-06 19:10:05 +0000433 MVT::ValueType VT = Op.Val->getValueType(0);
434 if (VT != MVT::Other) {
435 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
Rafael Espindola614057b2006-10-06 19:10:05 +0000436
437 SDOperand Value1 = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
438 Chain = Value1.getValue(1);
439 InFlag = Value1.getValue(2);
Rafael Espindola26a76d12006-10-13 16:47:22 +0000440 NodeTys.push_back(VT);
441 if (VT == MVT::i32) {
442 ResultVals.push_back(Value1);
443 if (Op.Val->getValueType(1) == MVT::i32) {
444 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
445 Chain = Value2.getValue(1);
446 ResultVals.push_back(Value2);
447 NodeTys.push_back(VT);
448 }
449 }
450 if (VT == MVT::f32) {
451 SDOperand Value = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Value1);
452 ResultVals.push_back(Value);
453 }
Rafael Espindola614057b2006-10-06 19:10:05 +0000454 if (VT == MVT::f64) {
455 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
456 Chain = Value2.getValue(1);
Rafael Espindola26a76d12006-10-13 16:47:22 +0000457 SDOperand Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
458 ResultVals.push_back(Value);
Rafael Espindola614057b2006-10-06 19:10:05 +0000459 }
Rafael Espindolafac00a92006-07-25 20:17:20 +0000460 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000461
462 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
463 DAG.getConstant(NumBytes, MVT::i32));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000464 NodeTys.push_back(MVT::Other);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000465
Rafael Espindolafac00a92006-07-25 20:17:20 +0000466 if (ResultVals.empty())
467 return Chain;
468
469 ResultVals.push_back(Chain);
Chris Lattner87428672006-08-11 17:22:35 +0000470 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
471 ResultVals.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000472 return Res.getValue(Op.ResNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000473}
474
475static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
476 SDOperand Copy;
Rafael Espindola4b023672006-06-05 22:26:14 +0000477 SDOperand Chain = Op.getOperand(0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000478 SDOperand R0 = DAG.getRegister(ARM::R0, MVT::i32);
479 SDOperand R1 = DAG.getRegister(ARM::R1, MVT::i32);
480
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000481 switch(Op.getNumOperands()) {
482 default:
483 assert(0 && "Do not know how to return this many arguments!");
484 abort();
Rafael Espindola4b023672006-06-05 22:26:14 +0000485 case 1: {
486 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
Rafael Espindola6312da02006-08-03 22:50:11 +0000487 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
Rafael Espindola4b023672006-06-05 22:26:14 +0000488 }
Rafael Espindola27185192006-09-29 21:20:16 +0000489 case 3: {
490 SDOperand Val = Op.getOperand(1);
491 assert(Val.getValueType() == MVT::i32 ||
Rafael Espindola9e071f02006-10-02 19:30:56 +0000492 Val.getValueType() == MVT::f32 ||
493 Val.getValueType() == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000494
Rafael Espindola9e071f02006-10-02 19:30:56 +0000495 if (Val.getValueType() == MVT::f64) {
496 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
497 SDOperand Ops[] = {Chain, R0, R1, Val};
498 Copy = DAG.getNode(ARMISD::FMRRD, VTs, Ops, 4);
499 } else {
500 if (Val.getValueType() == MVT::f32)
501 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
502 Copy = DAG.getCopyToReg(Chain, R0, Val, SDOperand());
503 }
504
505 if (DAG.getMachineFunction().liveout_empty()) {
Rafael Espindola4b023672006-06-05 22:26:14 +0000506 DAG.getMachineFunction().addLiveOut(ARM::R0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000507 if (Val.getValueType() == MVT::f64)
508 DAG.getMachineFunction().addLiveOut(ARM::R1);
509 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000510 break;
Rafael Espindola27185192006-09-29 21:20:16 +0000511 }
Rafael Espindola3a02f022006-09-04 19:05:01 +0000512 case 5:
513 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
514 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
515 // If we haven't noted the R0+R1 are live out, do so now.
516 if (DAG.getMachineFunction().liveout_empty()) {
517 DAG.getMachineFunction().addLiveOut(ARM::R0);
518 DAG.getMachineFunction().addLiveOut(ARM::R1);
519 }
520 break;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000521 }
Rafael Espindola4b023672006-06-05 22:26:14 +0000522
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000523 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
524 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000525}
526
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000527static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
528 MVT::ValueType PtrVT = Op.getValueType();
529 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengc356a572006-09-12 21:04:05 +0000530 Constant *C = CP->getConstVal();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000531 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
532
533 return CPI;
534}
535
536static SDOperand LowerGlobalAddress(SDOperand Op,
537 SelectionDAG &DAG) {
538 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Rafael Espindola61369da2006-08-14 19:01:24 +0000539 int alignment = 2;
540 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
Evan Cheng466685d2006-10-09 20:57:25 +0000541 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr, NULL, 0);
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000542}
543
Rafael Espindola755be9b2006-08-25 17:55:16 +0000544static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
545 unsigned VarArgsFrameIndex) {
546 // vastart just stores the address of the VarArgsFrameIndex slot into the
547 // memory location argument.
548 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
549 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +0000550 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
551 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), SV->getValue(),
552 SV->getOffset());
Rafael Espindola755be9b2006-08-25 17:55:16 +0000553}
554
555static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
556 int &VarArgsFrameIndex) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000557 MachineFunction &MF = DAG.getMachineFunction();
558 MachineFrameInfo *MFI = MF.getFrameInfo();
559 SSARegMap *RegMap = MF.getSSARegMap();
560 unsigned NumArgs = Op.Val->getNumValues()-1;
561 SDOperand Root = Op.getOperand(0);
562 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
563 static const unsigned REGS[] = {
564 ARM::R0, ARM::R1, ARM::R2, ARM::R3
565 };
566
567 std::vector<MVT::ValueType> Types(Op.Val->value_begin(), Op.Val->value_end() - 1);
568 ArgumentLayout Layout(Types);
569
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000570 std::vector<SDOperand> ArgValues;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000571 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000572 MVT::ValueType VT = Types[ArgNo];
Rafael Espindola4b442b52006-05-23 02:48:20 +0000573
Rafael Espindolaa2845842006-10-05 16:48:49 +0000574 SDOperand Value;
575 if (Layout.isRegister(ArgNo)) {
576 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
577 unsigned RegNum = Layout.getRegisterNum(ArgNo);
578 unsigned Reg1 = REGS[RegNum];
579 unsigned VReg1 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
580 SDOperand Value1 = DAG.getCopyFromReg(Root, VReg1, MVT::i32);
581 MF.addLiveIn(Reg1, VReg1);
582 if (VT == MVT::f64) {
583 unsigned Reg2 = REGS[RegNum + 1];
584 unsigned VReg2 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
585 SDOperand Value2 = DAG.getCopyFromReg(Root, VReg2, MVT::i32);
586 MF.addLiveIn(Reg2, VReg2);
587 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
588 } else {
589 Value = Value1;
590 if (VT == MVT::f32)
591 Value = DAG.getNode(ISD::BIT_CONVERT, VT, Value);
592 }
593 } else {
594 // If the argument is actually used, emit a load from the right stack
595 // slot.
596 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
597 unsigned Offset = Layout.getOffset(ArgNo);
598 unsigned Size = MVT::getSizeInBits(VT)/8;
599 int FI = MFI->CreateFixedObject(Size, Offset);
600 SDOperand FIN = DAG.getFrameIndex(FI, VT);
Evan Cheng466685d2006-10-09 20:57:25 +0000601 Value = DAG.getLoad(VT, Root, FIN, NULL, 0);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000602 } else {
603 Value = DAG.getNode(ISD::UNDEF, VT);
604 }
605 }
606 ArgValues.push_back(Value);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000607 }
608
Rafael Espindolaa2845842006-10-05 16:48:49 +0000609 unsigned NextRegNum = Layout.lastRegNum() + 1;
610
Rafael Espindola755be9b2006-08-25 17:55:16 +0000611 if (isVarArg) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000612 //If this function is vararg we must store the remaing
613 //registers so that they can be acessed with va_start
Rafael Espindola755be9b2006-08-25 17:55:16 +0000614 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000615 -16 + NextRegNum * 4);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000616
Rafael Espindola755be9b2006-08-25 17:55:16 +0000617 SmallVector<SDOperand, 4> MemOps;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000618 for (unsigned RegNo = NextRegNum; RegNo < 4; ++RegNo) {
619 int RegOffset = - (4 - RegNo) * 4;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000620 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000621 RegOffset);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000622 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
623
Rafael Espindolaa2845842006-10-05 16:48:49 +0000624 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
625 MF.addLiveIn(REGS[RegNo], VReg);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000626
627 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
Evan Cheng8b2794a2006-10-13 21:14:26 +0000628 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000629 MemOps.push_back(Store);
630 }
631 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
632 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000633
634 ArgValues.push_back(Root);
635
636 // Return the new list of results.
637 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
638 Op.Val->value_end());
Chris Lattner87428672006-08-11 17:22:35 +0000639 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
Rafael Espindoladc124a22006-05-18 21:45:49 +0000640}
641
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000642static SDOperand GetCMP(ISD::CondCode CC, SDOperand LHS, SDOperand RHS,
643 SelectionDAG &DAG) {
644 MVT::ValueType vt = LHS.getValueType();
Rafael Espindola0d9fe762006-10-10 16:33:47 +0000645 assert(vt == MVT::i32 || vt == MVT::f32 || vt == MVT::f64);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000646
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000647 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
Rafael Espindola42b62f32006-10-13 13:14:59 +0000648
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000649 if (vt != MVT::i32)
650 Cmp = DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp);
651 return Cmp;
652}
653
Rafael Espindola42b62f32006-10-13 13:14:59 +0000654static SDOperand GetARMCC(ISD::CondCode CC, MVT::ValueType vt,
655 SelectionDAG &DAG) {
656 assert(vt == MVT::i32 || vt == MVT::f32 || vt == MVT::f64);
657 if (vt == MVT::i32)
658 return DAG.getConstant(DAGIntCCToARMCC(CC), MVT::i32);
659 else
660 return DAG.getConstant(DAGFPCCToARMCC(CC), MVT::i32);
661}
662
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000663static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
664 SDOperand LHS = Op.getOperand(0);
665 SDOperand RHS = Op.getOperand(1);
666 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
667 SDOperand TrueVal = Op.getOperand(2);
668 SDOperand FalseVal = Op.getOperand(3);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000669 SDOperand Cmp = GetCMP(CC, LHS, RHS, DAG);
Rafael Espindola42b62f32006-10-13 13:14:59 +0000670 SDOperand ARMCC = GetARMCC(CC, LHS.getValueType(), DAG);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000671 return DAG.getNode(ARMISD::SELECT, MVT::i32, TrueVal, FalseVal, ARMCC, Cmp);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000672}
673
Rafael Espindola687bc492006-08-24 13:45:55 +0000674static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
675 SDOperand Chain = Op.getOperand(0);
676 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
677 SDOperand LHS = Op.getOperand(2);
678 SDOperand RHS = Op.getOperand(3);
679 SDOperand Dest = Op.getOperand(4);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000680 SDOperand Cmp = GetCMP(CC, LHS, RHS, DAG);
Rafael Espindola42b62f32006-10-13 13:14:59 +0000681 SDOperand ARMCC = GetARMCC(CC, LHS.getValueType(), DAG);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000682 return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, ARMCC, Cmp);
Rafael Espindola687bc492006-08-24 13:45:55 +0000683}
684
Rafael Espindola27185192006-09-29 21:20:16 +0000685static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola9e071f02006-10-02 19:30:56 +0000686 SDOperand IntVal = Op.getOperand(0);
Rafael Espindola27185192006-09-29 21:20:16 +0000687 assert(IntVal.getValueType() == MVT::i32);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000688 MVT::ValueType vt = Op.getValueType();
689 assert(vt == MVT::f32 ||
690 vt == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000691
692 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000693 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FSITOS : ARMISD::FSITOD;
694 return DAG.getNode(op, vt, Tmp);
Rafael Espindola27185192006-09-29 21:20:16 +0000695}
696
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000697static SDOperand LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
698 assert(Op.getValueType() == MVT::i32);
699 SDOperand FloatVal = Op.getOperand(0);
700 MVT::ValueType vt = FloatVal.getValueType();
701 assert(vt == MVT::f32 || vt == MVT::f64);
702
703 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FTOSIS : ARMISD::FTOSID;
704 SDOperand Tmp = DAG.getNode(op, MVT::f32, FloatVal);
705 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Tmp);
706}
707
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000708static SDOperand LowerUINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
709 SDOperand IntVal = Op.getOperand(0);
710 assert(IntVal.getValueType() == MVT::i32);
711 MVT::ValueType vt = Op.getValueType();
712 assert(vt == MVT::f32 ||
713 vt == MVT::f64);
714
715 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
716 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FUITOS : ARMISD::FUITOD;
717 return DAG.getNode(op, vt, Tmp);
718}
719
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000720static SDOperand LowerFP_TO_UINT(SDOperand Op, SelectionDAG &DAG) {
721 assert(Op.getValueType() == MVT::i32);
722 SDOperand FloatVal = Op.getOperand(0);
723 MVT::ValueType vt = FloatVal.getValueType();
724 assert(vt == MVT::f32 || vt == MVT::f64);
725
726 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FTOUIS : ARMISD::FTOUID;
727 SDOperand Tmp = DAG.getNode(op, MVT::f32, FloatVal);
728 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Tmp);
729}
730
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000731SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
732 switch (Op.getOpcode()) {
733 default:
734 assert(0 && "Should not custom lower this!");
Rafael Espindola1c8f0532006-05-15 22:34:39 +0000735 abort();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000736 case ISD::ConstantPool:
737 return LowerConstantPool(Op, DAG);
738 case ISD::GlobalAddress:
739 return LowerGlobalAddress(Op, DAG);
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000740 case ISD::FP_TO_SINT:
741 return LowerFP_TO_SINT(Op, DAG);
Rafael Espindola27185192006-09-29 21:20:16 +0000742 case ISD::SINT_TO_FP:
743 return LowerSINT_TO_FP(Op, DAG);
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000744 case ISD::FP_TO_UINT:
745 return LowerFP_TO_UINT(Op, DAG);
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000746 case ISD::UINT_TO_FP:
747 return LowerUINT_TO_FP(Op, DAG);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000748 case ISD::FORMAL_ARGUMENTS:
Rafael Espindola755be9b2006-08-25 17:55:16 +0000749 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000750 case ISD::CALL:
751 return LowerCALL(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000752 case ISD::RET:
753 return LowerRET(Op, DAG);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000754 case ISD::SELECT_CC:
755 return LowerSELECT_CC(Op, DAG);
Rafael Espindola687bc492006-08-24 13:45:55 +0000756 case ISD::BR_CC:
757 return LowerBR_CC(Op, DAG);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000758 case ISD::VASTART:
759 return LowerVASTART(Op, DAG, VarArgsFrameIndex);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000760 }
761}
762
763//===----------------------------------------------------------------------===//
764// Instruction Selector Implementation
765//===----------------------------------------------------------------------===//
766
767//===--------------------------------------------------------------------===//
768/// ARMDAGToDAGISel - ARM specific code to select ARM machine
769/// instructions for SelectionDAG operations.
770///
771namespace {
772class ARMDAGToDAGISel : public SelectionDAGISel {
773 ARMTargetLowering Lowering;
774
775public:
776 ARMDAGToDAGISel(TargetMachine &TM)
777 : SelectionDAGISel(Lowering), Lowering(TM) {
778 }
779
Evan Cheng9ade2182006-08-26 05:34:46 +0000780 SDNode *Select(SDOperand Op);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000781 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Evan Cheng0d538262006-11-08 20:34:28 +0000782 bool SelectAddrMode1(SDOperand Op, SDOperand N, SDOperand &Arg,
783 SDOperand &Shift, SDOperand &ShiftType);
Rafael Espindolaf64945d2006-12-12 01:03:11 +0000784 bool SelectAddrMode1a(SDOperand Op, SDOperand N, SDOperand &Arg,
785 SDOperand &Shift, SDOperand &ShiftType);
Evan Cheng0d538262006-11-08 20:34:28 +0000786 bool SelectAddrMode2(SDOperand Op, SDOperand N, SDOperand &Arg,
787 SDOperand &Offset);
788 bool SelectAddrMode5(SDOperand Op, SDOperand N, SDOperand &Arg,
789 SDOperand &Offset);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000790
791 // Include the pieces autogenerated from the target description.
792#include "ARMGenDAGISel.inc"
793};
794
795void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
796 DEBUG(BB->dump());
797
798 DAG.setRoot(SelectRoot(DAG.getRoot()));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000799 DAG.RemoveDeadNodes();
800
801 ScheduleAndEmitDAG(DAG);
802}
803
Rafael Espindola61369da2006-08-14 19:01:24 +0000804static bool isInt12Immediate(SDNode *N, short &Imm) {
805 if (N->getOpcode() != ISD::Constant)
806 return false;
807
808 int32_t t = cast<ConstantSDNode>(N)->getValue();
Rafael Espindola7246d332006-09-21 11:29:52 +0000809 int max = 1<<12;
Rafael Espindola61369da2006-08-14 19:01:24 +0000810 int min = -max;
811 if (t > min && t < max) {
812 Imm = t;
813 return true;
814 }
815 else
816 return false;
817}
818
819static bool isInt12Immediate(SDOperand Op, short &Imm) {
820 return isInt12Immediate(Op.Val, Imm);
821}
822
Rafael Espindola7246d332006-09-21 11:29:52 +0000823static uint32_t rotateL(uint32_t x) {
824 uint32_t bit31 = (x & (1 << 31)) >> 31;
825 uint32_t t = x << 1;
826 return t | bit31;
827}
828
829static bool isUInt8Immediate(uint32_t x) {
830 return x < (1 << 8);
831}
832
833static bool isRotInt8Immediate(uint32_t x) {
834 int r;
835 for (r = 0; r < 16; r++) {
836 if (isUInt8Immediate(x))
837 return true;
838 x = rotateL(rotateL(x));
839 }
840 return false;
841}
842
Evan Cheng0d538262006-11-08 20:34:28 +0000843bool ARMDAGToDAGISel::SelectAddrMode1(SDOperand Op,
844 SDOperand N,
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000845 SDOperand &Arg,
846 SDOperand &Shift,
847 SDOperand &ShiftType) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000848 switch(N.getOpcode()) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000849 case ISD::Constant: {
Rafael Espindola7246d332006-09-21 11:29:52 +0000850 uint32_t val = cast<ConstantSDNode>(N)->getValue();
851 if(!isRotInt8Immediate(val)) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000852 Constant *C = ConstantInt::get(Type::UIntTy, val);
Rafael Espindola7246d332006-09-21 11:29:52 +0000853 int alignment = 2;
854 SDOperand Addr = CurDAG->getTargetConstantPool(C, MVT::i32, alignment);
855 SDOperand Z = CurDAG->getTargetConstant(0, MVT::i32);
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000856 SDNode *n = CurDAG->getTargetNode(ARM::LDR, MVT::i32, Addr, Z);
Rafael Espindola7246d332006-09-21 11:29:52 +0000857 Arg = SDOperand(n, 0);
858 } else
859 Arg = CurDAG->getTargetConstant(val, MVT::i32);
860
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000861 Shift = CurDAG->getTargetConstant(0, MVT::i32);
862 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000863 return true;
864 }
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000865 case ISD::SRA:
866 Arg = N.getOperand(0);
867 Shift = N.getOperand(1);
868 ShiftType = CurDAG->getTargetConstant(ARMShift::ASR, MVT::i32);
869 return true;
870 case ISD::SRL:
871 Arg = N.getOperand(0);
872 Shift = N.getOperand(1);
873 ShiftType = CurDAG->getTargetConstant(ARMShift::LSR, MVT::i32);
874 return true;
875 case ISD::SHL:
876 Arg = N.getOperand(0);
877 Shift = N.getOperand(1);
878 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
879 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000880 }
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000881
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000882 Arg = N;
883 Shift = CurDAG->getTargetConstant(0, MVT::i32);
884 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000885 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000886}
887
Rafael Espindolaf64945d2006-12-12 01:03:11 +0000888bool ARMDAGToDAGISel::SelectAddrMode1a(SDOperand Op,
889 SDOperand N,
890 SDOperand &Arg,
891 SDOperand &Shift,
892 SDOperand &ShiftType) {
893 if (N.getOpcode() != ISD::Constant)
894 return false;
895
896 uint32_t val = ~cast<ConstantSDNode>(N)->getValue();
897 if(!isRotInt8Immediate(val))
898 return false;
899
900 Arg = CurDAG->getTargetConstant(val, MVT::i32);
901 Shift = CurDAG->getTargetConstant(0, MVT::i32);
902 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
903
904 return true;
905}
906
Evan Cheng0d538262006-11-08 20:34:28 +0000907bool ARMDAGToDAGISel::SelectAddrMode2(SDOperand Op, SDOperand N,
908 SDOperand &Arg, SDOperand &Offset) {
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000909 //TODO: complete and cleanup!
910 SDOperand Zero = CurDAG->getTargetConstant(0, MVT::i32);
911 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
912 Arg = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
913 Offset = Zero;
914 return true;
915 }
916 if (N.getOpcode() == ISD::ADD) {
917 short imm = 0;
918 if (isInt12Immediate(N.getOperand(1), imm)) {
919 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
920 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
921 Arg = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
922 } else {
923 Arg = N.getOperand(0);
924 }
925 return true; // [r+i]
926 }
927 }
928 Offset = Zero;
929 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
930 Arg = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
931 else
932 Arg = N;
933 return true;
934}
935
Evan Cheng0d538262006-11-08 20:34:28 +0000936bool ARMDAGToDAGISel::SelectAddrMode5(SDOperand Op,
937 SDOperand N, SDOperand &Arg,
Rafael Espindola32bd5f42006-10-17 18:04:53 +0000938 SDOperand &Offset) {
939 //TODO: detect offset
940 Offset = CurDAG->getTargetConstant(0, MVT::i32);
941 Arg = N;
942 return true;
943}
944
Evan Cheng9ade2182006-08-26 05:34:46 +0000945SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000946 SDNode *N = Op.Val;
947
948 switch (N->getOpcode()) {
949 default:
Evan Cheng9ade2182006-08-26 05:34:46 +0000950 return SelectCode(Op);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000951 break;
Rafael Espindolaf819a492006-11-09 13:58:55 +0000952 case ISD::FrameIndex: {
953 int FI = cast<FrameIndexSDNode>(N)->getIndex();
954 SDOperand Ops[] = {CurDAG->getTargetFrameIndex(FI, MVT::i32),
955 CurDAG->getTargetConstant(0, MVT::i32),
956 CurDAG->getTargetConstant(0, MVT::i32),
957 CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32)};
958
959 return CurDAG->SelectNodeTo(N, ARM::ADD, MVT::i32, Ops,
960 sizeof(Ops)/sizeof(SDOperand));
961 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000962 }
Rafael Espindolaf819a492006-11-09 13:58:55 +0000963 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000964}
965
966} // end anonymous namespace
967
968/// createARMISelDag - This pass converts a legalized DAG into a
969/// ARM-specific DAG, ready for instruction scheduling.
970///
971FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
972 return new ARMDAGToDAGISel(TM);
973}