blob: 307d4643036eb6e0934def688102d945aaa27122 [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
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"
Chris Lattner84bc5422007-12-31 04:13:23 +000027#include "llvm/GlobalValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000028#include "llvm/CodeGen/MachineBasicBlock.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chenga8e29892007-01-19 07:51:42 +000033#include "llvm/CodeGen/SelectionDAG.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000034#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000035#include "llvm/ADT/VectorExtras.h"
Evan Chengb01fad62007-03-12 23:30:29 +000036#include "llvm/Support/MathExtras.h"
Evan Chenga8e29892007-01-19 07:51:42 +000037using namespace llvm;
38
39ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
40 : TargetLowering(TM), ARMPCLabelIndex(0) {
41 Subtarget = &TM.getSubtarget<ARMSubtarget>();
42
Evan Chengb1df8f22007-04-27 08:15:43 +000043 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +000044 // Uses VFP for Thumb libfuncs if available.
45 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
46 // Single-precision floating-point arithmetic.
47 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
48 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
49 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
50 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +000051
Evan Chengb1df8f22007-04-27 08:15:43 +000052 // Double-precision floating-point arithmetic.
53 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
54 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
55 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
56 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +000057
Evan Chengb1df8f22007-04-27 08:15:43 +000058 // Single-precision comparisons.
59 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
60 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
61 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
62 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
63 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
64 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
65 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
66 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +000067
Evan Chengb1df8f22007-04-27 08:15:43 +000068 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
69 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
70 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
71 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
72 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
73 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
74 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
75 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +000076
Evan Chengb1df8f22007-04-27 08:15:43 +000077 // Double-precision comparisons.
78 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
79 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
80 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
81 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
82 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
83 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
84 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
85 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +000086
Evan Chengb1df8f22007-04-27 08:15:43 +000087 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
88 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
89 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
90 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
91 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
92 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
93 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
94 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +000095
Evan Chengb1df8f22007-04-27 08:15:43 +000096 // Floating-point to integer conversions.
97 // i64 conversions are done via library routines even when generating VFP
98 // instructions, so use the same ones.
99 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
100 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
101 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
102 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000103
Evan Chengb1df8f22007-04-27 08:15:43 +0000104 // Conversions between floating types.
105 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
106 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
107
108 // Integer to floating-point conversions.
109 // i64 conversions are done via library routines even when generating VFP
110 // instructions, so use the same ones.
111 // FIXME: There appears to be some naming inconsistency in ARM libgcc: e.g.
112 // __floatunsidf vs. __floatunssidfvfp.
113 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
114 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
115 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
116 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
117 }
Evan Chenga8e29892007-01-19 07:51:42 +0000118 }
119
120 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
Evan Chengb6ab2542007-01-31 08:40:13 +0000121 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000122 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
123 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Chris Lattnerddf89562008-01-17 19:59:44 +0000124
125 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000126 }
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.
Evan Cheng03294662008-10-14 21:26:46 +0000130 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000131
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000132 // ARM does not have i1 sign extending load.
Evan Cheng03294662008-10-14 21:26:46 +0000133 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000134
Evan Chenga8e29892007-01-19 07:51:42 +0000135 // ARM supports all 4 flavors of integer indexed load / store.
136 for (unsigned im = (unsigned)ISD::PRE_INC;
137 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
138 setIndexedLoadAction(im, MVT::i1, Legal);
139 setIndexedLoadAction(im, MVT::i8, Legal);
140 setIndexedLoadAction(im, MVT::i16, Legal);
141 setIndexedLoadAction(im, MVT::i32, Legal);
142 setIndexedStoreAction(im, MVT::i1, Legal);
143 setIndexedStoreAction(im, MVT::i8, Legal);
144 setIndexedStoreAction(im, MVT::i16, Legal);
145 setIndexedStoreAction(im, MVT::i32, Legal);
146 }
147
148 // i64 operation support.
149 if (Subtarget->isThumb()) {
150 setOperationAction(ISD::MUL, MVT::i64, Expand);
151 setOperationAction(ISD::MULHU, MVT::i32, Expand);
152 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000153 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
154 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000155 } else {
Dan Gohman525178c2007-10-08 18:33:35 +0000156 setOperationAction(ISD::MUL, MVT::i64, Expand);
157 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000158 if (!Subtarget->hasV6Ops())
Dan Gohman525178c2007-10-08 18:33:35 +0000159 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000160 }
161 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
162 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
163 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
164 setOperationAction(ISD::SRL, MVT::i64, Custom);
165 setOperationAction(ISD::SRA, MVT::i64, Custom);
166
167 // ARM does not have ROTL.
168 setOperationAction(ISD::ROTL, MVT::i32, Expand);
169 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
170 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
Evan Chengb0636152007-02-01 23:34:03 +0000171 if (!Subtarget->hasV5TOps() || Subtarget->isThumb())
Evan Chenga8e29892007-01-19 07:51:42 +0000172 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
173
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000174 // Only ARMv6 has BSWAP.
175 if (!Subtarget->hasV6Ops())
Chris Lattner1719e132007-03-20 02:25:53 +0000176 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000177
Evan Chenga8e29892007-01-19 07:51:42 +0000178 // These are expanded into libcalls.
179 setOperationAction(ISD::SDIV, MVT::i32, Expand);
180 setOperationAction(ISD::UDIV, MVT::i32, Expand);
181 setOperationAction(ISD::SREM, MVT::i32, Expand);
182 setOperationAction(ISD::UREM, MVT::i32, Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000183 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
184 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000185
186 // Support label based line numbers.
Dan Gohman7f460202008-06-30 20:59:49 +0000187 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000188 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000189
190 setOperationAction(ISD::RET, MVT::Other, Custom);
191 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
192 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000193 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000194 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000195
Evan Chenga8e29892007-01-19 07:51:42 +0000196 // Use the default implementation.
Nate Begeman48a65512008-02-04 21:44:06 +0000197 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000198 setOperationAction(ISD::VAARG , MVT::Other, Expand);
199 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
200 setOperationAction(ISD::VAEND , MVT::Other, Expand);
201 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
202 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
203 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000204 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000205
206 if (!Subtarget->hasV6Ops()) {
207 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
208 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
209 }
210 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
211
Evan Chengb6ab2542007-01-31 08:40:13 +0000212 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb())
Evan Chengc7c77292008-11-04 19:57:48 +0000213 // Turn f64->i64 into FMRRD, i64 -> f64 to FMDRR iff target supports vfp2.
Evan Chenga8e29892007-01-19 07:51:42 +0000214 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000215
216 // We want to custom lower some of our intrinsics.
217 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
218
Evan Chenga8e29892007-01-19 07:51:42 +0000219 setOperationAction(ISD::SETCC , MVT::i32, Expand);
220 setOperationAction(ISD::SETCC , MVT::f32, Expand);
221 setOperationAction(ISD::SETCC , MVT::f64, Expand);
222 setOperationAction(ISD::SELECT , MVT::i32, Expand);
223 setOperationAction(ISD::SELECT , MVT::f32, Expand);
224 setOperationAction(ISD::SELECT , MVT::f64, Expand);
225 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
226 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
227 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
228
229 setOperationAction(ISD::BRCOND , MVT::Other, Expand);
230 setOperationAction(ISD::BR_CC , MVT::i32, Custom);
231 setOperationAction(ISD::BR_CC , MVT::f32, Custom);
232 setOperationAction(ISD::BR_CC , MVT::f64, Custom);
233 setOperationAction(ISD::BR_JT , MVT::Other, Custom);
234
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000235 // We don't support sin/cos/fmod/copysign/pow
Evan Chenga8e29892007-01-19 07:51:42 +0000236 setOperationAction(ISD::FSIN , MVT::f64, Expand);
237 setOperationAction(ISD::FSIN , MVT::f32, Expand);
238 setOperationAction(ISD::FCOS , MVT::f32, Expand);
239 setOperationAction(ISD::FCOS , MVT::f64, Expand);
240 setOperationAction(ISD::FREM , MVT::f64, Expand);
241 setOperationAction(ISD::FREM , MVT::f32, Expand);
Evan Cheng110cf482008-04-01 01:50:16 +0000242 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) {
243 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
244 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
245 }
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000246 setOperationAction(ISD::FPOW , MVT::f64, Expand);
247 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000248
249 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
Evan Cheng110cf482008-04-01 01:50:16 +0000250 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) {
251 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
252 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
253 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
254 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
255 }
Evan Chenga8e29892007-01-19 07:51:42 +0000256
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000257 // We have target-specific dag combine patterns for the following nodes:
258 // ARMISD::FMRRD - No need to call setTargetDAGCombine
259
Evan Chenga8e29892007-01-19 07:51:42 +0000260 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Chenga8e29892007-01-19 07:51:42 +0000261 setSchedulingPreference(SchedulingForRegPressure);
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000262 setIfCvtBlockSizeLimit(Subtarget->isThumb() ? 0 : 10);
Evan Cheng97e604e2007-06-19 23:55:02 +0000263 setIfCvtDupBlockSizeLimit(Subtarget->isThumb() ? 0 : 2);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000264
265 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Evan Chenga8e29892007-01-19 07:51:42 +0000266}
267
268
269const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
270 switch (Opcode) {
271 default: return 0;
272 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000273 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
274 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000275 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000276 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
277 case ARMISD::tCALL: return "ARMISD::tCALL";
278 case ARMISD::BRCOND: return "ARMISD::BRCOND";
279 case ARMISD::BR_JT: return "ARMISD::BR_JT";
280 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
281 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
282 case ARMISD::CMP: return "ARMISD::CMP";
Lauro Ramos Venancio99966632007-04-02 01:30:03 +0000283 case ARMISD::CMPNZ: return "ARMISD::CMPNZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000284 case ARMISD::CMPFP: return "ARMISD::CMPFP";
285 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
286 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
287 case ARMISD::CMOV: return "ARMISD::CMOV";
288 case ARMISD::CNEG: return "ARMISD::CNEG";
289
290 case ARMISD::FTOSI: return "ARMISD::FTOSI";
291 case ARMISD::FTOUI: return "ARMISD::FTOUI";
292 case ARMISD::SITOF: return "ARMISD::SITOF";
293 case ARMISD::UITOF: return "ARMISD::UITOF";
Evan Chenga8e29892007-01-19 07:51:42 +0000294
295 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
296 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
297 case ARMISD::RRX: return "ARMISD::RRX";
298
299 case ARMISD::FMRRD: return "ARMISD::FMRRD";
300 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000301
302 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Evan Chenga8e29892007-01-19 07:51:42 +0000303 }
304}
305
306//===----------------------------------------------------------------------===//
307// Lowering Code
308//===----------------------------------------------------------------------===//
309
310
311/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
312static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
313 switch (CC) {
314 default: assert(0 && "Unknown condition code!");
315 case ISD::SETNE: return ARMCC::NE;
316 case ISD::SETEQ: return ARMCC::EQ;
317 case ISD::SETGT: return ARMCC::GT;
318 case ISD::SETGE: return ARMCC::GE;
319 case ISD::SETLT: return ARMCC::LT;
320 case ISD::SETLE: return ARMCC::LE;
321 case ISD::SETUGT: return ARMCC::HI;
322 case ISD::SETUGE: return ARMCC::HS;
323 case ISD::SETULT: return ARMCC::LO;
324 case ISD::SETULE: return ARMCC::LS;
325 }
326}
327
328/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. It
329/// returns true if the operands should be inverted to form the proper
330/// comparison.
331static bool FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
332 ARMCC::CondCodes &CondCode2) {
333 bool Invert = false;
334 CondCode2 = ARMCC::AL;
335 switch (CC) {
336 default: assert(0 && "Unknown FP condition!");
337 case ISD::SETEQ:
338 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
339 case ISD::SETGT:
340 case ISD::SETOGT: CondCode = ARMCC::GT; break;
341 case ISD::SETGE:
342 case ISD::SETOGE: CondCode = ARMCC::GE; break;
343 case ISD::SETOLT: CondCode = ARMCC::MI; break;
344 case ISD::SETOLE: CondCode = ARMCC::GT; Invert = true; break;
345 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
346 case ISD::SETO: CondCode = ARMCC::VC; break;
347 case ISD::SETUO: CondCode = ARMCC::VS; break;
348 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
349 case ISD::SETUGT: CondCode = ARMCC::HI; break;
350 case ISD::SETUGE: CondCode = ARMCC::PL; break;
351 case ISD::SETLT:
352 case ISD::SETULT: CondCode = ARMCC::LT; break;
353 case ISD::SETLE:
354 case ISD::SETULE: CondCode = ARMCC::LE; break;
355 case ISD::SETNE:
356 case ISD::SETUNE: CondCode = ARMCC::NE; break;
357 }
358 return Invert;
359}
360
361static void
Duncan Sands83ec4b62008-06-06 12:08:01 +0000362HowToPassArgument(MVT ObjectVT, unsigned NumGPRs,
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000363 unsigned StackOffset, unsigned &NeededGPRs,
364 unsigned &NeededStackSize, unsigned &GPRPad,
Duncan Sands276dcbd2008-03-21 09:14:45 +0000365 unsigned &StackPad, ISD::ArgFlagsTy Flags) {
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000366 NeededStackSize = 0;
367 NeededGPRs = 0;
368 StackPad = 0;
369 GPRPad = 0;
Duncan Sands276dcbd2008-03-21 09:14:45 +0000370 unsigned align = Flags.getOrigAlign();
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000371 GPRPad = NumGPRs % ((align + 3)/4);
372 StackPad = StackOffset % align;
373 unsigned firstGPR = NumGPRs + GPRPad;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000374 switch (ObjectVT.getSimpleVT()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000375 default: assert(0 && "Unhandled argument type!");
376 case MVT::i32:
377 case MVT::f32:
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000378 if (firstGPR < 4)
379 NeededGPRs = 1;
Evan Chenga8e29892007-01-19 07:51:42 +0000380 else
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000381 NeededStackSize = 4;
Evan Chenga8e29892007-01-19 07:51:42 +0000382 break;
383 case MVT::i64:
384 case MVT::f64:
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000385 if (firstGPR < 3)
386 NeededGPRs = 2;
387 else if (firstGPR == 3) {
388 NeededGPRs = 1;
389 NeededStackSize = 4;
Evan Chenga8e29892007-01-19 07:51:42 +0000390 } else
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000391 NeededStackSize = 8;
Evan Chenga8e29892007-01-19 07:51:42 +0000392 }
393}
394
Evan Chengfc403422007-02-03 08:53:01 +0000395/// LowerCALL - Lowering a ISD::CALL node into a callseq_start <-
396/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
397/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000398SDValue ARMTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Dan Gohman095cc292008-09-13 01:54:27 +0000399 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
400 MVT RetVT = TheCall->getRetValType(0);
401 SDValue Chain = TheCall->getChain();
Chris Lattner4469c532009-01-25 23:08:00 +0000402 assert((TheCall->getCallingConv() == CallingConv::C ||
403 TheCall->getCallingConv() == CallingConv::Fast) &&
404 "unknown calling convention");
Dan Gohman095cc292008-09-13 01:54:27 +0000405 SDValue Callee = TheCall->getCallee();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000406 unsigned NumOps = TheCall->getNumArgs();
407 DebugLoc dl = TheCall->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +0000408 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
409 unsigned NumGPRs = 0; // GPRs used for parameter passing.
410
411 // Count how many bytes are to be pushed on the stack.
412 unsigned NumBytes = 0;
413
414 // Add up all the space actually used.
415 for (unsigned i = 0; i < NumOps; ++i) {
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000416 unsigned ObjSize;
417 unsigned ObjGPRs;
418 unsigned StackPad;
419 unsigned GPRPad;
Dan Gohman095cc292008-09-13 01:54:27 +0000420 MVT ObjectVT = TheCall->getArg(i).getValueType();
421 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000422 HowToPassArgument(ObjectVT, NumGPRs, NumBytes, ObjGPRs, ObjSize,
423 GPRPad, StackPad, Flags);
424 NumBytes += ObjSize + StackPad;
425 NumGPRs += ObjGPRs + GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000426 }
427
428 // Adjust the stack pointer for the new arguments...
429 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000430 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000431
Dan Gohman475871a2008-07-27 21:46:04 +0000432 SDValue StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000433
434 static const unsigned GPRArgRegs[] = {
435 ARM::R0, ARM::R1, ARM::R2, ARM::R3
436 };
437
438 NumGPRs = 0;
Dan Gohman475871a2008-07-27 21:46:04 +0000439 std::vector<std::pair<unsigned, SDValue> > RegsToPass;
440 std::vector<SDValue> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000441 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohman095cc292008-09-13 01:54:27 +0000442 SDValue Arg = TheCall->getArg(i);
443 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000444 MVT ArgVT = Arg.getValueType();
Evan Chenga8e29892007-01-19 07:51:42 +0000445
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000446 unsigned ObjSize;
447 unsigned ObjGPRs;
448 unsigned GPRPad;
449 unsigned StackPad;
450 HowToPassArgument(ArgVT, NumGPRs, ArgOffset, ObjGPRs,
451 ObjSize, GPRPad, StackPad, Flags);
452 NumGPRs += GPRPad;
453 ArgOffset += StackPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000454 if (ObjGPRs > 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000455 switch (ArgVT.getSimpleVT()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000456 default: assert(0 && "Unexpected ValueType for argument!");
457 case MVT::i32:
458 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Arg));
459 break;
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000460 case MVT::f32:
Evan Chenga8e29892007-01-19 07:51:42 +0000461 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs],
Dale Johannesen33c960f2009-02-04 20:06:27 +0000462 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Arg)));
Evan Chenga8e29892007-01-19 07:51:42 +0000463 break;
464 case MVT::i64: {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000465 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Arg,
Evan Chenga8e29892007-01-19 07:51:42 +0000466 DAG.getConstant(0, getPointerTy()));
Dale Johannesen33c960f2009-02-04 20:06:27 +0000467 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Arg,
Evan Chenga8e29892007-01-19 07:51:42 +0000468 DAG.getConstant(1, getPointerTy()));
469 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Lo));
470 if (ObjGPRs == 2)
471 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1], Hi));
472 else {
Dan Gohman475871a2008-07-27 21:46:04 +0000473 SDValue PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType());
Dale Johannesen33c960f2009-02-04 20:06:27 +0000474 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
475 MemOpChains.push_back(DAG.getStore(Chain, dl, Hi, PtrOff, NULL, 0));
Evan Chenga8e29892007-01-19 07:51:42 +0000476 }
477 break;
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000478 }
Evan Chenga8e29892007-01-19 07:51:42 +0000479 case MVT::f64: {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000480 SDValue Cvt = DAG.getNode(ARMISD::FMRRD, dl,
Evan Chenga8e29892007-01-19 07:51:42 +0000481 DAG.getVTList(MVT::i32, MVT::i32),
482 &Arg, 1);
483 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Cvt));
484 if (ObjGPRs == 2)
485 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1],
486 Cvt.getValue(1)));
487 else {
Dan Gohman475871a2008-07-27 21:46:04 +0000488 SDValue PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType());
Dale Johannesen33c960f2009-02-04 20:06:27 +0000489 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
490 MemOpChains.push_back(DAG.getStore(Chain, dl, Cvt.getValue(1), PtrOff,
Evan Chenga8e29892007-01-19 07:51:42 +0000491 NULL, 0));
492 }
493 break;
494 }
495 }
496 } else {
497 assert(ObjSize != 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000498 SDValue PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Dale Johannesen33c960f2009-02-04 20:06:27 +0000499 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
500 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0));
Evan Chenga8e29892007-01-19 07:51:42 +0000501 }
502
503 NumGPRs += ObjGPRs;
504 ArgOffset += ObjSize;
505 }
506
507 if (!MemOpChains.empty())
Dale Johannesen33c960f2009-02-04 20:06:27 +0000508 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +0000509 &MemOpChains[0], MemOpChains.size());
510
511 // Build a sequence of copy-to-reg nodes chained together with token chain
512 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +0000513 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +0000514 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000515 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
516 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000517 InFlag = Chain.getValue(1);
518 }
519
Bill Wendling056292f2008-09-16 21:48:12 +0000520 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
521 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
522 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +0000523 bool isDirect = false;
524 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +0000525 bool isLocalARMFunc = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000526 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
527 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +0000528 isDirect = true;
Reid Spencer5cbf9852007-01-30 20:08:39 +0000529 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
Evan Chenga8e29892007-01-19 07:51:42 +0000530 GV->hasLinkOnceLinkage());
Evan Cheng970a4192007-01-19 19:28:01 +0000531 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +0000532 getTargetMachine().getRelocationModel() != Reloc::Static;
533 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +0000534 // ARM call to a local ARM function is predicable.
535 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +0000536 // tBX takes a register source operand.
537 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
538 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
539 ARMCP::CPStub, 4);
Dan Gohman475871a2008-07-27 21:46:04 +0000540 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 2);
Evan Chengc60e76d2007-01-30 20:37:08 +0000541 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000542 Callee = DAG.getLoad(getPointerTy(), dl,
543 DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000544 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000545 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
546 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +0000547 } else
548 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +0000549 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000550 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +0000551 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000552 getTargetMachine().getRelocationModel() != Reloc::Static;
553 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +0000554 // tBX takes a register source operand.
555 const char *Sym = S->getSymbol();
556 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
557 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(Sym, ARMPCLabelIndex,
558 ARMCP::CPStub, 4);
Dan Gohman475871a2008-07-27 21:46:04 +0000559 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 2);
Evan Chengc60e76d2007-01-30 20:37:08 +0000560 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000561 Callee = DAG.getLoad(getPointerTy(), dl,
562 DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000563 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000564 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
565 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +0000566 } else
Bill Wendling056292f2008-09-16 21:48:12 +0000567 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
Dale Johannesen33c960f2009-02-04 20:06:27 +0000584 Chain = DAG.getCopyToReg(Chain, dl, 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
Dan Gohman475871a2008-07-27 21:46:04 +0000589 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +0000590 Ops.push_back(Chain);
591 Ops.push_back(Callee);
592
593 // Add argument registers to the end of the list so that they are known live
594 // into the call.
595 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
596 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
597 RegsToPass[i].second.getValueType()));
598
Gabor Greifba36cb52008-08-28 21:40:38 +0000599 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +0000600 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +0000601 // Returns a chain and a flag for retval copy to use.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000602 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +0000603 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +0000604 InFlag = Chain.getValue(1);
605
Chris Lattnere563bbc2008-10-11 22:08:30 +0000606 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
607 DAG.getIntPtrConstant(0, true), InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000608 if (RetVT != MVT::Other)
609 InFlag = Chain.getValue(1);
610
Dan Gohman475871a2008-07-27 21:46:04 +0000611 std::vector<SDValue> ResultVals;
Evan Chenga8e29892007-01-19 07:51:42 +0000612
613 // If the call has results, copy the values out of the ret val registers.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000614 switch (RetVT.getSimpleVT()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000615 default: assert(0 && "Unexpected ret value!");
616 case MVT::Other:
617 break;
618 case MVT::i32:
Dale Johannesen33c960f2009-02-04 20:06:27 +0000619 Chain = DAG.getCopyFromReg(Chain, dl, ARM::R0,
620 MVT::i32, InFlag).getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +0000621 ResultVals.push_back(Chain.getValue(0));
Dan Gohman095cc292008-09-13 01:54:27 +0000622 if (TheCall->getNumRetVals() > 1 &&
623 TheCall->getRetValType(1) == MVT::i32) {
Evan Chenga8e29892007-01-19 07:51:42 +0000624 // Returns a i64 value.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000625 Chain = DAG.getCopyFromReg(Chain, dl, ARM::R1, MVT::i32,
Evan Chenga8e29892007-01-19 07:51:42 +0000626 Chain.getValue(2)).getValue(1);
627 ResultVals.push_back(Chain.getValue(0));
Evan Chenga8e29892007-01-19 07:51:42 +0000628 }
Evan Chenga8e29892007-01-19 07:51:42 +0000629 break;
630 case MVT::f32:
Dale Johannesen33c960f2009-02-04 20:06:27 +0000631 Chain = DAG.getCopyFromReg(Chain, dl, ARM::R0,
632 MVT::i32, InFlag).getValue(1);
633 ResultVals.push_back(DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32,
Evan Chenga8e29892007-01-19 07:51:42 +0000634 Chain.getValue(0)));
Evan Chenga8e29892007-01-19 07:51:42 +0000635 break;
636 case MVT::f64: {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000637 SDValue Lo = DAG.getCopyFromReg(Chain, dl, ARM::R0, MVT::i32, InFlag);
638 SDValue Hi = DAG.getCopyFromReg(Lo, dl, ARM::R1, MVT::i32, Lo.getValue(2));
639 ResultVals.push_back(DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi));
Evan Chenga8e29892007-01-19 07:51:42 +0000640 break;
641 }
642 }
643
Evan Chenga8e29892007-01-19 07:51:42 +0000644 if (ResultVals.empty())
645 return Chain;
646
647 ResultVals.push_back(Chain);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000648 SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
Gabor Greif99a6cb92008-08-26 22:36:50 +0000649 return Res.getValue(Op.getResNo());
Evan Chenga8e29892007-01-19 07:51:42 +0000650}
651
Dan Gohman475871a2008-07-27 21:46:04 +0000652static SDValue LowerRET(SDValue Op, SelectionDAG &DAG) {
653 SDValue Copy;
654 SDValue Chain = Op.getOperand(0);
Dale Johannesena05dca42009-02-04 23:02:30 +0000655 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +0000656 switch(Op.getNumOperands()) {
657 default:
658 assert(0 && "Do not know how to return this many arguments!");
659 abort();
660 case 1: {
Dan Gohman475871a2008-07-27 21:46:04 +0000661 SDValue LR = DAG.getRegister(ARM::LR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +0000662 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Evan Chenga8e29892007-01-19 07:51:42 +0000663 }
664 case 3:
665 Op = Op.getOperand(1);
666 if (Op.getValueType() == MVT::f32) {
Dale Johannesende064702009-02-06 21:50:26 +0000667 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Evan Chenga8e29892007-01-19 07:51:42 +0000668 } else if (Op.getValueType() == MVT::f64) {
Chris Lattner65a33232007-10-18 06:17:07 +0000669 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
670 // available.
Dale Johannesende064702009-02-06 21:50:26 +0000671 Op = DAG.getNode(ARMISD::FMRRD, dl,
672 DAG.getVTList(MVT::i32, MVT::i32), &Op,1);
Dan Gohman475871a2008-07-27 21:46:04 +0000673 SDValue Sign = DAG.getConstant(0, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +0000674 return DAG.getNode(ISD::RET, dl, MVT::Other, Chain, Op, Sign,
Chris Lattner65a33232007-10-18 06:17:07 +0000675 Op.getValue(1), Sign);
Evan Chenga8e29892007-01-19 07:51:42 +0000676 }
Dale Johannesena05dca42009-02-04 23:02:30 +0000677 Copy = DAG.getCopyToReg(Chain, dl, ARM::R0, Op, SDValue());
Chris Lattner84bc5422007-12-31 04:13:23 +0000678 if (DAG.getMachineFunction().getRegInfo().liveout_empty())
679 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R0);
Evan Chenga8e29892007-01-19 07:51:42 +0000680 break;
681 case 5:
Dale Johannesena05dca42009-02-04 23:02:30 +0000682 Copy = DAG.getCopyToReg(Chain, dl, ARM::R1, Op.getOperand(3), SDValue());
683 Copy = DAG.getCopyToReg(Copy, dl, ARM::R0, Op.getOperand(1),
684 Copy.getValue(1));
Evan Chenga8e29892007-01-19 07:51:42 +0000685 // If we haven't noted the R0+R1 are live out, do so now.
Chris Lattner84bc5422007-12-31 04:13:23 +0000686 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
687 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R0);
688 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R1);
Evan Chenga8e29892007-01-19 07:51:42 +0000689 }
690 break;
Chris Lattner78d60452008-07-11 20:53:00 +0000691 case 9: // i128 -> 4 regs
Dale Johannesena05dca42009-02-04 23:02:30 +0000692 Copy = DAG.getCopyToReg(Chain, dl, ARM::R3, Op.getOperand(7), SDValue());
693 Copy = DAG.getCopyToReg(Copy , dl, ARM::R2, Op.getOperand(5),
694 Copy.getValue(1));
695 Copy = DAG.getCopyToReg(Copy , dl, ARM::R1, Op.getOperand(3),
696 Copy.getValue(1));
697 Copy = DAG.getCopyToReg(Copy , dl, ARM::R0, Op.getOperand(1),
698 Copy.getValue(1));
Chris Lattner78d60452008-07-11 20:53:00 +0000699 // If we haven't noted the R0+R1 are live out, do so now.
700 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
701 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R0);
702 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R1);
703 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R2);
704 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R3);
705 }
706 break;
707
Evan Chenga8e29892007-01-19 07:51:42 +0000708 }
709
710 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
Dale Johannesende064702009-02-06 21:50:26 +0000711 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Copy, Copy.getValue(1));
Evan Chenga8e29892007-01-19 07:51:42 +0000712}
713
Bill Wendling056292f2008-09-16 21:48:12 +0000714// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
715// their target countpart wrapped in the ARMISD::Wrapper node. Suppose N is
716// one of the above mentioned nodes. It has to be wrapped because otherwise
717// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
718// be used to form addressing mode. These wrapped nodes will be selected
719// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +0000720static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000721 MVT PtrVT = Op.getValueType();
Evan Chenga8e29892007-01-19 07:51:42 +0000722 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +0000723 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +0000724 if (CP->isMachineConstantPoolEntry())
725 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
726 CP->getAlignment());
727 else
728 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
729 CP->getAlignment());
730 return DAG.getNode(ARMISD::Wrapper, MVT::i32, Res);
731}
732
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000733// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +0000734SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000735ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
736 SelectionDAG &DAG) {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000737 DebugLoc dl = GA->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000738 MVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000739 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
740 ARMConstantPoolValue *CPV =
741 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
742 PCAdj, "tlsgd", true);
Dan Gohman475871a2008-07-27 21:46:04 +0000743 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 2);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000744 Argument = DAG.getNode(ARMISD::Wrapper, MVT::i32, Argument);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000745 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000746 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000747
Dan Gohman475871a2008-07-27 21:46:04 +0000748 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000749 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000750
751 // call __tls_get_addr.
752 ArgListTy Args;
753 ArgListEntry Entry;
754 Entry.Node = Argument;
755 Entry.Ty = (const Type *) Type::Int32Ty;
756 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000757 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +0000758 std::pair<SDValue, SDValue> CallResult =
Dale Johannesen86098bd2008-09-26 19:31:26 +0000759 LowerCallTo(Chain, (const Type *) Type::Int32Ty, false, false, false, false,
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000760 CallingConv::C, false,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000761 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000762 return CallResult.first;
763}
764
765// Lower ISD::GlobalTLSAddress using the "initial exec" or
766// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +0000767SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000768ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
769 SelectionDAG &DAG) {
770 GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000771 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +0000772 SDValue Offset;
773 SDValue Chain = DAG.getEntryNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000774 MVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000775 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +0000776 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000777
778 if (GV->isDeclaration()){
779 // initial exec model
780 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
781 ARMConstantPoolValue *CPV =
782 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
783 PCAdj, "gottpoff", true);
784 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 2);
785 Offset = DAG.getNode(ARMISD::Wrapper, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000786 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000787 Chain = Offset.getValue(1);
788
Dan Gohman475871a2008-07-27 21:46:04 +0000789 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000790 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000791
Dale Johannesen33c960f2009-02-04 20:06:27 +0000792 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000793 } else {
794 // local exec model
795 ARMConstantPoolValue *CPV =
796 new ARMConstantPoolValue(GV, ARMCP::CPValue, "tpoff");
797 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 2);
798 Offset = DAG.getNode(ARMISD::Wrapper, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000799 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000800 }
801
802 // The address of the thread local variable is the add of the thread
803 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000804 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000805}
806
Dan Gohman475871a2008-07-27 21:46:04 +0000807SDValue
808ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000809 // TODO: implement the "local dynamic" model
810 assert(Subtarget->isTargetELF() &&
811 "TLS not implemented for non-ELF targets");
812 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
813 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
814 // otherwise use the "Local Exec" TLS Model
815 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
816 return LowerToTLSGeneralDynamicModel(GA, DAG);
817 else
818 return LowerToTLSExecModels(GA, DAG);
819}
820
Dan Gohman475871a2008-07-27 21:46:04 +0000821SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000822 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000823 MVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000824 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000825 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
826 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
827 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000828 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000829 ARMConstantPoolValue *CPV =
830 new ARMConstantPoolValue(GV, ARMCP::CPValue, UseGOTOFF ? "GOTOFF":"GOT");
Dan Gohman475871a2008-07-27 21:46:04 +0000831 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000832 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000833 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
834 CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000835 SDValue Chain = Result.getValue(1);
836 SDValue GOT = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000837 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000838 if (!UseGOTOFF)
Dale Johannesen33c960f2009-02-04 20:06:27 +0000839 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000840 return Result;
841 } else {
Dan Gohman475871a2008-07-27 21:46:04 +0000842 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 2);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000843 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000844 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000845 }
846}
847
Evan Chenga8e29892007-01-19 07:51:42 +0000848/// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol
Evan Cheng97c9bb52007-05-04 00:26:58 +0000849/// even in non-static mode.
850static bool GVIsIndirectSymbol(GlobalValue *GV, Reloc::Model RelocM) {
Evan Chengae94e592008-12-05 01:06:39 +0000851 // If symbol visibility is hidden, the extra load is not needed if
852 // the symbol is definitely defined in the current translation unit.
853 bool isDecl = GV->isDeclaration() && !GV->hasNotBeenReadFromBitcode();
854 if (GV->hasHiddenVisibility() && (!isDecl && !GV->hasCommonLinkage()))
855 return false;
856 return RelocM != Reloc::Static && (isDecl || GV->mayBeOverridden());
Evan Chenga8e29892007-01-19 07:51:42 +0000857}
858
Dan Gohman475871a2008-07-27 21:46:04 +0000859SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000860 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000861 MVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000862 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +0000863 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
864 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng97c9bb52007-05-04 00:26:58 +0000865 bool IsIndirect = GVIsIndirectSymbol(GV, RelocM);
Dan Gohman475871a2008-07-27 21:46:04 +0000866 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +0000867 if (RelocM == Reloc::Static)
868 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 2);
869 else {
870 unsigned PCAdj = (RelocM != Reloc::PIC_)
871 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Chengc60e76d2007-01-30 20:37:08 +0000872 ARMCP::ARMCPKind Kind = IsIndirect ? ARMCP::CPNonLazyPtr
873 : ARMCP::CPValue;
Evan Chenga8e29892007-01-19 07:51:42 +0000874 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
Evan Chengc60e76d2007-01-30 20:37:08 +0000875 Kind, PCAdj);
Evan Chenga8e29892007-01-19 07:51:42 +0000876 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
877 }
878 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
879
Dale Johannesen33c960f2009-02-04 20:06:27 +0000880 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000881 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +0000882
883 if (RelocM == Reloc::PIC_) {
Dan Gohman475871a2008-07-27 21:46:04 +0000884 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000885 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +0000886 }
887 if (IsIndirect)
Dale Johannesen33c960f2009-02-04 20:06:27 +0000888 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +0000889
890 return Result;
891}
892
Dan Gohman475871a2008-07-27 21:46:04 +0000893SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000894 SelectionDAG &DAG){
895 assert(Subtarget->isTargetELF() &&
896 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000897 MVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000898 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000899 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
900 ARMConstantPoolValue *CPV = new ARMConstantPoolValue("_GLOBAL_OFFSET_TABLE_",
901 ARMPCLabelIndex,
902 ARMCP::CPValue, PCAdj);
Dan Gohman475871a2008-07-27 21:46:04 +0000903 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000904 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000905 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000906 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000907 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000908}
909
Dan Gohman475871a2008-07-27 21:46:04 +0000910static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000911 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000912 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000913 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +0000914 default: return SDValue(); // Don't custom lower most intrinsics.
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000915 case Intrinsic::arm_thread_pointer:
916 return DAG.getNode(ARMISD::THREAD_POINTER, PtrVT);
917 }
918}
919
Dan Gohman475871a2008-07-27 21:46:04 +0000920static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +0000921 unsigned VarArgsFrameIndex) {
922 // vastart just stores the address of the VarArgsFrameIndex slot into the
923 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000924 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000925 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000926 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +0000927 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000928 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Chenga8e29892007-01-19 07:51:42 +0000929}
930
Dan Gohman475871a2008-07-27 21:46:04 +0000931static SDValue LowerFORMAL_ARGUMENT(SDValue Op, SelectionDAG &DAG,
Nate Begemanbf1caa92008-02-12 22:54:40 +0000932 unsigned ArgNo, unsigned &NumGPRs,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000933 unsigned &ArgOffset, DebugLoc dl) {
Evan Chenga8e29892007-01-19 07:51:42 +0000934 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000935 MVT ObjectVT = Op.getValue(ArgNo).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000936 SDValue Root = Op.getOperand(0);
Chris Lattner84bc5422007-12-31 04:13:23 +0000937 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Evan Chenga8e29892007-01-19 07:51:42 +0000938
939 static const unsigned GPRArgRegs[] = {
940 ARM::R0, ARM::R1, ARM::R2, ARM::R3
941 };
942
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000943 unsigned ObjSize;
944 unsigned ObjGPRs;
945 unsigned GPRPad;
946 unsigned StackPad;
Duncan Sands276dcbd2008-03-21 09:14:45 +0000947 ISD::ArgFlagsTy Flags =
948 cast<ARG_FLAGSSDNode>(Op.getOperand(ArgNo + 3))->getArgFlags();
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000949 HowToPassArgument(ObjectVT, NumGPRs, ArgOffset, ObjGPRs,
950 ObjSize, GPRPad, StackPad, Flags);
951 NumGPRs += GPRPad;
952 ArgOffset += StackPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000953
Dan Gohman475871a2008-07-27 21:46:04 +0000954 SDValue ArgValue;
Evan Chenga8e29892007-01-19 07:51:42 +0000955 if (ObjGPRs == 1) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000956 unsigned VReg = RegInfo.createVirtualRegister(&ARM::GPRRegClass);
957 RegInfo.addLiveIn(GPRArgRegs[NumGPRs], VReg);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000958 ArgValue = DAG.getCopyFromReg(Root, dl, VReg, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000959 if (ObjectVT == MVT::f32)
Dale Johannesen33c960f2009-02-04 20:06:27 +0000960 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, ArgValue);
Evan Chenga8e29892007-01-19 07:51:42 +0000961 } else if (ObjGPRs == 2) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000962 unsigned VReg = RegInfo.createVirtualRegister(&ARM::GPRRegClass);
963 RegInfo.addLiveIn(GPRArgRegs[NumGPRs], VReg);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000964 ArgValue = DAG.getCopyFromReg(Root, dl, VReg, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000965
Chris Lattner84bc5422007-12-31 04:13:23 +0000966 VReg = RegInfo.createVirtualRegister(&ARM::GPRRegClass);
967 RegInfo.addLiveIn(GPRArgRegs[NumGPRs+1], VReg);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000968 SDValue ArgValue2 = DAG.getCopyFromReg(Root, dl, VReg, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000969
Chris Lattner27a6c732007-11-24 07:07:01 +0000970 assert(ObjectVT != MVT::i64 && "i64 should already be lowered");
Dale Johannesen33c960f2009-02-04 20:06:27 +0000971 ArgValue = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, ArgValue, ArgValue2);
Evan Chenga8e29892007-01-19 07:51:42 +0000972 }
973 NumGPRs += ObjGPRs;
974
975 if (ObjSize) {
Chris Lattner9f72d1a2008-02-13 07:35:30 +0000976 MachineFrameInfo *MFI = MF.getFrameInfo();
977 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Dan Gohman475871a2008-07-27 21:46:04 +0000978 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
Chris Lattner9f72d1a2008-02-13 07:35:30 +0000979 if (ObjGPRs == 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +0000980 ArgValue = DAG.getLoad(ObjectVT, dl, Root, FIN, NULL, 0);
Chris Lattner9f72d1a2008-02-13 07:35:30 +0000981 else {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000982 SDValue ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN, NULL, 0);
Chris Lattner9f72d1a2008-02-13 07:35:30 +0000983 assert(ObjectVT != MVT::i64 && "i64 should already be lowered");
Dale Johannesen33c960f2009-02-04 20:06:27 +0000984 ArgValue = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, ArgValue, ArgValue2);
Evan Chenga8e29892007-01-19 07:51:42 +0000985 }
986
987 ArgOffset += ObjSize; // Move on to the next argument.
988 }
989
990 return ArgValue;
991}
992
Dan Gohman475871a2008-07-27 21:46:04 +0000993SDValue
994ARMTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
995 std::vector<SDValue> ArgValues;
996 SDValue Root = Op.getOperand(0);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000997 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +0000998 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
999 unsigned NumGPRs = 0; // GPRs used for parameter passing.
Evan Chenga8e29892007-01-19 07:51:42 +00001000
Gabor Greifba36cb52008-08-28 21:40:38 +00001001 unsigned NumArgs = Op.getNode()->getNumValues()-1;
Evan Chenga8e29892007-01-19 07:51:42 +00001002 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo)
Nate Begemanbf1caa92008-02-12 22:54:40 +00001003 ArgValues.push_back(LowerFORMAL_ARGUMENT(Op, DAG, ArgNo,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001004 NumGPRs, ArgOffset, dl));
Evan Chenga8e29892007-01-19 07:51:42 +00001005
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001006 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Evan Chenga8e29892007-01-19 07:51:42 +00001007 if (isVarArg) {
1008 static const unsigned GPRArgRegs[] = {
1009 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1010 };
1011
1012 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner84bc5422007-12-31 04:13:23 +00001013 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Evan Chenga8e29892007-01-19 07:51:42 +00001014 MachineFrameInfo *MFI = MF.getFrameInfo();
1015 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001016 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1017 unsigned VARegSize = (4 - NumGPRs) * 4;
1018 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Evan Chenga8e29892007-01-19 07:51:42 +00001019 if (VARegSaveSize) {
1020 // If this function is vararg, store any remaining integer argument regs
1021 // to their spots on the stack so that they may be loaded by deferencing
1022 // the result of va_next.
1023 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001024 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1025 VARegSaveSize - VARegSize);
Dan Gohman475871a2008-07-27 21:46:04 +00001026 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001027
Dan Gohman475871a2008-07-27 21:46:04 +00001028 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001029 for (; NumGPRs < 4; ++NumGPRs) {
Chris Lattner84bc5422007-12-31 04:13:23 +00001030 unsigned VReg = RegInfo.createVirtualRegister(&ARM::GPRRegClass);
1031 RegInfo.addLiveIn(GPRArgRegs[NumGPRs], VReg);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001032 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::i32);
1033 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001034 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001035 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001036 DAG.getConstant(4, getPointerTy()));
1037 }
1038 if (!MemOps.empty())
Dale Johannesen33c960f2009-02-04 20:06:27 +00001039 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001040 &MemOps[0], MemOps.size());
1041 } else
1042 // This will point to the next argument passed via stack.
1043 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1044 }
1045
1046 ArgValues.push_back(Root);
1047
1048 // Return the new list of results.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001049 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00001050 &ArgValues[0], ArgValues.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001051}
1052
1053/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001054static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001055 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001056 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001057 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001058 // Maybe this has already been legalized into the constant pool?
1059 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001060 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001061 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1062 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001063 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001064 }
1065 }
1066 return false;
1067}
1068
Evan Cheng9a2ef952007-02-02 01:53:26 +00001069static bool isLegalCmpImmediate(unsigned C, bool isThumb) {
Evan Chenga8e29892007-01-19 07:51:42 +00001070 return ( isThumb && (C & ~255U) == 0) ||
1071 (!isThumb && ARM_AM::getSOImmVal(C) != -1);
1072}
1073
1074/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1075/// the given operands.
Dan Gohman475871a2008-07-27 21:46:04 +00001076static SDValue getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Dale Johannesende064702009-02-06 21:50:26 +00001077 SDValue &ARMCC, SelectionDAG &DAG, bool isThumb,
1078 DebugLoc dl) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001079 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001080 unsigned C = RHSC->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001081 if (!isLegalCmpImmediate(C, isThumb)) {
1082 // Constant does not fit, try adjusting it by one?
1083 switch (CC) {
1084 default: break;
1085 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001086 case ISD::SETGE:
Evan Chenga8e29892007-01-19 07:51:42 +00001087 if (isLegalCmpImmediate(C-1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001088 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
1089 RHS = DAG.getConstant(C-1, MVT::i32);
1090 }
1091 break;
1092 case ISD::SETULT:
1093 case ISD::SETUGE:
1094 if (C > 0 && isLegalCmpImmediate(C-1, isThumb)) {
1095 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Evan Chenga8e29892007-01-19 07:51:42 +00001096 RHS = DAG.getConstant(C-1, MVT::i32);
1097 }
1098 break;
1099 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001100 case ISD::SETGT:
Evan Chenga8e29892007-01-19 07:51:42 +00001101 if (isLegalCmpImmediate(C+1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001102 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
1103 RHS = DAG.getConstant(C+1, MVT::i32);
1104 }
1105 break;
1106 case ISD::SETULE:
1107 case ISD::SETUGT:
1108 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb)) {
1109 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Evan Chenga8e29892007-01-19 07:51:42 +00001110 RHS = DAG.getConstant(C+1, MVT::i32);
1111 }
1112 break;
1113 }
1114 }
1115 }
1116
1117 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001118 ARMISD::NodeType CompareType;
1119 switch (CondCode) {
1120 default:
1121 CompareType = ARMISD::CMP;
1122 break;
1123 case ARMCC::EQ:
1124 case ARMCC::NE:
1125 case ARMCC::MI:
1126 case ARMCC::PL:
1127 // Uses only N and Z Flags
1128 CompareType = ARMISD::CMPNZ;
1129 break;
1130 }
Evan Chenga8e29892007-01-19 07:51:42 +00001131 ARMCC = DAG.getConstant(CondCode, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001132 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001133}
1134
1135/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Dale Johannesende064702009-02-06 21:50:26 +00001136static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
1137 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001138 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001139 if (!isFloatingPointZero(RHS))
Dale Johannesende064702009-02-06 21:50:26 +00001140 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001141 else
Dale Johannesende064702009-02-06 21:50:26 +00001142 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1143 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001144}
1145
Dan Gohman475871a2008-07-27 21:46:04 +00001146static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00001147 const ARMSubtarget *ST) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001148 MVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001149 SDValue LHS = Op.getOperand(0);
1150 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001151 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001152 SDValue TrueVal = Op.getOperand(2);
1153 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001154 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001155
1156 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001157 SDValue ARMCC;
1158 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001159 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb(), dl);
1160 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001161 }
1162
1163 ARMCC::CondCodes CondCode, CondCode2;
1164 if (FPCCToARMCC(CC, CondCode, CondCode2))
1165 std::swap(TrueVal, FalseVal);
1166
Dan Gohman475871a2008-07-27 21:46:04 +00001167 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1168 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001169 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1170 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001171 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001172 if (CondCode2 != ARMCC::AL) {
Dan Gohman475871a2008-07-27 21:46:04 +00001173 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001174 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00001175 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
1176 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
1177 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001178 }
1179 return Result;
1180}
1181
Dan Gohman475871a2008-07-27 21:46:04 +00001182static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00001183 const ARMSubtarget *ST) {
Dan Gohman475871a2008-07-27 21:46:04 +00001184 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001185 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001186 SDValue LHS = Op.getOperand(2);
1187 SDValue RHS = Op.getOperand(3);
1188 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00001189 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001190
1191 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001192 SDValue ARMCC;
1193 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001194 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb(), dl);
1195 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
1196 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001197 }
1198
1199 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
1200 ARMCC::CondCodes CondCode, CondCode2;
1201 if (FPCCToARMCC(CC, CondCode, CondCode2))
1202 // Swap the LHS/RHS of the comparison if needed.
1203 std::swap(LHS, RHS);
1204
Dale Johannesende064702009-02-06 21:50:26 +00001205 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Dan Gohman475871a2008-07-27 21:46:04 +00001206 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1207 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001208 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001209 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00001210 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001211 if (CondCode2 != ARMCC::AL) {
1212 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001213 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00001214 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001215 }
1216 return Res;
1217}
1218
Dan Gohman475871a2008-07-27 21:46:04 +00001219SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) {
1220 SDValue Chain = Op.getOperand(0);
1221 SDValue Table = Op.getOperand(1);
1222 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001223 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001224
Duncan Sands83ec4b62008-06-06 12:08:01 +00001225 MVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00001226 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1227 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Dan Gohman475871a2008-07-27 21:46:04 +00001228 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
1229 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Dale Johannesende064702009-02-06 21:50:26 +00001230 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001231 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
1232 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chenga8e29892007-01-19 07:51:42 +00001233 bool isPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001234 Addr = DAG.getLoad(isPIC ? (MVT)MVT::i32 : PTy, dl,
Evan Chenge2446c62007-06-26 18:31:22 +00001235 Chain, Addr, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001236 Chain = Addr.getValue(1);
1237 if (isPIC)
Dale Johannesen33c960f2009-02-04 20:06:27 +00001238 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
1239 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chenga8e29892007-01-19 07:51:42 +00001240}
1241
Dan Gohman475871a2008-07-27 21:46:04 +00001242static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00001243 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001244 unsigned Opc =
1245 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
Dale Johannesende064702009-02-06 21:50:26 +00001246 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
1247 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001248}
1249
Dan Gohman475871a2008-07-27 21:46:04 +00001250static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001251 MVT VT = Op.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001252 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001253 unsigned Opc =
1254 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1255
Dale Johannesende064702009-02-06 21:50:26 +00001256 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
1257 return DAG.getNode(Opc, dl, VT, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001258}
1259
Dan Gohman475871a2008-07-27 21:46:04 +00001260static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001261 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00001262 SDValue Tmp0 = Op.getOperand(0);
1263 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00001264 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001265 MVT VT = Op.getValueType();
1266 MVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001267 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
1268 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Dan Gohman475871a2008-07-27 21:46:04 +00001269 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1270 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001271 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001272}
1273
Dan Gohman475871a2008-07-27 21:46:04 +00001274SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00001275ARMTargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001276 SDValue Chain,
1277 SDValue Dst, SDValue Src,
1278 SDValue Size, unsigned Align,
Dan Gohman707e0182008-04-12 04:36:06 +00001279 bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00001280 const Value *DstSV, uint64_t DstSVOff,
1281 const Value *SrcSV, uint64_t SrcSVOff){
Evan Cheng4102eb52007-10-22 22:11:27 +00001282 // Do repeated 4-byte loads and stores. To be improved.
Dan Gohman707e0182008-04-12 04:36:06 +00001283 // This requires 4-byte alignment.
1284 if ((Align & 3) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00001285 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001286 // This requires the copy size to be a constant, preferrably
1287 // within a subtarget-specific limit.
1288 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
1289 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00001290 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001291 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001292 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00001293 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001294
1295 unsigned BytesLeft = SizeVal & 3;
1296 unsigned NumMemOps = SizeVal >> 2;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001297 unsigned EmittedNumMemOps = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001298 MVT VT = MVT::i32;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001299 unsigned VTSize = 4;
Evan Cheng4102eb52007-10-22 22:11:27 +00001300 unsigned i = 0;
Evan Chenge5e7ce42007-05-18 01:19:57 +00001301 const unsigned MAX_LOADS_IN_LDM = 6;
Dan Gohman475871a2008-07-27 21:46:04 +00001302 SDValue TFOps[MAX_LOADS_IN_LDM];
1303 SDValue Loads[MAX_LOADS_IN_LDM];
Dan Gohman1f13c682008-04-28 17:15:20 +00001304 uint64_t SrcOff = 0, DstOff = 0;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001305
Evan Cheng4102eb52007-10-22 22:11:27 +00001306 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
1307 // same number of stores. The loads and stores will get combined into
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001308 // ldm/stm later on.
Evan Cheng4102eb52007-10-22 22:11:27 +00001309 while (EmittedNumMemOps < NumMemOps) {
1310 for (i = 0;
1311 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001312 Loads[i] = DAG.getLoad(VT, dl, Chain,
1313 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
Evan Cheng4102eb52007-10-22 22:11:27 +00001314 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001315 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001316 TFOps[i] = Loads[i].getValue(1);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001317 SrcOff += VTSize;
1318 }
Dale Johannesen0f502f62009-02-03 22:26:09 +00001319 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001320
Evan Cheng4102eb52007-10-22 22:11:27 +00001321 for (i = 0;
1322 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001323 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
1324 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
Evan Cheng4102eb52007-10-22 22:11:27 +00001325 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001326 DstSV, DstSVOff + DstOff);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001327 DstOff += VTSize;
1328 }
Dale Johannesen0f502f62009-02-03 22:26:09 +00001329 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00001330
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001331 EmittedNumMemOps += i;
1332 }
1333
Evan Cheng4102eb52007-10-22 22:11:27 +00001334 if (BytesLeft == 0)
1335 return Chain;
1336
1337 // Issue loads / stores for the trailing (1 - 3) bytes.
1338 unsigned BytesLeftSave = BytesLeft;
1339 i = 0;
1340 while (BytesLeft) {
1341 if (BytesLeft >= 2) {
1342 VT = MVT::i16;
1343 VTSize = 2;
1344 } else {
1345 VT = MVT::i8;
1346 VTSize = 1;
1347 }
1348
Dale Johannesen0f502f62009-02-03 22:26:09 +00001349 Loads[i] = DAG.getLoad(VT, dl, Chain,
1350 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
Evan Cheng4102eb52007-10-22 22:11:27 +00001351 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001352 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001353 TFOps[i] = Loads[i].getValue(1);
1354 ++i;
1355 SrcOff += VTSize;
1356 BytesLeft -= VTSize;
1357 }
Dale Johannesen0f502f62009-02-03 22:26:09 +00001358 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00001359
1360 i = 0;
1361 BytesLeft = BytesLeftSave;
1362 while (BytesLeft) {
1363 if (BytesLeft >= 2) {
1364 VT = MVT::i16;
1365 VTSize = 2;
1366 } else {
1367 VT = MVT::i8;
1368 VTSize = 1;
1369 }
1370
Dale Johannesen0f502f62009-02-03 22:26:09 +00001371 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
1372 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
Evan Cheng4102eb52007-10-22 22:11:27 +00001373 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001374 DstSV, DstSVOff + DstOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001375 ++i;
1376 DstOff += VTSize;
1377 BytesLeft -= VTSize;
1378 }
Dale Johannesen0f502f62009-02-03 22:26:09 +00001379 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001380}
1381
Duncan Sands1607f052008-12-01 11:39:25 +00001382static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00001383 SDValue Op = N->getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +00001384 DebugLoc dl = N->getDebugLoc();
Evan Chengc7c77292008-11-04 19:57:48 +00001385 if (N->getValueType(0) == MVT::f64) {
1386 // Turn i64->f64 into FMDRR.
Dale Johannesende064702009-02-06 21:50:26 +00001387 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Evan Chengc7c77292008-11-04 19:57:48 +00001388 DAG.getConstant(0, MVT::i32));
Dale Johannesende064702009-02-06 21:50:26 +00001389 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Evan Chengc7c77292008-11-04 19:57:48 +00001390 DAG.getConstant(1, MVT::i32));
Dale Johannesende064702009-02-06 21:50:26 +00001391 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00001392 }
1393
1394 // Turn f64->i64 into FMRRD.
Dale Johannesende064702009-02-06 21:50:26 +00001395 SDValue Cvt = DAG.getNode(ARMISD::FMRRD, dl,
1396 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
Chris Lattner27a6c732007-11-24 07:07:01 +00001397
1398 // Merge the pieces into a single i64 value.
Dale Johannesende064702009-02-06 21:50:26 +00001399 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
Chris Lattner27a6c732007-11-24 07:07:01 +00001400}
1401
Duncan Sands1607f052008-12-01 11:39:25 +00001402static SDValue ExpandSRx(SDNode *N, SelectionDAG &DAG, const ARMSubtarget *ST) {
Chris Lattner27a6c732007-11-24 07:07:01 +00001403 assert(N->getValueType(0) == MVT::i64 &&
1404 (N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
1405 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00001406
Chris Lattner27a6c732007-11-24 07:07:01 +00001407 // We only lower SRA, SRL of 1 here, all others use generic lowering.
1408 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001409 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00001410 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00001411
1412 // If we are in thumb mode, we don't have RRX.
Duncan Sands1607f052008-12-01 11:39:25 +00001413 if (ST->isThumb()) return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00001414
1415 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Dale Johannesende064702009-02-06 21:50:26 +00001416 DebugLoc dl = N->getDebugLoc();
1417 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Chris Lattner27a6c732007-11-24 07:07:01 +00001418 DAG.getConstant(0, MVT::i32));
Dale Johannesende064702009-02-06 21:50:26 +00001419 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Chris Lattner27a6c732007-11-24 07:07:01 +00001420 DAG.getConstant(1, MVT::i32));
1421
1422 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
1423 // captures the result into a carry flag.
1424 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Dale Johannesende064702009-02-06 21:50:26 +00001425 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Chris Lattner27a6c732007-11-24 07:07:01 +00001426
1427 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Dale Johannesende064702009-02-06 21:50:26 +00001428 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Chris Lattner27a6c732007-11-24 07:07:01 +00001429
1430 // Merge the pieces into a single i64 value.
Dale Johannesende064702009-02-06 21:50:26 +00001431 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00001432}
1433
1434
Dan Gohman475871a2008-07-27 21:46:04 +00001435SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001436 switch (Op.getOpcode()) {
1437 default: assert(0 && "Don't know how to custom lower this!"); abort();
1438 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001439 case ISD::GlobalAddress:
1440 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
1441 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001442 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00001443 case ISD::CALL: return LowerCALL(Op, DAG);
1444 case ISD::RET: return LowerRET(Op, DAG);
1445 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
1446 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
1447 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
1448 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
1449 case ISD::SINT_TO_FP:
1450 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
1451 case ISD::FP_TO_SINT:
1452 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
1453 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00001454 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00001455 case ISD::RETURNADDR: break;
1456 case ISD::FRAMEADDR: break;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001457 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001458 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00001459 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00001460 case ISD::SRL:
Duncan Sands1607f052008-12-01 11:39:25 +00001461 case ISD::SRA: return ExpandSRx(Op.getNode(), DAG,Subtarget);
Evan Chenga8e29892007-01-19 07:51:42 +00001462 }
Dan Gohman475871a2008-07-27 21:46:04 +00001463 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001464}
1465
Chris Lattner27a6c732007-11-24 07:07:01 +00001466
Duncan Sands1607f052008-12-01 11:39:25 +00001467/// ReplaceNodeResults - Replace the results of node with an illegal result
1468/// type with new values built out of custom code.
1469///
1470void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
1471 SmallVectorImpl<SDValue>&Results,
1472 SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00001473 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00001474 default:
1475 assert(0 && "Don't know how to custom expand this!");
1476 return;
1477 case ISD::BIT_CONVERT:
1478 Results.push_back(ExpandBIT_CONVERT(N, DAG));
1479 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00001480 case ISD::SRL:
Duncan Sands1607f052008-12-01 11:39:25 +00001481 case ISD::SRA: {
1482 SDValue Res = ExpandSRx(N, DAG, Subtarget);
1483 if (Res.getNode())
1484 Results.push_back(Res);
1485 return;
1486 }
Chris Lattner27a6c732007-11-24 07:07:01 +00001487 }
1488}
1489
1490
Evan Chenga8e29892007-01-19 07:51:42 +00001491//===----------------------------------------------------------------------===//
1492// ARM Scheduler Hooks
1493//===----------------------------------------------------------------------===//
1494
1495MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00001496ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chenga8e29892007-01-19 07:51:42 +00001497 MachineBasicBlock *BB) {
1498 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1499 switch (MI->getOpcode()) {
1500 default: assert(false && "Unexpected instr type to insert");
1501 case ARM::tMOVCCr: {
1502 // To "insert" a SELECT_CC instruction, we actually have to insert the
1503 // diamond control-flow pattern. The incoming instruction knows the
1504 // destination vreg to set, the condition code register to branch on, the
1505 // true/false values to select between, and a branch opcode to use.
1506 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001507 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00001508 ++It;
1509
1510 // thisMBB:
1511 // ...
1512 // TrueVal = ...
1513 // cmpTY ccX, r1, r2
1514 // bCC copy1MBB
1515 // fallthrough --> copy0MBB
1516 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001517 MachineFunction *F = BB->getParent();
1518 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1519 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Chenga8e29892007-01-19 07:51:42 +00001520 BuildMI(BB, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00001521 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001522 F->insert(It, copy0MBB);
1523 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00001524 // Update machine-CFG edges by first adding all successors of the current
1525 // block to the new block which will contain the Phi node for the select.
1526 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1527 e = BB->succ_end(); i != e; ++i)
1528 sinkMBB->addSuccessor(*i);
1529 // Next, remove all successors of the current block, and add the true
1530 // and fallthrough blocks as its successors.
1531 while(!BB->succ_empty())
1532 BB->removeSuccessor(BB->succ_begin());
1533 BB->addSuccessor(copy0MBB);
1534 BB->addSuccessor(sinkMBB);
1535
1536 // copy0MBB:
1537 // %FalseValue = ...
1538 // # fallthrough to sinkMBB
1539 BB = copy0MBB;
1540
1541 // Update machine-CFG edges
1542 BB->addSuccessor(sinkMBB);
1543
1544 // sinkMBB:
1545 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1546 // ...
1547 BB = sinkMBB;
1548 BuildMI(BB, TII->get(ARM::PHI), MI->getOperand(0).getReg())
1549 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1550 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1551
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001552 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00001553 return BB;
1554 }
1555 }
1556}
1557
1558//===----------------------------------------------------------------------===//
1559// ARM Optimization Hooks
1560//===----------------------------------------------------------------------===//
1561
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001562/// PerformFMRRDCombine - Target-specific dag combine xforms for ARMISD::FMRRD.
Dan Gohman475871a2008-07-27 21:46:04 +00001563static SDValue PerformFMRRDCombine(SDNode *N,
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001564 TargetLowering::DAGCombinerInfo &DCI) {
1565 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00001566 SDValue InDouble = N->getOperand(0);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001567 if (InDouble.getOpcode() == ARMISD::FMDRR)
1568 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001569 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001570}
1571
Dan Gohman475871a2008-07-27 21:46:04 +00001572SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001573 DAGCombinerInfo &DCI) const {
1574 switch (N->getOpcode()) {
1575 default: break;
1576 case ARMISD::FMRRD: return PerformFMRRDCombine(N, DCI);
1577 }
1578
Dan Gohman475871a2008-07-27 21:46:04 +00001579 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001580}
1581
1582
Evan Chengb01fad62007-03-12 23:30:29 +00001583/// isLegalAddressImmediate - Return true if the integer value can be used
1584/// as the offset of the target addressing mode for load / store of the
1585/// given type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001586static bool isLegalAddressImmediate(int64_t V, MVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00001587 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00001588 if (V == 0)
1589 return true;
1590
Evan Chengb01fad62007-03-12 23:30:29 +00001591 if (Subtarget->isThumb()) {
1592 if (V < 0)
1593 return false;
1594
1595 unsigned Scale = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001596 switch (VT.getSimpleVT()) {
Evan Chengb01fad62007-03-12 23:30:29 +00001597 default: return false;
1598 case MVT::i1:
1599 case MVT::i8:
1600 // Scale == 1;
1601 break;
1602 case MVT::i16:
1603 // Scale == 2;
1604 Scale = 2;
1605 break;
1606 case MVT::i32:
1607 // Scale == 4;
1608 Scale = 4;
1609 break;
1610 }
1611
1612 if ((V & (Scale - 1)) != 0)
1613 return false;
1614 V /= Scale;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001615 return V == (V & ((1LL << 5) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001616 }
1617
1618 if (V < 0)
1619 V = - V;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001620 switch (VT.getSimpleVT()) {
Evan Chengb01fad62007-03-12 23:30:29 +00001621 default: return false;
1622 case MVT::i1:
1623 case MVT::i8:
1624 case MVT::i32:
1625 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001626 return V == (V & ((1LL << 12) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001627 case MVT::i16:
1628 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001629 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001630 case MVT::f32:
1631 case MVT::f64:
1632 if (!Subtarget->hasVFP2())
1633 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00001634 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00001635 return false;
1636 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001637 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001638 }
Evan Chenga8e29892007-01-19 07:51:42 +00001639}
1640
Chris Lattner37caf8c2007-04-09 23:33:39 +00001641/// isLegalAddressingMode - Return true if the addressing mode represented
1642/// by AM is legal for this target, for a load/store of the specified type.
1643bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
1644 const Type *Ty) const {
Evan Chengd1b3da62008-07-25 00:55:17 +00001645 if (!isLegalAddressImmediate(AM.BaseOffs, getValueType(Ty, true), Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00001646 return false;
Chris Lattner37caf8c2007-04-09 23:33:39 +00001647
1648 // Can never fold addr of global into load/store.
1649 if (AM.BaseGV)
1650 return false;
1651
1652 switch (AM.Scale) {
1653 case 0: // no scale reg, must be "r+i" or "r", or "i".
1654 break;
1655 case 1:
1656 if (Subtarget->isThumb())
1657 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00001658 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00001659 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00001660 // ARM doesn't support any R+R*scale+imm addr modes.
1661 if (AM.BaseOffs)
1662 return false;
1663
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001664 int Scale = AM.Scale;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001665 switch (getValueType(Ty).getSimpleVT()) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00001666 default: return false;
1667 case MVT::i1:
1668 case MVT::i8:
1669 case MVT::i32:
1670 case MVT::i64:
1671 // This assumes i64 is legalized to a pair of i32. If not (i.e.
1672 // ldrd / strd are used, then its address mode is same as i16.
1673 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001674 if (Scale < 0) Scale = -Scale;
1675 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001676 return true;
1677 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00001678 return isPowerOf2_32(Scale & ~1);
Chris Lattner37caf8c2007-04-09 23:33:39 +00001679 case MVT::i16:
1680 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001681 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001682 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00001683 return false;
1684
Chris Lattner37caf8c2007-04-09 23:33:39 +00001685 case MVT::isVoid:
1686 // Note, we allow "void" uses (basically, uses that aren't loads or
1687 // stores), because arm allows folding a scale into many arithmetic
1688 // operations. This should be made more precise and revisited later.
Chris Lattnerb2c594f2007-04-03 00:13:57 +00001689
Chris Lattner37caf8c2007-04-09 23:33:39 +00001690 // Allow r << imm, but the imm has to be a multiple of two.
1691 if (AM.Scale & 1) return false;
1692 return isPowerOf2_32(AM.Scale);
1693 }
1694 break;
Evan Chengb01fad62007-03-12 23:30:29 +00001695 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00001696 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00001697}
1698
Chris Lattner37caf8c2007-04-09 23:33:39 +00001699
Duncan Sands83ec4b62008-06-06 12:08:01 +00001700static bool getIndexedAddressParts(SDNode *Ptr, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001701 bool isSEXTLoad, SDValue &Base,
1702 SDValue &Offset, bool &isInc,
Evan Chenga8e29892007-01-19 07:51:42 +00001703 SelectionDAG &DAG) {
1704 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
1705 return false;
1706
1707 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
1708 // AddressingMode 3
1709 Base = Ptr->getOperand(0);
1710 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001711 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001712 if (RHSC < 0 && RHSC > -256) {
1713 isInc = false;
1714 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1715 return true;
1716 }
1717 }
1718 isInc = (Ptr->getOpcode() == ISD::ADD);
1719 Offset = Ptr->getOperand(1);
1720 return true;
1721 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
1722 // AddressingMode 2
1723 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001724 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001725 if (RHSC < 0 && RHSC > -0x1000) {
1726 isInc = false;
1727 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1728 Base = Ptr->getOperand(0);
1729 return true;
1730 }
1731 }
1732
1733 if (Ptr->getOpcode() == ISD::ADD) {
1734 isInc = true;
1735 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
1736 if (ShOpcVal != ARM_AM::no_shift) {
1737 Base = Ptr->getOperand(1);
1738 Offset = Ptr->getOperand(0);
1739 } else {
1740 Base = Ptr->getOperand(0);
1741 Offset = Ptr->getOperand(1);
1742 }
1743 return true;
1744 }
1745
1746 isInc = (Ptr->getOpcode() == ISD::ADD);
1747 Base = Ptr->getOperand(0);
1748 Offset = Ptr->getOperand(1);
1749 return true;
1750 }
1751
1752 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
1753 return false;
1754}
1755
1756/// getPreIndexedAddressParts - returns true by value, base pointer and
1757/// offset pointer and addressing mode by reference if the node's address
1758/// can be legally represented as pre-indexed load / store address.
1759bool
Dan Gohman475871a2008-07-27 21:46:04 +00001760ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1761 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00001762 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00001763 SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00001764 if (Subtarget->isThumb())
1765 return false;
1766
Duncan Sands83ec4b62008-06-06 12:08:01 +00001767 MVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00001768 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00001769 bool isSEXTLoad = false;
1770 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1771 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001772 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00001773 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1774 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
1775 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001776 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00001777 } else
1778 return false;
1779
1780 bool isInc;
Gabor Greifba36cb52008-08-28 21:40:38 +00001781 bool isLegal = getIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base, Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00001782 isInc, DAG);
1783 if (isLegal) {
1784 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
1785 return true;
1786 }
1787 return false;
1788}
1789
1790/// getPostIndexedAddressParts - returns true by value, base pointer and
1791/// offset pointer and addressing mode by reference if this node can be
1792/// combined with a load / store to form a post-indexed load / store.
1793bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00001794 SDValue &Base,
1795 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00001796 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00001797 SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00001798 if (Subtarget->isThumb())
1799 return false;
1800
Duncan Sands83ec4b62008-06-06 12:08:01 +00001801 MVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00001802 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00001803 bool isSEXTLoad = false;
1804 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001805 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00001806 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1807 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001808 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00001809 } else
1810 return false;
1811
1812 bool isInc;
1813 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
1814 isInc, DAG);
1815 if (isLegal) {
1816 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
1817 return true;
1818 }
1819 return false;
1820}
1821
Dan Gohman475871a2008-07-27 21:46:04 +00001822void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00001823 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001824 APInt &KnownZero,
1825 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001826 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00001827 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001828 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001829 switch (Op.getOpcode()) {
1830 default: break;
1831 case ARMISD::CMOV: {
1832 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00001833 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00001834 if (KnownZero == 0 && KnownOne == 0) return;
1835
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001836 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00001837 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
1838 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00001839 KnownZero &= KnownZeroRHS;
1840 KnownOne &= KnownOneRHS;
1841 return;
1842 }
1843 }
1844}
1845
1846//===----------------------------------------------------------------------===//
1847// ARM Inline Assembly Support
1848//===----------------------------------------------------------------------===//
1849
1850/// getConstraintType - Given a constraint letter, return the type of
1851/// constraint it is for this target.
1852ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00001853ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
1854 if (Constraint.size() == 1) {
1855 switch (Constraint[0]) {
1856 default: break;
1857 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001858 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00001859 }
Evan Chenga8e29892007-01-19 07:51:42 +00001860 }
Chris Lattner4234f572007-03-25 02:14:49 +00001861 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00001862}
1863
1864std::pair<unsigned, const TargetRegisterClass*>
1865ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001866 MVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00001867 if (Constraint.size() == 1) {
1868 // GCC RS6000 Constraint Letters
1869 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001870 case 'l':
1871 // FIXME: in thumb mode, 'l' is only low-regs.
1872 // FALL THROUGH.
1873 case 'r':
1874 return std::make_pair(0U, ARM::GPRRegisterClass);
1875 case 'w':
1876 if (VT == MVT::f32)
1877 return std::make_pair(0U, ARM::SPRRegisterClass);
Evan Cheng0a7baa22007-04-04 00:06:07 +00001878 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001879 return std::make_pair(0U, ARM::DPRRegisterClass);
1880 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001881 }
1882 }
1883 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1884}
1885
1886std::vector<unsigned> ARMTargetLowering::
1887getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001888 MVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00001889 if (Constraint.size() != 1)
1890 return std::vector<unsigned>();
1891
1892 switch (Constraint[0]) { // GCC ARM Constraint Letters
1893 default: break;
1894 case 'l':
1895 case 'r':
1896 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
1897 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
1898 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
1899 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001900 case 'w':
1901 if (VT == MVT::f32)
1902 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
1903 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
1904 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
1905 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
1906 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
1907 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
1908 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
1909 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
1910 if (VT == MVT::f64)
1911 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
1912 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
1913 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
1914 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
1915 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001916 }
1917
1918 return std::vector<unsigned>();
1919}