blob: 585b660b9cf11023bc6bb4dc7051181b5669bf90 [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
Evan Chengb1df8f22007-04-27 08:15:43 +000041 if (Subtarget->isTargetDarwin()) {
42 // Don't have these.
43 setLibcallName(RTLIB::UINTTOFP_I64_F32, NULL);
44 setLibcallName(RTLIB::UINTTOFP_I64_F64, NULL);
Evan Chenga8e29892007-01-19 07:51:42 +000045
Evan Chengb1df8f22007-04-27 08:15:43 +000046 // Uses VFP for Thumb libfuncs if available.
47 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
48 // Single-precision floating-point arithmetic.
49 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
50 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
51 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
52 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +000053
Evan Chengb1df8f22007-04-27 08:15:43 +000054 // Double-precision floating-point arithmetic.
55 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
56 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
57 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
58 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +000059
Evan Chengb1df8f22007-04-27 08:15:43 +000060 // Single-precision comparisons.
61 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
62 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
63 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
64 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
65 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
66 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
67 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
68 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +000069
Evan Chengb1df8f22007-04-27 08:15:43 +000070 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
71 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
72 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
73 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
74 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
75 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
76 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
77 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +000078
Evan Chengb1df8f22007-04-27 08:15:43 +000079 // Double-precision comparisons.
80 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
81 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
82 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
83 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
84 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
85 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
86 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
87 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +000088
Evan Chengb1df8f22007-04-27 08:15:43 +000089 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
90 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
91 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
92 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
93 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
94 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
95 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
96 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +000097
Evan Chengb1df8f22007-04-27 08:15:43 +000098 // Floating-point to integer conversions.
99 // i64 conversions are done via library routines even when generating VFP
100 // instructions, so use the same ones.
101 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
102 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
103 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
104 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000105
Evan Chengb1df8f22007-04-27 08:15:43 +0000106 // Conversions between floating types.
107 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
108 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
109
110 // Integer to floating-point conversions.
111 // i64 conversions are done via library routines even when generating VFP
112 // instructions, so use the same ones.
113 // FIXME: There appears to be some naming inconsistency in ARM libgcc: e.g.
114 // __floatunsidf vs. __floatunssidfvfp.
115 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
116 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
117 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
118 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
119 }
Evan Chenga8e29892007-01-19 07:51:42 +0000120 }
121
122 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
Evan Chengb6ab2542007-01-31 08:40:13 +0000123 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000124 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
125 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
126 }
127
128 // ARM does not have f32 extending load.
129 setLoadXAction(ISD::EXTLOAD, MVT::f32, Expand);
130
131 // ARM supports all 4 flavors of integer indexed load / store.
132 for (unsigned im = (unsigned)ISD::PRE_INC;
133 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
134 setIndexedLoadAction(im, MVT::i1, Legal);
135 setIndexedLoadAction(im, MVT::i8, Legal);
136 setIndexedLoadAction(im, MVT::i16, Legal);
137 setIndexedLoadAction(im, MVT::i32, Legal);
138 setIndexedStoreAction(im, MVT::i1, Legal);
139 setIndexedStoreAction(im, MVT::i8, Legal);
140 setIndexedStoreAction(im, MVT::i16, Legal);
141 setIndexedStoreAction(im, MVT::i32, Legal);
142 }
143
144 // i64 operation support.
145 if (Subtarget->isThumb()) {
146 setOperationAction(ISD::MUL, MVT::i64, Expand);
147 setOperationAction(ISD::MULHU, MVT::i32, Expand);
148 setOperationAction(ISD::MULHS, MVT::i32, Expand);
149 } else {
150 setOperationAction(ISD::MUL, MVT::i64, Custom);
151 setOperationAction(ISD::MULHU, MVT::i32, Custom);
152 if (!Subtarget->hasV6Ops())
153 setOperationAction(ISD::MULHS, MVT::i32, Custom);
154 }
155 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
156 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
157 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
158 setOperationAction(ISD::SRL, MVT::i64, Custom);
159 setOperationAction(ISD::SRA, MVT::i64, Custom);
160
161 // ARM does not have ROTL.
162 setOperationAction(ISD::ROTL, MVT::i32, Expand);
163 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
164 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
Evan Chengb0636152007-02-01 23:34:03 +0000165 if (!Subtarget->hasV5TOps() || Subtarget->isThumb())
Evan Chenga8e29892007-01-19 07:51:42 +0000166 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
167
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000168 // Only ARMv6 has BSWAP.
169 if (!Subtarget->hasV6Ops())
Chris Lattner1719e132007-03-20 02:25:53 +0000170 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000171
Evan Chenga8e29892007-01-19 07:51:42 +0000172 // These are expanded into libcalls.
173 setOperationAction(ISD::SDIV, MVT::i32, Expand);
174 setOperationAction(ISD::UDIV, MVT::i32, Expand);
175 setOperationAction(ISD::SREM, MVT::i32, Expand);
176 setOperationAction(ISD::UREM, MVT::i32, Expand);
177
178 // Support label based line numbers.
179 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
180 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000181
182 setOperationAction(ISD::RET, MVT::Other, Custom);
183 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
184 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000185 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000186 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000187
188 // Expand mem operations genericly.
189 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
190 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
191 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
192
193 // Use the default implementation.
194 setOperationAction(ISD::VASTART , MVT::Other, Expand);
195 setOperationAction(ISD::VAARG , MVT::Other, Expand);
196 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
197 setOperationAction(ISD::VAEND , MVT::Other, Expand);
198 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
199 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
200 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
201
202 if (!Subtarget->hasV6Ops()) {
203 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
204 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
205 }
206 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
207
Evan Chengb6ab2542007-01-31 08:40:13 +0000208 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb())
Evan Chenga8e29892007-01-19 07:51:42 +0000209 // Turn f64->i64 into FMRRD iff target supports vfp2.
210 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
211
212 setOperationAction(ISD::SETCC , MVT::i32, Expand);
213 setOperationAction(ISD::SETCC , MVT::f32, Expand);
214 setOperationAction(ISD::SETCC , MVT::f64, Expand);
215 setOperationAction(ISD::SELECT , MVT::i32, Expand);
216 setOperationAction(ISD::SELECT , MVT::f32, Expand);
217 setOperationAction(ISD::SELECT , MVT::f64, Expand);
218 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
219 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
220 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
221
222 setOperationAction(ISD::BRCOND , MVT::Other, Expand);
223 setOperationAction(ISD::BR_CC , MVT::i32, Custom);
224 setOperationAction(ISD::BR_CC , MVT::f32, Custom);
225 setOperationAction(ISD::BR_CC , MVT::f64, Custom);
226 setOperationAction(ISD::BR_JT , MVT::Other, Custom);
227
228 setOperationAction(ISD::VASTART, MVT::Other, Custom);
229 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
230 setOperationAction(ISD::VAEND, MVT::Other, Expand);
231 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
232 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
233
234 // FP Constants can't be immediates.
235 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
236 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
237
238 // We don't support sin/cos/fmod/copysign
239 setOperationAction(ISD::FSIN , MVT::f64, Expand);
240 setOperationAction(ISD::FSIN , MVT::f32, Expand);
241 setOperationAction(ISD::FCOS , MVT::f32, Expand);
242 setOperationAction(ISD::FCOS , MVT::f64, Expand);
243 setOperationAction(ISD::FREM , MVT::f64, Expand);
244 setOperationAction(ISD::FREM , MVT::f32, Expand);
245 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
246 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
247
248 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
249 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
250 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
251 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
252 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
253
254 setStackPointerRegisterToSaveRestore(ARM::SP);
255
256 setSchedulingPreference(SchedulingForRegPressure);
257 computeRegisterProperties();
258}
259
260
261const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
262 switch (Opcode) {
263 default: return 0;
264 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000265 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
266 case ARMISD::CALL: return "ARMISD::CALL";
267 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
268 case ARMISD::tCALL: return "ARMISD::tCALL";
269 case ARMISD::BRCOND: return "ARMISD::BRCOND";
270 case ARMISD::BR_JT: return "ARMISD::BR_JT";
271 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
272 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
273 case ARMISD::CMP: return "ARMISD::CMP";
Lauro Ramos Venancio99966632007-04-02 01:30:03 +0000274 case ARMISD::CMPNZ: return "ARMISD::CMPNZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000275 case ARMISD::CMPFP: return "ARMISD::CMPFP";
276 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
277 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
278 case ARMISD::CMOV: return "ARMISD::CMOV";
279 case ARMISD::CNEG: return "ARMISD::CNEG";
280
281 case ARMISD::FTOSI: return "ARMISD::FTOSI";
282 case ARMISD::FTOUI: return "ARMISD::FTOUI";
283 case ARMISD::SITOF: return "ARMISD::SITOF";
284 case ARMISD::UITOF: return "ARMISD::UITOF";
285 case ARMISD::MULHILOU: return "ARMISD::MULHILOU";
286 case ARMISD::MULHILOS: return "ARMISD::MULHILOS";
287
288 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
289 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
290 case ARMISD::RRX: return "ARMISD::RRX";
291
292 case ARMISD::FMRRD: return "ARMISD::FMRRD";
293 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000294
295 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Evan Chenga8e29892007-01-19 07:51:42 +0000296 }
297}
298
299//===----------------------------------------------------------------------===//
300// Lowering Code
301//===----------------------------------------------------------------------===//
302
303
304/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
305static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
306 switch (CC) {
307 default: assert(0 && "Unknown condition code!");
308 case ISD::SETNE: return ARMCC::NE;
309 case ISD::SETEQ: return ARMCC::EQ;
310 case ISD::SETGT: return ARMCC::GT;
311 case ISD::SETGE: return ARMCC::GE;
312 case ISD::SETLT: return ARMCC::LT;
313 case ISD::SETLE: return ARMCC::LE;
314 case ISD::SETUGT: return ARMCC::HI;
315 case ISD::SETUGE: return ARMCC::HS;
316 case ISD::SETULT: return ARMCC::LO;
317 case ISD::SETULE: return ARMCC::LS;
318 }
319}
320
321/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. It
322/// returns true if the operands should be inverted to form the proper
323/// comparison.
324static bool FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
325 ARMCC::CondCodes &CondCode2) {
326 bool Invert = false;
327 CondCode2 = ARMCC::AL;
328 switch (CC) {
329 default: assert(0 && "Unknown FP condition!");
330 case ISD::SETEQ:
331 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
332 case ISD::SETGT:
333 case ISD::SETOGT: CondCode = ARMCC::GT; break;
334 case ISD::SETGE:
335 case ISD::SETOGE: CondCode = ARMCC::GE; break;
336 case ISD::SETOLT: CondCode = ARMCC::MI; break;
337 case ISD::SETOLE: CondCode = ARMCC::GT; Invert = true; break;
338 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
339 case ISD::SETO: CondCode = ARMCC::VC; break;
340 case ISD::SETUO: CondCode = ARMCC::VS; break;
341 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
342 case ISD::SETUGT: CondCode = ARMCC::HI; break;
343 case ISD::SETUGE: CondCode = ARMCC::PL; break;
344 case ISD::SETLT:
345 case ISD::SETULT: CondCode = ARMCC::LT; break;
346 case ISD::SETLE:
347 case ISD::SETULE: CondCode = ARMCC::LE; break;
348 case ISD::SETNE:
349 case ISD::SETUNE: CondCode = ARMCC::NE; break;
350 }
351 return Invert;
352}
353
354static void
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000355HowToPassArgument(MVT::ValueType ObjectVT, unsigned NumGPRs,
356 unsigned StackOffset, unsigned &NeededGPRs,
357 unsigned &NeededStackSize, unsigned &GPRPad,
358 unsigned &StackPad, unsigned Flags) {
359 NeededStackSize = 0;
360 NeededGPRs = 0;
361 StackPad = 0;
362 GPRPad = 0;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +0000363 unsigned align = (Flags >> ISD::ParamFlags::OrigAlignmentOffs);
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000364 GPRPad = NumGPRs % ((align + 3)/4);
365 StackPad = StackOffset % align;
366 unsigned firstGPR = NumGPRs + GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000367 switch (ObjectVT) {
368 default: assert(0 && "Unhandled argument type!");
369 case MVT::i32:
370 case MVT::f32:
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000371 if (firstGPR < 4)
372 NeededGPRs = 1;
Evan Chenga8e29892007-01-19 07:51:42 +0000373 else
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000374 NeededStackSize = 4;
Evan Chenga8e29892007-01-19 07:51:42 +0000375 break;
376 case MVT::i64:
377 case MVT::f64:
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000378 if (firstGPR < 3)
379 NeededGPRs = 2;
380 else if (firstGPR == 3) {
381 NeededGPRs = 1;
382 NeededStackSize = 4;
Evan Chenga8e29892007-01-19 07:51:42 +0000383 } else
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000384 NeededStackSize = 8;
Evan Chenga8e29892007-01-19 07:51:42 +0000385 }
386}
387
Evan Chengfc403422007-02-03 08:53:01 +0000388/// LowerCALL - Lowering a ISD::CALL node into a callseq_start <-
389/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
390/// nodes.
Evan Chenga8e29892007-01-19 07:51:42 +0000391SDOperand ARMTargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
392 MVT::ValueType RetVT= Op.Val->getValueType(0);
393 SDOperand Chain = Op.getOperand(0);
394 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
395 assert((CallConv == CallingConv::C ||
Evan Chenga8e29892007-01-19 07:51:42 +0000396 CallConv == CallingConv::Fast) && "unknown calling convention");
397 SDOperand Callee = Op.getOperand(4);
398 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
399 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
400 unsigned NumGPRs = 0; // GPRs used for parameter passing.
401
402 // Count how many bytes are to be pushed on the stack.
403 unsigned NumBytes = 0;
404
405 // Add up all the space actually used.
406 for (unsigned i = 0; i < NumOps; ++i) {
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000407 unsigned ObjSize;
408 unsigned ObjGPRs;
409 unsigned StackPad;
410 unsigned GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000411 MVT::ValueType ObjectVT = Op.getOperand(5+2*i).getValueType();
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000412 unsigned Flags = Op.getConstantOperandVal(5+2*i+1);
413 HowToPassArgument(ObjectVT, NumGPRs, NumBytes, ObjGPRs, ObjSize,
414 GPRPad, StackPad, Flags);
415 NumBytes += ObjSize + StackPad;
416 NumGPRs += ObjGPRs + GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000417 }
418
419 // Adjust the stack pointer for the new arguments...
420 // These operations are automatically eliminated by the prolog/epilog pass
421 Chain = DAG.getCALLSEQ_START(Chain,
422 DAG.getConstant(NumBytes, MVT::i32));
423
424 SDOperand StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
425
426 static const unsigned GPRArgRegs[] = {
427 ARM::R0, ARM::R1, ARM::R2, ARM::R3
428 };
429
430 NumGPRs = 0;
431 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
432 std::vector<SDOperand> MemOpChains;
433 for (unsigned i = 0; i != NumOps; ++i) {
434 SDOperand Arg = Op.getOperand(5+2*i);
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000435 unsigned Flags = Op.getConstantOperandVal(5+2*i+1);
Evan Chenga8e29892007-01-19 07:51:42 +0000436 MVT::ValueType ArgVT = Arg.getValueType();
437
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000438 unsigned ObjSize;
439 unsigned ObjGPRs;
440 unsigned GPRPad;
441 unsigned StackPad;
442 HowToPassArgument(ArgVT, NumGPRs, ArgOffset, ObjGPRs,
443 ObjSize, GPRPad, StackPad, Flags);
444 NumGPRs += GPRPad;
445 ArgOffset += StackPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000446 if (ObjGPRs > 0) {
447 switch (ArgVT) {
448 default: assert(0 && "Unexpected ValueType for argument!");
449 case MVT::i32:
450 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Arg));
451 break;
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000452 case MVT::f32:
Evan Chenga8e29892007-01-19 07:51:42 +0000453 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs],
454 DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg)));
455 break;
456 case MVT::i64: {
457 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Arg,
458 DAG.getConstant(0, getPointerTy()));
459 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Arg,
460 DAG.getConstant(1, getPointerTy()));
461 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Lo));
462 if (ObjGPRs == 2)
463 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1], Hi));
464 else {
465 SDOperand PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType());
466 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
467 MemOpChains.push_back(DAG.getStore(Chain, Hi, PtrOff, NULL, 0));
468 }
469 break;
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000470 }
Evan Chenga8e29892007-01-19 07:51:42 +0000471 case MVT::f64: {
472 SDOperand Cvt = DAG.getNode(ARMISD::FMRRD,
473 DAG.getVTList(MVT::i32, MVT::i32),
474 &Arg, 1);
475 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Cvt));
476 if (ObjGPRs == 2)
477 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1],
478 Cvt.getValue(1)));
479 else {
480 SDOperand PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType());
481 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
482 MemOpChains.push_back(DAG.getStore(Chain, Cvt.getValue(1), PtrOff,
483 NULL, 0));
484 }
485 break;
486 }
487 }
488 } else {
489 assert(ObjSize != 0);
490 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
491 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
492 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
493 }
494
495 NumGPRs += ObjGPRs;
496 ArgOffset += ObjSize;
497 }
498
499 if (!MemOpChains.empty())
500 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
501 &MemOpChains[0], MemOpChains.size());
502
503 // Build a sequence of copy-to-reg nodes chained together with token chain
504 // and flag operands which copy the outgoing args into the appropriate regs.
505 SDOperand InFlag;
506 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
507 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
508 InFlag);
509 InFlag = Chain.getValue(1);
510 }
511
512 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
513 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
514 // node so that legalize doesn't hack it.
515 bool isDirect = false;
516 bool isARMFunc = false;
517 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
518 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +0000519 isDirect = true;
Reid Spencer5cbf9852007-01-30 20:08:39 +0000520 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
Evan Chenga8e29892007-01-19 07:51:42 +0000521 GV->hasLinkOnceLinkage());
Evan Cheng970a4192007-01-19 19:28:01 +0000522 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +0000523 getTargetMachine().getRelocationModel() != Reloc::Static;
524 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +0000525 // tBX takes a register source operand.
526 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
527 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
528 ARMCP::CPStub, 4);
529 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 2);
530 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
531 Callee = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), CPAddr, NULL, 0);
532 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
533 Callee = DAG.getNode(ARMISD::PIC_ADD, getPointerTy(), Callee, PICLabel);
534 } else
535 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000536 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000537 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +0000538 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000539 getTargetMachine().getRelocationModel() != Reloc::Static;
540 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +0000541 // tBX takes a register source operand.
542 const char *Sym = S->getSymbol();
543 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
544 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(Sym, ARMPCLabelIndex,
545 ARMCP::CPStub, 4);
546 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 2);
547 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
548 Callee = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), CPAddr, NULL, 0);
549 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
550 Callee = DAG.getNode(ARMISD::PIC_ADD, getPointerTy(), Callee, PICLabel);
551 } else
552 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000553 }
554
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000555 // FIXME: handle tail calls differently.
556 unsigned CallOpc;
557 if (Subtarget->isThumb()) {
558 if (!Subtarget->hasV5TOps() && (!isDirect || isARMFunc))
559 CallOpc = ARMISD::CALL_NOLINK;
560 else
561 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
562 } else {
563 CallOpc = (isDirect || Subtarget->hasV5TOps())
564 ? ARMISD::CALL : ARMISD::CALL_NOLINK;
565 }
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +0000566 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb()) {
567 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000568 Chain = DAG.getCopyToReg(Chain, ARM::LR,
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +0000569 DAG.getNode(ISD::UNDEF, MVT::i32), InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000570 InFlag = Chain.getValue(1);
571 }
572
Evan Chenga8e29892007-01-19 07:51:42 +0000573 std::vector<MVT::ValueType> NodeTys;
574 NodeTys.push_back(MVT::Other); // Returns a chain
575 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
576
577 std::vector<SDOperand> Ops;
578 Ops.push_back(Chain);
579 Ops.push_back(Callee);
580
581 // Add argument registers to the end of the list so that they are known live
582 // into the call.
583 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
584 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
585 RegsToPass[i].second.getValueType()));
586
Evan Chenga8e29892007-01-19 07:51:42 +0000587 if (InFlag.Val)
588 Ops.push_back(InFlag);
589 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
590 InFlag = Chain.getValue(1);
591
592 SDOperand CSOps[] = { Chain, DAG.getConstant(NumBytes, MVT::i32), InFlag };
593 Chain = DAG.getNode(ISD::CALLSEQ_END,
594 DAG.getNodeValueTypes(MVT::Other, MVT::Flag),
595 ((RetVT != MVT::Other) ? 2 : 1), CSOps, 3);
596 if (RetVT != MVT::Other)
597 InFlag = Chain.getValue(1);
598
599 std::vector<SDOperand> ResultVals;
600 NodeTys.clear();
601
602 // If the call has results, copy the values out of the ret val registers.
603 switch (RetVT) {
604 default: assert(0 && "Unexpected ret value!");
605 case MVT::Other:
606 break;
607 case MVT::i32:
608 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
609 ResultVals.push_back(Chain.getValue(0));
610 if (Op.Val->getValueType(1) == MVT::i32) {
611 // Returns a i64 value.
612 Chain = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32,
613 Chain.getValue(2)).getValue(1);
614 ResultVals.push_back(Chain.getValue(0));
615 NodeTys.push_back(MVT::i32);
616 }
617 NodeTys.push_back(MVT::i32);
618 break;
619 case MVT::f32:
620 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
621 ResultVals.push_back(DAG.getNode(ISD::BIT_CONVERT, MVT::f32,
622 Chain.getValue(0)));
623 NodeTys.push_back(MVT::f32);
624 break;
625 case MVT::f64: {
626 SDOperand Lo = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
627 SDOperand Hi = DAG.getCopyFromReg(Lo, ARM::R1, MVT::i32, Lo.getValue(2));
628 ResultVals.push_back(DAG.getNode(ARMISD::FMDRR, MVT::f64, Lo, Hi));
629 NodeTys.push_back(MVT::f64);
630 break;
631 }
632 }
633
634 NodeTys.push_back(MVT::Other);
635
636 if (ResultVals.empty())
637 return Chain;
638
639 ResultVals.push_back(Chain);
640 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
641 ResultVals.size());
642 return Res.getValue(Op.ResNo);
643}
644
645static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
646 SDOperand Copy;
647 SDOperand Chain = Op.getOperand(0);
648 switch(Op.getNumOperands()) {
649 default:
650 assert(0 && "Do not know how to return this many arguments!");
651 abort();
652 case 1: {
653 SDOperand LR = DAG.getRegister(ARM::LR, MVT::i32);
654 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
655 }
656 case 3:
657 Op = Op.getOperand(1);
658 if (Op.getValueType() == MVT::f32) {
659 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
660 } else if (Op.getValueType() == MVT::f64) {
661 // Recursively legalize f64 -> i64.
662 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Op);
663 return DAG.getNode(ISD::RET, MVT::Other, Chain, Op,
664 DAG.getConstant(0, MVT::i32));
665 }
666 Copy = DAG.getCopyToReg(Chain, ARM::R0, Op, SDOperand());
667 if (DAG.getMachineFunction().liveout_empty())
668 DAG.getMachineFunction().addLiveOut(ARM::R0);
669 break;
670 case 5:
671 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
672 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
673 // If we haven't noted the R0+R1 are live out, do so now.
674 if (DAG.getMachineFunction().liveout_empty()) {
675 DAG.getMachineFunction().addLiveOut(ARM::R0);
676 DAG.getMachineFunction().addLiveOut(ARM::R1);
677 }
678 break;
679 }
680
681 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
682 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
683}
684
685// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
686// their target countpart wrapped in the ARMISD::Wrapper node. Suppose N is
687// one of the above mentioned nodes. It has to be wrapped because otherwise
688// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
689// be used to form addressing mode. These wrapped nodes will be selected
Evan Cheng9f6636f2007-03-19 07:48:02 +0000690// into MOVi.
Evan Chenga8e29892007-01-19 07:51:42 +0000691static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
692 MVT::ValueType PtrVT = Op.getValueType();
693 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
694 SDOperand Res;
695 if (CP->isMachineConstantPoolEntry())
696 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
697 CP->getAlignment());
698 else
699 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
700 CP->getAlignment());
701 return DAG.getNode(ARMISD::Wrapper, MVT::i32, Res);
702}
703
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000704// Lower ISD::GlobalTLSAddress using the "general dynamic" model
705SDOperand
706ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
707 SelectionDAG &DAG) {
708 MVT::ValueType PtrVT = getPointerTy();
709 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
710 ARMConstantPoolValue *CPV =
711 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
712 PCAdj, "tlsgd", true);
713 SDOperand Argument = DAG.getTargetConstantPool(CPV, PtrVT, 2);
714 Argument = DAG.getNode(ARMISD::Wrapper, MVT::i32, Argument);
715 Argument = DAG.getLoad(PtrVT, DAG.getEntryNode(), Argument, NULL, 0);
716 SDOperand Chain = Argument.getValue(1);
717
718 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
719 Argument = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Argument, PICLabel);
720
721 // call __tls_get_addr.
722 ArgListTy Args;
723 ArgListEntry Entry;
724 Entry.Node = Argument;
725 Entry.Ty = (const Type *) Type::Int32Ty;
726 Args.push_back(Entry);
727 std::pair<SDOperand, SDOperand> CallResult =
728 LowerCallTo(Chain, (const Type *) Type::Int32Ty, false, false,
729 CallingConv::C, false,
730 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG);
731 return CallResult.first;
732}
733
734// Lower ISD::GlobalTLSAddress using the "initial exec" or
735// "local exec" model.
736SDOperand
737ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
738 SelectionDAG &DAG) {
739 GlobalValue *GV = GA->getGlobal();
740 SDOperand Offset;
741 SDOperand Chain = DAG.getEntryNode();
742 MVT::ValueType PtrVT = getPointerTy();
743 // Get the Thread Pointer
744 SDOperand ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, PtrVT);
745
746 if (GV->isDeclaration()){
747 // initial exec model
748 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
749 ARMConstantPoolValue *CPV =
750 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
751 PCAdj, "gottpoff", true);
752 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 2);
753 Offset = DAG.getNode(ARMISD::Wrapper, MVT::i32, Offset);
754 Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0);
755 Chain = Offset.getValue(1);
756
757 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
758 Offset = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Offset, PICLabel);
759
760 Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0);
761 } else {
762 // local exec model
763 ARMConstantPoolValue *CPV =
764 new ARMConstantPoolValue(GV, ARMCP::CPValue, "tpoff");
765 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 2);
766 Offset = DAG.getNode(ARMISD::Wrapper, MVT::i32, Offset);
767 Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0);
768 }
769
770 // The address of the thread local variable is the add of the thread
771 // pointer with the offset of the variable.
772 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
773}
774
775SDOperand
776ARMTargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
777 // TODO: implement the "local dynamic" model
778 assert(Subtarget->isTargetELF() &&
779 "TLS not implemented for non-ELF targets");
780 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
781 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
782 // otherwise use the "Local Exec" TLS Model
783 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
784 return LowerToTLSGeneralDynamicModel(GA, DAG);
785 else
786 return LowerToTLSExecModels(GA, DAG);
787}
788
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000789SDOperand ARMTargetLowering::LowerGlobalAddressELF(SDOperand Op,
790 SelectionDAG &DAG) {
791 MVT::ValueType PtrVT = getPointerTy();
792 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
793 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
794 if (RelocM == Reloc::PIC_) {
795 bool UseGOTOFF = GV->hasInternalLinkage();
796 ARMConstantPoolValue *CPV =
797 new ARMConstantPoolValue(GV, ARMCP::CPValue, UseGOTOFF ? "GOTOFF":"GOT");
798 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
799 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
800 SDOperand Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
801 SDOperand Chain = Result.getValue(1);
802 SDOperand GOT = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PtrVT);
803 Result = DAG.getNode(ISD::ADD, PtrVT, Result, GOT);
804 if (!UseGOTOFF)
805 Result = DAG.getLoad(PtrVT, Chain, Result, NULL, 0);
806 return Result;
807 } else {
808 SDOperand CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 2);
809 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
810 return DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
811 }
812}
813
Evan Chenga8e29892007-01-19 07:51:42 +0000814/// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol
815/// even in dynamic-no-pic mode.
816static bool GVIsIndirectSymbol(GlobalValue *GV) {
817 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
Reid Spencer5cbf9852007-01-30 20:08:39 +0000818 (GV->isDeclaration() && !GV->hasNotBeenReadFromBytecode()));
Evan Chenga8e29892007-01-19 07:51:42 +0000819}
820
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000821SDOperand ARMTargetLowering::LowerGlobalAddressDarwin(SDOperand Op,
822 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +0000823 MVT::ValueType PtrVT = getPointerTy();
824 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
825 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000826 bool IsIndirect = GVIsIndirectSymbol(GV);
Evan Chenga8e29892007-01-19 07:51:42 +0000827 SDOperand CPAddr;
828 if (RelocM == Reloc::Static)
829 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 2);
830 else {
831 unsigned PCAdj = (RelocM != Reloc::PIC_)
832 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Chengc60e76d2007-01-30 20:37:08 +0000833 ARMCP::ARMCPKind Kind = IsIndirect ? ARMCP::CPNonLazyPtr
834 : ARMCP::CPValue;
Evan Chenga8e29892007-01-19 07:51:42 +0000835 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
Evan Chengc60e76d2007-01-30 20:37:08 +0000836 Kind, PCAdj);
Evan Chenga8e29892007-01-19 07:51:42 +0000837 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
838 }
839 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
840
841 SDOperand Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
842 SDOperand Chain = Result.getValue(1);
843
844 if (RelocM == Reloc::PIC_) {
845 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
846 Result = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Result, PICLabel);
847 }
848 if (IsIndirect)
849 Result = DAG.getLoad(PtrVT, Chain, Result, NULL, 0);
850
851 return Result;
852}
853
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000854SDOperand ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDOperand Op,
855 SelectionDAG &DAG){
856 assert(Subtarget->isTargetELF() &&
857 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
858 MVT::ValueType PtrVT = getPointerTy();
859 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
860 ARMConstantPoolValue *CPV = new ARMConstantPoolValue("_GLOBAL_OFFSET_TABLE_",
861 ARMPCLabelIndex,
862 ARMCP::CPValue, PCAdj);
863 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
864 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
865 SDOperand Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
866 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
867 return DAG.getNode(ARMISD::PIC_ADD, PtrVT, Result, PICLabel);
868}
869
Evan Chenga8e29892007-01-19 07:51:42 +0000870static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
871 unsigned VarArgsFrameIndex) {
872 // vastart just stores the address of the VarArgsFrameIndex slot into the
873 // memory location argument.
874 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
875 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
876 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
877 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), SV->getValue(),
878 SV->getOffset());
879}
880
881static SDOperand LowerFORMAL_ARGUMENT(SDOperand Op, SelectionDAG &DAG,
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000882 unsigned *vRegs, unsigned ArgNo,
Evan Chenga8e29892007-01-19 07:51:42 +0000883 unsigned &NumGPRs, unsigned &ArgOffset) {
884 MachineFunction &MF = DAG.getMachineFunction();
885 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
886 SDOperand Root = Op.getOperand(0);
887 std::vector<SDOperand> ArgValues;
888 SSARegMap *RegMap = MF.getSSARegMap();
889
890 static const unsigned GPRArgRegs[] = {
891 ARM::R0, ARM::R1, ARM::R2, ARM::R3
892 };
893
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000894 unsigned ObjSize;
895 unsigned ObjGPRs;
896 unsigned GPRPad;
897 unsigned StackPad;
898 unsigned Flags = Op.getConstantOperandVal(ArgNo + 3);
899 HowToPassArgument(ObjectVT, NumGPRs, ArgOffset, ObjGPRs,
900 ObjSize, GPRPad, StackPad, Flags);
901 NumGPRs += GPRPad;
902 ArgOffset += StackPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000903
904 SDOperand ArgValue;
905 if (ObjGPRs == 1) {
906 unsigned VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
907 MF.addLiveIn(GPRArgRegs[NumGPRs], VReg);
908 vRegs[NumGPRs] = VReg;
909 ArgValue = DAG.getCopyFromReg(Root, VReg, MVT::i32);
910 if (ObjectVT == MVT::f32)
911 ArgValue = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, ArgValue);
912 } else if (ObjGPRs == 2) {
913 unsigned VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
914 MF.addLiveIn(GPRArgRegs[NumGPRs], VReg);
915 vRegs[NumGPRs] = VReg;
916 ArgValue = DAG.getCopyFromReg(Root, VReg, MVT::i32);
917
918 VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
919 MF.addLiveIn(GPRArgRegs[NumGPRs+1], VReg);
920 vRegs[NumGPRs+1] = VReg;
921 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, VReg, MVT::i32);
922
923 if (ObjectVT == MVT::i64)
924 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
925 else
926 ArgValue = DAG.getNode(ARMISD::FMDRR, MVT::f64, ArgValue, ArgValue2);
927 }
928 NumGPRs += ObjGPRs;
929
930 if (ObjSize) {
931 // If the argument is actually used, emit a load from the right stack
932 // slot.
933 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
934 MachineFrameInfo *MFI = MF.getFrameInfo();
935 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
936 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
937 if (ObjGPRs == 0)
938 ArgValue = DAG.getLoad(ObjectVT, Root, FIN, NULL, 0);
939 else {
940 SDOperand ArgValue2 =
941 DAG.getLoad(MVT::i32, Root, FIN, NULL, 0);
942 if (ObjectVT == MVT::i64)
943 ArgValue= DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
944 else
945 ArgValue= DAG.getNode(ARMISD::FMDRR, MVT::f64, ArgValue, ArgValue2);
946 }
947 } else {
948 // Don't emit a dead load.
949 ArgValue = DAG.getNode(ISD::UNDEF, ObjectVT);
950 }
951
952 ArgOffset += ObjSize; // Move on to the next argument.
953 }
954
955 return ArgValue;
956}
957
958SDOperand
959ARMTargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
960 std::vector<SDOperand> ArgValues;
961 SDOperand Root = Op.getOperand(0);
962 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
963 unsigned NumGPRs = 0; // GPRs used for parameter passing.
964 unsigned VRegs[4];
965
966 unsigned NumArgs = Op.Val->getNumValues()-1;
967 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo)
968 ArgValues.push_back(LowerFORMAL_ARGUMENT(Op, DAG, VRegs, ArgNo,
969 NumGPRs, ArgOffset));
970
971 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
972 if (isVarArg) {
973 static const unsigned GPRArgRegs[] = {
974 ARM::R0, ARM::R1, ARM::R2, ARM::R3
975 };
976
977 MachineFunction &MF = DAG.getMachineFunction();
978 SSARegMap *RegMap = MF.getSSARegMap();
979 MachineFrameInfo *MFI = MF.getFrameInfo();
980 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +0000981 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
982 unsigned VARegSize = (4 - NumGPRs) * 4;
983 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Evan Chenga8e29892007-01-19 07:51:42 +0000984 if (VARegSaveSize) {
985 // If this function is vararg, store any remaining integer argument regs
986 // to their spots on the stack so that they may be loaded by deferencing
987 // the result of va_next.
988 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +0000989 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
990 VARegSaveSize - VARegSize);
Evan Chenga8e29892007-01-19 07:51:42 +0000991 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
992
993 SmallVector<SDOperand, 4> MemOps;
994 for (; NumGPRs < 4; ++NumGPRs) {
995 unsigned VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
996 MF.addLiveIn(GPRArgRegs[NumGPRs], VReg);
997 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
998 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
999 MemOps.push_back(Store);
1000 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1001 DAG.getConstant(4, getPointerTy()));
1002 }
1003 if (!MemOps.empty())
1004 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1005 &MemOps[0], MemOps.size());
1006 } else
1007 // This will point to the next argument passed via stack.
1008 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1009 }
1010
1011 ArgValues.push_back(Root);
1012
1013 // Return the new list of results.
1014 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
1015 Op.Val->value_end());
1016 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
1017}
1018
1019/// isFloatingPointZero - Return true if this is +0.0.
1020static bool isFloatingPointZero(SDOperand Op) {
1021 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
1022 return CFP->isExactlyValue(0.0);
1023 else if (ISD::isEXTLoad(Op.Val) || ISD::isNON_EXTLoad(Op.Val)) {
1024 // Maybe this has already been legalized into the constant pool?
1025 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
1026 SDOperand WrapperOp = Op.getOperand(1).getOperand(0);
1027 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1028 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
1029 return CFP->isExactlyValue(0.0);
1030 }
1031 }
1032 return false;
1033}
1034
Evan Cheng9a2ef952007-02-02 01:53:26 +00001035static bool isLegalCmpImmediate(unsigned C, bool isThumb) {
Evan Chenga8e29892007-01-19 07:51:42 +00001036 return ( isThumb && (C & ~255U) == 0) ||
1037 (!isThumb && ARM_AM::getSOImmVal(C) != -1);
1038}
1039
1040/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1041/// the given operands.
1042static SDOperand getARMCmp(SDOperand LHS, SDOperand RHS, ISD::CondCode CC,
1043 SDOperand &ARMCC, SelectionDAG &DAG, bool isThumb) {
1044 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.Val)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001045 unsigned C = RHSC->getValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001046 if (!isLegalCmpImmediate(C, isThumb)) {
1047 // Constant does not fit, try adjusting it by one?
1048 switch (CC) {
1049 default: break;
1050 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001051 case ISD::SETGE:
Evan Chenga8e29892007-01-19 07:51:42 +00001052 if (isLegalCmpImmediate(C-1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001053 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
1054 RHS = DAG.getConstant(C-1, MVT::i32);
1055 }
1056 break;
1057 case ISD::SETULT:
1058 case ISD::SETUGE:
1059 if (C > 0 && isLegalCmpImmediate(C-1, isThumb)) {
1060 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Evan Chenga8e29892007-01-19 07:51:42 +00001061 RHS = DAG.getConstant(C-1, MVT::i32);
1062 }
1063 break;
1064 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001065 case ISD::SETGT:
Evan Chenga8e29892007-01-19 07:51:42 +00001066 if (isLegalCmpImmediate(C+1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001067 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
1068 RHS = DAG.getConstant(C+1, MVT::i32);
1069 }
1070 break;
1071 case ISD::SETULE:
1072 case ISD::SETUGT:
1073 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb)) {
1074 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Evan Chenga8e29892007-01-19 07:51:42 +00001075 RHS = DAG.getConstant(C+1, MVT::i32);
1076 }
1077 break;
1078 }
1079 }
1080 }
1081
1082 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001083 ARMISD::NodeType CompareType;
1084 switch (CondCode) {
1085 default:
1086 CompareType = ARMISD::CMP;
1087 break;
1088 case ARMCC::EQ:
1089 case ARMCC::NE:
1090 case ARMCC::MI:
1091 case ARMCC::PL:
1092 // Uses only N and Z Flags
1093 CompareType = ARMISD::CMPNZ;
1094 break;
1095 }
Evan Chenga8e29892007-01-19 07:51:42 +00001096 ARMCC = DAG.getConstant(CondCode, MVT::i32);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001097 return DAG.getNode(CompareType, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001098}
1099
1100/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
1101static SDOperand getVFPCmp(SDOperand LHS, SDOperand RHS, SelectionDAG &DAG) {
1102 SDOperand Cmp;
1103 if (!isFloatingPointZero(RHS))
1104 Cmp = DAG.getNode(ARMISD::CMPFP, MVT::Flag, LHS, RHS);
1105 else
1106 Cmp = DAG.getNode(ARMISD::CMPFPw0, MVT::Flag, LHS);
1107 return DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp);
1108}
1109
1110static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG,
1111 const ARMSubtarget *ST) {
1112 MVT::ValueType VT = Op.getValueType();
1113 SDOperand LHS = Op.getOperand(0);
1114 SDOperand RHS = Op.getOperand(1);
1115 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1116 SDOperand TrueVal = Op.getOperand(2);
1117 SDOperand FalseVal = Op.getOperand(3);
1118
1119 if (LHS.getValueType() == MVT::i32) {
1120 SDOperand ARMCC;
1121 SDOperand Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb());
1122 return DAG.getNode(ARMISD::CMOV, VT, FalseVal, TrueVal, ARMCC, Cmp);
1123 }
1124
1125 ARMCC::CondCodes CondCode, CondCode2;
1126 if (FPCCToARMCC(CC, CondCode, CondCode2))
1127 std::swap(TrueVal, FalseVal);
1128
1129 SDOperand ARMCC = DAG.getConstant(CondCode, MVT::i32);
1130 SDOperand Cmp = getVFPCmp(LHS, RHS, DAG);
1131 SDOperand Result = DAG.getNode(ARMISD::CMOV, VT, FalseVal, TrueVal,
1132 ARMCC, Cmp);
1133 if (CondCode2 != ARMCC::AL) {
1134 SDOperand ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
1135 // FIXME: Needs another CMP because flag can have but one use.
1136 SDOperand Cmp2 = getVFPCmp(LHS, RHS, DAG);
1137 Result = DAG.getNode(ARMISD::CMOV, VT, Result, TrueVal, ARMCC2, Cmp2);
1138 }
1139 return Result;
1140}
1141
1142static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG,
1143 const ARMSubtarget *ST) {
1144 SDOperand Chain = Op.getOperand(0);
1145 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1146 SDOperand LHS = Op.getOperand(2);
1147 SDOperand RHS = Op.getOperand(3);
1148 SDOperand Dest = Op.getOperand(4);
1149
1150 if (LHS.getValueType() == MVT::i32) {
1151 SDOperand ARMCC;
1152 SDOperand Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb());
1153 return DAG.getNode(ARMISD::BRCOND, MVT::Other, Chain, Dest, ARMCC, Cmp);
1154 }
1155
1156 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
1157 ARMCC::CondCodes CondCode, CondCode2;
1158 if (FPCCToARMCC(CC, CondCode, CondCode2))
1159 // Swap the LHS/RHS of the comparison if needed.
1160 std::swap(LHS, RHS);
1161
1162 SDOperand Cmp = getVFPCmp(LHS, RHS, DAG);
1163 SDOperand ARMCC = DAG.getConstant(CondCode, MVT::i32);
1164 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
1165 SDOperand Ops[] = { Chain, Dest, ARMCC, Cmp };
1166 SDOperand Res = DAG.getNode(ARMISD::BRCOND, VTList, Ops, 4);
1167 if (CondCode2 != ARMCC::AL) {
1168 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
1169 SDOperand Ops[] = { Res, Dest, ARMCC, Res.getValue(1) };
1170 Res = DAG.getNode(ARMISD::BRCOND, VTList, Ops, 4);
1171 }
1172 return Res;
1173}
1174
1175SDOperand ARMTargetLowering::LowerBR_JT(SDOperand Op, SelectionDAG &DAG) {
1176 SDOperand Chain = Op.getOperand(0);
1177 SDOperand Table = Op.getOperand(1);
1178 SDOperand Index = Op.getOperand(2);
1179
1180 MVT::ValueType PTy = getPointerTy();
1181 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1182 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
1183 SDOperand UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
1184 SDOperand JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
1185 Table = DAG.getNode(ARMISD::WrapperJT, MVT::i32, JTI, UId);
1186 Index = DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(4, PTy));
1187 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
1188 bool isPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
1189 Addr = DAG.getLoad(isPIC ? MVT::i32 : PTy, Chain, Addr, NULL, 0);
1190 Chain = Addr.getValue(1);
1191 if (isPIC)
1192 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Table);
1193 return DAG.getNode(ARMISD::BR_JT, MVT::Other, Chain, Addr, JTI, UId);
1194}
1195
1196static SDOperand LowerFP_TO_INT(SDOperand Op, SelectionDAG &DAG) {
1197 unsigned Opc =
1198 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
1199 Op = DAG.getNode(Opc, MVT::f32, Op.getOperand(0));
1200 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
1201}
1202
1203static SDOperand LowerINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
1204 MVT::ValueType VT = Op.getValueType();
1205 unsigned Opc =
1206 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1207
1208 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Op.getOperand(0));
1209 return DAG.getNode(Opc, VT, Op);
1210}
1211
1212static SDOperand LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
1213 // Implement fcopysign with a fabs and a conditional fneg.
1214 SDOperand Tmp0 = Op.getOperand(0);
1215 SDOperand Tmp1 = Op.getOperand(1);
1216 MVT::ValueType VT = Op.getValueType();
1217 MVT::ValueType SrcVT = Tmp1.getValueType();
1218 SDOperand AbsVal = DAG.getNode(ISD::FABS, VT, Tmp0);
1219 SDOperand Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG);
1220 SDOperand ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1221 return DAG.getNode(ARMISD::CNEG, VT, AbsVal, AbsVal, ARMCC, Cmp);
1222}
1223
1224static SDOperand LowerBIT_CONVERT(SDOperand Op, SelectionDAG &DAG) {
1225 // Turn f64->i64 into FMRRD.
1226 assert(Op.getValueType() == MVT::i64 &&
1227 Op.getOperand(0).getValueType() == MVT::f64);
1228
1229 Op = Op.getOperand(0);
1230 SDOperand Cvt = DAG.getNode(ARMISD::FMRRD, DAG.getVTList(MVT::i32, MVT::i32),
1231 &Op, 1);
1232
1233 // Merge the pieces into a single i64 value.
1234 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Cvt, Cvt.getValue(1));
1235}
1236
1237static SDOperand LowerMUL(SDOperand Op, SelectionDAG &DAG) {
1238 // FIXME: All this code is target-independent. Create a new target-indep
1239 // MULHILO node and move this code to the legalizer.
1240 //
1241 assert(Op.getValueType() == MVT::i64 && "Only handles i64 expand right now!");
1242
1243 SDOperand LL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
1244 DAG.getConstant(0, MVT::i32));
1245 SDOperand RL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(1),
1246 DAG.getConstant(0, MVT::i32));
1247
1248 const TargetLowering &TL = DAG.getTargetLoweringInfo();
1249 unsigned LHSSB = TL.ComputeNumSignBits(Op.getOperand(0));
1250 unsigned RHSSB = TL.ComputeNumSignBits(Op.getOperand(1));
1251
1252 SDOperand Lo, Hi;
1253 // Figure out how to lower this multiply.
1254 if (LHSSB >= 33 && RHSSB >= 33) {
1255 // If the input values are both sign extended, we can emit a mulhs+mul.
1256 Lo = DAG.getNode(ISD::MUL, MVT::i32, LL, RL);
1257 Hi = DAG.getNode(ISD::MULHS, MVT::i32, LL, RL);
1258 } else if (LHSSB == 32 && RHSSB == 32 &&
1259 TL.MaskedValueIsZero(Op.getOperand(0), 0xFFFFFFFF00000000ULL) &&
1260 TL.MaskedValueIsZero(Op.getOperand(1), 0xFFFFFFFF00000000ULL)) {
1261 // If the inputs are zero extended, use mulhu.
1262 Lo = DAG.getNode(ISD::MUL, MVT::i32, LL, RL);
1263 Hi = DAG.getNode(ISD::MULHU, MVT::i32, LL, RL);
1264 } else {
1265 SDOperand LH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
1266 DAG.getConstant(1, MVT::i32));
1267 SDOperand RH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(1),
1268 DAG.getConstant(1, MVT::i32));
1269
1270 // Lo,Hi = umul LHS, RHS.
1271 SDOperand Ops[] = { LL, RL };
1272 SDOperand UMul64 = DAG.getNode(ARMISD::MULHILOU,
1273 DAG.getVTList(MVT::i32, MVT::i32), Ops, 2);
1274 Lo = UMul64;
1275 Hi = UMul64.getValue(1);
1276 RH = DAG.getNode(ISD::MUL, MVT::i32, LL, RH);
1277 LH = DAG.getNode(ISD::MUL, MVT::i32, LH, RL);
1278 Hi = DAG.getNode(ISD::ADD, MVT::i32, Hi, RH);
1279 Hi = DAG.getNode(ISD::ADD, MVT::i32, Hi, LH);
1280 }
1281
1282 // Merge the pieces into a single i64 value.
1283 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1284}
1285
1286static SDOperand LowerMULHU(SDOperand Op, SelectionDAG &DAG) {
1287 SDOperand Ops[] = { Op.getOperand(0), Op.getOperand(1) };
1288 return DAG.getNode(ARMISD::MULHILOU,
1289 DAG.getVTList(MVT::i32, MVT::i32), Ops, 2).getValue(1);
1290}
1291
1292static SDOperand LowerMULHS(SDOperand Op, SelectionDAG &DAG) {
1293 SDOperand Ops[] = { Op.getOperand(0), Op.getOperand(1) };
1294 return DAG.getNode(ARMISD::MULHILOS,
1295 DAG.getVTList(MVT::i32, MVT::i32), Ops, 2).getValue(1);
1296}
1297
1298static SDOperand LowerSRx(SDOperand Op, SelectionDAG &DAG,
1299 const ARMSubtarget *ST) {
1300 assert(Op.getValueType() == MVT::i64 &&
1301 (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SRA) &&
1302 "Unknown shift to lower!");
1303
1304 // We only lower SRA, SRL of 1 here, all others use generic lowering.
1305 if (!isa<ConstantSDNode>(Op.getOperand(1)) ||
1306 cast<ConstantSDNode>(Op.getOperand(1))->getValue() != 1)
1307 return SDOperand();
1308
1309 // If we are in thumb mode, we don't have RRX.
1310 if (ST->isThumb()) return SDOperand();
1311
1312 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
1313 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
1314 DAG.getConstant(0, MVT::i32));
1315 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
1316 DAG.getConstant(1, MVT::i32));
1317
1318 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
1319 // captures the result into a carry flag.
1320 unsigned Opc = Op.getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
1321 Hi = DAG.getNode(Opc, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
1322
1323 // The low part is an ARMISD::RRX operand, which shifts the carry in.
1324 Lo = DAG.getNode(ARMISD::RRX, MVT::i32, Lo, Hi.getValue(1));
1325
1326 // Merge the pieces into a single i64 value.
1327 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1328}
1329
1330SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1331 switch (Op.getOpcode()) {
1332 default: assert(0 && "Don't know how to custom lower this!"); abort();
1333 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001334 case ISD::GlobalAddress:
1335 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
1336 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001337 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00001338 case ISD::CALL: return LowerCALL(Op, DAG);
1339 case ISD::RET: return LowerRET(Op, DAG);
1340 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
1341 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
1342 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
1343 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
1344 case ISD::SINT_TO_FP:
1345 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
1346 case ISD::FP_TO_SINT:
1347 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
1348 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
1349 case ISD::BIT_CONVERT: return LowerBIT_CONVERT(Op, DAG);
1350 case ISD::MUL: return LowerMUL(Op, DAG);
1351 case ISD::MULHU: return LowerMULHU(Op, DAG);
1352 case ISD::MULHS: return LowerMULHS(Op, DAG);
1353 case ISD::SRL:
1354 case ISD::SRA: return LowerSRx(Op, DAG, Subtarget);
1355 case ISD::FORMAL_ARGUMENTS:
1356 return LowerFORMAL_ARGUMENTS(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00001357 case ISD::RETURNADDR: break;
1358 case ISD::FRAMEADDR: break;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001359 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00001360 }
Nate Begemanbcc5f362007-01-29 22:58:52 +00001361 return SDOperand();
Evan Chenga8e29892007-01-19 07:51:42 +00001362}
1363
1364//===----------------------------------------------------------------------===//
1365// ARM Scheduler Hooks
1366//===----------------------------------------------------------------------===//
1367
1368MachineBasicBlock *
1369ARMTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1370 MachineBasicBlock *BB) {
1371 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1372 switch (MI->getOpcode()) {
1373 default: assert(false && "Unexpected instr type to insert");
1374 case ARM::tMOVCCr: {
1375 // To "insert" a SELECT_CC instruction, we actually have to insert the
1376 // diamond control-flow pattern. The incoming instruction knows the
1377 // destination vreg to set, the condition code register to branch on, the
1378 // true/false values to select between, and a branch opcode to use.
1379 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1380 ilist<MachineBasicBlock>::iterator It = BB;
1381 ++It;
1382
1383 // thisMBB:
1384 // ...
1385 // TrueVal = ...
1386 // cmpTY ccX, r1, r2
1387 // bCC copy1MBB
1388 // fallthrough --> copy0MBB
1389 MachineBasicBlock *thisMBB = BB;
1390 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1391 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1392 BuildMI(BB, TII->get(ARM::tBcc)).addMBB(sinkMBB)
1393 .addImm(MI->getOperand(3).getImm());
1394 MachineFunction *F = BB->getParent();
1395 F->getBasicBlockList().insert(It, copy0MBB);
1396 F->getBasicBlockList().insert(It, sinkMBB);
1397 // Update machine-CFG edges by first adding all successors of the current
1398 // block to the new block which will contain the Phi node for the select.
1399 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1400 e = BB->succ_end(); i != e; ++i)
1401 sinkMBB->addSuccessor(*i);
1402 // Next, remove all successors of the current block, and add the true
1403 // and fallthrough blocks as its successors.
1404 while(!BB->succ_empty())
1405 BB->removeSuccessor(BB->succ_begin());
1406 BB->addSuccessor(copy0MBB);
1407 BB->addSuccessor(sinkMBB);
1408
1409 // copy0MBB:
1410 // %FalseValue = ...
1411 // # fallthrough to sinkMBB
1412 BB = copy0MBB;
1413
1414 // Update machine-CFG edges
1415 BB->addSuccessor(sinkMBB);
1416
1417 // sinkMBB:
1418 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1419 // ...
1420 BB = sinkMBB;
1421 BuildMI(BB, TII->get(ARM::PHI), MI->getOperand(0).getReg())
1422 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1423 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1424
1425 delete MI; // The pseudo instruction is gone now.
1426 return BB;
1427 }
1428 }
1429}
1430
1431//===----------------------------------------------------------------------===//
1432// ARM Optimization Hooks
1433//===----------------------------------------------------------------------===//
1434
Evan Chengb01fad62007-03-12 23:30:29 +00001435/// isLegalAddressImmediate - Return true if the integer value can be used
1436/// as the offset of the target addressing mode for load / store of the
1437/// given type.
Chris Lattner37caf8c2007-04-09 23:33:39 +00001438static bool isLegalAddressImmediate(int64_t V, MVT::ValueType VT,
1439 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00001440 if (V == 0)
1441 return true;
1442
Evan Chengb01fad62007-03-12 23:30:29 +00001443 if (Subtarget->isThumb()) {
1444 if (V < 0)
1445 return false;
1446
1447 unsigned Scale = 1;
1448 switch (VT) {
1449 default: return false;
1450 case MVT::i1:
1451 case MVT::i8:
1452 // Scale == 1;
1453 break;
1454 case MVT::i16:
1455 // Scale == 2;
1456 Scale = 2;
1457 break;
1458 case MVT::i32:
1459 // Scale == 4;
1460 Scale = 4;
1461 break;
1462 }
1463
1464 if ((V & (Scale - 1)) != 0)
1465 return false;
1466 V /= Scale;
1467 return V == V & ((1LL << 5) - 1);
1468 }
1469
1470 if (V < 0)
1471 V = - V;
1472 switch (VT) {
1473 default: return false;
1474 case MVT::i1:
1475 case MVT::i8:
1476 case MVT::i32:
1477 // +- imm12
1478 return V == V & ((1LL << 12) - 1);
1479 case MVT::i16:
1480 // +- imm8
1481 return V == V & ((1LL << 8) - 1);
1482 case MVT::f32:
1483 case MVT::f64:
1484 if (!Subtarget->hasVFP2())
1485 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00001486 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00001487 return false;
1488 V >>= 2;
1489 return V == V & ((1LL << 8) - 1);
1490 }
Evan Chenga8e29892007-01-19 07:51:42 +00001491}
1492
Chris Lattner37caf8c2007-04-09 23:33:39 +00001493/// isLegalAddressingMode - Return true if the addressing mode represented
1494/// by AM is legal for this target, for a load/store of the specified type.
1495bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
1496 const Type *Ty) const {
1497 if (!isLegalAddressImmediate(AM.BaseOffs, getValueType(Ty), Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00001498 return false;
Chris Lattner37caf8c2007-04-09 23:33:39 +00001499
1500 // Can never fold addr of global into load/store.
1501 if (AM.BaseGV)
1502 return false;
1503
1504 switch (AM.Scale) {
1505 case 0: // no scale reg, must be "r+i" or "r", or "i".
1506 break;
1507 case 1:
1508 if (Subtarget->isThumb())
1509 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00001510 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00001511 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00001512 // ARM doesn't support any R+R*scale+imm addr modes.
1513 if (AM.BaseOffs)
1514 return false;
1515
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001516 int Scale = AM.Scale;
Chris Lattner37caf8c2007-04-09 23:33:39 +00001517 switch (getValueType(Ty)) {
1518 default: return false;
1519 case MVT::i1:
1520 case MVT::i8:
1521 case MVT::i32:
1522 case MVT::i64:
1523 // This assumes i64 is legalized to a pair of i32. If not (i.e.
1524 // ldrd / strd are used, then its address mode is same as i16.
1525 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001526 if (Scale < 0) Scale = -Scale;
1527 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001528 return true;
1529 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00001530 return isPowerOf2_32(Scale & ~1);
Chris Lattner37caf8c2007-04-09 23:33:39 +00001531 case MVT::i16:
1532 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001533 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001534 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00001535 return false;
1536
Chris Lattner37caf8c2007-04-09 23:33:39 +00001537 case MVT::isVoid:
1538 // Note, we allow "void" uses (basically, uses that aren't loads or
1539 // stores), because arm allows folding a scale into many arithmetic
1540 // operations. This should be made more precise and revisited later.
Chris Lattnerb2c594f2007-04-03 00:13:57 +00001541
Chris Lattner37caf8c2007-04-09 23:33:39 +00001542 // Allow r << imm, but the imm has to be a multiple of two.
1543 if (AM.Scale & 1) return false;
1544 return isPowerOf2_32(AM.Scale);
1545 }
1546 break;
Evan Chengb01fad62007-03-12 23:30:29 +00001547 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00001548 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00001549}
1550
Chris Lattner37caf8c2007-04-09 23:33:39 +00001551
Evan Chenga8e29892007-01-19 07:51:42 +00001552static bool getIndexedAddressParts(SDNode *Ptr, MVT::ValueType VT,
1553 bool isSEXTLoad, SDOperand &Base,
1554 SDOperand &Offset, bool &isInc,
1555 SelectionDAG &DAG) {
1556 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
1557 return false;
1558
1559 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
1560 // AddressingMode 3
1561 Base = Ptr->getOperand(0);
1562 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
1563 int RHSC = (int)RHS->getValue();
1564 if (RHSC < 0 && RHSC > -256) {
1565 isInc = false;
1566 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1567 return true;
1568 }
1569 }
1570 isInc = (Ptr->getOpcode() == ISD::ADD);
1571 Offset = Ptr->getOperand(1);
1572 return true;
1573 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
1574 // AddressingMode 2
1575 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
1576 int RHSC = (int)RHS->getValue();
1577 if (RHSC < 0 && RHSC > -0x1000) {
1578 isInc = false;
1579 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1580 Base = Ptr->getOperand(0);
1581 return true;
1582 }
1583 }
1584
1585 if (Ptr->getOpcode() == ISD::ADD) {
1586 isInc = true;
1587 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
1588 if (ShOpcVal != ARM_AM::no_shift) {
1589 Base = Ptr->getOperand(1);
1590 Offset = Ptr->getOperand(0);
1591 } else {
1592 Base = Ptr->getOperand(0);
1593 Offset = Ptr->getOperand(1);
1594 }
1595 return true;
1596 }
1597
1598 isInc = (Ptr->getOpcode() == ISD::ADD);
1599 Base = Ptr->getOperand(0);
1600 Offset = Ptr->getOperand(1);
1601 return true;
1602 }
1603
1604 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
1605 return false;
1606}
1607
1608/// getPreIndexedAddressParts - returns true by value, base pointer and
1609/// offset pointer and addressing mode by reference if the node's address
1610/// can be legally represented as pre-indexed load / store address.
1611bool
1612ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDOperand &Base,
1613 SDOperand &Offset,
1614 ISD::MemIndexedMode &AM,
1615 SelectionDAG &DAG) {
1616 if (Subtarget->isThumb())
1617 return false;
1618
1619 MVT::ValueType VT;
1620 SDOperand Ptr;
1621 bool isSEXTLoad = false;
1622 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1623 Ptr = LD->getBasePtr();
1624 VT = LD->getLoadedVT();
1625 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1626 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
1627 Ptr = ST->getBasePtr();
1628 VT = ST->getStoredVT();
1629 } else
1630 return false;
1631
1632 bool isInc;
1633 bool isLegal = getIndexedAddressParts(Ptr.Val, VT, isSEXTLoad, Base, Offset,
1634 isInc, DAG);
1635 if (isLegal) {
1636 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
1637 return true;
1638 }
1639 return false;
1640}
1641
1642/// getPostIndexedAddressParts - returns true by value, base pointer and
1643/// offset pointer and addressing mode by reference if this node can be
1644/// combined with a load / store to form a post-indexed load / store.
1645bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
1646 SDOperand &Base,
1647 SDOperand &Offset,
1648 ISD::MemIndexedMode &AM,
1649 SelectionDAG &DAG) {
1650 if (Subtarget->isThumb())
1651 return false;
1652
1653 MVT::ValueType VT;
1654 SDOperand Ptr;
1655 bool isSEXTLoad = false;
1656 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1657 VT = LD->getLoadedVT();
1658 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1659 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
1660 VT = ST->getStoredVT();
1661 } else
1662 return false;
1663
1664 bool isInc;
1665 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
1666 isInc, DAG);
1667 if (isLegal) {
1668 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
1669 return true;
1670 }
1671 return false;
1672}
1673
1674void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
1675 uint64_t Mask,
1676 uint64_t &KnownZero,
1677 uint64_t &KnownOne,
1678 unsigned Depth) const {
1679 KnownZero = 0;
1680 KnownOne = 0;
1681 switch (Op.getOpcode()) {
1682 default: break;
1683 case ARMISD::CMOV: {
1684 // Bits are known zero/one if known on the LHS and RHS.
1685 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1686 if (KnownZero == 0 && KnownOne == 0) return;
1687
1688 uint64_t KnownZeroRHS, KnownOneRHS;
1689 ComputeMaskedBits(Op.getOperand(1), Mask,
1690 KnownZeroRHS, KnownOneRHS, Depth+1);
1691 KnownZero &= KnownZeroRHS;
1692 KnownOne &= KnownOneRHS;
1693 return;
1694 }
1695 }
1696}
1697
1698//===----------------------------------------------------------------------===//
1699// ARM Inline Assembly Support
1700//===----------------------------------------------------------------------===//
1701
1702/// getConstraintType - Given a constraint letter, return the type of
1703/// constraint it is for this target.
1704ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00001705ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
1706 if (Constraint.size() == 1) {
1707 switch (Constraint[0]) {
1708 default: break;
1709 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001710 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00001711 }
Evan Chenga8e29892007-01-19 07:51:42 +00001712 }
Chris Lattner4234f572007-03-25 02:14:49 +00001713 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00001714}
1715
1716std::pair<unsigned, const TargetRegisterClass*>
1717ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
1718 MVT::ValueType VT) const {
1719 if (Constraint.size() == 1) {
1720 // GCC RS6000 Constraint Letters
1721 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001722 case 'l':
1723 // FIXME: in thumb mode, 'l' is only low-regs.
1724 // FALL THROUGH.
1725 case 'r':
1726 return std::make_pair(0U, ARM::GPRRegisterClass);
1727 case 'w':
1728 if (VT == MVT::f32)
1729 return std::make_pair(0U, ARM::SPRRegisterClass);
Evan Cheng0a7baa22007-04-04 00:06:07 +00001730 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001731 return std::make_pair(0U, ARM::DPRRegisterClass);
1732 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001733 }
1734 }
1735 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1736}
1737
1738std::vector<unsigned> ARMTargetLowering::
1739getRegClassForInlineAsmConstraint(const std::string &Constraint,
1740 MVT::ValueType VT) const {
1741 if (Constraint.size() != 1)
1742 return std::vector<unsigned>();
1743
1744 switch (Constraint[0]) { // GCC ARM Constraint Letters
1745 default: break;
1746 case 'l':
1747 case 'r':
1748 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
1749 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
1750 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
1751 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001752 case 'w':
1753 if (VT == MVT::f32)
1754 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
1755 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
1756 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
1757 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
1758 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
1759 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
1760 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
1761 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
1762 if (VT == MVT::f64)
1763 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
1764 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
1765 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
1766 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
1767 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001768 }
1769
1770 return std::vector<unsigned>();
1771}