blob: 608cc4c15de23911f95bce1fb12caa87d1f4a0e4 [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"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000026#include "llvm/Intrinsics.h"
Evan Chenga8e29892007-01-19 07:51:42 +000027#include "llvm/CodeGen/MachineBasicBlock.h"
28#include "llvm/CodeGen/MachineFrameInfo.h"
29#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
31#include "llvm/CodeGen/SelectionDAG.h"
32#include "llvm/CodeGen/SSARegMap.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000033#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000034#include "llvm/ADT/VectorExtras.h"
Evan Chengb01fad62007-03-12 23:30:29 +000035#include "llvm/Support/MathExtras.h"
Evan Chenga8e29892007-01-19 07:51:42 +000036using namespace llvm;
37
38ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
39 : TargetLowering(TM), ARMPCLabelIndex(0) {
40 Subtarget = &TM.getSubtarget<ARMSubtarget>();
41
Evan Chengb1df8f22007-04-27 08:15:43 +000042 if (Subtarget->isTargetDarwin()) {
43 // Don't have these.
44 setLibcallName(RTLIB::UINTTOFP_I64_F32, NULL);
45 setLibcallName(RTLIB::UINTTOFP_I64_F64, NULL);
Evan Chenga8e29892007-01-19 07:51:42 +000046
Evan Chengb1df8f22007-04-27 08:15:43 +000047 // Uses VFP for Thumb libfuncs if available.
48 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
49 // Single-precision floating-point arithmetic.
50 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
51 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
52 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
53 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +000054
Evan Chengb1df8f22007-04-27 08:15:43 +000055 // Double-precision floating-point arithmetic.
56 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
57 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
58 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
59 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +000060
Evan Chengb1df8f22007-04-27 08:15:43 +000061 // Single-precision comparisons.
62 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
63 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
64 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
65 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
66 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
67 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
68 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
69 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +000070
Evan Chengb1df8f22007-04-27 08:15:43 +000071 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
72 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
73 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
74 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
75 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
76 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
77 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
78 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +000079
Evan Chengb1df8f22007-04-27 08:15:43 +000080 // Double-precision comparisons.
81 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
82 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
83 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
84 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
85 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
86 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
87 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
88 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +000089
Evan Chengb1df8f22007-04-27 08:15:43 +000090 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
91 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
92 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
93 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
94 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
95 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
96 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
97 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +000098
Evan Chengb1df8f22007-04-27 08:15:43 +000099 // Floating-point to integer conversions.
100 // i64 conversions are done via library routines even when generating VFP
101 // instructions, so use the same ones.
102 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
103 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
104 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
105 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000106
Evan Chengb1df8f22007-04-27 08:15:43 +0000107 // Conversions between floating types.
108 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
109 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
110
111 // Integer to floating-point conversions.
112 // i64 conversions are done via library routines even when generating VFP
113 // instructions, so use the same ones.
114 // FIXME: There appears to be some naming inconsistency in ARM libgcc: e.g.
115 // __floatunsidf vs. __floatunssidfvfp.
116 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
117 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
118 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
119 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
120 }
Evan Chenga8e29892007-01-19 07:51:42 +0000121 }
122
123 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
Evan Chengb6ab2542007-01-31 08:40:13 +0000124 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000125 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
126 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
127 }
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000128 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000129
130 // ARM does not have f32 extending load.
131 setLoadXAction(ISD::EXTLOAD, MVT::f32, Expand);
132
133 // ARM supports all 4 flavors of integer indexed load / store.
134 for (unsigned im = (unsigned)ISD::PRE_INC;
135 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
136 setIndexedLoadAction(im, MVT::i1, Legal);
137 setIndexedLoadAction(im, MVT::i8, Legal);
138 setIndexedLoadAction(im, MVT::i16, Legal);
139 setIndexedLoadAction(im, MVT::i32, Legal);
140 setIndexedStoreAction(im, MVT::i1, Legal);
141 setIndexedStoreAction(im, MVT::i8, Legal);
142 setIndexedStoreAction(im, MVT::i16, Legal);
143 setIndexedStoreAction(im, MVT::i32, Legal);
144 }
145
146 // i64 operation support.
147 if (Subtarget->isThumb()) {
148 setOperationAction(ISD::MUL, MVT::i64, Expand);
149 setOperationAction(ISD::MULHU, MVT::i32, Expand);
150 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000151 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
152 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000153 } else {
Dan Gohman525178c2007-10-08 18:33:35 +0000154 setOperationAction(ISD::MUL, MVT::i64, Expand);
155 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000156 if (!Subtarget->hasV6Ops())
Dan Gohman525178c2007-10-08 18:33:35 +0000157 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000158 }
159 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
160 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
161 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
162 setOperationAction(ISD::SRL, MVT::i64, Custom);
163 setOperationAction(ISD::SRA, MVT::i64, Custom);
164
165 // ARM does not have ROTL.
166 setOperationAction(ISD::ROTL, MVT::i32, Expand);
167 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
168 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
Evan Chengb0636152007-02-01 23:34:03 +0000169 if (!Subtarget->hasV5TOps() || Subtarget->isThumb())
Evan Chenga8e29892007-01-19 07:51:42 +0000170 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
171
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000172 // Only ARMv6 has BSWAP.
173 if (!Subtarget->hasV6Ops())
Chris Lattner1719e132007-03-20 02:25:53 +0000174 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000175
Evan Chenga8e29892007-01-19 07:51:42 +0000176 // These are expanded into libcalls.
177 setOperationAction(ISD::SDIV, MVT::i32, Expand);
178 setOperationAction(ISD::UDIV, MVT::i32, Expand);
179 setOperationAction(ISD::SREM, MVT::i32, Expand);
180 setOperationAction(ISD::UREM, MVT::i32, Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000181 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
182 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000183
184 // Support label based line numbers.
185 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
186 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000187
188 setOperationAction(ISD::RET, MVT::Other, Custom);
189 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
190 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000191 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000192 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000193
194 // Expand mem operations genericly.
195 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000196 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000197 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
Duncan Sands36397f52007-07-27 12:58:54 +0000198
Evan Chenga8e29892007-01-19 07:51:42 +0000199 // Use the default implementation.
200 setOperationAction(ISD::VASTART , MVT::Other, Expand);
201 setOperationAction(ISD::VAARG , MVT::Other, Expand);
202 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
203 setOperationAction(ISD::VAEND , MVT::Other, Expand);
204 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
205 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
206 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
207
208 if (!Subtarget->hasV6Ops()) {
209 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
210 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
211 }
212 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
213
Evan Chengb6ab2542007-01-31 08:40:13 +0000214 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb())
Evan Chenga8e29892007-01-19 07:51:42 +0000215 // Turn f64->i64 into FMRRD iff target supports vfp2.
216 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000217
218 // We want to custom lower some of our intrinsics.
219 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
220
Evan Chenga8e29892007-01-19 07:51:42 +0000221 setOperationAction(ISD::SETCC , MVT::i32, Expand);
222 setOperationAction(ISD::SETCC , MVT::f32, Expand);
223 setOperationAction(ISD::SETCC , MVT::f64, Expand);
224 setOperationAction(ISD::SELECT , MVT::i32, Expand);
225 setOperationAction(ISD::SELECT , MVT::f32, Expand);
226 setOperationAction(ISD::SELECT , MVT::f64, Expand);
227 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
228 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
229 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
230
231 setOperationAction(ISD::BRCOND , MVT::Other, Expand);
232 setOperationAction(ISD::BR_CC , MVT::i32, Custom);
233 setOperationAction(ISD::BR_CC , MVT::f32, Custom);
234 setOperationAction(ISD::BR_CC , MVT::f64, Custom);
235 setOperationAction(ISD::BR_JT , MVT::Other, Custom);
236
237 setOperationAction(ISD::VASTART, MVT::Other, Custom);
238 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
239 setOperationAction(ISD::VAEND, MVT::Other, Expand);
240 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
241 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
242
243 // FP Constants can't be immediates.
244 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
245 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
246
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000247 // We don't support sin/cos/fmod/copysign/pow
Evan Chenga8e29892007-01-19 07:51:42 +0000248 setOperationAction(ISD::FSIN , MVT::f64, Expand);
249 setOperationAction(ISD::FSIN , MVT::f32, Expand);
250 setOperationAction(ISD::FCOS , MVT::f32, Expand);
251 setOperationAction(ISD::FCOS , MVT::f64, Expand);
252 setOperationAction(ISD::FREM , MVT::f64, Expand);
253 setOperationAction(ISD::FREM , MVT::f32, Expand);
254 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
255 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000256 setOperationAction(ISD::FPOW , MVT::f64, Expand);
257 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000258
259 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
260 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
261 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
262 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
263 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
264
265 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Chenga8e29892007-01-19 07:51:42 +0000266 setSchedulingPreference(SchedulingForRegPressure);
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000267 setIfCvtBlockSizeLimit(Subtarget->isThumb() ? 0 : 10);
Evan Cheng97e604e2007-06-19 23:55:02 +0000268 setIfCvtDupBlockSizeLimit(Subtarget->isThumb() ? 0 : 2);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000269
270 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Evan Chenga8e29892007-01-19 07:51:42 +0000271}
272
273
274const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
275 switch (Opcode) {
276 default: return 0;
277 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000278 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
279 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000280 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000281 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
282 case ARMISD::tCALL: return "ARMISD::tCALL";
283 case ARMISD::BRCOND: return "ARMISD::BRCOND";
284 case ARMISD::BR_JT: return "ARMISD::BR_JT";
285 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
286 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
287 case ARMISD::CMP: return "ARMISD::CMP";
Lauro Ramos Venancio99966632007-04-02 01:30:03 +0000288 case ARMISD::CMPNZ: return "ARMISD::CMPNZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000289 case ARMISD::CMPFP: return "ARMISD::CMPFP";
290 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
291 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
292 case ARMISD::CMOV: return "ARMISD::CMOV";
293 case ARMISD::CNEG: return "ARMISD::CNEG";
294
295 case ARMISD::FTOSI: return "ARMISD::FTOSI";
296 case ARMISD::FTOUI: return "ARMISD::FTOUI";
297 case ARMISD::SITOF: return "ARMISD::SITOF";
298 case ARMISD::UITOF: return "ARMISD::UITOF";
Evan Chenga8e29892007-01-19 07:51:42 +0000299
300 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
301 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
302 case ARMISD::RRX: return "ARMISD::RRX";
303
304 case ARMISD::FMRRD: return "ARMISD::FMRRD";
305 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000306
307 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Evan Chenga8e29892007-01-19 07:51:42 +0000308 }
309}
310
311//===----------------------------------------------------------------------===//
312// Lowering Code
313//===----------------------------------------------------------------------===//
314
315
316/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
317static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
318 switch (CC) {
319 default: assert(0 && "Unknown condition code!");
320 case ISD::SETNE: return ARMCC::NE;
321 case ISD::SETEQ: return ARMCC::EQ;
322 case ISD::SETGT: return ARMCC::GT;
323 case ISD::SETGE: return ARMCC::GE;
324 case ISD::SETLT: return ARMCC::LT;
325 case ISD::SETLE: return ARMCC::LE;
326 case ISD::SETUGT: return ARMCC::HI;
327 case ISD::SETUGE: return ARMCC::HS;
328 case ISD::SETULT: return ARMCC::LO;
329 case ISD::SETULE: return ARMCC::LS;
330 }
331}
332
333/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. It
334/// returns true if the operands should be inverted to form the proper
335/// comparison.
336static bool FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
337 ARMCC::CondCodes &CondCode2) {
338 bool Invert = false;
339 CondCode2 = ARMCC::AL;
340 switch (CC) {
341 default: assert(0 && "Unknown FP condition!");
342 case ISD::SETEQ:
343 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
344 case ISD::SETGT:
345 case ISD::SETOGT: CondCode = ARMCC::GT; break;
346 case ISD::SETGE:
347 case ISD::SETOGE: CondCode = ARMCC::GE; break;
348 case ISD::SETOLT: CondCode = ARMCC::MI; break;
349 case ISD::SETOLE: CondCode = ARMCC::GT; Invert = true; break;
350 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
351 case ISD::SETO: CondCode = ARMCC::VC; break;
352 case ISD::SETUO: CondCode = ARMCC::VS; break;
353 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
354 case ISD::SETUGT: CondCode = ARMCC::HI; break;
355 case ISD::SETUGE: CondCode = ARMCC::PL; break;
356 case ISD::SETLT:
357 case ISD::SETULT: CondCode = ARMCC::LT; break;
358 case ISD::SETLE:
359 case ISD::SETULE: CondCode = ARMCC::LE; break;
360 case ISD::SETNE:
361 case ISD::SETUNE: CondCode = ARMCC::NE; break;
362 }
363 return Invert;
364}
365
366static void
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000367HowToPassArgument(MVT::ValueType ObjectVT, unsigned NumGPRs,
368 unsigned StackOffset, unsigned &NeededGPRs,
369 unsigned &NeededStackSize, unsigned &GPRPad,
370 unsigned &StackPad, unsigned Flags) {
371 NeededStackSize = 0;
372 NeededGPRs = 0;
373 StackPad = 0;
374 GPRPad = 0;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +0000375 unsigned align = (Flags >> ISD::ParamFlags::OrigAlignmentOffs);
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000376 GPRPad = NumGPRs % ((align + 3)/4);
377 StackPad = StackOffset % align;
378 unsigned firstGPR = NumGPRs + GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000379 switch (ObjectVT) {
380 default: assert(0 && "Unhandled argument type!");
381 case MVT::i32:
382 case MVT::f32:
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000383 if (firstGPR < 4)
384 NeededGPRs = 1;
Evan Chenga8e29892007-01-19 07:51:42 +0000385 else
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000386 NeededStackSize = 4;
Evan Chenga8e29892007-01-19 07:51:42 +0000387 break;
388 case MVT::i64:
389 case MVT::f64:
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000390 if (firstGPR < 3)
391 NeededGPRs = 2;
392 else if (firstGPR == 3) {
393 NeededGPRs = 1;
394 NeededStackSize = 4;
Evan Chenga8e29892007-01-19 07:51:42 +0000395 } else
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000396 NeededStackSize = 8;
Evan Chenga8e29892007-01-19 07:51:42 +0000397 }
398}
399
Evan Chengfc403422007-02-03 08:53:01 +0000400/// LowerCALL - Lowering a ISD::CALL node into a callseq_start <-
401/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
402/// nodes.
Evan Chenga8e29892007-01-19 07:51:42 +0000403SDOperand ARMTargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
404 MVT::ValueType RetVT= Op.Val->getValueType(0);
405 SDOperand Chain = Op.getOperand(0);
406 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
407 assert((CallConv == CallingConv::C ||
Evan Chenga8e29892007-01-19 07:51:42 +0000408 CallConv == CallingConv::Fast) && "unknown calling convention");
409 SDOperand Callee = Op.getOperand(4);
410 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
411 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
412 unsigned NumGPRs = 0; // GPRs used for parameter passing.
413
414 // Count how many bytes are to be pushed on the stack.
415 unsigned NumBytes = 0;
416
417 // Add up all the space actually used.
418 for (unsigned i = 0; i < NumOps; ++i) {
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000419 unsigned ObjSize;
420 unsigned ObjGPRs;
421 unsigned StackPad;
422 unsigned GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000423 MVT::ValueType ObjectVT = Op.getOperand(5+2*i).getValueType();
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000424 unsigned Flags = Op.getConstantOperandVal(5+2*i+1);
425 HowToPassArgument(ObjectVT, NumGPRs, NumBytes, ObjGPRs, ObjSize,
426 GPRPad, StackPad, Flags);
427 NumBytes += ObjSize + StackPad;
428 NumGPRs += ObjGPRs + GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000429 }
430
431 // Adjust the stack pointer for the new arguments...
432 // These operations are automatically eliminated by the prolog/epilog pass
433 Chain = DAG.getCALLSEQ_START(Chain,
434 DAG.getConstant(NumBytes, MVT::i32));
435
436 SDOperand StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
437
438 static const unsigned GPRArgRegs[] = {
439 ARM::R0, ARM::R1, ARM::R2, ARM::R3
440 };
441
442 NumGPRs = 0;
443 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
444 std::vector<SDOperand> MemOpChains;
445 for (unsigned i = 0; i != NumOps; ++i) {
446 SDOperand Arg = Op.getOperand(5+2*i);
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000447 unsigned Flags = Op.getConstantOperandVal(5+2*i+1);
Evan Chenga8e29892007-01-19 07:51:42 +0000448 MVT::ValueType ArgVT = Arg.getValueType();
449
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000450 unsigned ObjSize;
451 unsigned ObjGPRs;
452 unsigned GPRPad;
453 unsigned StackPad;
454 HowToPassArgument(ArgVT, NumGPRs, ArgOffset, ObjGPRs,
455 ObjSize, GPRPad, StackPad, Flags);
456 NumGPRs += GPRPad;
457 ArgOffset += StackPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000458 if (ObjGPRs > 0) {
459 switch (ArgVT) {
460 default: assert(0 && "Unexpected ValueType for argument!");
461 case MVT::i32:
462 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Arg));
463 break;
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000464 case MVT::f32:
Evan Chenga8e29892007-01-19 07:51:42 +0000465 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs],
466 DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg)));
467 break;
468 case MVT::i64: {
469 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Arg,
470 DAG.getConstant(0, getPointerTy()));
471 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Arg,
472 DAG.getConstant(1, getPointerTy()));
473 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Lo));
474 if (ObjGPRs == 2)
475 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1], Hi));
476 else {
477 SDOperand PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType());
478 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
479 MemOpChains.push_back(DAG.getStore(Chain, Hi, PtrOff, NULL, 0));
480 }
481 break;
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000482 }
Evan Chenga8e29892007-01-19 07:51:42 +0000483 case MVT::f64: {
484 SDOperand Cvt = DAG.getNode(ARMISD::FMRRD,
485 DAG.getVTList(MVT::i32, MVT::i32),
486 &Arg, 1);
487 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Cvt));
488 if (ObjGPRs == 2)
489 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1],
490 Cvt.getValue(1)));
491 else {
492 SDOperand PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType());
493 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
494 MemOpChains.push_back(DAG.getStore(Chain, Cvt.getValue(1), PtrOff,
495 NULL, 0));
496 }
497 break;
498 }
499 }
500 } else {
501 assert(ObjSize != 0);
502 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
503 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
504 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
505 }
506
507 NumGPRs += ObjGPRs;
508 ArgOffset += ObjSize;
509 }
510
511 if (!MemOpChains.empty())
512 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
513 &MemOpChains[0], MemOpChains.size());
514
515 // Build a sequence of copy-to-reg nodes chained together with token chain
516 // and flag operands which copy the outgoing args into the appropriate regs.
517 SDOperand InFlag;
518 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
519 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
520 InFlag);
521 InFlag = Chain.getValue(1);
522 }
523
524 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
525 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
526 // node so that legalize doesn't hack it.
527 bool isDirect = false;
528 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +0000529 bool isLocalARMFunc = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000530 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
531 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +0000532 isDirect = true;
Reid Spencer5cbf9852007-01-30 20:08:39 +0000533 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
Evan Chenga8e29892007-01-19 07:51:42 +0000534 GV->hasLinkOnceLinkage());
Evan Cheng970a4192007-01-19 19:28:01 +0000535 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +0000536 getTargetMachine().getRelocationModel() != Reloc::Static;
537 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +0000538 // ARM call to a local ARM function is predicable.
539 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +0000540 // tBX takes a register source operand.
541 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
542 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
543 ARMCP::CPStub, 4);
544 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 2);
545 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
546 Callee = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), CPAddr, NULL, 0);
547 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
548 Callee = DAG.getNode(ARMISD::PIC_ADD, getPointerTy(), Callee, PICLabel);
549 } else
550 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000551 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000552 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +0000553 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000554 getTargetMachine().getRelocationModel() != Reloc::Static;
555 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +0000556 // tBX takes a register source operand.
557 const char *Sym = S->getSymbol();
558 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
559 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(Sym, ARMPCLabelIndex,
560 ARMCP::CPStub, 4);
561 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 2);
562 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
563 Callee = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), CPAddr, NULL, 0);
564 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
565 Callee = DAG.getNode(ARMISD::PIC_ADD, getPointerTy(), Callee, PICLabel);
566 } else
567 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000568 }
569
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000570 // FIXME: handle tail calls differently.
571 unsigned CallOpc;
572 if (Subtarget->isThumb()) {
573 if (!Subtarget->hasV5TOps() && (!isDirect || isARMFunc))
574 CallOpc = ARMISD::CALL_NOLINK;
575 else
576 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
577 } else {
578 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +0000579 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
580 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000581 }
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +0000582 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb()) {
583 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000584 Chain = DAG.getCopyToReg(Chain, ARM::LR,
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +0000585 DAG.getNode(ISD::UNDEF, MVT::i32), InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000586 InFlag = Chain.getValue(1);
587 }
588
Evan Chenga8e29892007-01-19 07:51:42 +0000589 std::vector<MVT::ValueType> NodeTys;
590 NodeTys.push_back(MVT::Other); // Returns a chain
591 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
592
593 std::vector<SDOperand> Ops;
594 Ops.push_back(Chain);
595 Ops.push_back(Callee);
596
597 // Add argument registers to the end of the list so that they are known live
598 // into the call.
599 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
600 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
601 RegsToPass[i].second.getValueType()));
602
Evan Chenga8e29892007-01-19 07:51:42 +0000603 if (InFlag.Val)
604 Ops.push_back(InFlag);
605 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
606 InFlag = Chain.getValue(1);
607
Bill Wendling0f8d9c02007-11-13 00:44:25 +0000608 Chain = DAG.getCALLSEQ_END(Chain,
609 DAG.getConstant(NumBytes, MVT::i32),
610 DAG.getConstant(0, MVT::i32),
611 InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000612 if (RetVT != MVT::Other)
613 InFlag = Chain.getValue(1);
614
615 std::vector<SDOperand> ResultVals;
616 NodeTys.clear();
617
618 // If the call has results, copy the values out of the ret val registers.
619 switch (RetVT) {
620 default: assert(0 && "Unexpected ret value!");
621 case MVT::Other:
622 break;
623 case MVT::i32:
624 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
625 ResultVals.push_back(Chain.getValue(0));
626 if (Op.Val->getValueType(1) == MVT::i32) {
627 // Returns a i64 value.
628 Chain = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32,
629 Chain.getValue(2)).getValue(1);
630 ResultVals.push_back(Chain.getValue(0));
631 NodeTys.push_back(MVT::i32);
632 }
633 NodeTys.push_back(MVT::i32);
634 break;
635 case MVT::f32:
636 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
637 ResultVals.push_back(DAG.getNode(ISD::BIT_CONVERT, MVT::f32,
638 Chain.getValue(0)));
639 NodeTys.push_back(MVT::f32);
640 break;
641 case MVT::f64: {
642 SDOperand Lo = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
643 SDOperand Hi = DAG.getCopyFromReg(Lo, ARM::R1, MVT::i32, Lo.getValue(2));
644 ResultVals.push_back(DAG.getNode(ARMISD::FMDRR, MVT::f64, Lo, Hi));
645 NodeTys.push_back(MVT::f64);
646 break;
647 }
648 }
649
650 NodeTys.push_back(MVT::Other);
651
652 if (ResultVals.empty())
653 return Chain;
654
655 ResultVals.push_back(Chain);
656 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
657 ResultVals.size());
658 return Res.getValue(Op.ResNo);
659}
660
661static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
662 SDOperand Copy;
663 SDOperand Chain = Op.getOperand(0);
664 switch(Op.getNumOperands()) {
665 default:
666 assert(0 && "Do not know how to return this many arguments!");
667 abort();
668 case 1: {
669 SDOperand LR = DAG.getRegister(ARM::LR, MVT::i32);
670 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
671 }
672 case 3:
673 Op = Op.getOperand(1);
674 if (Op.getValueType() == MVT::f32) {
675 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
676 } else if (Op.getValueType() == MVT::f64) {
Chris Lattner65a33232007-10-18 06:17:07 +0000677 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
678 // available.
679 Op = DAG.getNode(ARMISD::FMRRD, DAG.getVTList(MVT::i32, MVT::i32), &Op,1);
680 SDOperand Sign = DAG.getConstant(0, MVT::i32);
681 return DAG.getNode(ISD::RET, MVT::Other, Chain, Op, Sign,
682 Op.getValue(1), Sign);
Evan Chenga8e29892007-01-19 07:51:42 +0000683 }
684 Copy = DAG.getCopyToReg(Chain, ARM::R0, Op, SDOperand());
685 if (DAG.getMachineFunction().liveout_empty())
686 DAG.getMachineFunction().addLiveOut(ARM::R0);
687 break;
688 case 5:
689 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
690 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
691 // If we haven't noted the R0+R1 are live out, do so now.
692 if (DAG.getMachineFunction().liveout_empty()) {
693 DAG.getMachineFunction().addLiveOut(ARM::R0);
694 DAG.getMachineFunction().addLiveOut(ARM::R1);
695 }
696 break;
697 }
698
699 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
700 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
701}
702
703// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
704// their target countpart wrapped in the ARMISD::Wrapper node. Suppose N is
705// one of the above mentioned nodes. It has to be wrapped because otherwise
706// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
707// be used to form addressing mode. These wrapped nodes will be selected
Evan Cheng9f6636f2007-03-19 07:48:02 +0000708// into MOVi.
Evan Chenga8e29892007-01-19 07:51:42 +0000709static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
710 MVT::ValueType PtrVT = Op.getValueType();
711 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
712 SDOperand Res;
713 if (CP->isMachineConstantPoolEntry())
714 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
715 CP->getAlignment());
716 else
717 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
718 CP->getAlignment());
719 return DAG.getNode(ARMISD::Wrapper, MVT::i32, Res);
720}
721
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000722// Lower ISD::GlobalTLSAddress using the "general dynamic" model
723SDOperand
724ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
725 SelectionDAG &DAG) {
726 MVT::ValueType PtrVT = getPointerTy();
727 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
728 ARMConstantPoolValue *CPV =
729 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
730 PCAdj, "tlsgd", true);
731 SDOperand Argument = DAG.getTargetConstantPool(CPV, PtrVT, 2);
732 Argument = DAG.getNode(ARMISD::Wrapper, MVT::i32, Argument);
733 Argument = DAG.getLoad(PtrVT, DAG.getEntryNode(), Argument, NULL, 0);
734 SDOperand Chain = Argument.getValue(1);
735
736 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
737 Argument = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Argument, PICLabel);
738
739 // call __tls_get_addr.
740 ArgListTy Args;
741 ArgListEntry Entry;
742 Entry.Node = Argument;
743 Entry.Ty = (const Type *) Type::Int32Ty;
744 Args.push_back(Entry);
745 std::pair<SDOperand, SDOperand> CallResult =
746 LowerCallTo(Chain, (const Type *) Type::Int32Ty, false, false,
747 CallingConv::C, false,
748 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG);
749 return CallResult.first;
750}
751
752// Lower ISD::GlobalTLSAddress using the "initial exec" or
753// "local exec" model.
754SDOperand
755ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
756 SelectionDAG &DAG) {
757 GlobalValue *GV = GA->getGlobal();
758 SDOperand Offset;
759 SDOperand Chain = DAG.getEntryNode();
760 MVT::ValueType PtrVT = getPointerTy();
761 // Get the Thread Pointer
762 SDOperand ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, PtrVT);
763
764 if (GV->isDeclaration()){
765 // initial exec model
766 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
767 ARMConstantPoolValue *CPV =
768 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
769 PCAdj, "gottpoff", true);
770 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 2);
771 Offset = DAG.getNode(ARMISD::Wrapper, MVT::i32, Offset);
772 Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0);
773 Chain = Offset.getValue(1);
774
775 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
776 Offset = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Offset, PICLabel);
777
778 Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0);
779 } else {
780 // local exec model
781 ARMConstantPoolValue *CPV =
782 new ARMConstantPoolValue(GV, ARMCP::CPValue, "tpoff");
783 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 2);
784 Offset = DAG.getNode(ARMISD::Wrapper, MVT::i32, Offset);
785 Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0);
786 }
787
788 // The address of the thread local variable is the add of the thread
789 // pointer with the offset of the variable.
790 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
791}
792
793SDOperand
794ARMTargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
795 // TODO: implement the "local dynamic" model
796 assert(Subtarget->isTargetELF() &&
797 "TLS not implemented for non-ELF targets");
798 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
799 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
800 // otherwise use the "Local Exec" TLS Model
801 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
802 return LowerToTLSGeneralDynamicModel(GA, DAG);
803 else
804 return LowerToTLSExecModels(GA, DAG);
805}
806
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000807SDOperand ARMTargetLowering::LowerGlobalAddressELF(SDOperand Op,
808 SelectionDAG &DAG) {
809 MVT::ValueType PtrVT = getPointerTy();
810 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
811 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
812 if (RelocM == Reloc::PIC_) {
Lauro Ramos Venancio5d3d44a2007-05-14 23:20:21 +0000813 bool UseGOTOFF = GV->hasInternalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000814 ARMConstantPoolValue *CPV =
815 new ARMConstantPoolValue(GV, ARMCP::CPValue, UseGOTOFF ? "GOTOFF":"GOT");
816 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
817 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
818 SDOperand Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
819 SDOperand Chain = Result.getValue(1);
820 SDOperand GOT = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PtrVT);
821 Result = DAG.getNode(ISD::ADD, PtrVT, Result, GOT);
822 if (!UseGOTOFF)
823 Result = DAG.getLoad(PtrVT, Chain, Result, NULL, 0);
824 return Result;
825 } else {
826 SDOperand CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 2);
827 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
828 return DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
829 }
830}
831
Evan Chenga8e29892007-01-19 07:51:42 +0000832/// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol
Evan Cheng97c9bb52007-05-04 00:26:58 +0000833/// even in non-static mode.
834static bool GVIsIndirectSymbol(GlobalValue *GV, Reloc::Model RelocM) {
835 return RelocM != Reloc::Static &&
836 (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
Gabor Greifa99be512007-07-05 17:07:56 +0000837 (GV->isDeclaration() && !GV->hasNotBeenReadFromBitcode()));
Evan Chenga8e29892007-01-19 07:51:42 +0000838}
839
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000840SDOperand ARMTargetLowering::LowerGlobalAddressDarwin(SDOperand Op,
841 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +0000842 MVT::ValueType PtrVT = getPointerTy();
843 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
844 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng97c9bb52007-05-04 00:26:58 +0000845 bool IsIndirect = GVIsIndirectSymbol(GV, RelocM);
Evan Chenga8e29892007-01-19 07:51:42 +0000846 SDOperand CPAddr;
847 if (RelocM == Reloc::Static)
848 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 2);
849 else {
850 unsigned PCAdj = (RelocM != Reloc::PIC_)
851 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Chengc60e76d2007-01-30 20:37:08 +0000852 ARMCP::ARMCPKind Kind = IsIndirect ? ARMCP::CPNonLazyPtr
853 : ARMCP::CPValue;
Evan Chenga8e29892007-01-19 07:51:42 +0000854 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
Evan Chengc60e76d2007-01-30 20:37:08 +0000855 Kind, PCAdj);
Evan Chenga8e29892007-01-19 07:51:42 +0000856 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
857 }
858 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
859
860 SDOperand Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
861 SDOperand Chain = Result.getValue(1);
862
863 if (RelocM == Reloc::PIC_) {
864 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
865 Result = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Result, PICLabel);
866 }
867 if (IsIndirect)
868 Result = DAG.getLoad(PtrVT, Chain, Result, NULL, 0);
869
870 return Result;
871}
872
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000873SDOperand ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDOperand Op,
874 SelectionDAG &DAG){
875 assert(Subtarget->isTargetELF() &&
876 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
877 MVT::ValueType PtrVT = getPointerTy();
878 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
879 ARMConstantPoolValue *CPV = new ARMConstantPoolValue("_GLOBAL_OFFSET_TABLE_",
880 ARMPCLabelIndex,
881 ARMCP::CPValue, PCAdj);
882 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
883 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
884 SDOperand Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
885 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
886 return DAG.getNode(ARMISD::PIC_ADD, PtrVT, Result, PICLabel);
887}
888
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000889static SDOperand LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
890 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
891 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
892 switch (IntNo) {
893 default: return SDOperand(); // Don't custom lower most intrinsics.
894 case Intrinsic::arm_thread_pointer:
895 return DAG.getNode(ARMISD::THREAD_POINTER, PtrVT);
896 }
897}
898
Evan Chenga8e29892007-01-19 07:51:42 +0000899static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
900 unsigned VarArgsFrameIndex) {
901 // vastart just stores the address of the VarArgsFrameIndex slot into the
902 // memory location argument.
903 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
904 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
905 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
906 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), SV->getValue(),
907 SV->getOffset());
908}
909
910static SDOperand LowerFORMAL_ARGUMENT(SDOperand Op, SelectionDAG &DAG,
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000911 unsigned *vRegs, unsigned ArgNo,
Evan Chenga8e29892007-01-19 07:51:42 +0000912 unsigned &NumGPRs, unsigned &ArgOffset) {
913 MachineFunction &MF = DAG.getMachineFunction();
914 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
915 SDOperand Root = Op.getOperand(0);
916 std::vector<SDOperand> ArgValues;
917 SSARegMap *RegMap = MF.getSSARegMap();
918
919 static const unsigned GPRArgRegs[] = {
920 ARM::R0, ARM::R1, ARM::R2, ARM::R3
921 };
922
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000923 unsigned ObjSize;
924 unsigned ObjGPRs;
925 unsigned GPRPad;
926 unsigned StackPad;
927 unsigned Flags = Op.getConstantOperandVal(ArgNo + 3);
928 HowToPassArgument(ObjectVT, NumGPRs, ArgOffset, ObjGPRs,
929 ObjSize, GPRPad, StackPad, Flags);
930 NumGPRs += GPRPad;
931 ArgOffset += StackPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000932
933 SDOperand ArgValue;
934 if (ObjGPRs == 1) {
935 unsigned VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
936 MF.addLiveIn(GPRArgRegs[NumGPRs], VReg);
937 vRegs[NumGPRs] = VReg;
938 ArgValue = DAG.getCopyFromReg(Root, VReg, MVT::i32);
939 if (ObjectVT == MVT::f32)
940 ArgValue = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, ArgValue);
941 } else if (ObjGPRs == 2) {
942 unsigned VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
943 MF.addLiveIn(GPRArgRegs[NumGPRs], VReg);
944 vRegs[NumGPRs] = VReg;
945 ArgValue = DAG.getCopyFromReg(Root, VReg, MVT::i32);
946
947 VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
948 MF.addLiveIn(GPRArgRegs[NumGPRs+1], VReg);
949 vRegs[NumGPRs+1] = VReg;
950 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, VReg, MVT::i32);
951
Chris Lattner27a6c732007-11-24 07:07:01 +0000952 assert(ObjectVT != MVT::i64 && "i64 should already be lowered");
953 ArgValue = DAG.getNode(ARMISD::FMDRR, MVT::f64, ArgValue, ArgValue2);
Evan Chenga8e29892007-01-19 07:51:42 +0000954 }
955 NumGPRs += ObjGPRs;
956
957 if (ObjSize) {
958 // If the argument is actually used, emit a load from the right stack
959 // slot.
960 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
961 MachineFrameInfo *MFI = MF.getFrameInfo();
962 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
963 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
964 if (ObjGPRs == 0)
965 ArgValue = DAG.getLoad(ObjectVT, Root, FIN, NULL, 0);
966 else {
Chris Lattner27a6c732007-11-24 07:07:01 +0000967 SDOperand ArgValue2 = DAG.getLoad(MVT::i32, Root, FIN, NULL, 0);
968 assert(ObjectVT != MVT::i64 && "i64 should already be lowered");
969 ArgValue = DAG.getNode(ARMISD::FMDRR, MVT::f64, ArgValue, ArgValue2);
Evan Chenga8e29892007-01-19 07:51:42 +0000970 }
971 } else {
972 // Don't emit a dead load.
973 ArgValue = DAG.getNode(ISD::UNDEF, ObjectVT);
974 }
975
976 ArgOffset += ObjSize; // Move on to the next argument.
977 }
978
979 return ArgValue;
980}
981
982SDOperand
983ARMTargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
984 std::vector<SDOperand> ArgValues;
985 SDOperand Root = Op.getOperand(0);
986 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
987 unsigned NumGPRs = 0; // GPRs used for parameter passing.
988 unsigned VRegs[4];
989
990 unsigned NumArgs = Op.Val->getNumValues()-1;
991 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo)
992 ArgValues.push_back(LowerFORMAL_ARGUMENT(Op, DAG, VRegs, ArgNo,
993 NumGPRs, ArgOffset));
994
995 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
996 if (isVarArg) {
997 static const unsigned GPRArgRegs[] = {
998 ARM::R0, ARM::R1, ARM::R2, ARM::R3
999 };
1000
1001 MachineFunction &MF = DAG.getMachineFunction();
1002 SSARegMap *RegMap = MF.getSSARegMap();
1003 MachineFrameInfo *MFI = MF.getFrameInfo();
1004 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001005 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1006 unsigned VARegSize = (4 - NumGPRs) * 4;
1007 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Evan Chenga8e29892007-01-19 07:51:42 +00001008 if (VARegSaveSize) {
1009 // If this function is vararg, store any remaining integer argument regs
1010 // to their spots on the stack so that they may be loaded by deferencing
1011 // the result of va_next.
1012 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001013 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1014 VARegSaveSize - VARegSize);
Evan Chenga8e29892007-01-19 07:51:42 +00001015 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
1016
1017 SmallVector<SDOperand, 4> MemOps;
1018 for (; NumGPRs < 4; ++NumGPRs) {
1019 unsigned VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
1020 MF.addLiveIn(GPRArgRegs[NumGPRs], VReg);
1021 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
1022 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1023 MemOps.push_back(Store);
1024 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1025 DAG.getConstant(4, getPointerTy()));
1026 }
1027 if (!MemOps.empty())
1028 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1029 &MemOps[0], MemOps.size());
1030 } else
1031 // This will point to the next argument passed via stack.
1032 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1033 }
1034
1035 ArgValues.push_back(Root);
1036
1037 // Return the new list of results.
1038 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
1039 Op.Val->value_end());
1040 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
1041}
1042
1043/// isFloatingPointZero - Return true if this is +0.0.
1044static bool isFloatingPointZero(SDOperand Op) {
1045 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001046 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001047 else if (ISD::isEXTLoad(Op.Val) || ISD::isNON_EXTLoad(Op.Val)) {
1048 // Maybe this has already been legalized into the constant pool?
1049 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
1050 SDOperand WrapperOp = Op.getOperand(1).getOperand(0);
1051 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1052 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001053 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001054 }
1055 }
1056 return false;
1057}
1058
Evan Cheng9a2ef952007-02-02 01:53:26 +00001059static bool isLegalCmpImmediate(unsigned C, bool isThumb) {
Evan Chenga8e29892007-01-19 07:51:42 +00001060 return ( isThumb && (C & ~255U) == 0) ||
1061 (!isThumb && ARM_AM::getSOImmVal(C) != -1);
1062}
1063
1064/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1065/// the given operands.
1066static SDOperand getARMCmp(SDOperand LHS, SDOperand RHS, ISD::CondCode CC,
1067 SDOperand &ARMCC, SelectionDAG &DAG, bool isThumb) {
1068 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.Val)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001069 unsigned C = RHSC->getValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001070 if (!isLegalCmpImmediate(C, isThumb)) {
1071 // Constant does not fit, try adjusting it by one?
1072 switch (CC) {
1073 default: break;
1074 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001075 case ISD::SETGE:
Evan Chenga8e29892007-01-19 07:51:42 +00001076 if (isLegalCmpImmediate(C-1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001077 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
1078 RHS = DAG.getConstant(C-1, MVT::i32);
1079 }
1080 break;
1081 case ISD::SETULT:
1082 case ISD::SETUGE:
1083 if (C > 0 && isLegalCmpImmediate(C-1, isThumb)) {
1084 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Evan Chenga8e29892007-01-19 07:51:42 +00001085 RHS = DAG.getConstant(C-1, MVT::i32);
1086 }
1087 break;
1088 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001089 case ISD::SETGT:
Evan Chenga8e29892007-01-19 07:51:42 +00001090 if (isLegalCmpImmediate(C+1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001091 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
1092 RHS = DAG.getConstant(C+1, MVT::i32);
1093 }
1094 break;
1095 case ISD::SETULE:
1096 case ISD::SETUGT:
1097 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb)) {
1098 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Evan Chenga8e29892007-01-19 07:51:42 +00001099 RHS = DAG.getConstant(C+1, MVT::i32);
1100 }
1101 break;
1102 }
1103 }
1104 }
1105
1106 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001107 ARMISD::NodeType CompareType;
1108 switch (CondCode) {
1109 default:
1110 CompareType = ARMISD::CMP;
1111 break;
1112 case ARMCC::EQ:
1113 case ARMCC::NE:
1114 case ARMCC::MI:
1115 case ARMCC::PL:
1116 // Uses only N and Z Flags
1117 CompareType = ARMISD::CMPNZ;
1118 break;
1119 }
Evan Chenga8e29892007-01-19 07:51:42 +00001120 ARMCC = DAG.getConstant(CondCode, MVT::i32);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001121 return DAG.getNode(CompareType, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001122}
1123
1124/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
1125static SDOperand getVFPCmp(SDOperand LHS, SDOperand RHS, SelectionDAG &DAG) {
1126 SDOperand Cmp;
1127 if (!isFloatingPointZero(RHS))
1128 Cmp = DAG.getNode(ARMISD::CMPFP, MVT::Flag, LHS, RHS);
1129 else
1130 Cmp = DAG.getNode(ARMISD::CMPFPw0, MVT::Flag, LHS);
1131 return DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp);
1132}
1133
1134static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG,
1135 const ARMSubtarget *ST) {
1136 MVT::ValueType VT = Op.getValueType();
1137 SDOperand LHS = Op.getOperand(0);
1138 SDOperand RHS = Op.getOperand(1);
1139 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1140 SDOperand TrueVal = Op.getOperand(2);
1141 SDOperand FalseVal = Op.getOperand(3);
1142
1143 if (LHS.getValueType() == MVT::i32) {
1144 SDOperand ARMCC;
Evan Cheng0e1d3792007-07-05 07:18:20 +00001145 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001146 SDOperand Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb());
Evan Cheng0e1d3792007-07-05 07:18:20 +00001147 return DAG.getNode(ARMISD::CMOV, VT, FalseVal, TrueVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001148 }
1149
1150 ARMCC::CondCodes CondCode, CondCode2;
1151 if (FPCCToARMCC(CC, CondCode, CondCode2))
1152 std::swap(TrueVal, FalseVal);
1153
1154 SDOperand ARMCC = DAG.getConstant(CondCode, MVT::i32);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001155 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001156 SDOperand Cmp = getVFPCmp(LHS, RHS, DAG);
1157 SDOperand Result = DAG.getNode(ARMISD::CMOV, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001158 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001159 if (CondCode2 != ARMCC::AL) {
1160 SDOperand ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
1161 // FIXME: Needs another CMP because flag can have but one use.
1162 SDOperand Cmp2 = getVFPCmp(LHS, RHS, DAG);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001163 Result = DAG.getNode(ARMISD::CMOV, VT, Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001164 }
1165 return Result;
1166}
1167
1168static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG,
1169 const ARMSubtarget *ST) {
1170 SDOperand Chain = Op.getOperand(0);
1171 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1172 SDOperand LHS = Op.getOperand(2);
1173 SDOperand RHS = Op.getOperand(3);
1174 SDOperand Dest = Op.getOperand(4);
1175
1176 if (LHS.getValueType() == MVT::i32) {
1177 SDOperand ARMCC;
Evan Cheng0e1d3792007-07-05 07:18:20 +00001178 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001179 SDOperand Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb());
Evan Cheng0e1d3792007-07-05 07:18:20 +00001180 return DAG.getNode(ARMISD::BRCOND, MVT::Other, Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001181 }
1182
1183 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
1184 ARMCC::CondCodes CondCode, CondCode2;
1185 if (FPCCToARMCC(CC, CondCode, CondCode2))
1186 // Swap the LHS/RHS of the comparison if needed.
1187 std::swap(LHS, RHS);
1188
1189 SDOperand Cmp = getVFPCmp(LHS, RHS, DAG);
1190 SDOperand ARMCC = DAG.getConstant(CondCode, MVT::i32);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001191 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001192 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001193 SDOperand Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
1194 SDOperand Res = DAG.getNode(ARMISD::BRCOND, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001195 if (CondCode2 != ARMCC::AL) {
1196 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001197 SDOperand Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
1198 Res = DAG.getNode(ARMISD::BRCOND, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001199 }
1200 return Res;
1201}
1202
1203SDOperand ARMTargetLowering::LowerBR_JT(SDOperand Op, SelectionDAG &DAG) {
1204 SDOperand Chain = Op.getOperand(0);
1205 SDOperand Table = Op.getOperand(1);
1206 SDOperand Index = Op.getOperand(2);
1207
1208 MVT::ValueType PTy = getPointerTy();
1209 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1210 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
1211 SDOperand UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
1212 SDOperand JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
1213 Table = DAG.getNode(ARMISD::WrapperJT, MVT::i32, JTI, UId);
1214 Index = DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(4, PTy));
1215 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
1216 bool isPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Evan Chenge2446c62007-06-26 18:31:22 +00001217 Addr = DAG.getLoad(isPIC ? (MVT::ValueType)MVT::i32 : PTy,
1218 Chain, Addr, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001219 Chain = Addr.getValue(1);
1220 if (isPIC)
1221 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Table);
1222 return DAG.getNode(ARMISD::BR_JT, MVT::Other, Chain, Addr, JTI, UId);
1223}
1224
1225static SDOperand LowerFP_TO_INT(SDOperand Op, SelectionDAG &DAG) {
1226 unsigned Opc =
1227 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
1228 Op = DAG.getNode(Opc, MVT::f32, Op.getOperand(0));
1229 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
1230}
1231
1232static SDOperand LowerINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
1233 MVT::ValueType VT = Op.getValueType();
1234 unsigned Opc =
1235 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1236
1237 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Op.getOperand(0));
1238 return DAG.getNode(Opc, VT, Op);
1239}
1240
1241static SDOperand LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
1242 // Implement fcopysign with a fabs and a conditional fneg.
1243 SDOperand Tmp0 = Op.getOperand(0);
1244 SDOperand Tmp1 = Op.getOperand(1);
1245 MVT::ValueType VT = Op.getValueType();
1246 MVT::ValueType SrcVT = Tmp1.getValueType();
1247 SDOperand AbsVal = DAG.getNode(ISD::FABS, VT, Tmp0);
1248 SDOperand Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG);
1249 SDOperand ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001250 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1251 return DAG.getNode(ARMISD::CNEG, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001252}
1253
Rafael Espindola7b73a5d2007-10-19 14:35:17 +00001254SDOperand ARMTargetLowering::LowerMEMCPYInline(SDOperand Chain,
1255 SDOperand Dest,
1256 SDOperand Source,
1257 unsigned Size,
1258 unsigned Align,
1259 SelectionDAG &DAG) {
Evan Cheng4102eb52007-10-22 22:11:27 +00001260 // Do repeated 4-byte loads and stores. To be improved.
1261 assert((Align & 3) == 0 && "Expected 4-byte aligned addresses!");
1262 unsigned BytesLeft = Size & 3;
Rafael Espindola7b73a5d2007-10-19 14:35:17 +00001263 unsigned NumMemOps = Size >> 2;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001264 unsigned EmittedNumMemOps = 0;
1265 unsigned SrcOff = 0, DstOff = 0;
1266 MVT::ValueType VT = MVT::i32;
1267 unsigned VTSize = 4;
Evan Cheng4102eb52007-10-22 22:11:27 +00001268 unsigned i = 0;
Evan Chenge5e7ce42007-05-18 01:19:57 +00001269 const unsigned MAX_LOADS_IN_LDM = 6;
Evan Cheng4102eb52007-10-22 22:11:27 +00001270 SDOperand TFOps[MAX_LOADS_IN_LDM];
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001271 SDOperand Loads[MAX_LOADS_IN_LDM];
1272
Evan Cheng4102eb52007-10-22 22:11:27 +00001273 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
1274 // same number of stores. The loads and stores will get combined into
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001275 // ldm/stm later on.
Evan Cheng4102eb52007-10-22 22:11:27 +00001276 while (EmittedNumMemOps < NumMemOps) {
1277 for (i = 0;
1278 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001279 Loads[i] = DAG.getLoad(VT, Chain,
Evan Cheng4102eb52007-10-22 22:11:27 +00001280 DAG.getNode(ISD::ADD, MVT::i32, Source,
1281 DAG.getConstant(SrcOff, MVT::i32)),
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001282 NULL, 0);
Evan Cheng4102eb52007-10-22 22:11:27 +00001283 TFOps[i] = Loads[i].getValue(1);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001284 SrcOff += VTSize;
1285 }
Evan Cheng4102eb52007-10-22 22:11:27 +00001286 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001287
Evan Cheng4102eb52007-10-22 22:11:27 +00001288 for (i = 0;
1289 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
1290 TFOps[i] = DAG.getStore(Chain, Loads[i],
1291 DAG.getNode(ISD::ADD, MVT::i32, Dest,
1292 DAG.getConstant(DstOff, MVT::i32)),
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001293 NULL, 0);
1294 DstOff += VTSize;
1295 }
Evan Cheng4102eb52007-10-22 22:11:27 +00001296 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &TFOps[0], i);
1297
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001298 EmittedNumMemOps += i;
1299 }
1300
Evan Cheng4102eb52007-10-22 22:11:27 +00001301 if (BytesLeft == 0)
1302 return Chain;
1303
1304 // Issue loads / stores for the trailing (1 - 3) bytes.
1305 unsigned BytesLeftSave = BytesLeft;
1306 i = 0;
1307 while (BytesLeft) {
1308 if (BytesLeft >= 2) {
1309 VT = MVT::i16;
1310 VTSize = 2;
1311 } else {
1312 VT = MVT::i8;
1313 VTSize = 1;
1314 }
1315
1316 Loads[i] = DAG.getLoad(VT, Chain,
1317 DAG.getNode(ISD::ADD, MVT::i32, Source,
1318 DAG.getConstant(SrcOff, MVT::i32)),
1319 NULL, 0);
1320 TFOps[i] = Loads[i].getValue(1);
1321 ++i;
1322 SrcOff += VTSize;
1323 BytesLeft -= VTSize;
1324 }
1325 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &TFOps[0], i);
1326
1327 i = 0;
1328 BytesLeft = BytesLeftSave;
1329 while (BytesLeft) {
1330 if (BytesLeft >= 2) {
1331 VT = MVT::i16;
1332 VTSize = 2;
1333 } else {
1334 VT = MVT::i8;
1335 VTSize = 1;
1336 }
1337
1338 TFOps[i] = DAG.getStore(Chain, Loads[i],
1339 DAG.getNode(ISD::ADD, MVT::i32, Dest,
1340 DAG.getConstant(DstOff, MVT::i32)),
1341 NULL, 0);
1342 ++i;
1343 DstOff += VTSize;
1344 BytesLeft -= VTSize;
1345 }
1346 return DAG.getNode(ISD::TokenFactor, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001347}
1348
Chris Lattner27a6c732007-11-24 07:07:01 +00001349static SDNode *ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
1350 // Turn f64->i64 into FMRRD.
1351 assert(N->getValueType(0) == MVT::i64 &&
1352 N->getOperand(0).getValueType() == MVT::f64);
1353
1354 SDOperand Op = N->getOperand(0);
1355 SDOperand Cvt = DAG.getNode(ARMISD::FMRRD, DAG.getVTList(MVT::i32, MVT::i32),
1356 &Op, 1);
1357
1358 // Merge the pieces into a single i64 value.
1359 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Cvt, Cvt.getValue(1)).Val;
1360}
1361
1362static SDNode *ExpandSRx(SDNode *N, SelectionDAG &DAG, const ARMSubtarget *ST) {
1363 assert(N->getValueType(0) == MVT::i64 &&
1364 (N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
1365 "Unknown shift to lower!");
1366
1367 // We only lower SRA, SRL of 1 here, all others use generic lowering.
1368 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
1369 cast<ConstantSDNode>(N->getOperand(1))->getValue() != 1)
1370 return 0;
1371
1372 // If we are in thumb mode, we don't have RRX.
1373 if (ST->isThumb()) return 0;
1374
1375 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
1376 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(0),
1377 DAG.getConstant(0, MVT::i32));
1378 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(0),
1379 DAG.getConstant(1, MVT::i32));
1380
1381 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
1382 // captures the result into a carry flag.
1383 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
1384 Hi = DAG.getNode(Opc, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
1385
1386 // The low part is an ARMISD::RRX operand, which shifts the carry in.
1387 Lo = DAG.getNode(ARMISD::RRX, MVT::i32, Lo, Hi.getValue(1));
1388
1389 // Merge the pieces into a single i64 value.
1390 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi).Val;
1391}
1392
1393
Evan Chenga8e29892007-01-19 07:51:42 +00001394SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1395 switch (Op.getOpcode()) {
1396 default: assert(0 && "Don't know how to custom lower this!"); abort();
1397 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001398 case ISD::GlobalAddress:
1399 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
1400 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001401 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00001402 case ISD::CALL: return LowerCALL(Op, DAG);
1403 case ISD::RET: return LowerRET(Op, DAG);
1404 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
1405 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
1406 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
1407 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
1408 case ISD::SINT_TO_FP:
1409 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
1410 case ISD::FP_TO_SINT:
1411 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
1412 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00001413 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00001414 case ISD::RETURNADDR: break;
1415 case ISD::FRAMEADDR: break;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001416 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Rafael Espindolae0703c82007-10-31 14:39:58 +00001417 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001418 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00001419
1420
1421 // FIXME: Remove these when LegalizeDAGTypes lands.
1422 case ISD::BIT_CONVERT: return SDOperand(ExpandBIT_CONVERT(Op.Val, DAG), 0);
1423 case ISD::SRL:
1424 case ISD::SRA: return SDOperand(ExpandSRx(Op.Val, DAG,Subtarget),0);
Evan Chenga8e29892007-01-19 07:51:42 +00001425 }
Nate Begemanbcc5f362007-01-29 22:58:52 +00001426 return SDOperand();
Evan Chenga8e29892007-01-19 07:51:42 +00001427}
1428
Chris Lattner27a6c732007-11-24 07:07:01 +00001429
1430/// ExpandOperationResult - Provide custom lowering hooks for expanding
1431/// operations.
1432SDNode *ARMTargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
1433 switch (N->getOpcode()) {
1434 default: assert(0 && "Don't know how to custom expand this!"); abort();
1435 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(N, DAG);
1436 case ISD::SRL:
1437 case ISD::SRA: return ExpandSRx(N, DAG, Subtarget);
1438 }
1439}
1440
1441
Evan Chenga8e29892007-01-19 07:51:42 +00001442//===----------------------------------------------------------------------===//
1443// ARM Scheduler Hooks
1444//===----------------------------------------------------------------------===//
1445
1446MachineBasicBlock *
1447ARMTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1448 MachineBasicBlock *BB) {
1449 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1450 switch (MI->getOpcode()) {
1451 default: assert(false && "Unexpected instr type to insert");
1452 case ARM::tMOVCCr: {
1453 // To "insert" a SELECT_CC instruction, we actually have to insert the
1454 // diamond control-flow pattern. The incoming instruction knows the
1455 // destination vreg to set, the condition code register to branch on, the
1456 // true/false values to select between, and a branch opcode to use.
1457 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1458 ilist<MachineBasicBlock>::iterator It = BB;
1459 ++It;
1460
1461 // thisMBB:
1462 // ...
1463 // TrueVal = ...
1464 // cmpTY ccX, r1, r2
1465 // bCC copy1MBB
1466 // fallthrough --> copy0MBB
1467 MachineBasicBlock *thisMBB = BB;
1468 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1469 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1470 BuildMI(BB, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00001471 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001472 MachineFunction *F = BB->getParent();
1473 F->getBasicBlockList().insert(It, copy0MBB);
1474 F->getBasicBlockList().insert(It, sinkMBB);
1475 // Update machine-CFG edges by first adding all successors of the current
1476 // block to the new block which will contain the Phi node for the select.
1477 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1478 e = BB->succ_end(); i != e; ++i)
1479 sinkMBB->addSuccessor(*i);
1480 // Next, remove all successors of the current block, and add the true
1481 // and fallthrough blocks as its successors.
1482 while(!BB->succ_empty())
1483 BB->removeSuccessor(BB->succ_begin());
1484 BB->addSuccessor(copy0MBB);
1485 BB->addSuccessor(sinkMBB);
1486
1487 // copy0MBB:
1488 // %FalseValue = ...
1489 // # fallthrough to sinkMBB
1490 BB = copy0MBB;
1491
1492 // Update machine-CFG edges
1493 BB->addSuccessor(sinkMBB);
1494
1495 // sinkMBB:
1496 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1497 // ...
1498 BB = sinkMBB;
1499 BuildMI(BB, TII->get(ARM::PHI), MI->getOperand(0).getReg())
1500 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1501 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1502
1503 delete MI; // The pseudo instruction is gone now.
1504 return BB;
1505 }
1506 }
1507}
1508
1509//===----------------------------------------------------------------------===//
1510// ARM Optimization Hooks
1511//===----------------------------------------------------------------------===//
1512
Evan Chengb01fad62007-03-12 23:30:29 +00001513/// isLegalAddressImmediate - Return true if the integer value can be used
1514/// as the offset of the target addressing mode for load / store of the
1515/// given type.
Chris Lattner37caf8c2007-04-09 23:33:39 +00001516static bool isLegalAddressImmediate(int64_t V, MVT::ValueType VT,
1517 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00001518 if (V == 0)
1519 return true;
1520
Evan Chengb01fad62007-03-12 23:30:29 +00001521 if (Subtarget->isThumb()) {
1522 if (V < 0)
1523 return false;
1524
1525 unsigned Scale = 1;
1526 switch (VT) {
1527 default: return false;
1528 case MVT::i1:
1529 case MVT::i8:
1530 // Scale == 1;
1531 break;
1532 case MVT::i16:
1533 // Scale == 2;
1534 Scale = 2;
1535 break;
1536 case MVT::i32:
1537 // Scale == 4;
1538 Scale = 4;
1539 break;
1540 }
1541
1542 if ((V & (Scale - 1)) != 0)
1543 return false;
1544 V /= Scale;
1545 return V == V & ((1LL << 5) - 1);
1546 }
1547
1548 if (V < 0)
1549 V = - V;
1550 switch (VT) {
1551 default: return false;
1552 case MVT::i1:
1553 case MVT::i8:
1554 case MVT::i32:
1555 // +- imm12
1556 return V == V & ((1LL << 12) - 1);
1557 case MVT::i16:
1558 // +- imm8
1559 return V == V & ((1LL << 8) - 1);
1560 case MVT::f32:
1561 case MVT::f64:
1562 if (!Subtarget->hasVFP2())
1563 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00001564 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00001565 return false;
1566 V >>= 2;
1567 return V == V & ((1LL << 8) - 1);
1568 }
Evan Chenga8e29892007-01-19 07:51:42 +00001569}
1570
Chris Lattner37caf8c2007-04-09 23:33:39 +00001571/// isLegalAddressingMode - Return true if the addressing mode represented
1572/// by AM is legal for this target, for a load/store of the specified type.
1573bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
1574 const Type *Ty) const {
1575 if (!isLegalAddressImmediate(AM.BaseOffs, getValueType(Ty), Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00001576 return false;
Chris Lattner37caf8c2007-04-09 23:33:39 +00001577
1578 // Can never fold addr of global into load/store.
1579 if (AM.BaseGV)
1580 return false;
1581
1582 switch (AM.Scale) {
1583 case 0: // no scale reg, must be "r+i" or "r", or "i".
1584 break;
1585 case 1:
1586 if (Subtarget->isThumb())
1587 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00001588 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00001589 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00001590 // ARM doesn't support any R+R*scale+imm addr modes.
1591 if (AM.BaseOffs)
1592 return false;
1593
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001594 int Scale = AM.Scale;
Chris Lattner37caf8c2007-04-09 23:33:39 +00001595 switch (getValueType(Ty)) {
1596 default: return false;
1597 case MVT::i1:
1598 case MVT::i8:
1599 case MVT::i32:
1600 case MVT::i64:
1601 // This assumes i64 is legalized to a pair of i32. If not (i.e.
1602 // ldrd / strd are used, then its address mode is same as i16.
1603 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001604 if (Scale < 0) Scale = -Scale;
1605 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001606 return true;
1607 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00001608 return isPowerOf2_32(Scale & ~1);
Chris Lattner37caf8c2007-04-09 23:33:39 +00001609 case MVT::i16:
1610 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001611 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001612 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00001613 return false;
1614
Chris Lattner37caf8c2007-04-09 23:33:39 +00001615 case MVT::isVoid:
1616 // Note, we allow "void" uses (basically, uses that aren't loads or
1617 // stores), because arm allows folding a scale into many arithmetic
1618 // operations. This should be made more precise and revisited later.
Chris Lattnerb2c594f2007-04-03 00:13:57 +00001619
Chris Lattner37caf8c2007-04-09 23:33:39 +00001620 // Allow r << imm, but the imm has to be a multiple of two.
1621 if (AM.Scale & 1) return false;
1622 return isPowerOf2_32(AM.Scale);
1623 }
1624 break;
Evan Chengb01fad62007-03-12 23:30:29 +00001625 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00001626 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00001627}
1628
Chris Lattner37caf8c2007-04-09 23:33:39 +00001629
Evan Chenga8e29892007-01-19 07:51:42 +00001630static bool getIndexedAddressParts(SDNode *Ptr, MVT::ValueType VT,
1631 bool isSEXTLoad, SDOperand &Base,
1632 SDOperand &Offset, bool &isInc,
1633 SelectionDAG &DAG) {
1634 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
1635 return false;
1636
1637 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
1638 // AddressingMode 3
1639 Base = Ptr->getOperand(0);
1640 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
1641 int RHSC = (int)RHS->getValue();
1642 if (RHSC < 0 && RHSC > -256) {
1643 isInc = false;
1644 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1645 return true;
1646 }
1647 }
1648 isInc = (Ptr->getOpcode() == ISD::ADD);
1649 Offset = Ptr->getOperand(1);
1650 return true;
1651 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
1652 // AddressingMode 2
1653 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
1654 int RHSC = (int)RHS->getValue();
1655 if (RHSC < 0 && RHSC > -0x1000) {
1656 isInc = false;
1657 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1658 Base = Ptr->getOperand(0);
1659 return true;
1660 }
1661 }
1662
1663 if (Ptr->getOpcode() == ISD::ADD) {
1664 isInc = true;
1665 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
1666 if (ShOpcVal != ARM_AM::no_shift) {
1667 Base = Ptr->getOperand(1);
1668 Offset = Ptr->getOperand(0);
1669 } else {
1670 Base = Ptr->getOperand(0);
1671 Offset = Ptr->getOperand(1);
1672 }
1673 return true;
1674 }
1675
1676 isInc = (Ptr->getOpcode() == ISD::ADD);
1677 Base = Ptr->getOperand(0);
1678 Offset = Ptr->getOperand(1);
1679 return true;
1680 }
1681
1682 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
1683 return false;
1684}
1685
1686/// getPreIndexedAddressParts - returns true by value, base pointer and
1687/// offset pointer and addressing mode by reference if the node's address
1688/// can be legally represented as pre-indexed load / store address.
1689bool
1690ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDOperand &Base,
1691 SDOperand &Offset,
1692 ISD::MemIndexedMode &AM,
1693 SelectionDAG &DAG) {
1694 if (Subtarget->isThumb())
1695 return false;
1696
1697 MVT::ValueType VT;
1698 SDOperand Ptr;
1699 bool isSEXTLoad = false;
1700 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1701 Ptr = LD->getBasePtr();
1702 VT = LD->getLoadedVT();
1703 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1704 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
1705 Ptr = ST->getBasePtr();
1706 VT = ST->getStoredVT();
1707 } else
1708 return false;
1709
1710 bool isInc;
1711 bool isLegal = getIndexedAddressParts(Ptr.Val, VT, isSEXTLoad, Base, Offset,
1712 isInc, DAG);
1713 if (isLegal) {
1714 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
1715 return true;
1716 }
1717 return false;
1718}
1719
1720/// getPostIndexedAddressParts - returns true by value, base pointer and
1721/// offset pointer and addressing mode by reference if this node can be
1722/// combined with a load / store to form a post-indexed load / store.
1723bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
1724 SDOperand &Base,
1725 SDOperand &Offset,
1726 ISD::MemIndexedMode &AM,
1727 SelectionDAG &DAG) {
1728 if (Subtarget->isThumb())
1729 return false;
1730
1731 MVT::ValueType VT;
1732 SDOperand Ptr;
1733 bool isSEXTLoad = false;
1734 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1735 VT = LD->getLoadedVT();
1736 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1737 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
1738 VT = ST->getStoredVT();
1739 } else
1740 return false;
1741
1742 bool isInc;
1743 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
1744 isInc, DAG);
1745 if (isLegal) {
1746 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
1747 return true;
1748 }
1749 return false;
1750}
1751
1752void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
1753 uint64_t Mask,
1754 uint64_t &KnownZero,
1755 uint64_t &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001756 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00001757 unsigned Depth) const {
1758 KnownZero = 0;
1759 KnownOne = 0;
1760 switch (Op.getOpcode()) {
1761 default: break;
1762 case ARMISD::CMOV: {
1763 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00001764 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00001765 if (KnownZero == 0 && KnownOne == 0) return;
1766
1767 uint64_t KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00001768 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
1769 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00001770 KnownZero &= KnownZeroRHS;
1771 KnownOne &= KnownOneRHS;
1772 return;
1773 }
1774 }
1775}
1776
1777//===----------------------------------------------------------------------===//
1778// ARM Inline Assembly Support
1779//===----------------------------------------------------------------------===//
1780
1781/// getConstraintType - Given a constraint letter, return the type of
1782/// constraint it is for this target.
1783ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00001784ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
1785 if (Constraint.size() == 1) {
1786 switch (Constraint[0]) {
1787 default: break;
1788 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001789 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00001790 }
Evan Chenga8e29892007-01-19 07:51:42 +00001791 }
Chris Lattner4234f572007-03-25 02:14:49 +00001792 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00001793}
1794
1795std::pair<unsigned, const TargetRegisterClass*>
1796ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
1797 MVT::ValueType VT) const {
1798 if (Constraint.size() == 1) {
1799 // GCC RS6000 Constraint Letters
1800 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001801 case 'l':
1802 // FIXME: in thumb mode, 'l' is only low-regs.
1803 // FALL THROUGH.
1804 case 'r':
1805 return std::make_pair(0U, ARM::GPRRegisterClass);
1806 case 'w':
1807 if (VT == MVT::f32)
1808 return std::make_pair(0U, ARM::SPRRegisterClass);
Evan Cheng0a7baa22007-04-04 00:06:07 +00001809 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001810 return std::make_pair(0U, ARM::DPRRegisterClass);
1811 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001812 }
1813 }
1814 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1815}
1816
1817std::vector<unsigned> ARMTargetLowering::
1818getRegClassForInlineAsmConstraint(const std::string &Constraint,
1819 MVT::ValueType VT) const {
1820 if (Constraint.size() != 1)
1821 return std::vector<unsigned>();
1822
1823 switch (Constraint[0]) { // GCC ARM Constraint Letters
1824 default: break;
1825 case 'l':
1826 case 'r':
1827 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
1828 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
1829 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
1830 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001831 case 'w':
1832 if (VT == MVT::f32)
1833 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
1834 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
1835 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
1836 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
1837 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
1838 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
1839 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
1840 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
1841 if (VT == MVT::f64)
1842 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
1843 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
1844 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
1845 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
1846 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001847 }
1848
1849 return std::vector<unsigned>();
1850}