blob: d0ad39fbc70f359ed6b101710e0f4de98d0e1f58 [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);
Lauro Ramos Venancio301009a2006-12-28 13:11:14 +000076 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
77 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Rafael Espindolad8ed7f82006-10-23 20:08:22 +000078
Rafael Espindola97815c62006-12-05 17:57:23 +000079 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
Rafael Espindolad8ed7f82006-10-23 20:08:22 +000080 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
Rafael Espindola97815c62006-12-05 17:57:23 +000081 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
Rafael Espindolad8ed7f82006-10-23 20:08:22 +000082
Evan Chengc35497f2006-10-30 08:02:39 +000083 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
84 setOperationAction(ISD::BRIND, MVT::Other, Expand);
Rafael Espindola687bc492006-08-24 13:45:55 +000085 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +000086 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
87 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Rafael Espindola3c000bf2006-08-21 22:00:32 +000088
Rafael Espindolad2b56682006-10-14 17:59:54 +000089 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
90
Rafael Espindola0505be02006-10-16 21:10:32 +000091 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
92 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
93 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Rafael Espindola226f8bc2006-10-17 21:05:33 +000094 setOperationAction(ISD::SDIV, MVT::i32, Expand);
95 setOperationAction(ISD::UDIV, MVT::i32, Expand);
96 setOperationAction(ISD::SREM, MVT::i32, Expand);
97 setOperationAction(ISD::UREM, MVT::i32, Expand);
Rafael Espindola0505be02006-10-16 21:10:32 +000098
Rafael Espindola755be9b2006-08-25 17:55:16 +000099 setOperationAction(ISD::VASTART, MVT::Other, Custom);
Rafael Espindola0e5e3aa2006-10-24 20:15:21 +0000100 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000101 setOperationAction(ISD::VAEND, MVT::Other, Expand);
Rafael Espindola7ae68ab2006-10-26 13:31:26 +0000102 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000103
Rafael Espindolacd71da52006-10-03 17:27:58 +0000104 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
105 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
106
Rafael Espindola7ae68ab2006-10-26 13:31:26 +0000107 setStackPointerRegisterToSaveRestore(ARM::R13);
108
Rafael Espindola341b8642006-08-04 12:48:42 +0000109 setSchedulingPreference(SchedulingForRegPressure);
Rafael Espindola3717ca92006-08-20 01:49:49 +0000110 computeRegisterProperties();
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000111}
112
Rafael Espindola84b19be2006-07-16 01:02:57 +0000113namespace llvm {
114 namespace ARMISD {
115 enum NodeType {
116 // Start the numbering where the builting ops and target ops leave off.
117 FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
118 /// CALL - A direct function call.
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000119 CALL,
120
121 /// Return with a flag operand.
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000122 RET_FLAG,
123
124 CMP,
125
Rafael Espindola687bc492006-08-24 13:45:55 +0000126 SELECT,
127
Rafael Espindola27185192006-09-29 21:20:16 +0000128 BR,
129
Rafael Espindola9e071f02006-10-02 19:30:56 +0000130 FSITOS,
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000131 FTOSIS,
Rafael Espindola9e071f02006-10-02 19:30:56 +0000132
133 FSITOD,
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000134 FTOSID,
Rafael Espindola9e071f02006-10-02 19:30:56 +0000135
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000136 FUITOS,
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000137 FTOUIS,
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000138
139 FUITOD,
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000140 FTOUID,
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000141
Rafael Espindolaa2845842006-10-05 16:48:49 +0000142 FMRRD,
143
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000144 FMDRR,
145
146 FMSTAT
Rafael Espindola84b19be2006-07-16 01:02:57 +0000147 };
148 }
149}
150
Rafael Espindola42b62f32006-10-13 13:14:59 +0000151/// DAGFPCCToARMCC - Convert a DAG fp condition code to an ARM CC
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000152// Unordered = !N & !Z & C & V = V
153// Ordered = N | Z | !C | !V = N | Z | !V
Rafael Espindola42b62f32006-10-13 13:14:59 +0000154static ARMCC::CondCodes DAGFPCCToARMCC(ISD::CondCode CC) {
Rafael Espindola6f602de2006-08-24 16:13:15 +0000155 switch (CC) {
Rafael Espindolaebdabda2006-09-21 13:06:26 +0000156 default:
Rafael Espindola42b62f32006-10-13 13:14:59 +0000157 assert(0 && "Unknown fp condition code!");
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000158// SETOEQ = (N | Z | !V) & Z = Z = EQ
159 case ISD::SETEQ:
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000160 case ISD::SETOEQ: return ARMCC::EQ;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000161// SETOGT = (N | Z | !V) & !N & !Z = !V &!N &!Z = (N = V) & !Z = GT
162 case ISD::SETGT:
Rafael Espindola42b62f32006-10-13 13:14:59 +0000163 case ISD::SETOGT: return ARMCC::GT;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000164// SETOGE = (N | Z | !V) & !N = (Z | !V) & !N = !V & !N = GE
165 case ISD::SETGE:
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000166 case ISD::SETOGE: return ARMCC::GE;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000167// SETOLT = (N | Z | !V) & N = N = MI
168 case ISD::SETLT:
169 case ISD::SETOLT: return ARMCC::MI;
170// SETOLE = (N | Z | !V) & (N | Z) = N | Z = !C | Z = LS
171 case ISD::SETLE:
172 case ISD::SETOLE: return ARMCC::LS;
173// SETONE = (N | Z | !V) & !Z = (N | !V) & Z = !V & Z = Z = NE
174 case ISD::SETNE:
Rafael Espindola42b62f32006-10-13 13:14:59 +0000175 case ISD::SETONE: return ARMCC::NE;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000176// SETO = N | Z | !V = Z | !V = !V = VC
177 case ISD::SETO: return ARMCC::VC;
178// SETUO = V = VS
179 case ISD::SETUO: return ARMCC::VS;
180// SETUEQ = V | Z = ??
181// SETUGT = V | (!Z & !N) = !Z & !N = !Z & C = HI
182 case ISD::SETUGT: return ARMCC::HI;
183// SETUGE = V | !N = !N = PL
Rafael Espindola42b62f32006-10-13 13:14:59 +0000184 case ISD::SETUGE: return ARMCC::PL;
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000185// SETULT = V | N = ??
186// SETULE = V | Z | N = ??
187// SETUNE = V | !Z = !Z = NE
Rafael Espindola42b62f32006-10-13 13:14:59 +0000188 case ISD::SETUNE: return ARMCC::NE;
189 }
190}
191
192/// DAGIntCCToARMCC - Convert a DAG integer condition code to an ARM CC
193static ARMCC::CondCodes DAGIntCCToARMCC(ISD::CondCode CC) {
194 switch (CC) {
195 default:
196 assert(0 && "Unknown integer condition code!");
197 case ISD::SETEQ: return ARMCC::EQ;
198 case ISD::SETNE: return ARMCC::NE;
199 case ISD::SETLT: return ARMCC::LT;
200 case ISD::SETLE: return ARMCC::LE;
201 case ISD::SETGT: return ARMCC::GT;
202 case ISD::SETGE: return ARMCC::GE;
Rafael Espindolabc4cec92006-09-03 13:19:16 +0000203 case ISD::SETULT: return ARMCC::CC;
Rafael Espindola42b62f32006-10-13 13:14:59 +0000204 case ISD::SETULE: return ARMCC::LS;
205 case ISD::SETUGT: return ARMCC::HI;
206 case ISD::SETUGE: return ARMCC::CS;
Rafael Espindola6f602de2006-08-24 16:13:15 +0000207 }
208}
209
Rafael Espindola462af9a2006-12-05 17:37:31 +0000210std::vector<unsigned> ARMTargetLowering::
211getRegClassForInlineAsmConstraint(const std::string &Constraint,
212 MVT::ValueType VT) const {
213 if (Constraint.size() == 1) {
214 // FIXME: handling only r regs
215 switch (Constraint[0]) {
216 default: break; // Unknown constraint letter
217
218 case 'r': // GENERAL_REGS
219 case 'R': // LEGACY_REGS
220 if (VT == MVT::i32)
221 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
222 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
223 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
224 ARM::R12, ARM::R13, ARM::R14, 0);
225 break;
226
227 }
228 }
229
230 return std::vector<unsigned>();
231}
232
Rafael Espindola84b19be2006-07-16 01:02:57 +0000233const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
234 switch (Opcode) {
235 default: return 0;
236 case ARMISD::CALL: return "ARMISD::CALL";
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000237 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000238 case ARMISD::SELECT: return "ARMISD::SELECT";
239 case ARMISD::CMP: return "ARMISD::CMP";
Rafael Espindola687bc492006-08-24 13:45:55 +0000240 case ARMISD::BR: return "ARMISD::BR";
Rafael Espindola27185192006-09-29 21:20:16 +0000241 case ARMISD::FSITOS: return "ARMISD::FSITOS";
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000242 case ARMISD::FTOSIS: return "ARMISD::FTOSIS";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000243 case ARMISD::FSITOD: return "ARMISD::FSITOD";
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000244 case ARMISD::FTOSID: return "ARMISD::FTOSID";
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000245 case ARMISD::FUITOS: return "ARMISD::FUITOS";
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000246 case ARMISD::FTOUIS: return "ARMISD::FTOUIS";
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000247 case ARMISD::FUITOD: return "ARMISD::FUITOD";
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000248 case ARMISD::FTOUID: return "ARMISD::FTOUID";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000249 case ARMISD::FMRRD: return "ARMISD::FMRRD";
Rafael Espindolaa2845842006-10-05 16:48:49 +0000250 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000251 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Rafael Espindola84b19be2006-07-16 01:02:57 +0000252 }
253}
254
Rafael Espindolaa2845842006-10-05 16:48:49 +0000255class ArgumentLayout {
256 std::vector<bool> is_reg;
257 std::vector<unsigned> pos;
258 std::vector<MVT::ValueType> types;
259public:
Rafael Espindola39b5a212006-10-05 17:46:48 +0000260 ArgumentLayout(const std::vector<MVT::ValueType> &Types) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000261 types = Types;
262
263 unsigned RegNum = 0;
264 unsigned StackOffset = 0;
Rafael Espindola39b5a212006-10-05 17:46:48 +0000265 for(std::vector<MVT::ValueType>::const_iterator I = Types.begin();
Rafael Espindolaa2845842006-10-05 16:48:49 +0000266 I != Types.end();
267 ++I) {
268 MVT::ValueType VT = *I;
269 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
270 unsigned size = MVT::getSizeInBits(VT)/32;
271
272 RegNum = ((RegNum + size - 1) / size) * size;
273 if (RegNum < 4) {
274 pos.push_back(RegNum);
275 is_reg.push_back(true);
276 RegNum += size;
277 } else {
278 unsigned bytes = size * 32/8;
279 StackOffset = ((StackOffset + bytes - 1) / bytes) * bytes;
280 pos.push_back(StackOffset);
281 is_reg.push_back(false);
282 StackOffset += bytes;
283 }
284 }
285 }
286 unsigned getRegisterNum(unsigned argNum) {
287 assert(isRegister(argNum));
288 return pos[argNum];
289 }
290 unsigned getOffset(unsigned argNum) {
291 assert(isOffset(argNum));
292 return pos[argNum];
293 }
294 unsigned isRegister(unsigned argNum) {
295 assert(argNum < is_reg.size());
296 return is_reg[argNum];
297 }
298 unsigned isOffset(unsigned argNum) {
299 return !isRegister(argNum);
300 }
301 MVT::ValueType getType(unsigned argNum) {
302 assert(argNum < types.size());
303 return types[argNum];
304 }
305 unsigned getStackSize(void) {
306 int last = is_reg.size() - 1;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000307 if (last < 0)
308 return 0;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000309 if (isRegister(last))
310 return 0;
311 return getOffset(last) + MVT::getSizeInBits(getType(last))/8;
312 }
313 int lastRegArg(void) {
314 int size = is_reg.size();
315 int last = 0;
316 while(last < size && isRegister(last))
317 last++;
318 last--;
319 return last;
320 }
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000321 int lastRegNum(void) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000322 int l = lastRegArg();
323 if (l < 0)
324 return -1;
325 unsigned r = getRegisterNum(l);
326 MVT::ValueType t = getType(l);
327 assert(t == MVT::i32 || t == MVT::f32 || t == MVT::f64);
328 if (t == MVT::f64)
329 return r + 1;
330 return r;
331 }
332};
333
Rafael Espindola84b19be2006-07-16 01:02:57 +0000334// This transforms a ISD::CALL node into a
335// callseq_star <- ARMISD:CALL <- callseq_end
336// chain
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000337static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola84b19be2006-07-16 01:02:57 +0000338 SDOperand Chain = Op.getOperand(0);
339 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Rafael Espindola5f1b6982006-10-18 12:03:07 +0000340 assert((CallConv == CallingConv::C ||
341 CallConv == CallingConv::Fast)
342 && "unknown calling convention");
Rafael Espindola84b19be2006-07-16 01:02:57 +0000343 SDOperand Callee = Op.getOperand(4);
344 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Rafael Espindola1a009462006-08-08 13:02:29 +0000345 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000346 static const unsigned regs[] = {
Rafael Espindolafac00a92006-07-25 20:17:20 +0000347 ARM::R0, ARM::R1, ARM::R2, ARM::R3
348 };
349
Rafael Espindolaa2845842006-10-05 16:48:49 +0000350 std::vector<MVT::ValueType> Types;
351 for (unsigned i = 0; i < NumOps; ++i) {
352 MVT::ValueType VT = Op.getOperand(5+2*i).getValueType();
353 Types.push_back(VT);
354 }
355 ArgumentLayout Layout(Types);
Rafael Espindolafac00a92006-07-25 20:17:20 +0000356
Rafael Espindolaa2845842006-10-05 16:48:49 +0000357 unsigned NumBytes = Layout.getStackSize();
358
359 Chain = DAG.getCALLSEQ_START(Chain,
360 DAG.getConstant(NumBytes, MVT::i32));
361
362 //Build a sequence of stores
363 std::vector<SDOperand> MemOpChains;
364 for (unsigned i = Layout.lastRegArg() + 1; i < NumOps; ++i) {
365 SDOperand Arg = Op.getOperand(5+2*i);
366 unsigned ArgOffset = Layout.getOffset(i);
367 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
368 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Evan Cheng8b2794a2006-10-13 21:14:26 +0000369 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000370 }
Rafael Espindola1a009462006-08-08 13:02:29 +0000371 if (!MemOpChains.empty())
Chris Lattnere2199452006-08-11 17:38:39 +0000372 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
373 &MemOpChains[0], MemOpChains.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000374
Rafael Espindola0505be02006-10-16 21:10:32 +0000375 // If the callee is a GlobalAddress node (quite common, every direct call is)
376 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
377 // Likewise ExternalSymbol -> TargetExternalSymbol.
378 assert(Callee.getValueType() == MVT::i32);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000379 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Rafael Espindola0505be02006-10-16 21:10:32 +0000380 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
381 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
382 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000383
384 // If this is a direct call, pass the chain and the callee.
385 assert (Callee.Val);
386 std::vector<SDOperand> Ops;
387 Ops.push_back(Chain);
388 Ops.push_back(Callee);
389
Rafael Espindolaa2845842006-10-05 16:48:49 +0000390 // Build a sequence of copy-to-reg nodes chained together with token chain
391 // and flag operands which copy the outgoing args into the appropriate regs.
392 SDOperand InFlag;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000393 for (int i = 0, e = Layout.lastRegArg(); i <= e; ++i) {
Rafael Espindola4a408d42006-10-06 12:50:22 +0000394 SDOperand Arg = Op.getOperand(5+2*i);
395 unsigned RegNum = Layout.getRegisterNum(i);
396 unsigned Reg1 = regs[RegNum];
397 MVT::ValueType VT = Layout.getType(i);
398 assert(VT == Arg.getValueType());
399 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000400
401 // Add argument register to the end of the list so that it is known live
402 // into the call.
Rafael Espindola4a408d42006-10-06 12:50:22 +0000403 Ops.push_back(DAG.getRegister(Reg1, MVT::i32));
404 if (VT == MVT::f64) {
405 unsigned Reg2 = regs[RegNum + 1];
406 SDOperand SDReg1 = DAG.getRegister(Reg1, MVT::i32);
407 SDOperand SDReg2 = DAG.getRegister(Reg2, MVT::i32);
408
409 Ops.push_back(DAG.getRegister(Reg2, MVT::i32));
410 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola935b1f82006-10-06 20:33:26 +0000411 SDOperand Ops[] = {Chain, SDReg1, SDReg2, Arg, InFlag};
412 Chain = DAG.getNode(ARMISD::FMRRD, VTs, Ops, InFlag.Val ? 5 : 4);
Rafael Espindola4a408d42006-10-06 12:50:22 +0000413 } else {
414 if (VT == MVT::f32)
415 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg);
416 Chain = DAG.getCopyToReg(Chain, Reg1, Arg, InFlag);
417 }
418 InFlag = Chain.getValue(1);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000419 }
420
421 std::vector<MVT::ValueType> NodeTys;
422 NodeTys.push_back(MVT::Other); // Returns a chain
423 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Rafael Espindola7a53bd02006-08-09 16:41:12 +0000424
Rafael Espindola84b19be2006-07-16 01:02:57 +0000425 unsigned CallOpc = ARMISD::CALL;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000426 if (InFlag.Val)
427 Ops.push_back(InFlag);
Chris Lattner87428672006-08-11 17:22:35 +0000428 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000429 InFlag = Chain.getValue(1);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000430
Rafael Espindolafac00a92006-07-25 20:17:20 +0000431 std::vector<SDOperand> ResultVals;
432 NodeTys.clear();
433
434 // If the call has results, copy the values out of the ret val registers.
Rafael Espindola614057b2006-10-06 19:10:05 +0000435 MVT::ValueType VT = Op.Val->getValueType(0);
436 if (VT != MVT::Other) {
437 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
Rafael Espindola614057b2006-10-06 19:10:05 +0000438
439 SDOperand Value1 = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
440 Chain = Value1.getValue(1);
441 InFlag = Value1.getValue(2);
Rafael Espindola26a76d12006-10-13 16:47:22 +0000442 NodeTys.push_back(VT);
443 if (VT == MVT::i32) {
444 ResultVals.push_back(Value1);
445 if (Op.Val->getValueType(1) == MVT::i32) {
446 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
447 Chain = Value2.getValue(1);
448 ResultVals.push_back(Value2);
449 NodeTys.push_back(VT);
450 }
451 }
452 if (VT == MVT::f32) {
453 SDOperand Value = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Value1);
454 ResultVals.push_back(Value);
455 }
Rafael Espindola614057b2006-10-06 19:10:05 +0000456 if (VT == MVT::f64) {
457 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
458 Chain = Value2.getValue(1);
Rafael Espindola26a76d12006-10-13 16:47:22 +0000459 SDOperand Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
460 ResultVals.push_back(Value);
Rafael Espindola614057b2006-10-06 19:10:05 +0000461 }
Rafael Espindolafac00a92006-07-25 20:17:20 +0000462 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000463
464 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
465 DAG.getConstant(NumBytes, MVT::i32));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000466 NodeTys.push_back(MVT::Other);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000467
Rafael Espindolafac00a92006-07-25 20:17:20 +0000468 if (ResultVals.empty())
469 return Chain;
470
471 ResultVals.push_back(Chain);
Chris Lattner87428672006-08-11 17:22:35 +0000472 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
473 ResultVals.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000474 return Res.getValue(Op.ResNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000475}
476
477static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
478 SDOperand Copy;
Rafael Espindola4b023672006-06-05 22:26:14 +0000479 SDOperand Chain = Op.getOperand(0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000480 SDOperand R0 = DAG.getRegister(ARM::R0, MVT::i32);
481 SDOperand R1 = DAG.getRegister(ARM::R1, MVT::i32);
482
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000483 switch(Op.getNumOperands()) {
484 default:
485 assert(0 && "Do not know how to return this many arguments!");
486 abort();
Rafael Espindola4b023672006-06-05 22:26:14 +0000487 case 1: {
488 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
Rafael Espindola6312da02006-08-03 22:50:11 +0000489 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
Rafael Espindola4b023672006-06-05 22:26:14 +0000490 }
Rafael Espindola27185192006-09-29 21:20:16 +0000491 case 3: {
492 SDOperand Val = Op.getOperand(1);
493 assert(Val.getValueType() == MVT::i32 ||
Rafael Espindola9e071f02006-10-02 19:30:56 +0000494 Val.getValueType() == MVT::f32 ||
495 Val.getValueType() == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000496
Rafael Espindola9e071f02006-10-02 19:30:56 +0000497 if (Val.getValueType() == MVT::f64) {
498 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
499 SDOperand Ops[] = {Chain, R0, R1, Val};
500 Copy = DAG.getNode(ARMISD::FMRRD, VTs, Ops, 4);
501 } else {
502 if (Val.getValueType() == MVT::f32)
503 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
504 Copy = DAG.getCopyToReg(Chain, R0, Val, SDOperand());
505 }
506
507 if (DAG.getMachineFunction().liveout_empty()) {
Rafael Espindola4b023672006-06-05 22:26:14 +0000508 DAG.getMachineFunction().addLiveOut(ARM::R0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000509 if (Val.getValueType() == MVT::f64)
510 DAG.getMachineFunction().addLiveOut(ARM::R1);
511 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000512 break;
Rafael Espindola27185192006-09-29 21:20:16 +0000513 }
Rafael Espindola3a02f022006-09-04 19:05:01 +0000514 case 5:
515 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
516 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
517 // If we haven't noted the R0+R1 are live out, do so now.
518 if (DAG.getMachineFunction().liveout_empty()) {
519 DAG.getMachineFunction().addLiveOut(ARM::R0);
520 DAG.getMachineFunction().addLiveOut(ARM::R1);
521 }
522 break;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000523 }
Rafael Espindola4b023672006-06-05 22:26:14 +0000524
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000525 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
526 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000527}
528
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000529static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
530 MVT::ValueType PtrVT = Op.getValueType();
531 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengc356a572006-09-12 21:04:05 +0000532 Constant *C = CP->getConstVal();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000533 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
534
535 return CPI;
536}
537
538static SDOperand LowerGlobalAddress(SDOperand Op,
539 SelectionDAG &DAG) {
540 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Rafael Espindola61369da2006-08-14 19:01:24 +0000541 int alignment = 2;
542 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
Evan Cheng466685d2006-10-09 20:57:25 +0000543 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr, NULL, 0);
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000544}
545
Rafael Espindola755be9b2006-08-25 17:55:16 +0000546static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
547 unsigned VarArgsFrameIndex) {
548 // vastart just stores the address of the VarArgsFrameIndex slot into the
549 // memory location argument.
550 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
551 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +0000552 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
553 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), SV->getValue(),
554 SV->getOffset());
Rafael Espindola755be9b2006-08-25 17:55:16 +0000555}
556
557static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
558 int &VarArgsFrameIndex) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000559 MachineFunction &MF = DAG.getMachineFunction();
560 MachineFrameInfo *MFI = MF.getFrameInfo();
561 SSARegMap *RegMap = MF.getSSARegMap();
562 unsigned NumArgs = Op.Val->getNumValues()-1;
563 SDOperand Root = Op.getOperand(0);
564 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
565 static const unsigned REGS[] = {
566 ARM::R0, ARM::R1, ARM::R2, ARM::R3
567 };
568
569 std::vector<MVT::ValueType> Types(Op.Val->value_begin(), Op.Val->value_end() - 1);
570 ArgumentLayout Layout(Types);
571
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000572 std::vector<SDOperand> ArgValues;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000573 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000574 MVT::ValueType VT = Types[ArgNo];
Rafael Espindola4b442b52006-05-23 02:48:20 +0000575
Rafael Espindolaa2845842006-10-05 16:48:49 +0000576 SDOperand Value;
577 if (Layout.isRegister(ArgNo)) {
578 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
579 unsigned RegNum = Layout.getRegisterNum(ArgNo);
580 unsigned Reg1 = REGS[RegNum];
581 unsigned VReg1 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
582 SDOperand Value1 = DAG.getCopyFromReg(Root, VReg1, MVT::i32);
583 MF.addLiveIn(Reg1, VReg1);
584 if (VT == MVT::f64) {
585 unsigned Reg2 = REGS[RegNum + 1];
586 unsigned VReg2 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
587 SDOperand Value2 = DAG.getCopyFromReg(Root, VReg2, MVT::i32);
588 MF.addLiveIn(Reg2, VReg2);
589 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
590 } else {
591 Value = Value1;
592 if (VT == MVT::f32)
593 Value = DAG.getNode(ISD::BIT_CONVERT, VT, Value);
594 }
595 } else {
596 // If the argument is actually used, emit a load from the right stack
597 // slot.
598 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
599 unsigned Offset = Layout.getOffset(ArgNo);
600 unsigned Size = MVT::getSizeInBits(VT)/8;
601 int FI = MFI->CreateFixedObject(Size, Offset);
602 SDOperand FIN = DAG.getFrameIndex(FI, VT);
Evan Cheng466685d2006-10-09 20:57:25 +0000603 Value = DAG.getLoad(VT, Root, FIN, NULL, 0);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000604 } else {
605 Value = DAG.getNode(ISD::UNDEF, VT);
606 }
607 }
608 ArgValues.push_back(Value);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000609 }
610
Rafael Espindolaa2845842006-10-05 16:48:49 +0000611 unsigned NextRegNum = Layout.lastRegNum() + 1;
612
Rafael Espindola755be9b2006-08-25 17:55:16 +0000613 if (isVarArg) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000614 //If this function is vararg we must store the remaing
615 //registers so that they can be acessed with va_start
Rafael Espindola755be9b2006-08-25 17:55:16 +0000616 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000617 -16 + NextRegNum * 4);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000618
Rafael Espindola755be9b2006-08-25 17:55:16 +0000619 SmallVector<SDOperand, 4> MemOps;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000620 for (unsigned RegNo = NextRegNum; RegNo < 4; ++RegNo) {
621 int RegOffset = - (4 - RegNo) * 4;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000622 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000623 RegOffset);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000624 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
625
Rafael Espindolaa2845842006-10-05 16:48:49 +0000626 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
627 MF.addLiveIn(REGS[RegNo], VReg);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000628
629 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
Evan Cheng8b2794a2006-10-13 21:14:26 +0000630 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000631 MemOps.push_back(Store);
632 }
633 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
634 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000635
636 ArgValues.push_back(Root);
637
638 // Return the new list of results.
639 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
640 Op.Val->value_end());
Chris Lattner87428672006-08-11 17:22:35 +0000641 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
Rafael Espindoladc124a22006-05-18 21:45:49 +0000642}
643
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000644static SDOperand GetCMP(ISD::CondCode CC, SDOperand LHS, SDOperand RHS,
645 SelectionDAG &DAG) {
646 MVT::ValueType vt = LHS.getValueType();
Rafael Espindola0d9fe762006-10-10 16:33:47 +0000647 assert(vt == MVT::i32 || vt == MVT::f32 || vt == MVT::f64);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000648
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000649 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
Rafael Espindola42b62f32006-10-13 13:14:59 +0000650
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000651 if (vt != MVT::i32)
652 Cmp = DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp);
653 return Cmp;
654}
655
Rafael Espindola42b62f32006-10-13 13:14:59 +0000656static SDOperand GetARMCC(ISD::CondCode CC, MVT::ValueType vt,
657 SelectionDAG &DAG) {
658 assert(vt == MVT::i32 || vt == MVT::f32 || vt == MVT::f64);
659 if (vt == MVT::i32)
660 return DAG.getConstant(DAGIntCCToARMCC(CC), MVT::i32);
661 else
662 return DAG.getConstant(DAGFPCCToARMCC(CC), MVT::i32);
663}
664
Rafael Espindola8897a7b2006-12-14 18:58:37 +0000665static bool isUInt8Immediate(uint32_t x) {
666 return x < (1 << 8);
667}
668
669static uint32_t rotateL(uint32_t x) {
670 uint32_t bit31 = (x & (1 << 31)) >> 31;
671 uint32_t t = x << 1;
672 return t | bit31;
673}
674
675static bool isRotInt8Immediate(uint32_t x) {
676 int r;
677 for (r = 0; r < 16; r++) {
678 if (isUInt8Immediate(x))
679 return true;
680 x = rotateL(rotateL(x));
681 }
682 return false;
683}
684
685static void LowerCMP(SDOperand &Cmp, SDOperand &ARMCC, SDOperand LHS,
686 SDOperand RHS, ISD::CondCode CC, SelectionDAG &DAG) {
687 MVT::ValueType vt = LHS.getValueType();
688 if (vt == MVT::i32) {
689 assert(!isa<ConstantSDNode>(LHS));
690 if (ConstantSDNode *SD_C = dyn_cast<ConstantSDNode>(RHS.Val)) {
691 uint32_t C = SD_C->getValue();
692
693 uint32_t NC;
694 switch(CC) {
695 default:
696 NC = C; break;
697 case ISD::SETLT:
698 case ISD::SETULT:
699 case ISD::SETGE:
700 case ISD::SETUGE:
701 NC = C - 1; break;
702 case ISD::SETLE:
703 case ISD::SETULE:
704 case ISD::SETGT:
705 case ISD::SETUGT:
706 NC = C + 1; break;
707 }
708
709 ISD::CondCode NCC;
710 switch(CC) {
711 default:
712 NCC = CC; break;
713 case ISD::SETLT:
714 NCC = ISD::SETLE; break;
715 case ISD::SETULT:
716 NCC = ISD::SETULE; break;
717 case ISD::SETGE:
718 NCC = ISD::SETGT; break;
719 case ISD::SETUGE:
720 NCC = ISD::SETUGT; break;
721 case ISD::SETLE:
722 NCC = ISD::SETLT; break;
723 case ISD::SETULE:
724 NCC = ISD::SETULT; break;
725 case ISD::SETGT:
726 NCC = ISD::SETGE; break;
727 case ISD::SETUGT:
728 NCC = ISD::SETUGE; break;
729 }
730
731 if (!isRotInt8Immediate(C) && isRotInt8Immediate(NC)) {
732 RHS = DAG.getConstant(NC, MVT::i32);
733 CC = NCC;
734 }
735 }
736 }
737 Cmp = GetCMP(CC, LHS, RHS, DAG);
738 ARMCC = GetARMCC(CC, vt, DAG);
739}
740
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000741static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
742 SDOperand LHS = Op.getOperand(0);
743 SDOperand RHS = Op.getOperand(1);
744 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
745 SDOperand TrueVal = Op.getOperand(2);
746 SDOperand FalseVal = Op.getOperand(3);
Rafael Espindola8897a7b2006-12-14 18:58:37 +0000747 SDOperand Cmp;
748 SDOperand ARMCC;
749 LowerCMP(Cmp, ARMCC, LHS, RHS, CC, DAG);
Lauro Ramos Venancio301009a2006-12-28 13:11:14 +0000750 return DAG.getNode(ARMISD::SELECT, Op.getValueType(), TrueVal, FalseVal, ARMCC, Cmp);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000751}
752
Rafael Espindola687bc492006-08-24 13:45:55 +0000753static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
754 SDOperand Chain = Op.getOperand(0);
755 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
756 SDOperand LHS = Op.getOperand(2);
757 SDOperand RHS = Op.getOperand(3);
758 SDOperand Dest = Op.getOperand(4);
Rafael Espindola8897a7b2006-12-14 18:58:37 +0000759 SDOperand Cmp;
760 SDOperand ARMCC;
761 LowerCMP(Cmp, ARMCC, LHS, RHS, CC, DAG);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000762 return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, ARMCC, Cmp);
Rafael Espindola687bc492006-08-24 13:45:55 +0000763}
764
Rafael Espindola27185192006-09-29 21:20:16 +0000765static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola9e071f02006-10-02 19:30:56 +0000766 SDOperand IntVal = Op.getOperand(0);
Rafael Espindola27185192006-09-29 21:20:16 +0000767 assert(IntVal.getValueType() == MVT::i32);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000768 MVT::ValueType vt = Op.getValueType();
769 assert(vt == MVT::f32 ||
770 vt == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000771
772 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000773 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FSITOS : ARMISD::FSITOD;
774 return DAG.getNode(op, vt, Tmp);
Rafael Espindola27185192006-09-29 21:20:16 +0000775}
776
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000777static SDOperand LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
778 assert(Op.getValueType() == MVT::i32);
779 SDOperand FloatVal = Op.getOperand(0);
780 MVT::ValueType vt = FloatVal.getValueType();
781 assert(vt == MVT::f32 || vt == MVT::f64);
782
783 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FTOSIS : ARMISD::FTOSID;
784 SDOperand Tmp = DAG.getNode(op, MVT::f32, FloatVal);
785 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Tmp);
786}
787
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000788static SDOperand LowerUINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
789 SDOperand IntVal = Op.getOperand(0);
790 assert(IntVal.getValueType() == MVT::i32);
791 MVT::ValueType vt = Op.getValueType();
792 assert(vt == MVT::f32 ||
793 vt == MVT::f64);
794
795 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
796 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FUITOS : ARMISD::FUITOD;
797 return DAG.getNode(op, vt, Tmp);
798}
799
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000800static SDOperand LowerFP_TO_UINT(SDOperand Op, SelectionDAG &DAG) {
801 assert(Op.getValueType() == MVT::i32);
802 SDOperand FloatVal = Op.getOperand(0);
803 MVT::ValueType vt = FloatVal.getValueType();
804 assert(vt == MVT::f32 || vt == MVT::f64);
805
806 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FTOUIS : ARMISD::FTOUID;
807 SDOperand Tmp = DAG.getNode(op, MVT::f32, FloatVal);
808 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Tmp);
809}
810
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000811SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
812 switch (Op.getOpcode()) {
813 default:
814 assert(0 && "Should not custom lower this!");
Rafael Espindola1c8f0532006-05-15 22:34:39 +0000815 abort();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000816 case ISD::ConstantPool:
817 return LowerConstantPool(Op, DAG);
818 case ISD::GlobalAddress:
819 return LowerGlobalAddress(Op, DAG);
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000820 case ISD::FP_TO_SINT:
821 return LowerFP_TO_SINT(Op, DAG);
Rafael Espindola27185192006-09-29 21:20:16 +0000822 case ISD::SINT_TO_FP:
823 return LowerSINT_TO_FP(Op, DAG);
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000824 case ISD::FP_TO_UINT:
825 return LowerFP_TO_UINT(Op, DAG);
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000826 case ISD::UINT_TO_FP:
827 return LowerUINT_TO_FP(Op, DAG);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000828 case ISD::FORMAL_ARGUMENTS:
Rafael Espindola755be9b2006-08-25 17:55:16 +0000829 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000830 case ISD::CALL:
831 return LowerCALL(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000832 case ISD::RET:
833 return LowerRET(Op, DAG);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000834 case ISD::SELECT_CC:
835 return LowerSELECT_CC(Op, DAG);
Rafael Espindola687bc492006-08-24 13:45:55 +0000836 case ISD::BR_CC:
837 return LowerBR_CC(Op, DAG);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000838 case ISD::VASTART:
839 return LowerVASTART(Op, DAG, VarArgsFrameIndex);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000840 }
841}
842
843//===----------------------------------------------------------------------===//
844// Instruction Selector Implementation
845//===----------------------------------------------------------------------===//
846
847//===--------------------------------------------------------------------===//
848/// ARMDAGToDAGISel - ARM specific code to select ARM machine
849/// instructions for SelectionDAG operations.
850///
851namespace {
852class ARMDAGToDAGISel : public SelectionDAGISel {
853 ARMTargetLowering Lowering;
854
855public:
856 ARMDAGToDAGISel(TargetMachine &TM)
857 : SelectionDAGISel(Lowering), Lowering(TM) {
858 }
859
Evan Cheng9ade2182006-08-26 05:34:46 +0000860 SDNode *Select(SDOperand Op);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000861 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Evan Cheng0d538262006-11-08 20:34:28 +0000862 bool SelectAddrMode1(SDOperand Op, SDOperand N, SDOperand &Arg,
863 SDOperand &Shift, SDOperand &ShiftType);
Rafael Espindolaf64945d2006-12-12 01:03:11 +0000864 bool SelectAddrMode1a(SDOperand Op, SDOperand N, SDOperand &Arg,
865 SDOperand &Shift, SDOperand &ShiftType);
Evan Cheng0d538262006-11-08 20:34:28 +0000866 bool SelectAddrMode2(SDOperand Op, SDOperand N, SDOperand &Arg,
867 SDOperand &Offset);
868 bool SelectAddrMode5(SDOperand Op, SDOperand N, SDOperand &Arg,
869 SDOperand &Offset);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000870
871 // Include the pieces autogenerated from the target description.
872#include "ARMGenDAGISel.inc"
873};
874
875void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
876 DEBUG(BB->dump());
877
878 DAG.setRoot(SelectRoot(DAG.getRoot()));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000879 DAG.RemoveDeadNodes();
880
881 ScheduleAndEmitDAG(DAG);
882}
883
Rafael Espindola61369da2006-08-14 19:01:24 +0000884static bool isInt12Immediate(SDNode *N, short &Imm) {
885 if (N->getOpcode() != ISD::Constant)
886 return false;
887
888 int32_t t = cast<ConstantSDNode>(N)->getValue();
Rafael Espindola7246d332006-09-21 11:29:52 +0000889 int max = 1<<12;
Rafael Espindola61369da2006-08-14 19:01:24 +0000890 int min = -max;
891 if (t > min && t < max) {
892 Imm = t;
893 return true;
894 }
895 else
896 return false;
897}
898
899static bool isInt12Immediate(SDOperand Op, short &Imm) {
900 return isInt12Immediate(Op.Val, Imm);
901}
902
Evan Cheng0d538262006-11-08 20:34:28 +0000903bool ARMDAGToDAGISel::SelectAddrMode1(SDOperand Op,
904 SDOperand N,
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000905 SDOperand &Arg,
906 SDOperand &Shift,
907 SDOperand &ShiftType) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000908 switch(N.getOpcode()) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000909 case ISD::Constant: {
Rafael Espindola7246d332006-09-21 11:29:52 +0000910 uint32_t val = cast<ConstantSDNode>(N)->getValue();
911 if(!isRotInt8Immediate(val)) {
Rafael Espindolaa898ce62006-12-12 17:10:13 +0000912 SDOperand Z = CurDAG->getTargetConstant(0, MVT::i32);
913 SDNode *n;
914 if (isRotInt8Immediate(~val)) {
915 SDOperand C = CurDAG->getTargetConstant(~val, MVT::i32);
916 n = CurDAG->getTargetNode(ARM::MVN, MVT::i32, C, Z, Z);
917 } else {
918 Constant *C = ConstantInt::get(Type::UIntTy, val);
919 int alignment = 2;
920 SDOperand Addr = CurDAG->getTargetConstantPool(C, MVT::i32, alignment);
921 n = CurDAG->getTargetNode(ARM::LDR, MVT::i32, Addr, Z);
922 }
Rafael Espindola7246d332006-09-21 11:29:52 +0000923 Arg = SDOperand(n, 0);
924 } else
925 Arg = CurDAG->getTargetConstant(val, MVT::i32);
926
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000927 Shift = CurDAG->getTargetConstant(0, MVT::i32);
928 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000929 return true;
930 }
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000931 case ISD::SRA:
932 Arg = N.getOperand(0);
933 Shift = N.getOperand(1);
934 ShiftType = CurDAG->getTargetConstant(ARMShift::ASR, MVT::i32);
935 return true;
936 case ISD::SRL:
937 Arg = N.getOperand(0);
938 Shift = N.getOperand(1);
939 ShiftType = CurDAG->getTargetConstant(ARMShift::LSR, MVT::i32);
940 return true;
941 case ISD::SHL:
942 Arg = N.getOperand(0);
943 Shift = N.getOperand(1);
944 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
945 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000946 }
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000947
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000948 Arg = N;
949 Shift = CurDAG->getTargetConstant(0, MVT::i32);
950 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000951 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000952}
953
Evan Cheng0d538262006-11-08 20:34:28 +0000954bool ARMDAGToDAGISel::SelectAddrMode2(SDOperand Op, SDOperand N,
955 SDOperand &Arg, SDOperand &Offset) {
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000956 //TODO: complete and cleanup!
957 SDOperand Zero = CurDAG->getTargetConstant(0, MVT::i32);
958 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
959 Arg = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
960 Offset = Zero;
961 return true;
962 }
963 if (N.getOpcode() == ISD::ADD) {
964 short imm = 0;
965 if (isInt12Immediate(N.getOperand(1), imm)) {
966 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
967 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
968 Arg = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
969 } else {
970 Arg = N.getOperand(0);
971 }
972 return true; // [r+i]
973 }
974 }
975 Offset = Zero;
976 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
977 Arg = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
978 else
979 Arg = N;
980 return true;
981}
982
Evan Cheng0d538262006-11-08 20:34:28 +0000983bool ARMDAGToDAGISel::SelectAddrMode5(SDOperand Op,
984 SDOperand N, SDOperand &Arg,
Rafael Espindola32bd5f42006-10-17 18:04:53 +0000985 SDOperand &Offset) {
986 //TODO: detect offset
987 Offset = CurDAG->getTargetConstant(0, MVT::i32);
988 Arg = N;
989 return true;
990}
991
Evan Cheng9ade2182006-08-26 05:34:46 +0000992SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000993 SDNode *N = Op.Val;
994
995 switch (N->getOpcode()) {
996 default:
Evan Cheng9ade2182006-08-26 05:34:46 +0000997 return SelectCode(Op);
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000998 break;
Rafael Espindolaf819a492006-11-09 13:58:55 +0000999 case ISD::FrameIndex: {
1000 int FI = cast<FrameIndexSDNode>(N)->getIndex();
1001 SDOperand Ops[] = {CurDAG->getTargetFrameIndex(FI, MVT::i32),
1002 CurDAG->getTargetConstant(0, MVT::i32),
1003 CurDAG->getTargetConstant(0, MVT::i32),
1004 CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32)};
1005
1006 return CurDAG->SelectNodeTo(N, ARM::ADD, MVT::i32, Ops,
1007 sizeof(Ops)/sizeof(SDOperand));
1008 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +00001009 }
Rafael Espindolaf819a492006-11-09 13:58:55 +00001010 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001011}
1012
1013} // end anonymous namespace
1014
1015/// createARMISelDag - This pass converts a legalized DAG into a
1016/// ARM-specific DAG, ready for instruction scheduling.
1017///
1018FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
1019 return new ARMDAGToDAGISel(TM);
1020}