blob: f23d8aeb37eb447411e85732839e9b4de46e0caf [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Evan Cheng and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "ARM.h"
16#include "ARMAddressingModes.h"
17#include "ARMConstantPoolValue.h"
18#include "ARMISelLowering.h"
19#include "ARMMachineFunctionInfo.h"
20#include "ARMRegisterInfo.h"
21#include "ARMSubtarget.h"
22#include "ARMTargetMachine.h"
23#include "llvm/CallingConv.h"
24#include "llvm/Constants.h"
25#include "llvm/CodeGen/MachineBasicBlock.h"
26#include "llvm/CodeGen/MachineFrameInfo.h"
27#include "llvm/CodeGen/MachineFunction.h"
28#include "llvm/CodeGen/MachineInstrBuilder.h"
29#include "llvm/CodeGen/SelectionDAG.h"
30#include "llvm/CodeGen/SSARegMap.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000031#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000032#include "llvm/ADT/VectorExtras.h"
Evan Chengb01fad62007-03-12 23:30:29 +000033#include "llvm/Support/MathExtras.h"
Evan Chenga8e29892007-01-19 07:51:42 +000034using namespace llvm;
35
36ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
37 : TargetLowering(TM), ARMPCLabelIndex(0) {
38 Subtarget = &TM.getSubtarget<ARMSubtarget>();
39
40 // Uses VFP for Thumb libfuncs if available.
Evan Chengb6ab2542007-01-31 08:40:13 +000041 if (!UseSoftFloat && Subtarget->isThumb() && Subtarget->hasVFP2()) {
Evan Chenga8e29892007-01-19 07:51:42 +000042 // Single-precision floating-point arithmetic.
43 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
44 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
45 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
46 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
47
48 // Double-precision floating-point arithmetic.
49 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
50 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
51 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
52 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
53
54 // Single-precision comparisons.
55 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
56 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
57 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
58 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
59 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
60 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
61 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
Evan Cheng193f8502007-01-31 09:30:58 +000062 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
63
64 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
65 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
66 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
67 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
68 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
69 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
70 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
71 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +000072
73 // Double-precision comparisons.
74 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
75 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
76 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
77 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
78 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
79 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
80 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
Evan Cheng193f8502007-01-31 09:30:58 +000081 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
82
83 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
84 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
85 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
86 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
87 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
88 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
89 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
90 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +000091
92 // Floating-point to integer conversions.
93 // i64 conversions are done via library routines even when generating VFP
94 // instructions, so use the same ones.
95 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
96 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
97 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
98 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
99
100 // Conversions between floating types.
101 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
102 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
103
104 // Integer to floating-point conversions.
105 // i64 conversions are done via library routines even when generating VFP
106 // instructions, so use the same ones.
107 // FIXME: There appears to be some naming inconsistency in ARM libgcc: e.g.
108 // __floatunsidf vs. __floatunssidfvfp.
109 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
110 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
111 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
112 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
113 }
114
115 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
Evan Chengb6ab2542007-01-31 08:40:13 +0000116 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000117 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
118 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
119 }
120
121 // ARM does not have f32 extending load.
122 setLoadXAction(ISD::EXTLOAD, MVT::f32, Expand);
123
124 // ARM supports all 4 flavors of integer indexed load / store.
125 for (unsigned im = (unsigned)ISD::PRE_INC;
126 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
127 setIndexedLoadAction(im, MVT::i1, Legal);
128 setIndexedLoadAction(im, MVT::i8, Legal);
129 setIndexedLoadAction(im, MVT::i16, Legal);
130 setIndexedLoadAction(im, MVT::i32, Legal);
131 setIndexedStoreAction(im, MVT::i1, Legal);
132 setIndexedStoreAction(im, MVT::i8, Legal);
133 setIndexedStoreAction(im, MVT::i16, Legal);
134 setIndexedStoreAction(im, MVT::i32, Legal);
135 }
136
137 // i64 operation support.
138 if (Subtarget->isThumb()) {
139 setOperationAction(ISD::MUL, MVT::i64, Expand);
140 setOperationAction(ISD::MULHU, MVT::i32, Expand);
141 setOperationAction(ISD::MULHS, MVT::i32, Expand);
142 } else {
143 setOperationAction(ISD::MUL, MVT::i64, Custom);
144 setOperationAction(ISD::MULHU, MVT::i32, Custom);
145 if (!Subtarget->hasV6Ops())
146 setOperationAction(ISD::MULHS, MVT::i32, Custom);
147 }
148 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
149 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
150 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
151 setOperationAction(ISD::SRL, MVT::i64, Custom);
152 setOperationAction(ISD::SRA, MVT::i64, Custom);
153
154 // ARM does not have ROTL.
155 setOperationAction(ISD::ROTL, MVT::i32, Expand);
156 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
157 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
Evan Chengb0636152007-02-01 23:34:03 +0000158 if (!Subtarget->hasV5TOps() || Subtarget->isThumb())
Evan Chenga8e29892007-01-19 07:51:42 +0000159 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
160
161 // These are expanded into libcalls.
162 setOperationAction(ISD::SDIV, MVT::i32, Expand);
163 setOperationAction(ISD::UDIV, MVT::i32, Expand);
164 setOperationAction(ISD::SREM, MVT::i32, Expand);
165 setOperationAction(ISD::UREM, MVT::i32, Expand);
166
167 // Support label based line numbers.
168 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
169 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
170 // FIXME - use subtarget debug flags
Evan Chengb582b1b2007-03-08 21:59:30 +0000171 if (!Subtarget->isTargetDarwin())
Jim Laskey1ee29252007-01-26 14:34:52 +0000172 setOperationAction(ISD::LABEL, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000173
174 setOperationAction(ISD::RET, MVT::Other, Custom);
175 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
176 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
177
178 // Expand mem operations genericly.
179 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
180 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
181 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
182
183 // Use the default implementation.
184 setOperationAction(ISD::VASTART , MVT::Other, Expand);
185 setOperationAction(ISD::VAARG , MVT::Other, Expand);
186 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
187 setOperationAction(ISD::VAEND , MVT::Other, Expand);
188 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
189 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
190 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
191
192 if (!Subtarget->hasV6Ops()) {
193 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
194 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
195 }
196 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
197
Evan Chengb6ab2542007-01-31 08:40:13 +0000198 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb())
Evan Chenga8e29892007-01-19 07:51:42 +0000199 // Turn f64->i64 into FMRRD iff target supports vfp2.
200 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
201
202 setOperationAction(ISD::SETCC , MVT::i32, Expand);
203 setOperationAction(ISD::SETCC , MVT::f32, Expand);
204 setOperationAction(ISD::SETCC , MVT::f64, Expand);
205 setOperationAction(ISD::SELECT , MVT::i32, Expand);
206 setOperationAction(ISD::SELECT , MVT::f32, Expand);
207 setOperationAction(ISD::SELECT , MVT::f64, Expand);
208 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
209 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
210 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
211
212 setOperationAction(ISD::BRCOND , MVT::Other, Expand);
213 setOperationAction(ISD::BR_CC , MVT::i32, Custom);
214 setOperationAction(ISD::BR_CC , MVT::f32, Custom);
215 setOperationAction(ISD::BR_CC , MVT::f64, Custom);
216 setOperationAction(ISD::BR_JT , MVT::Other, Custom);
217
218 setOperationAction(ISD::VASTART, MVT::Other, Custom);
219 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
220 setOperationAction(ISD::VAEND, MVT::Other, Expand);
221 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
222 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
223
224 // FP Constants can't be immediates.
225 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
226 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
227
228 // We don't support sin/cos/fmod/copysign
229 setOperationAction(ISD::FSIN , MVT::f64, Expand);
230 setOperationAction(ISD::FSIN , MVT::f32, Expand);
231 setOperationAction(ISD::FCOS , MVT::f32, Expand);
232 setOperationAction(ISD::FCOS , MVT::f64, Expand);
233 setOperationAction(ISD::FREM , MVT::f64, Expand);
234 setOperationAction(ISD::FREM , MVT::f32, Expand);
235 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
236 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
237
238 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
239 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
240 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
241 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
242 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
243
244 setStackPointerRegisterToSaveRestore(ARM::SP);
245
246 setSchedulingPreference(SchedulingForRegPressure);
247 computeRegisterProperties();
248}
249
250
251const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
252 switch (Opcode) {
253 default: return 0;
254 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000255 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
256 case ARMISD::CALL: return "ARMISD::CALL";
257 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
258 case ARMISD::tCALL: return "ARMISD::tCALL";
259 case ARMISD::BRCOND: return "ARMISD::BRCOND";
260 case ARMISD::BR_JT: return "ARMISD::BR_JT";
261 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
262 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
263 case ARMISD::CMP: return "ARMISD::CMP";
264 case ARMISD::CMPFP: return "ARMISD::CMPFP";
265 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
266 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
267 case ARMISD::CMOV: return "ARMISD::CMOV";
268 case ARMISD::CNEG: return "ARMISD::CNEG";
269
270 case ARMISD::FTOSI: return "ARMISD::FTOSI";
271 case ARMISD::FTOUI: return "ARMISD::FTOUI";
272 case ARMISD::SITOF: return "ARMISD::SITOF";
273 case ARMISD::UITOF: return "ARMISD::UITOF";
274 case ARMISD::MULHILOU: return "ARMISD::MULHILOU";
275 case ARMISD::MULHILOS: return "ARMISD::MULHILOS";
276
277 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
278 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
279 case ARMISD::RRX: return "ARMISD::RRX";
280
281 case ARMISD::FMRRD: return "ARMISD::FMRRD";
282 case ARMISD::FMDRR: return "ARMISD::FMDRR";
283 }
284}
285
286//===----------------------------------------------------------------------===//
287// Lowering Code
288//===----------------------------------------------------------------------===//
289
290
291/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
292static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
293 switch (CC) {
294 default: assert(0 && "Unknown condition code!");
295 case ISD::SETNE: return ARMCC::NE;
296 case ISD::SETEQ: return ARMCC::EQ;
297 case ISD::SETGT: return ARMCC::GT;
298 case ISD::SETGE: return ARMCC::GE;
299 case ISD::SETLT: return ARMCC::LT;
300 case ISD::SETLE: return ARMCC::LE;
301 case ISD::SETUGT: return ARMCC::HI;
302 case ISD::SETUGE: return ARMCC::HS;
303 case ISD::SETULT: return ARMCC::LO;
304 case ISD::SETULE: return ARMCC::LS;
305 }
306}
307
308/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. It
309/// returns true if the operands should be inverted to form the proper
310/// comparison.
311static bool FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
312 ARMCC::CondCodes &CondCode2) {
313 bool Invert = false;
314 CondCode2 = ARMCC::AL;
315 switch (CC) {
316 default: assert(0 && "Unknown FP condition!");
317 case ISD::SETEQ:
318 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
319 case ISD::SETGT:
320 case ISD::SETOGT: CondCode = ARMCC::GT; break;
321 case ISD::SETGE:
322 case ISD::SETOGE: CondCode = ARMCC::GE; break;
323 case ISD::SETOLT: CondCode = ARMCC::MI; break;
324 case ISD::SETOLE: CondCode = ARMCC::GT; Invert = true; break;
325 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
326 case ISD::SETO: CondCode = ARMCC::VC; break;
327 case ISD::SETUO: CondCode = ARMCC::VS; break;
328 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
329 case ISD::SETUGT: CondCode = ARMCC::HI; break;
330 case ISD::SETUGE: CondCode = ARMCC::PL; break;
331 case ISD::SETLT:
332 case ISD::SETULT: CondCode = ARMCC::LT; break;
333 case ISD::SETLE:
334 case ISD::SETULE: CondCode = ARMCC::LE; break;
335 case ISD::SETNE:
336 case ISD::SETUNE: CondCode = ARMCC::NE; break;
337 }
338 return Invert;
339}
340
341static void
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000342HowToPassArgument(MVT::ValueType ObjectVT, unsigned NumGPRs,
343 unsigned StackOffset, unsigned &NeededGPRs,
344 unsigned &NeededStackSize, unsigned &GPRPad,
345 unsigned &StackPad, unsigned Flags) {
346 NeededStackSize = 0;
347 NeededGPRs = 0;
348 StackPad = 0;
349 GPRPad = 0;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +0000350 unsigned align = (Flags >> ISD::ParamFlags::OrigAlignmentOffs);
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000351 GPRPad = NumGPRs % ((align + 3)/4);
352 StackPad = StackOffset % align;
353 unsigned firstGPR = NumGPRs + GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000354 switch (ObjectVT) {
355 default: assert(0 && "Unhandled argument type!");
356 case MVT::i32:
357 case MVT::f32:
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000358 if (firstGPR < 4)
359 NeededGPRs = 1;
Evan Chenga8e29892007-01-19 07:51:42 +0000360 else
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000361 NeededStackSize = 4;
Evan Chenga8e29892007-01-19 07:51:42 +0000362 break;
363 case MVT::i64:
364 case MVT::f64:
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000365 if (firstGPR < 3)
366 NeededGPRs = 2;
367 else if (firstGPR == 3) {
368 NeededGPRs = 1;
369 NeededStackSize = 4;
Evan Chenga8e29892007-01-19 07:51:42 +0000370 } else
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000371 NeededStackSize = 8;
Evan Chenga8e29892007-01-19 07:51:42 +0000372 }
373}
374
Evan Chengfc403422007-02-03 08:53:01 +0000375/// LowerCALL - Lowering a ISD::CALL node into a callseq_start <-
376/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
377/// nodes.
Evan Chenga8e29892007-01-19 07:51:42 +0000378SDOperand ARMTargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
379 MVT::ValueType RetVT= Op.Val->getValueType(0);
380 SDOperand Chain = Op.getOperand(0);
381 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
382 assert((CallConv == CallingConv::C ||
Evan Chenga8e29892007-01-19 07:51:42 +0000383 CallConv == CallingConv::Fast) && "unknown calling convention");
384 SDOperand Callee = Op.getOperand(4);
385 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
386 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
387 unsigned NumGPRs = 0; // GPRs used for parameter passing.
388
389 // Count how many bytes are to be pushed on the stack.
390 unsigned NumBytes = 0;
391
392 // Add up all the space actually used.
393 for (unsigned i = 0; i < NumOps; ++i) {
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000394 unsigned ObjSize;
395 unsigned ObjGPRs;
396 unsigned StackPad;
397 unsigned GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000398 MVT::ValueType ObjectVT = Op.getOperand(5+2*i).getValueType();
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000399 unsigned Flags = Op.getConstantOperandVal(5+2*i+1);
400 HowToPassArgument(ObjectVT, NumGPRs, NumBytes, ObjGPRs, ObjSize,
401 GPRPad, StackPad, Flags);
402 NumBytes += ObjSize + StackPad;
403 NumGPRs += ObjGPRs + GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000404 }
405
406 // Adjust the stack pointer for the new arguments...
407 // These operations are automatically eliminated by the prolog/epilog pass
408 Chain = DAG.getCALLSEQ_START(Chain,
409 DAG.getConstant(NumBytes, MVT::i32));
410
411 SDOperand StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
412
413 static const unsigned GPRArgRegs[] = {
414 ARM::R0, ARM::R1, ARM::R2, ARM::R3
415 };
416
417 NumGPRs = 0;
418 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
419 std::vector<SDOperand> MemOpChains;
420 for (unsigned i = 0; i != NumOps; ++i) {
421 SDOperand Arg = Op.getOperand(5+2*i);
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000422 unsigned Flags = Op.getConstantOperandVal(5+2*i+1);
Evan Chenga8e29892007-01-19 07:51:42 +0000423 MVT::ValueType ArgVT = Arg.getValueType();
424
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000425 unsigned ObjSize;
426 unsigned ObjGPRs;
427 unsigned GPRPad;
428 unsigned StackPad;
429 HowToPassArgument(ArgVT, NumGPRs, ArgOffset, ObjGPRs,
430 ObjSize, GPRPad, StackPad, Flags);
431 NumGPRs += GPRPad;
432 ArgOffset += StackPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000433 if (ObjGPRs > 0) {
434 switch (ArgVT) {
435 default: assert(0 && "Unexpected ValueType for argument!");
436 case MVT::i32:
437 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Arg));
438 break;
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000439 case MVT::f32:
Evan Chenga8e29892007-01-19 07:51:42 +0000440 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs],
441 DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg)));
442 break;
443 case MVT::i64: {
444 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Arg,
445 DAG.getConstant(0, getPointerTy()));
446 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Arg,
447 DAG.getConstant(1, getPointerTy()));
448 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Lo));
449 if (ObjGPRs == 2)
450 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1], Hi));
451 else {
452 SDOperand PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType());
453 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
454 MemOpChains.push_back(DAG.getStore(Chain, Hi, PtrOff, NULL, 0));
455 }
456 break;
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000457 }
Evan Chenga8e29892007-01-19 07:51:42 +0000458 case MVT::f64: {
459 SDOperand Cvt = DAG.getNode(ARMISD::FMRRD,
460 DAG.getVTList(MVT::i32, MVT::i32),
461 &Arg, 1);
462 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Cvt));
463 if (ObjGPRs == 2)
464 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1],
465 Cvt.getValue(1)));
466 else {
467 SDOperand PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType());
468 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
469 MemOpChains.push_back(DAG.getStore(Chain, Cvt.getValue(1), PtrOff,
470 NULL, 0));
471 }
472 break;
473 }
474 }
475 } else {
476 assert(ObjSize != 0);
477 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
478 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
479 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
480 }
481
482 NumGPRs += ObjGPRs;
483 ArgOffset += ObjSize;
484 }
485
486 if (!MemOpChains.empty())
487 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
488 &MemOpChains[0], MemOpChains.size());
489
490 // Build a sequence of copy-to-reg nodes chained together with token chain
491 // and flag operands which copy the outgoing args into the appropriate regs.
492 SDOperand InFlag;
493 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
494 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
495 InFlag);
496 InFlag = Chain.getValue(1);
497 }
498
499 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
500 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
501 // node so that legalize doesn't hack it.
502 bool isDirect = false;
503 bool isARMFunc = false;
504 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
505 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +0000506 isDirect = true;
Reid Spencer5cbf9852007-01-30 20:08:39 +0000507 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
Evan Chenga8e29892007-01-19 07:51:42 +0000508 GV->hasLinkOnceLinkage());
Evan Cheng970a4192007-01-19 19:28:01 +0000509 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +0000510 getTargetMachine().getRelocationModel() != Reloc::Static;
511 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +0000512 // tBX takes a register source operand.
513 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
514 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
515 ARMCP::CPStub, 4);
516 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 2);
517 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
518 Callee = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), CPAddr, NULL, 0);
519 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
520 Callee = DAG.getNode(ARMISD::PIC_ADD, getPointerTy(), Callee, PICLabel);
521 } else
522 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000523 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000524 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +0000525 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000526 getTargetMachine().getRelocationModel() != Reloc::Static;
527 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +0000528 // tBX takes a register source operand.
529 const char *Sym = S->getSymbol();
530 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
531 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(Sym, ARMPCLabelIndex,
532 ARMCP::CPStub, 4);
533 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 2);
534 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
535 Callee = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), CPAddr, NULL, 0);
536 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
537 Callee = DAG.getNode(ARMISD::PIC_ADD, getPointerTy(), Callee, PICLabel);
538 } else
539 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000540 }
541
542 std::vector<MVT::ValueType> NodeTys;
543 NodeTys.push_back(MVT::Other); // Returns a chain
544 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
545
546 std::vector<SDOperand> Ops;
547 Ops.push_back(Chain);
548 Ops.push_back(Callee);
549
550 // Add argument registers to the end of the list so that they are known live
551 // into the call.
552 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
553 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
554 RegsToPass[i].second.getValueType()));
555
556 // FIXME: handle tail calls differently.
557 unsigned CallOpc;
558 if (Subtarget->isThumb()) {
559 if (!Subtarget->hasV5TOps() && (!isDirect || isARMFunc))
560 CallOpc = ARMISD::CALL_NOLINK;
561 else
562 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
563 } else {
564 CallOpc = (isDirect || Subtarget->hasV5TOps())
565 ? ARMISD::CALL : ARMISD::CALL_NOLINK;
566 }
567 if (InFlag.Val)
568 Ops.push_back(InFlag);
569 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
570 InFlag = Chain.getValue(1);
571
572 SDOperand CSOps[] = { Chain, DAG.getConstant(NumBytes, MVT::i32), InFlag };
573 Chain = DAG.getNode(ISD::CALLSEQ_END,
574 DAG.getNodeValueTypes(MVT::Other, MVT::Flag),
575 ((RetVT != MVT::Other) ? 2 : 1), CSOps, 3);
576 if (RetVT != MVT::Other)
577 InFlag = Chain.getValue(1);
578
579 std::vector<SDOperand> ResultVals;
580 NodeTys.clear();
581
582 // If the call has results, copy the values out of the ret val registers.
583 switch (RetVT) {
584 default: assert(0 && "Unexpected ret value!");
585 case MVT::Other:
586 break;
587 case MVT::i32:
588 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
589 ResultVals.push_back(Chain.getValue(0));
590 if (Op.Val->getValueType(1) == MVT::i32) {
591 // Returns a i64 value.
592 Chain = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32,
593 Chain.getValue(2)).getValue(1);
594 ResultVals.push_back(Chain.getValue(0));
595 NodeTys.push_back(MVT::i32);
596 }
597 NodeTys.push_back(MVT::i32);
598 break;
599 case MVT::f32:
600 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
601 ResultVals.push_back(DAG.getNode(ISD::BIT_CONVERT, MVT::f32,
602 Chain.getValue(0)));
603 NodeTys.push_back(MVT::f32);
604 break;
605 case MVT::f64: {
606 SDOperand Lo = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
607 SDOperand Hi = DAG.getCopyFromReg(Lo, ARM::R1, MVT::i32, Lo.getValue(2));
608 ResultVals.push_back(DAG.getNode(ARMISD::FMDRR, MVT::f64, Lo, Hi));
609 NodeTys.push_back(MVT::f64);
610 break;
611 }
612 }
613
614 NodeTys.push_back(MVT::Other);
615
616 if (ResultVals.empty())
617 return Chain;
618
619 ResultVals.push_back(Chain);
620 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
621 ResultVals.size());
622 return Res.getValue(Op.ResNo);
623}
624
625static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
626 SDOperand Copy;
627 SDOperand Chain = Op.getOperand(0);
628 switch(Op.getNumOperands()) {
629 default:
630 assert(0 && "Do not know how to return this many arguments!");
631 abort();
632 case 1: {
633 SDOperand LR = DAG.getRegister(ARM::LR, MVT::i32);
634 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
635 }
636 case 3:
637 Op = Op.getOperand(1);
638 if (Op.getValueType() == MVT::f32) {
639 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
640 } else if (Op.getValueType() == MVT::f64) {
641 // Recursively legalize f64 -> i64.
642 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Op);
643 return DAG.getNode(ISD::RET, MVT::Other, Chain, Op,
644 DAG.getConstant(0, MVT::i32));
645 }
646 Copy = DAG.getCopyToReg(Chain, ARM::R0, Op, SDOperand());
647 if (DAG.getMachineFunction().liveout_empty())
648 DAG.getMachineFunction().addLiveOut(ARM::R0);
649 break;
650 case 5:
651 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
652 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
653 // If we haven't noted the R0+R1 are live out, do so now.
654 if (DAG.getMachineFunction().liveout_empty()) {
655 DAG.getMachineFunction().addLiveOut(ARM::R0);
656 DAG.getMachineFunction().addLiveOut(ARM::R1);
657 }
658 break;
659 }
660
661 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
662 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
663}
664
665// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
666// their target countpart wrapped in the ARMISD::Wrapper node. Suppose N is
667// one of the above mentioned nodes. It has to be wrapped because otherwise
668// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
669// be used to form addressing mode. These wrapped nodes will be selected
670// into MOVri.
671static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
672 MVT::ValueType PtrVT = Op.getValueType();
673 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
674 SDOperand Res;
675 if (CP->isMachineConstantPoolEntry())
676 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
677 CP->getAlignment());
678 else
679 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
680 CP->getAlignment());
681 return DAG.getNode(ARMISD::Wrapper, MVT::i32, Res);
682}
683
684/// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol
685/// even in dynamic-no-pic mode.
686static bool GVIsIndirectSymbol(GlobalValue *GV) {
687 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
Reid Spencer5cbf9852007-01-30 20:08:39 +0000688 (GV->isDeclaration() && !GV->hasNotBeenReadFromBytecode()));
Evan Chenga8e29892007-01-19 07:51:42 +0000689}
690
691SDOperand ARMTargetLowering::LowerGlobalAddress(SDOperand Op,
692 SelectionDAG &DAG) {
693 MVT::ValueType PtrVT = getPointerTy();
694 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
695 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng970a4192007-01-19 19:28:01 +0000696 bool IsIndirect = Subtarget->isTargetDarwin() && GVIsIndirectSymbol(GV);
Evan Chenga8e29892007-01-19 07:51:42 +0000697 SDOperand CPAddr;
698 if (RelocM == Reloc::Static)
699 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 2);
700 else {
701 unsigned PCAdj = (RelocM != Reloc::PIC_)
702 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Chengc60e76d2007-01-30 20:37:08 +0000703 ARMCP::ARMCPKind Kind = IsIndirect ? ARMCP::CPNonLazyPtr
704 : ARMCP::CPValue;
Evan Chenga8e29892007-01-19 07:51:42 +0000705 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
Evan Chengc60e76d2007-01-30 20:37:08 +0000706 Kind, PCAdj);
Evan Chenga8e29892007-01-19 07:51:42 +0000707 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
708 }
709 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
710
711 SDOperand Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
712 SDOperand Chain = Result.getValue(1);
713
714 if (RelocM == Reloc::PIC_) {
715 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
716 Result = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Result, PICLabel);
717 }
718 if (IsIndirect)
719 Result = DAG.getLoad(PtrVT, Chain, Result, NULL, 0);
720
721 return Result;
722}
723
724static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
725 unsigned VarArgsFrameIndex) {
726 // vastart just stores the address of the VarArgsFrameIndex slot into the
727 // memory location argument.
728 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
729 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
730 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
731 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), SV->getValue(),
732 SV->getOffset());
733}
734
735static SDOperand LowerFORMAL_ARGUMENT(SDOperand Op, SelectionDAG &DAG,
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000736 unsigned *vRegs, unsigned ArgNo,
Evan Chenga8e29892007-01-19 07:51:42 +0000737 unsigned &NumGPRs, unsigned &ArgOffset) {
738 MachineFunction &MF = DAG.getMachineFunction();
739 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
740 SDOperand Root = Op.getOperand(0);
741 std::vector<SDOperand> ArgValues;
742 SSARegMap *RegMap = MF.getSSARegMap();
743
744 static const unsigned GPRArgRegs[] = {
745 ARM::R0, ARM::R1, ARM::R2, ARM::R3
746 };
747
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000748 unsigned ObjSize;
749 unsigned ObjGPRs;
750 unsigned GPRPad;
751 unsigned StackPad;
752 unsigned Flags = Op.getConstantOperandVal(ArgNo + 3);
753 HowToPassArgument(ObjectVT, NumGPRs, ArgOffset, ObjGPRs,
754 ObjSize, GPRPad, StackPad, Flags);
755 NumGPRs += GPRPad;
756 ArgOffset += StackPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000757
758 SDOperand ArgValue;
759 if (ObjGPRs == 1) {
760 unsigned VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
761 MF.addLiveIn(GPRArgRegs[NumGPRs], VReg);
762 vRegs[NumGPRs] = VReg;
763 ArgValue = DAG.getCopyFromReg(Root, VReg, MVT::i32);
764 if (ObjectVT == MVT::f32)
765 ArgValue = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, ArgValue);
766 } else if (ObjGPRs == 2) {
767 unsigned VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
768 MF.addLiveIn(GPRArgRegs[NumGPRs], VReg);
769 vRegs[NumGPRs] = VReg;
770 ArgValue = DAG.getCopyFromReg(Root, VReg, MVT::i32);
771
772 VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
773 MF.addLiveIn(GPRArgRegs[NumGPRs+1], VReg);
774 vRegs[NumGPRs+1] = VReg;
775 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, VReg, MVT::i32);
776
777 if (ObjectVT == MVT::i64)
778 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
779 else
780 ArgValue = DAG.getNode(ARMISD::FMDRR, MVT::f64, ArgValue, ArgValue2);
781 }
782 NumGPRs += ObjGPRs;
783
784 if (ObjSize) {
785 // If the argument is actually used, emit a load from the right stack
786 // slot.
787 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
788 MachineFrameInfo *MFI = MF.getFrameInfo();
789 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
790 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
791 if (ObjGPRs == 0)
792 ArgValue = DAG.getLoad(ObjectVT, Root, FIN, NULL, 0);
793 else {
794 SDOperand ArgValue2 =
795 DAG.getLoad(MVT::i32, Root, FIN, NULL, 0);
796 if (ObjectVT == MVT::i64)
797 ArgValue= DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
798 else
799 ArgValue= DAG.getNode(ARMISD::FMDRR, MVT::f64, ArgValue, ArgValue2);
800 }
801 } else {
802 // Don't emit a dead load.
803 ArgValue = DAG.getNode(ISD::UNDEF, ObjectVT);
804 }
805
806 ArgOffset += ObjSize; // Move on to the next argument.
807 }
808
809 return ArgValue;
810}
811
812SDOperand
813ARMTargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
814 std::vector<SDOperand> ArgValues;
815 SDOperand Root = Op.getOperand(0);
816 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
817 unsigned NumGPRs = 0; // GPRs used for parameter passing.
818 unsigned VRegs[4];
819
820 unsigned NumArgs = Op.Val->getNumValues()-1;
821 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo)
822 ArgValues.push_back(LowerFORMAL_ARGUMENT(Op, DAG, VRegs, ArgNo,
823 NumGPRs, ArgOffset));
824
825 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
826 if (isVarArg) {
827 static const unsigned GPRArgRegs[] = {
828 ARM::R0, ARM::R1, ARM::R2, ARM::R3
829 };
830
831 MachineFunction &MF = DAG.getMachineFunction();
832 SSARegMap *RegMap = MF.getSSARegMap();
833 MachineFrameInfo *MFI = MF.getFrameInfo();
834 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +0000835 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
836 unsigned VARegSize = (4 - NumGPRs) * 4;
837 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Evan Chenga8e29892007-01-19 07:51:42 +0000838 if (VARegSaveSize) {
839 // If this function is vararg, store any remaining integer argument regs
840 // to their spots on the stack so that they may be loaded by deferencing
841 // the result of va_next.
842 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +0000843 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
844 VARegSaveSize - VARegSize);
Evan Chenga8e29892007-01-19 07:51:42 +0000845 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
846
847 SmallVector<SDOperand, 4> MemOps;
848 for (; NumGPRs < 4; ++NumGPRs) {
849 unsigned VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
850 MF.addLiveIn(GPRArgRegs[NumGPRs], VReg);
851 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
852 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
853 MemOps.push_back(Store);
854 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
855 DAG.getConstant(4, getPointerTy()));
856 }
857 if (!MemOps.empty())
858 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
859 &MemOps[0], MemOps.size());
860 } else
861 // This will point to the next argument passed via stack.
862 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
863 }
864
865 ArgValues.push_back(Root);
866
867 // Return the new list of results.
868 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
869 Op.Val->value_end());
870 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
871}
872
873/// isFloatingPointZero - Return true if this is +0.0.
874static bool isFloatingPointZero(SDOperand Op) {
875 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
876 return CFP->isExactlyValue(0.0);
877 else if (ISD::isEXTLoad(Op.Val) || ISD::isNON_EXTLoad(Op.Val)) {
878 // Maybe this has already been legalized into the constant pool?
879 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
880 SDOperand WrapperOp = Op.getOperand(1).getOperand(0);
881 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
882 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
883 return CFP->isExactlyValue(0.0);
884 }
885 }
886 return false;
887}
888
Evan Cheng9a2ef952007-02-02 01:53:26 +0000889static bool isLegalCmpImmediate(unsigned C, bool isThumb) {
Evan Chenga8e29892007-01-19 07:51:42 +0000890 return ( isThumb && (C & ~255U) == 0) ||
891 (!isThumb && ARM_AM::getSOImmVal(C) != -1);
892}
893
894/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
895/// the given operands.
896static SDOperand getARMCmp(SDOperand LHS, SDOperand RHS, ISD::CondCode CC,
897 SDOperand &ARMCC, SelectionDAG &DAG, bool isThumb) {
898 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.Val)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +0000899 unsigned C = RHSC->getValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000900 if (!isLegalCmpImmediate(C, isThumb)) {
901 // Constant does not fit, try adjusting it by one?
902 switch (CC) {
903 default: break;
904 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +0000905 case ISD::SETGE:
Evan Chenga8e29892007-01-19 07:51:42 +0000906 if (isLegalCmpImmediate(C-1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +0000907 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
908 RHS = DAG.getConstant(C-1, MVT::i32);
909 }
910 break;
911 case ISD::SETULT:
912 case ISD::SETUGE:
913 if (C > 0 && isLegalCmpImmediate(C-1, isThumb)) {
914 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Evan Chenga8e29892007-01-19 07:51:42 +0000915 RHS = DAG.getConstant(C-1, MVT::i32);
916 }
917 break;
918 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +0000919 case ISD::SETGT:
Evan Chenga8e29892007-01-19 07:51:42 +0000920 if (isLegalCmpImmediate(C+1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +0000921 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
922 RHS = DAG.getConstant(C+1, MVT::i32);
923 }
924 break;
925 case ISD::SETULE:
926 case ISD::SETUGT:
927 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb)) {
928 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Evan Chenga8e29892007-01-19 07:51:42 +0000929 RHS = DAG.getConstant(C+1, MVT::i32);
930 }
931 break;
932 }
933 }
934 }
935
936 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
937 ARMCC = DAG.getConstant(CondCode, MVT::i32);
938 return DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
939}
940
941/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
942static SDOperand getVFPCmp(SDOperand LHS, SDOperand RHS, SelectionDAG &DAG) {
943 SDOperand Cmp;
944 if (!isFloatingPointZero(RHS))
945 Cmp = DAG.getNode(ARMISD::CMPFP, MVT::Flag, LHS, RHS);
946 else
947 Cmp = DAG.getNode(ARMISD::CMPFPw0, MVT::Flag, LHS);
948 return DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp);
949}
950
951static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG,
952 const ARMSubtarget *ST) {
953 MVT::ValueType VT = Op.getValueType();
954 SDOperand LHS = Op.getOperand(0);
955 SDOperand RHS = Op.getOperand(1);
956 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
957 SDOperand TrueVal = Op.getOperand(2);
958 SDOperand FalseVal = Op.getOperand(3);
959
960 if (LHS.getValueType() == MVT::i32) {
961 SDOperand ARMCC;
962 SDOperand Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb());
963 return DAG.getNode(ARMISD::CMOV, VT, FalseVal, TrueVal, ARMCC, Cmp);
964 }
965
966 ARMCC::CondCodes CondCode, CondCode2;
967 if (FPCCToARMCC(CC, CondCode, CondCode2))
968 std::swap(TrueVal, FalseVal);
969
970 SDOperand ARMCC = DAG.getConstant(CondCode, MVT::i32);
971 SDOperand Cmp = getVFPCmp(LHS, RHS, DAG);
972 SDOperand Result = DAG.getNode(ARMISD::CMOV, VT, FalseVal, TrueVal,
973 ARMCC, Cmp);
974 if (CondCode2 != ARMCC::AL) {
975 SDOperand ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
976 // FIXME: Needs another CMP because flag can have but one use.
977 SDOperand Cmp2 = getVFPCmp(LHS, RHS, DAG);
978 Result = DAG.getNode(ARMISD::CMOV, VT, Result, TrueVal, ARMCC2, Cmp2);
979 }
980 return Result;
981}
982
983static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG,
984 const ARMSubtarget *ST) {
985 SDOperand Chain = Op.getOperand(0);
986 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
987 SDOperand LHS = Op.getOperand(2);
988 SDOperand RHS = Op.getOperand(3);
989 SDOperand Dest = Op.getOperand(4);
990
991 if (LHS.getValueType() == MVT::i32) {
992 SDOperand ARMCC;
993 SDOperand Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb());
994 return DAG.getNode(ARMISD::BRCOND, MVT::Other, Chain, Dest, ARMCC, Cmp);
995 }
996
997 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
998 ARMCC::CondCodes CondCode, CondCode2;
999 if (FPCCToARMCC(CC, CondCode, CondCode2))
1000 // Swap the LHS/RHS of the comparison if needed.
1001 std::swap(LHS, RHS);
1002
1003 SDOperand Cmp = getVFPCmp(LHS, RHS, DAG);
1004 SDOperand ARMCC = DAG.getConstant(CondCode, MVT::i32);
1005 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
1006 SDOperand Ops[] = { Chain, Dest, ARMCC, Cmp };
1007 SDOperand Res = DAG.getNode(ARMISD::BRCOND, VTList, Ops, 4);
1008 if (CondCode2 != ARMCC::AL) {
1009 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
1010 SDOperand Ops[] = { Res, Dest, ARMCC, Res.getValue(1) };
1011 Res = DAG.getNode(ARMISD::BRCOND, VTList, Ops, 4);
1012 }
1013 return Res;
1014}
1015
1016SDOperand ARMTargetLowering::LowerBR_JT(SDOperand Op, SelectionDAG &DAG) {
1017 SDOperand Chain = Op.getOperand(0);
1018 SDOperand Table = Op.getOperand(1);
1019 SDOperand Index = Op.getOperand(2);
1020
1021 MVT::ValueType PTy = getPointerTy();
1022 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1023 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
1024 SDOperand UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
1025 SDOperand JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
1026 Table = DAG.getNode(ARMISD::WrapperJT, MVT::i32, JTI, UId);
1027 Index = DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(4, PTy));
1028 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
1029 bool isPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
1030 Addr = DAG.getLoad(isPIC ? MVT::i32 : PTy, Chain, Addr, NULL, 0);
1031 Chain = Addr.getValue(1);
1032 if (isPIC)
1033 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Table);
1034 return DAG.getNode(ARMISD::BR_JT, MVT::Other, Chain, Addr, JTI, UId);
1035}
1036
1037static SDOperand LowerFP_TO_INT(SDOperand Op, SelectionDAG &DAG) {
1038 unsigned Opc =
1039 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
1040 Op = DAG.getNode(Opc, MVT::f32, Op.getOperand(0));
1041 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
1042}
1043
1044static SDOperand LowerINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
1045 MVT::ValueType VT = Op.getValueType();
1046 unsigned Opc =
1047 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1048
1049 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Op.getOperand(0));
1050 return DAG.getNode(Opc, VT, Op);
1051}
1052
1053static SDOperand LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
1054 // Implement fcopysign with a fabs and a conditional fneg.
1055 SDOperand Tmp0 = Op.getOperand(0);
1056 SDOperand Tmp1 = Op.getOperand(1);
1057 MVT::ValueType VT = Op.getValueType();
1058 MVT::ValueType SrcVT = Tmp1.getValueType();
1059 SDOperand AbsVal = DAG.getNode(ISD::FABS, VT, Tmp0);
1060 SDOperand Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG);
1061 SDOperand ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1062 return DAG.getNode(ARMISD::CNEG, VT, AbsVal, AbsVal, ARMCC, Cmp);
1063}
1064
1065static SDOperand LowerBIT_CONVERT(SDOperand Op, SelectionDAG &DAG) {
1066 // Turn f64->i64 into FMRRD.
1067 assert(Op.getValueType() == MVT::i64 &&
1068 Op.getOperand(0).getValueType() == MVT::f64);
1069
1070 Op = Op.getOperand(0);
1071 SDOperand Cvt = DAG.getNode(ARMISD::FMRRD, DAG.getVTList(MVT::i32, MVT::i32),
1072 &Op, 1);
1073
1074 // Merge the pieces into a single i64 value.
1075 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Cvt, Cvt.getValue(1));
1076}
1077
1078static SDOperand LowerMUL(SDOperand Op, SelectionDAG &DAG) {
1079 // FIXME: All this code is target-independent. Create a new target-indep
1080 // MULHILO node and move this code to the legalizer.
1081 //
1082 assert(Op.getValueType() == MVT::i64 && "Only handles i64 expand right now!");
1083
1084 SDOperand LL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
1085 DAG.getConstant(0, MVT::i32));
1086 SDOperand RL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(1),
1087 DAG.getConstant(0, MVT::i32));
1088
1089 const TargetLowering &TL = DAG.getTargetLoweringInfo();
1090 unsigned LHSSB = TL.ComputeNumSignBits(Op.getOperand(0));
1091 unsigned RHSSB = TL.ComputeNumSignBits(Op.getOperand(1));
1092
1093 SDOperand Lo, Hi;
1094 // Figure out how to lower this multiply.
1095 if (LHSSB >= 33 && RHSSB >= 33) {
1096 // If the input values are both sign extended, we can emit a mulhs+mul.
1097 Lo = DAG.getNode(ISD::MUL, MVT::i32, LL, RL);
1098 Hi = DAG.getNode(ISD::MULHS, MVT::i32, LL, RL);
1099 } else if (LHSSB == 32 && RHSSB == 32 &&
1100 TL.MaskedValueIsZero(Op.getOperand(0), 0xFFFFFFFF00000000ULL) &&
1101 TL.MaskedValueIsZero(Op.getOperand(1), 0xFFFFFFFF00000000ULL)) {
1102 // If the inputs are zero extended, use mulhu.
1103 Lo = DAG.getNode(ISD::MUL, MVT::i32, LL, RL);
1104 Hi = DAG.getNode(ISD::MULHU, MVT::i32, LL, RL);
1105 } else {
1106 SDOperand LH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
1107 DAG.getConstant(1, MVT::i32));
1108 SDOperand RH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(1),
1109 DAG.getConstant(1, MVT::i32));
1110
1111 // Lo,Hi = umul LHS, RHS.
1112 SDOperand Ops[] = { LL, RL };
1113 SDOperand UMul64 = DAG.getNode(ARMISD::MULHILOU,
1114 DAG.getVTList(MVT::i32, MVT::i32), Ops, 2);
1115 Lo = UMul64;
1116 Hi = UMul64.getValue(1);
1117 RH = DAG.getNode(ISD::MUL, MVT::i32, LL, RH);
1118 LH = DAG.getNode(ISD::MUL, MVT::i32, LH, RL);
1119 Hi = DAG.getNode(ISD::ADD, MVT::i32, Hi, RH);
1120 Hi = DAG.getNode(ISD::ADD, MVT::i32, Hi, LH);
1121 }
1122
1123 // Merge the pieces into a single i64 value.
1124 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1125}
1126
1127static SDOperand LowerMULHU(SDOperand Op, SelectionDAG &DAG) {
1128 SDOperand Ops[] = { Op.getOperand(0), Op.getOperand(1) };
1129 return DAG.getNode(ARMISD::MULHILOU,
1130 DAG.getVTList(MVT::i32, MVT::i32), Ops, 2).getValue(1);
1131}
1132
1133static SDOperand LowerMULHS(SDOperand Op, SelectionDAG &DAG) {
1134 SDOperand Ops[] = { Op.getOperand(0), Op.getOperand(1) };
1135 return DAG.getNode(ARMISD::MULHILOS,
1136 DAG.getVTList(MVT::i32, MVT::i32), Ops, 2).getValue(1);
1137}
1138
1139static SDOperand LowerSRx(SDOperand Op, SelectionDAG &DAG,
1140 const ARMSubtarget *ST) {
1141 assert(Op.getValueType() == MVT::i64 &&
1142 (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SRA) &&
1143 "Unknown shift to lower!");
1144
1145 // We only lower SRA, SRL of 1 here, all others use generic lowering.
1146 if (!isa<ConstantSDNode>(Op.getOperand(1)) ||
1147 cast<ConstantSDNode>(Op.getOperand(1))->getValue() != 1)
1148 return SDOperand();
1149
1150 // If we are in thumb mode, we don't have RRX.
1151 if (ST->isThumb()) return SDOperand();
1152
1153 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
1154 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
1155 DAG.getConstant(0, MVT::i32));
1156 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
1157 DAG.getConstant(1, MVT::i32));
1158
1159 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
1160 // captures the result into a carry flag.
1161 unsigned Opc = Op.getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
1162 Hi = DAG.getNode(Opc, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
1163
1164 // The low part is an ARMISD::RRX operand, which shifts the carry in.
1165 Lo = DAG.getNode(ARMISD::RRX, MVT::i32, Lo, Hi.getValue(1));
1166
1167 // Merge the pieces into a single i64 value.
1168 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1169}
1170
1171SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1172 switch (Op.getOpcode()) {
1173 default: assert(0 && "Don't know how to custom lower this!"); abort();
1174 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
1175 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
1176 case ISD::CALL: return LowerCALL(Op, DAG);
1177 case ISD::RET: return LowerRET(Op, DAG);
1178 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
1179 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
1180 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
1181 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
1182 case ISD::SINT_TO_FP:
1183 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
1184 case ISD::FP_TO_SINT:
1185 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
1186 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
1187 case ISD::BIT_CONVERT: return LowerBIT_CONVERT(Op, DAG);
1188 case ISD::MUL: return LowerMUL(Op, DAG);
1189 case ISD::MULHU: return LowerMULHU(Op, DAG);
1190 case ISD::MULHS: return LowerMULHS(Op, DAG);
1191 case ISD::SRL:
1192 case ISD::SRA: return LowerSRx(Op, DAG, Subtarget);
1193 case ISD::FORMAL_ARGUMENTS:
1194 return LowerFORMAL_ARGUMENTS(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00001195 case ISD::RETURNADDR: break;
1196 case ISD::FRAMEADDR: break;
Evan Chenga8e29892007-01-19 07:51:42 +00001197 }
Nate Begemanbcc5f362007-01-29 22:58:52 +00001198 return SDOperand();
Evan Chenga8e29892007-01-19 07:51:42 +00001199}
1200
1201//===----------------------------------------------------------------------===//
1202// ARM Scheduler Hooks
1203//===----------------------------------------------------------------------===//
1204
1205MachineBasicBlock *
1206ARMTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1207 MachineBasicBlock *BB) {
1208 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1209 switch (MI->getOpcode()) {
1210 default: assert(false && "Unexpected instr type to insert");
1211 case ARM::tMOVCCr: {
1212 // To "insert" a SELECT_CC instruction, we actually have to insert the
1213 // diamond control-flow pattern. The incoming instruction knows the
1214 // destination vreg to set, the condition code register to branch on, the
1215 // true/false values to select between, and a branch opcode to use.
1216 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1217 ilist<MachineBasicBlock>::iterator It = BB;
1218 ++It;
1219
1220 // thisMBB:
1221 // ...
1222 // TrueVal = ...
1223 // cmpTY ccX, r1, r2
1224 // bCC copy1MBB
1225 // fallthrough --> copy0MBB
1226 MachineBasicBlock *thisMBB = BB;
1227 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1228 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1229 BuildMI(BB, TII->get(ARM::tBcc)).addMBB(sinkMBB)
1230 .addImm(MI->getOperand(3).getImm());
1231 MachineFunction *F = BB->getParent();
1232 F->getBasicBlockList().insert(It, copy0MBB);
1233 F->getBasicBlockList().insert(It, sinkMBB);
1234 // Update machine-CFG edges by first adding all successors of the current
1235 // block to the new block which will contain the Phi node for the select.
1236 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1237 e = BB->succ_end(); i != e; ++i)
1238 sinkMBB->addSuccessor(*i);
1239 // Next, remove all successors of the current block, and add the true
1240 // and fallthrough blocks as its successors.
1241 while(!BB->succ_empty())
1242 BB->removeSuccessor(BB->succ_begin());
1243 BB->addSuccessor(copy0MBB);
1244 BB->addSuccessor(sinkMBB);
1245
1246 // copy0MBB:
1247 // %FalseValue = ...
1248 // # fallthrough to sinkMBB
1249 BB = copy0MBB;
1250
1251 // Update machine-CFG edges
1252 BB->addSuccessor(sinkMBB);
1253
1254 // sinkMBB:
1255 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1256 // ...
1257 BB = sinkMBB;
1258 BuildMI(BB, TII->get(ARM::PHI), MI->getOperand(0).getReg())
1259 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1260 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1261
1262 delete MI; // The pseudo instruction is gone now.
1263 return BB;
1264 }
1265 }
1266}
1267
1268//===----------------------------------------------------------------------===//
1269// ARM Optimization Hooks
1270//===----------------------------------------------------------------------===//
1271
Evan Chengb01fad62007-03-12 23:30:29 +00001272/// isLegalAddressImmediate - Return true if the integer value can be used
1273/// as the offset of the target addressing mode for load / store of the
1274/// given type.
1275bool ARMTargetLowering::isLegalAddressImmediate(int64_t V,const Type *Ty) const{
Evan Cheng961f8792007-03-13 20:37:59 +00001276 if (V == 0)
1277 return true;
1278
Evan Chengb01fad62007-03-12 23:30:29 +00001279 MVT::ValueType VT = getValueType(Ty);
1280 if (Subtarget->isThumb()) {
1281 if (V < 0)
1282 return false;
1283
1284 unsigned Scale = 1;
1285 switch (VT) {
1286 default: return false;
1287 case MVT::i1:
1288 case MVT::i8:
1289 // Scale == 1;
1290 break;
1291 case MVT::i16:
1292 // Scale == 2;
1293 Scale = 2;
1294 break;
1295 case MVT::i32:
1296 // Scale == 4;
1297 Scale = 4;
1298 break;
1299 }
1300
1301 if ((V & (Scale - 1)) != 0)
1302 return false;
1303 V /= Scale;
1304 return V == V & ((1LL << 5) - 1);
1305 }
1306
1307 if (V < 0)
1308 V = - V;
1309 switch (VT) {
1310 default: return false;
1311 case MVT::i1:
1312 case MVT::i8:
1313 case MVT::i32:
1314 // +- imm12
1315 return V == V & ((1LL << 12) - 1);
1316 case MVT::i16:
1317 // +- imm8
1318 return V == V & ((1LL << 8) - 1);
1319 case MVT::f32:
1320 case MVT::f64:
1321 if (!Subtarget->hasVFP2())
1322 return false;
1323 if ((V % 3) != 0)
1324 return false;
1325 V >>= 2;
1326 return V == V & ((1LL << 8) - 1);
1327 }
Evan Chenga8e29892007-01-19 07:51:42 +00001328}
1329
1330bool ARMTargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
1331 return false;
1332}
1333
Evan Chengb01fad62007-03-12 23:30:29 +00001334/// isLegalAddressScale - Return true if the integer value can be used as
1335/// the scale of the target addressing mode for load / store of the given
1336/// type.
1337bool ARMTargetLowering::isLegalAddressScale(int64_t S, const Type *Ty) const {
1338 if (Subtarget->isThumb())
1339 return false;
1340
1341 MVT::ValueType VT = getValueType(Ty);
1342 switch (VT) {
1343 default: return false;
1344 case MVT::i1:
1345 case MVT::i8:
1346 case MVT::i32:
1347 // r + r
1348 if (S == 2)
1349 return true;
1350 // r + r << imm
1351 S &= ~1;
1352 return isPowerOf2_32(S);
1353 }
1354}
1355
Evan Chenga8e29892007-01-19 07:51:42 +00001356static bool getIndexedAddressParts(SDNode *Ptr, MVT::ValueType VT,
1357 bool isSEXTLoad, SDOperand &Base,
1358 SDOperand &Offset, bool &isInc,
1359 SelectionDAG &DAG) {
1360 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
1361 return false;
1362
1363 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
1364 // AddressingMode 3
1365 Base = Ptr->getOperand(0);
1366 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
1367 int RHSC = (int)RHS->getValue();
1368 if (RHSC < 0 && RHSC > -256) {
1369 isInc = false;
1370 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1371 return true;
1372 }
1373 }
1374 isInc = (Ptr->getOpcode() == ISD::ADD);
1375 Offset = Ptr->getOperand(1);
1376 return true;
1377 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
1378 // AddressingMode 2
1379 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
1380 int RHSC = (int)RHS->getValue();
1381 if (RHSC < 0 && RHSC > -0x1000) {
1382 isInc = false;
1383 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1384 Base = Ptr->getOperand(0);
1385 return true;
1386 }
1387 }
1388
1389 if (Ptr->getOpcode() == ISD::ADD) {
1390 isInc = true;
1391 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
1392 if (ShOpcVal != ARM_AM::no_shift) {
1393 Base = Ptr->getOperand(1);
1394 Offset = Ptr->getOperand(0);
1395 } else {
1396 Base = Ptr->getOperand(0);
1397 Offset = Ptr->getOperand(1);
1398 }
1399 return true;
1400 }
1401
1402 isInc = (Ptr->getOpcode() == ISD::ADD);
1403 Base = Ptr->getOperand(0);
1404 Offset = Ptr->getOperand(1);
1405 return true;
1406 }
1407
1408 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
1409 return false;
1410}
1411
1412/// getPreIndexedAddressParts - returns true by value, base pointer and
1413/// offset pointer and addressing mode by reference if the node's address
1414/// can be legally represented as pre-indexed load / store address.
1415bool
1416ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDOperand &Base,
1417 SDOperand &Offset,
1418 ISD::MemIndexedMode &AM,
1419 SelectionDAG &DAG) {
1420 if (Subtarget->isThumb())
1421 return false;
1422
1423 MVT::ValueType VT;
1424 SDOperand Ptr;
1425 bool isSEXTLoad = false;
1426 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1427 Ptr = LD->getBasePtr();
1428 VT = LD->getLoadedVT();
1429 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1430 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
1431 Ptr = ST->getBasePtr();
1432 VT = ST->getStoredVT();
1433 } else
1434 return false;
1435
1436 bool isInc;
1437 bool isLegal = getIndexedAddressParts(Ptr.Val, VT, isSEXTLoad, Base, Offset,
1438 isInc, DAG);
1439 if (isLegal) {
1440 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
1441 return true;
1442 }
1443 return false;
1444}
1445
1446/// getPostIndexedAddressParts - returns true by value, base pointer and
1447/// offset pointer and addressing mode by reference if this node can be
1448/// combined with a load / store to form a post-indexed load / store.
1449bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
1450 SDOperand &Base,
1451 SDOperand &Offset,
1452 ISD::MemIndexedMode &AM,
1453 SelectionDAG &DAG) {
1454 if (Subtarget->isThumb())
1455 return false;
1456
1457 MVT::ValueType VT;
1458 SDOperand Ptr;
1459 bool isSEXTLoad = false;
1460 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1461 VT = LD->getLoadedVT();
1462 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1463 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
1464 VT = ST->getStoredVT();
1465 } else
1466 return false;
1467
1468 bool isInc;
1469 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
1470 isInc, DAG);
1471 if (isLegal) {
1472 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
1473 return true;
1474 }
1475 return false;
1476}
1477
1478void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
1479 uint64_t Mask,
1480 uint64_t &KnownZero,
1481 uint64_t &KnownOne,
1482 unsigned Depth) const {
1483 KnownZero = 0;
1484 KnownOne = 0;
1485 switch (Op.getOpcode()) {
1486 default: break;
1487 case ARMISD::CMOV: {
1488 // Bits are known zero/one if known on the LHS and RHS.
1489 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1490 if (KnownZero == 0 && KnownOne == 0) return;
1491
1492 uint64_t KnownZeroRHS, KnownOneRHS;
1493 ComputeMaskedBits(Op.getOperand(1), Mask,
1494 KnownZeroRHS, KnownOneRHS, Depth+1);
1495 KnownZero &= KnownZeroRHS;
1496 KnownOne &= KnownOneRHS;
1497 return;
1498 }
1499 }
1500}
1501
1502//===----------------------------------------------------------------------===//
1503// ARM Inline Assembly Support
1504//===----------------------------------------------------------------------===//
1505
1506/// getConstraintType - Given a constraint letter, return the type of
1507/// constraint it is for this target.
1508ARMTargetLowering::ConstraintType
1509ARMTargetLowering::getConstraintType(char ConstraintLetter) const {
1510 switch (ConstraintLetter) {
1511 case 'l':
1512 return C_RegisterClass;
1513 default: return TargetLowering::getConstraintType(ConstraintLetter);
1514 }
1515}
1516
1517std::pair<unsigned, const TargetRegisterClass*>
1518ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
1519 MVT::ValueType VT) const {
1520 if (Constraint.size() == 1) {
1521 // GCC RS6000 Constraint Letters
1522 switch (Constraint[0]) {
1523 case 'l':
1524 // FIXME: in thumb mode, 'l' is only low-regs.
1525 // FALL THROUGH.
1526 case 'r':
1527 return std::make_pair(0U, ARM::GPRRegisterClass);
1528 break;
1529 }
1530 }
1531 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1532}
1533
1534std::vector<unsigned> ARMTargetLowering::
1535getRegClassForInlineAsmConstraint(const std::string &Constraint,
1536 MVT::ValueType VT) const {
1537 if (Constraint.size() != 1)
1538 return std::vector<unsigned>();
1539
1540 switch (Constraint[0]) { // GCC ARM Constraint Letters
1541 default: break;
1542 case 'l':
1543 case 'r':
1544 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
1545 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
1546 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
1547 ARM::R12, ARM::LR, 0);
1548 }
1549
1550 return std::vector<unsigned>();
1551}