blob: fdcccffd877f04621c7cc165149ed7bfe98e62bc [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"
Bob Wilson1f595bb2009-04-17 19:07:39 +000025#include "llvm/Function.h"
Evan Cheng27707472007-03-16 08:43:56 +000026#include "llvm/Instruction.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000027#include "llvm/Intrinsics.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000028#include "llvm/GlobalValue.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000029#include "llvm/CodeGen/CallingConvLower.h"
Evan Chenga8e29892007-01-19 07:51:42 +000030#include "llvm/CodeGen/MachineBasicBlock.h"
31#include "llvm/CodeGen/MachineFrameInfo.h"
32#include "llvm/CodeGen/MachineFunction.h"
33#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000035#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000036#include "llvm/CodeGen/SelectionDAG.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000037#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000038#include "llvm/ADT/VectorExtras.h"
Evan Chengb01fad62007-03-12 23:30:29 +000039#include "llvm/Support/MathExtras.h"
Evan Chenga8e29892007-01-19 07:51:42 +000040using namespace llvm;
41
Bob Wilsondee46d72009-04-17 20:35:10 +000042static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000043 CCValAssign::LocInfo &LocInfo,
44 ISD::ArgFlagsTy &ArgFlags,
45 CCState &State);
Bob Wilsondee46d72009-04-17 20:35:10 +000046static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000047 CCValAssign::LocInfo &LocInfo,
48 ISD::ArgFlagsTy &ArgFlags,
49 CCState &State);
Bob Wilsondee46d72009-04-17 20:35:10 +000050static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000051 CCValAssign::LocInfo &LocInfo,
52 ISD::ArgFlagsTy &ArgFlags,
53 CCState &State);
Bob Wilsondee46d72009-04-17 20:35:10 +000054static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000055 CCValAssign::LocInfo &LocInfo,
56 ISD::ArgFlagsTy &ArgFlags,
57 CCState &State);
58
Evan Chenga8e29892007-01-19 07:51:42 +000059ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
60 : TargetLowering(TM), ARMPCLabelIndex(0) {
61 Subtarget = &TM.getSubtarget<ARMSubtarget>();
62
Evan Chengb1df8f22007-04-27 08:15:43 +000063 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +000064 // Uses VFP for Thumb libfuncs if available.
65 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
66 // Single-precision floating-point arithmetic.
67 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
68 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
69 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
70 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +000071
Evan Chengb1df8f22007-04-27 08:15:43 +000072 // Double-precision floating-point arithmetic.
73 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
74 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
75 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
76 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +000077
Evan Chengb1df8f22007-04-27 08:15:43 +000078 // Single-precision comparisons.
79 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
80 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
81 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
82 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
83 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
84 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
85 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
86 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +000087
Evan Chengb1df8f22007-04-27 08:15:43 +000088 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
89 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
90 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
91 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
92 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
93 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
94 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
95 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +000096
Evan Chengb1df8f22007-04-27 08:15:43 +000097 // Double-precision comparisons.
98 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
99 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
100 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
101 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
102 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
103 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
104 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
105 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000106
Evan Chengb1df8f22007-04-27 08:15:43 +0000107 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
108 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
109 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
110 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
111 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
112 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
113 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
114 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000115
Evan Chengb1df8f22007-04-27 08:15:43 +0000116 // Floating-point to integer conversions.
117 // i64 conversions are done via library routines even when generating VFP
118 // instructions, so use the same ones.
119 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
120 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
121 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
122 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000123
Evan Chengb1df8f22007-04-27 08:15:43 +0000124 // Conversions between floating types.
125 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
126 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
127
128 // Integer to floating-point conversions.
129 // i64 conversions are done via library routines even when generating VFP
130 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000131 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
132 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000133 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
134 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
135 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
136 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
137 }
Evan Chenga8e29892007-01-19 07:51:42 +0000138 }
139
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000140 if (Subtarget->isThumb())
141 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
142 else
143 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
Evan Chengb6ab2542007-01-31 08:40:13 +0000144 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000145 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
146 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000147
Chris Lattnerddf89562008-01-17 19:59:44 +0000148 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000149 }
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000150 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000151
152 // ARM does not have f32 extending load.
Evan Cheng03294662008-10-14 21:26:46 +0000153 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000154
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000155 // ARM does not have i1 sign extending load.
Evan Cheng03294662008-10-14 21:26:46 +0000156 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000157
Evan Chenga8e29892007-01-19 07:51:42 +0000158 // ARM supports all 4 flavors of integer indexed load / store.
159 for (unsigned im = (unsigned)ISD::PRE_INC;
160 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
161 setIndexedLoadAction(im, MVT::i1, Legal);
162 setIndexedLoadAction(im, MVT::i8, Legal);
163 setIndexedLoadAction(im, MVT::i16, Legal);
164 setIndexedLoadAction(im, MVT::i32, Legal);
165 setIndexedStoreAction(im, MVT::i1, Legal);
166 setIndexedStoreAction(im, MVT::i8, Legal);
167 setIndexedStoreAction(im, MVT::i16, Legal);
168 setIndexedStoreAction(im, MVT::i32, Legal);
169 }
170
171 // i64 operation support.
172 if (Subtarget->isThumb()) {
173 setOperationAction(ISD::MUL, MVT::i64, Expand);
174 setOperationAction(ISD::MULHU, MVT::i32, Expand);
175 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000176 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
177 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000178 } else {
Dan Gohman525178c2007-10-08 18:33:35 +0000179 setOperationAction(ISD::MUL, MVT::i64, Expand);
180 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000181 if (!Subtarget->hasV6Ops())
Dan Gohman525178c2007-10-08 18:33:35 +0000182 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000183 }
184 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
185 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
186 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
187 setOperationAction(ISD::SRL, MVT::i64, Custom);
188 setOperationAction(ISD::SRA, MVT::i64, Custom);
189
190 // ARM does not have ROTL.
191 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000192 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000193 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
Evan Chengb0636152007-02-01 23:34:03 +0000194 if (!Subtarget->hasV5TOps() || Subtarget->isThumb())
Evan Chenga8e29892007-01-19 07:51:42 +0000195 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
196
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000197 // Only ARMv6 has BSWAP.
198 if (!Subtarget->hasV6Ops())
Chris Lattner1719e132007-03-20 02:25:53 +0000199 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000200
Evan Chenga8e29892007-01-19 07:51:42 +0000201 // These are expanded into libcalls.
202 setOperationAction(ISD::SDIV, MVT::i32, Expand);
203 setOperationAction(ISD::UDIV, MVT::i32, Expand);
204 setOperationAction(ISD::SREM, MVT::i32, Expand);
205 setOperationAction(ISD::UREM, MVT::i32, Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000206 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
207 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000208
Evan Chenga8e29892007-01-19 07:51:42 +0000209 // Support label based line numbers.
Dan Gohman7f460202008-06-30 20:59:49 +0000210 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000211 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000212
213 setOperationAction(ISD::RET, MVT::Other, Custom);
214 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
215 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000216 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000217 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000218
Evan Chenga8e29892007-01-19 07:51:42 +0000219 // Use the default implementation.
Bob Wilson2dc4f542009-03-20 22:42:55 +0000220 setOperationAction(ISD::VASTART, MVT::Other, Custom);
221 setOperationAction(ISD::VAARG, MVT::Other, Expand);
222 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
223 setOperationAction(ISD::VAEND, MVT::Other, Expand);
224 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000225 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000226 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
227 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000228
229 if (!Subtarget->hasV6Ops()) {
230 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
231 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
232 }
233 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
234
Evan Chengb6ab2542007-01-31 08:40:13 +0000235 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb())
Evan Chengc7c77292008-11-04 19:57:48 +0000236 // Turn f64->i64 into FMRRD, i64 -> f64 to FMDRR iff target supports vfp2.
Evan Chenga8e29892007-01-19 07:51:42 +0000237 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000238
239 // We want to custom lower some of our intrinsics.
240 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
241
Bob Wilson2dc4f542009-03-20 22:42:55 +0000242 setOperationAction(ISD::SETCC, MVT::i32, Expand);
243 setOperationAction(ISD::SETCC, MVT::f32, Expand);
244 setOperationAction(ISD::SETCC, MVT::f64, Expand);
245 setOperationAction(ISD::SELECT, MVT::i32, Expand);
246 setOperationAction(ISD::SELECT, MVT::f32, Expand);
247 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000248 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
249 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
250 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
251
Bob Wilson2dc4f542009-03-20 22:42:55 +0000252 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
253 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
254 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
255 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
256 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000257
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000258 // We don't support sin/cos/fmod/copysign/pow
Bob Wilson2dc4f542009-03-20 22:42:55 +0000259 setOperationAction(ISD::FSIN, MVT::f64, Expand);
260 setOperationAction(ISD::FSIN, MVT::f32, Expand);
261 setOperationAction(ISD::FCOS, MVT::f32, Expand);
262 setOperationAction(ISD::FCOS, MVT::f64, Expand);
263 setOperationAction(ISD::FREM, MVT::f64, Expand);
264 setOperationAction(ISD::FREM, MVT::f32, Expand);
Evan Cheng110cf482008-04-01 01:50:16 +0000265 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) {
266 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
267 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
268 }
Bob Wilson2dc4f542009-03-20 22:42:55 +0000269 setOperationAction(ISD::FPOW, MVT::f64, Expand);
270 setOperationAction(ISD::FPOW, MVT::f32, Expand);
271
Evan Chenga8e29892007-01-19 07:51:42 +0000272 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
Evan Cheng110cf482008-04-01 01:50:16 +0000273 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) {
274 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
275 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
276 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
277 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
278 }
Evan Chenga8e29892007-01-19 07:51:42 +0000279
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000280 // We have target-specific dag combine patterns for the following nodes:
281 // ARMISD::FMRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000282 setTargetDAGCombine(ISD::ADD);
283 setTargetDAGCombine(ISD::SUB);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000284
Evan Chenga8e29892007-01-19 07:51:42 +0000285 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Chenga8e29892007-01-19 07:51:42 +0000286 setSchedulingPreference(SchedulingForRegPressure);
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000287 setIfCvtBlockSizeLimit(Subtarget->isThumb() ? 0 : 10);
Evan Cheng97e604e2007-06-19 23:55:02 +0000288 setIfCvtDupBlockSizeLimit(Subtarget->isThumb() ? 0 : 2);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000289
290 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Evan Chenga8e29892007-01-19 07:51:42 +0000291}
292
Evan Chenga8e29892007-01-19 07:51:42 +0000293const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
294 switch (Opcode) {
295 default: return 0;
296 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000297 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
298 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000299 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000300 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
301 case ARMISD::tCALL: return "ARMISD::tCALL";
302 case ARMISD::BRCOND: return "ARMISD::BRCOND";
303 case ARMISD::BR_JT: return "ARMISD::BR_JT";
304 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
305 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
306 case ARMISD::CMP: return "ARMISD::CMP";
Lauro Ramos Venancio99966632007-04-02 01:30:03 +0000307 case ARMISD::CMPNZ: return "ARMISD::CMPNZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000308 case ARMISD::CMPFP: return "ARMISD::CMPFP";
309 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
310 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
311 case ARMISD::CMOV: return "ARMISD::CMOV";
312 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000313
Evan Chenga8e29892007-01-19 07:51:42 +0000314 case ARMISD::FTOSI: return "ARMISD::FTOSI";
315 case ARMISD::FTOUI: return "ARMISD::FTOUI";
316 case ARMISD::SITOF: return "ARMISD::SITOF";
317 case ARMISD::UITOF: return "ARMISD::UITOF";
Evan Chenga8e29892007-01-19 07:51:42 +0000318
319 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
320 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
321 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000322
Evan Chenga8e29892007-01-19 07:51:42 +0000323 case ARMISD::FMRRD: return "ARMISD::FMRRD";
324 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000325
326 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Evan Chenga8e29892007-01-19 07:51:42 +0000327 }
328}
329
330//===----------------------------------------------------------------------===//
331// Lowering Code
332//===----------------------------------------------------------------------===//
333
Evan Chenga8e29892007-01-19 07:51:42 +0000334/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
335static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
336 switch (CC) {
337 default: assert(0 && "Unknown condition code!");
338 case ISD::SETNE: return ARMCC::NE;
339 case ISD::SETEQ: return ARMCC::EQ;
340 case ISD::SETGT: return ARMCC::GT;
341 case ISD::SETGE: return ARMCC::GE;
342 case ISD::SETLT: return ARMCC::LT;
343 case ISD::SETLE: return ARMCC::LE;
344 case ISD::SETUGT: return ARMCC::HI;
345 case ISD::SETUGE: return ARMCC::HS;
346 case ISD::SETULT: return ARMCC::LO;
347 case ISD::SETULE: return ARMCC::LS;
348 }
349}
350
351/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. It
352/// returns true if the operands should be inverted to form the proper
353/// comparison.
354static bool FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
355 ARMCC::CondCodes &CondCode2) {
356 bool Invert = false;
357 CondCode2 = ARMCC::AL;
358 switch (CC) {
359 default: assert(0 && "Unknown FP condition!");
360 case ISD::SETEQ:
361 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
362 case ISD::SETGT:
363 case ISD::SETOGT: CondCode = ARMCC::GT; break;
364 case ISD::SETGE:
365 case ISD::SETOGE: CondCode = ARMCC::GE; break;
366 case ISD::SETOLT: CondCode = ARMCC::MI; break;
367 case ISD::SETOLE: CondCode = ARMCC::GT; Invert = true; break;
368 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
369 case ISD::SETO: CondCode = ARMCC::VC; break;
370 case ISD::SETUO: CondCode = ARMCC::VS; break;
371 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
372 case ISD::SETUGT: CondCode = ARMCC::HI; break;
373 case ISD::SETUGE: CondCode = ARMCC::PL; break;
374 case ISD::SETLT:
375 case ISD::SETULT: CondCode = ARMCC::LT; break;
376 case ISD::SETLE:
377 case ISD::SETULE: CondCode = ARMCC::LE; break;
378 case ISD::SETNE:
379 case ISD::SETUNE: CondCode = ARMCC::NE; break;
380 }
381 return Invert;
382}
383
Bob Wilson1f595bb2009-04-17 19:07:39 +0000384//===----------------------------------------------------------------------===//
385// Calling Convention Implementation
386//
387// The lower operations present on calling convention works on this order:
388// LowerCALL (virt regs --> phys regs, virt regs --> stack)
389// LowerFORMAL_ARGUMENTS (phys --> virt regs, stack --> virt regs)
390// LowerRET (virt regs --> phys regs)
391// LowerCALL (phys regs --> virt regs)
392//
393//===----------------------------------------------------------------------===//
394
395#include "ARMGenCallingConv.inc"
396
397// APCS f64 is in register pairs, possibly split to stack
Bob Wilsondee46d72009-04-17 20:35:10 +0000398static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000399 CCValAssign::LocInfo &LocInfo,
400 ISD::ArgFlagsTy &ArgFlags,
401 CCState &State) {
402 static const unsigned HiRegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
403 static const unsigned LoRegList[] = { ARM::R1,
404 ARM::R2,
405 ARM::R3,
406 ARM::NoRegister };
407
Bob Wilsone65586b2009-04-17 20:40:45 +0000408 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 4);
409 if (Reg == 0)
410 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000411
Bob Wilsone65586b2009-04-17 20:40:45 +0000412 unsigned i;
413 for (i = 0; i < 4; ++i)
414 if (HiRegList[i] == Reg)
415 break;
416
417 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, MVT::i32, LocInfo));
418 if (LoRegList[i] != ARM::NoRegister)
419 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson1f595bb2009-04-17 19:07:39 +0000420 MVT::i32, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000421 else
422 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
423 State.AllocateStack(4, 4),
424 MVT::i32, LocInfo));
425 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000426}
427
428// AAPCS f64 is in aligned register pairs
Bob Wilsondee46d72009-04-17 20:35:10 +0000429static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000430 CCValAssign::LocInfo &LocInfo,
431 ISD::ArgFlagsTy &ArgFlags,
432 CCState &State) {
433 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
434 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
435
Bob Wilsone65586b2009-04-17 20:40:45 +0000436 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
437 if (Reg == 0)
438 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000439
Bob Wilsone65586b2009-04-17 20:40:45 +0000440 unsigned i;
441 for (i = 0; i < 2; ++i)
442 if (HiRegList[i] == Reg)
443 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000444
Bob Wilsone65586b2009-04-17 20:40:45 +0000445 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, MVT::i32, LocInfo));
446 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
447 MVT::i32, LocInfo));
448 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000449}
450
Bob Wilsondee46d72009-04-17 20:35:10 +0000451static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000452 CCValAssign::LocInfo &LocInfo,
453 ISD::ArgFlagsTy &ArgFlags,
454 CCState &State) {
455 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
456 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
457
Bob Wilsone65586b2009-04-17 20:40:45 +0000458 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
459 if (Reg == 0)
460 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000461
Bob Wilsone65586b2009-04-17 20:40:45 +0000462 unsigned i;
463 for (i = 0; i < 2; ++i)
464 if (HiRegList[i] == Reg)
465 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000466
Bob Wilsone65586b2009-04-17 20:40:45 +0000467 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, MVT::i32, LocInfo));
468 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
469 MVT::i32, LocInfo));
470 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000471}
472
Bob Wilsondee46d72009-04-17 20:35:10 +0000473static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000474 CCValAssign::LocInfo &LocInfo,
475 ISD::ArgFlagsTy &ArgFlags,
476 CCState &State) {
477 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
478 State);
479}
480
Bob Wilson1f595bb2009-04-17 19:07:39 +0000481/// LowerCallResult - Lower the result values of an ISD::CALL into the
482/// appropriate copies out of appropriate physical registers. This assumes that
483/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
484/// being lowered. The returns a SDNode with the same number of values as the
485/// ISD::CALL.
486SDNode *ARMTargetLowering::
487LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
488 unsigned CallingConv, SelectionDAG &DAG) {
489
490 DebugLoc dl = TheCall->getDebugLoc();
491 // Assign locations to each value returned by this call.
492 SmallVector<CCValAssign, 16> RVLocs;
493 bool isVarArg = TheCall->isVarArg();
494 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
495 CCInfo.AnalyzeCallResult(TheCall, RetCC_ARM);
496
497 SmallVector<SDValue, 8> ResultVals;
498
499 // Copy all of the result registers out of their specified physreg.
500 for (unsigned i = 0; i != RVLocs.size(); ++i) {
501 CCValAssign VA = RVLocs[i];
502
503 // handle f64 as custom
504 if (VA.needsCustom()) {
505 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
506 InFlag);
507 VA = RVLocs[++i]; // skip ahead to next loc
508 SDValue Hi = DAG.getCopyFromReg(Lo, dl, VA.getLocReg(), VA.getLocVT(),
509 Lo.getValue(2));
510 ResultVals.push_back(DAG.getNode(ARMISD::FMDRR, dl, VA.getValVT(), Lo,
511 Hi));
512 } else {
513 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
514 InFlag).getValue(1);
515 SDValue Val = Chain.getValue(0);
516 InFlag = Chain.getValue(2);
517
518 switch (VA.getLocInfo()) {
519 default: assert(0 && "Unknown loc info!");
520 case CCValAssign::Full: break;
521 case CCValAssign::BCvt:
522 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(),
523 Chain.getValue(0));
524 break;
525 }
526
527 ResultVals.push_back(Val);
528 }
529 }
530
531 // Merge everything together with a MERGE_VALUES node.
532 ResultVals.push_back(Chain);
533 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
534 &ResultVals[0], ResultVals.size()).getNode();
535}
536
537/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
538/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000539/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000540/// a byval function parameter.
541/// Sometimes what we are copying is the end of a larger object, the part that
542/// does not fit in registers.
543static SDValue
544CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
545 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
546 DebugLoc dl) {
547 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
548 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
549 /*AlwaysInline=*/false, NULL, 0, NULL, 0);
550}
551
Bob Wilsondee46d72009-04-17 20:35:10 +0000552/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000553SDValue
554ARMTargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
555 const SDValue &StackPtr,
Bob Wilsondee46d72009-04-17 20:35:10 +0000556 const CCValAssign &VA, SDValue Chain,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000557 SDValue Arg, ISD::ArgFlagsTy Flags) {
558 DebugLoc dl = TheCall->getDebugLoc();
559 unsigned LocMemOffset = VA.getLocMemOffset();
560 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
561 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
562 if (Flags.isByVal()) {
563 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
564 }
565 return DAG.getStore(Chain, dl, Arg, PtrOff,
566 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chenga8e29892007-01-19 07:51:42 +0000567}
568
Evan Chengfc403422007-02-03 08:53:01 +0000569/// LowerCALL - Lowering a ISD::CALL node into a callseq_start <-
570/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
571/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000572SDValue ARMTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Dan Gohman095cc292008-09-13 01:54:27 +0000573 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
Bob Wilson1f595bb2009-04-17 19:07:39 +0000574 MVT RetVT = TheCall->getRetValType(0);
575 SDValue Chain = TheCall->getChain();
576 unsigned CC = TheCall->getCallingConv();
577 assert((CC == CallingConv::C ||
578 CC == CallingConv::Fast) && "unknown calling convention");
579 bool isVarArg = TheCall->isVarArg();
580 SDValue Callee = TheCall->getCallee();
581 DebugLoc dl = TheCall->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +0000582
Bob Wilson1f595bb2009-04-17 19:07:39 +0000583 // Analyze operands of the call, assigning locations to each operand.
584 SmallVector<CCValAssign, 16> ArgLocs;
585 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
586 CCInfo.AnalyzeCallOperands(TheCall, CC_ARM);
Evan Chenga8e29892007-01-19 07:51:42 +0000587
Bob Wilson1f595bb2009-04-17 19:07:39 +0000588 // Get a count of how many bytes are to be pushed on the stack.
589 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000590
591 // Adjust the stack pointer for the new arguments...
592 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000593 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000594
Dan Gohman475871a2008-07-27 21:46:04 +0000595 SDValue StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000596
Bob Wilson1f595bb2009-04-17 19:07:39 +0000597 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
598 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000599
Bob Wilson1f595bb2009-04-17 19:07:39 +0000600 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +0000601 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000602 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
603 i != e;
604 ++i, ++realArgIdx) {
605 CCValAssign &VA = ArgLocs[i];
606 SDValue Arg = TheCall->getArg(realArgIdx);
607 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(realArgIdx);
Evan Chenga8e29892007-01-19 07:51:42 +0000608
Bob Wilson1f595bb2009-04-17 19:07:39 +0000609 // Promote the value if needed.
610 switch (VA.getLocInfo()) {
611 default: assert(0 && "Unknown loc info!");
612 case CCValAssign::Full: break;
613 case CCValAssign::SExt:
614 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
615 break;
616 case CCValAssign::ZExt:
617 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
618 break;
619 case CCValAssign::AExt:
620 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
621 break;
622 case CCValAssign::BCvt:
623 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
624 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000625 }
626
Bob Wilson1f595bb2009-04-17 19:07:39 +0000627 // f64 is passed in i32 pairs and must be combined
628 if (VA.needsCustom()) {
629 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
630 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
631 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
632 VA = ArgLocs[++i]; // skip ahead to next loc
633 if (VA.isRegLoc())
Bob Wilsondee46d72009-04-17 20:35:10 +0000634 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(1)));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000635 else {
636 assert(VA.isMemLoc());
637 if (StackPtr.getNode() == 0)
638 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
639
640 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
641 Chain, fmrrd.getValue(1),
642 Flags));
643 }
644 } else if (VA.isRegLoc()) {
645 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
646 } else {
647 assert(VA.isMemLoc());
648 if (StackPtr.getNode() == 0)
649 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
650
651 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
652 Chain, Arg, Flags));
653 }
Evan Chenga8e29892007-01-19 07:51:42 +0000654 }
655
656 if (!MemOpChains.empty())
Dale Johannesen33c960f2009-02-04 20:06:27 +0000657 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +0000658 &MemOpChains[0], MemOpChains.size());
659
660 // Build a sequence of copy-to-reg nodes chained together with token chain
661 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +0000662 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +0000663 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +0000664 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000665 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000666 InFlag = Chain.getValue(1);
667 }
668
Bill Wendling056292f2008-09-16 21:48:12 +0000669 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
670 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
671 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +0000672 bool isDirect = false;
673 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +0000674 bool isLocalARMFunc = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000675 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
676 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +0000677 isDirect = true;
Reid Spencer5cbf9852007-01-30 20:08:39 +0000678 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
Evan Chenga8e29892007-01-19 07:51:42 +0000679 GV->hasLinkOnceLinkage());
Evan Cheng970a4192007-01-19 19:28:01 +0000680 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +0000681 getTargetMachine().getRelocationModel() != Reloc::Static;
682 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +0000683 // ARM call to a local ARM function is predicable.
684 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +0000685 // tBX takes a register source operand.
686 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
687 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
688 ARMCP::CPStub, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000689 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000690 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000691 Callee = DAG.getLoad(getPointerTy(), dl,
692 DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000693 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000694 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000695 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +0000696 } else
697 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +0000698 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000699 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +0000700 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000701 getTargetMachine().getRelocationModel() != Reloc::Static;
702 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +0000703 // tBX takes a register source operand.
704 const char *Sym = S->getSymbol();
705 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
706 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(Sym, ARMPCLabelIndex,
707 ARMCP::CPStub, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000708 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000709 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000710 Callee = DAG.getLoad(getPointerTy(), dl,
Bob Wilson2dc4f542009-03-20 22:42:55 +0000711 DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000712 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000713 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000714 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +0000715 } else
Bill Wendling056292f2008-09-16 21:48:12 +0000716 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000717 }
718
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000719 // FIXME: handle tail calls differently.
720 unsigned CallOpc;
721 if (Subtarget->isThumb()) {
722 if (!Subtarget->hasV5TOps() && (!isDirect || isARMFunc))
723 CallOpc = ARMISD::CALL_NOLINK;
724 else
725 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
726 } else {
727 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +0000728 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
729 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000730 }
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +0000731 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb()) {
732 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Dale Johannesene8d72302009-02-06 23:05:02 +0000733 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000734 InFlag = Chain.getValue(1);
735 }
736
Dan Gohman475871a2008-07-27 21:46:04 +0000737 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +0000738 Ops.push_back(Chain);
739 Ops.push_back(Callee);
740
741 // Add argument registers to the end of the list so that they are known live
742 // into the call.
743 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
744 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
745 RegsToPass[i].second.getValueType()));
746
Gabor Greifba36cb52008-08-28 21:40:38 +0000747 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +0000748 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +0000749 // Returns a chain and a flag for retval copy to use.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000750 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +0000751 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +0000752 InFlag = Chain.getValue(1);
753
Chris Lattnere563bbc2008-10-11 22:08:30 +0000754 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
755 DAG.getIntPtrConstant(0, true), InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000756 if (RetVT != MVT::Other)
757 InFlag = Chain.getValue(1);
758
Bob Wilson1f595bb2009-04-17 19:07:39 +0000759 // Handle result values, copying them out of physregs into vregs that we
760 // return.
761 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
762 Op.getResNo());
Evan Chenga8e29892007-01-19 07:51:42 +0000763}
764
Bob Wilson1f595bb2009-04-17 19:07:39 +0000765SDValue ARMTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
766 // The chain is always operand #0
Dan Gohman475871a2008-07-27 21:46:04 +0000767 SDValue Chain = Op.getOperand(0);
Dale Johannesena05dca42009-02-04 23:02:30 +0000768 DebugLoc dl = Op.getDebugLoc();
Bob Wilson2dc4f542009-03-20 22:42:55 +0000769
Bob Wilsondee46d72009-04-17 20:35:10 +0000770 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000771 SmallVector<CCValAssign, 16> RVLocs;
772 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
773 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
774
Bob Wilsondee46d72009-04-17 20:35:10 +0000775 // CCState - Info about the registers and stack slots.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000776 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
777
Bob Wilsondee46d72009-04-17 20:35:10 +0000778 // Analyze return values of ISD::RET.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000779 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_ARM);
780
781 // If this is the first return lowered for this function, add
782 // the regs to the liveout set for the function.
783 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
784 for (unsigned i = 0; i != RVLocs.size(); ++i)
785 if (RVLocs[i].isRegLoc())
786 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +0000787 }
788
Bob Wilson1f595bb2009-04-17 19:07:39 +0000789 SDValue Flag;
790
791 // Copy the result values into the output registers.
792 for (unsigned i = 0, realRVLocIdx = 0;
793 i != RVLocs.size();
794 ++i, ++realRVLocIdx) {
795 CCValAssign &VA = RVLocs[i];
796 assert(VA.isRegLoc() && "Can only return in registers!");
797
798 // ISD::RET => ret chain, (regnum1,val1), ...
799 // So i*2+1 index only the regnums
800 SDValue Arg = Op.getOperand(realRVLocIdx*2+1);
801
802 switch (VA.getLocInfo()) {
803 default: assert(0 && "Unknown loc info!");
804 case CCValAssign::Full: break;
805 case CCValAssign::BCvt:
806 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
807 break;
808 }
809
810 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
811 // available.
812 if (VA.needsCustom()) {
813 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
814 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
815 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
816 VA = RVLocs[++i]; // skip ahead to next loc
817 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
818 Flag);
819 } else
820 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
821
Bob Wilsondee46d72009-04-17 20:35:10 +0000822 // Guarantee that all emitted copies are
823 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000824 Flag = Chain.getValue(1);
825 }
826
827 SDValue result;
828 if (Flag.getNode())
829 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
830 else // Return Void
831 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
832
833 return result;
Evan Chenga8e29892007-01-19 07:51:42 +0000834}
835
Bob Wilson2dc4f542009-03-20 22:42:55 +0000836// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Bill Wendling056292f2008-09-16 21:48:12 +0000837// their target countpart wrapped in the ARMISD::Wrapper node. Suppose N is
838// one of the above mentioned nodes. It has to be wrapped because otherwise
839// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
840// be used to form addressing mode. These wrapped nodes will be selected
841// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +0000842static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000843 MVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000844 // FIXME there is no actual debug info here
845 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +0000846 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +0000847 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +0000848 if (CP->isMachineConstantPoolEntry())
849 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
850 CP->getAlignment());
851 else
852 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
853 CP->getAlignment());
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000854 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +0000855}
856
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000857// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +0000858SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000859ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
860 SelectionDAG &DAG) {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000861 DebugLoc dl = GA->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000862 MVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000863 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
864 ARMConstantPoolValue *CPV =
865 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
866 PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000867 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000868 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000869 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000870 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000871
Dan Gohman475871a2008-07-27 21:46:04 +0000872 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000873 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000874
875 // call __tls_get_addr.
876 ArgListTy Args;
877 ArgListEntry Entry;
878 Entry.Node = Argument;
879 Entry.Ty = (const Type *) Type::Int32Ty;
880 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000881 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +0000882 std::pair<SDValue, SDValue> CallResult =
Dale Johannesen86098bd2008-09-26 19:31:26 +0000883 LowerCallTo(Chain, (const Type *) Type::Int32Ty, false, false, false, false,
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000884 CallingConv::C, false,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000885 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000886 return CallResult.first;
887}
888
889// Lower ISD::GlobalTLSAddress using the "initial exec" or
890// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +0000891SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000892ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Bob Wilson2dc4f542009-03-20 22:42:55 +0000893 SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000894 GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000895 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +0000896 SDValue Offset;
897 SDValue Chain = DAG.getEntryNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000898 MVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000899 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +0000900 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000901
902 if (GV->isDeclaration()){
903 // initial exec model
904 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
905 ARMConstantPoolValue *CPV =
906 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
907 PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000908 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000909 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000910 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000911 Chain = Offset.getValue(1);
912
Dan Gohman475871a2008-07-27 21:46:04 +0000913 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000914 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000915
Dale Johannesen33c960f2009-02-04 20:06:27 +0000916 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000917 } else {
918 // local exec model
919 ARMConstantPoolValue *CPV =
920 new ARMConstantPoolValue(GV, ARMCP::CPValue, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +0000921 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000922 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000923 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000924 }
925
926 // The address of the thread local variable is the add of the thread
927 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000928 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000929}
930
Dan Gohman475871a2008-07-27 21:46:04 +0000931SDValue
932ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000933 // TODO: implement the "local dynamic" model
934 assert(Subtarget->isTargetELF() &&
935 "TLS not implemented for non-ELF targets");
936 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
937 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
938 // otherwise use the "Local Exec" TLS Model
939 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
940 return LowerToTLSGeneralDynamicModel(GA, DAG);
941 else
942 return LowerToTLSExecModels(GA, DAG);
943}
944
Dan Gohman475871a2008-07-27 21:46:04 +0000945SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +0000946 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000947 MVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000948 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000949 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
950 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
951 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000952 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000953 ARMConstantPoolValue *CPV =
954 new ARMConstantPoolValue(GV, ARMCP::CPValue, UseGOTOFF ? "GOTOFF":"GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +0000955 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000956 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000957 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Dale Johannesen33c960f2009-02-04 20:06:27 +0000958 CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000959 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000960 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000961 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000962 if (!UseGOTOFF)
Dale Johannesen33c960f2009-02-04 20:06:27 +0000963 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000964 return Result;
965 } else {
Evan Cheng1606e8e2009-03-13 07:51:59 +0000966 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000967 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000968 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000969 }
970}
971
Evan Chenga8e29892007-01-19 07:51:42 +0000972/// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol
Evan Cheng97c9bb52007-05-04 00:26:58 +0000973/// even in non-static mode.
974static bool GVIsIndirectSymbol(GlobalValue *GV, Reloc::Model RelocM) {
Evan Chengae94e592008-12-05 01:06:39 +0000975 // If symbol visibility is hidden, the extra load is not needed if
976 // the symbol is definitely defined in the current translation unit.
977 bool isDecl = GV->isDeclaration() && !GV->hasNotBeenReadFromBitcode();
978 if (GV->hasHiddenVisibility() && (!isDecl && !GV->hasCommonLinkage()))
979 return false;
Duncan Sands667d4b82009-03-07 15:45:40 +0000980 return RelocM != Reloc::Static && (isDecl || GV->isWeakForLinker());
Evan Chenga8e29892007-01-19 07:51:42 +0000981}
982
Dan Gohman475871a2008-07-27 21:46:04 +0000983SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +0000984 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000985 MVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000986 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +0000987 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
988 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng97c9bb52007-05-04 00:26:58 +0000989 bool IsIndirect = GVIsIndirectSymbol(GV, RelocM);
Dan Gohman475871a2008-07-27 21:46:04 +0000990 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +0000991 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +0000992 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +0000993 else {
994 unsigned PCAdj = (RelocM != Reloc::PIC_)
995 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Chengc60e76d2007-01-30 20:37:08 +0000996 ARMCP::ARMCPKind Kind = IsIndirect ? ARMCP::CPNonLazyPtr
997 : ARMCP::CPValue;
Evan Chenga8e29892007-01-19 07:51:42 +0000998 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
Evan Chengc60e76d2007-01-30 20:37:08 +0000999 Kind, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001000 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001001 }
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001002 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001003
Dale Johannesen33c960f2009-02-04 20:06:27 +00001004 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001005 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001006
1007 if (RelocM == Reloc::PIC_) {
Dan Gohman475871a2008-07-27 21:46:04 +00001008 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001009 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001010 }
1011 if (IsIndirect)
Dale Johannesen33c960f2009-02-04 20:06:27 +00001012 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001013
1014 return Result;
1015}
1016
Dan Gohman475871a2008-07-27 21:46:04 +00001017SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001018 SelectionDAG &DAG){
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001019 assert(Subtarget->isTargetELF() &&
1020 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Duncan Sands83ec4b62008-06-06 12:08:01 +00001021 MVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001022 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001023 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
1024 ARMConstantPoolValue *CPV = new ARMConstantPoolValue("_GLOBAL_OFFSET_TABLE_",
1025 ARMPCLabelIndex,
1026 ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001027 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001028 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001029 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001030 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001031 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001032}
1033
Dan Gohman475871a2008-07-27 21:46:04 +00001034static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001035 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001036 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001037 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001038 default: return SDValue(); // Don't custom lower most intrinsics.
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001039 case Intrinsic::arm_thread_pointer:
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001040 return DAG.getNode(ARMISD::THREAD_POINTER, DebugLoc::getUnknownLoc(),
1041 PtrVT);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001042 }
1043}
1044
Dan Gohman475871a2008-07-27 21:46:04 +00001045static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001046 unsigned VarArgsFrameIndex) {
Evan Chenga8e29892007-01-19 07:51:42 +00001047 // vastart just stores the address of the VarArgsFrameIndex slot into the
1048 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001049 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001050 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001051 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001052 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001053 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001054}
1055
Bob Wilsone3fa9ef2009-04-17 20:42:34 +00001056/// AddLiveIn - This helper function adds the specified physical register to the
1057/// MachineFunction as a live-in value. It also creates a corresponding virtual
1058/// register for it.
1059static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1060 const TargetRegisterClass *RC) {
1061 assert(RC->contains(PReg) && "Not the correct regclass!");
1062 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1063 MF.getRegInfo().addLiveIn(PReg, VReg);
1064 return VReg;
1065}
1066
Dan Gohman475871a2008-07-27 21:46:04 +00001067SDValue
1068ARMTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001069 MachineFunction &MF = DAG.getMachineFunction();
1070 MachineFrameInfo *MFI = MF.getFrameInfo();
1071
Dan Gohman475871a2008-07-27 21:46:04 +00001072 SDValue Root = Op.getOperand(0);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001073 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001074 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001075 unsigned CC = MF.getFunction()->getCallingConv();
1076 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1077
1078 // Assign locations to all of the incoming arguments.
1079 SmallVector<CCValAssign, 16> ArgLocs;
1080 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1081 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_ARM);
1082
1083 SmallVector<SDValue, 16> ArgValues;
1084
1085 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1086 CCValAssign &VA = ArgLocs[i];
1087
Bob Wilsondee46d72009-04-17 20:35:10 +00001088 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001089 if (VA.isRegLoc()) {
1090 MVT RegVT = VA.getLocVT();
1091 TargetRegisterClass *RC;
1092 if (AFI->isThumbFunction())
1093 RC = ARM::tGPRRegisterClass;
1094 else
1095 RC = ARM::GPRRegisterClass;
1096
1097 if (RegVT == MVT::f64) {
Bob Wilsondee46d72009-04-17 20:35:10 +00001098 // f64 is passed in pairs of GPRs and must be combined.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001099 RegVT = MVT::i32;
1100 } else if (!((RegVT == MVT::i32) || (RegVT == MVT::f32)))
1101 assert(0 && "RegVT not supported by FORMAL_ARGUMENTS Lowering");
1102
Bob Wilsondee46d72009-04-17 20:35:10 +00001103 // Transform the arguments stored in physical registers into virtual ones.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001104 unsigned Reg = AddLiveIn(MF, VA.getLocReg(), RC);
1105 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, RegVT);
1106
Bob Wilsondee46d72009-04-17 20:35:10 +00001107 // f64 is passed in i32 pairs and must be combined.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001108 if (VA.needsCustom()) {
1109 SDValue ArgValue2;
1110
1111 VA = ArgLocs[++i]; // skip ahead to next loc
1112 if (VA.isMemLoc()) {
1113 // must be APCS and older than V5T to split like this
1114 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1115 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset());
1116
Bob Wilsondee46d72009-04-17 20:35:10 +00001117 // Create load node to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001118 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1119 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN, NULL, 0);
1120 } else {
1121 Reg = AddLiveIn(MF, VA.getLocReg(), RC);
1122 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
1123 }
1124
1125 ArgValue = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64,
1126 ArgValue, ArgValue2);
1127 }
1128
1129 // If this is an 8 or 16-bit value, it is really passed promoted
1130 // to 32 bits. Insert an assert[sz]ext to capture this, then
1131 // truncate to the right size.
1132 switch (VA.getLocInfo()) {
1133 default: assert(0 && "Unknown loc info!");
1134 case CCValAssign::Full: break;
1135 case CCValAssign::BCvt:
1136 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1137 break;
1138 case CCValAssign::SExt:
1139 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1140 DAG.getValueType(VA.getValVT()));
1141 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1142 break;
1143 case CCValAssign::ZExt:
1144 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1145 DAG.getValueType(VA.getValVT()));
1146 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1147 break;
1148 }
1149
1150 ArgValues.push_back(ArgValue);
1151
1152 } else { // VA.isRegLoc()
1153
1154 // sanity check
1155 assert(VA.isMemLoc());
1156 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
1157
1158 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1159 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset());
1160
Bob Wilsondee46d72009-04-17 20:35:10 +00001161 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001162 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1163 ArgValues.push_back(DAG.getLoad(VA.getValVT(), dl, Root, FIN, NULL, 0));
1164 }
1165 }
1166
1167 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001168 if (isVarArg) {
1169 static const unsigned GPRArgRegs[] = {
1170 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1171 };
1172
Bob Wilsondee46d72009-04-17 20:35:10 +00001173 unsigned NumGPRs = CCInfo.getFirstUnallocated
1174 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001175
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001176 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1177 unsigned VARegSize = (4 - NumGPRs) * 4;
1178 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001179 unsigned ArgOffset = 0;
Evan Chenga8e29892007-01-19 07:51:42 +00001180 if (VARegSaveSize) {
1181 // If this function is vararg, store any remaining integer argument regs
1182 // to their spots on the stack so that they may be loaded by deferencing
1183 // the result of va_next.
1184 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001185 ArgOffset = CCInfo.getNextStackOffset();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001186 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1187 VARegSaveSize - VARegSize);
Dan Gohman475871a2008-07-27 21:46:04 +00001188 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001189
Dan Gohman475871a2008-07-27 21:46:04 +00001190 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001191 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001192 TargetRegisterClass *RC;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001193 if (AFI->isThumbFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001194 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001195 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001196 RC = ARM::GPRRegisterClass;
1197
1198 unsigned VReg = AddLiveIn(MF, GPRArgRegs[NumGPRs], RC);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001199 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::i32);
1200 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001201 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001202 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001203 DAG.getConstant(4, getPointerTy()));
1204 }
1205 if (!MemOps.empty())
Dale Johannesen33c960f2009-02-04 20:06:27 +00001206 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001207 &MemOps[0], MemOps.size());
1208 } else
1209 // This will point to the next argument passed via stack.
1210 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1211 }
1212
1213 ArgValues.push_back(Root);
1214
1215 // Return the new list of results.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001216 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Bob Wilson1f595bb2009-04-17 19:07:39 +00001217 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Evan Chenga8e29892007-01-19 07:51:42 +00001218}
1219
1220/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001221static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001222 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001223 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001224 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001225 // Maybe this has already been legalized into the constant pool?
1226 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001227 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001228 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1229 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001230 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001231 }
1232 }
1233 return false;
1234}
1235
Evan Cheng9a2ef952007-02-02 01:53:26 +00001236static bool isLegalCmpImmediate(unsigned C, bool isThumb) {
Evan Chenga8e29892007-01-19 07:51:42 +00001237 return ( isThumb && (C & ~255U) == 0) ||
1238 (!isThumb && ARM_AM::getSOImmVal(C) != -1);
1239}
1240
1241/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1242/// the given operands.
Dan Gohman475871a2008-07-27 21:46:04 +00001243static SDValue getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Dale Johannesende064702009-02-06 21:50:26 +00001244 SDValue &ARMCC, SelectionDAG &DAG, bool isThumb,
1245 DebugLoc dl) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001246 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001247 unsigned C = RHSC->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001248 if (!isLegalCmpImmediate(C, isThumb)) {
1249 // Constant does not fit, try adjusting it by one?
1250 switch (CC) {
1251 default: break;
1252 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001253 case ISD::SETGE:
Evan Chenga8e29892007-01-19 07:51:42 +00001254 if (isLegalCmpImmediate(C-1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001255 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
1256 RHS = DAG.getConstant(C-1, MVT::i32);
1257 }
1258 break;
1259 case ISD::SETULT:
1260 case ISD::SETUGE:
1261 if (C > 0 && isLegalCmpImmediate(C-1, isThumb)) {
1262 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Evan Chenga8e29892007-01-19 07:51:42 +00001263 RHS = DAG.getConstant(C-1, MVT::i32);
1264 }
1265 break;
1266 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001267 case ISD::SETGT:
Evan Chenga8e29892007-01-19 07:51:42 +00001268 if (isLegalCmpImmediate(C+1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001269 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
1270 RHS = DAG.getConstant(C+1, MVT::i32);
1271 }
1272 break;
1273 case ISD::SETULE:
1274 case ISD::SETUGT:
1275 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb)) {
1276 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Evan Chenga8e29892007-01-19 07:51:42 +00001277 RHS = DAG.getConstant(C+1, MVT::i32);
1278 }
1279 break;
1280 }
1281 }
1282 }
1283
1284 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001285 ARMISD::NodeType CompareType;
1286 switch (CondCode) {
1287 default:
1288 CompareType = ARMISD::CMP;
1289 break;
1290 case ARMCC::EQ:
1291 case ARMCC::NE:
1292 case ARMCC::MI:
1293 case ARMCC::PL:
1294 // Uses only N and Z Flags
1295 CompareType = ARMISD::CMPNZ;
1296 break;
1297 }
Evan Chenga8e29892007-01-19 07:51:42 +00001298 ARMCC = DAG.getConstant(CondCode, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001299 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001300}
1301
1302/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001303static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001304 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001305 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001306 if (!isFloatingPointZero(RHS))
Dale Johannesende064702009-02-06 21:50:26 +00001307 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001308 else
Dale Johannesende064702009-02-06 21:50:26 +00001309 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1310 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001311}
1312
Dan Gohman475871a2008-07-27 21:46:04 +00001313static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001314 const ARMSubtarget *ST) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001315 MVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001316 SDValue LHS = Op.getOperand(0);
1317 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001318 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001319 SDValue TrueVal = Op.getOperand(2);
1320 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001321 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001322
1323 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001324 SDValue ARMCC;
1325 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001326 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb(), dl);
1327 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001328 }
1329
1330 ARMCC::CondCodes CondCode, CondCode2;
1331 if (FPCCToARMCC(CC, CondCode, CondCode2))
1332 std::swap(TrueVal, FalseVal);
1333
Dan Gohman475871a2008-07-27 21:46:04 +00001334 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1335 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001336 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1337 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001338 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001339 if (CondCode2 != ARMCC::AL) {
Dan Gohman475871a2008-07-27 21:46:04 +00001340 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001341 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00001342 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001343 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00001344 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001345 }
1346 return Result;
1347}
1348
Dan Gohman475871a2008-07-27 21:46:04 +00001349static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001350 const ARMSubtarget *ST) {
Dan Gohman475871a2008-07-27 21:46:04 +00001351 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001352 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001353 SDValue LHS = Op.getOperand(2);
1354 SDValue RHS = Op.getOperand(3);
1355 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00001356 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001357
1358 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001359 SDValue ARMCC;
1360 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001361 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb(), dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001362 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00001363 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001364 }
1365
1366 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
1367 ARMCC::CondCodes CondCode, CondCode2;
1368 if (FPCCToARMCC(CC, CondCode, CondCode2))
1369 // Swap the LHS/RHS of the comparison if needed.
1370 std::swap(LHS, RHS);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001371
Dale Johannesende064702009-02-06 21:50:26 +00001372 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Dan Gohman475871a2008-07-27 21:46:04 +00001373 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1374 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001375 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001376 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00001377 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001378 if (CondCode2 != ARMCC::AL) {
1379 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001380 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00001381 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001382 }
1383 return Res;
1384}
1385
Dan Gohman475871a2008-07-27 21:46:04 +00001386SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) {
1387 SDValue Chain = Op.getOperand(0);
1388 SDValue Table = Op.getOperand(1);
1389 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001390 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001391
Duncan Sands83ec4b62008-06-06 12:08:01 +00001392 MVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00001393 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1394 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Dan Gohman475871a2008-07-27 21:46:04 +00001395 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
1396 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Dale Johannesende064702009-02-06 21:50:26 +00001397 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001398 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
1399 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chenga8e29892007-01-19 07:51:42 +00001400 bool isPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001401 Addr = DAG.getLoad(isPIC ? (MVT)MVT::i32 : PTy, dl,
Evan Chenge2446c62007-06-26 18:31:22 +00001402 Chain, Addr, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001403 Chain = Addr.getValue(1);
1404 if (isPIC)
Dale Johannesen33c960f2009-02-04 20:06:27 +00001405 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
1406 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chenga8e29892007-01-19 07:51:42 +00001407}
1408
Dan Gohman475871a2008-07-27 21:46:04 +00001409static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00001410 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001411 unsigned Opc =
1412 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
Dale Johannesende064702009-02-06 21:50:26 +00001413 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
1414 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001415}
1416
Dan Gohman475871a2008-07-27 21:46:04 +00001417static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001418 MVT VT = Op.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001419 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001420 unsigned Opc =
1421 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1422
Dale Johannesende064702009-02-06 21:50:26 +00001423 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
1424 return DAG.getNode(Opc, dl, VT, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001425}
1426
Dan Gohman475871a2008-07-27 21:46:04 +00001427static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001428 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00001429 SDValue Tmp0 = Op.getOperand(0);
1430 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00001431 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001432 MVT VT = Op.getValueType();
1433 MVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001434 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
1435 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Dan Gohman475871a2008-07-27 21:46:04 +00001436 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1437 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001438 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001439}
1440
Dan Gohman475871a2008-07-27 21:46:04 +00001441SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00001442ARMTargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001443 SDValue Chain,
1444 SDValue Dst, SDValue Src,
1445 SDValue Size, unsigned Align,
Dan Gohman707e0182008-04-12 04:36:06 +00001446 bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00001447 const Value *DstSV, uint64_t DstSVOff,
1448 const Value *SrcSV, uint64_t SrcSVOff){
Evan Cheng4102eb52007-10-22 22:11:27 +00001449 // Do repeated 4-byte loads and stores. To be improved.
Dan Gohman707e0182008-04-12 04:36:06 +00001450 // This requires 4-byte alignment.
1451 if ((Align & 3) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00001452 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001453 // This requires the copy size to be a constant, preferrably
1454 // within a subtarget-specific limit.
1455 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
1456 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00001457 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001458 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001459 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00001460 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001461
1462 unsigned BytesLeft = SizeVal & 3;
1463 unsigned NumMemOps = SizeVal >> 2;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001464 unsigned EmittedNumMemOps = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001465 MVT VT = MVT::i32;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001466 unsigned VTSize = 4;
Evan Cheng4102eb52007-10-22 22:11:27 +00001467 unsigned i = 0;
Evan Chenge5e7ce42007-05-18 01:19:57 +00001468 const unsigned MAX_LOADS_IN_LDM = 6;
Dan Gohman475871a2008-07-27 21:46:04 +00001469 SDValue TFOps[MAX_LOADS_IN_LDM];
1470 SDValue Loads[MAX_LOADS_IN_LDM];
Dan Gohman1f13c682008-04-28 17:15:20 +00001471 uint64_t SrcOff = 0, DstOff = 0;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001472
Evan Cheng4102eb52007-10-22 22:11:27 +00001473 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
1474 // same number of stores. The loads and stores will get combined into
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001475 // ldm/stm later on.
Evan Cheng4102eb52007-10-22 22:11:27 +00001476 while (EmittedNumMemOps < NumMemOps) {
1477 for (i = 0;
1478 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001479 Loads[i] = DAG.getLoad(VT, dl, Chain,
1480 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
Evan Cheng4102eb52007-10-22 22:11:27 +00001481 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001482 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001483 TFOps[i] = Loads[i].getValue(1);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001484 SrcOff += VTSize;
1485 }
Dale Johannesen0f502f62009-02-03 22:26:09 +00001486 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001487
Evan Cheng4102eb52007-10-22 22:11:27 +00001488 for (i = 0;
1489 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001490 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Bob Wilson2dc4f542009-03-20 22:42:55 +00001491 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
Evan Cheng4102eb52007-10-22 22:11:27 +00001492 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001493 DstSV, DstSVOff + DstOff);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001494 DstOff += VTSize;
1495 }
Dale Johannesen0f502f62009-02-03 22:26:09 +00001496 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00001497
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001498 EmittedNumMemOps += i;
1499 }
1500
Bob Wilson2dc4f542009-03-20 22:42:55 +00001501 if (BytesLeft == 0)
Evan Cheng4102eb52007-10-22 22:11:27 +00001502 return Chain;
1503
1504 // Issue loads / stores for the trailing (1 - 3) bytes.
1505 unsigned BytesLeftSave = BytesLeft;
1506 i = 0;
1507 while (BytesLeft) {
1508 if (BytesLeft >= 2) {
1509 VT = MVT::i16;
1510 VTSize = 2;
1511 } else {
1512 VT = MVT::i8;
1513 VTSize = 1;
1514 }
1515
Dale Johannesen0f502f62009-02-03 22:26:09 +00001516 Loads[i] = DAG.getLoad(VT, dl, Chain,
1517 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
Evan Cheng4102eb52007-10-22 22:11:27 +00001518 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001519 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001520 TFOps[i] = Loads[i].getValue(1);
1521 ++i;
1522 SrcOff += VTSize;
1523 BytesLeft -= VTSize;
1524 }
Dale Johannesen0f502f62009-02-03 22:26:09 +00001525 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00001526
1527 i = 0;
1528 BytesLeft = BytesLeftSave;
1529 while (BytesLeft) {
1530 if (BytesLeft >= 2) {
1531 VT = MVT::i16;
1532 VTSize = 2;
1533 } else {
1534 VT = MVT::i8;
1535 VTSize = 1;
1536 }
1537
Dale Johannesen0f502f62009-02-03 22:26:09 +00001538 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Bob Wilson2dc4f542009-03-20 22:42:55 +00001539 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
Evan Cheng4102eb52007-10-22 22:11:27 +00001540 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001541 DstSV, DstSVOff + DstOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001542 ++i;
1543 DstOff += VTSize;
1544 BytesLeft -= VTSize;
1545 }
Dale Johannesen0f502f62009-02-03 22:26:09 +00001546 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001547}
1548
Duncan Sands1607f052008-12-01 11:39:25 +00001549static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00001550 SDValue Op = N->getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +00001551 DebugLoc dl = N->getDebugLoc();
Evan Chengc7c77292008-11-04 19:57:48 +00001552 if (N->getValueType(0) == MVT::f64) {
1553 // Turn i64->f64 into FMDRR.
Dale Johannesende064702009-02-06 21:50:26 +00001554 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Evan Chengc7c77292008-11-04 19:57:48 +00001555 DAG.getConstant(0, MVT::i32));
Dale Johannesende064702009-02-06 21:50:26 +00001556 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Evan Chengc7c77292008-11-04 19:57:48 +00001557 DAG.getConstant(1, MVT::i32));
Dale Johannesende064702009-02-06 21:50:26 +00001558 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00001559 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00001560
Evan Chengc7c77292008-11-04 19:57:48 +00001561 // Turn f64->i64 into FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001562 SDValue Cvt = DAG.getNode(ARMISD::FMRRD, dl,
Dale Johannesende064702009-02-06 21:50:26 +00001563 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001564
Chris Lattner27a6c732007-11-24 07:07:01 +00001565 // Merge the pieces into a single i64 value.
Dale Johannesende064702009-02-06 21:50:26 +00001566 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
Chris Lattner27a6c732007-11-24 07:07:01 +00001567}
1568
Duncan Sands1607f052008-12-01 11:39:25 +00001569static SDValue ExpandSRx(SDNode *N, SelectionDAG &DAG, const ARMSubtarget *ST) {
Chris Lattner27a6c732007-11-24 07:07:01 +00001570 assert(N->getValueType(0) == MVT::i64 &&
1571 (N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
1572 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00001573
Chris Lattner27a6c732007-11-24 07:07:01 +00001574 // We only lower SRA, SRL of 1 here, all others use generic lowering.
1575 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001576 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00001577 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00001578
Chris Lattner27a6c732007-11-24 07:07:01 +00001579 // If we are in thumb mode, we don't have RRX.
Duncan Sands1607f052008-12-01 11:39:25 +00001580 if (ST->isThumb()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00001581
Chris Lattner27a6c732007-11-24 07:07:01 +00001582 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Dale Johannesende064702009-02-06 21:50:26 +00001583 DebugLoc dl = N->getDebugLoc();
1584 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Chris Lattner27a6c732007-11-24 07:07:01 +00001585 DAG.getConstant(0, MVT::i32));
Dale Johannesende064702009-02-06 21:50:26 +00001586 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Chris Lattner27a6c732007-11-24 07:07:01 +00001587 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00001588
Chris Lattner27a6c732007-11-24 07:07:01 +00001589 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
1590 // captures the result into a carry flag.
1591 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Dale Johannesende064702009-02-06 21:50:26 +00001592 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001593
Chris Lattner27a6c732007-11-24 07:07:01 +00001594 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Dale Johannesende064702009-02-06 21:50:26 +00001595 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00001596
Chris Lattner27a6c732007-11-24 07:07:01 +00001597 // Merge the pieces into a single i64 value.
Dale Johannesende064702009-02-06 21:50:26 +00001598 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00001599}
1600
Dan Gohman475871a2008-07-27 21:46:04 +00001601SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001602 switch (Op.getOpcode()) {
1603 default: assert(0 && "Don't know how to custom lower this!"); abort();
1604 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001605 case ISD::GlobalAddress:
1606 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
1607 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001608 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00001609 case ISD::CALL: return LowerCALL(Op, DAG);
1610 case ISD::RET: return LowerRET(Op, DAG);
1611 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
1612 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
1613 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
1614 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
1615 case ISD::SINT_TO_FP:
1616 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
1617 case ISD::FP_TO_SINT:
1618 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
1619 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00001620 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00001621 case ISD::RETURNADDR: break;
1622 case ISD::FRAMEADDR: break;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001623 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001624 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00001625 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00001626 case ISD::SRL:
Duncan Sands1607f052008-12-01 11:39:25 +00001627 case ISD::SRA: return ExpandSRx(Op.getNode(), DAG,Subtarget);
Evan Chenga8e29892007-01-19 07:51:42 +00001628 }
Dan Gohman475871a2008-07-27 21:46:04 +00001629 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001630}
1631
Duncan Sands1607f052008-12-01 11:39:25 +00001632/// ReplaceNodeResults - Replace the results of node with an illegal result
1633/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00001634void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
1635 SmallVectorImpl<SDValue>&Results,
1636 SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00001637 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00001638 default:
1639 assert(0 && "Don't know how to custom expand this!");
1640 return;
1641 case ISD::BIT_CONVERT:
1642 Results.push_back(ExpandBIT_CONVERT(N, DAG));
1643 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00001644 case ISD::SRL:
Duncan Sands1607f052008-12-01 11:39:25 +00001645 case ISD::SRA: {
1646 SDValue Res = ExpandSRx(N, DAG, Subtarget);
1647 if (Res.getNode())
1648 Results.push_back(Res);
1649 return;
1650 }
Chris Lattner27a6c732007-11-24 07:07:01 +00001651 }
1652}
Chris Lattner27a6c732007-11-24 07:07:01 +00001653
Evan Chenga8e29892007-01-19 07:51:42 +00001654//===----------------------------------------------------------------------===//
1655// ARM Scheduler Hooks
1656//===----------------------------------------------------------------------===//
1657
1658MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00001659ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00001660 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00001661 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00001662 DebugLoc dl = MI->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001663 switch (MI->getOpcode()) {
1664 default: assert(false && "Unexpected instr type to insert");
1665 case ARM::tMOVCCr: {
1666 // To "insert" a SELECT_CC instruction, we actually have to insert the
1667 // diamond control-flow pattern. The incoming instruction knows the
1668 // destination vreg to set, the condition code register to branch on, the
1669 // true/false values to select between, and a branch opcode to use.
1670 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001671 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00001672 ++It;
1673
1674 // thisMBB:
1675 // ...
1676 // TrueVal = ...
1677 // cmpTY ccX, r1, r2
1678 // bCC copy1MBB
1679 // fallthrough --> copy0MBB
1680 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001681 MachineFunction *F = BB->getParent();
1682 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1683 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00001684 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00001685 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001686 F->insert(It, copy0MBB);
1687 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00001688 // Update machine-CFG edges by first adding all successors of the current
1689 // block to the new block which will contain the Phi node for the select.
1690 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1691 e = BB->succ_end(); i != e; ++i)
1692 sinkMBB->addSuccessor(*i);
1693 // Next, remove all successors of the current block, and add the true
1694 // and fallthrough blocks as its successors.
1695 while(!BB->succ_empty())
1696 BB->removeSuccessor(BB->succ_begin());
1697 BB->addSuccessor(copy0MBB);
1698 BB->addSuccessor(sinkMBB);
1699
1700 // copy0MBB:
1701 // %FalseValue = ...
1702 // # fallthrough to sinkMBB
1703 BB = copy0MBB;
1704
1705 // Update machine-CFG edges
1706 BB->addSuccessor(sinkMBB);
1707
1708 // sinkMBB:
1709 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1710 // ...
1711 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00001712 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00001713 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1714 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1715
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001716 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00001717 return BB;
1718 }
1719 }
1720}
1721
1722//===----------------------------------------------------------------------===//
1723// ARM Optimization Hooks
1724//===----------------------------------------------------------------------===//
1725
Chris Lattnerd1980a52009-03-12 06:52:53 +00001726static
1727SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
1728 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00001729 SelectionDAG &DAG = DCI.DAG;
1730 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1731 MVT VT = N->getValueType(0);
1732 unsigned Opc = N->getOpcode();
1733 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
1734 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
1735 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
1736 ISD::CondCode CC = ISD::SETCC_INVALID;
1737
1738 if (isSlctCC) {
1739 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
1740 } else {
1741 SDValue CCOp = Slct.getOperand(0);
1742 if (CCOp.getOpcode() == ISD::SETCC)
1743 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
1744 }
1745
1746 bool DoXform = false;
1747 bool InvCC = false;
1748 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
1749 "Bad input!");
1750
1751 if (LHS.getOpcode() == ISD::Constant &&
1752 cast<ConstantSDNode>(LHS)->isNullValue()) {
1753 DoXform = true;
1754 } else if (CC != ISD::SETCC_INVALID &&
1755 RHS.getOpcode() == ISD::Constant &&
1756 cast<ConstantSDNode>(RHS)->isNullValue()) {
1757 std::swap(LHS, RHS);
1758 SDValue Op0 = Slct.getOperand(0);
1759 MVT OpVT = isSlctCC ? Op0.getValueType() :
1760 Op0.getOperand(0).getValueType();
1761 bool isInt = OpVT.isInteger();
1762 CC = ISD::getSetCCInverse(CC, isInt);
1763
1764 if (!TLI.isCondCodeLegal(CC, OpVT))
1765 return SDValue(); // Inverse operator isn't legal.
1766
1767 DoXform = true;
1768 InvCC = true;
1769 }
1770
1771 if (DoXform) {
1772 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
1773 if (isSlctCC)
1774 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
1775 Slct.getOperand(0), Slct.getOperand(1), CC);
1776 SDValue CCOp = Slct.getOperand(0);
1777 if (InvCC)
1778 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
1779 CCOp.getOperand(0), CCOp.getOperand(1), CC);
1780 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
1781 CCOp, OtherOp, Result);
1782 }
1783 return SDValue();
1784}
1785
1786/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
1787static SDValue PerformADDCombine(SDNode *N,
1788 TargetLowering::DAGCombinerInfo &DCI) {
1789 // added by evan in r37685 with no testcase.
1790 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001791
Chris Lattnerd1980a52009-03-12 06:52:53 +00001792 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
1793 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
1794 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
1795 if (Result.getNode()) return Result;
1796 }
1797 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
1798 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
1799 if (Result.getNode()) return Result;
1800 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00001801
Chris Lattnerd1980a52009-03-12 06:52:53 +00001802 return SDValue();
1803}
1804
1805/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
1806static SDValue PerformSUBCombine(SDNode *N,
1807 TargetLowering::DAGCombinerInfo &DCI) {
1808 // added by evan in r37685 with no testcase.
1809 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001810
Chris Lattnerd1980a52009-03-12 06:52:53 +00001811 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1812 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
1813 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
1814 if (Result.getNode()) return Result;
1815 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00001816
Chris Lattnerd1980a52009-03-12 06:52:53 +00001817 return SDValue();
1818}
1819
1820
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001821/// PerformFMRRDCombine - Target-specific dag combine xforms for ARMISD::FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001822static SDValue PerformFMRRDCombine(SDNode *N,
1823 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001824 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00001825 SDValue InDouble = N->getOperand(0);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001826 if (InDouble.getOpcode() == ARMISD::FMDRR)
1827 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001828 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001829}
1830
Dan Gohman475871a2008-07-27 21:46:04 +00001831SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001832 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001833 switch (N->getOpcode()) {
1834 default: break;
Chris Lattnerd1980a52009-03-12 06:52:53 +00001835 case ISD::ADD: return PerformADDCombine(N, DCI);
1836 case ISD::SUB: return PerformSUBCombine(N, DCI);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001837 case ARMISD::FMRRD: return PerformFMRRDCombine(N, DCI);
1838 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00001839
Dan Gohman475871a2008-07-27 21:46:04 +00001840 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001841}
1842
Evan Chengb01fad62007-03-12 23:30:29 +00001843/// isLegalAddressImmediate - Return true if the integer value can be used
1844/// as the offset of the target addressing mode for load / store of the
1845/// given type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001846static bool isLegalAddressImmediate(int64_t V, MVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00001847 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00001848 if (V == 0)
1849 return true;
1850
Evan Cheng65011532009-03-09 19:15:00 +00001851 if (!VT.isSimple())
1852 return false;
1853
Evan Chengb01fad62007-03-12 23:30:29 +00001854 if (Subtarget->isThumb()) {
1855 if (V < 0)
1856 return false;
1857
1858 unsigned Scale = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001859 switch (VT.getSimpleVT()) {
Evan Chengb01fad62007-03-12 23:30:29 +00001860 default: return false;
1861 case MVT::i1:
1862 case MVT::i8:
1863 // Scale == 1;
1864 break;
1865 case MVT::i16:
1866 // Scale == 2;
1867 Scale = 2;
1868 break;
1869 case MVT::i32:
1870 // Scale == 4;
1871 Scale = 4;
1872 break;
1873 }
1874
1875 if ((V & (Scale - 1)) != 0)
1876 return false;
1877 V /= Scale;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001878 return V == (V & ((1LL << 5) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001879 }
1880
1881 if (V < 0)
1882 V = - V;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001883 switch (VT.getSimpleVT()) {
Evan Chengb01fad62007-03-12 23:30:29 +00001884 default: return false;
1885 case MVT::i1:
1886 case MVT::i8:
1887 case MVT::i32:
1888 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001889 return V == (V & ((1LL << 12) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001890 case MVT::i16:
1891 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001892 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001893 case MVT::f32:
1894 case MVT::f64:
1895 if (!Subtarget->hasVFP2())
1896 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00001897 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00001898 return false;
1899 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001900 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001901 }
Evan Chenga8e29892007-01-19 07:51:42 +00001902}
1903
Chris Lattner37caf8c2007-04-09 23:33:39 +00001904/// isLegalAddressingMode - Return true if the addressing mode represented
1905/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001906bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00001907 const Type *Ty) const {
Bob Wilson2c7dab12009-04-08 17:55:28 +00001908 MVT VT = getValueType(Ty, true);
1909 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00001910 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00001911
Chris Lattner37caf8c2007-04-09 23:33:39 +00001912 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001913 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001914 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00001915
Chris Lattner37caf8c2007-04-09 23:33:39 +00001916 switch (AM.Scale) {
1917 case 0: // no scale reg, must be "r+i" or "r", or "i".
1918 break;
1919 case 1:
1920 if (Subtarget->isThumb())
1921 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00001922 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00001923 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00001924 // ARM doesn't support any R+R*scale+imm addr modes.
1925 if (AM.BaseOffs)
1926 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00001927
Bob Wilson2c7dab12009-04-08 17:55:28 +00001928 if (!VT.isSimple())
1929 return false;
1930
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001931 int Scale = AM.Scale;
Bob Wilson2c7dab12009-04-08 17:55:28 +00001932 switch (VT.getSimpleVT()) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00001933 default: return false;
1934 case MVT::i1:
1935 case MVT::i8:
1936 case MVT::i32:
1937 case MVT::i64:
1938 // This assumes i64 is legalized to a pair of i32. If not (i.e.
1939 // ldrd / strd are used, then its address mode is same as i16.
1940 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001941 if (Scale < 0) Scale = -Scale;
1942 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001943 return true;
1944 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00001945 return isPowerOf2_32(Scale & ~1);
Chris Lattner37caf8c2007-04-09 23:33:39 +00001946 case MVT::i16:
1947 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001948 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001949 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00001950 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00001951
Chris Lattner37caf8c2007-04-09 23:33:39 +00001952 case MVT::isVoid:
1953 // Note, we allow "void" uses (basically, uses that aren't loads or
1954 // stores), because arm allows folding a scale into many arithmetic
1955 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001956
Chris Lattner37caf8c2007-04-09 23:33:39 +00001957 // Allow r << imm, but the imm has to be a multiple of two.
1958 if (AM.Scale & 1) return false;
1959 return isPowerOf2_32(AM.Scale);
1960 }
1961 break;
Evan Chengb01fad62007-03-12 23:30:29 +00001962 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00001963 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00001964}
1965
Duncan Sands83ec4b62008-06-06 12:08:01 +00001966static bool getIndexedAddressParts(SDNode *Ptr, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001967 bool isSEXTLoad, SDValue &Base,
1968 SDValue &Offset, bool &isInc,
Evan Chenga8e29892007-01-19 07:51:42 +00001969 SelectionDAG &DAG) {
1970 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
1971 return false;
1972
1973 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
1974 // AddressingMode 3
1975 Base = Ptr->getOperand(0);
1976 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001977 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001978 if (RHSC < 0 && RHSC > -256) {
1979 isInc = false;
1980 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1981 return true;
1982 }
1983 }
1984 isInc = (Ptr->getOpcode() == ISD::ADD);
1985 Offset = Ptr->getOperand(1);
1986 return true;
1987 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
1988 // AddressingMode 2
1989 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001990 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001991 if (RHSC < 0 && RHSC > -0x1000) {
1992 isInc = false;
1993 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1994 Base = Ptr->getOperand(0);
1995 return true;
1996 }
1997 }
1998
1999 if (Ptr->getOpcode() == ISD::ADD) {
2000 isInc = true;
2001 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
2002 if (ShOpcVal != ARM_AM::no_shift) {
2003 Base = Ptr->getOperand(1);
2004 Offset = Ptr->getOperand(0);
2005 } else {
2006 Base = Ptr->getOperand(0);
2007 Offset = Ptr->getOperand(1);
2008 }
2009 return true;
2010 }
2011
2012 isInc = (Ptr->getOpcode() == ISD::ADD);
2013 Base = Ptr->getOperand(0);
2014 Offset = Ptr->getOperand(1);
2015 return true;
2016 }
2017
2018 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
2019 return false;
2020}
2021
2022/// getPreIndexedAddressParts - returns true by value, base pointer and
2023/// offset pointer and addressing mode by reference if the node's address
2024/// can be legally represented as pre-indexed load / store address.
2025bool
Dan Gohman475871a2008-07-27 21:46:04 +00002026ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
2027 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00002028 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00002029 SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002030 if (Subtarget->isThumb())
2031 return false;
2032
Duncan Sands83ec4b62008-06-06 12:08:01 +00002033 MVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00002034 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00002035 bool isSEXTLoad = false;
2036 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
2037 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002038 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00002039 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
2040 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
2041 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002042 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00002043 } else
2044 return false;
2045
2046 bool isInc;
Gabor Greifba36cb52008-08-28 21:40:38 +00002047 bool isLegal = getIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base, Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00002048 isInc, DAG);
2049 if (isLegal) {
2050 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
2051 return true;
2052 }
2053 return false;
2054}
2055
2056/// getPostIndexedAddressParts - returns true by value, base pointer and
2057/// offset pointer and addressing mode by reference if this node can be
2058/// combined with a load / store to form a post-indexed load / store.
2059bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00002060 SDValue &Base,
2061 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00002062 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00002063 SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002064 if (Subtarget->isThumb())
2065 return false;
2066
Duncan Sands83ec4b62008-06-06 12:08:01 +00002067 MVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00002068 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00002069 bool isSEXTLoad = false;
2070 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002071 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00002072 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
2073 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002074 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00002075 } else
2076 return false;
2077
2078 bool isInc;
2079 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
2080 isInc, DAG);
2081 if (isLegal) {
2082 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
2083 return true;
2084 }
2085 return false;
2086}
2087
Dan Gohman475871a2008-07-27 21:46:04 +00002088void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00002089 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00002090 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002091 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00002092 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00002093 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002094 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002095 switch (Op.getOpcode()) {
2096 default: break;
2097 case ARMISD::CMOV: {
2098 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00002099 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00002100 if (KnownZero == 0 && KnownOne == 0) return;
2101
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002102 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00002103 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
2104 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00002105 KnownZero &= KnownZeroRHS;
2106 KnownOne &= KnownOneRHS;
2107 return;
2108 }
2109 }
2110}
2111
2112//===----------------------------------------------------------------------===//
2113// ARM Inline Assembly Support
2114//===----------------------------------------------------------------------===//
2115
2116/// getConstraintType - Given a constraint letter, return the type of
2117/// constraint it is for this target.
2118ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00002119ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
2120 if (Constraint.size() == 1) {
2121 switch (Constraint[0]) {
2122 default: break;
2123 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00002124 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00002125 }
Evan Chenga8e29892007-01-19 07:51:42 +00002126 }
Chris Lattner4234f572007-03-25 02:14:49 +00002127 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00002128}
2129
Bob Wilson2dc4f542009-03-20 22:42:55 +00002130std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00002131ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002132 MVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002133 if (Constraint.size() == 1) {
2134 // GCC RS6000 Constraint Letters
2135 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00002136 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002137 if (Subtarget->isThumb())
2138 return std::make_pair(0U, ARM::tGPRRegisterClass);
2139 else
2140 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00002141 case 'r':
2142 return std::make_pair(0U, ARM::GPRRegisterClass);
2143 case 'w':
2144 if (VT == MVT::f32)
2145 return std::make_pair(0U, ARM::SPRRegisterClass);
Evan Cheng0a7baa22007-04-04 00:06:07 +00002146 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00002147 return std::make_pair(0U, ARM::DPRRegisterClass);
2148 break;
Evan Chenga8e29892007-01-19 07:51:42 +00002149 }
2150 }
2151 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2152}
2153
2154std::vector<unsigned> ARMTargetLowering::
2155getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002156 MVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002157 if (Constraint.size() != 1)
2158 return std::vector<unsigned>();
2159
2160 switch (Constraint[0]) { // GCC ARM Constraint Letters
2161 default: break;
2162 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002163 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
2164 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
2165 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002166 case 'r':
2167 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
2168 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
2169 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
2170 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00002171 case 'w':
2172 if (VT == MVT::f32)
2173 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
2174 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
2175 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
2176 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
2177 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
2178 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
2179 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
2180 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
2181 if (VT == MVT::f64)
2182 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
2183 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
2184 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
2185 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
2186 break;
Evan Chenga8e29892007-01-19 07:51:42 +00002187 }
2188
2189 return std::vector<unsigned>();
2190}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00002191
2192/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
2193/// vector. If it is invalid, don't add anything to Ops.
2194void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
2195 char Constraint,
2196 bool hasMemory,
2197 std::vector<SDValue>&Ops,
2198 SelectionDAG &DAG) const {
2199 SDValue Result(0, 0);
2200
2201 switch (Constraint) {
2202 default: break;
2203 case 'I': case 'J': case 'K': case 'L':
2204 case 'M': case 'N': case 'O':
2205 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
2206 if (!C)
2207 return;
2208
2209 int64_t CVal64 = C->getSExtValue();
2210 int CVal = (int) CVal64;
2211 // None of these constraints allow values larger than 32 bits. Check
2212 // that the value fits in an int.
2213 if (CVal != CVal64)
2214 return;
2215
2216 switch (Constraint) {
2217 case 'I':
2218 if (Subtarget->isThumb()) {
2219 // This must be a constant between 0 and 255, for ADD immediates.
2220 if (CVal >= 0 && CVal <= 255)
2221 break;
2222 } else {
2223 // A constant that can be used as an immediate value in a
2224 // data-processing instruction.
2225 if (ARM_AM::getSOImmVal(CVal) != -1)
2226 break;
2227 }
2228 return;
2229
2230 case 'J':
2231 if (Subtarget->isThumb()) {
2232 // This must be a constant between -255 and -1, for negated ADD
2233 // immediates. This can be used in GCC with an "n" modifier that
2234 // prints the negated value, for use with SUB instructions. It is
2235 // not useful otherwise but is implemented for compatibility.
2236 if (CVal >= -255 && CVal <= -1)
2237 break;
2238 } else {
2239 // This must be a constant between -4095 and 4095. It is not clear
2240 // what this constraint is intended for. Implemented for
2241 // compatibility with GCC.
2242 if (CVal >= -4095 && CVal <= 4095)
2243 break;
2244 }
2245 return;
2246
2247 case 'K':
2248 if (Subtarget->isThumb()) {
2249 // A 32-bit value where only one byte has a nonzero value. Exclude
2250 // zero to match GCC. This constraint is used by GCC internally for
2251 // constants that can be loaded with a move/shift combination.
2252 // It is not useful otherwise but is implemented for compatibility.
2253 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
2254 break;
2255 } else {
2256 // A constant whose bitwise inverse can be used as an immediate
2257 // value in a data-processing instruction. This can be used in GCC
2258 // with a "B" modifier that prints the inverted value, for use with
2259 // BIC and MVN instructions. It is not useful otherwise but is
2260 // implemented for compatibility.
2261 if (ARM_AM::getSOImmVal(~CVal) != -1)
2262 break;
2263 }
2264 return;
2265
2266 case 'L':
2267 if (Subtarget->isThumb()) {
2268 // This must be a constant between -7 and 7,
2269 // for 3-operand ADD/SUB immediate instructions.
2270 if (CVal >= -7 && CVal < 7)
2271 break;
2272 } else {
2273 // A constant whose negation can be used as an immediate value in a
2274 // data-processing instruction. This can be used in GCC with an "n"
2275 // modifier that prints the negated value, for use with SUB
2276 // instructions. It is not useful otherwise but is implemented for
2277 // compatibility.
2278 if (ARM_AM::getSOImmVal(-CVal) != -1)
2279 break;
2280 }
2281 return;
2282
2283 case 'M':
2284 if (Subtarget->isThumb()) {
2285 // This must be a multiple of 4 between 0 and 1020, for
2286 // ADD sp + immediate.
2287 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
2288 break;
2289 } else {
2290 // A power of two or a constant between 0 and 32. This is used in
2291 // GCC for the shift amount on shifted register operands, but it is
2292 // useful in general for any shift amounts.
2293 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
2294 break;
2295 }
2296 return;
2297
2298 case 'N':
2299 if (Subtarget->isThumb()) {
2300 // This must be a constant between 0 and 31, for shift amounts.
2301 if (CVal >= 0 && CVal <= 31)
2302 break;
2303 }
2304 return;
2305
2306 case 'O':
2307 if (Subtarget->isThumb()) {
2308 // This must be a multiple of 4 between -508 and 508, for
2309 // ADD/SUB sp = sp + immediate.
2310 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
2311 break;
2312 }
2313 return;
2314 }
2315 Result = DAG.getTargetConstant(CVal, Op.getValueType());
2316 break;
2317 }
2318
2319 if (Result.getNode()) {
2320 Ops.push_back(Result);
2321 return;
2322 }
2323 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
2324 Ops, DAG);
2325}