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