blob: 721213ddc1061fa5556c9a3c0fd541e88bd5843e [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()) {
44 // Don't have these.
45 setLibcallName(RTLIB::UINTTOFP_I64_F32, NULL);
46 setLibcallName(RTLIB::UINTTOFP_I64_F64, NULL);
Evan Chenga8e29892007-01-19 07:51:42 +000047
Evan Chengb1df8f22007-04-27 08:15:43 +000048 // Uses VFP for Thumb libfuncs if available.
49 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
50 // Single-precision floating-point arithmetic.
51 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
52 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
53 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
54 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +000055
Evan Chengb1df8f22007-04-27 08:15:43 +000056 // Double-precision floating-point arithmetic.
57 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
58 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
59 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
60 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +000061
Evan Chengb1df8f22007-04-27 08:15:43 +000062 // Single-precision comparisons.
63 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
64 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
65 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
66 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
67 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
68 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
69 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
70 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +000071
Evan Chengb1df8f22007-04-27 08:15:43 +000072 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
73 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
74 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
75 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
76 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
77 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
78 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
79 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +000080
Evan Chengb1df8f22007-04-27 08:15:43 +000081 // Double-precision comparisons.
82 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
83 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
84 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
85 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
86 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
87 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
88 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
89 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +000090
Evan Chengb1df8f22007-04-27 08:15:43 +000091 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
92 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
93 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
94 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
95 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
96 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
97 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
98 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +000099
Evan Chengb1df8f22007-04-27 08:15:43 +0000100 // Floating-point to integer conversions.
101 // i64 conversions are done via library routines even when generating VFP
102 // instructions, so use the same ones.
103 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
104 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
105 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
106 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000107
Evan Chengb1df8f22007-04-27 08:15:43 +0000108 // Conversions between floating types.
109 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
110 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
111
112 // Integer to floating-point conversions.
113 // i64 conversions are done via library routines even when generating VFP
114 // instructions, so use the same ones.
115 // FIXME: There appears to be some naming inconsistency in ARM libgcc: e.g.
116 // __floatunsidf vs. __floatunssidfvfp.
117 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
118 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
119 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
120 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
121 }
Evan Chenga8e29892007-01-19 07:51:42 +0000122 }
123
124 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
Evan Chengb6ab2542007-01-31 08:40:13 +0000125 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000126 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
127 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Chris Lattnerddf89562008-01-17 19:59:44 +0000128
129 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000130 }
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000131 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000132
133 // ARM does not have f32 extending load.
Evan Cheng03294662008-10-14 21:26:46 +0000134 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000135
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000136 // ARM does not have i1 sign extending load.
Evan Cheng03294662008-10-14 21:26:46 +0000137 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000138
Evan Chenga8e29892007-01-19 07:51:42 +0000139 // ARM supports all 4 flavors of integer indexed load / store.
140 for (unsigned im = (unsigned)ISD::PRE_INC;
141 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
142 setIndexedLoadAction(im, MVT::i1, Legal);
143 setIndexedLoadAction(im, MVT::i8, Legal);
144 setIndexedLoadAction(im, MVT::i16, Legal);
145 setIndexedLoadAction(im, MVT::i32, Legal);
146 setIndexedStoreAction(im, MVT::i1, Legal);
147 setIndexedStoreAction(im, MVT::i8, Legal);
148 setIndexedStoreAction(im, MVT::i16, Legal);
149 setIndexedStoreAction(im, MVT::i32, Legal);
150 }
151
152 // i64 operation support.
153 if (Subtarget->isThumb()) {
154 setOperationAction(ISD::MUL, MVT::i64, Expand);
155 setOperationAction(ISD::MULHU, MVT::i32, Expand);
156 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000157 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
158 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000159 } else {
Dan Gohman525178c2007-10-08 18:33:35 +0000160 setOperationAction(ISD::MUL, MVT::i64, Expand);
161 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000162 if (!Subtarget->hasV6Ops())
Dan Gohman525178c2007-10-08 18:33:35 +0000163 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000164 }
165 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
166 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
167 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
168 setOperationAction(ISD::SRL, MVT::i64, Custom);
169 setOperationAction(ISD::SRA, MVT::i64, Custom);
170
171 // ARM does not have ROTL.
172 setOperationAction(ISD::ROTL, MVT::i32, Expand);
173 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
174 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
Evan Chengb0636152007-02-01 23:34:03 +0000175 if (!Subtarget->hasV5TOps() || Subtarget->isThumb())
Evan Chenga8e29892007-01-19 07:51:42 +0000176 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
177
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000178 // Only ARMv6 has BSWAP.
179 if (!Subtarget->hasV6Ops())
Chris Lattner1719e132007-03-20 02:25:53 +0000180 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000181
Evan Chenga8e29892007-01-19 07:51:42 +0000182 // These are expanded into libcalls.
183 setOperationAction(ISD::SDIV, MVT::i32, Expand);
184 setOperationAction(ISD::UDIV, MVT::i32, Expand);
185 setOperationAction(ISD::SREM, MVT::i32, Expand);
186 setOperationAction(ISD::UREM, MVT::i32, Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000187 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
188 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000189
190 // Support label based line numbers.
Dan Gohman7f460202008-06-30 20:59:49 +0000191 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000192 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000193
194 setOperationAction(ISD::RET, MVT::Other, Custom);
195 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
196 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000197 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000198 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000199
Evan Chenga8e29892007-01-19 07:51:42 +0000200 // Use the default implementation.
Nate Begeman48a65512008-02-04 21:44:06 +0000201 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000202 setOperationAction(ISD::VAARG , MVT::Other, Expand);
203 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
204 setOperationAction(ISD::VAEND , MVT::Other, Expand);
205 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
206 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
207 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000208 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000209
210 if (!Subtarget->hasV6Ops()) {
211 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
212 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
213 }
214 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
215
Evan Chengb6ab2542007-01-31 08:40:13 +0000216 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb())
Evan Chengc7c77292008-11-04 19:57:48 +0000217 // Turn f64->i64 into FMRRD, i64 -> f64 to FMDRR iff target supports vfp2.
Evan Chenga8e29892007-01-19 07:51:42 +0000218 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000219
220 // We want to custom lower some of our intrinsics.
221 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
222
Evan Chenga8e29892007-01-19 07:51:42 +0000223 setOperationAction(ISD::SETCC , MVT::i32, Expand);
224 setOperationAction(ISD::SETCC , MVT::f32, Expand);
225 setOperationAction(ISD::SETCC , MVT::f64, Expand);
226 setOperationAction(ISD::SELECT , MVT::i32, Expand);
227 setOperationAction(ISD::SELECT , MVT::f32, Expand);
228 setOperationAction(ISD::SELECT , MVT::f64, Expand);
229 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
230 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
231 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
232
233 setOperationAction(ISD::BRCOND , MVT::Other, Expand);
234 setOperationAction(ISD::BR_CC , MVT::i32, Custom);
235 setOperationAction(ISD::BR_CC , MVT::f32, Custom);
236 setOperationAction(ISD::BR_CC , MVT::f64, Custom);
237 setOperationAction(ISD::BR_JT , MVT::Other, Custom);
238
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000239 // We don't support sin/cos/fmod/copysign/pow
Evan Chenga8e29892007-01-19 07:51:42 +0000240 setOperationAction(ISD::FSIN , MVT::f64, Expand);
241 setOperationAction(ISD::FSIN , MVT::f32, Expand);
242 setOperationAction(ISD::FCOS , MVT::f32, Expand);
243 setOperationAction(ISD::FCOS , MVT::f64, Expand);
244 setOperationAction(ISD::FREM , MVT::f64, Expand);
245 setOperationAction(ISD::FREM , MVT::f32, Expand);
Evan Cheng110cf482008-04-01 01:50:16 +0000246 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) {
247 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
248 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
249 }
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000250 setOperationAction(ISD::FPOW , MVT::f64, Expand);
251 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000252
253 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
Evan Cheng110cf482008-04-01 01:50:16 +0000254 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) {
255 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
256 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
257 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
258 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
259 }
Evan Chenga8e29892007-01-19 07:51:42 +0000260
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000261 // We have target-specific dag combine patterns for the following nodes:
262 // ARMISD::FMRRD - No need to call setTargetDAGCombine
263
Evan Chenga8e29892007-01-19 07:51:42 +0000264 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Chenga8e29892007-01-19 07:51:42 +0000265 setSchedulingPreference(SchedulingForRegPressure);
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000266 setIfCvtBlockSizeLimit(Subtarget->isThumb() ? 0 : 10);
Evan Cheng97e604e2007-06-19 23:55:02 +0000267 setIfCvtDupBlockSizeLimit(Subtarget->isThumb() ? 0 : 2);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000268
269 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Evan Chenga8e29892007-01-19 07:51:42 +0000270}
271
272
273const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
274 switch (Opcode) {
275 default: return 0;
276 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000277 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
278 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000279 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000280 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
281 case ARMISD::tCALL: return "ARMISD::tCALL";
282 case ARMISD::BRCOND: return "ARMISD::BRCOND";
283 case ARMISD::BR_JT: return "ARMISD::BR_JT";
284 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
285 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
286 case ARMISD::CMP: return "ARMISD::CMP";
Lauro Ramos Venancio99966632007-04-02 01:30:03 +0000287 case ARMISD::CMPNZ: return "ARMISD::CMPNZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000288 case ARMISD::CMPFP: return "ARMISD::CMPFP";
289 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
290 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
291 case ARMISD::CMOV: return "ARMISD::CMOV";
292 case ARMISD::CNEG: return "ARMISD::CNEG";
293
294 case ARMISD::FTOSI: return "ARMISD::FTOSI";
295 case ARMISD::FTOUI: return "ARMISD::FTOUI";
296 case ARMISD::SITOF: return "ARMISD::SITOF";
297 case ARMISD::UITOF: return "ARMISD::UITOF";
Evan Chenga8e29892007-01-19 07:51:42 +0000298
299 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
300 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
301 case ARMISD::RRX: return "ARMISD::RRX";
302
303 case ARMISD::FMRRD: return "ARMISD::FMRRD";
304 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000305
306 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Evan Chenga8e29892007-01-19 07:51:42 +0000307 }
308}
309
310//===----------------------------------------------------------------------===//
311// Lowering Code
312//===----------------------------------------------------------------------===//
313
314
315/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
316static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
317 switch (CC) {
318 default: assert(0 && "Unknown condition code!");
319 case ISD::SETNE: return ARMCC::NE;
320 case ISD::SETEQ: return ARMCC::EQ;
321 case ISD::SETGT: return ARMCC::GT;
322 case ISD::SETGE: return ARMCC::GE;
323 case ISD::SETLT: return ARMCC::LT;
324 case ISD::SETLE: return ARMCC::LE;
325 case ISD::SETUGT: return ARMCC::HI;
326 case ISD::SETUGE: return ARMCC::HS;
327 case ISD::SETULT: return ARMCC::LO;
328 case ISD::SETULE: return ARMCC::LS;
329 }
330}
331
332/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. It
333/// returns true if the operands should be inverted to form the proper
334/// comparison.
335static bool FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
336 ARMCC::CondCodes &CondCode2) {
337 bool Invert = false;
338 CondCode2 = ARMCC::AL;
339 switch (CC) {
340 default: assert(0 && "Unknown FP condition!");
341 case ISD::SETEQ:
342 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
343 case ISD::SETGT:
344 case ISD::SETOGT: CondCode = ARMCC::GT; break;
345 case ISD::SETGE:
346 case ISD::SETOGE: CondCode = ARMCC::GE; break;
347 case ISD::SETOLT: CondCode = ARMCC::MI; break;
348 case ISD::SETOLE: CondCode = ARMCC::GT; Invert = true; break;
349 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
350 case ISD::SETO: CondCode = ARMCC::VC; break;
351 case ISD::SETUO: CondCode = ARMCC::VS; break;
352 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
353 case ISD::SETUGT: CondCode = ARMCC::HI; break;
354 case ISD::SETUGE: CondCode = ARMCC::PL; break;
355 case ISD::SETLT:
356 case ISD::SETULT: CondCode = ARMCC::LT; break;
357 case ISD::SETLE:
358 case ISD::SETULE: CondCode = ARMCC::LE; break;
359 case ISD::SETNE:
360 case ISD::SETUNE: CondCode = ARMCC::NE; break;
361 }
362 return Invert;
363}
364
365static void
Duncan Sands83ec4b62008-06-06 12:08:01 +0000366HowToPassArgument(MVT ObjectVT, unsigned NumGPRs,
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000367 unsigned StackOffset, unsigned &NeededGPRs,
368 unsigned &NeededStackSize, unsigned &GPRPad,
Duncan Sands276dcbd2008-03-21 09:14:45 +0000369 unsigned &StackPad, ISD::ArgFlagsTy Flags) {
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000370 NeededStackSize = 0;
371 NeededGPRs = 0;
372 StackPad = 0;
373 GPRPad = 0;
Duncan Sands276dcbd2008-03-21 09:14:45 +0000374 unsigned align = Flags.getOrigAlign();
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000375 GPRPad = NumGPRs % ((align + 3)/4);
376 StackPad = StackOffset % align;
377 unsigned firstGPR = NumGPRs + GPRPad;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000378 switch (ObjectVT.getSimpleVT()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000379 default: assert(0 && "Unhandled argument type!");
380 case MVT::i32:
381 case MVT::f32:
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000382 if (firstGPR < 4)
383 NeededGPRs = 1;
Evan Chenga8e29892007-01-19 07:51:42 +0000384 else
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000385 NeededStackSize = 4;
Evan Chenga8e29892007-01-19 07:51:42 +0000386 break;
387 case MVT::i64:
388 case MVT::f64:
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000389 if (firstGPR < 3)
390 NeededGPRs = 2;
391 else if (firstGPR == 3) {
392 NeededGPRs = 1;
393 NeededStackSize = 4;
Evan Chenga8e29892007-01-19 07:51:42 +0000394 } else
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000395 NeededStackSize = 8;
Evan Chenga8e29892007-01-19 07:51:42 +0000396 }
397}
398
Evan Chengfc403422007-02-03 08:53:01 +0000399/// LowerCALL - Lowering a ISD::CALL node into a callseq_start <-
400/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
401/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000402SDValue ARMTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Dan Gohman095cc292008-09-13 01:54:27 +0000403 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
404 MVT RetVT = TheCall->getRetValType(0);
405 SDValue Chain = TheCall->getChain();
406 unsigned CallConv = TheCall->getCallingConv();
Evan Chenga8e29892007-01-19 07:51:42 +0000407 assert((CallConv == CallingConv::C ||
Evan Chenga8e29892007-01-19 07:51:42 +0000408 CallConv == CallingConv::Fast) && "unknown calling convention");
Dan Gohman095cc292008-09-13 01:54:27 +0000409 SDValue Callee = TheCall->getCallee();
410 unsigned NumOps = TheCall->getNumArgs();
Evan Chenga8e29892007-01-19 07:51:42 +0000411 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
412 unsigned NumGPRs = 0; // GPRs used for parameter passing.
413
414 // Count how many bytes are to be pushed on the stack.
415 unsigned NumBytes = 0;
416
417 // Add up all the space actually used.
418 for (unsigned i = 0; i < NumOps; ++i) {
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000419 unsigned ObjSize;
420 unsigned ObjGPRs;
421 unsigned StackPad;
422 unsigned GPRPad;
Dan Gohman095cc292008-09-13 01:54:27 +0000423 MVT ObjectVT = TheCall->getArg(i).getValueType();
424 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000425 HowToPassArgument(ObjectVT, NumGPRs, NumBytes, ObjGPRs, ObjSize,
426 GPRPad, StackPad, Flags);
427 NumBytes += ObjSize + StackPad;
428 NumGPRs += ObjGPRs + GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000429 }
430
431 // Adjust the stack pointer for the new arguments...
432 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000433 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000434
Dan Gohman475871a2008-07-27 21:46:04 +0000435 SDValue StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000436
437 static const unsigned GPRArgRegs[] = {
438 ARM::R0, ARM::R1, ARM::R2, ARM::R3
439 };
440
441 NumGPRs = 0;
Dan Gohman475871a2008-07-27 21:46:04 +0000442 std::vector<std::pair<unsigned, SDValue> > RegsToPass;
443 std::vector<SDValue> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000444 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohman095cc292008-09-13 01:54:27 +0000445 SDValue Arg = TheCall->getArg(i);
446 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000447 MVT ArgVT = Arg.getValueType();
Evan Chenga8e29892007-01-19 07:51:42 +0000448
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000449 unsigned ObjSize;
450 unsigned ObjGPRs;
451 unsigned GPRPad;
452 unsigned StackPad;
453 HowToPassArgument(ArgVT, NumGPRs, ArgOffset, ObjGPRs,
454 ObjSize, GPRPad, StackPad, Flags);
455 NumGPRs += GPRPad;
456 ArgOffset += StackPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000457 if (ObjGPRs > 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000458 switch (ArgVT.getSimpleVT()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000459 default: assert(0 && "Unexpected ValueType for argument!");
460 case MVT::i32:
461 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Arg));
462 break;
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000463 case MVT::f32:
Evan Chenga8e29892007-01-19 07:51:42 +0000464 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs],
465 DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg)));
466 break;
467 case MVT::i64: {
Dan Gohman475871a2008-07-27 21:46:04 +0000468 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Arg,
Evan Chenga8e29892007-01-19 07:51:42 +0000469 DAG.getConstant(0, getPointerTy()));
Dan Gohman475871a2008-07-27 21:46:04 +0000470 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Arg,
Evan Chenga8e29892007-01-19 07:51:42 +0000471 DAG.getConstant(1, getPointerTy()));
472 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Lo));
473 if (ObjGPRs == 2)
474 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1], Hi));
475 else {
Dan Gohman475871a2008-07-27 21:46:04 +0000476 SDValue PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType());
Evan Chenga8e29892007-01-19 07:51:42 +0000477 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
478 MemOpChains.push_back(DAG.getStore(Chain, Hi, PtrOff, NULL, 0));
479 }
480 break;
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000481 }
Evan Chenga8e29892007-01-19 07:51:42 +0000482 case MVT::f64: {
Dan Gohman475871a2008-07-27 21:46:04 +0000483 SDValue Cvt = DAG.getNode(ARMISD::FMRRD,
Evan Chenga8e29892007-01-19 07:51:42 +0000484 DAG.getVTList(MVT::i32, MVT::i32),
485 &Arg, 1);
486 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Cvt));
487 if (ObjGPRs == 2)
488 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1],
489 Cvt.getValue(1)));
490 else {
Dan Gohman475871a2008-07-27 21:46:04 +0000491 SDValue PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType());
Evan Chenga8e29892007-01-19 07:51:42 +0000492 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
493 MemOpChains.push_back(DAG.getStore(Chain, Cvt.getValue(1), PtrOff,
494 NULL, 0));
495 }
496 break;
497 }
498 }
499 } else {
500 assert(ObjSize != 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000501 SDValue PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Evan Chenga8e29892007-01-19 07:51:42 +0000502 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
503 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
504 }
505
506 NumGPRs += ObjGPRs;
507 ArgOffset += ObjSize;
508 }
509
510 if (!MemOpChains.empty())
511 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
512 &MemOpChains[0], MemOpChains.size());
513
514 // Build a sequence of copy-to-reg nodes chained together with token chain
515 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +0000516 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +0000517 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
518 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
519 InFlag);
520 InFlag = Chain.getValue(1);
521 }
522
Bill Wendling056292f2008-09-16 21:48:12 +0000523 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
524 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
525 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +0000526 bool isDirect = false;
527 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +0000528 bool isLocalARMFunc = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000529 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
530 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +0000531 isDirect = true;
Reid Spencer5cbf9852007-01-30 20:08:39 +0000532 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
Evan Chenga8e29892007-01-19 07:51:42 +0000533 GV->hasLinkOnceLinkage());
Evan Cheng970a4192007-01-19 19:28:01 +0000534 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +0000535 getTargetMachine().getRelocationModel() != Reloc::Static;
536 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +0000537 // ARM call to a local ARM function is predicable.
538 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +0000539 // tBX takes a register source operand.
540 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
541 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
542 ARMCP::CPStub, 4);
Dan Gohman475871a2008-07-27 21:46:04 +0000543 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 2);
Evan Chengc60e76d2007-01-30 20:37:08 +0000544 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
545 Callee = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000546 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Evan Chengc60e76d2007-01-30 20:37:08 +0000547 Callee = DAG.getNode(ARMISD::PIC_ADD, getPointerTy(), Callee, PICLabel);
548 } else
549 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +0000550 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000551 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +0000552 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000553 getTargetMachine().getRelocationModel() != Reloc::Static;
554 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +0000555 // tBX takes a register source operand.
556 const char *Sym = S->getSymbol();
557 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
558 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(Sym, ARMPCLabelIndex,
559 ARMCP::CPStub, 4);
Dan Gohman475871a2008-07-27 21:46:04 +0000560 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 2);
Evan Chengc60e76d2007-01-30 20:37:08 +0000561 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
562 Callee = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000563 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Evan Chengc60e76d2007-01-30 20:37:08 +0000564 Callee = DAG.getNode(ARMISD::PIC_ADD, getPointerTy(), Callee, PICLabel);
565 } else
Bill Wendling056292f2008-09-16 21:48:12 +0000566 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000567 }
568
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000569 // FIXME: handle tail calls differently.
570 unsigned CallOpc;
571 if (Subtarget->isThumb()) {
572 if (!Subtarget->hasV5TOps() && (!isDirect || isARMFunc))
573 CallOpc = ARMISD::CALL_NOLINK;
574 else
575 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
576 } else {
577 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +0000578 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
579 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000580 }
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +0000581 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb()) {
582 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000583 Chain = DAG.getCopyToReg(Chain, ARM::LR,
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +0000584 DAG.getNode(ISD::UNDEF, MVT::i32), InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000585 InFlag = Chain.getValue(1);
586 }
587
Dan Gohman475871a2008-07-27 21:46:04 +0000588 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +0000589 Ops.push_back(Chain);
590 Ops.push_back(Callee);
591
592 // Add argument registers to the end of the list so that they are known live
593 // into the call.
594 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
595 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
596 RegsToPass[i].second.getValueType()));
597
Gabor Greifba36cb52008-08-28 21:40:38 +0000598 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +0000599 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +0000600 // Returns a chain and a flag for retval copy to use.
601 Chain = DAG.getNode(CallOpc, DAG.getVTList(MVT::Other, MVT::Flag),
602 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +0000603 InFlag = Chain.getValue(1);
604
Chris Lattnere563bbc2008-10-11 22:08:30 +0000605 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
606 DAG.getIntPtrConstant(0, true), InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000607 if (RetVT != MVT::Other)
608 InFlag = Chain.getValue(1);
609
Dan Gohman475871a2008-07-27 21:46:04 +0000610 std::vector<SDValue> ResultVals;
Evan Chenga8e29892007-01-19 07:51:42 +0000611
612 // If the call has results, copy the values out of the ret val registers.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000613 switch (RetVT.getSimpleVT()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000614 default: assert(0 && "Unexpected ret value!");
615 case MVT::Other:
616 break;
617 case MVT::i32:
618 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
619 ResultVals.push_back(Chain.getValue(0));
Dan Gohman095cc292008-09-13 01:54:27 +0000620 if (TheCall->getNumRetVals() > 1 &&
621 TheCall->getRetValType(1) == MVT::i32) {
Evan Chenga8e29892007-01-19 07:51:42 +0000622 // Returns a i64 value.
623 Chain = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32,
624 Chain.getValue(2)).getValue(1);
625 ResultVals.push_back(Chain.getValue(0));
Evan Chenga8e29892007-01-19 07:51:42 +0000626 }
Evan Chenga8e29892007-01-19 07:51:42 +0000627 break;
628 case MVT::f32:
629 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
630 ResultVals.push_back(DAG.getNode(ISD::BIT_CONVERT, MVT::f32,
631 Chain.getValue(0)));
Evan Chenga8e29892007-01-19 07:51:42 +0000632 break;
633 case MVT::f64: {
Dan Gohman475871a2008-07-27 21:46:04 +0000634 SDValue Lo = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
635 SDValue Hi = DAG.getCopyFromReg(Lo, ARM::R1, MVT::i32, Lo.getValue(2));
Evan Chenga8e29892007-01-19 07:51:42 +0000636 ResultVals.push_back(DAG.getNode(ARMISD::FMDRR, MVT::f64, Lo, Hi));
Evan Chenga8e29892007-01-19 07:51:42 +0000637 break;
638 }
639 }
640
Evan Chenga8e29892007-01-19 07:51:42 +0000641 if (ResultVals.empty())
642 return Chain;
643
644 ResultVals.push_back(Chain);
Dan Gohman475871a2008-07-27 21:46:04 +0000645 SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size());
Gabor Greif99a6cb92008-08-26 22:36:50 +0000646 return Res.getValue(Op.getResNo());
Evan Chenga8e29892007-01-19 07:51:42 +0000647}
648
Dan Gohman475871a2008-07-27 21:46:04 +0000649static SDValue LowerRET(SDValue Op, SelectionDAG &DAG) {
650 SDValue Copy;
651 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +0000652 switch(Op.getNumOperands()) {
653 default:
654 assert(0 && "Do not know how to return this many arguments!");
655 abort();
656 case 1: {
Dan Gohman475871a2008-07-27 21:46:04 +0000657 SDValue LR = DAG.getRegister(ARM::LR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000658 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
659 }
660 case 3:
661 Op = Op.getOperand(1);
662 if (Op.getValueType() == MVT::f32) {
663 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
664 } else if (Op.getValueType() == MVT::f64) {
Chris Lattner65a33232007-10-18 06:17:07 +0000665 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
666 // available.
667 Op = DAG.getNode(ARMISD::FMRRD, DAG.getVTList(MVT::i32, MVT::i32), &Op,1);
Dan Gohman475871a2008-07-27 21:46:04 +0000668 SDValue Sign = DAG.getConstant(0, MVT::i32);
Chris Lattner65a33232007-10-18 06:17:07 +0000669 return DAG.getNode(ISD::RET, MVT::Other, Chain, Op, Sign,
670 Op.getValue(1), Sign);
Evan Chenga8e29892007-01-19 07:51:42 +0000671 }
Dan Gohman475871a2008-07-27 21:46:04 +0000672 Copy = DAG.getCopyToReg(Chain, ARM::R0, Op, SDValue());
Chris Lattner84bc5422007-12-31 04:13:23 +0000673 if (DAG.getMachineFunction().getRegInfo().liveout_empty())
674 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R0);
Evan Chenga8e29892007-01-19 07:51:42 +0000675 break;
676 case 5:
Dan Gohman475871a2008-07-27 21:46:04 +0000677 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDValue());
Evan Chenga8e29892007-01-19 07:51:42 +0000678 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
679 // If we haven't noted the R0+R1 are live out, do so now.
Chris Lattner84bc5422007-12-31 04:13:23 +0000680 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
681 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R0);
682 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R1);
Evan Chenga8e29892007-01-19 07:51:42 +0000683 }
684 break;
Chris Lattner78d60452008-07-11 20:53:00 +0000685 case 9: // i128 -> 4 regs
Dan Gohman475871a2008-07-27 21:46:04 +0000686 Copy = DAG.getCopyToReg(Chain, ARM::R3, Op.getOperand(7), SDValue());
Chris Lattner78d60452008-07-11 20:53:00 +0000687 Copy = DAG.getCopyToReg(Copy , ARM::R2, Op.getOperand(5), Copy.getValue(1));
688 Copy = DAG.getCopyToReg(Copy , ARM::R1, Op.getOperand(3), Copy.getValue(1));
689 Copy = DAG.getCopyToReg(Copy , ARM::R0, Op.getOperand(1), Copy.getValue(1));
690 // If we haven't noted the R0+R1 are live out, do so now.
691 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
692 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R0);
693 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R1);
694 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R2);
695 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R3);
696 }
697 break;
698
Evan Chenga8e29892007-01-19 07:51:42 +0000699 }
700
701 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
702 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
703}
704
Bill Wendling056292f2008-09-16 21:48:12 +0000705// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
706// their target countpart wrapped in the ARMISD::Wrapper node. Suppose N is
707// one of the above mentioned nodes. It has to be wrapped because otherwise
708// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
709// be used to form addressing mode. These wrapped nodes will be selected
710// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +0000711static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000712 MVT PtrVT = Op.getValueType();
Evan Chenga8e29892007-01-19 07:51:42 +0000713 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +0000714 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +0000715 if (CP->isMachineConstantPoolEntry())
716 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
717 CP->getAlignment());
718 else
719 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
720 CP->getAlignment());
721 return DAG.getNode(ARMISD::Wrapper, MVT::i32, Res);
722}
723
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000724// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +0000725SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000726ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
727 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000728 MVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000729 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
730 ARMConstantPoolValue *CPV =
731 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
732 PCAdj, "tlsgd", true);
Dan Gohman475871a2008-07-27 21:46:04 +0000733 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 2);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000734 Argument = DAG.getNode(ARMISD::Wrapper, MVT::i32, Argument);
735 Argument = DAG.getLoad(PtrVT, DAG.getEntryNode(), Argument, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000736 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000737
Dan Gohman475871a2008-07-27 21:46:04 +0000738 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000739 Argument = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Argument, PICLabel);
740
741 // call __tls_get_addr.
742 ArgListTy Args;
743 ArgListEntry Entry;
744 Entry.Node = Argument;
745 Entry.Ty = (const Type *) Type::Int32Ty;
746 Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +0000747 std::pair<SDValue, SDValue> CallResult =
Dale Johannesen86098bd2008-09-26 19:31:26 +0000748 LowerCallTo(Chain, (const Type *) Type::Int32Ty, false, false, false, false,
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000749 CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +0000750 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000751 return CallResult.first;
752}
753
754// Lower ISD::GlobalTLSAddress using the "initial exec" or
755// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +0000756SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000757ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
758 SelectionDAG &DAG) {
759 GlobalValue *GV = GA->getGlobal();
Dan Gohman475871a2008-07-27 21:46:04 +0000760 SDValue Offset;
761 SDValue Chain = DAG.getEntryNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000762 MVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000763 // Get the Thread Pointer
Dan Gohman475871a2008-07-27 21:46:04 +0000764 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000765
766 if (GV->isDeclaration()){
767 // initial exec model
768 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
769 ARMConstantPoolValue *CPV =
770 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
771 PCAdj, "gottpoff", true);
772 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 2);
773 Offset = DAG.getNode(ARMISD::Wrapper, MVT::i32, Offset);
774 Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0);
775 Chain = Offset.getValue(1);
776
Dan Gohman475871a2008-07-27 21:46:04 +0000777 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000778 Offset = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Offset, PICLabel);
779
780 Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0);
781 } else {
782 // local exec model
783 ARMConstantPoolValue *CPV =
784 new ARMConstantPoolValue(GV, ARMCP::CPValue, "tpoff");
785 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 2);
786 Offset = DAG.getNode(ARMISD::Wrapper, MVT::i32, Offset);
787 Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0);
788 }
789
790 // The address of the thread local variable is the add of the thread
791 // pointer with the offset of the variable.
792 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
793}
794
Dan Gohman475871a2008-07-27 21:46:04 +0000795SDValue
796ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000797 // TODO: implement the "local dynamic" model
798 assert(Subtarget->isTargetELF() &&
799 "TLS not implemented for non-ELF targets");
800 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
801 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
802 // otherwise use the "Local Exec" TLS Model
803 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
804 return LowerToTLSGeneralDynamicModel(GA, DAG);
805 else
806 return LowerToTLSExecModels(GA, DAG);
807}
808
Dan Gohman475871a2008-07-27 21:46:04 +0000809SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000810 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000811 MVT PtrVT = getPointerTy();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000812 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
813 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
814 if (RelocM == Reloc::PIC_) {
Lauro Ramos Venancio5d3d44a2007-05-14 23:20:21 +0000815 bool UseGOTOFF = GV->hasInternalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000816 ARMConstantPoolValue *CPV =
817 new ARMConstantPoolValue(GV, ARMCP::CPValue, UseGOTOFF ? "GOTOFF":"GOT");
Dan Gohman475871a2008-07-27 21:46:04 +0000818 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000819 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
Dan Gohman475871a2008-07-27 21:46:04 +0000820 SDValue Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
821 SDValue Chain = Result.getValue(1);
822 SDValue GOT = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PtrVT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000823 Result = DAG.getNode(ISD::ADD, PtrVT, Result, GOT);
824 if (!UseGOTOFF)
825 Result = DAG.getLoad(PtrVT, Chain, Result, NULL, 0);
826 return Result;
827 } else {
Dan Gohman475871a2008-07-27 21:46:04 +0000828 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 2);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000829 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
830 return DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
831 }
832}
833
Evan Chenga8e29892007-01-19 07:51:42 +0000834/// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol
Evan Cheng97c9bb52007-05-04 00:26:58 +0000835/// even in non-static mode.
836static bool GVIsIndirectSymbol(GlobalValue *GV, Reloc::Model RelocM) {
837 return RelocM != Reloc::Static &&
838 (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
Gabor Greifa99be512007-07-05 17:07:56 +0000839 (GV->isDeclaration() && !GV->hasNotBeenReadFromBitcode()));
Evan Chenga8e29892007-01-19 07:51:42 +0000840}
841
Dan Gohman475871a2008-07-27 21:46:04 +0000842SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000843 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000844 MVT PtrVT = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +0000845 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
846 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng97c9bb52007-05-04 00:26:58 +0000847 bool IsIndirect = GVIsIndirectSymbol(GV, RelocM);
Dan Gohman475871a2008-07-27 21:46:04 +0000848 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +0000849 if (RelocM == Reloc::Static)
850 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 2);
851 else {
852 unsigned PCAdj = (RelocM != Reloc::PIC_)
853 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Chengc60e76d2007-01-30 20:37:08 +0000854 ARMCP::ARMCPKind Kind = IsIndirect ? ARMCP::CPNonLazyPtr
855 : ARMCP::CPValue;
Evan Chenga8e29892007-01-19 07:51:42 +0000856 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
Evan Chengc60e76d2007-01-30 20:37:08 +0000857 Kind, PCAdj);
Evan Chenga8e29892007-01-19 07:51:42 +0000858 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
859 }
860 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
861
Dan Gohman475871a2008-07-27 21:46:04 +0000862 SDValue Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
863 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +0000864
865 if (RelocM == Reloc::PIC_) {
Dan Gohman475871a2008-07-27 21:46:04 +0000866 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000867 Result = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Result, PICLabel);
868 }
869 if (IsIndirect)
870 Result = DAG.getLoad(PtrVT, Chain, Result, NULL, 0);
871
872 return Result;
873}
874
Dan Gohman475871a2008-07-27 21:46:04 +0000875SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000876 SelectionDAG &DAG){
877 assert(Subtarget->isTargetELF() &&
878 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000879 MVT PtrVT = getPointerTy();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000880 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
881 ARMConstantPoolValue *CPV = new ARMConstantPoolValue("_GLOBAL_OFFSET_TABLE_",
882 ARMPCLabelIndex,
883 ARMCP::CPValue, PCAdj);
Dan Gohman475871a2008-07-27 21:46:04 +0000884 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000885 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
Dan Gohman475871a2008-07-27 21:46:04 +0000886 SDValue Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
887 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000888 return DAG.getNode(ARMISD::PIC_ADD, PtrVT, Result, PICLabel);
889}
890
Dan Gohman475871a2008-07-27 21:46:04 +0000891static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000892 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000893 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000894 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +0000895 default: return SDValue(); // Don't custom lower most intrinsics.
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000896 case Intrinsic::arm_thread_pointer:
897 return DAG.getNode(ARMISD::THREAD_POINTER, PtrVT);
898 }
899}
900
Dan Gohman475871a2008-07-27 21:46:04 +0000901static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +0000902 unsigned VarArgsFrameIndex) {
903 // vastart just stores the address of the VarArgsFrameIndex slot into the
904 // memory location argument.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000905 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000906 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +0000907 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
908 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), SV, 0);
Evan Chenga8e29892007-01-19 07:51:42 +0000909}
910
Dan Gohman475871a2008-07-27 21:46:04 +0000911static SDValue LowerFORMAL_ARGUMENT(SDValue Op, SelectionDAG &DAG,
Nate Begemanbf1caa92008-02-12 22:54:40 +0000912 unsigned ArgNo, unsigned &NumGPRs,
913 unsigned &ArgOffset) {
Evan Chenga8e29892007-01-19 07:51:42 +0000914 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000915 MVT ObjectVT = Op.getValue(ArgNo).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000916 SDValue Root = Op.getOperand(0);
Chris Lattner84bc5422007-12-31 04:13:23 +0000917 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Evan Chenga8e29892007-01-19 07:51:42 +0000918
919 static const unsigned GPRArgRegs[] = {
920 ARM::R0, ARM::R1, ARM::R2, ARM::R3
921 };
922
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000923 unsigned ObjSize;
924 unsigned ObjGPRs;
925 unsigned GPRPad;
926 unsigned StackPad;
Duncan Sands276dcbd2008-03-21 09:14:45 +0000927 ISD::ArgFlagsTy Flags =
928 cast<ARG_FLAGSSDNode>(Op.getOperand(ArgNo + 3))->getArgFlags();
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000929 HowToPassArgument(ObjectVT, NumGPRs, ArgOffset, ObjGPRs,
930 ObjSize, GPRPad, StackPad, Flags);
931 NumGPRs += GPRPad;
932 ArgOffset += StackPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000933
Dan Gohman475871a2008-07-27 21:46:04 +0000934 SDValue ArgValue;
Evan Chenga8e29892007-01-19 07:51:42 +0000935 if (ObjGPRs == 1) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000936 unsigned VReg = RegInfo.createVirtualRegister(&ARM::GPRRegClass);
937 RegInfo.addLiveIn(GPRArgRegs[NumGPRs], VReg);
Evan Chenga8e29892007-01-19 07:51:42 +0000938 ArgValue = DAG.getCopyFromReg(Root, VReg, MVT::i32);
939 if (ObjectVT == MVT::f32)
940 ArgValue = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, ArgValue);
941 } else if (ObjGPRs == 2) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000942 unsigned VReg = RegInfo.createVirtualRegister(&ARM::GPRRegClass);
943 RegInfo.addLiveIn(GPRArgRegs[NumGPRs], VReg);
Evan Chenga8e29892007-01-19 07:51:42 +0000944 ArgValue = DAG.getCopyFromReg(Root, VReg, MVT::i32);
945
Chris Lattner84bc5422007-12-31 04:13:23 +0000946 VReg = RegInfo.createVirtualRegister(&ARM::GPRRegClass);
947 RegInfo.addLiveIn(GPRArgRegs[NumGPRs+1], VReg);
Dan Gohman475871a2008-07-27 21:46:04 +0000948 SDValue ArgValue2 = DAG.getCopyFromReg(Root, VReg, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000949
Chris Lattner27a6c732007-11-24 07:07:01 +0000950 assert(ObjectVT != MVT::i64 && "i64 should already be lowered");
951 ArgValue = DAG.getNode(ARMISD::FMDRR, MVT::f64, ArgValue, ArgValue2);
Evan Chenga8e29892007-01-19 07:51:42 +0000952 }
953 NumGPRs += ObjGPRs;
954
955 if (ObjSize) {
Chris Lattner9f72d1a2008-02-13 07:35:30 +0000956 MachineFrameInfo *MFI = MF.getFrameInfo();
957 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Dan Gohman475871a2008-07-27 21:46:04 +0000958 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
Chris Lattner9f72d1a2008-02-13 07:35:30 +0000959 if (ObjGPRs == 0)
960 ArgValue = DAG.getLoad(ObjectVT, Root, FIN, NULL, 0);
961 else {
Dan Gohman475871a2008-07-27 21:46:04 +0000962 SDValue ArgValue2 = DAG.getLoad(MVT::i32, Root, FIN, NULL, 0);
Chris Lattner9f72d1a2008-02-13 07:35:30 +0000963 assert(ObjectVT != MVT::i64 && "i64 should already be lowered");
964 ArgValue = DAG.getNode(ARMISD::FMDRR, MVT::f64, ArgValue, ArgValue2);
Evan Chenga8e29892007-01-19 07:51:42 +0000965 }
966
967 ArgOffset += ObjSize; // Move on to the next argument.
968 }
969
970 return ArgValue;
971}
972
Dan Gohman475871a2008-07-27 21:46:04 +0000973SDValue
974ARMTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
975 std::vector<SDValue> ArgValues;
976 SDValue Root = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +0000977 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
978 unsigned NumGPRs = 0; // GPRs used for parameter passing.
Evan Chenga8e29892007-01-19 07:51:42 +0000979
Gabor Greifba36cb52008-08-28 21:40:38 +0000980 unsigned NumArgs = Op.getNode()->getNumValues()-1;
Evan Chenga8e29892007-01-19 07:51:42 +0000981 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo)
Nate Begemanbf1caa92008-02-12 22:54:40 +0000982 ArgValues.push_back(LowerFORMAL_ARGUMENT(Op, DAG, ArgNo,
Evan Chenga8e29892007-01-19 07:51:42 +0000983 NumGPRs, ArgOffset));
984
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000985 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Evan Chenga8e29892007-01-19 07:51:42 +0000986 if (isVarArg) {
987 static const unsigned GPRArgRegs[] = {
988 ARM::R0, ARM::R1, ARM::R2, ARM::R3
989 };
990
991 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner84bc5422007-12-31 04:13:23 +0000992 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Evan Chenga8e29892007-01-19 07:51:42 +0000993 MachineFrameInfo *MFI = MF.getFrameInfo();
994 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +0000995 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
996 unsigned VARegSize = (4 - NumGPRs) * 4;
997 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Evan Chenga8e29892007-01-19 07:51:42 +0000998 if (VARegSaveSize) {
999 // If this function is vararg, store any remaining integer argument regs
1000 // to their spots on the stack so that they may be loaded by deferencing
1001 // the result of va_next.
1002 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001003 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1004 VARegSaveSize - VARegSize);
Dan Gohman475871a2008-07-27 21:46:04 +00001005 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001006
Dan Gohman475871a2008-07-27 21:46:04 +00001007 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001008 for (; NumGPRs < 4; ++NumGPRs) {
Chris Lattner84bc5422007-12-31 04:13:23 +00001009 unsigned VReg = RegInfo.createVirtualRegister(&ARM::GPRRegClass);
1010 RegInfo.addLiveIn(GPRArgRegs[NumGPRs], VReg);
Dan Gohman475871a2008-07-27 21:46:04 +00001011 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
1012 SDValue Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001013 MemOps.push_back(Store);
1014 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1015 DAG.getConstant(4, getPointerTy()));
1016 }
1017 if (!MemOps.empty())
1018 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1019 &MemOps[0], MemOps.size());
1020 } else
1021 // This will point to the next argument passed via stack.
1022 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1023 }
1024
1025 ArgValues.push_back(Root);
1026
1027 // Return the new list of results.
Gabor Greifba36cb52008-08-28 21:40:38 +00001028 return DAG.getMergeValues(Op.getNode()->getVTList(), &ArgValues[0],
Duncan Sandsf9516202008-06-30 10:19:09 +00001029 ArgValues.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001030}
1031
1032/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001033static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001034 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001035 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001036 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001037 // Maybe this has already been legalized into the constant pool?
1038 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001039 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001040 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1041 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001042 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001043 }
1044 }
1045 return false;
1046}
1047
Evan Cheng9a2ef952007-02-02 01:53:26 +00001048static bool isLegalCmpImmediate(unsigned C, bool isThumb) {
Evan Chenga8e29892007-01-19 07:51:42 +00001049 return ( isThumb && (C & ~255U) == 0) ||
1050 (!isThumb && ARM_AM::getSOImmVal(C) != -1);
1051}
1052
1053/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1054/// the given operands.
Dan Gohman475871a2008-07-27 21:46:04 +00001055static SDValue getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
1056 SDValue &ARMCC, SelectionDAG &DAG, bool isThumb) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001057 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001058 unsigned C = RHSC->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001059 if (!isLegalCmpImmediate(C, isThumb)) {
1060 // Constant does not fit, try adjusting it by one?
1061 switch (CC) {
1062 default: break;
1063 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001064 case ISD::SETGE:
Evan Chenga8e29892007-01-19 07:51:42 +00001065 if (isLegalCmpImmediate(C-1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001066 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
1067 RHS = DAG.getConstant(C-1, MVT::i32);
1068 }
1069 break;
1070 case ISD::SETULT:
1071 case ISD::SETUGE:
1072 if (C > 0 && isLegalCmpImmediate(C-1, isThumb)) {
1073 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Evan Chenga8e29892007-01-19 07:51:42 +00001074 RHS = DAG.getConstant(C-1, MVT::i32);
1075 }
1076 break;
1077 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001078 case ISD::SETGT:
Evan Chenga8e29892007-01-19 07:51:42 +00001079 if (isLegalCmpImmediate(C+1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001080 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
1081 RHS = DAG.getConstant(C+1, MVT::i32);
1082 }
1083 break;
1084 case ISD::SETULE:
1085 case ISD::SETUGT:
1086 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb)) {
1087 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Evan Chenga8e29892007-01-19 07:51:42 +00001088 RHS = DAG.getConstant(C+1, MVT::i32);
1089 }
1090 break;
1091 }
1092 }
1093 }
1094
1095 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001096 ARMISD::NodeType CompareType;
1097 switch (CondCode) {
1098 default:
1099 CompareType = ARMISD::CMP;
1100 break;
1101 case ARMCC::EQ:
1102 case ARMCC::NE:
1103 case ARMCC::MI:
1104 case ARMCC::PL:
1105 // Uses only N and Z Flags
1106 CompareType = ARMISD::CMPNZ;
1107 break;
1108 }
Evan Chenga8e29892007-01-19 07:51:42 +00001109 ARMCC = DAG.getConstant(CondCode, MVT::i32);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001110 return DAG.getNode(CompareType, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001111}
1112
1113/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Dan Gohman475871a2008-07-27 21:46:04 +00001114static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG) {
1115 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001116 if (!isFloatingPointZero(RHS))
1117 Cmp = DAG.getNode(ARMISD::CMPFP, MVT::Flag, LHS, RHS);
1118 else
1119 Cmp = DAG.getNode(ARMISD::CMPFPw0, MVT::Flag, LHS);
1120 return DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp);
1121}
1122
Dan Gohman475871a2008-07-27 21:46:04 +00001123static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00001124 const ARMSubtarget *ST) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001125 MVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001126 SDValue LHS = Op.getOperand(0);
1127 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001128 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001129 SDValue TrueVal = Op.getOperand(2);
1130 SDValue FalseVal = Op.getOperand(3);
Evan Chenga8e29892007-01-19 07:51:42 +00001131
1132 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001133 SDValue ARMCC;
1134 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1135 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb());
Evan Cheng0e1d3792007-07-05 07:18:20 +00001136 return DAG.getNode(ARMISD::CMOV, VT, FalseVal, TrueVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001137 }
1138
1139 ARMCC::CondCodes CondCode, CondCode2;
1140 if (FPCCToARMCC(CC, CondCode, CondCode2))
1141 std::swap(TrueVal, FalseVal);
1142
Dan Gohman475871a2008-07-27 21:46:04 +00001143 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1144 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1145 SDValue Cmp = getVFPCmp(LHS, RHS, DAG);
1146 SDValue Result = DAG.getNode(ARMISD::CMOV, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001147 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001148 if (CondCode2 != ARMCC::AL) {
Dan Gohman475871a2008-07-27 21:46:04 +00001149 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001150 // FIXME: Needs another CMP because flag can have but one use.
Dan Gohman475871a2008-07-27 21:46:04 +00001151 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001152 Result = DAG.getNode(ARMISD::CMOV, VT, Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001153 }
1154 return Result;
1155}
1156
Dan Gohman475871a2008-07-27 21:46:04 +00001157static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00001158 const ARMSubtarget *ST) {
Dan Gohman475871a2008-07-27 21:46:04 +00001159 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001160 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001161 SDValue LHS = Op.getOperand(2);
1162 SDValue RHS = Op.getOperand(3);
1163 SDValue Dest = Op.getOperand(4);
Evan Chenga8e29892007-01-19 07:51:42 +00001164
1165 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001166 SDValue ARMCC;
1167 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1168 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb());
Evan Cheng0e1d3792007-07-05 07:18:20 +00001169 return DAG.getNode(ARMISD::BRCOND, MVT::Other, Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001170 }
1171
1172 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
1173 ARMCC::CondCodes CondCode, CondCode2;
1174 if (FPCCToARMCC(CC, CondCode, CondCode2))
1175 // Swap the LHS/RHS of the comparison if needed.
1176 std::swap(LHS, RHS);
1177
Dan Gohman475871a2008-07-27 21:46:04 +00001178 SDValue Cmp = getVFPCmp(LHS, RHS, DAG);
1179 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1180 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001181 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001182 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
1183 SDValue Res = DAG.getNode(ARMISD::BRCOND, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001184 if (CondCode2 != ARMCC::AL) {
1185 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001186 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Evan Cheng0e1d3792007-07-05 07:18:20 +00001187 Res = DAG.getNode(ARMISD::BRCOND, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001188 }
1189 return Res;
1190}
1191
Dan Gohman475871a2008-07-27 21:46:04 +00001192SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) {
1193 SDValue Chain = Op.getOperand(0);
1194 SDValue Table = Op.getOperand(1);
1195 SDValue Index = Op.getOperand(2);
Evan Chenga8e29892007-01-19 07:51:42 +00001196
Duncan Sands83ec4b62008-06-06 12:08:01 +00001197 MVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00001198 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1199 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Dan Gohman475871a2008-07-27 21:46:04 +00001200 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
1201 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Evan Chenga8e29892007-01-19 07:51:42 +00001202 Table = DAG.getNode(ARMISD::WrapperJT, MVT::i32, JTI, UId);
1203 Index = DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(4, PTy));
Dan Gohman475871a2008-07-27 21:46:04 +00001204 SDValue Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Evan Chenga8e29892007-01-19 07:51:42 +00001205 bool isPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001206 Addr = DAG.getLoad(isPIC ? (MVT)MVT::i32 : PTy,
Evan Chenge2446c62007-06-26 18:31:22 +00001207 Chain, Addr, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001208 Chain = Addr.getValue(1);
1209 if (isPIC)
1210 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Table);
1211 return DAG.getNode(ARMISD::BR_JT, MVT::Other, Chain, Addr, JTI, UId);
1212}
1213
Dan Gohman475871a2008-07-27 21:46:04 +00001214static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001215 unsigned Opc =
1216 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
1217 Op = DAG.getNode(Opc, MVT::f32, Op.getOperand(0));
1218 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
1219}
1220
Dan Gohman475871a2008-07-27 21:46:04 +00001221static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001222 MVT VT = Op.getValueType();
Evan Chenga8e29892007-01-19 07:51:42 +00001223 unsigned Opc =
1224 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1225
1226 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Op.getOperand(0));
1227 return DAG.getNode(Opc, VT, Op);
1228}
1229
Dan Gohman475871a2008-07-27 21:46:04 +00001230static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001231 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00001232 SDValue Tmp0 = Op.getOperand(0);
1233 SDValue Tmp1 = Op.getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001234 MVT VT = Op.getValueType();
1235 MVT SrcVT = Tmp1.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001236 SDValue AbsVal = DAG.getNode(ISD::FABS, VT, Tmp0);
1237 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG);
1238 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1239 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001240 return DAG.getNode(ARMISD::CNEG, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001241}
1242
Dan Gohman475871a2008-07-27 21:46:04 +00001243SDValue
Dan Gohman707e0182008-04-12 04:36:06 +00001244ARMTargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001245 SDValue Chain,
1246 SDValue Dst, SDValue Src,
1247 SDValue Size, unsigned Align,
Dan Gohman707e0182008-04-12 04:36:06 +00001248 bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00001249 const Value *DstSV, uint64_t DstSVOff,
1250 const Value *SrcSV, uint64_t SrcSVOff){
Evan Cheng4102eb52007-10-22 22:11:27 +00001251 // Do repeated 4-byte loads and stores. To be improved.
Dan Gohman707e0182008-04-12 04:36:06 +00001252 // This requires 4-byte alignment.
1253 if ((Align & 3) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00001254 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001255 // This requires the copy size to be a constant, preferrably
1256 // within a subtarget-specific limit.
1257 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
1258 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00001259 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001260 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001261 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00001262 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001263
1264 unsigned BytesLeft = SizeVal & 3;
1265 unsigned NumMemOps = SizeVal >> 2;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001266 unsigned EmittedNumMemOps = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001267 MVT VT = MVT::i32;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001268 unsigned VTSize = 4;
Evan Cheng4102eb52007-10-22 22:11:27 +00001269 unsigned i = 0;
Evan Chenge5e7ce42007-05-18 01:19:57 +00001270 const unsigned MAX_LOADS_IN_LDM = 6;
Dan Gohman475871a2008-07-27 21:46:04 +00001271 SDValue TFOps[MAX_LOADS_IN_LDM];
1272 SDValue Loads[MAX_LOADS_IN_LDM];
Dan Gohman1f13c682008-04-28 17:15:20 +00001273 uint64_t SrcOff = 0, DstOff = 0;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001274
Evan Cheng4102eb52007-10-22 22:11:27 +00001275 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
1276 // same number of stores. The loads and stores will get combined into
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001277 // ldm/stm later on.
Evan Cheng4102eb52007-10-22 22:11:27 +00001278 while (EmittedNumMemOps < NumMemOps) {
1279 for (i = 0;
1280 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001281 Loads[i] = DAG.getLoad(VT, Chain,
Dan Gohman707e0182008-04-12 04:36:06 +00001282 DAG.getNode(ISD::ADD, MVT::i32, Src,
Evan Cheng4102eb52007-10-22 22:11:27 +00001283 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001284 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001285 TFOps[i] = Loads[i].getValue(1);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001286 SrcOff += VTSize;
1287 }
Evan Cheng4102eb52007-10-22 22:11:27 +00001288 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001289
Evan Cheng4102eb52007-10-22 22:11:27 +00001290 for (i = 0;
1291 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
1292 TFOps[i] = DAG.getStore(Chain, Loads[i],
Dan Gohman707e0182008-04-12 04:36:06 +00001293 DAG.getNode(ISD::ADD, MVT::i32, Dst,
Evan Cheng4102eb52007-10-22 22:11:27 +00001294 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001295 DstSV, DstSVOff + DstOff);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001296 DstOff += VTSize;
1297 }
Evan Cheng4102eb52007-10-22 22:11:27 +00001298 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &TFOps[0], i);
1299
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001300 EmittedNumMemOps += i;
1301 }
1302
Evan Cheng4102eb52007-10-22 22:11:27 +00001303 if (BytesLeft == 0)
1304 return Chain;
1305
1306 // Issue loads / stores for the trailing (1 - 3) bytes.
1307 unsigned BytesLeftSave = BytesLeft;
1308 i = 0;
1309 while (BytesLeft) {
1310 if (BytesLeft >= 2) {
1311 VT = MVT::i16;
1312 VTSize = 2;
1313 } else {
1314 VT = MVT::i8;
1315 VTSize = 1;
1316 }
1317
1318 Loads[i] = DAG.getLoad(VT, Chain,
Dan Gohman707e0182008-04-12 04:36:06 +00001319 DAG.getNode(ISD::ADD, MVT::i32, Src,
Evan Cheng4102eb52007-10-22 22:11:27 +00001320 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001321 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001322 TFOps[i] = Loads[i].getValue(1);
1323 ++i;
1324 SrcOff += VTSize;
1325 BytesLeft -= VTSize;
1326 }
1327 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &TFOps[0], i);
1328
1329 i = 0;
1330 BytesLeft = BytesLeftSave;
1331 while (BytesLeft) {
1332 if (BytesLeft >= 2) {
1333 VT = MVT::i16;
1334 VTSize = 2;
1335 } else {
1336 VT = MVT::i8;
1337 VTSize = 1;
1338 }
1339
1340 TFOps[i] = DAG.getStore(Chain, Loads[i],
Dan Gohman707e0182008-04-12 04:36:06 +00001341 DAG.getNode(ISD::ADD, MVT::i32, Dst,
Evan Cheng4102eb52007-10-22 22:11:27 +00001342 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001343 DstSV, DstSVOff + DstOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001344 ++i;
1345 DstOff += VTSize;
1346 BytesLeft -= VTSize;
1347 }
1348 return DAG.getNode(ISD::TokenFactor, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001349}
1350
Chris Lattner27a6c732007-11-24 07:07:01 +00001351static SDNode *ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00001352 SDValue Op = N->getOperand(0);
Evan Chengc7c77292008-11-04 19:57:48 +00001353 if (N->getValueType(0) == MVT::f64) {
1354 // Turn i64->f64 into FMDRR.
1355 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
1356 DAG.getConstant(0, MVT::i32));
1357 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
1358 DAG.getConstant(1, MVT::i32));
1359 return DAG.getNode(ARMISD::FMDRR, MVT::f64, Lo, Hi).getNode();
1360 }
1361
1362 // Turn f64->i64 into FMRRD.
Dan Gohman475871a2008-07-27 21:46:04 +00001363 SDValue Cvt = DAG.getNode(ARMISD::FMRRD, DAG.getVTList(MVT::i32, MVT::i32),
Evan Chengc7c77292008-11-04 19:57:48 +00001364 &Op, 1);
Chris Lattner27a6c732007-11-24 07:07:01 +00001365
1366 // Merge the pieces into a single i64 value.
Gabor Greifba36cb52008-08-28 21:40:38 +00001367 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Cvt, Cvt.getValue(1)).getNode();
Chris Lattner27a6c732007-11-24 07:07:01 +00001368}
1369
1370static SDNode *ExpandSRx(SDNode *N, SelectionDAG &DAG, const ARMSubtarget *ST) {
1371 assert(N->getValueType(0) == MVT::i64 &&
1372 (N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
1373 "Unknown shift to lower!");
1374
1375 // We only lower SRA, SRL of 1 here, all others use generic lowering.
1376 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001377 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Chris Lattner27a6c732007-11-24 07:07:01 +00001378 return 0;
1379
1380 // If we are in thumb mode, we don't have RRX.
1381 if (ST->isThumb()) return 0;
1382
1383 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Dan Gohman475871a2008-07-27 21:46:04 +00001384 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(0),
Chris Lattner27a6c732007-11-24 07:07:01 +00001385 DAG.getConstant(0, MVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +00001386 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(0),
Chris Lattner27a6c732007-11-24 07:07:01 +00001387 DAG.getConstant(1, MVT::i32));
1388
1389 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
1390 // captures the result into a carry flag.
1391 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
1392 Hi = DAG.getNode(Opc, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
1393
1394 // The low part is an ARMISD::RRX operand, which shifts the carry in.
1395 Lo = DAG.getNode(ARMISD::RRX, MVT::i32, Lo, Hi.getValue(1));
1396
1397 // Merge the pieces into a single i64 value.
Gabor Greifba36cb52008-08-28 21:40:38 +00001398 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi).getNode();
Chris Lattner27a6c732007-11-24 07:07:01 +00001399}
1400
1401
Dan Gohman475871a2008-07-27 21:46:04 +00001402SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001403 switch (Op.getOpcode()) {
1404 default: assert(0 && "Don't know how to custom lower this!"); abort();
1405 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001406 case ISD::GlobalAddress:
1407 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
1408 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001409 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00001410 case ISD::CALL: return LowerCALL(Op, DAG);
1411 case ISD::RET: return LowerRET(Op, DAG);
1412 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
1413 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
1414 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
1415 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
1416 case ISD::SINT_TO_FP:
1417 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
1418 case ISD::FP_TO_SINT:
1419 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
1420 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00001421 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00001422 case ISD::RETURNADDR: break;
1423 case ISD::FRAMEADDR: break;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001424 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001425 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001426 case ISD::BIT_CONVERT: return SDValue(ExpandBIT_CONVERT(Op.getNode(), DAG), 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00001427 case ISD::SRL:
Gabor Greifba36cb52008-08-28 21:40:38 +00001428 case ISD::SRA: return SDValue(ExpandSRx(Op.getNode(), DAG,Subtarget),0);
Evan Chenga8e29892007-01-19 07:51:42 +00001429 }
Dan Gohman475871a2008-07-27 21:46:04 +00001430 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001431}
1432
Chris Lattner27a6c732007-11-24 07:07:01 +00001433
Duncan Sands126d9072008-07-04 11:47:58 +00001434/// ReplaceNodeResults - Provide custom lowering hooks for nodes with illegal
1435/// result types.
1436SDNode *ARMTargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00001437 switch (N->getOpcode()) {
1438 default: assert(0 && "Don't know how to custom expand this!"); abort();
1439 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(N, DAG);
1440 case ISD::SRL:
1441 case ISD::SRA: return ExpandSRx(N, DAG, Subtarget);
1442 }
1443}
1444
1445
Evan Chenga8e29892007-01-19 07:51:42 +00001446//===----------------------------------------------------------------------===//
1447// ARM Scheduler Hooks
1448//===----------------------------------------------------------------------===//
1449
1450MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00001451ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chenga8e29892007-01-19 07:51:42 +00001452 MachineBasicBlock *BB) {
1453 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1454 switch (MI->getOpcode()) {
1455 default: assert(false && "Unexpected instr type to insert");
1456 case ARM::tMOVCCr: {
1457 // To "insert" a SELECT_CC instruction, we actually have to insert the
1458 // diamond control-flow pattern. The incoming instruction knows the
1459 // destination vreg to set, the condition code register to branch on, the
1460 // true/false values to select between, and a branch opcode to use.
1461 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001462 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00001463 ++It;
1464
1465 // thisMBB:
1466 // ...
1467 // TrueVal = ...
1468 // cmpTY ccX, r1, r2
1469 // bCC copy1MBB
1470 // fallthrough --> copy0MBB
1471 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001472 MachineFunction *F = BB->getParent();
1473 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1474 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Chenga8e29892007-01-19 07:51:42 +00001475 BuildMI(BB, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00001476 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001477 F->insert(It, copy0MBB);
1478 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00001479 // Update machine-CFG edges by first adding all successors of the current
1480 // block to the new block which will contain the Phi node for the select.
1481 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1482 e = BB->succ_end(); i != e; ++i)
1483 sinkMBB->addSuccessor(*i);
1484 // Next, remove all successors of the current block, and add the true
1485 // and fallthrough blocks as its successors.
1486 while(!BB->succ_empty())
1487 BB->removeSuccessor(BB->succ_begin());
1488 BB->addSuccessor(copy0MBB);
1489 BB->addSuccessor(sinkMBB);
1490
1491 // copy0MBB:
1492 // %FalseValue = ...
1493 // # fallthrough to sinkMBB
1494 BB = copy0MBB;
1495
1496 // Update machine-CFG edges
1497 BB->addSuccessor(sinkMBB);
1498
1499 // sinkMBB:
1500 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1501 // ...
1502 BB = sinkMBB;
1503 BuildMI(BB, TII->get(ARM::PHI), MI->getOperand(0).getReg())
1504 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1505 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1506
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001507 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00001508 return BB;
1509 }
1510 }
1511}
1512
1513//===----------------------------------------------------------------------===//
1514// ARM Optimization Hooks
1515//===----------------------------------------------------------------------===//
1516
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001517/// PerformFMRRDCombine - Target-specific dag combine xforms for ARMISD::FMRRD.
Dan Gohman475871a2008-07-27 21:46:04 +00001518static SDValue PerformFMRRDCombine(SDNode *N,
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001519 TargetLowering::DAGCombinerInfo &DCI) {
1520 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00001521 SDValue InDouble = N->getOperand(0);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001522 if (InDouble.getOpcode() == ARMISD::FMDRR)
1523 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001524 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001525}
1526
Dan Gohman475871a2008-07-27 21:46:04 +00001527SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001528 DAGCombinerInfo &DCI) const {
1529 switch (N->getOpcode()) {
1530 default: break;
1531 case ARMISD::FMRRD: return PerformFMRRDCombine(N, DCI);
1532 }
1533
Dan Gohman475871a2008-07-27 21:46:04 +00001534 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001535}
1536
1537
Evan Chengb01fad62007-03-12 23:30:29 +00001538/// isLegalAddressImmediate - Return true if the integer value can be used
1539/// as the offset of the target addressing mode for load / store of the
1540/// given type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001541static bool isLegalAddressImmediate(int64_t V, MVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00001542 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00001543 if (V == 0)
1544 return true;
1545
Evan Chengb01fad62007-03-12 23:30:29 +00001546 if (Subtarget->isThumb()) {
1547 if (V < 0)
1548 return false;
1549
1550 unsigned Scale = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001551 switch (VT.getSimpleVT()) {
Evan Chengb01fad62007-03-12 23:30:29 +00001552 default: return false;
1553 case MVT::i1:
1554 case MVT::i8:
1555 // Scale == 1;
1556 break;
1557 case MVT::i16:
1558 // Scale == 2;
1559 Scale = 2;
1560 break;
1561 case MVT::i32:
1562 // Scale == 4;
1563 Scale = 4;
1564 break;
1565 }
1566
1567 if ((V & (Scale - 1)) != 0)
1568 return false;
1569 V /= Scale;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001570 return V == (V & ((1LL << 5) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001571 }
1572
1573 if (V < 0)
1574 V = - V;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001575 switch (VT.getSimpleVT()) {
Evan Chengb01fad62007-03-12 23:30:29 +00001576 default: return false;
1577 case MVT::i1:
1578 case MVT::i8:
1579 case MVT::i32:
1580 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001581 return V == (V & ((1LL << 12) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001582 case MVT::i16:
1583 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001584 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001585 case MVT::f32:
1586 case MVT::f64:
1587 if (!Subtarget->hasVFP2())
1588 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00001589 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00001590 return false;
1591 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001592 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001593 }
Evan Chenga8e29892007-01-19 07:51:42 +00001594}
1595
Chris Lattner37caf8c2007-04-09 23:33:39 +00001596/// isLegalAddressingMode - Return true if the addressing mode represented
1597/// by AM is legal for this target, for a load/store of the specified type.
1598bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
1599 const Type *Ty) const {
Evan Chengd1b3da62008-07-25 00:55:17 +00001600 if (!isLegalAddressImmediate(AM.BaseOffs, getValueType(Ty, true), Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00001601 return false;
Chris Lattner37caf8c2007-04-09 23:33:39 +00001602
1603 // Can never fold addr of global into load/store.
1604 if (AM.BaseGV)
1605 return false;
1606
1607 switch (AM.Scale) {
1608 case 0: // no scale reg, must be "r+i" or "r", or "i".
1609 break;
1610 case 1:
1611 if (Subtarget->isThumb())
1612 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00001613 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00001614 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00001615 // ARM doesn't support any R+R*scale+imm addr modes.
1616 if (AM.BaseOffs)
1617 return false;
1618
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001619 int Scale = AM.Scale;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001620 switch (getValueType(Ty).getSimpleVT()) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00001621 default: return false;
1622 case MVT::i1:
1623 case MVT::i8:
1624 case MVT::i32:
1625 case MVT::i64:
1626 // This assumes i64 is legalized to a pair of i32. If not (i.e.
1627 // ldrd / strd are used, then its address mode is same as i16.
1628 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001629 if (Scale < 0) Scale = -Scale;
1630 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001631 return true;
1632 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00001633 return isPowerOf2_32(Scale & ~1);
Chris Lattner37caf8c2007-04-09 23:33:39 +00001634 case MVT::i16:
1635 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001636 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001637 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00001638 return false;
1639
Chris Lattner37caf8c2007-04-09 23:33:39 +00001640 case MVT::isVoid:
1641 // Note, we allow "void" uses (basically, uses that aren't loads or
1642 // stores), because arm allows folding a scale into many arithmetic
1643 // operations. This should be made more precise and revisited later.
Chris Lattnerb2c594f2007-04-03 00:13:57 +00001644
Chris Lattner37caf8c2007-04-09 23:33:39 +00001645 // Allow r << imm, but the imm has to be a multiple of two.
1646 if (AM.Scale & 1) return false;
1647 return isPowerOf2_32(AM.Scale);
1648 }
1649 break;
Evan Chengb01fad62007-03-12 23:30:29 +00001650 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00001651 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00001652}
1653
Chris Lattner37caf8c2007-04-09 23:33:39 +00001654
Duncan Sands83ec4b62008-06-06 12:08:01 +00001655static bool getIndexedAddressParts(SDNode *Ptr, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001656 bool isSEXTLoad, SDValue &Base,
1657 SDValue &Offset, bool &isInc,
Evan Chenga8e29892007-01-19 07:51:42 +00001658 SelectionDAG &DAG) {
1659 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
1660 return false;
1661
1662 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
1663 // AddressingMode 3
1664 Base = Ptr->getOperand(0);
1665 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001666 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001667 if (RHSC < 0 && RHSC > -256) {
1668 isInc = false;
1669 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1670 return true;
1671 }
1672 }
1673 isInc = (Ptr->getOpcode() == ISD::ADD);
1674 Offset = Ptr->getOperand(1);
1675 return true;
1676 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
1677 // AddressingMode 2
1678 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001679 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001680 if (RHSC < 0 && RHSC > -0x1000) {
1681 isInc = false;
1682 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1683 Base = Ptr->getOperand(0);
1684 return true;
1685 }
1686 }
1687
1688 if (Ptr->getOpcode() == ISD::ADD) {
1689 isInc = true;
1690 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
1691 if (ShOpcVal != ARM_AM::no_shift) {
1692 Base = Ptr->getOperand(1);
1693 Offset = Ptr->getOperand(0);
1694 } else {
1695 Base = Ptr->getOperand(0);
1696 Offset = Ptr->getOperand(1);
1697 }
1698 return true;
1699 }
1700
1701 isInc = (Ptr->getOpcode() == ISD::ADD);
1702 Base = Ptr->getOperand(0);
1703 Offset = Ptr->getOperand(1);
1704 return true;
1705 }
1706
1707 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
1708 return false;
1709}
1710
1711/// getPreIndexedAddressParts - returns true by value, base pointer and
1712/// offset pointer and addressing mode by reference if the node's address
1713/// can be legally represented as pre-indexed load / store address.
1714bool
Dan Gohman475871a2008-07-27 21:46:04 +00001715ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1716 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00001717 ISD::MemIndexedMode &AM,
1718 SelectionDAG &DAG) {
1719 if (Subtarget->isThumb())
1720 return false;
1721
Duncan Sands83ec4b62008-06-06 12:08:01 +00001722 MVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00001723 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00001724 bool isSEXTLoad = false;
1725 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1726 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001727 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00001728 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1729 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
1730 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001731 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00001732 } else
1733 return false;
1734
1735 bool isInc;
Gabor Greifba36cb52008-08-28 21:40:38 +00001736 bool isLegal = getIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base, Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00001737 isInc, DAG);
1738 if (isLegal) {
1739 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
1740 return true;
1741 }
1742 return false;
1743}
1744
1745/// getPostIndexedAddressParts - returns true by value, base pointer and
1746/// offset pointer and addressing mode by reference if this node can be
1747/// combined with a load / store to form a post-indexed load / store.
1748bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00001749 SDValue &Base,
1750 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00001751 ISD::MemIndexedMode &AM,
1752 SelectionDAG &DAG) {
1753 if (Subtarget->isThumb())
1754 return false;
1755
Duncan Sands83ec4b62008-06-06 12:08:01 +00001756 MVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00001757 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00001758 bool isSEXTLoad = false;
1759 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001760 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00001761 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1762 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001763 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00001764 } else
1765 return false;
1766
1767 bool isInc;
1768 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
1769 isInc, DAG);
1770 if (isLegal) {
1771 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
1772 return true;
1773 }
1774 return false;
1775}
1776
Dan Gohman475871a2008-07-27 21:46:04 +00001777void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00001778 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001779 APInt &KnownZero,
1780 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001781 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00001782 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001783 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001784 switch (Op.getOpcode()) {
1785 default: break;
1786 case ARMISD::CMOV: {
1787 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00001788 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00001789 if (KnownZero == 0 && KnownOne == 0) return;
1790
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001791 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00001792 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
1793 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00001794 KnownZero &= KnownZeroRHS;
1795 KnownOne &= KnownOneRHS;
1796 return;
1797 }
1798 }
1799}
1800
1801//===----------------------------------------------------------------------===//
1802// ARM Inline Assembly Support
1803//===----------------------------------------------------------------------===//
1804
1805/// getConstraintType - Given a constraint letter, return the type of
1806/// constraint it is for this target.
1807ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00001808ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
1809 if (Constraint.size() == 1) {
1810 switch (Constraint[0]) {
1811 default: break;
1812 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001813 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00001814 }
Evan Chenga8e29892007-01-19 07:51:42 +00001815 }
Chris Lattner4234f572007-03-25 02:14:49 +00001816 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00001817}
1818
1819std::pair<unsigned, const TargetRegisterClass*>
1820ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001821 MVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00001822 if (Constraint.size() == 1) {
1823 // GCC RS6000 Constraint Letters
1824 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001825 case 'l':
1826 // FIXME: in thumb mode, 'l' is only low-regs.
1827 // FALL THROUGH.
1828 case 'r':
1829 return std::make_pair(0U, ARM::GPRRegisterClass);
1830 case 'w':
1831 if (VT == MVT::f32)
1832 return std::make_pair(0U, ARM::SPRRegisterClass);
Evan Cheng0a7baa22007-04-04 00:06:07 +00001833 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001834 return std::make_pair(0U, ARM::DPRRegisterClass);
1835 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001836 }
1837 }
1838 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1839}
1840
1841std::vector<unsigned> ARMTargetLowering::
1842getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001843 MVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00001844 if (Constraint.size() != 1)
1845 return std::vector<unsigned>();
1846
1847 switch (Constraint[0]) { // GCC ARM Constraint Letters
1848 default: break;
1849 case 'l':
1850 case 'r':
1851 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
1852 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
1853 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
1854 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001855 case 'w':
1856 if (VT == MVT::f32)
1857 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
1858 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
1859 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
1860 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
1861 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
1862 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
1863 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
1864 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
1865 if (VT == MVT::f64)
1866 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
1867 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
1868 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
1869 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
1870 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001871 }
1872
1873 return std::vector<unsigned>();
1874}