blob: aeaa7e3f4ea1df4e32e52a66f982fe2dce3f5a24 [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 }
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000127 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000128
129 // ARM does not have f32 extending load.
130 setLoadXAction(ISD::EXTLOAD, MVT::f32, Expand);
131
132 // ARM supports all 4 flavors of integer indexed load / store.
133 for (unsigned im = (unsigned)ISD::PRE_INC;
134 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
135 setIndexedLoadAction(im, MVT::i1, Legal);
136 setIndexedLoadAction(im, MVT::i8, Legal);
137 setIndexedLoadAction(im, MVT::i16, Legal);
138 setIndexedLoadAction(im, MVT::i32, Legal);
139 setIndexedStoreAction(im, MVT::i1, Legal);
140 setIndexedStoreAction(im, MVT::i8, Legal);
141 setIndexedStoreAction(im, MVT::i16, Legal);
142 setIndexedStoreAction(im, MVT::i32, Legal);
143 }
144
145 // i64 operation support.
146 if (Subtarget->isThumb()) {
147 setOperationAction(ISD::MUL, MVT::i64, Expand);
148 setOperationAction(ISD::MULHU, MVT::i32, Expand);
149 setOperationAction(ISD::MULHS, MVT::i32, Expand);
150 } else {
151 setOperationAction(ISD::MUL, MVT::i64, Custom);
152 setOperationAction(ISD::MULHU, MVT::i32, Custom);
153 if (!Subtarget->hasV6Ops())
154 setOperationAction(ISD::MULHS, MVT::i32, Custom);
155 }
156 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
157 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
158 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
159 setOperationAction(ISD::SRL, MVT::i64, Custom);
160 setOperationAction(ISD::SRA, MVT::i64, Custom);
161
162 // ARM does not have ROTL.
163 setOperationAction(ISD::ROTL, MVT::i32, Expand);
164 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
165 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
Evan Chengb0636152007-02-01 23:34:03 +0000166 if (!Subtarget->hasV5TOps() || Subtarget->isThumb())
Evan Chenga8e29892007-01-19 07:51:42 +0000167 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
168
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000169 // Only ARMv6 has BSWAP.
170 if (!Subtarget->hasV6Ops())
Chris Lattner1719e132007-03-20 02:25:53 +0000171 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000172
Evan Chenga8e29892007-01-19 07:51:42 +0000173 // These are expanded into libcalls.
174 setOperationAction(ISD::SDIV, MVT::i32, Expand);
175 setOperationAction(ISD::UDIV, MVT::i32, Expand);
176 setOperationAction(ISD::SREM, MVT::i32, Expand);
177 setOperationAction(ISD::UREM, MVT::i32, Expand);
178
179 // Support label based line numbers.
180 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
181 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000182
183 setOperationAction(ISD::RET, MVT::Other, Custom);
184 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
185 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000186 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000187 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000188
189 // Expand mem operations genericly.
190 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000191 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000192 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
193
194 // Use the default implementation.
195 setOperationAction(ISD::VASTART , MVT::Other, Expand);
196 setOperationAction(ISD::VAARG , MVT::Other, Expand);
197 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
198 setOperationAction(ISD::VAEND , MVT::Other, Expand);
199 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
200 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
201 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
202
203 if (!Subtarget->hasV6Ops()) {
204 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
205 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
206 }
207 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
208
Evan Chengb6ab2542007-01-31 08:40:13 +0000209 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb())
Evan Chenga8e29892007-01-19 07:51:42 +0000210 // Turn f64->i64 into FMRRD iff target supports vfp2.
211 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
212
213 setOperationAction(ISD::SETCC , MVT::i32, Expand);
214 setOperationAction(ISD::SETCC , MVT::f32, Expand);
215 setOperationAction(ISD::SETCC , MVT::f64, Expand);
216 setOperationAction(ISD::SELECT , MVT::i32, Expand);
217 setOperationAction(ISD::SELECT , MVT::f32, Expand);
218 setOperationAction(ISD::SELECT , MVT::f64, Expand);
219 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
220 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
221 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
222
223 setOperationAction(ISD::BRCOND , MVT::Other, Expand);
224 setOperationAction(ISD::BR_CC , MVT::i32, Custom);
225 setOperationAction(ISD::BR_CC , MVT::f32, Custom);
226 setOperationAction(ISD::BR_CC , MVT::f64, Custom);
227 setOperationAction(ISD::BR_JT , MVT::Other, Custom);
228
229 setOperationAction(ISD::VASTART, MVT::Other, Custom);
230 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
231 setOperationAction(ISD::VAEND, MVT::Other, Expand);
232 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
233 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
234
235 // FP Constants can't be immediates.
236 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
237 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
238
239 // We don't support sin/cos/fmod/copysign
240 setOperationAction(ISD::FSIN , MVT::f64, Expand);
241 setOperationAction(ISD::FSIN , MVT::f32, Expand);
242 setOperationAction(ISD::FCOS , MVT::f32, Expand);
243 setOperationAction(ISD::FCOS , MVT::f64, Expand);
244 setOperationAction(ISD::FREM , MVT::f64, Expand);
245 setOperationAction(ISD::FREM , MVT::f32, Expand);
246 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
247 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
248
249 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
250 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
251 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
252 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
253 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
254
255 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Chenga8e29892007-01-19 07:51:42 +0000256 setSchedulingPreference(SchedulingForRegPressure);
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000257 setIfCvtBlockSizeLimit(Subtarget->isThumb() ? 0 : 10);
Evan Cheng144fd1f2007-06-01 08:28:59 +0000258 setIfCvtDupBlockSizeLimit(Subtarget->isThumb() ? 0 : 3);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000259
260 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Evan Chenga8e29892007-01-19 07:51:42 +0000261}
262
263
264const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
265 switch (Opcode) {
266 default: return 0;
267 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000268 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
269 case ARMISD::CALL: return "ARMISD::CALL";
270 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
271 case ARMISD::tCALL: return "ARMISD::tCALL";
272 case ARMISD::BRCOND: return "ARMISD::BRCOND";
273 case ARMISD::BR_JT: return "ARMISD::BR_JT";
274 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
275 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
276 case ARMISD::CMP: return "ARMISD::CMP";
Lauro Ramos Venancio99966632007-04-02 01:30:03 +0000277 case ARMISD::CMPNZ: return "ARMISD::CMPNZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000278 case ARMISD::CMPFP: return "ARMISD::CMPFP";
279 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
280 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
281 case ARMISD::CMOV: return "ARMISD::CMOV";
282 case ARMISD::CNEG: return "ARMISD::CNEG";
283
284 case ARMISD::FTOSI: return "ARMISD::FTOSI";
285 case ARMISD::FTOUI: return "ARMISD::FTOUI";
286 case ARMISD::SITOF: return "ARMISD::SITOF";
287 case ARMISD::UITOF: return "ARMISD::UITOF";
288 case ARMISD::MULHILOU: return "ARMISD::MULHILOU";
289 case ARMISD::MULHILOS: return "ARMISD::MULHILOS";
290
291 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
292 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
293 case ARMISD::RRX: return "ARMISD::RRX";
294
295 case ARMISD::FMRRD: return "ARMISD::FMRRD";
296 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000297
298 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Evan Chenga8e29892007-01-19 07:51:42 +0000299 }
300}
301
302//===----------------------------------------------------------------------===//
303// Lowering Code
304//===----------------------------------------------------------------------===//
305
306
307/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
308static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
309 switch (CC) {
310 default: assert(0 && "Unknown condition code!");
311 case ISD::SETNE: return ARMCC::NE;
312 case ISD::SETEQ: return ARMCC::EQ;
313 case ISD::SETGT: return ARMCC::GT;
314 case ISD::SETGE: return ARMCC::GE;
315 case ISD::SETLT: return ARMCC::LT;
316 case ISD::SETLE: return ARMCC::LE;
317 case ISD::SETUGT: return ARMCC::HI;
318 case ISD::SETUGE: return ARMCC::HS;
319 case ISD::SETULT: return ARMCC::LO;
320 case ISD::SETULE: return ARMCC::LS;
321 }
322}
323
324/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. It
325/// returns true if the operands should be inverted to form the proper
326/// comparison.
327static bool FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
328 ARMCC::CondCodes &CondCode2) {
329 bool Invert = false;
330 CondCode2 = ARMCC::AL;
331 switch (CC) {
332 default: assert(0 && "Unknown FP condition!");
333 case ISD::SETEQ:
334 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
335 case ISD::SETGT:
336 case ISD::SETOGT: CondCode = ARMCC::GT; break;
337 case ISD::SETGE:
338 case ISD::SETOGE: CondCode = ARMCC::GE; break;
339 case ISD::SETOLT: CondCode = ARMCC::MI; break;
340 case ISD::SETOLE: CondCode = ARMCC::GT; Invert = true; break;
341 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
342 case ISD::SETO: CondCode = ARMCC::VC; break;
343 case ISD::SETUO: CondCode = ARMCC::VS; break;
344 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
345 case ISD::SETUGT: CondCode = ARMCC::HI; break;
346 case ISD::SETUGE: CondCode = ARMCC::PL; break;
347 case ISD::SETLT:
348 case ISD::SETULT: CondCode = ARMCC::LT; break;
349 case ISD::SETLE:
350 case ISD::SETULE: CondCode = ARMCC::LE; break;
351 case ISD::SETNE:
352 case ISD::SETUNE: CondCode = ARMCC::NE; break;
353 }
354 return Invert;
355}
356
357static void
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000358HowToPassArgument(MVT::ValueType ObjectVT, unsigned NumGPRs,
359 unsigned StackOffset, unsigned &NeededGPRs,
360 unsigned &NeededStackSize, unsigned &GPRPad,
361 unsigned &StackPad, unsigned Flags) {
362 NeededStackSize = 0;
363 NeededGPRs = 0;
364 StackPad = 0;
365 GPRPad = 0;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +0000366 unsigned align = (Flags >> ISD::ParamFlags::OrigAlignmentOffs);
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000367 GPRPad = NumGPRs % ((align + 3)/4);
368 StackPad = StackOffset % align;
369 unsigned firstGPR = NumGPRs + GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000370 switch (ObjectVT) {
371 default: assert(0 && "Unhandled argument type!");
372 case MVT::i32:
373 case MVT::f32:
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000374 if (firstGPR < 4)
375 NeededGPRs = 1;
Evan Chenga8e29892007-01-19 07:51:42 +0000376 else
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000377 NeededStackSize = 4;
Evan Chenga8e29892007-01-19 07:51:42 +0000378 break;
379 case MVT::i64:
380 case MVT::f64:
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000381 if (firstGPR < 3)
382 NeededGPRs = 2;
383 else if (firstGPR == 3) {
384 NeededGPRs = 1;
385 NeededStackSize = 4;
Evan Chenga8e29892007-01-19 07:51:42 +0000386 } else
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000387 NeededStackSize = 8;
Evan Chenga8e29892007-01-19 07:51:42 +0000388 }
389}
390
Evan Chengfc403422007-02-03 08:53:01 +0000391/// LowerCALL - Lowering a ISD::CALL node into a callseq_start <-
392/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
393/// nodes.
Evan Chenga8e29892007-01-19 07:51:42 +0000394SDOperand ARMTargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
395 MVT::ValueType RetVT= Op.Val->getValueType(0);
396 SDOperand Chain = Op.getOperand(0);
397 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
398 assert((CallConv == CallingConv::C ||
Evan Chenga8e29892007-01-19 07:51:42 +0000399 CallConv == CallingConv::Fast) && "unknown calling convention");
400 SDOperand Callee = Op.getOperand(4);
401 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
402 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
403 unsigned NumGPRs = 0; // GPRs used for parameter passing.
404
405 // Count how many bytes are to be pushed on the stack.
406 unsigned NumBytes = 0;
407
408 // Add up all the space actually used.
409 for (unsigned i = 0; i < NumOps; ++i) {
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000410 unsigned ObjSize;
411 unsigned ObjGPRs;
412 unsigned StackPad;
413 unsigned GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000414 MVT::ValueType ObjectVT = Op.getOperand(5+2*i).getValueType();
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000415 unsigned Flags = Op.getConstantOperandVal(5+2*i+1);
416 HowToPassArgument(ObjectVT, NumGPRs, NumBytes, ObjGPRs, ObjSize,
417 GPRPad, StackPad, Flags);
418 NumBytes += ObjSize + StackPad;
419 NumGPRs += ObjGPRs + GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000420 }
421
422 // Adjust the stack pointer for the new arguments...
423 // These operations are automatically eliminated by the prolog/epilog pass
424 Chain = DAG.getCALLSEQ_START(Chain,
425 DAG.getConstant(NumBytes, MVT::i32));
426
427 SDOperand StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
428
429 static const unsigned GPRArgRegs[] = {
430 ARM::R0, ARM::R1, ARM::R2, ARM::R3
431 };
432
433 NumGPRs = 0;
434 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
435 std::vector<SDOperand> MemOpChains;
436 for (unsigned i = 0; i != NumOps; ++i) {
437 SDOperand Arg = Op.getOperand(5+2*i);
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000438 unsigned Flags = Op.getConstantOperandVal(5+2*i+1);
Evan Chenga8e29892007-01-19 07:51:42 +0000439 MVT::ValueType ArgVT = Arg.getValueType();
440
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000441 unsigned ObjSize;
442 unsigned ObjGPRs;
443 unsigned GPRPad;
444 unsigned StackPad;
445 HowToPassArgument(ArgVT, NumGPRs, ArgOffset, ObjGPRs,
446 ObjSize, GPRPad, StackPad, Flags);
447 NumGPRs += GPRPad;
448 ArgOffset += StackPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000449 if (ObjGPRs > 0) {
450 switch (ArgVT) {
451 default: assert(0 && "Unexpected ValueType for argument!");
452 case MVT::i32:
453 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Arg));
454 break;
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000455 case MVT::f32:
Evan Chenga8e29892007-01-19 07:51:42 +0000456 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs],
457 DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg)));
458 break;
459 case MVT::i64: {
460 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Arg,
461 DAG.getConstant(0, getPointerTy()));
462 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Arg,
463 DAG.getConstant(1, getPointerTy()));
464 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Lo));
465 if (ObjGPRs == 2)
466 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1], Hi));
467 else {
468 SDOperand PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType());
469 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
470 MemOpChains.push_back(DAG.getStore(Chain, Hi, PtrOff, NULL, 0));
471 }
472 break;
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000473 }
Evan Chenga8e29892007-01-19 07:51:42 +0000474 case MVT::f64: {
475 SDOperand Cvt = DAG.getNode(ARMISD::FMRRD,
476 DAG.getVTList(MVT::i32, MVT::i32),
477 &Arg, 1);
478 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Cvt));
479 if (ObjGPRs == 2)
480 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1],
481 Cvt.getValue(1)));
482 else {
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, Cvt.getValue(1), PtrOff,
486 NULL, 0));
487 }
488 break;
489 }
490 }
491 } else {
492 assert(ObjSize != 0);
493 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
494 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
495 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
496 }
497
498 NumGPRs += ObjGPRs;
499 ArgOffset += ObjSize;
500 }
501
502 if (!MemOpChains.empty())
503 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
504 &MemOpChains[0], MemOpChains.size());
505
506 // Build a sequence of copy-to-reg nodes chained together with token chain
507 // and flag operands which copy the outgoing args into the appropriate regs.
508 SDOperand InFlag;
509 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
510 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
511 InFlag);
512 InFlag = Chain.getValue(1);
513 }
514
515 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
516 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
517 // node so that legalize doesn't hack it.
518 bool isDirect = false;
519 bool isARMFunc = false;
520 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
521 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +0000522 isDirect = true;
Reid Spencer5cbf9852007-01-30 20:08:39 +0000523 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
Evan Chenga8e29892007-01-19 07:51:42 +0000524 GV->hasLinkOnceLinkage());
Evan Cheng970a4192007-01-19 19:28:01 +0000525 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +0000526 getTargetMachine().getRelocationModel() != Reloc::Static;
527 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +0000528 // tBX takes a register source operand.
529 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
530 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
531 ARMCP::CPStub, 4);
532 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 2);
533 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
534 Callee = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), CPAddr, NULL, 0);
535 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
536 Callee = DAG.getNode(ARMISD::PIC_ADD, getPointerTy(), Callee, PICLabel);
537 } else
538 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000539 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000540 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +0000541 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000542 getTargetMachine().getRelocationModel() != Reloc::Static;
543 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +0000544 // tBX takes a register source operand.
545 const char *Sym = S->getSymbol();
546 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
547 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(Sym, ARMPCLabelIndex,
548 ARMCP::CPStub, 4);
549 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 2);
550 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
551 Callee = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), CPAddr, NULL, 0);
552 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
553 Callee = DAG.getNode(ARMISD::PIC_ADD, getPointerTy(), Callee, PICLabel);
554 } else
555 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000556 }
557
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000558 // FIXME: handle tail calls differently.
559 unsigned CallOpc;
560 if (Subtarget->isThumb()) {
561 if (!Subtarget->hasV5TOps() && (!isDirect || isARMFunc))
562 CallOpc = ARMISD::CALL_NOLINK;
563 else
564 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
565 } else {
566 CallOpc = (isDirect || Subtarget->hasV5TOps())
567 ? ARMISD::CALL : ARMISD::CALL_NOLINK;
568 }
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +0000569 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb()) {
570 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000571 Chain = DAG.getCopyToReg(Chain, ARM::LR,
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +0000572 DAG.getNode(ISD::UNDEF, MVT::i32), InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000573 InFlag = Chain.getValue(1);
574 }
575
Evan Chenga8e29892007-01-19 07:51:42 +0000576 std::vector<MVT::ValueType> NodeTys;
577 NodeTys.push_back(MVT::Other); // Returns a chain
578 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
579
580 std::vector<SDOperand> Ops;
581 Ops.push_back(Chain);
582 Ops.push_back(Callee);
583
584 // Add argument registers to the end of the list so that they are known live
585 // into the call.
586 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
587 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
588 RegsToPass[i].second.getValueType()));
589
Evan Chenga8e29892007-01-19 07:51:42 +0000590 if (InFlag.Val)
591 Ops.push_back(InFlag);
592 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
593 InFlag = Chain.getValue(1);
594
595 SDOperand CSOps[] = { Chain, DAG.getConstant(NumBytes, MVT::i32), InFlag };
596 Chain = DAG.getNode(ISD::CALLSEQ_END,
597 DAG.getNodeValueTypes(MVT::Other, MVT::Flag),
598 ((RetVT != MVT::Other) ? 2 : 1), CSOps, 3);
599 if (RetVT != MVT::Other)
600 InFlag = Chain.getValue(1);
601
602 std::vector<SDOperand> ResultVals;
603 NodeTys.clear();
604
605 // If the call has results, copy the values out of the ret val registers.
606 switch (RetVT) {
607 default: assert(0 && "Unexpected ret value!");
608 case MVT::Other:
609 break;
610 case MVT::i32:
611 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
612 ResultVals.push_back(Chain.getValue(0));
613 if (Op.Val->getValueType(1) == MVT::i32) {
614 // Returns a i64 value.
615 Chain = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32,
616 Chain.getValue(2)).getValue(1);
617 ResultVals.push_back(Chain.getValue(0));
618 NodeTys.push_back(MVT::i32);
619 }
620 NodeTys.push_back(MVT::i32);
621 break;
622 case MVT::f32:
623 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
624 ResultVals.push_back(DAG.getNode(ISD::BIT_CONVERT, MVT::f32,
625 Chain.getValue(0)));
626 NodeTys.push_back(MVT::f32);
627 break;
628 case MVT::f64: {
629 SDOperand Lo = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
630 SDOperand Hi = DAG.getCopyFromReg(Lo, ARM::R1, MVT::i32, Lo.getValue(2));
631 ResultVals.push_back(DAG.getNode(ARMISD::FMDRR, MVT::f64, Lo, Hi));
632 NodeTys.push_back(MVT::f64);
633 break;
634 }
635 }
636
637 NodeTys.push_back(MVT::Other);
638
639 if (ResultVals.empty())
640 return Chain;
641
642 ResultVals.push_back(Chain);
643 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
644 ResultVals.size());
645 return Res.getValue(Op.ResNo);
646}
647
648static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
649 SDOperand Copy;
650 SDOperand Chain = Op.getOperand(0);
651 switch(Op.getNumOperands()) {
652 default:
653 assert(0 && "Do not know how to return this many arguments!");
654 abort();
655 case 1: {
656 SDOperand LR = DAG.getRegister(ARM::LR, MVT::i32);
657 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
658 }
659 case 3:
660 Op = Op.getOperand(1);
661 if (Op.getValueType() == MVT::f32) {
662 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
663 } else if (Op.getValueType() == MVT::f64) {
664 // Recursively legalize f64 -> i64.
665 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Op);
666 return DAG.getNode(ISD::RET, MVT::Other, Chain, Op,
667 DAG.getConstant(0, MVT::i32));
668 }
669 Copy = DAG.getCopyToReg(Chain, ARM::R0, Op, SDOperand());
670 if (DAG.getMachineFunction().liveout_empty())
671 DAG.getMachineFunction().addLiveOut(ARM::R0);
672 break;
673 case 5:
674 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
675 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
676 // If we haven't noted the R0+R1 are live out, do so now.
677 if (DAG.getMachineFunction().liveout_empty()) {
678 DAG.getMachineFunction().addLiveOut(ARM::R0);
679 DAG.getMachineFunction().addLiveOut(ARM::R1);
680 }
681 break;
682 }
683
684 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
685 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
686}
687
688// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
689// their target countpart wrapped in the ARMISD::Wrapper node. Suppose N is
690// one of the above mentioned nodes. It has to be wrapped because otherwise
691// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
692// be used to form addressing mode. These wrapped nodes will be selected
Evan Cheng9f6636f2007-03-19 07:48:02 +0000693// into MOVi.
Evan Chenga8e29892007-01-19 07:51:42 +0000694static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
695 MVT::ValueType PtrVT = Op.getValueType();
696 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
697 SDOperand Res;
698 if (CP->isMachineConstantPoolEntry())
699 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
700 CP->getAlignment());
701 else
702 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
703 CP->getAlignment());
704 return DAG.getNode(ARMISD::Wrapper, MVT::i32, Res);
705}
706
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000707// Lower ISD::GlobalTLSAddress using the "general dynamic" model
708SDOperand
709ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
710 SelectionDAG &DAG) {
711 MVT::ValueType PtrVT = getPointerTy();
712 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
713 ARMConstantPoolValue *CPV =
714 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
715 PCAdj, "tlsgd", true);
716 SDOperand Argument = DAG.getTargetConstantPool(CPV, PtrVT, 2);
717 Argument = DAG.getNode(ARMISD::Wrapper, MVT::i32, Argument);
718 Argument = DAG.getLoad(PtrVT, DAG.getEntryNode(), Argument, NULL, 0);
719 SDOperand Chain = Argument.getValue(1);
720
721 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
722 Argument = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Argument, PICLabel);
723
724 // call __tls_get_addr.
725 ArgListTy Args;
726 ArgListEntry Entry;
727 Entry.Node = Argument;
728 Entry.Ty = (const Type *) Type::Int32Ty;
729 Args.push_back(Entry);
730 std::pair<SDOperand, SDOperand> CallResult =
731 LowerCallTo(Chain, (const Type *) Type::Int32Ty, false, false,
732 CallingConv::C, false,
733 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG);
734 return CallResult.first;
735}
736
737// Lower ISD::GlobalTLSAddress using the "initial exec" or
738// "local exec" model.
739SDOperand
740ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
741 SelectionDAG &DAG) {
742 GlobalValue *GV = GA->getGlobal();
743 SDOperand Offset;
744 SDOperand Chain = DAG.getEntryNode();
745 MVT::ValueType PtrVT = getPointerTy();
746 // Get the Thread Pointer
747 SDOperand ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, PtrVT);
748
749 if (GV->isDeclaration()){
750 // initial exec model
751 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
752 ARMConstantPoolValue *CPV =
753 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
754 PCAdj, "gottpoff", true);
755 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 2);
756 Offset = DAG.getNode(ARMISD::Wrapper, MVT::i32, Offset);
757 Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0);
758 Chain = Offset.getValue(1);
759
760 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
761 Offset = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Offset, PICLabel);
762
763 Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0);
764 } else {
765 // local exec model
766 ARMConstantPoolValue *CPV =
767 new ARMConstantPoolValue(GV, ARMCP::CPValue, "tpoff");
768 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 2);
769 Offset = DAG.getNode(ARMISD::Wrapper, MVT::i32, Offset);
770 Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0);
771 }
772
773 // The address of the thread local variable is the add of the thread
774 // pointer with the offset of the variable.
775 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
776}
777
778SDOperand
779ARMTargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
780 // TODO: implement the "local dynamic" model
781 assert(Subtarget->isTargetELF() &&
782 "TLS not implemented for non-ELF targets");
783 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
784 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
785 // otherwise use the "Local Exec" TLS Model
786 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
787 return LowerToTLSGeneralDynamicModel(GA, DAG);
788 else
789 return LowerToTLSExecModels(GA, DAG);
790}
791
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000792SDOperand ARMTargetLowering::LowerGlobalAddressELF(SDOperand Op,
793 SelectionDAG &DAG) {
794 MVT::ValueType PtrVT = getPointerTy();
795 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
796 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
797 if (RelocM == Reloc::PIC_) {
Lauro Ramos Venancio5d3d44a2007-05-14 23:20:21 +0000798 bool UseGOTOFF = GV->hasInternalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000799 ARMConstantPoolValue *CPV =
800 new ARMConstantPoolValue(GV, ARMCP::CPValue, UseGOTOFF ? "GOTOFF":"GOT");
801 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
802 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
803 SDOperand Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
804 SDOperand Chain = Result.getValue(1);
805 SDOperand GOT = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PtrVT);
806 Result = DAG.getNode(ISD::ADD, PtrVT, Result, GOT);
807 if (!UseGOTOFF)
808 Result = DAG.getLoad(PtrVT, Chain, Result, NULL, 0);
809 return Result;
810 } else {
811 SDOperand CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 2);
812 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
813 return DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
814 }
815}
816
Evan Chenga8e29892007-01-19 07:51:42 +0000817/// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol
Evan Cheng97c9bb52007-05-04 00:26:58 +0000818/// even in non-static mode.
819static bool GVIsIndirectSymbol(GlobalValue *GV, Reloc::Model RelocM) {
820 return RelocM != Reloc::Static &&
821 (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
822 (GV->isDeclaration() && !GV->hasNotBeenReadFromBytecode()));
Evan Chenga8e29892007-01-19 07:51:42 +0000823}
824
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000825SDOperand ARMTargetLowering::LowerGlobalAddressDarwin(SDOperand Op,
826 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +0000827 MVT::ValueType PtrVT = getPointerTy();
828 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
829 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng97c9bb52007-05-04 00:26:58 +0000830 bool IsIndirect = GVIsIndirectSymbol(GV, RelocM);
Evan Chenga8e29892007-01-19 07:51:42 +0000831 SDOperand CPAddr;
832 if (RelocM == Reloc::Static)
833 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 2);
834 else {
835 unsigned PCAdj = (RelocM != Reloc::PIC_)
836 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Chengc60e76d2007-01-30 20:37:08 +0000837 ARMCP::ARMCPKind Kind = IsIndirect ? ARMCP::CPNonLazyPtr
838 : ARMCP::CPValue;
Evan Chenga8e29892007-01-19 07:51:42 +0000839 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
Evan Chengc60e76d2007-01-30 20:37:08 +0000840 Kind, PCAdj);
Evan Chenga8e29892007-01-19 07:51:42 +0000841 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
842 }
843 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
844
845 SDOperand Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
846 SDOperand Chain = Result.getValue(1);
847
848 if (RelocM == Reloc::PIC_) {
849 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
850 Result = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Result, PICLabel);
851 }
852 if (IsIndirect)
853 Result = DAG.getLoad(PtrVT, Chain, Result, NULL, 0);
854
855 return Result;
856}
857
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000858SDOperand ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDOperand Op,
859 SelectionDAG &DAG){
860 assert(Subtarget->isTargetELF() &&
861 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
862 MVT::ValueType PtrVT = getPointerTy();
863 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
864 ARMConstantPoolValue *CPV = new ARMConstantPoolValue("_GLOBAL_OFFSET_TABLE_",
865 ARMPCLabelIndex,
866 ARMCP::CPValue, PCAdj);
867 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
868 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
869 SDOperand Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
870 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
871 return DAG.getNode(ARMISD::PIC_ADD, PtrVT, Result, PICLabel);
872}
873
Evan Chenga8e29892007-01-19 07:51:42 +0000874static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
875 unsigned VarArgsFrameIndex) {
876 // vastart just stores the address of the VarArgsFrameIndex slot into the
877 // memory location argument.
878 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
879 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
880 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
881 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), SV->getValue(),
882 SV->getOffset());
883}
884
885static SDOperand LowerFORMAL_ARGUMENT(SDOperand Op, SelectionDAG &DAG,
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000886 unsigned *vRegs, unsigned ArgNo,
Evan Chenga8e29892007-01-19 07:51:42 +0000887 unsigned &NumGPRs, unsigned &ArgOffset) {
888 MachineFunction &MF = DAG.getMachineFunction();
889 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
890 SDOperand Root = Op.getOperand(0);
891 std::vector<SDOperand> ArgValues;
892 SSARegMap *RegMap = MF.getSSARegMap();
893
894 static const unsigned GPRArgRegs[] = {
895 ARM::R0, ARM::R1, ARM::R2, ARM::R3
896 };
897
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000898 unsigned ObjSize;
899 unsigned ObjGPRs;
900 unsigned GPRPad;
901 unsigned StackPad;
902 unsigned Flags = Op.getConstantOperandVal(ArgNo + 3);
903 HowToPassArgument(ObjectVT, NumGPRs, ArgOffset, ObjGPRs,
904 ObjSize, GPRPad, StackPad, Flags);
905 NumGPRs += GPRPad;
906 ArgOffset += StackPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000907
908 SDOperand ArgValue;
909 if (ObjGPRs == 1) {
910 unsigned VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
911 MF.addLiveIn(GPRArgRegs[NumGPRs], VReg);
912 vRegs[NumGPRs] = VReg;
913 ArgValue = DAG.getCopyFromReg(Root, VReg, MVT::i32);
914 if (ObjectVT == MVT::f32)
915 ArgValue = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, ArgValue);
916 } else if (ObjGPRs == 2) {
917 unsigned VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
918 MF.addLiveIn(GPRArgRegs[NumGPRs], VReg);
919 vRegs[NumGPRs] = VReg;
920 ArgValue = DAG.getCopyFromReg(Root, VReg, MVT::i32);
921
922 VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
923 MF.addLiveIn(GPRArgRegs[NumGPRs+1], VReg);
924 vRegs[NumGPRs+1] = VReg;
925 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, VReg, MVT::i32);
926
927 if (ObjectVT == MVT::i64)
928 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
929 else
930 ArgValue = DAG.getNode(ARMISD::FMDRR, MVT::f64, ArgValue, ArgValue2);
931 }
932 NumGPRs += ObjGPRs;
933
934 if (ObjSize) {
935 // If the argument is actually used, emit a load from the right stack
936 // slot.
937 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
938 MachineFrameInfo *MFI = MF.getFrameInfo();
939 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
940 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
941 if (ObjGPRs == 0)
942 ArgValue = DAG.getLoad(ObjectVT, Root, FIN, NULL, 0);
943 else {
944 SDOperand ArgValue2 =
945 DAG.getLoad(MVT::i32, Root, FIN, NULL, 0);
946 if (ObjectVT == MVT::i64)
947 ArgValue= DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
948 else
949 ArgValue= DAG.getNode(ARMISD::FMDRR, MVT::f64, ArgValue, ArgValue2);
950 }
951 } else {
952 // Don't emit a dead load.
953 ArgValue = DAG.getNode(ISD::UNDEF, ObjectVT);
954 }
955
956 ArgOffset += ObjSize; // Move on to the next argument.
957 }
958
959 return ArgValue;
960}
961
962SDOperand
963ARMTargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
964 std::vector<SDOperand> ArgValues;
965 SDOperand Root = Op.getOperand(0);
966 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
967 unsigned NumGPRs = 0; // GPRs used for parameter passing.
968 unsigned VRegs[4];
969
970 unsigned NumArgs = Op.Val->getNumValues()-1;
971 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo)
972 ArgValues.push_back(LowerFORMAL_ARGUMENT(Op, DAG, VRegs, ArgNo,
973 NumGPRs, ArgOffset));
974
975 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
976 if (isVarArg) {
977 static const unsigned GPRArgRegs[] = {
978 ARM::R0, ARM::R1, ARM::R2, ARM::R3
979 };
980
981 MachineFunction &MF = DAG.getMachineFunction();
982 SSARegMap *RegMap = MF.getSSARegMap();
983 MachineFrameInfo *MFI = MF.getFrameInfo();
984 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +0000985 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
986 unsigned VARegSize = (4 - NumGPRs) * 4;
987 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Evan Chenga8e29892007-01-19 07:51:42 +0000988 if (VARegSaveSize) {
989 // If this function is vararg, store any remaining integer argument regs
990 // to their spots on the stack so that they may be loaded by deferencing
991 // the result of va_next.
992 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +0000993 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
994 VARegSaveSize - VARegSize);
Evan Chenga8e29892007-01-19 07:51:42 +0000995 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
996
997 SmallVector<SDOperand, 4> MemOps;
998 for (; NumGPRs < 4; ++NumGPRs) {
999 unsigned VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
1000 MF.addLiveIn(GPRArgRegs[NumGPRs], VReg);
1001 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
1002 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1003 MemOps.push_back(Store);
1004 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1005 DAG.getConstant(4, getPointerTy()));
1006 }
1007 if (!MemOps.empty())
1008 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1009 &MemOps[0], MemOps.size());
1010 } else
1011 // This will point to the next argument passed via stack.
1012 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1013 }
1014
1015 ArgValues.push_back(Root);
1016
1017 // Return the new list of results.
1018 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
1019 Op.Val->value_end());
1020 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
1021}
1022
1023/// isFloatingPointZero - Return true if this is +0.0.
1024static bool isFloatingPointZero(SDOperand Op) {
1025 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
1026 return CFP->isExactlyValue(0.0);
1027 else if (ISD::isEXTLoad(Op.Val) || ISD::isNON_EXTLoad(Op.Val)) {
1028 // Maybe this has already been legalized into the constant pool?
1029 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
1030 SDOperand WrapperOp = Op.getOperand(1).getOperand(0);
1031 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1032 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
1033 return CFP->isExactlyValue(0.0);
1034 }
1035 }
1036 return false;
1037}
1038
Evan Cheng9a2ef952007-02-02 01:53:26 +00001039static bool isLegalCmpImmediate(unsigned C, bool isThumb) {
Evan Chenga8e29892007-01-19 07:51:42 +00001040 return ( isThumb && (C & ~255U) == 0) ||
1041 (!isThumb && ARM_AM::getSOImmVal(C) != -1);
1042}
1043
1044/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1045/// the given operands.
1046static SDOperand getARMCmp(SDOperand LHS, SDOperand RHS, ISD::CondCode CC,
1047 SDOperand &ARMCC, SelectionDAG &DAG, bool isThumb) {
1048 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.Val)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001049 unsigned C = RHSC->getValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001050 if (!isLegalCmpImmediate(C, isThumb)) {
1051 // Constant does not fit, try adjusting it by one?
1052 switch (CC) {
1053 default: break;
1054 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001055 case ISD::SETGE:
Evan Chenga8e29892007-01-19 07:51:42 +00001056 if (isLegalCmpImmediate(C-1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001057 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
1058 RHS = DAG.getConstant(C-1, MVT::i32);
1059 }
1060 break;
1061 case ISD::SETULT:
1062 case ISD::SETUGE:
1063 if (C > 0 && isLegalCmpImmediate(C-1, isThumb)) {
1064 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Evan Chenga8e29892007-01-19 07:51:42 +00001065 RHS = DAG.getConstant(C-1, MVT::i32);
1066 }
1067 break;
1068 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001069 case ISD::SETGT:
Evan Chenga8e29892007-01-19 07:51:42 +00001070 if (isLegalCmpImmediate(C+1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001071 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
1072 RHS = DAG.getConstant(C+1, MVT::i32);
1073 }
1074 break;
1075 case ISD::SETULE:
1076 case ISD::SETUGT:
1077 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb)) {
1078 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Evan Chenga8e29892007-01-19 07:51:42 +00001079 RHS = DAG.getConstant(C+1, MVT::i32);
1080 }
1081 break;
1082 }
1083 }
1084 }
1085
1086 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001087 ARMISD::NodeType CompareType;
1088 switch (CondCode) {
1089 default:
1090 CompareType = ARMISD::CMP;
1091 break;
1092 case ARMCC::EQ:
1093 case ARMCC::NE:
1094 case ARMCC::MI:
1095 case ARMCC::PL:
1096 // Uses only N and Z Flags
1097 CompareType = ARMISD::CMPNZ;
1098 break;
1099 }
Evan Chenga8e29892007-01-19 07:51:42 +00001100 ARMCC = DAG.getConstant(CondCode, MVT::i32);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001101 return DAG.getNode(CompareType, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001102}
1103
1104/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
1105static SDOperand getVFPCmp(SDOperand LHS, SDOperand RHS, SelectionDAG &DAG) {
1106 SDOperand Cmp;
1107 if (!isFloatingPointZero(RHS))
1108 Cmp = DAG.getNode(ARMISD::CMPFP, MVT::Flag, LHS, RHS);
1109 else
1110 Cmp = DAG.getNode(ARMISD::CMPFPw0, MVT::Flag, LHS);
1111 return DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp);
1112}
1113
1114static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG,
1115 const ARMSubtarget *ST) {
1116 MVT::ValueType VT = Op.getValueType();
1117 SDOperand LHS = Op.getOperand(0);
1118 SDOperand RHS = Op.getOperand(1);
1119 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1120 SDOperand TrueVal = Op.getOperand(2);
1121 SDOperand FalseVal = Op.getOperand(3);
1122
1123 if (LHS.getValueType() == MVT::i32) {
1124 SDOperand ARMCC;
1125 SDOperand Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb());
1126 return DAG.getNode(ARMISD::CMOV, VT, FalseVal, TrueVal, ARMCC, Cmp);
1127 }
1128
1129 ARMCC::CondCodes CondCode, CondCode2;
1130 if (FPCCToARMCC(CC, CondCode, CondCode2))
1131 std::swap(TrueVal, FalseVal);
1132
1133 SDOperand ARMCC = DAG.getConstant(CondCode, MVT::i32);
1134 SDOperand Cmp = getVFPCmp(LHS, RHS, DAG);
1135 SDOperand Result = DAG.getNode(ARMISD::CMOV, VT, FalseVal, TrueVal,
1136 ARMCC, Cmp);
1137 if (CondCode2 != ARMCC::AL) {
1138 SDOperand ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
1139 // FIXME: Needs another CMP because flag can have but one use.
1140 SDOperand Cmp2 = getVFPCmp(LHS, RHS, DAG);
1141 Result = DAG.getNode(ARMISD::CMOV, VT, Result, TrueVal, ARMCC2, Cmp2);
1142 }
1143 return Result;
1144}
1145
1146static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG,
1147 const ARMSubtarget *ST) {
1148 SDOperand Chain = Op.getOperand(0);
1149 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1150 SDOperand LHS = Op.getOperand(2);
1151 SDOperand RHS = Op.getOperand(3);
1152 SDOperand Dest = Op.getOperand(4);
1153
1154 if (LHS.getValueType() == MVT::i32) {
1155 SDOperand ARMCC;
1156 SDOperand Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb());
1157 return DAG.getNode(ARMISD::BRCOND, MVT::Other, Chain, Dest, ARMCC, Cmp);
1158 }
1159
1160 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
1161 ARMCC::CondCodes CondCode, CondCode2;
1162 if (FPCCToARMCC(CC, CondCode, CondCode2))
1163 // Swap the LHS/RHS of the comparison if needed.
1164 std::swap(LHS, RHS);
1165
1166 SDOperand Cmp = getVFPCmp(LHS, RHS, DAG);
1167 SDOperand ARMCC = DAG.getConstant(CondCode, MVT::i32);
1168 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
1169 SDOperand Ops[] = { Chain, Dest, ARMCC, Cmp };
1170 SDOperand Res = DAG.getNode(ARMISD::BRCOND, VTList, Ops, 4);
1171 if (CondCode2 != ARMCC::AL) {
1172 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
1173 SDOperand Ops[] = { Res, Dest, ARMCC, Res.getValue(1) };
1174 Res = DAG.getNode(ARMISD::BRCOND, VTList, Ops, 4);
1175 }
1176 return Res;
1177}
1178
1179SDOperand ARMTargetLowering::LowerBR_JT(SDOperand Op, SelectionDAG &DAG) {
1180 SDOperand Chain = Op.getOperand(0);
1181 SDOperand Table = Op.getOperand(1);
1182 SDOperand Index = Op.getOperand(2);
1183
1184 MVT::ValueType PTy = getPointerTy();
1185 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1186 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
1187 SDOperand UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
1188 SDOperand JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
1189 Table = DAG.getNode(ARMISD::WrapperJT, MVT::i32, JTI, UId);
1190 Index = DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(4, PTy));
1191 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
1192 bool isPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
1193 Addr = DAG.getLoad(isPIC ? MVT::i32 : PTy, Chain, Addr, NULL, 0);
1194 Chain = Addr.getValue(1);
1195 if (isPIC)
1196 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Table);
1197 return DAG.getNode(ARMISD::BR_JT, MVT::Other, Chain, Addr, JTI, UId);
1198}
1199
1200static SDOperand LowerFP_TO_INT(SDOperand Op, SelectionDAG &DAG) {
1201 unsigned Opc =
1202 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
1203 Op = DAG.getNode(Opc, MVT::f32, Op.getOperand(0));
1204 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
1205}
1206
1207static SDOperand LowerINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
1208 MVT::ValueType VT = Op.getValueType();
1209 unsigned Opc =
1210 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1211
1212 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Op.getOperand(0));
1213 return DAG.getNode(Opc, VT, Op);
1214}
1215
1216static SDOperand LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
1217 // Implement fcopysign with a fabs and a conditional fneg.
1218 SDOperand Tmp0 = Op.getOperand(0);
1219 SDOperand Tmp1 = Op.getOperand(1);
1220 MVT::ValueType VT = Op.getValueType();
1221 MVT::ValueType SrcVT = Tmp1.getValueType();
1222 SDOperand AbsVal = DAG.getNode(ISD::FABS, VT, Tmp0);
1223 SDOperand Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG);
1224 SDOperand ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1225 return DAG.getNode(ARMISD::CNEG, VT, AbsVal, AbsVal, ARMCC, Cmp);
1226}
1227
1228static SDOperand LowerBIT_CONVERT(SDOperand Op, SelectionDAG &DAG) {
1229 // Turn f64->i64 into FMRRD.
1230 assert(Op.getValueType() == MVT::i64 &&
1231 Op.getOperand(0).getValueType() == MVT::f64);
1232
1233 Op = Op.getOperand(0);
1234 SDOperand Cvt = DAG.getNode(ARMISD::FMRRD, DAG.getVTList(MVT::i32, MVT::i32),
1235 &Op, 1);
1236
1237 // Merge the pieces into a single i64 value.
1238 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Cvt, Cvt.getValue(1));
1239}
1240
1241static SDOperand LowerMUL(SDOperand Op, SelectionDAG &DAG) {
1242 // FIXME: All this code is target-independent. Create a new target-indep
1243 // MULHILO node and move this code to the legalizer.
1244 //
1245 assert(Op.getValueType() == MVT::i64 && "Only handles i64 expand right now!");
1246
1247 SDOperand LL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
1248 DAG.getConstant(0, MVT::i32));
1249 SDOperand RL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(1),
1250 DAG.getConstant(0, MVT::i32));
1251
1252 const TargetLowering &TL = DAG.getTargetLoweringInfo();
1253 unsigned LHSSB = TL.ComputeNumSignBits(Op.getOperand(0));
1254 unsigned RHSSB = TL.ComputeNumSignBits(Op.getOperand(1));
1255
1256 SDOperand Lo, Hi;
1257 // Figure out how to lower this multiply.
1258 if (LHSSB >= 33 && RHSSB >= 33) {
1259 // If the input values are both sign extended, we can emit a mulhs+mul.
1260 Lo = DAG.getNode(ISD::MUL, MVT::i32, LL, RL);
1261 Hi = DAG.getNode(ISD::MULHS, MVT::i32, LL, RL);
1262 } else if (LHSSB == 32 && RHSSB == 32 &&
1263 TL.MaskedValueIsZero(Op.getOperand(0), 0xFFFFFFFF00000000ULL) &&
1264 TL.MaskedValueIsZero(Op.getOperand(1), 0xFFFFFFFF00000000ULL)) {
1265 // If the inputs are zero extended, use mulhu.
1266 Lo = DAG.getNode(ISD::MUL, MVT::i32, LL, RL);
1267 Hi = DAG.getNode(ISD::MULHU, MVT::i32, LL, RL);
1268 } else {
1269 SDOperand LH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
1270 DAG.getConstant(1, MVT::i32));
1271 SDOperand RH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(1),
1272 DAG.getConstant(1, MVT::i32));
1273
1274 // Lo,Hi = umul LHS, RHS.
1275 SDOperand Ops[] = { LL, RL };
1276 SDOperand UMul64 = DAG.getNode(ARMISD::MULHILOU,
1277 DAG.getVTList(MVT::i32, MVT::i32), Ops, 2);
1278 Lo = UMul64;
1279 Hi = UMul64.getValue(1);
1280 RH = DAG.getNode(ISD::MUL, MVT::i32, LL, RH);
1281 LH = DAG.getNode(ISD::MUL, MVT::i32, LH, RL);
1282 Hi = DAG.getNode(ISD::ADD, MVT::i32, Hi, RH);
1283 Hi = DAG.getNode(ISD::ADD, MVT::i32, Hi, LH);
1284 }
1285
1286 // Merge the pieces into a single i64 value.
1287 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1288}
1289
1290static SDOperand LowerMULHU(SDOperand Op, SelectionDAG &DAG) {
1291 SDOperand Ops[] = { Op.getOperand(0), Op.getOperand(1) };
1292 return DAG.getNode(ARMISD::MULHILOU,
1293 DAG.getVTList(MVT::i32, MVT::i32), Ops, 2).getValue(1);
1294}
1295
1296static SDOperand LowerMULHS(SDOperand Op, SelectionDAG &DAG) {
1297 SDOperand Ops[] = { Op.getOperand(0), Op.getOperand(1) };
1298 return DAG.getNode(ARMISD::MULHILOS,
1299 DAG.getVTList(MVT::i32, MVT::i32), Ops, 2).getValue(1);
1300}
1301
1302static SDOperand LowerSRx(SDOperand Op, SelectionDAG &DAG,
1303 const ARMSubtarget *ST) {
1304 assert(Op.getValueType() == MVT::i64 &&
1305 (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SRA) &&
1306 "Unknown shift to lower!");
1307
1308 // We only lower SRA, SRL of 1 here, all others use generic lowering.
1309 if (!isa<ConstantSDNode>(Op.getOperand(1)) ||
1310 cast<ConstantSDNode>(Op.getOperand(1))->getValue() != 1)
1311 return SDOperand();
1312
1313 // If we are in thumb mode, we don't have RRX.
1314 if (ST->isThumb()) return SDOperand();
1315
1316 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
1317 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
1318 DAG.getConstant(0, MVT::i32));
1319 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
1320 DAG.getConstant(1, MVT::i32));
1321
1322 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
1323 // captures the result into a carry flag.
1324 unsigned Opc = Op.getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
1325 Hi = DAG.getNode(Opc, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
1326
1327 // The low part is an ARMISD::RRX operand, which shifts the carry in.
1328 Lo = DAG.getNode(ARMISD::RRX, MVT::i32, Lo, Hi.getValue(1));
1329
1330 // Merge the pieces into a single i64 value.
1331 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1332}
1333
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001334SDOperand ARMTargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
1335 SDOperand Chain = Op.getOperand(0);
1336 SDOperand Dest = Op.getOperand(1);
1337 SDOperand Src = Op.getOperand(2);
1338 SDOperand Count = Op.getOperand(3);
1339 unsigned Align =
1340 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1341 if (Align == 0) Align = 1;
1342
1343 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Count);
1344 // Just call memcpy if:
1345 // not 4-byte aligned
1346 // size is unknown
1347 // size is >= the threshold.
1348 if ((Align & 3) != 0 ||
1349 !I ||
1350 I->getValue() >= 64 ||
1351 (I->getValue() & 3) != 0) {
1352 MVT::ValueType IntPtr = getPointerTy();
1353 TargetLowering::ArgListTy Args;
1354 TargetLowering::ArgListEntry Entry;
1355 Entry.Ty = getTargetData()->getIntPtrType();
1356 Entry.Node = Op.getOperand(1); Args.push_back(Entry);
1357 Entry.Node = Op.getOperand(2); Args.push_back(Entry);
1358 Entry.Node = Op.getOperand(3); Args.push_back(Entry);
1359 std::pair<SDOperand,SDOperand> CallResult =
1360 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
1361 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
1362 return CallResult.second;
1363 }
1364
1365 // Otherwise do repeated 4-byte loads and stores. To be improved.
1366 assert((I->getValue() & 3) == 0);
1367 assert((Align & 3) == 0);
1368 unsigned NumMemOps = I->getValue() >> 2;
1369 unsigned EmittedNumMemOps = 0;
1370 unsigned SrcOff = 0, DstOff = 0;
1371 MVT::ValueType VT = MVT::i32;
1372 unsigned VTSize = 4;
Evan Chenge5e7ce42007-05-18 01:19:57 +00001373 const unsigned MAX_LOADS_IN_LDM = 6;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001374 SDOperand LoadChains[MAX_LOADS_IN_LDM];
1375 SDOperand Loads[MAX_LOADS_IN_LDM];
1376
1377 // Emit up to 4 loads, then a TokenFactor barrier, then the same
1378 // number of stores. The loads and stores will get combined into
1379 // ldm/stm later on.
1380 while(EmittedNumMemOps < NumMemOps) {
Evan Chenge5e7ce42007-05-18 01:19:57 +00001381 unsigned i;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001382 for (i=0; i<MAX_LOADS_IN_LDM && EmittedNumMemOps+i < NumMemOps; i++) {
1383 Loads[i] = DAG.getLoad(VT, Chain,
1384 DAG.getNode(ISD::ADD, VT, Src,
1385 DAG.getConstant(SrcOff, VT)),
1386 NULL, 0);
1387 LoadChains[i] = Loads[i].getValue(1);
1388 SrcOff += VTSize;
1389 }
1390
1391 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &LoadChains[0], i);
1392
1393 for (i=0; i<MAX_LOADS_IN_LDM && EmittedNumMemOps+i < NumMemOps; i++) {
1394 Chain = DAG.getStore(Chain, Loads[i],
1395 DAG.getNode(ISD::ADD, VT, Dest,
1396 DAG.getConstant(DstOff, VT)),
1397 NULL, 0);
1398 DstOff += VTSize;
1399 }
1400 EmittedNumMemOps += i;
1401 }
1402
1403 return Chain;
1404}
1405
Evan Chenga8e29892007-01-19 07:51:42 +00001406SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1407 switch (Op.getOpcode()) {
1408 default: assert(0 && "Don't know how to custom lower this!"); abort();
1409 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001410 case ISD::GlobalAddress:
1411 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
1412 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001413 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00001414 case ISD::CALL: return LowerCALL(Op, DAG);
1415 case ISD::RET: return LowerRET(Op, DAG);
1416 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
1417 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
1418 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
1419 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
1420 case ISD::SINT_TO_FP:
1421 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
1422 case ISD::FP_TO_SINT:
1423 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
1424 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
1425 case ISD::BIT_CONVERT: return LowerBIT_CONVERT(Op, DAG);
1426 case ISD::MUL: return LowerMUL(Op, DAG);
1427 case ISD::MULHU: return LowerMULHU(Op, DAG);
1428 case ISD::MULHS: return LowerMULHS(Op, DAG);
1429 case ISD::SRL:
1430 case ISD::SRA: return LowerSRx(Op, DAG, Subtarget);
1431 case ISD::FORMAL_ARGUMENTS:
1432 return LowerFORMAL_ARGUMENTS(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00001433 case ISD::RETURNADDR: break;
1434 case ISD::FRAMEADDR: break;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001435 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001436 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00001437 }
Nate Begemanbcc5f362007-01-29 22:58:52 +00001438 return SDOperand();
Evan Chenga8e29892007-01-19 07:51:42 +00001439}
1440
1441//===----------------------------------------------------------------------===//
1442// ARM Scheduler Hooks
1443//===----------------------------------------------------------------------===//
1444
1445MachineBasicBlock *
1446ARMTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1447 MachineBasicBlock *BB) {
1448 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1449 switch (MI->getOpcode()) {
1450 default: assert(false && "Unexpected instr type to insert");
1451 case ARM::tMOVCCr: {
1452 // To "insert" a SELECT_CC instruction, we actually have to insert the
1453 // diamond control-flow pattern. The incoming instruction knows the
1454 // destination vreg to set, the condition code register to branch on, the
1455 // true/false values to select between, and a branch opcode to use.
1456 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1457 ilist<MachineBasicBlock>::iterator It = BB;
1458 ++It;
1459
1460 // thisMBB:
1461 // ...
1462 // TrueVal = ...
1463 // cmpTY ccX, r1, r2
1464 // bCC copy1MBB
1465 // fallthrough --> copy0MBB
1466 MachineBasicBlock *thisMBB = BB;
1467 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1468 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1469 BuildMI(BB, TII->get(ARM::tBcc)).addMBB(sinkMBB)
1470 .addImm(MI->getOperand(3).getImm());
1471 MachineFunction *F = BB->getParent();
1472 F->getBasicBlockList().insert(It, copy0MBB);
1473 F->getBasicBlockList().insert(It, sinkMBB);
1474 // Update machine-CFG edges by first adding all successors of the current
1475 // block to the new block which will contain the Phi node for the select.
1476 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1477 e = BB->succ_end(); i != e; ++i)
1478 sinkMBB->addSuccessor(*i);
1479 // Next, remove all successors of the current block, and add the true
1480 // and fallthrough blocks as its successors.
1481 while(!BB->succ_empty())
1482 BB->removeSuccessor(BB->succ_begin());
1483 BB->addSuccessor(copy0MBB);
1484 BB->addSuccessor(sinkMBB);
1485
1486 // copy0MBB:
1487 // %FalseValue = ...
1488 // # fallthrough to sinkMBB
1489 BB = copy0MBB;
1490
1491 // Update machine-CFG edges
1492 BB->addSuccessor(sinkMBB);
1493
1494 // sinkMBB:
1495 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1496 // ...
1497 BB = sinkMBB;
1498 BuildMI(BB, TII->get(ARM::PHI), MI->getOperand(0).getReg())
1499 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1500 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1501
1502 delete MI; // The pseudo instruction is gone now.
1503 return BB;
1504 }
1505 }
1506}
1507
1508//===----------------------------------------------------------------------===//
1509// ARM Optimization Hooks
1510//===----------------------------------------------------------------------===//
1511
Evan Chengb01fad62007-03-12 23:30:29 +00001512/// isLegalAddressImmediate - Return true if the integer value can be used
1513/// as the offset of the target addressing mode for load / store of the
1514/// given type.
Chris Lattner37caf8c2007-04-09 23:33:39 +00001515static bool isLegalAddressImmediate(int64_t V, MVT::ValueType VT,
1516 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00001517 if (V == 0)
1518 return true;
1519
Evan Chengb01fad62007-03-12 23:30:29 +00001520 if (Subtarget->isThumb()) {
1521 if (V < 0)
1522 return false;
1523
1524 unsigned Scale = 1;
1525 switch (VT) {
1526 default: return false;
1527 case MVT::i1:
1528 case MVT::i8:
1529 // Scale == 1;
1530 break;
1531 case MVT::i16:
1532 // Scale == 2;
1533 Scale = 2;
1534 break;
1535 case MVT::i32:
1536 // Scale == 4;
1537 Scale = 4;
1538 break;
1539 }
1540
1541 if ((V & (Scale - 1)) != 0)
1542 return false;
1543 V /= Scale;
1544 return V == V & ((1LL << 5) - 1);
1545 }
1546
1547 if (V < 0)
1548 V = - V;
1549 switch (VT) {
1550 default: return false;
1551 case MVT::i1:
1552 case MVT::i8:
1553 case MVT::i32:
1554 // +- imm12
1555 return V == V & ((1LL << 12) - 1);
1556 case MVT::i16:
1557 // +- imm8
1558 return V == V & ((1LL << 8) - 1);
1559 case MVT::f32:
1560 case MVT::f64:
1561 if (!Subtarget->hasVFP2())
1562 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00001563 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00001564 return false;
1565 V >>= 2;
1566 return V == V & ((1LL << 8) - 1);
1567 }
Evan Chenga8e29892007-01-19 07:51:42 +00001568}
1569
Chris Lattner37caf8c2007-04-09 23:33:39 +00001570/// isLegalAddressingMode - Return true if the addressing mode represented
1571/// by AM is legal for this target, for a load/store of the specified type.
1572bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
1573 const Type *Ty) const {
1574 if (!isLegalAddressImmediate(AM.BaseOffs, getValueType(Ty), Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00001575 return false;
Chris Lattner37caf8c2007-04-09 23:33:39 +00001576
1577 // Can never fold addr of global into load/store.
1578 if (AM.BaseGV)
1579 return false;
1580
1581 switch (AM.Scale) {
1582 case 0: // no scale reg, must be "r+i" or "r", or "i".
1583 break;
1584 case 1:
1585 if (Subtarget->isThumb())
1586 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00001587 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00001588 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00001589 // ARM doesn't support any R+R*scale+imm addr modes.
1590 if (AM.BaseOffs)
1591 return false;
1592
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001593 int Scale = AM.Scale;
Chris Lattner37caf8c2007-04-09 23:33:39 +00001594 switch (getValueType(Ty)) {
1595 default: return false;
1596 case MVT::i1:
1597 case MVT::i8:
1598 case MVT::i32:
1599 case MVT::i64:
1600 // This assumes i64 is legalized to a pair of i32. If not (i.e.
1601 // ldrd / strd are used, then its address mode is same as i16.
1602 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001603 if (Scale < 0) Scale = -Scale;
1604 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001605 return true;
1606 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00001607 return isPowerOf2_32(Scale & ~1);
Chris Lattner37caf8c2007-04-09 23:33:39 +00001608 case MVT::i16:
1609 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001610 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001611 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00001612 return false;
1613
Chris Lattner37caf8c2007-04-09 23:33:39 +00001614 case MVT::isVoid:
1615 // Note, we allow "void" uses (basically, uses that aren't loads or
1616 // stores), because arm allows folding a scale into many arithmetic
1617 // operations. This should be made more precise and revisited later.
Chris Lattnerb2c594f2007-04-03 00:13:57 +00001618
Chris Lattner37caf8c2007-04-09 23:33:39 +00001619 // Allow r << imm, but the imm has to be a multiple of two.
1620 if (AM.Scale & 1) return false;
1621 return isPowerOf2_32(AM.Scale);
1622 }
1623 break;
Evan Chengb01fad62007-03-12 23:30:29 +00001624 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00001625 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00001626}
1627
Chris Lattner37caf8c2007-04-09 23:33:39 +00001628
Evan Chenga8e29892007-01-19 07:51:42 +00001629static bool getIndexedAddressParts(SDNode *Ptr, MVT::ValueType VT,
1630 bool isSEXTLoad, SDOperand &Base,
1631 SDOperand &Offset, bool &isInc,
1632 SelectionDAG &DAG) {
1633 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
1634 return false;
1635
1636 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
1637 // AddressingMode 3
1638 Base = Ptr->getOperand(0);
1639 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
1640 int RHSC = (int)RHS->getValue();
1641 if (RHSC < 0 && RHSC > -256) {
1642 isInc = false;
1643 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1644 return true;
1645 }
1646 }
1647 isInc = (Ptr->getOpcode() == ISD::ADD);
1648 Offset = Ptr->getOperand(1);
1649 return true;
1650 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
1651 // AddressingMode 2
1652 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
1653 int RHSC = (int)RHS->getValue();
1654 if (RHSC < 0 && RHSC > -0x1000) {
1655 isInc = false;
1656 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1657 Base = Ptr->getOperand(0);
1658 return true;
1659 }
1660 }
1661
1662 if (Ptr->getOpcode() == ISD::ADD) {
1663 isInc = true;
1664 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
1665 if (ShOpcVal != ARM_AM::no_shift) {
1666 Base = Ptr->getOperand(1);
1667 Offset = Ptr->getOperand(0);
1668 } else {
1669 Base = Ptr->getOperand(0);
1670 Offset = Ptr->getOperand(1);
1671 }
1672 return true;
1673 }
1674
1675 isInc = (Ptr->getOpcode() == ISD::ADD);
1676 Base = Ptr->getOperand(0);
1677 Offset = Ptr->getOperand(1);
1678 return true;
1679 }
1680
1681 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
1682 return false;
1683}
1684
1685/// getPreIndexedAddressParts - returns true by value, base pointer and
1686/// offset pointer and addressing mode by reference if the node's address
1687/// can be legally represented as pre-indexed load / store address.
1688bool
1689ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDOperand &Base,
1690 SDOperand &Offset,
1691 ISD::MemIndexedMode &AM,
1692 SelectionDAG &DAG) {
1693 if (Subtarget->isThumb())
1694 return false;
1695
1696 MVT::ValueType VT;
1697 SDOperand Ptr;
1698 bool isSEXTLoad = false;
1699 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1700 Ptr = LD->getBasePtr();
1701 VT = LD->getLoadedVT();
1702 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1703 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
1704 Ptr = ST->getBasePtr();
1705 VT = ST->getStoredVT();
1706 } else
1707 return false;
1708
1709 bool isInc;
1710 bool isLegal = getIndexedAddressParts(Ptr.Val, VT, isSEXTLoad, Base, Offset,
1711 isInc, DAG);
1712 if (isLegal) {
1713 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
1714 return true;
1715 }
1716 return false;
1717}
1718
1719/// getPostIndexedAddressParts - returns true by value, base pointer and
1720/// offset pointer and addressing mode by reference if this node can be
1721/// combined with a load / store to form a post-indexed load / store.
1722bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
1723 SDOperand &Base,
1724 SDOperand &Offset,
1725 ISD::MemIndexedMode &AM,
1726 SelectionDAG &DAG) {
1727 if (Subtarget->isThumb())
1728 return false;
1729
1730 MVT::ValueType VT;
1731 SDOperand Ptr;
1732 bool isSEXTLoad = false;
1733 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1734 VT = LD->getLoadedVT();
1735 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1736 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
1737 VT = ST->getStoredVT();
1738 } else
1739 return false;
1740
1741 bool isInc;
1742 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
1743 isInc, DAG);
1744 if (isLegal) {
1745 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
1746 return true;
1747 }
1748 return false;
1749}
1750
1751void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
1752 uint64_t Mask,
1753 uint64_t &KnownZero,
1754 uint64_t &KnownOne,
1755 unsigned Depth) const {
1756 KnownZero = 0;
1757 KnownOne = 0;
1758 switch (Op.getOpcode()) {
1759 default: break;
1760 case ARMISD::CMOV: {
1761 // Bits are known zero/one if known on the LHS and RHS.
1762 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1763 if (KnownZero == 0 && KnownOne == 0) return;
1764
1765 uint64_t KnownZeroRHS, KnownOneRHS;
1766 ComputeMaskedBits(Op.getOperand(1), Mask,
1767 KnownZeroRHS, KnownOneRHS, Depth+1);
1768 KnownZero &= KnownZeroRHS;
1769 KnownOne &= KnownOneRHS;
1770 return;
1771 }
1772 }
1773}
1774
1775//===----------------------------------------------------------------------===//
1776// ARM Inline Assembly Support
1777//===----------------------------------------------------------------------===//
1778
1779/// getConstraintType - Given a constraint letter, return the type of
1780/// constraint it is for this target.
1781ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00001782ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
1783 if (Constraint.size() == 1) {
1784 switch (Constraint[0]) {
1785 default: break;
1786 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001787 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00001788 }
Evan Chenga8e29892007-01-19 07:51:42 +00001789 }
Chris Lattner4234f572007-03-25 02:14:49 +00001790 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00001791}
1792
1793std::pair<unsigned, const TargetRegisterClass*>
1794ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
1795 MVT::ValueType VT) const {
1796 if (Constraint.size() == 1) {
1797 // GCC RS6000 Constraint Letters
1798 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001799 case 'l':
1800 // FIXME: in thumb mode, 'l' is only low-regs.
1801 // FALL THROUGH.
1802 case 'r':
1803 return std::make_pair(0U, ARM::GPRRegisterClass);
1804 case 'w':
1805 if (VT == MVT::f32)
1806 return std::make_pair(0U, ARM::SPRRegisterClass);
Evan Cheng0a7baa22007-04-04 00:06:07 +00001807 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001808 return std::make_pair(0U, ARM::DPRRegisterClass);
1809 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001810 }
1811 }
1812 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1813}
1814
1815std::vector<unsigned> ARMTargetLowering::
1816getRegClassForInlineAsmConstraint(const std::string &Constraint,
1817 MVT::ValueType VT) const {
1818 if (Constraint.size() != 1)
1819 return std::vector<unsigned>();
1820
1821 switch (Constraint[0]) { // GCC ARM Constraint Letters
1822 default: break;
1823 case 'l':
1824 case 'r':
1825 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
1826 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
1827 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
1828 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001829 case 'w':
1830 if (VT == MVT::f32)
1831 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
1832 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
1833 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
1834 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
1835 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
1836 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
1837 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
1838 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
1839 if (VT == MVT::f64)
1840 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
1841 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
1842 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
1843 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
1844 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001845 }
1846
1847 return std::vector<unsigned>();
1848}