blob: cf8386ce5175527b88cc00751a27f4be9ef62a8e [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 Espindola9985f9f2006-12-31 18:52:39 +0000154static std::vector<unsigned> 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 Espindola9985f9f2006-12-31 18:52:39 +0000160 case ISD::SETOEQ: return make_vector<unsigned>(ARMCC::EQ, 0);
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 Espindola9985f9f2006-12-31 18:52:39 +0000163 case ISD::SETOGT: return make_vector<unsigned>(ARMCC::GT, 0);
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000164// SETOGE = (N | Z | !V) & !N = (Z | !V) & !N = !V & !N = GE
165 case ISD::SETGE:
Rafael Espindola9985f9f2006-12-31 18:52:39 +0000166 case ISD::SETOGE: return make_vector<unsigned>(ARMCC::GE, 0);
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000167// SETOLT = (N | Z | !V) & N = N = MI
168 case ISD::SETLT:
Rafael Espindola9985f9f2006-12-31 18:52:39 +0000169 case ISD::SETOLT: return make_vector<unsigned>(ARMCC::MI, 0);
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000170// SETOLE = (N | Z | !V) & (N | Z) = N | Z = !C | Z = LS
171 case ISD::SETLE:
Rafael Espindola9985f9f2006-12-31 18:52:39 +0000172 case ISD::SETOLE: return make_vector<unsigned>(ARMCC::LS, 0);
173// SETONE = OGT | OLT
174 case ISD::SETONE: return make_vector<unsigned>(ARMCC::GT, ARMCC::MI, 0);
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000175// SETO = N | Z | !V = Z | !V = !V = VC
Rafael Espindola9985f9f2006-12-31 18:52:39 +0000176 case ISD::SETO: return make_vector<unsigned>(ARMCC::VC, 0);
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000177// SETUO = V = VS
Rafael Espindola9985f9f2006-12-31 18:52:39 +0000178 case ISD::SETUO: return make_vector<unsigned>(ARMCC::VS, 0);
179// SETUEQ = V | Z (need two instructions) = EQ/VS
180 case ISD::SETUEQ: return make_vector<unsigned>(ARMCC::EQ, ARMCC::VS, 0);
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000181// SETUGT = V | (!Z & !N) = !Z & !N = !Z & C = HI
Rafael Espindola9985f9f2006-12-31 18:52:39 +0000182 case ISD::SETUGT: return make_vector<unsigned>(ARMCC::HI, 0);
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000183// SETUGE = V | !N = !N = PL
Rafael Espindola9985f9f2006-12-31 18:52:39 +0000184 case ISD::SETUGE: return make_vector<unsigned>(ARMCC::PL, 0);
185// SETULT = V | N = LT
186 case ISD::SETULT: return make_vector<unsigned>(ARMCC::LT, 0);
187// SETULE = V | Z | N = LE
188 case ISD::SETULE: return make_vector<unsigned>(ARMCC::LE, 0);
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000189// SETUNE = V | !Z = !Z = NE
Rafael Espindola9985f9f2006-12-31 18:52:39 +0000190 case ISD::SETNE:
191 case ISD::SETUNE: return make_vector<unsigned>(ARMCC::NE, 0);
Rafael Espindola42b62f32006-10-13 13:14:59 +0000192 }
193}
194
195/// DAGIntCCToARMCC - Convert a DAG integer condition code to an ARM CC
Rafael Espindola9985f9f2006-12-31 18:52:39 +0000196static std::vector<unsigned> DAGIntCCToARMCC(ISD::CondCode CC) {
Rafael Espindola42b62f32006-10-13 13:14:59 +0000197 switch (CC) {
198 default:
199 assert(0 && "Unknown integer condition code!");
Rafael Espindola9985f9f2006-12-31 18:52:39 +0000200 case ISD::SETEQ: return make_vector<unsigned>(ARMCC::EQ, 0);
201 case ISD::SETNE: return make_vector<unsigned>(ARMCC::NE, 0);
202 case ISD::SETLT: return make_vector<unsigned>(ARMCC::LT, 0);
203 case ISD::SETLE: return make_vector<unsigned>(ARMCC::LE, 0);
204 case ISD::SETGT: return make_vector<unsigned>(ARMCC::GT, 0);
205 case ISD::SETGE: return make_vector<unsigned>(ARMCC::GE, 0);
206 case ISD::SETULT: return make_vector<unsigned>(ARMCC::CC, 0);
207 case ISD::SETULE: return make_vector<unsigned>(ARMCC::LS, 0);
208 case ISD::SETUGT: return make_vector<unsigned>(ARMCC::HI, 0);
209 case ISD::SETUGE: return make_vector<unsigned>(ARMCC::CS, 0);
Rafael Espindola6f602de2006-08-24 16:13:15 +0000210 }
211}
212
Rafael Espindola462af9a2006-12-05 17:37:31 +0000213std::vector<unsigned> ARMTargetLowering::
214getRegClassForInlineAsmConstraint(const std::string &Constraint,
215 MVT::ValueType VT) const {
216 if (Constraint.size() == 1) {
217 // FIXME: handling only r regs
218 switch (Constraint[0]) {
219 default: break; // Unknown constraint letter
220
221 case 'r': // GENERAL_REGS
222 case 'R': // LEGACY_REGS
223 if (VT == MVT::i32)
224 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
225 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
226 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
227 ARM::R12, ARM::R13, ARM::R14, 0);
228 break;
229
230 }
231 }
232
233 return std::vector<unsigned>();
234}
235
Rafael Espindola84b19be2006-07-16 01:02:57 +0000236const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
237 switch (Opcode) {
238 default: return 0;
239 case ARMISD::CALL: return "ARMISD::CALL";
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000240 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000241 case ARMISD::SELECT: return "ARMISD::SELECT";
242 case ARMISD::CMP: return "ARMISD::CMP";
Rafael Espindola687bc492006-08-24 13:45:55 +0000243 case ARMISD::BR: return "ARMISD::BR";
Rafael Espindola27185192006-09-29 21:20:16 +0000244 case ARMISD::FSITOS: return "ARMISD::FSITOS";
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000245 case ARMISD::FTOSIS: return "ARMISD::FTOSIS";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000246 case ARMISD::FSITOD: return "ARMISD::FSITOD";
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000247 case ARMISD::FTOSID: return "ARMISD::FTOSID";
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000248 case ARMISD::FUITOS: return "ARMISD::FUITOS";
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000249 case ARMISD::FTOUIS: return "ARMISD::FTOUIS";
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000250 case ARMISD::FUITOD: return "ARMISD::FUITOD";
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000251 case ARMISD::FTOUID: return "ARMISD::FTOUID";
Rafael Espindola9e071f02006-10-02 19:30:56 +0000252 case ARMISD::FMRRD: return "ARMISD::FMRRD";
Rafael Espindolaa2845842006-10-05 16:48:49 +0000253 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000254 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Rafael Espindola84b19be2006-07-16 01:02:57 +0000255 }
256}
257
Rafael Espindolaa2845842006-10-05 16:48:49 +0000258class ArgumentLayout {
259 std::vector<bool> is_reg;
260 std::vector<unsigned> pos;
261 std::vector<MVT::ValueType> types;
262public:
Rafael Espindola39b5a212006-10-05 17:46:48 +0000263 ArgumentLayout(const std::vector<MVT::ValueType> &Types) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000264 types = Types;
265
266 unsigned RegNum = 0;
267 unsigned StackOffset = 0;
Rafael Espindola39b5a212006-10-05 17:46:48 +0000268 for(std::vector<MVT::ValueType>::const_iterator I = Types.begin();
Rafael Espindolaa2845842006-10-05 16:48:49 +0000269 I != Types.end();
270 ++I) {
271 MVT::ValueType VT = *I;
272 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
273 unsigned size = MVT::getSizeInBits(VT)/32;
274
275 RegNum = ((RegNum + size - 1) / size) * size;
276 if (RegNum < 4) {
277 pos.push_back(RegNum);
278 is_reg.push_back(true);
279 RegNum += size;
280 } else {
281 unsigned bytes = size * 32/8;
282 StackOffset = ((StackOffset + bytes - 1) / bytes) * bytes;
283 pos.push_back(StackOffset);
284 is_reg.push_back(false);
285 StackOffset += bytes;
286 }
287 }
288 }
289 unsigned getRegisterNum(unsigned argNum) {
290 assert(isRegister(argNum));
291 return pos[argNum];
292 }
293 unsigned getOffset(unsigned argNum) {
294 assert(isOffset(argNum));
295 return pos[argNum];
296 }
297 unsigned isRegister(unsigned argNum) {
298 assert(argNum < is_reg.size());
299 return is_reg[argNum];
300 }
301 unsigned isOffset(unsigned argNum) {
302 return !isRegister(argNum);
303 }
304 MVT::ValueType getType(unsigned argNum) {
305 assert(argNum < types.size());
306 return types[argNum];
307 }
308 unsigned getStackSize(void) {
309 int last = is_reg.size() - 1;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000310 if (last < 0)
311 return 0;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000312 if (isRegister(last))
313 return 0;
314 return getOffset(last) + MVT::getSizeInBits(getType(last))/8;
315 }
316 int lastRegArg(void) {
317 int size = is_reg.size();
318 int last = 0;
319 while(last < size && isRegister(last))
320 last++;
321 last--;
322 return last;
323 }
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000324 int lastRegNum(void) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000325 int l = lastRegArg();
326 if (l < 0)
327 return -1;
328 unsigned r = getRegisterNum(l);
329 MVT::ValueType t = getType(l);
330 assert(t == MVT::i32 || t == MVT::f32 || t == MVT::f64);
331 if (t == MVT::f64)
332 return r + 1;
333 return r;
334 }
335};
336
Rafael Espindola84b19be2006-07-16 01:02:57 +0000337// This transforms a ISD::CALL node into a
338// callseq_star <- ARMISD:CALL <- callseq_end
339// chain
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000340static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola84b19be2006-07-16 01:02:57 +0000341 SDOperand Chain = Op.getOperand(0);
342 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Rafael Espindola5f1b6982006-10-18 12:03:07 +0000343 assert((CallConv == CallingConv::C ||
344 CallConv == CallingConv::Fast)
345 && "unknown calling convention");
Rafael Espindola84b19be2006-07-16 01:02:57 +0000346 SDOperand Callee = Op.getOperand(4);
347 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Rafael Espindola1a009462006-08-08 13:02:29 +0000348 SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000349 static const unsigned regs[] = {
Rafael Espindolafac00a92006-07-25 20:17:20 +0000350 ARM::R0, ARM::R1, ARM::R2, ARM::R3
351 };
352
Rafael Espindolaa2845842006-10-05 16:48:49 +0000353 std::vector<MVT::ValueType> Types;
354 for (unsigned i = 0; i < NumOps; ++i) {
355 MVT::ValueType VT = Op.getOperand(5+2*i).getValueType();
356 Types.push_back(VT);
357 }
358 ArgumentLayout Layout(Types);
Rafael Espindolafac00a92006-07-25 20:17:20 +0000359
Rafael Espindolaa2845842006-10-05 16:48:49 +0000360 unsigned NumBytes = Layout.getStackSize();
361
362 Chain = DAG.getCALLSEQ_START(Chain,
363 DAG.getConstant(NumBytes, MVT::i32));
364
365 //Build a sequence of stores
366 std::vector<SDOperand> MemOpChains;
367 for (unsigned i = Layout.lastRegArg() + 1; i < NumOps; ++i) {
368 SDOperand Arg = Op.getOperand(5+2*i);
369 unsigned ArgOffset = Layout.getOffset(i);
370 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
371 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Evan Cheng8b2794a2006-10-13 21:14:26 +0000372 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000373 }
Rafael Espindola1a009462006-08-08 13:02:29 +0000374 if (!MemOpChains.empty())
Chris Lattnere2199452006-08-11 17:38:39 +0000375 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
376 &MemOpChains[0], MemOpChains.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000377
Rafael Espindola0505be02006-10-16 21:10:32 +0000378 // If the callee is a GlobalAddress node (quite common, every direct call is)
379 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
380 // Likewise ExternalSymbol -> TargetExternalSymbol.
381 assert(Callee.getValueType() == MVT::i32);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000382 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Rafael Espindola0505be02006-10-16 21:10:32 +0000383 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
384 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
385 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000386
387 // If this is a direct call, pass the chain and the callee.
388 assert (Callee.Val);
389 std::vector<SDOperand> Ops;
390 Ops.push_back(Chain);
391 Ops.push_back(Callee);
392
Rafael Espindolaa2845842006-10-05 16:48:49 +0000393 // Build a sequence of copy-to-reg nodes chained together with token chain
394 // and flag operands which copy the outgoing args into the appropriate regs.
395 SDOperand InFlag;
Rafael Espindolaaf1dabe2006-10-06 17:26:30 +0000396 for (int i = 0, e = Layout.lastRegArg(); i <= e; ++i) {
Rafael Espindola4a408d42006-10-06 12:50:22 +0000397 SDOperand Arg = Op.getOperand(5+2*i);
398 unsigned RegNum = Layout.getRegisterNum(i);
399 unsigned Reg1 = regs[RegNum];
400 MVT::ValueType VT = Layout.getType(i);
401 assert(VT == Arg.getValueType());
402 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000403
404 // Add argument register to the end of the list so that it is known live
405 // into the call.
Rafael Espindola4a408d42006-10-06 12:50:22 +0000406 Ops.push_back(DAG.getRegister(Reg1, MVT::i32));
407 if (VT == MVT::f64) {
408 unsigned Reg2 = regs[RegNum + 1];
409 SDOperand SDReg1 = DAG.getRegister(Reg1, MVT::i32);
410 SDOperand SDReg2 = DAG.getRegister(Reg2, MVT::i32);
411
412 Ops.push_back(DAG.getRegister(Reg2, MVT::i32));
413 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola935b1f82006-10-06 20:33:26 +0000414 SDOperand Ops[] = {Chain, SDReg1, SDReg2, Arg, InFlag};
415 Chain = DAG.getNode(ARMISD::FMRRD, VTs, Ops, InFlag.Val ? 5 : 4);
Rafael Espindola4a408d42006-10-06 12:50:22 +0000416 } else {
417 if (VT == MVT::f32)
418 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg);
419 Chain = DAG.getCopyToReg(Chain, Reg1, Arg, InFlag);
420 }
421 InFlag = Chain.getValue(1);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000422 }
423
424 std::vector<MVT::ValueType> NodeTys;
425 NodeTys.push_back(MVT::Other); // Returns a chain
426 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Rafael Espindola7a53bd02006-08-09 16:41:12 +0000427
Rafael Espindola84b19be2006-07-16 01:02:57 +0000428 unsigned CallOpc = ARMISD::CALL;
Rafael Espindolafac00a92006-07-25 20:17:20 +0000429 if (InFlag.Val)
430 Ops.push_back(InFlag);
Chris Lattner87428672006-08-11 17:22:35 +0000431 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000432 InFlag = Chain.getValue(1);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000433
Rafael Espindolafac00a92006-07-25 20:17:20 +0000434 std::vector<SDOperand> ResultVals;
435 NodeTys.clear();
436
437 // If the call has results, copy the values out of the ret val registers.
Rafael Espindola614057b2006-10-06 19:10:05 +0000438 MVT::ValueType VT = Op.Val->getValueType(0);
439 if (VT != MVT::Other) {
440 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
Rafael Espindola614057b2006-10-06 19:10:05 +0000441
442 SDOperand Value1 = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
443 Chain = Value1.getValue(1);
444 InFlag = Value1.getValue(2);
Rafael Espindola26a76d12006-10-13 16:47:22 +0000445 NodeTys.push_back(VT);
446 if (VT == MVT::i32) {
447 ResultVals.push_back(Value1);
448 if (Op.Val->getValueType(1) == MVT::i32) {
449 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
450 Chain = Value2.getValue(1);
451 ResultVals.push_back(Value2);
452 NodeTys.push_back(VT);
453 }
454 }
455 if (VT == MVT::f32) {
456 SDOperand Value = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Value1);
457 ResultVals.push_back(Value);
458 }
Rafael Espindola614057b2006-10-06 19:10:05 +0000459 if (VT == MVT::f64) {
460 SDOperand Value2 = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, InFlag);
461 Chain = Value2.getValue(1);
Rafael Espindola26a76d12006-10-13 16:47:22 +0000462 SDOperand Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
463 ResultVals.push_back(Value);
Rafael Espindola614057b2006-10-06 19:10:05 +0000464 }
Rafael Espindolafac00a92006-07-25 20:17:20 +0000465 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000466
467 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
468 DAG.getConstant(NumBytes, MVT::i32));
Rafael Espindolafac00a92006-07-25 20:17:20 +0000469 NodeTys.push_back(MVT::Other);
Rafael Espindola84b19be2006-07-16 01:02:57 +0000470
Rafael Espindolafac00a92006-07-25 20:17:20 +0000471 if (ResultVals.empty())
472 return Chain;
473
474 ResultVals.push_back(Chain);
Chris Lattner87428672006-08-11 17:22:35 +0000475 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
476 ResultVals.size());
Rafael Espindolafac00a92006-07-25 20:17:20 +0000477 return Res.getValue(Op.ResNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000478}
479
480static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
481 SDOperand Copy;
Rafael Espindola4b023672006-06-05 22:26:14 +0000482 SDOperand Chain = Op.getOperand(0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000483 SDOperand R0 = DAG.getRegister(ARM::R0, MVT::i32);
484 SDOperand R1 = DAG.getRegister(ARM::R1, MVT::i32);
485
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000486 switch(Op.getNumOperands()) {
487 default:
488 assert(0 && "Do not know how to return this many arguments!");
489 abort();
Rafael Espindola4b023672006-06-05 22:26:14 +0000490 case 1: {
491 SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
Rafael Espindola6312da02006-08-03 22:50:11 +0000492 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
Rafael Espindola4b023672006-06-05 22:26:14 +0000493 }
Rafael Espindola27185192006-09-29 21:20:16 +0000494 case 3: {
495 SDOperand Val = Op.getOperand(1);
496 assert(Val.getValueType() == MVT::i32 ||
Rafael Espindola9e071f02006-10-02 19:30:56 +0000497 Val.getValueType() == MVT::f32 ||
498 Val.getValueType() == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000499
Rafael Espindola9e071f02006-10-02 19:30:56 +0000500 if (Val.getValueType() == MVT::f64) {
501 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
502 SDOperand Ops[] = {Chain, R0, R1, Val};
503 Copy = DAG.getNode(ARMISD::FMRRD, VTs, Ops, 4);
504 } else {
505 if (Val.getValueType() == MVT::f32)
506 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
507 Copy = DAG.getCopyToReg(Chain, R0, Val, SDOperand());
508 }
509
510 if (DAG.getMachineFunction().liveout_empty()) {
Rafael Espindola4b023672006-06-05 22:26:14 +0000511 DAG.getMachineFunction().addLiveOut(ARM::R0);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000512 if (Val.getValueType() == MVT::f64)
513 DAG.getMachineFunction().addLiveOut(ARM::R1);
514 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000515 break;
Rafael Espindola27185192006-09-29 21:20:16 +0000516 }
Rafael Espindola3a02f022006-09-04 19:05:01 +0000517 case 5:
518 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
519 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
520 // If we haven't noted the R0+R1 are live out, do so now.
521 if (DAG.getMachineFunction().liveout_empty()) {
522 DAG.getMachineFunction().addLiveOut(ARM::R0);
523 DAG.getMachineFunction().addLiveOut(ARM::R1);
524 }
525 break;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000526 }
Rafael Espindola4b023672006-06-05 22:26:14 +0000527
Rafael Espindolaf4fda802006-08-03 17:02:20 +0000528 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
529 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000530}
531
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000532static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
533 MVT::ValueType PtrVT = Op.getValueType();
534 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengc356a572006-09-12 21:04:05 +0000535 Constant *C = CP->getConstVal();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000536 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
537
538 return CPI;
539}
540
541static SDOperand LowerGlobalAddress(SDOperand Op,
542 SelectionDAG &DAG) {
543 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Rafael Espindola61369da2006-08-14 19:01:24 +0000544 int alignment = 2;
545 SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
Evan Cheng466685d2006-10-09 20:57:25 +0000546 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr, NULL, 0);
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000547}
548
Rafael Espindola755be9b2006-08-25 17:55:16 +0000549static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
550 unsigned VarArgsFrameIndex) {
551 // vastart just stores the address of the VarArgsFrameIndex slot into the
552 // memory location argument.
553 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
554 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +0000555 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
556 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), SV->getValue(),
557 SV->getOffset());
Rafael Espindola755be9b2006-08-25 17:55:16 +0000558}
559
560static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
561 int &VarArgsFrameIndex) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000562 MachineFunction &MF = DAG.getMachineFunction();
563 MachineFrameInfo *MFI = MF.getFrameInfo();
564 SSARegMap *RegMap = MF.getSSARegMap();
565 unsigned NumArgs = Op.Val->getNumValues()-1;
566 SDOperand Root = Op.getOperand(0);
567 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
568 static const unsigned REGS[] = {
569 ARM::R0, ARM::R1, ARM::R2, ARM::R3
570 };
571
572 std::vector<MVT::ValueType> Types(Op.Val->value_begin(), Op.Val->value_end() - 1);
573 ArgumentLayout Layout(Types);
574
Rafael Espindola337c4ad62006-06-12 12:28:08 +0000575 std::vector<SDOperand> ArgValues;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000576 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000577 MVT::ValueType VT = Types[ArgNo];
Rafael Espindola4b442b52006-05-23 02:48:20 +0000578
Rafael Espindolaa2845842006-10-05 16:48:49 +0000579 SDOperand Value;
580 if (Layout.isRegister(ArgNo)) {
581 assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
582 unsigned RegNum = Layout.getRegisterNum(ArgNo);
583 unsigned Reg1 = REGS[RegNum];
584 unsigned VReg1 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
585 SDOperand Value1 = DAG.getCopyFromReg(Root, VReg1, MVT::i32);
586 MF.addLiveIn(Reg1, VReg1);
587 if (VT == MVT::f64) {
588 unsigned Reg2 = REGS[RegNum + 1];
589 unsigned VReg2 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
590 SDOperand Value2 = DAG.getCopyFromReg(Root, VReg2, MVT::i32);
591 MF.addLiveIn(Reg2, VReg2);
592 Value = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
593 } else {
594 Value = Value1;
595 if (VT == MVT::f32)
596 Value = DAG.getNode(ISD::BIT_CONVERT, VT, Value);
597 }
598 } else {
599 // If the argument is actually used, emit a load from the right stack
600 // slot.
601 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
602 unsigned Offset = Layout.getOffset(ArgNo);
603 unsigned Size = MVT::getSizeInBits(VT)/8;
604 int FI = MFI->CreateFixedObject(Size, Offset);
605 SDOperand FIN = DAG.getFrameIndex(FI, VT);
Evan Cheng466685d2006-10-09 20:57:25 +0000606 Value = DAG.getLoad(VT, Root, FIN, NULL, 0);
Rafael Espindolaa2845842006-10-05 16:48:49 +0000607 } else {
608 Value = DAG.getNode(ISD::UNDEF, VT);
609 }
610 }
611 ArgValues.push_back(Value);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000612 }
613
Rafael Espindolaa2845842006-10-05 16:48:49 +0000614 unsigned NextRegNum = Layout.lastRegNum() + 1;
615
Rafael Espindola755be9b2006-08-25 17:55:16 +0000616 if (isVarArg) {
Rafael Espindolaa2845842006-10-05 16:48:49 +0000617 //If this function is vararg we must store the remaing
618 //registers so that they can be acessed with va_start
Rafael Espindola755be9b2006-08-25 17:55:16 +0000619 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000620 -16 + NextRegNum * 4);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000621
Rafael Espindola755be9b2006-08-25 17:55:16 +0000622 SmallVector<SDOperand, 4> MemOps;
Rafael Espindolaa2845842006-10-05 16:48:49 +0000623 for (unsigned RegNo = NextRegNum; RegNo < 4; ++RegNo) {
624 int RegOffset = - (4 - RegNo) * 4;
Rafael Espindola755be9b2006-08-25 17:55:16 +0000625 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
Rafael Espindolaa2845842006-10-05 16:48:49 +0000626 RegOffset);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000627 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
628
Rafael Espindolaa2845842006-10-05 16:48:49 +0000629 unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
630 MF.addLiveIn(REGS[RegNo], VReg);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000631
632 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
Evan Cheng8b2794a2006-10-13 21:14:26 +0000633 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000634 MemOps.push_back(Store);
635 }
636 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
637 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000638
639 ArgValues.push_back(Root);
640
641 // Return the new list of results.
642 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
643 Op.Val->value_end());
Chris Lattner87428672006-08-11 17:22:35 +0000644 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
Rafael Espindoladc124a22006-05-18 21:45:49 +0000645}
646
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000647static SDOperand GetCMP(ISD::CondCode CC, SDOperand LHS, SDOperand RHS,
648 SelectionDAG &DAG) {
649 MVT::ValueType vt = LHS.getValueType();
Rafael Espindola0d9fe762006-10-10 16:33:47 +0000650 assert(vt == MVT::i32 || vt == MVT::f32 || vt == MVT::f64);
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000651
Rafael Espindola6c5ae3e2006-10-14 13:42:53 +0000652 SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
Rafael Espindola42b62f32006-10-13 13:14:59 +0000653
Rafael Espindola4b20fbc2006-10-10 12:56:00 +0000654 if (vt != MVT::i32)
655 Cmp = DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp);
656 return Cmp;
657}
658
Rafael Espindola9985f9f2006-12-31 18:52:39 +0000659static std::vector<SDOperand> GetARMCC(ISD::CondCode CC, MVT::ValueType vt,
Rafael Espindola42b62f32006-10-13 13:14:59 +0000660 SelectionDAG &DAG) {
661 assert(vt == MVT::i32 || vt == MVT::f32 || vt == MVT::f64);
Rafael Espindola9985f9f2006-12-31 18:52:39 +0000662 std::vector<unsigned> vcc;
Rafael Espindola42b62f32006-10-13 13:14:59 +0000663 if (vt == MVT::i32)
Rafael Espindola9985f9f2006-12-31 18:52:39 +0000664 vcc = DAGIntCCToARMCC(CC);
Rafael Espindola42b62f32006-10-13 13:14:59 +0000665 else
Rafael Espindola9985f9f2006-12-31 18:52:39 +0000666 vcc = DAGFPCCToARMCC(CC);
667
668 std::vector<unsigned>::iterator it;
669 std::vector<SDOperand> result;
670 for( it = vcc.begin(); it != vcc.end(); it++ )
671 result.push_back(DAG.getConstant(*it,MVT::i32));
672 return result;
Rafael Espindola42b62f32006-10-13 13:14:59 +0000673}
674
Rafael Espindola8897a7b2006-12-14 18:58:37 +0000675static bool isUInt8Immediate(uint32_t x) {
676 return x < (1 << 8);
677}
678
679static uint32_t rotateL(uint32_t x) {
680 uint32_t bit31 = (x & (1 << 31)) >> 31;
681 uint32_t t = x << 1;
682 return t | bit31;
683}
684
685static bool isRotInt8Immediate(uint32_t x) {
686 int r;
687 for (r = 0; r < 16; r++) {
688 if (isUInt8Immediate(x))
689 return true;
690 x = rotateL(rotateL(x));
691 }
692 return false;
693}
694
Rafael Espindola9985f9f2006-12-31 18:52:39 +0000695static void LowerCMP(SDOperand &Cmp, std::vector<SDOperand> &ARMCC,
696 SDOperand LHS, SDOperand RHS, ISD::CondCode CC,
697 SelectionDAG &DAG) {
Rafael Espindola8897a7b2006-12-14 18:58:37 +0000698 MVT::ValueType vt = LHS.getValueType();
699 if (vt == MVT::i32) {
700 assert(!isa<ConstantSDNode>(LHS));
701 if (ConstantSDNode *SD_C = dyn_cast<ConstantSDNode>(RHS.Val)) {
702 uint32_t C = SD_C->getValue();
703
704 uint32_t NC;
705 switch(CC) {
706 default:
707 NC = C; break;
708 case ISD::SETLT:
709 case ISD::SETULT:
710 case ISD::SETGE:
711 case ISD::SETUGE:
712 NC = C - 1; break;
713 case ISD::SETLE:
714 case ISD::SETULE:
715 case ISD::SETGT:
716 case ISD::SETUGT:
717 NC = C + 1; break;
718 }
719
720 ISD::CondCode NCC;
721 switch(CC) {
722 default:
723 NCC = CC; break;
724 case ISD::SETLT:
725 NCC = ISD::SETLE; break;
726 case ISD::SETULT:
727 NCC = ISD::SETULE; break;
728 case ISD::SETGE:
729 NCC = ISD::SETGT; break;
730 case ISD::SETUGE:
731 NCC = ISD::SETUGT; break;
732 case ISD::SETLE:
733 NCC = ISD::SETLT; break;
734 case ISD::SETULE:
735 NCC = ISD::SETULT; break;
736 case ISD::SETGT:
737 NCC = ISD::SETGE; break;
738 case ISD::SETUGT:
739 NCC = ISD::SETUGE; break;
740 }
741
742 if (!isRotInt8Immediate(C) && isRotInt8Immediate(NC)) {
743 RHS = DAG.getConstant(NC, MVT::i32);
744 CC = NCC;
745 }
746 }
747 }
748 Cmp = GetCMP(CC, LHS, RHS, DAG);
749 ARMCC = GetARMCC(CC, vt, DAG);
750}
751
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000752static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
753 SDOperand LHS = Op.getOperand(0);
754 SDOperand RHS = Op.getOperand(1);
755 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
756 SDOperand TrueVal = Op.getOperand(2);
757 SDOperand FalseVal = Op.getOperand(3);
Rafael Espindola8897a7b2006-12-14 18:58:37 +0000758 SDOperand Cmp;
Rafael Espindola9985f9f2006-12-31 18:52:39 +0000759 std::vector<SDOperand> ARMCC;
Rafael Espindola8897a7b2006-12-14 18:58:37 +0000760 LowerCMP(Cmp, ARMCC, LHS, RHS, CC, DAG);
Rafael Espindola9985f9f2006-12-31 18:52:39 +0000761
762 SDOperand Aux = FalseVal;
763 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
764 std::vector<SDOperand>::iterator it;
765 for (it = ARMCC.begin(); it != ARMCC.end(); ++it){
766 SDOperand Flag = it == ARMCC.begin() ? Cmp : Aux.getValue(1);
767 SDOperand Ops[] = {TrueVal, Aux, *it, Flag};
768 Aux = DAG.getNode(ARMISD::SELECT, VTs, Ops, 4);
769 }
770 return Aux;
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000771}
772
Rafael Espindola687bc492006-08-24 13:45:55 +0000773static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
774 SDOperand Chain = Op.getOperand(0);
775 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
776 SDOperand LHS = Op.getOperand(2);
777 SDOperand RHS = Op.getOperand(3);
778 SDOperand Dest = Op.getOperand(4);
Rafael Espindola8897a7b2006-12-14 18:58:37 +0000779 SDOperand Cmp;
Rafael Espindola9985f9f2006-12-31 18:52:39 +0000780 std::vector<SDOperand> ARMCC;
Rafael Espindola8897a7b2006-12-14 18:58:37 +0000781 LowerCMP(Cmp, ARMCC, LHS, RHS, CC, DAG);
Rafael Espindola9985f9f2006-12-31 18:52:39 +0000782
783 SDOperand Aux = Chain;
784 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Flag);
785 std::vector<SDOperand>::iterator it;
786 for (it = ARMCC.begin(); it != ARMCC.end(); it++){
787 SDOperand Flag = it == ARMCC.begin() ? Cmp : Aux.getValue(1);
788 SDOperand Ops[] = {Aux, Dest, *it, Flag};
789 Aux = DAG.getNode(ARMISD::BR, VTs, Ops, 4);
790 }
791 return Aux;
Rafael Espindola687bc492006-08-24 13:45:55 +0000792}
793
Rafael Espindola27185192006-09-29 21:20:16 +0000794static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola9e071f02006-10-02 19:30:56 +0000795 SDOperand IntVal = Op.getOperand(0);
Rafael Espindola27185192006-09-29 21:20:16 +0000796 assert(IntVal.getValueType() == MVT::i32);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000797 MVT::ValueType vt = Op.getValueType();
798 assert(vt == MVT::f32 ||
799 vt == MVT::f64);
Rafael Espindola27185192006-09-29 21:20:16 +0000800
801 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
Rafael Espindola9e071f02006-10-02 19:30:56 +0000802 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FSITOS : ARMISD::FSITOD;
803 return DAG.getNode(op, vt, Tmp);
Rafael Espindola27185192006-09-29 21:20:16 +0000804}
805
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000806static SDOperand LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
807 assert(Op.getValueType() == MVT::i32);
808 SDOperand FloatVal = Op.getOperand(0);
809 MVT::ValueType vt = FloatVal.getValueType();
810 assert(vt == MVT::f32 || vt == MVT::f64);
811
812 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FTOSIS : ARMISD::FTOSID;
813 SDOperand Tmp = DAG.getNode(op, MVT::f32, FloatVal);
814 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Tmp);
815}
816
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000817static SDOperand LowerUINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
818 SDOperand IntVal = Op.getOperand(0);
819 assert(IntVal.getValueType() == MVT::i32);
820 MVT::ValueType vt = Op.getValueType();
821 assert(vt == MVT::f32 ||
822 vt == MVT::f64);
823
824 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
825 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FUITOS : ARMISD::FUITOD;
826 return DAG.getNode(op, vt, Tmp);
827}
828
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000829static SDOperand LowerFP_TO_UINT(SDOperand Op, SelectionDAG &DAG) {
830 assert(Op.getValueType() == MVT::i32);
831 SDOperand FloatVal = Op.getOperand(0);
832 MVT::ValueType vt = FloatVal.getValueType();
833 assert(vt == MVT::f32 || vt == MVT::f64);
834
835 ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FTOUIS : ARMISD::FTOUID;
836 SDOperand Tmp = DAG.getNode(op, MVT::f32, FloatVal);
837 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Tmp);
838}
839
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000840SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
841 switch (Op.getOpcode()) {
842 default:
843 assert(0 && "Should not custom lower this!");
Rafael Espindola1c8f0532006-05-15 22:34:39 +0000844 abort();
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000845 case ISD::ConstantPool:
846 return LowerConstantPool(Op, DAG);
847 case ISD::GlobalAddress:
848 return LowerGlobalAddress(Op, DAG);
Rafael Espindolab47e1d02006-10-10 18:55:14 +0000849 case ISD::FP_TO_SINT:
850 return LowerFP_TO_SINT(Op, DAG);
Rafael Espindola27185192006-09-29 21:20:16 +0000851 case ISD::SINT_TO_FP:
852 return LowerSINT_TO_FP(Op, DAG);
Rafael Espindola493a7fc2006-10-10 20:38:57 +0000853 case ISD::FP_TO_UINT:
854 return LowerFP_TO_UINT(Op, DAG);
Rafael Espindolae5bbd6d2006-10-07 14:24:52 +0000855 case ISD::UINT_TO_FP:
856 return LowerUINT_TO_FP(Op, DAG);
Rafael Espindoladc124a22006-05-18 21:45:49 +0000857 case ISD::FORMAL_ARGUMENTS:
Rafael Espindola755be9b2006-08-25 17:55:16 +0000858 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
Rafael Espindolac3c1a862006-05-25 11:00:18 +0000859 case ISD::CALL:
860 return LowerCALL(Op, DAG);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000861 case ISD::RET:
862 return LowerRET(Op, DAG);
Rafael Espindola3c000bf2006-08-21 22:00:32 +0000863 case ISD::SELECT_CC:
864 return LowerSELECT_CC(Op, DAG);
Rafael Espindola687bc492006-08-24 13:45:55 +0000865 case ISD::BR_CC:
866 return LowerBR_CC(Op, DAG);
Rafael Espindola755be9b2006-08-25 17:55:16 +0000867 case ISD::VASTART:
868 return LowerVASTART(Op, DAG, VarArgsFrameIndex);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000869 }
870}
871
872//===----------------------------------------------------------------------===//
873// Instruction Selector Implementation
874//===----------------------------------------------------------------------===//
875
876//===--------------------------------------------------------------------===//
877/// ARMDAGToDAGISel - ARM specific code to select ARM machine
878/// instructions for SelectionDAG operations.
879///
880namespace {
881class ARMDAGToDAGISel : public SelectionDAGISel {
882 ARMTargetLowering Lowering;
883
884public:
885 ARMDAGToDAGISel(TargetMachine &TM)
886 : SelectionDAGISel(Lowering), Lowering(TM) {
887 }
888
Evan Cheng9ade2182006-08-26 05:34:46 +0000889 SDNode *Select(SDOperand Op);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000890 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Evan Cheng0d538262006-11-08 20:34:28 +0000891 bool SelectAddrMode1(SDOperand Op, SDOperand N, SDOperand &Arg,
892 SDOperand &Shift, SDOperand &ShiftType);
Rafael Espindolaf64945d2006-12-12 01:03:11 +0000893 bool SelectAddrMode1a(SDOperand Op, SDOperand N, SDOperand &Arg,
894 SDOperand &Shift, SDOperand &ShiftType);
Evan Cheng0d538262006-11-08 20:34:28 +0000895 bool SelectAddrMode2(SDOperand Op, SDOperand N, SDOperand &Arg,
896 SDOperand &Offset);
897 bool SelectAddrMode5(SDOperand Op, SDOperand N, SDOperand &Arg,
898 SDOperand &Offset);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000899
900 // Include the pieces autogenerated from the target description.
901#include "ARMGenDAGISel.inc"
902};
903
904void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
905 DEBUG(BB->dump());
906
907 DAG.setRoot(SelectRoot(DAG.getRoot()));
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000908 DAG.RemoveDeadNodes();
909
910 ScheduleAndEmitDAG(DAG);
911}
912
Rafael Espindola61369da2006-08-14 19:01:24 +0000913static bool isInt12Immediate(SDNode *N, short &Imm) {
914 if (N->getOpcode() != ISD::Constant)
915 return false;
916
917 int32_t t = cast<ConstantSDNode>(N)->getValue();
Rafael Espindola7246d332006-09-21 11:29:52 +0000918 int max = 1<<12;
Rafael Espindola61369da2006-08-14 19:01:24 +0000919 int min = -max;
920 if (t > min && t < max) {
921 Imm = t;
922 return true;
923 }
924 else
925 return false;
926}
927
928static bool isInt12Immediate(SDOperand Op, short &Imm) {
929 return isInt12Immediate(Op.Val, Imm);
930}
931
Evan Cheng0d538262006-11-08 20:34:28 +0000932bool ARMDAGToDAGISel::SelectAddrMode1(SDOperand Op,
933 SDOperand N,
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000934 SDOperand &Arg,
935 SDOperand &Shift,
936 SDOperand &ShiftType) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000937 switch(N.getOpcode()) {
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000938 case ISD::Constant: {
Rafael Espindola7246d332006-09-21 11:29:52 +0000939 uint32_t val = cast<ConstantSDNode>(N)->getValue();
940 if(!isRotInt8Immediate(val)) {
Rafael Espindolaa898ce62006-12-12 17:10:13 +0000941 SDOperand Z = CurDAG->getTargetConstant(0, MVT::i32);
942 SDNode *n;
943 if (isRotInt8Immediate(~val)) {
944 SDOperand C = CurDAG->getTargetConstant(~val, MVT::i32);
945 n = CurDAG->getTargetNode(ARM::MVN, MVT::i32, C, Z, Z);
946 } else {
Reid Spencer47857812006-12-31 05:55:36 +0000947 Constant *C = ConstantInt::get(Type::Int32Ty, val);
Rafael Espindolaa898ce62006-12-12 17:10:13 +0000948 int alignment = 2;
949 SDOperand Addr = CurDAG->getTargetConstantPool(C, MVT::i32, alignment);
950 n = CurDAG->getTargetNode(ARM::LDR, MVT::i32, Addr, Z);
951 }
Rafael Espindola7246d332006-09-21 11:29:52 +0000952 Arg = SDOperand(n, 0);
953 } else
954 Arg = CurDAG->getTargetConstant(val, MVT::i32);
955
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000956 Shift = CurDAG->getTargetConstant(0, MVT::i32);
957 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000958 return true;
959 }
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000960 case ISD::SRA:
961 Arg = N.getOperand(0);
962 Shift = N.getOperand(1);
963 ShiftType = CurDAG->getTargetConstant(ARMShift::ASR, MVT::i32);
964 return true;
965 case ISD::SRL:
966 Arg = N.getOperand(0);
967 Shift = N.getOperand(1);
968 ShiftType = CurDAG->getTargetConstant(ARMShift::LSR, MVT::i32);
969 return true;
970 case ISD::SHL:
971 Arg = N.getOperand(0);
972 Shift = N.getOperand(1);
973 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
974 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000975 }
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000976
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000977 Arg = N;
978 Shift = CurDAG->getTargetConstant(0, MVT::i32);
979 ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
Rafael Espindola1b3956b2006-09-11 19:23:32 +0000980 return true;
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000981}
982
Evan Cheng0d538262006-11-08 20:34:28 +0000983bool ARMDAGToDAGISel::SelectAddrMode2(SDOperand Op, SDOperand N,
984 SDOperand &Arg, SDOperand &Offset) {
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000985 //TODO: complete and cleanup!
986 SDOperand Zero = CurDAG->getTargetConstant(0, MVT::i32);
987 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
988 Arg = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
989 Offset = Zero;
990 return true;
991 }
992 if (N.getOpcode() == ISD::ADD) {
993 short imm = 0;
994 if (isInt12Immediate(N.getOperand(1), imm)) {
995 Offset = CurDAG->getTargetConstant(imm, MVT::i32);
996 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
997 Arg = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
998 } else {
999 Arg = N.getOperand(0);
1000 }
1001 return true; // [r+i]
1002 }
1003 }
1004 Offset = Zero;
1005 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
1006 Arg = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
1007 else
1008 Arg = N;
1009 return true;
1010}
1011
Evan Cheng0d538262006-11-08 20:34:28 +00001012bool ARMDAGToDAGISel::SelectAddrMode5(SDOperand Op,
1013 SDOperand N, SDOperand &Arg,
Rafael Espindola32bd5f42006-10-17 18:04:53 +00001014 SDOperand &Offset) {
1015 //TODO: detect offset
1016 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1017 Arg = N;
1018 return true;
1019}
1020
Evan Cheng9ade2182006-08-26 05:34:46 +00001021SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
Rafael Espindola337c4ad62006-06-12 12:28:08 +00001022 SDNode *N = Op.Val;
1023
1024 switch (N->getOpcode()) {
1025 default:
Evan Cheng9ade2182006-08-26 05:34:46 +00001026 return SelectCode(Op);
Rafael Espindola337c4ad62006-06-12 12:28:08 +00001027 break;
Rafael Espindolaf819a492006-11-09 13:58:55 +00001028 case ISD::FrameIndex: {
1029 int FI = cast<FrameIndexSDNode>(N)->getIndex();
1030 SDOperand Ops[] = {CurDAG->getTargetFrameIndex(FI, MVT::i32),
1031 CurDAG->getTargetConstant(0, MVT::i32),
1032 CurDAG->getTargetConstant(0, MVT::i32),
1033 CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32)};
1034
1035 return CurDAG->SelectNodeTo(N, ARM::ADD, MVT::i32, Ops,
1036 sizeof(Ops)/sizeof(SDOperand));
1037 break;
Rafael Espindola337c4ad62006-06-12 12:28:08 +00001038 }
Rafael Espindolaf819a492006-11-09 13:58:55 +00001039 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001040}
1041
1042} // end anonymous namespace
1043
1044/// createARMISelDag - This pass converts a legalized DAG into a
1045/// ARM-specific DAG, ready for instruction scheduling.
1046///
1047FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
1048 return new ARMDAGToDAGISel(TM);
1049}