blob: 6f63fbdcb31132b5e0557a37a1b89a22d646cdbb [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 Cheng97e604e2007-06-19 23:55:02 +0000258 setIfCvtDupBlockSizeLimit(Subtarget->isThumb() ? 0 : 2);
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";
Evan Cheng277f0742007-06-19 21:05:09 +0000270 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000271 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
272 case ARMISD::tCALL: return "ARMISD::tCALL";
273 case ARMISD::BRCOND: return "ARMISD::BRCOND";
274 case ARMISD::BR_JT: return "ARMISD::BR_JT";
275 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
276 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
277 case ARMISD::CMP: return "ARMISD::CMP";
Lauro Ramos Venancio99966632007-04-02 01:30:03 +0000278 case ARMISD::CMPNZ: return "ARMISD::CMPNZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000279 case ARMISD::CMPFP: return "ARMISD::CMPFP";
280 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
281 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
282 case ARMISD::CMOV: return "ARMISD::CMOV";
283 case ARMISD::CNEG: return "ARMISD::CNEG";
284
285 case ARMISD::FTOSI: return "ARMISD::FTOSI";
286 case ARMISD::FTOUI: return "ARMISD::FTOUI";
287 case ARMISD::SITOF: return "ARMISD::SITOF";
288 case ARMISD::UITOF: return "ARMISD::UITOF";
289 case ARMISD::MULHILOU: return "ARMISD::MULHILOU";
290 case ARMISD::MULHILOS: return "ARMISD::MULHILOS";
291
292 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
293 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
294 case ARMISD::RRX: return "ARMISD::RRX";
295
296 case ARMISD::FMRRD: return "ARMISD::FMRRD";
297 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000298
299 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Evan Chenga8e29892007-01-19 07:51:42 +0000300 }
301}
302
303//===----------------------------------------------------------------------===//
304// Lowering Code
305//===----------------------------------------------------------------------===//
306
307
308/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
309static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
310 switch (CC) {
311 default: assert(0 && "Unknown condition code!");
312 case ISD::SETNE: return ARMCC::NE;
313 case ISD::SETEQ: return ARMCC::EQ;
314 case ISD::SETGT: return ARMCC::GT;
315 case ISD::SETGE: return ARMCC::GE;
316 case ISD::SETLT: return ARMCC::LT;
317 case ISD::SETLE: return ARMCC::LE;
318 case ISD::SETUGT: return ARMCC::HI;
319 case ISD::SETUGE: return ARMCC::HS;
320 case ISD::SETULT: return ARMCC::LO;
321 case ISD::SETULE: return ARMCC::LS;
322 }
323}
324
325/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. It
326/// returns true if the operands should be inverted to form the proper
327/// comparison.
328static bool FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
329 ARMCC::CondCodes &CondCode2) {
330 bool Invert = false;
331 CondCode2 = ARMCC::AL;
332 switch (CC) {
333 default: assert(0 && "Unknown FP condition!");
334 case ISD::SETEQ:
335 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
336 case ISD::SETGT:
337 case ISD::SETOGT: CondCode = ARMCC::GT; break;
338 case ISD::SETGE:
339 case ISD::SETOGE: CondCode = ARMCC::GE; break;
340 case ISD::SETOLT: CondCode = ARMCC::MI; break;
341 case ISD::SETOLE: CondCode = ARMCC::GT; Invert = true; break;
342 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
343 case ISD::SETO: CondCode = ARMCC::VC; break;
344 case ISD::SETUO: CondCode = ARMCC::VS; break;
345 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
346 case ISD::SETUGT: CondCode = ARMCC::HI; break;
347 case ISD::SETUGE: CondCode = ARMCC::PL; break;
348 case ISD::SETLT:
349 case ISD::SETULT: CondCode = ARMCC::LT; break;
350 case ISD::SETLE:
351 case ISD::SETULE: CondCode = ARMCC::LE; break;
352 case ISD::SETNE:
353 case ISD::SETUNE: CondCode = ARMCC::NE; break;
354 }
355 return Invert;
356}
357
358static void
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000359HowToPassArgument(MVT::ValueType ObjectVT, unsigned NumGPRs,
360 unsigned StackOffset, unsigned &NeededGPRs,
361 unsigned &NeededStackSize, unsigned &GPRPad,
362 unsigned &StackPad, unsigned Flags) {
363 NeededStackSize = 0;
364 NeededGPRs = 0;
365 StackPad = 0;
366 GPRPad = 0;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +0000367 unsigned align = (Flags >> ISD::ParamFlags::OrigAlignmentOffs);
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000368 GPRPad = NumGPRs % ((align + 3)/4);
369 StackPad = StackOffset % align;
370 unsigned firstGPR = NumGPRs + GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000371 switch (ObjectVT) {
372 default: assert(0 && "Unhandled argument type!");
373 case MVT::i32:
374 case MVT::f32:
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000375 if (firstGPR < 4)
376 NeededGPRs = 1;
Evan Chenga8e29892007-01-19 07:51:42 +0000377 else
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000378 NeededStackSize = 4;
Evan Chenga8e29892007-01-19 07:51:42 +0000379 break;
380 case MVT::i64:
381 case MVT::f64:
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000382 if (firstGPR < 3)
383 NeededGPRs = 2;
384 else if (firstGPR == 3) {
385 NeededGPRs = 1;
386 NeededStackSize = 4;
Evan Chenga8e29892007-01-19 07:51:42 +0000387 } else
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000388 NeededStackSize = 8;
Evan Chenga8e29892007-01-19 07:51:42 +0000389 }
390}
391
Evan Chengfc403422007-02-03 08:53:01 +0000392/// LowerCALL - Lowering a ISD::CALL node into a callseq_start <-
393/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
394/// nodes.
Evan Chenga8e29892007-01-19 07:51:42 +0000395SDOperand ARMTargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
396 MVT::ValueType RetVT= Op.Val->getValueType(0);
397 SDOperand Chain = Op.getOperand(0);
398 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
399 assert((CallConv == CallingConv::C ||
Evan Chenga8e29892007-01-19 07:51:42 +0000400 CallConv == CallingConv::Fast) && "unknown calling convention");
401 SDOperand Callee = Op.getOperand(4);
402 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
403 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
404 unsigned NumGPRs = 0; // GPRs used for parameter passing.
405
406 // Count how many bytes are to be pushed on the stack.
407 unsigned NumBytes = 0;
408
409 // Add up all the space actually used.
410 for (unsigned i = 0; i < NumOps; ++i) {
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000411 unsigned ObjSize;
412 unsigned ObjGPRs;
413 unsigned StackPad;
414 unsigned GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000415 MVT::ValueType ObjectVT = Op.getOperand(5+2*i).getValueType();
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000416 unsigned Flags = Op.getConstantOperandVal(5+2*i+1);
417 HowToPassArgument(ObjectVT, NumGPRs, NumBytes, ObjGPRs, ObjSize,
418 GPRPad, StackPad, Flags);
419 NumBytes += ObjSize + StackPad;
420 NumGPRs += ObjGPRs + GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000421 }
422
423 // Adjust the stack pointer for the new arguments...
424 // These operations are automatically eliminated by the prolog/epilog pass
425 Chain = DAG.getCALLSEQ_START(Chain,
426 DAG.getConstant(NumBytes, MVT::i32));
427
428 SDOperand StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
429
430 static const unsigned GPRArgRegs[] = {
431 ARM::R0, ARM::R1, ARM::R2, ARM::R3
432 };
433
434 NumGPRs = 0;
435 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
436 std::vector<SDOperand> MemOpChains;
437 for (unsigned i = 0; i != NumOps; ++i) {
438 SDOperand Arg = Op.getOperand(5+2*i);
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000439 unsigned Flags = Op.getConstantOperandVal(5+2*i+1);
Evan Chenga8e29892007-01-19 07:51:42 +0000440 MVT::ValueType ArgVT = Arg.getValueType();
441
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000442 unsigned ObjSize;
443 unsigned ObjGPRs;
444 unsigned GPRPad;
445 unsigned StackPad;
446 HowToPassArgument(ArgVT, NumGPRs, ArgOffset, ObjGPRs,
447 ObjSize, GPRPad, StackPad, Flags);
448 NumGPRs += GPRPad;
449 ArgOffset += StackPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000450 if (ObjGPRs > 0) {
451 switch (ArgVT) {
452 default: assert(0 && "Unexpected ValueType for argument!");
453 case MVT::i32:
454 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Arg));
455 break;
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000456 case MVT::f32:
Evan Chenga8e29892007-01-19 07:51:42 +0000457 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs],
458 DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg)));
459 break;
460 case MVT::i64: {
461 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Arg,
462 DAG.getConstant(0, getPointerTy()));
463 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Arg,
464 DAG.getConstant(1, getPointerTy()));
465 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Lo));
466 if (ObjGPRs == 2)
467 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1], Hi));
468 else {
469 SDOperand PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType());
470 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
471 MemOpChains.push_back(DAG.getStore(Chain, Hi, PtrOff, NULL, 0));
472 }
473 break;
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000474 }
Evan Chenga8e29892007-01-19 07:51:42 +0000475 case MVT::f64: {
476 SDOperand Cvt = DAG.getNode(ARMISD::FMRRD,
477 DAG.getVTList(MVT::i32, MVT::i32),
478 &Arg, 1);
479 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Cvt));
480 if (ObjGPRs == 2)
481 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1],
482 Cvt.getValue(1)));
483 else {
484 SDOperand PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType());
485 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
486 MemOpChains.push_back(DAG.getStore(Chain, Cvt.getValue(1), PtrOff,
487 NULL, 0));
488 }
489 break;
490 }
491 }
492 } else {
493 assert(ObjSize != 0);
494 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
495 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
496 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
497 }
498
499 NumGPRs += ObjGPRs;
500 ArgOffset += ObjSize;
501 }
502
503 if (!MemOpChains.empty())
504 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
505 &MemOpChains[0], MemOpChains.size());
506
507 // Build a sequence of copy-to-reg nodes chained together with token chain
508 // and flag operands which copy the outgoing args into the appropriate regs.
509 SDOperand InFlag;
510 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
511 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
512 InFlag);
513 InFlag = Chain.getValue(1);
514 }
515
516 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
517 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
518 // node so that legalize doesn't hack it.
519 bool isDirect = false;
520 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +0000521 bool isLocalARMFunc = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000522 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
523 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +0000524 isDirect = true;
Reid Spencer5cbf9852007-01-30 20:08:39 +0000525 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
Evan Chenga8e29892007-01-19 07:51:42 +0000526 GV->hasLinkOnceLinkage());
Evan Cheng970a4192007-01-19 19:28:01 +0000527 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +0000528 getTargetMachine().getRelocationModel() != Reloc::Static;
529 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +0000530 // ARM call to a local ARM function is predicable.
531 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +0000532 // tBX takes a register source operand.
533 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
534 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
535 ARMCP::CPStub, 4);
536 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 2);
537 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
538 Callee = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), CPAddr, NULL, 0);
539 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
540 Callee = DAG.getNode(ARMISD::PIC_ADD, getPointerTy(), Callee, PICLabel);
541 } else
542 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000543 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000544 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +0000545 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000546 getTargetMachine().getRelocationModel() != Reloc::Static;
547 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +0000548 // tBX takes a register source operand.
549 const char *Sym = S->getSymbol();
550 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
551 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(Sym, ARMPCLabelIndex,
552 ARMCP::CPStub, 4);
553 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 2);
554 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
555 Callee = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), CPAddr, NULL, 0);
556 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
557 Callee = DAG.getNode(ARMISD::PIC_ADD, getPointerTy(), Callee, PICLabel);
558 } else
559 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000560 }
561
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000562 // FIXME: handle tail calls differently.
563 unsigned CallOpc;
564 if (Subtarget->isThumb()) {
565 if (!Subtarget->hasV5TOps() && (!isDirect || isARMFunc))
566 CallOpc = ARMISD::CALL_NOLINK;
567 else
568 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
569 } else {
570 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +0000571 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
572 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000573 }
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +0000574 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb()) {
575 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000576 Chain = DAG.getCopyToReg(Chain, ARM::LR,
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +0000577 DAG.getNode(ISD::UNDEF, MVT::i32), InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000578 InFlag = Chain.getValue(1);
579 }
580
Evan Chenga8e29892007-01-19 07:51:42 +0000581 std::vector<MVT::ValueType> NodeTys;
582 NodeTys.push_back(MVT::Other); // Returns a chain
583 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
584
585 std::vector<SDOperand> Ops;
586 Ops.push_back(Chain);
587 Ops.push_back(Callee);
588
589 // Add argument registers to the end of the list so that they are known live
590 // into the call.
591 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
592 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
593 RegsToPass[i].second.getValueType()));
594
Evan Chenga8e29892007-01-19 07:51:42 +0000595 if (InFlag.Val)
596 Ops.push_back(InFlag);
597 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
598 InFlag = Chain.getValue(1);
599
600 SDOperand CSOps[] = { Chain, DAG.getConstant(NumBytes, MVT::i32), InFlag };
601 Chain = DAG.getNode(ISD::CALLSEQ_END,
602 DAG.getNodeValueTypes(MVT::Other, MVT::Flag),
603 ((RetVT != MVT::Other) ? 2 : 1), CSOps, 3);
604 if (RetVT != MVT::Other)
605 InFlag = Chain.getValue(1);
606
607 std::vector<SDOperand> ResultVals;
608 NodeTys.clear();
609
610 // If the call has results, copy the values out of the ret val registers.
611 switch (RetVT) {
612 default: assert(0 && "Unexpected ret value!");
613 case MVT::Other:
614 break;
615 case MVT::i32:
616 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
617 ResultVals.push_back(Chain.getValue(0));
618 if (Op.Val->getValueType(1) == MVT::i32) {
619 // Returns a i64 value.
620 Chain = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32,
621 Chain.getValue(2)).getValue(1);
622 ResultVals.push_back(Chain.getValue(0));
623 NodeTys.push_back(MVT::i32);
624 }
625 NodeTys.push_back(MVT::i32);
626 break;
627 case MVT::f32:
628 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
629 ResultVals.push_back(DAG.getNode(ISD::BIT_CONVERT, MVT::f32,
630 Chain.getValue(0)));
631 NodeTys.push_back(MVT::f32);
632 break;
633 case MVT::f64: {
634 SDOperand Lo = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
635 SDOperand Hi = DAG.getCopyFromReg(Lo, ARM::R1, MVT::i32, Lo.getValue(2));
636 ResultVals.push_back(DAG.getNode(ARMISD::FMDRR, MVT::f64, Lo, Hi));
637 NodeTys.push_back(MVT::f64);
638 break;
639 }
640 }
641
642 NodeTys.push_back(MVT::Other);
643
644 if (ResultVals.empty())
645 return Chain;
646
647 ResultVals.push_back(Chain);
648 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
649 ResultVals.size());
650 return Res.getValue(Op.ResNo);
651}
652
653static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
654 SDOperand Copy;
655 SDOperand Chain = Op.getOperand(0);
656 switch(Op.getNumOperands()) {
657 default:
658 assert(0 && "Do not know how to return this many arguments!");
659 abort();
660 case 1: {
661 SDOperand LR = DAG.getRegister(ARM::LR, MVT::i32);
662 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
663 }
664 case 3:
665 Op = Op.getOperand(1);
666 if (Op.getValueType() == MVT::f32) {
667 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
668 } else if (Op.getValueType() == MVT::f64) {
669 // Recursively legalize f64 -> i64.
670 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Op);
671 return DAG.getNode(ISD::RET, MVT::Other, Chain, Op,
672 DAG.getConstant(0, MVT::i32));
673 }
674 Copy = DAG.getCopyToReg(Chain, ARM::R0, Op, SDOperand());
675 if (DAG.getMachineFunction().liveout_empty())
676 DAG.getMachineFunction().addLiveOut(ARM::R0);
677 break;
678 case 5:
679 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
680 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
681 // If we haven't noted the R0+R1 are live out, do so now.
682 if (DAG.getMachineFunction().liveout_empty()) {
683 DAG.getMachineFunction().addLiveOut(ARM::R0);
684 DAG.getMachineFunction().addLiveOut(ARM::R1);
685 }
686 break;
687 }
688
689 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
690 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
691}
692
693// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
694// their target countpart wrapped in the ARMISD::Wrapper node. Suppose N is
695// one of the above mentioned nodes. It has to be wrapped because otherwise
696// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
697// be used to form addressing mode. These wrapped nodes will be selected
Evan Cheng9f6636f2007-03-19 07:48:02 +0000698// into MOVi.
Evan Chenga8e29892007-01-19 07:51:42 +0000699static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
700 MVT::ValueType PtrVT = Op.getValueType();
701 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
702 SDOperand Res;
703 if (CP->isMachineConstantPoolEntry())
704 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
705 CP->getAlignment());
706 else
707 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
708 CP->getAlignment());
709 return DAG.getNode(ARMISD::Wrapper, MVT::i32, Res);
710}
711
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000712// Lower ISD::GlobalTLSAddress using the "general dynamic" model
713SDOperand
714ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
715 SelectionDAG &DAG) {
716 MVT::ValueType PtrVT = getPointerTy();
717 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
718 ARMConstantPoolValue *CPV =
719 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
720 PCAdj, "tlsgd", true);
721 SDOperand Argument = DAG.getTargetConstantPool(CPV, PtrVT, 2);
722 Argument = DAG.getNode(ARMISD::Wrapper, MVT::i32, Argument);
723 Argument = DAG.getLoad(PtrVT, DAG.getEntryNode(), Argument, NULL, 0);
724 SDOperand Chain = Argument.getValue(1);
725
726 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
727 Argument = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Argument, PICLabel);
728
729 // call __tls_get_addr.
730 ArgListTy Args;
731 ArgListEntry Entry;
732 Entry.Node = Argument;
733 Entry.Ty = (const Type *) Type::Int32Ty;
734 Args.push_back(Entry);
735 std::pair<SDOperand, SDOperand> CallResult =
736 LowerCallTo(Chain, (const Type *) Type::Int32Ty, false, false,
737 CallingConv::C, false,
738 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG);
739 return CallResult.first;
740}
741
742// Lower ISD::GlobalTLSAddress using the "initial exec" or
743// "local exec" model.
744SDOperand
745ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
746 SelectionDAG &DAG) {
747 GlobalValue *GV = GA->getGlobal();
748 SDOperand Offset;
749 SDOperand Chain = DAG.getEntryNode();
750 MVT::ValueType PtrVT = getPointerTy();
751 // Get the Thread Pointer
752 SDOperand ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, PtrVT);
753
754 if (GV->isDeclaration()){
755 // initial exec model
756 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
757 ARMConstantPoolValue *CPV =
758 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
759 PCAdj, "gottpoff", true);
760 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 2);
761 Offset = DAG.getNode(ARMISD::Wrapper, MVT::i32, Offset);
762 Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0);
763 Chain = Offset.getValue(1);
764
765 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
766 Offset = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Offset, PICLabel);
767
768 Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0);
769 } else {
770 // local exec model
771 ARMConstantPoolValue *CPV =
772 new ARMConstantPoolValue(GV, ARMCP::CPValue, "tpoff");
773 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 2);
774 Offset = DAG.getNode(ARMISD::Wrapper, MVT::i32, Offset);
775 Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0);
776 }
777
778 // The address of the thread local variable is the add of the thread
779 // pointer with the offset of the variable.
780 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
781}
782
783SDOperand
784ARMTargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
785 // TODO: implement the "local dynamic" model
786 assert(Subtarget->isTargetELF() &&
787 "TLS not implemented for non-ELF targets");
788 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
789 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
790 // otherwise use the "Local Exec" TLS Model
791 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
792 return LowerToTLSGeneralDynamicModel(GA, DAG);
793 else
794 return LowerToTLSExecModels(GA, DAG);
795}
796
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000797SDOperand ARMTargetLowering::LowerGlobalAddressELF(SDOperand Op,
798 SelectionDAG &DAG) {
799 MVT::ValueType PtrVT = getPointerTy();
800 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
801 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
802 if (RelocM == Reloc::PIC_) {
Lauro Ramos Venancio5d3d44a2007-05-14 23:20:21 +0000803 bool UseGOTOFF = GV->hasInternalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000804 ARMConstantPoolValue *CPV =
805 new ARMConstantPoolValue(GV, ARMCP::CPValue, UseGOTOFF ? "GOTOFF":"GOT");
806 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
807 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
808 SDOperand Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
809 SDOperand Chain = Result.getValue(1);
810 SDOperand GOT = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PtrVT);
811 Result = DAG.getNode(ISD::ADD, PtrVT, Result, GOT);
812 if (!UseGOTOFF)
813 Result = DAG.getLoad(PtrVT, Chain, Result, NULL, 0);
814 return Result;
815 } else {
816 SDOperand CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 2);
817 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
818 return DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
819 }
820}
821
Evan Chenga8e29892007-01-19 07:51:42 +0000822/// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol
Evan Cheng97c9bb52007-05-04 00:26:58 +0000823/// even in non-static mode.
824static bool GVIsIndirectSymbol(GlobalValue *GV, Reloc::Model RelocM) {
825 return RelocM != Reloc::Static &&
826 (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
Gabor Greifa99be512007-07-05 17:07:56 +0000827 (GV->isDeclaration() && !GV->hasNotBeenReadFromBitcode()));
Evan Chenga8e29892007-01-19 07:51:42 +0000828}
829
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000830SDOperand ARMTargetLowering::LowerGlobalAddressDarwin(SDOperand Op,
831 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +0000832 MVT::ValueType PtrVT = getPointerTy();
833 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
834 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng97c9bb52007-05-04 00:26:58 +0000835 bool IsIndirect = GVIsIndirectSymbol(GV, RelocM);
Evan Chenga8e29892007-01-19 07:51:42 +0000836 SDOperand CPAddr;
837 if (RelocM == Reloc::Static)
838 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 2);
839 else {
840 unsigned PCAdj = (RelocM != Reloc::PIC_)
841 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Chengc60e76d2007-01-30 20:37:08 +0000842 ARMCP::ARMCPKind Kind = IsIndirect ? ARMCP::CPNonLazyPtr
843 : ARMCP::CPValue;
Evan Chenga8e29892007-01-19 07:51:42 +0000844 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
Evan Chengc60e76d2007-01-30 20:37:08 +0000845 Kind, PCAdj);
Evan Chenga8e29892007-01-19 07:51:42 +0000846 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
847 }
848 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
849
850 SDOperand Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
851 SDOperand Chain = Result.getValue(1);
852
853 if (RelocM == Reloc::PIC_) {
854 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
855 Result = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Result, PICLabel);
856 }
857 if (IsIndirect)
858 Result = DAG.getLoad(PtrVT, Chain, Result, NULL, 0);
859
860 return Result;
861}
862
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000863SDOperand ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDOperand Op,
864 SelectionDAG &DAG){
865 assert(Subtarget->isTargetELF() &&
866 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
867 MVT::ValueType PtrVT = getPointerTy();
868 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
869 ARMConstantPoolValue *CPV = new ARMConstantPoolValue("_GLOBAL_OFFSET_TABLE_",
870 ARMPCLabelIndex,
871 ARMCP::CPValue, PCAdj);
872 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
873 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
874 SDOperand Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
875 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
876 return DAG.getNode(ARMISD::PIC_ADD, PtrVT, Result, PICLabel);
877}
878
Evan Chenga8e29892007-01-19 07:51:42 +0000879static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
880 unsigned VarArgsFrameIndex) {
881 // vastart just stores the address of the VarArgsFrameIndex slot into the
882 // memory location argument.
883 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
884 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
885 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
886 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), SV->getValue(),
887 SV->getOffset());
888}
889
890static SDOperand LowerFORMAL_ARGUMENT(SDOperand Op, SelectionDAG &DAG,
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000891 unsigned *vRegs, unsigned ArgNo,
Evan Chenga8e29892007-01-19 07:51:42 +0000892 unsigned &NumGPRs, unsigned &ArgOffset) {
893 MachineFunction &MF = DAG.getMachineFunction();
894 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
895 SDOperand Root = Op.getOperand(0);
896 std::vector<SDOperand> ArgValues;
897 SSARegMap *RegMap = MF.getSSARegMap();
898
899 static const unsigned GPRArgRegs[] = {
900 ARM::R0, ARM::R1, ARM::R2, ARM::R3
901 };
902
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000903 unsigned ObjSize;
904 unsigned ObjGPRs;
905 unsigned GPRPad;
906 unsigned StackPad;
907 unsigned Flags = Op.getConstantOperandVal(ArgNo + 3);
908 HowToPassArgument(ObjectVT, NumGPRs, ArgOffset, ObjGPRs,
909 ObjSize, GPRPad, StackPad, Flags);
910 NumGPRs += GPRPad;
911 ArgOffset += StackPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000912
913 SDOperand ArgValue;
914 if (ObjGPRs == 1) {
915 unsigned VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
916 MF.addLiveIn(GPRArgRegs[NumGPRs], VReg);
917 vRegs[NumGPRs] = VReg;
918 ArgValue = DAG.getCopyFromReg(Root, VReg, MVT::i32);
919 if (ObjectVT == MVT::f32)
920 ArgValue = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, ArgValue);
921 } else if (ObjGPRs == 2) {
922 unsigned VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
923 MF.addLiveIn(GPRArgRegs[NumGPRs], VReg);
924 vRegs[NumGPRs] = VReg;
925 ArgValue = DAG.getCopyFromReg(Root, VReg, MVT::i32);
926
927 VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
928 MF.addLiveIn(GPRArgRegs[NumGPRs+1], VReg);
929 vRegs[NumGPRs+1] = VReg;
930 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, VReg, MVT::i32);
931
932 if (ObjectVT == MVT::i64)
933 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
934 else
935 ArgValue = DAG.getNode(ARMISD::FMDRR, MVT::f64, ArgValue, ArgValue2);
936 }
937 NumGPRs += ObjGPRs;
938
939 if (ObjSize) {
940 // If the argument is actually used, emit a load from the right stack
941 // slot.
942 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
943 MachineFrameInfo *MFI = MF.getFrameInfo();
944 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
945 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
946 if (ObjGPRs == 0)
947 ArgValue = DAG.getLoad(ObjectVT, Root, FIN, NULL, 0);
948 else {
949 SDOperand ArgValue2 =
950 DAG.getLoad(MVT::i32, Root, FIN, NULL, 0);
951 if (ObjectVT == MVT::i64)
952 ArgValue= DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
953 else
954 ArgValue= DAG.getNode(ARMISD::FMDRR, MVT::f64, ArgValue, ArgValue2);
955 }
956 } else {
957 // Don't emit a dead load.
958 ArgValue = DAG.getNode(ISD::UNDEF, ObjectVT);
959 }
960
961 ArgOffset += ObjSize; // Move on to the next argument.
962 }
963
964 return ArgValue;
965}
966
967SDOperand
968ARMTargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
969 std::vector<SDOperand> ArgValues;
970 SDOperand Root = Op.getOperand(0);
971 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
972 unsigned NumGPRs = 0; // GPRs used for parameter passing.
973 unsigned VRegs[4];
974
975 unsigned NumArgs = Op.Val->getNumValues()-1;
976 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo)
977 ArgValues.push_back(LowerFORMAL_ARGUMENT(Op, DAG, VRegs, ArgNo,
978 NumGPRs, ArgOffset));
979
980 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
981 if (isVarArg) {
982 static const unsigned GPRArgRegs[] = {
983 ARM::R0, ARM::R1, ARM::R2, ARM::R3
984 };
985
986 MachineFunction &MF = DAG.getMachineFunction();
987 SSARegMap *RegMap = MF.getSSARegMap();
988 MachineFrameInfo *MFI = MF.getFrameInfo();
989 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +0000990 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
991 unsigned VARegSize = (4 - NumGPRs) * 4;
992 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Evan Chenga8e29892007-01-19 07:51:42 +0000993 if (VARegSaveSize) {
994 // If this function is vararg, store any remaining integer argument regs
995 // to their spots on the stack so that they may be loaded by deferencing
996 // the result of va_next.
997 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +0000998 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
999 VARegSaveSize - VARegSize);
Evan Chenga8e29892007-01-19 07:51:42 +00001000 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
1001
1002 SmallVector<SDOperand, 4> MemOps;
1003 for (; NumGPRs < 4; ++NumGPRs) {
1004 unsigned VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
1005 MF.addLiveIn(GPRArgRegs[NumGPRs], VReg);
1006 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
1007 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1008 MemOps.push_back(Store);
1009 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1010 DAG.getConstant(4, getPointerTy()));
1011 }
1012 if (!MemOps.empty())
1013 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1014 &MemOps[0], MemOps.size());
1015 } else
1016 // This will point to the next argument passed via stack.
1017 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1018 }
1019
1020 ArgValues.push_back(Root);
1021
1022 // Return the new list of results.
1023 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
1024 Op.Val->value_end());
1025 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
1026}
1027
1028/// isFloatingPointZero - Return true if this is +0.0.
1029static bool isFloatingPointZero(SDOperand Op) {
1030 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
1031 return CFP->isExactlyValue(0.0);
1032 else if (ISD::isEXTLoad(Op.Val) || ISD::isNON_EXTLoad(Op.Val)) {
1033 // Maybe this has already been legalized into the constant pool?
1034 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
1035 SDOperand WrapperOp = Op.getOperand(1).getOperand(0);
1036 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1037 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
1038 return CFP->isExactlyValue(0.0);
1039 }
1040 }
1041 return false;
1042}
1043
Evan Cheng9a2ef952007-02-02 01:53:26 +00001044static bool isLegalCmpImmediate(unsigned C, bool isThumb) {
Evan Chenga8e29892007-01-19 07:51:42 +00001045 return ( isThumb && (C & ~255U) == 0) ||
1046 (!isThumb && ARM_AM::getSOImmVal(C) != -1);
1047}
1048
1049/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1050/// the given operands.
1051static SDOperand getARMCmp(SDOperand LHS, SDOperand RHS, ISD::CondCode CC,
1052 SDOperand &ARMCC, SelectionDAG &DAG, bool isThumb) {
1053 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.Val)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001054 unsigned C = RHSC->getValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001055 if (!isLegalCmpImmediate(C, isThumb)) {
1056 // Constant does not fit, try adjusting it by one?
1057 switch (CC) {
1058 default: break;
1059 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001060 case ISD::SETGE:
Evan Chenga8e29892007-01-19 07:51:42 +00001061 if (isLegalCmpImmediate(C-1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001062 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
1063 RHS = DAG.getConstant(C-1, MVT::i32);
1064 }
1065 break;
1066 case ISD::SETULT:
1067 case ISD::SETUGE:
1068 if (C > 0 && isLegalCmpImmediate(C-1, isThumb)) {
1069 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Evan Chenga8e29892007-01-19 07:51:42 +00001070 RHS = DAG.getConstant(C-1, MVT::i32);
1071 }
1072 break;
1073 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001074 case ISD::SETGT:
Evan Chenga8e29892007-01-19 07:51:42 +00001075 if (isLegalCmpImmediate(C+1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001076 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
1077 RHS = DAG.getConstant(C+1, MVT::i32);
1078 }
1079 break;
1080 case ISD::SETULE:
1081 case ISD::SETUGT:
1082 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb)) {
1083 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Evan Chenga8e29892007-01-19 07:51:42 +00001084 RHS = DAG.getConstant(C+1, MVT::i32);
1085 }
1086 break;
1087 }
1088 }
1089 }
1090
1091 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001092 ARMISD::NodeType CompareType;
1093 switch (CondCode) {
1094 default:
1095 CompareType = ARMISD::CMP;
1096 break;
1097 case ARMCC::EQ:
1098 case ARMCC::NE:
1099 case ARMCC::MI:
1100 case ARMCC::PL:
1101 // Uses only N and Z Flags
1102 CompareType = ARMISD::CMPNZ;
1103 break;
1104 }
Evan Chenga8e29892007-01-19 07:51:42 +00001105 ARMCC = DAG.getConstant(CondCode, MVT::i32);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001106 return DAG.getNode(CompareType, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001107}
1108
1109/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
1110static SDOperand getVFPCmp(SDOperand LHS, SDOperand RHS, SelectionDAG &DAG) {
1111 SDOperand Cmp;
1112 if (!isFloatingPointZero(RHS))
1113 Cmp = DAG.getNode(ARMISD::CMPFP, MVT::Flag, LHS, RHS);
1114 else
1115 Cmp = DAG.getNode(ARMISD::CMPFPw0, MVT::Flag, LHS);
1116 return DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp);
1117}
1118
1119static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG,
1120 const ARMSubtarget *ST) {
1121 MVT::ValueType VT = Op.getValueType();
1122 SDOperand LHS = Op.getOperand(0);
1123 SDOperand RHS = Op.getOperand(1);
1124 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1125 SDOperand TrueVal = Op.getOperand(2);
1126 SDOperand FalseVal = Op.getOperand(3);
1127
1128 if (LHS.getValueType() == MVT::i32) {
1129 SDOperand ARMCC;
Evan Cheng0e1d3792007-07-05 07:18:20 +00001130 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001131 SDOperand Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb());
Evan Cheng0e1d3792007-07-05 07:18:20 +00001132 return DAG.getNode(ARMISD::CMOV, VT, FalseVal, TrueVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001133 }
1134
1135 ARMCC::CondCodes CondCode, CondCode2;
1136 if (FPCCToARMCC(CC, CondCode, CondCode2))
1137 std::swap(TrueVal, FalseVal);
1138
1139 SDOperand ARMCC = DAG.getConstant(CondCode, MVT::i32);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001140 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001141 SDOperand Cmp = getVFPCmp(LHS, RHS, DAG);
1142 SDOperand Result = DAG.getNode(ARMISD::CMOV, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001143 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001144 if (CondCode2 != ARMCC::AL) {
1145 SDOperand ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
1146 // FIXME: Needs another CMP because flag can have but one use.
1147 SDOperand Cmp2 = getVFPCmp(LHS, RHS, DAG);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001148 Result = DAG.getNode(ARMISD::CMOV, VT, Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001149 }
1150 return Result;
1151}
1152
1153static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG,
1154 const ARMSubtarget *ST) {
1155 SDOperand Chain = Op.getOperand(0);
1156 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1157 SDOperand LHS = Op.getOperand(2);
1158 SDOperand RHS = Op.getOperand(3);
1159 SDOperand Dest = Op.getOperand(4);
1160
1161 if (LHS.getValueType() == MVT::i32) {
1162 SDOperand ARMCC;
Evan Cheng0e1d3792007-07-05 07:18:20 +00001163 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001164 SDOperand Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb());
Evan Cheng0e1d3792007-07-05 07:18:20 +00001165 return DAG.getNode(ARMISD::BRCOND, MVT::Other, Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001166 }
1167
1168 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
1169 ARMCC::CondCodes CondCode, CondCode2;
1170 if (FPCCToARMCC(CC, CondCode, CondCode2))
1171 // Swap the LHS/RHS of the comparison if needed.
1172 std::swap(LHS, RHS);
1173
1174 SDOperand Cmp = getVFPCmp(LHS, RHS, DAG);
1175 SDOperand ARMCC = DAG.getConstant(CondCode, MVT::i32);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001176 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001177 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001178 SDOperand Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
1179 SDOperand Res = DAG.getNode(ARMISD::BRCOND, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001180 if (CondCode2 != ARMCC::AL) {
1181 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001182 SDOperand Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
1183 Res = DAG.getNode(ARMISD::BRCOND, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001184 }
1185 return Res;
1186}
1187
1188SDOperand ARMTargetLowering::LowerBR_JT(SDOperand Op, SelectionDAG &DAG) {
1189 SDOperand Chain = Op.getOperand(0);
1190 SDOperand Table = Op.getOperand(1);
1191 SDOperand Index = Op.getOperand(2);
1192
1193 MVT::ValueType PTy = getPointerTy();
1194 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1195 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
1196 SDOperand UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
1197 SDOperand JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
1198 Table = DAG.getNode(ARMISD::WrapperJT, MVT::i32, JTI, UId);
1199 Index = DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(4, PTy));
1200 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
1201 bool isPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Evan Chenge2446c62007-06-26 18:31:22 +00001202 Addr = DAG.getLoad(isPIC ? (MVT::ValueType)MVT::i32 : PTy,
1203 Chain, Addr, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001204 Chain = Addr.getValue(1);
1205 if (isPIC)
1206 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Table);
1207 return DAG.getNode(ARMISD::BR_JT, MVT::Other, Chain, Addr, JTI, UId);
1208}
1209
1210static SDOperand LowerFP_TO_INT(SDOperand Op, SelectionDAG &DAG) {
1211 unsigned Opc =
1212 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
1213 Op = DAG.getNode(Opc, MVT::f32, Op.getOperand(0));
1214 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
1215}
1216
1217static SDOperand LowerINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
1218 MVT::ValueType VT = Op.getValueType();
1219 unsigned Opc =
1220 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1221
1222 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Op.getOperand(0));
1223 return DAG.getNode(Opc, VT, Op);
1224}
1225
1226static SDOperand LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
1227 // Implement fcopysign with a fabs and a conditional fneg.
1228 SDOperand Tmp0 = Op.getOperand(0);
1229 SDOperand Tmp1 = Op.getOperand(1);
1230 MVT::ValueType VT = Op.getValueType();
1231 MVT::ValueType SrcVT = Tmp1.getValueType();
1232 SDOperand AbsVal = DAG.getNode(ISD::FABS, VT, Tmp0);
1233 SDOperand Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG);
1234 SDOperand ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001235 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1236 return DAG.getNode(ARMISD::CNEG, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001237}
1238
1239static SDOperand LowerBIT_CONVERT(SDOperand Op, SelectionDAG &DAG) {
1240 // Turn f64->i64 into FMRRD.
1241 assert(Op.getValueType() == MVT::i64 &&
1242 Op.getOperand(0).getValueType() == MVT::f64);
1243
1244 Op = Op.getOperand(0);
1245 SDOperand Cvt = DAG.getNode(ARMISD::FMRRD, DAG.getVTList(MVT::i32, MVT::i32),
1246 &Op, 1);
1247
1248 // Merge the pieces into a single i64 value.
1249 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Cvt, Cvt.getValue(1));
1250}
1251
1252static SDOperand LowerMUL(SDOperand Op, SelectionDAG &DAG) {
1253 // FIXME: All this code is target-independent. Create a new target-indep
1254 // MULHILO node and move this code to the legalizer.
1255 //
1256 assert(Op.getValueType() == MVT::i64 && "Only handles i64 expand right now!");
1257
1258 SDOperand LL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
1259 DAG.getConstant(0, MVT::i32));
1260 SDOperand RL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(1),
1261 DAG.getConstant(0, MVT::i32));
1262
Dan Gohmanea859be2007-06-22 14:59:07 +00001263 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
1264 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Evan Chenga8e29892007-01-19 07:51:42 +00001265
1266 SDOperand Lo, Hi;
1267 // Figure out how to lower this multiply.
1268 if (LHSSB >= 33 && RHSSB >= 33) {
1269 // If the input values are both sign extended, we can emit a mulhs+mul.
1270 Lo = DAG.getNode(ISD::MUL, MVT::i32, LL, RL);
1271 Hi = DAG.getNode(ISD::MULHS, MVT::i32, LL, RL);
1272 } else if (LHSSB == 32 && RHSSB == 32 &&
Dan Gohmanea859be2007-06-22 14:59:07 +00001273 DAG.MaskedValueIsZero(Op.getOperand(0), 0xFFFFFFFF00000000ULL) &&
1274 DAG.MaskedValueIsZero(Op.getOperand(1), 0xFFFFFFFF00000000ULL)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001275 // If the inputs are zero extended, use mulhu.
1276 Lo = DAG.getNode(ISD::MUL, MVT::i32, LL, RL);
1277 Hi = DAG.getNode(ISD::MULHU, MVT::i32, LL, RL);
1278 } else {
1279 SDOperand LH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
1280 DAG.getConstant(1, MVT::i32));
1281 SDOperand RH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(1),
1282 DAG.getConstant(1, MVT::i32));
1283
1284 // Lo,Hi = umul LHS, RHS.
1285 SDOperand Ops[] = { LL, RL };
1286 SDOperand UMul64 = DAG.getNode(ARMISD::MULHILOU,
1287 DAG.getVTList(MVT::i32, MVT::i32), Ops, 2);
1288 Lo = UMul64;
1289 Hi = UMul64.getValue(1);
1290 RH = DAG.getNode(ISD::MUL, MVT::i32, LL, RH);
1291 LH = DAG.getNode(ISD::MUL, MVT::i32, LH, RL);
1292 Hi = DAG.getNode(ISD::ADD, MVT::i32, Hi, RH);
1293 Hi = DAG.getNode(ISD::ADD, MVT::i32, Hi, LH);
1294 }
1295
1296 // Merge the pieces into a single i64 value.
1297 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1298}
1299
1300static SDOperand LowerMULHU(SDOperand Op, SelectionDAG &DAG) {
1301 SDOperand Ops[] = { Op.getOperand(0), Op.getOperand(1) };
1302 return DAG.getNode(ARMISD::MULHILOU,
1303 DAG.getVTList(MVT::i32, MVT::i32), Ops, 2).getValue(1);
1304}
1305
1306static SDOperand LowerMULHS(SDOperand Op, SelectionDAG &DAG) {
1307 SDOperand Ops[] = { Op.getOperand(0), Op.getOperand(1) };
1308 return DAG.getNode(ARMISD::MULHILOS,
1309 DAG.getVTList(MVT::i32, MVT::i32), Ops, 2).getValue(1);
1310}
1311
1312static SDOperand LowerSRx(SDOperand Op, SelectionDAG &DAG,
1313 const ARMSubtarget *ST) {
1314 assert(Op.getValueType() == MVT::i64 &&
1315 (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SRA) &&
1316 "Unknown shift to lower!");
1317
1318 // We only lower SRA, SRL of 1 here, all others use generic lowering.
1319 if (!isa<ConstantSDNode>(Op.getOperand(1)) ||
1320 cast<ConstantSDNode>(Op.getOperand(1))->getValue() != 1)
1321 return SDOperand();
1322
1323 // If we are in thumb mode, we don't have RRX.
1324 if (ST->isThumb()) return SDOperand();
1325
1326 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
1327 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
1328 DAG.getConstant(0, MVT::i32));
1329 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
1330 DAG.getConstant(1, MVT::i32));
1331
1332 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
1333 // captures the result into a carry flag.
1334 unsigned Opc = Op.getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
1335 Hi = DAG.getNode(Opc, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
1336
1337 // The low part is an ARMISD::RRX operand, which shifts the carry in.
1338 Lo = DAG.getNode(ARMISD::RRX, MVT::i32, Lo, Hi.getValue(1));
1339
1340 // Merge the pieces into a single i64 value.
1341 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1342}
1343
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001344SDOperand ARMTargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
1345 SDOperand Chain = Op.getOperand(0);
1346 SDOperand Dest = Op.getOperand(1);
1347 SDOperand Src = Op.getOperand(2);
1348 SDOperand Count = Op.getOperand(3);
1349 unsigned Align =
1350 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1351 if (Align == 0) Align = 1;
1352
1353 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Count);
1354 // Just call memcpy if:
1355 // not 4-byte aligned
1356 // size is unknown
1357 // size is >= the threshold.
1358 if ((Align & 3) != 0 ||
1359 !I ||
1360 I->getValue() >= 64 ||
1361 (I->getValue() & 3) != 0) {
1362 MVT::ValueType IntPtr = getPointerTy();
1363 TargetLowering::ArgListTy Args;
1364 TargetLowering::ArgListEntry Entry;
1365 Entry.Ty = getTargetData()->getIntPtrType();
1366 Entry.Node = Op.getOperand(1); Args.push_back(Entry);
1367 Entry.Node = Op.getOperand(2); Args.push_back(Entry);
1368 Entry.Node = Op.getOperand(3); Args.push_back(Entry);
1369 std::pair<SDOperand,SDOperand> CallResult =
1370 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
1371 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
1372 return CallResult.second;
1373 }
1374
1375 // Otherwise do repeated 4-byte loads and stores. To be improved.
1376 assert((I->getValue() & 3) == 0);
1377 assert((Align & 3) == 0);
1378 unsigned NumMemOps = I->getValue() >> 2;
1379 unsigned EmittedNumMemOps = 0;
1380 unsigned SrcOff = 0, DstOff = 0;
1381 MVT::ValueType VT = MVT::i32;
1382 unsigned VTSize = 4;
Evan Chenge5e7ce42007-05-18 01:19:57 +00001383 const unsigned MAX_LOADS_IN_LDM = 6;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001384 SDOperand LoadChains[MAX_LOADS_IN_LDM];
1385 SDOperand Loads[MAX_LOADS_IN_LDM];
1386
1387 // Emit up to 4 loads, then a TokenFactor barrier, then the same
1388 // number of stores. The loads and stores will get combined into
1389 // ldm/stm later on.
1390 while(EmittedNumMemOps < NumMemOps) {
Evan Chenge5e7ce42007-05-18 01:19:57 +00001391 unsigned i;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001392 for (i=0; i<MAX_LOADS_IN_LDM && EmittedNumMemOps+i < NumMemOps; i++) {
1393 Loads[i] = DAG.getLoad(VT, Chain,
1394 DAG.getNode(ISD::ADD, VT, Src,
1395 DAG.getConstant(SrcOff, VT)),
1396 NULL, 0);
1397 LoadChains[i] = Loads[i].getValue(1);
1398 SrcOff += VTSize;
1399 }
1400
1401 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &LoadChains[0], i);
1402
1403 for (i=0; i<MAX_LOADS_IN_LDM && EmittedNumMemOps+i < NumMemOps; i++) {
1404 Chain = DAG.getStore(Chain, Loads[i],
1405 DAG.getNode(ISD::ADD, VT, Dest,
1406 DAG.getConstant(DstOff, VT)),
1407 NULL, 0);
1408 DstOff += VTSize;
1409 }
1410 EmittedNumMemOps += i;
1411 }
1412
1413 return Chain;
1414}
1415
Evan Chenga8e29892007-01-19 07:51:42 +00001416SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1417 switch (Op.getOpcode()) {
1418 default: assert(0 && "Don't know how to custom lower this!"); abort();
1419 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001420 case ISD::GlobalAddress:
1421 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
1422 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001423 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00001424 case ISD::CALL: return LowerCALL(Op, DAG);
1425 case ISD::RET: return LowerRET(Op, DAG);
1426 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
1427 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
1428 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
1429 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
1430 case ISD::SINT_TO_FP:
1431 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
1432 case ISD::FP_TO_SINT:
1433 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
1434 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
1435 case ISD::BIT_CONVERT: return LowerBIT_CONVERT(Op, DAG);
1436 case ISD::MUL: return LowerMUL(Op, DAG);
1437 case ISD::MULHU: return LowerMULHU(Op, DAG);
1438 case ISD::MULHS: return LowerMULHS(Op, DAG);
1439 case ISD::SRL:
1440 case ISD::SRA: return LowerSRx(Op, DAG, Subtarget);
1441 case ISD::FORMAL_ARGUMENTS:
1442 return LowerFORMAL_ARGUMENTS(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00001443 case ISD::RETURNADDR: break;
1444 case ISD::FRAMEADDR: break;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001445 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001446 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00001447 }
Nate Begemanbcc5f362007-01-29 22:58:52 +00001448 return SDOperand();
Evan Chenga8e29892007-01-19 07:51:42 +00001449}
1450
1451//===----------------------------------------------------------------------===//
1452// ARM Scheduler Hooks
1453//===----------------------------------------------------------------------===//
1454
1455MachineBasicBlock *
1456ARMTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1457 MachineBasicBlock *BB) {
1458 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1459 switch (MI->getOpcode()) {
1460 default: assert(false && "Unexpected instr type to insert");
1461 case ARM::tMOVCCr: {
1462 // To "insert" a SELECT_CC instruction, we actually have to insert the
1463 // diamond control-flow pattern. The incoming instruction knows the
1464 // destination vreg to set, the condition code register to branch on, the
1465 // true/false values to select between, and a branch opcode to use.
1466 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1467 ilist<MachineBasicBlock>::iterator It = BB;
1468 ++It;
1469
1470 // thisMBB:
1471 // ...
1472 // TrueVal = ...
1473 // cmpTY ccX, r1, r2
1474 // bCC copy1MBB
1475 // fallthrough --> copy0MBB
1476 MachineBasicBlock *thisMBB = BB;
1477 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1478 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1479 BuildMI(BB, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00001480 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001481 MachineFunction *F = BB->getParent();
1482 F->getBasicBlockList().insert(It, copy0MBB);
1483 F->getBasicBlockList().insert(It, sinkMBB);
1484 // Update machine-CFG edges by first adding all successors of the current
1485 // block to the new block which will contain the Phi node for the select.
1486 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1487 e = BB->succ_end(); i != e; ++i)
1488 sinkMBB->addSuccessor(*i);
1489 // Next, remove all successors of the current block, and add the true
1490 // and fallthrough blocks as its successors.
1491 while(!BB->succ_empty())
1492 BB->removeSuccessor(BB->succ_begin());
1493 BB->addSuccessor(copy0MBB);
1494 BB->addSuccessor(sinkMBB);
1495
1496 // copy0MBB:
1497 // %FalseValue = ...
1498 // # fallthrough to sinkMBB
1499 BB = copy0MBB;
1500
1501 // Update machine-CFG edges
1502 BB->addSuccessor(sinkMBB);
1503
1504 // sinkMBB:
1505 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1506 // ...
1507 BB = sinkMBB;
1508 BuildMI(BB, TII->get(ARM::PHI), MI->getOperand(0).getReg())
1509 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1510 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1511
1512 delete MI; // The pseudo instruction is gone now.
1513 return BB;
1514 }
1515 }
1516}
1517
1518//===----------------------------------------------------------------------===//
1519// ARM Optimization Hooks
1520//===----------------------------------------------------------------------===//
1521
Evan Chengb01fad62007-03-12 23:30:29 +00001522/// isLegalAddressImmediate - Return true if the integer value can be used
1523/// as the offset of the target addressing mode for load / store of the
1524/// given type.
Chris Lattner37caf8c2007-04-09 23:33:39 +00001525static bool isLegalAddressImmediate(int64_t V, MVT::ValueType VT,
1526 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00001527 if (V == 0)
1528 return true;
1529
Evan Chengb01fad62007-03-12 23:30:29 +00001530 if (Subtarget->isThumb()) {
1531 if (V < 0)
1532 return false;
1533
1534 unsigned Scale = 1;
1535 switch (VT) {
1536 default: return false;
1537 case MVT::i1:
1538 case MVT::i8:
1539 // Scale == 1;
1540 break;
1541 case MVT::i16:
1542 // Scale == 2;
1543 Scale = 2;
1544 break;
1545 case MVT::i32:
1546 // Scale == 4;
1547 Scale = 4;
1548 break;
1549 }
1550
1551 if ((V & (Scale - 1)) != 0)
1552 return false;
1553 V /= Scale;
1554 return V == V & ((1LL << 5) - 1);
1555 }
1556
1557 if (V < 0)
1558 V = - V;
1559 switch (VT) {
1560 default: return false;
1561 case MVT::i1:
1562 case MVT::i8:
1563 case MVT::i32:
1564 // +- imm12
1565 return V == V & ((1LL << 12) - 1);
1566 case MVT::i16:
1567 // +- imm8
1568 return V == V & ((1LL << 8) - 1);
1569 case MVT::f32:
1570 case MVT::f64:
1571 if (!Subtarget->hasVFP2())
1572 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00001573 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00001574 return false;
1575 V >>= 2;
1576 return V == V & ((1LL << 8) - 1);
1577 }
Evan Chenga8e29892007-01-19 07:51:42 +00001578}
1579
Chris Lattner37caf8c2007-04-09 23:33:39 +00001580/// isLegalAddressingMode - Return true if the addressing mode represented
1581/// by AM is legal for this target, for a load/store of the specified type.
1582bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
1583 const Type *Ty) const {
1584 if (!isLegalAddressImmediate(AM.BaseOffs, getValueType(Ty), Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00001585 return false;
Chris Lattner37caf8c2007-04-09 23:33:39 +00001586
1587 // Can never fold addr of global into load/store.
1588 if (AM.BaseGV)
1589 return false;
1590
1591 switch (AM.Scale) {
1592 case 0: // no scale reg, must be "r+i" or "r", or "i".
1593 break;
1594 case 1:
1595 if (Subtarget->isThumb())
1596 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00001597 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00001598 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00001599 // ARM doesn't support any R+R*scale+imm addr modes.
1600 if (AM.BaseOffs)
1601 return false;
1602
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001603 int Scale = AM.Scale;
Chris Lattner37caf8c2007-04-09 23:33:39 +00001604 switch (getValueType(Ty)) {
1605 default: return false;
1606 case MVT::i1:
1607 case MVT::i8:
1608 case MVT::i32:
1609 case MVT::i64:
1610 // This assumes i64 is legalized to a pair of i32. If not (i.e.
1611 // ldrd / strd are used, then its address mode is same as i16.
1612 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001613 if (Scale < 0) Scale = -Scale;
1614 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001615 return true;
1616 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00001617 return isPowerOf2_32(Scale & ~1);
Chris Lattner37caf8c2007-04-09 23:33:39 +00001618 case MVT::i16:
1619 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001620 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001621 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00001622 return false;
1623
Chris Lattner37caf8c2007-04-09 23:33:39 +00001624 case MVT::isVoid:
1625 // Note, we allow "void" uses (basically, uses that aren't loads or
1626 // stores), because arm allows folding a scale into many arithmetic
1627 // operations. This should be made more precise and revisited later.
Chris Lattnerb2c594f2007-04-03 00:13:57 +00001628
Chris Lattner37caf8c2007-04-09 23:33:39 +00001629 // Allow r << imm, but the imm has to be a multiple of two.
1630 if (AM.Scale & 1) return false;
1631 return isPowerOf2_32(AM.Scale);
1632 }
1633 break;
Evan Chengb01fad62007-03-12 23:30:29 +00001634 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00001635 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00001636}
1637
Chris Lattner37caf8c2007-04-09 23:33:39 +00001638
Evan Chenga8e29892007-01-19 07:51:42 +00001639static bool getIndexedAddressParts(SDNode *Ptr, MVT::ValueType VT,
1640 bool isSEXTLoad, SDOperand &Base,
1641 SDOperand &Offset, bool &isInc,
1642 SelectionDAG &DAG) {
1643 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
1644 return false;
1645
1646 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
1647 // AddressingMode 3
1648 Base = Ptr->getOperand(0);
1649 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
1650 int RHSC = (int)RHS->getValue();
1651 if (RHSC < 0 && RHSC > -256) {
1652 isInc = false;
1653 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1654 return true;
1655 }
1656 }
1657 isInc = (Ptr->getOpcode() == ISD::ADD);
1658 Offset = Ptr->getOperand(1);
1659 return true;
1660 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
1661 // AddressingMode 2
1662 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
1663 int RHSC = (int)RHS->getValue();
1664 if (RHSC < 0 && RHSC > -0x1000) {
1665 isInc = false;
1666 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1667 Base = Ptr->getOperand(0);
1668 return true;
1669 }
1670 }
1671
1672 if (Ptr->getOpcode() == ISD::ADD) {
1673 isInc = true;
1674 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
1675 if (ShOpcVal != ARM_AM::no_shift) {
1676 Base = Ptr->getOperand(1);
1677 Offset = Ptr->getOperand(0);
1678 } else {
1679 Base = Ptr->getOperand(0);
1680 Offset = Ptr->getOperand(1);
1681 }
1682 return true;
1683 }
1684
1685 isInc = (Ptr->getOpcode() == ISD::ADD);
1686 Base = Ptr->getOperand(0);
1687 Offset = Ptr->getOperand(1);
1688 return true;
1689 }
1690
1691 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
1692 return false;
1693}
1694
1695/// getPreIndexedAddressParts - returns true by value, base pointer and
1696/// offset pointer and addressing mode by reference if the node's address
1697/// can be legally represented as pre-indexed load / store address.
1698bool
1699ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDOperand &Base,
1700 SDOperand &Offset,
1701 ISD::MemIndexedMode &AM,
1702 SelectionDAG &DAG) {
1703 if (Subtarget->isThumb())
1704 return false;
1705
1706 MVT::ValueType VT;
1707 SDOperand Ptr;
1708 bool isSEXTLoad = false;
1709 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1710 Ptr = LD->getBasePtr();
1711 VT = LD->getLoadedVT();
1712 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1713 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
1714 Ptr = ST->getBasePtr();
1715 VT = ST->getStoredVT();
1716 } else
1717 return false;
1718
1719 bool isInc;
1720 bool isLegal = getIndexedAddressParts(Ptr.Val, VT, isSEXTLoad, Base, Offset,
1721 isInc, DAG);
1722 if (isLegal) {
1723 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
1724 return true;
1725 }
1726 return false;
1727}
1728
1729/// getPostIndexedAddressParts - returns true by value, base pointer and
1730/// offset pointer and addressing mode by reference if this node can be
1731/// combined with a load / store to form a post-indexed load / store.
1732bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
1733 SDOperand &Base,
1734 SDOperand &Offset,
1735 ISD::MemIndexedMode &AM,
1736 SelectionDAG &DAG) {
1737 if (Subtarget->isThumb())
1738 return false;
1739
1740 MVT::ValueType VT;
1741 SDOperand Ptr;
1742 bool isSEXTLoad = false;
1743 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1744 VT = LD->getLoadedVT();
1745 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1746 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
1747 VT = ST->getStoredVT();
1748 } else
1749 return false;
1750
1751 bool isInc;
1752 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
1753 isInc, DAG);
1754 if (isLegal) {
1755 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
1756 return true;
1757 }
1758 return false;
1759}
1760
1761void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
1762 uint64_t Mask,
1763 uint64_t &KnownZero,
1764 uint64_t &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001765 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00001766 unsigned Depth) const {
1767 KnownZero = 0;
1768 KnownOne = 0;
1769 switch (Op.getOpcode()) {
1770 default: break;
1771 case ARMISD::CMOV: {
1772 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00001773 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00001774 if (KnownZero == 0 && KnownOne == 0) return;
1775
1776 uint64_t KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00001777 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
1778 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00001779 KnownZero &= KnownZeroRHS;
1780 KnownOne &= KnownOneRHS;
1781 return;
1782 }
1783 }
1784}
1785
1786//===----------------------------------------------------------------------===//
1787// ARM Inline Assembly Support
1788//===----------------------------------------------------------------------===//
1789
1790/// getConstraintType - Given a constraint letter, return the type of
1791/// constraint it is for this target.
1792ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00001793ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
1794 if (Constraint.size() == 1) {
1795 switch (Constraint[0]) {
1796 default: break;
1797 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001798 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00001799 }
Evan Chenga8e29892007-01-19 07:51:42 +00001800 }
Chris Lattner4234f572007-03-25 02:14:49 +00001801 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00001802}
1803
1804std::pair<unsigned, const TargetRegisterClass*>
1805ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
1806 MVT::ValueType VT) const {
1807 if (Constraint.size() == 1) {
1808 // GCC RS6000 Constraint Letters
1809 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001810 case 'l':
1811 // FIXME: in thumb mode, 'l' is only low-regs.
1812 // FALL THROUGH.
1813 case 'r':
1814 return std::make_pair(0U, ARM::GPRRegisterClass);
1815 case 'w':
1816 if (VT == MVT::f32)
1817 return std::make_pair(0U, ARM::SPRRegisterClass);
Evan Cheng0a7baa22007-04-04 00:06:07 +00001818 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001819 return std::make_pair(0U, ARM::DPRRegisterClass);
1820 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001821 }
1822 }
1823 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1824}
1825
1826std::vector<unsigned> ARMTargetLowering::
1827getRegClassForInlineAsmConstraint(const std::string &Constraint,
1828 MVT::ValueType VT) const {
1829 if (Constraint.size() != 1)
1830 return std::vector<unsigned>();
1831
1832 switch (Constraint[0]) { // GCC ARM Constraint Letters
1833 default: break;
1834 case 'l':
1835 case 'r':
1836 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
1837 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
1838 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
1839 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001840 case 'w':
1841 if (VT == MVT::f32)
1842 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
1843 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
1844 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
1845 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
1846 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
1847 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
1848 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
1849 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
1850 if (VT == MVT::f64)
1851 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
1852 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
1853 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
1854 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
1855 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001856 }
1857
1858 return std::vector<unsigned>();
1859}