blob: 52828e5c56e36f3377eb59e60e717364dc086aaa [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);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000507 Chain = Lo.getValue(1);
508 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000509 VA = RVLocs[++i]; // skip ahead to next loc
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000510 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
511 InFlag);
512 Chain = Hi.getValue(1);
513 InFlag = Hi.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000514 ResultVals.push_back(DAG.getNode(ARMISD::FMDRR, dl, VA.getValVT(), Lo,
515 Hi));
516 } else {
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000517 SDValue Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
518 InFlag);
519 Chain = Val.getValue(1);
520 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000521
522 switch (VA.getLocInfo()) {
523 default: assert(0 && "Unknown loc info!");
524 case CCValAssign::Full: break;
525 case CCValAssign::BCvt:
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000526 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000527 break;
528 }
529
530 ResultVals.push_back(Val);
531 }
532 }
533
534 // Merge everything together with a MERGE_VALUES node.
535 ResultVals.push_back(Chain);
536 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
537 &ResultVals[0], ResultVals.size()).getNode();
538}
539
540/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
541/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000542/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000543/// a byval function parameter.
544/// Sometimes what we are copying is the end of a larger object, the part that
545/// does not fit in registers.
546static SDValue
547CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
548 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
549 DebugLoc dl) {
550 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
551 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
552 /*AlwaysInline=*/false, NULL, 0, NULL, 0);
553}
554
Bob Wilsondee46d72009-04-17 20:35:10 +0000555/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000556SDValue
557ARMTargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
558 const SDValue &StackPtr,
Bob Wilsondee46d72009-04-17 20:35:10 +0000559 const CCValAssign &VA, SDValue Chain,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000560 SDValue Arg, ISD::ArgFlagsTy Flags) {
561 DebugLoc dl = TheCall->getDebugLoc();
562 unsigned LocMemOffset = VA.getLocMemOffset();
563 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
564 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
565 if (Flags.isByVal()) {
566 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
567 }
568 return DAG.getStore(Chain, dl, Arg, PtrOff,
569 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chenga8e29892007-01-19 07:51:42 +0000570}
571
Evan Chengfc403422007-02-03 08:53:01 +0000572/// LowerCALL - Lowering a ISD::CALL node into a callseq_start <-
573/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
574/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000575SDValue ARMTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Dan Gohman095cc292008-09-13 01:54:27 +0000576 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
Bob Wilson1f595bb2009-04-17 19:07:39 +0000577 MVT RetVT = TheCall->getRetValType(0);
578 SDValue Chain = TheCall->getChain();
579 unsigned CC = TheCall->getCallingConv();
580 assert((CC == CallingConv::C ||
581 CC == CallingConv::Fast) && "unknown calling convention");
582 bool isVarArg = TheCall->isVarArg();
583 SDValue Callee = TheCall->getCallee();
584 DebugLoc dl = TheCall->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +0000585
Bob Wilson1f595bb2009-04-17 19:07:39 +0000586 // Analyze operands of the call, assigning locations to each operand.
587 SmallVector<CCValAssign, 16> ArgLocs;
588 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
589 CCInfo.AnalyzeCallOperands(TheCall, CC_ARM);
Evan Chenga8e29892007-01-19 07:51:42 +0000590
Bob Wilson1f595bb2009-04-17 19:07:39 +0000591 // Get a count of how many bytes are to be pushed on the stack.
592 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000593
594 // Adjust the stack pointer for the new arguments...
595 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000596 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000597
Dan Gohman475871a2008-07-27 21:46:04 +0000598 SDValue StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000599
Bob Wilson1f595bb2009-04-17 19:07:39 +0000600 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
601 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000602
Bob Wilson1f595bb2009-04-17 19:07:39 +0000603 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +0000604 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000605 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
606 i != e;
607 ++i, ++realArgIdx) {
608 CCValAssign &VA = ArgLocs[i];
609 SDValue Arg = TheCall->getArg(realArgIdx);
610 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(realArgIdx);
Evan Chenga8e29892007-01-19 07:51:42 +0000611
Bob Wilson1f595bb2009-04-17 19:07:39 +0000612 // Promote the value if needed.
613 switch (VA.getLocInfo()) {
614 default: assert(0 && "Unknown loc info!");
615 case CCValAssign::Full: break;
616 case CCValAssign::SExt:
617 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
618 break;
619 case CCValAssign::ZExt:
620 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
621 break;
622 case CCValAssign::AExt:
623 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
624 break;
625 case CCValAssign::BCvt:
626 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
627 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000628 }
629
Bob Wilson1f595bb2009-04-17 19:07:39 +0000630 // f64 is passed in i32 pairs and must be combined
631 if (VA.needsCustom()) {
632 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
633 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
634 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
635 VA = ArgLocs[++i]; // skip ahead to next loc
636 if (VA.isRegLoc())
Bob Wilsondee46d72009-04-17 20:35:10 +0000637 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(1)));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000638 else {
639 assert(VA.isMemLoc());
640 if (StackPtr.getNode() == 0)
641 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
642
643 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
644 Chain, fmrrd.getValue(1),
645 Flags));
646 }
647 } else if (VA.isRegLoc()) {
648 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
649 } else {
650 assert(VA.isMemLoc());
651 if (StackPtr.getNode() == 0)
652 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
653
654 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
655 Chain, Arg, Flags));
656 }
Evan Chenga8e29892007-01-19 07:51:42 +0000657 }
658
659 if (!MemOpChains.empty())
Dale Johannesen33c960f2009-02-04 20:06:27 +0000660 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +0000661 &MemOpChains[0], MemOpChains.size());
662
663 // Build a sequence of copy-to-reg nodes chained together with token chain
664 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +0000665 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +0000666 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +0000667 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000668 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000669 InFlag = Chain.getValue(1);
670 }
671
Bill Wendling056292f2008-09-16 21:48:12 +0000672 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
673 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
674 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +0000675 bool isDirect = false;
676 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +0000677 bool isLocalARMFunc = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000678 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
679 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +0000680 isDirect = true;
Reid Spencer5cbf9852007-01-30 20:08:39 +0000681 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
Evan Chenga8e29892007-01-19 07:51:42 +0000682 GV->hasLinkOnceLinkage());
Evan Cheng970a4192007-01-19 19:28:01 +0000683 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +0000684 getTargetMachine().getRelocationModel() != Reloc::Static;
685 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +0000686 // ARM call to a local ARM function is predicable.
687 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +0000688 // tBX takes a register source operand.
689 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
690 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
691 ARMCP::CPStub, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000692 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000693 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000694 Callee = DAG.getLoad(getPointerTy(), dl,
695 DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000696 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000697 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000698 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +0000699 } else
700 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +0000701 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000702 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +0000703 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000704 getTargetMachine().getRelocationModel() != Reloc::Static;
705 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +0000706 // tBX takes a register source operand.
707 const char *Sym = S->getSymbol();
708 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
709 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(Sym, ARMPCLabelIndex,
710 ARMCP::CPStub, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000711 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000712 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000713 Callee = DAG.getLoad(getPointerTy(), dl,
Bob Wilson2dc4f542009-03-20 22:42:55 +0000714 DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000715 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000716 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000717 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +0000718 } else
Bill Wendling056292f2008-09-16 21:48:12 +0000719 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000720 }
721
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000722 // FIXME: handle tail calls differently.
723 unsigned CallOpc;
724 if (Subtarget->isThumb()) {
725 if (!Subtarget->hasV5TOps() && (!isDirect || isARMFunc))
726 CallOpc = ARMISD::CALL_NOLINK;
727 else
728 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
729 } else {
730 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +0000731 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
732 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000733 }
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +0000734 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb()) {
735 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Dale Johannesene8d72302009-02-06 23:05:02 +0000736 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000737 InFlag = Chain.getValue(1);
738 }
739
Dan Gohman475871a2008-07-27 21:46:04 +0000740 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +0000741 Ops.push_back(Chain);
742 Ops.push_back(Callee);
743
744 // Add argument registers to the end of the list so that they are known live
745 // into the call.
746 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
747 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
748 RegsToPass[i].second.getValueType()));
749
Gabor Greifba36cb52008-08-28 21:40:38 +0000750 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +0000751 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +0000752 // Returns a chain and a flag for retval copy to use.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000753 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +0000754 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +0000755 InFlag = Chain.getValue(1);
756
Chris Lattnere563bbc2008-10-11 22:08:30 +0000757 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
758 DAG.getIntPtrConstant(0, true), InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000759 if (RetVT != MVT::Other)
760 InFlag = Chain.getValue(1);
761
Bob Wilson1f595bb2009-04-17 19:07:39 +0000762 // Handle result values, copying them out of physregs into vregs that we
763 // return.
764 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
765 Op.getResNo());
Evan Chenga8e29892007-01-19 07:51:42 +0000766}
767
Bob Wilson1f595bb2009-04-17 19:07:39 +0000768SDValue ARMTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
769 // The chain is always operand #0
Dan Gohman475871a2008-07-27 21:46:04 +0000770 SDValue Chain = Op.getOperand(0);
Dale Johannesena05dca42009-02-04 23:02:30 +0000771 DebugLoc dl = Op.getDebugLoc();
Bob Wilson2dc4f542009-03-20 22:42:55 +0000772
Bob Wilsondee46d72009-04-17 20:35:10 +0000773 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000774 SmallVector<CCValAssign, 16> RVLocs;
775 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
776 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
777
Bob Wilsondee46d72009-04-17 20:35:10 +0000778 // CCState - Info about the registers and stack slots.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000779 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
780
Bob Wilsondee46d72009-04-17 20:35:10 +0000781 // Analyze return values of ISD::RET.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000782 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_ARM);
783
784 // If this is the first return lowered for this function, add
785 // the regs to the liveout set for the function.
786 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
787 for (unsigned i = 0; i != RVLocs.size(); ++i)
788 if (RVLocs[i].isRegLoc())
789 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +0000790 }
791
Bob Wilson1f595bb2009-04-17 19:07:39 +0000792 SDValue Flag;
793
794 // Copy the result values into the output registers.
795 for (unsigned i = 0, realRVLocIdx = 0;
796 i != RVLocs.size();
797 ++i, ++realRVLocIdx) {
798 CCValAssign &VA = RVLocs[i];
799 assert(VA.isRegLoc() && "Can only return in registers!");
800
801 // ISD::RET => ret chain, (regnum1,val1), ...
802 // So i*2+1 index only the regnums
803 SDValue Arg = Op.getOperand(realRVLocIdx*2+1);
804
805 switch (VA.getLocInfo()) {
806 default: assert(0 && "Unknown loc info!");
807 case CCValAssign::Full: break;
808 case CCValAssign::BCvt:
809 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
810 break;
811 }
812
813 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
814 // available.
815 if (VA.needsCustom()) {
816 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
817 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
818 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000819 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000820 VA = RVLocs[++i]; // skip ahead to next loc
821 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
822 Flag);
823 } else
824 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
825
Bob Wilsondee46d72009-04-17 20:35:10 +0000826 // Guarantee that all emitted copies are
827 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000828 Flag = Chain.getValue(1);
829 }
830
831 SDValue result;
832 if (Flag.getNode())
833 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
834 else // Return Void
835 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
836
837 return result;
Evan Chenga8e29892007-01-19 07:51:42 +0000838}
839
Bob Wilson2dc4f542009-03-20 22:42:55 +0000840// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Bill Wendling056292f2008-09-16 21:48:12 +0000841// their target countpart wrapped in the ARMISD::Wrapper node. Suppose N is
842// one of the above mentioned nodes. It has to be wrapped because otherwise
843// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
844// be used to form addressing mode. These wrapped nodes will be selected
845// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +0000846static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000847 MVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000848 // FIXME there is no actual debug info here
849 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +0000850 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +0000851 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +0000852 if (CP->isMachineConstantPoolEntry())
853 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
854 CP->getAlignment());
855 else
856 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
857 CP->getAlignment());
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000858 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +0000859}
860
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000861// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +0000862SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000863ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
864 SelectionDAG &DAG) {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000865 DebugLoc dl = GA->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000866 MVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000867 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
868 ARMConstantPoolValue *CPV =
869 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
870 PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000871 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000872 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000873 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000874 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000875
Dan Gohman475871a2008-07-27 21:46:04 +0000876 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000877 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000878
879 // call __tls_get_addr.
880 ArgListTy Args;
881 ArgListEntry Entry;
882 Entry.Node = Argument;
883 Entry.Ty = (const Type *) Type::Int32Ty;
884 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000885 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +0000886 std::pair<SDValue, SDValue> CallResult =
Dale Johannesen86098bd2008-09-26 19:31:26 +0000887 LowerCallTo(Chain, (const Type *) Type::Int32Ty, false, false, false, false,
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000888 CallingConv::C, false,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000889 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000890 return CallResult.first;
891}
892
893// Lower ISD::GlobalTLSAddress using the "initial exec" or
894// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +0000895SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000896ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Bob Wilson2dc4f542009-03-20 22:42:55 +0000897 SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000898 GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000899 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +0000900 SDValue Offset;
901 SDValue Chain = DAG.getEntryNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000902 MVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000903 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +0000904 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000905
906 if (GV->isDeclaration()){
907 // initial exec model
908 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
909 ARMConstantPoolValue *CPV =
910 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
911 PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000912 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000913 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000914 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000915 Chain = Offset.getValue(1);
916
Dan Gohman475871a2008-07-27 21:46:04 +0000917 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000918 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000919
Dale Johannesen33c960f2009-02-04 20:06:27 +0000920 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000921 } else {
922 // local exec model
923 ARMConstantPoolValue *CPV =
924 new ARMConstantPoolValue(GV, ARMCP::CPValue, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +0000925 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000926 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000927 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000928 }
929
930 // The address of the thread local variable is the add of the thread
931 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000932 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000933}
934
Dan Gohman475871a2008-07-27 21:46:04 +0000935SDValue
936ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000937 // TODO: implement the "local dynamic" model
938 assert(Subtarget->isTargetELF() &&
939 "TLS not implemented for non-ELF targets");
940 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
941 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
942 // otherwise use the "Local Exec" TLS Model
943 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
944 return LowerToTLSGeneralDynamicModel(GA, DAG);
945 else
946 return LowerToTLSExecModels(GA, DAG);
947}
948
Dan Gohman475871a2008-07-27 21:46:04 +0000949SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +0000950 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000951 MVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000952 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000953 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
954 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
955 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000956 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000957 ARMConstantPoolValue *CPV =
958 new ARMConstantPoolValue(GV, ARMCP::CPValue, UseGOTOFF ? "GOTOFF":"GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +0000959 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000960 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000961 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Dale Johannesen33c960f2009-02-04 20:06:27 +0000962 CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000963 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000964 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000965 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000966 if (!UseGOTOFF)
Dale Johannesen33c960f2009-02-04 20:06:27 +0000967 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000968 return Result;
969 } else {
Evan Cheng1606e8e2009-03-13 07:51:59 +0000970 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000971 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000972 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000973 }
974}
975
Evan Chenga8e29892007-01-19 07:51:42 +0000976/// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol
Evan Cheng97c9bb52007-05-04 00:26:58 +0000977/// even in non-static mode.
978static bool GVIsIndirectSymbol(GlobalValue *GV, Reloc::Model RelocM) {
Evan Chengae94e592008-12-05 01:06:39 +0000979 // If symbol visibility is hidden, the extra load is not needed if
980 // the symbol is definitely defined in the current translation unit.
981 bool isDecl = GV->isDeclaration() && !GV->hasNotBeenReadFromBitcode();
982 if (GV->hasHiddenVisibility() && (!isDecl && !GV->hasCommonLinkage()))
983 return false;
Duncan Sands667d4b82009-03-07 15:45:40 +0000984 return RelocM != Reloc::Static && (isDecl || GV->isWeakForLinker());
Evan Chenga8e29892007-01-19 07:51:42 +0000985}
986
Dan Gohman475871a2008-07-27 21:46:04 +0000987SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +0000988 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000989 MVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000990 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +0000991 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
992 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng97c9bb52007-05-04 00:26:58 +0000993 bool IsIndirect = GVIsIndirectSymbol(GV, RelocM);
Dan Gohman475871a2008-07-27 21:46:04 +0000994 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +0000995 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +0000996 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +0000997 else {
998 unsigned PCAdj = (RelocM != Reloc::PIC_)
999 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Chengc60e76d2007-01-30 20:37:08 +00001000 ARMCP::ARMCPKind Kind = IsIndirect ? ARMCP::CPNonLazyPtr
1001 : ARMCP::CPValue;
Evan Chenga8e29892007-01-19 07:51:42 +00001002 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
Evan Chengc60e76d2007-01-30 20:37:08 +00001003 Kind, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001004 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001005 }
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001006 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001007
Dale Johannesen33c960f2009-02-04 20:06:27 +00001008 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001009 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001010
1011 if (RelocM == Reloc::PIC_) {
Dan Gohman475871a2008-07-27 21:46:04 +00001012 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001013 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001014 }
1015 if (IsIndirect)
Dale Johannesen33c960f2009-02-04 20:06:27 +00001016 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001017
1018 return Result;
1019}
1020
Dan Gohman475871a2008-07-27 21:46:04 +00001021SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001022 SelectionDAG &DAG){
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001023 assert(Subtarget->isTargetELF() &&
1024 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Duncan Sands83ec4b62008-06-06 12:08:01 +00001025 MVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001026 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001027 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
1028 ARMConstantPoolValue *CPV = new ARMConstantPoolValue("_GLOBAL_OFFSET_TABLE_",
1029 ARMPCLabelIndex,
1030 ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001031 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001032 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001033 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001034 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001035 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001036}
1037
Dan Gohman475871a2008-07-27 21:46:04 +00001038static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001039 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001040 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001041 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001042 default: return SDValue(); // Don't custom lower most intrinsics.
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001043 case Intrinsic::arm_thread_pointer:
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001044 return DAG.getNode(ARMISD::THREAD_POINTER, DebugLoc::getUnknownLoc(),
1045 PtrVT);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001046 }
1047}
1048
Dan Gohman475871a2008-07-27 21:46:04 +00001049static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001050 unsigned VarArgsFrameIndex) {
Evan Chenga8e29892007-01-19 07:51:42 +00001051 // vastart just stores the address of the VarArgsFrameIndex slot into the
1052 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001053 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001054 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001055 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001056 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001057 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001058}
1059
Dan Gohman475871a2008-07-27 21:46:04 +00001060SDValue
1061ARMTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001062 MachineFunction &MF = DAG.getMachineFunction();
1063 MachineFrameInfo *MFI = MF.getFrameInfo();
1064
Dan Gohman475871a2008-07-27 21:46:04 +00001065 SDValue Root = Op.getOperand(0);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001066 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001067 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001068 unsigned CC = MF.getFunction()->getCallingConv();
1069 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1070
1071 // Assign locations to all of the incoming arguments.
1072 SmallVector<CCValAssign, 16> ArgLocs;
1073 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1074 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_ARM);
1075
1076 SmallVector<SDValue, 16> ArgValues;
1077
1078 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1079 CCValAssign &VA = ArgLocs[i];
1080
Bob Wilsondee46d72009-04-17 20:35:10 +00001081 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001082 if (VA.isRegLoc()) {
1083 MVT RegVT = VA.getLocVT();
1084 TargetRegisterClass *RC;
1085 if (AFI->isThumbFunction())
1086 RC = ARM::tGPRRegisterClass;
1087 else
1088 RC = ARM::GPRRegisterClass;
1089
1090 if (RegVT == MVT::f64) {
Bob Wilsondee46d72009-04-17 20:35:10 +00001091 // f64 is passed in pairs of GPRs and must be combined.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001092 RegVT = MVT::i32;
1093 } else if (!((RegVT == MVT::i32) || (RegVT == MVT::f32)))
1094 assert(0 && "RegVT not supported by FORMAL_ARGUMENTS Lowering");
1095
Bob Wilsondee46d72009-04-17 20:35:10 +00001096 // Transform the arguments stored in physical registers into virtual ones.
Bob Wilson998e1252009-04-20 18:36:57 +00001097 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001098 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, RegVT);
1099
Bob Wilsondee46d72009-04-17 20:35:10 +00001100 // f64 is passed in i32 pairs and must be combined.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001101 if (VA.needsCustom()) {
1102 SDValue ArgValue2;
1103
1104 VA = ArgLocs[++i]; // skip ahead to next loc
1105 if (VA.isMemLoc()) {
Bob Wilsond55bd512009-04-24 17:05:01 +00001106 // must be APCS to split like this
Bob Wilson1f595bb2009-04-17 19:07:39 +00001107 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1108 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset());
1109
Bob Wilsondee46d72009-04-17 20:35:10 +00001110 // Create load node to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001111 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1112 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN, NULL, 0);
1113 } else {
Bob Wilson998e1252009-04-20 18:36:57 +00001114 Reg = MF.addLiveIn(VA.getLocReg(), RC);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001115 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
1116 }
1117
1118 ArgValue = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64,
1119 ArgValue, ArgValue2);
1120 }
1121
1122 // If this is an 8 or 16-bit value, it is really passed promoted
1123 // to 32 bits. Insert an assert[sz]ext to capture this, then
1124 // truncate to the right size.
1125 switch (VA.getLocInfo()) {
1126 default: assert(0 && "Unknown loc info!");
1127 case CCValAssign::Full: break;
1128 case CCValAssign::BCvt:
1129 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1130 break;
1131 case CCValAssign::SExt:
1132 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1133 DAG.getValueType(VA.getValVT()));
1134 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1135 break;
1136 case CCValAssign::ZExt:
1137 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1138 DAG.getValueType(VA.getValVT()));
1139 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1140 break;
1141 }
1142
1143 ArgValues.push_back(ArgValue);
1144
1145 } else { // VA.isRegLoc()
1146
1147 // sanity check
1148 assert(VA.isMemLoc());
1149 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
1150
1151 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1152 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset());
1153
Bob Wilsondee46d72009-04-17 20:35:10 +00001154 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001155 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1156 ArgValues.push_back(DAG.getLoad(VA.getValVT(), dl, Root, FIN, NULL, 0));
1157 }
1158 }
1159
1160 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001161 if (isVarArg) {
1162 static const unsigned GPRArgRegs[] = {
1163 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1164 };
1165
Bob Wilsondee46d72009-04-17 20:35:10 +00001166 unsigned NumGPRs = CCInfo.getFirstUnallocated
1167 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001168
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001169 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1170 unsigned VARegSize = (4 - NumGPRs) * 4;
1171 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001172 unsigned ArgOffset = 0;
Evan Chenga8e29892007-01-19 07:51:42 +00001173 if (VARegSaveSize) {
1174 // If this function is vararg, store any remaining integer argument regs
1175 // to their spots on the stack so that they may be loaded by deferencing
1176 // the result of va_next.
1177 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001178 ArgOffset = CCInfo.getNextStackOffset();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001179 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1180 VARegSaveSize - VARegSize);
Dan Gohman475871a2008-07-27 21:46:04 +00001181 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001182
Dan Gohman475871a2008-07-27 21:46:04 +00001183 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001184 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001185 TargetRegisterClass *RC;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001186 if (AFI->isThumbFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001187 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001188 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001189 RC = ARM::GPRRegisterClass;
1190
Bob Wilson998e1252009-04-20 18:36:57 +00001191 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001192 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::i32);
1193 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001194 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001195 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001196 DAG.getConstant(4, getPointerTy()));
1197 }
1198 if (!MemOps.empty())
Dale Johannesen33c960f2009-02-04 20:06:27 +00001199 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001200 &MemOps[0], MemOps.size());
1201 } else
1202 // This will point to the next argument passed via stack.
1203 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1204 }
1205
1206 ArgValues.push_back(Root);
1207
1208 // Return the new list of results.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001209 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Bob Wilson1f595bb2009-04-17 19:07:39 +00001210 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Evan Chenga8e29892007-01-19 07:51:42 +00001211}
1212
1213/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001214static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001215 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001216 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001217 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001218 // Maybe this has already been legalized into the constant pool?
1219 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001220 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001221 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1222 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001223 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001224 }
1225 }
1226 return false;
1227}
1228
Evan Cheng9a2ef952007-02-02 01:53:26 +00001229static bool isLegalCmpImmediate(unsigned C, bool isThumb) {
Evan Chenga8e29892007-01-19 07:51:42 +00001230 return ( isThumb && (C & ~255U) == 0) ||
1231 (!isThumb && ARM_AM::getSOImmVal(C) != -1);
1232}
1233
1234/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1235/// the given operands.
Dan Gohman475871a2008-07-27 21:46:04 +00001236static SDValue getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Dale Johannesende064702009-02-06 21:50:26 +00001237 SDValue &ARMCC, SelectionDAG &DAG, bool isThumb,
1238 DebugLoc dl) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001239 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001240 unsigned C = RHSC->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001241 if (!isLegalCmpImmediate(C, isThumb)) {
1242 // Constant does not fit, try adjusting it by one?
1243 switch (CC) {
1244 default: break;
1245 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001246 case ISD::SETGE:
Evan Chenga8e29892007-01-19 07:51:42 +00001247 if (isLegalCmpImmediate(C-1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001248 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
1249 RHS = DAG.getConstant(C-1, MVT::i32);
1250 }
1251 break;
1252 case ISD::SETULT:
1253 case ISD::SETUGE:
1254 if (C > 0 && isLegalCmpImmediate(C-1, isThumb)) {
1255 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Evan Chenga8e29892007-01-19 07:51:42 +00001256 RHS = DAG.getConstant(C-1, MVT::i32);
1257 }
1258 break;
1259 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001260 case ISD::SETGT:
Evan Chenga8e29892007-01-19 07:51:42 +00001261 if (isLegalCmpImmediate(C+1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001262 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
1263 RHS = DAG.getConstant(C+1, MVT::i32);
1264 }
1265 break;
1266 case ISD::SETULE:
1267 case ISD::SETUGT:
1268 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb)) {
1269 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Evan Chenga8e29892007-01-19 07:51:42 +00001270 RHS = DAG.getConstant(C+1, MVT::i32);
1271 }
1272 break;
1273 }
1274 }
1275 }
1276
1277 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001278 ARMISD::NodeType CompareType;
1279 switch (CondCode) {
1280 default:
1281 CompareType = ARMISD::CMP;
1282 break;
1283 case ARMCC::EQ:
1284 case ARMCC::NE:
1285 case ARMCC::MI:
1286 case ARMCC::PL:
1287 // Uses only N and Z Flags
1288 CompareType = ARMISD::CMPNZ;
1289 break;
1290 }
Evan Chenga8e29892007-01-19 07:51:42 +00001291 ARMCC = DAG.getConstant(CondCode, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001292 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001293}
1294
1295/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001296static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001297 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001298 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001299 if (!isFloatingPointZero(RHS))
Dale Johannesende064702009-02-06 21:50:26 +00001300 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001301 else
Dale Johannesende064702009-02-06 21:50:26 +00001302 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1303 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001304}
1305
Dan Gohman475871a2008-07-27 21:46:04 +00001306static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001307 const ARMSubtarget *ST) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001308 MVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001309 SDValue LHS = Op.getOperand(0);
1310 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001311 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001312 SDValue TrueVal = Op.getOperand(2);
1313 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001314 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001315
1316 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001317 SDValue ARMCC;
1318 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001319 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb(), dl);
1320 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001321 }
1322
1323 ARMCC::CondCodes CondCode, CondCode2;
1324 if (FPCCToARMCC(CC, CondCode, CondCode2))
1325 std::swap(TrueVal, FalseVal);
1326
Dan Gohman475871a2008-07-27 21:46:04 +00001327 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1328 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001329 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1330 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001331 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001332 if (CondCode2 != ARMCC::AL) {
Dan Gohman475871a2008-07-27 21:46:04 +00001333 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001334 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00001335 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001336 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00001337 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001338 }
1339 return Result;
1340}
1341
Dan Gohman475871a2008-07-27 21:46:04 +00001342static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001343 const ARMSubtarget *ST) {
Dan Gohman475871a2008-07-27 21:46:04 +00001344 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001345 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001346 SDValue LHS = Op.getOperand(2);
1347 SDValue RHS = Op.getOperand(3);
1348 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00001349 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001350
1351 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001352 SDValue ARMCC;
1353 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001354 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb(), dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001355 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00001356 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001357 }
1358
1359 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
1360 ARMCC::CondCodes CondCode, CondCode2;
1361 if (FPCCToARMCC(CC, CondCode, CondCode2))
1362 // Swap the LHS/RHS of the comparison if needed.
1363 std::swap(LHS, RHS);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001364
Dale Johannesende064702009-02-06 21:50:26 +00001365 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Dan Gohman475871a2008-07-27 21:46:04 +00001366 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1367 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001368 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001369 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00001370 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001371 if (CondCode2 != ARMCC::AL) {
1372 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001373 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00001374 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001375 }
1376 return Res;
1377}
1378
Dan Gohman475871a2008-07-27 21:46:04 +00001379SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) {
1380 SDValue Chain = Op.getOperand(0);
1381 SDValue Table = Op.getOperand(1);
1382 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001383 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001384
Duncan Sands83ec4b62008-06-06 12:08:01 +00001385 MVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00001386 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1387 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Dan Gohman475871a2008-07-27 21:46:04 +00001388 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
1389 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Dale Johannesende064702009-02-06 21:50:26 +00001390 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001391 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
1392 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chenga8e29892007-01-19 07:51:42 +00001393 bool isPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001394 Addr = DAG.getLoad(isPIC ? (MVT)MVT::i32 : PTy, dl,
Evan Chenge2446c62007-06-26 18:31:22 +00001395 Chain, Addr, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001396 Chain = Addr.getValue(1);
1397 if (isPIC)
Dale Johannesen33c960f2009-02-04 20:06:27 +00001398 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
1399 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chenga8e29892007-01-19 07:51:42 +00001400}
1401
Dan Gohman475871a2008-07-27 21:46:04 +00001402static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00001403 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001404 unsigned Opc =
1405 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
Dale Johannesende064702009-02-06 21:50:26 +00001406 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
1407 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001408}
1409
Dan Gohman475871a2008-07-27 21:46:04 +00001410static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001411 MVT VT = Op.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001412 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001413 unsigned Opc =
1414 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1415
Dale Johannesende064702009-02-06 21:50:26 +00001416 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
1417 return DAG.getNode(Opc, dl, VT, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001418}
1419
Dan Gohman475871a2008-07-27 21:46:04 +00001420static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001421 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00001422 SDValue Tmp0 = Op.getOperand(0);
1423 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00001424 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001425 MVT VT = Op.getValueType();
1426 MVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001427 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
1428 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Dan Gohman475871a2008-07-27 21:46:04 +00001429 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1430 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001431 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001432}
1433
Dan Gohman475871a2008-07-27 21:46:04 +00001434SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00001435ARMTargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001436 SDValue Chain,
1437 SDValue Dst, SDValue Src,
1438 SDValue Size, unsigned Align,
Dan Gohman707e0182008-04-12 04:36:06 +00001439 bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00001440 const Value *DstSV, uint64_t DstSVOff,
1441 const Value *SrcSV, uint64_t SrcSVOff){
Evan Cheng4102eb52007-10-22 22:11:27 +00001442 // Do repeated 4-byte loads and stores. To be improved.
Dan Gohman707e0182008-04-12 04:36:06 +00001443 // This requires 4-byte alignment.
1444 if ((Align & 3) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00001445 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001446 // This requires the copy size to be a constant, preferrably
1447 // within a subtarget-specific limit.
1448 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
1449 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00001450 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001451 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001452 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00001453 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001454
1455 unsigned BytesLeft = SizeVal & 3;
1456 unsigned NumMemOps = SizeVal >> 2;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001457 unsigned EmittedNumMemOps = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001458 MVT VT = MVT::i32;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001459 unsigned VTSize = 4;
Evan Cheng4102eb52007-10-22 22:11:27 +00001460 unsigned i = 0;
Evan Chenge5e7ce42007-05-18 01:19:57 +00001461 const unsigned MAX_LOADS_IN_LDM = 6;
Dan Gohman475871a2008-07-27 21:46:04 +00001462 SDValue TFOps[MAX_LOADS_IN_LDM];
1463 SDValue Loads[MAX_LOADS_IN_LDM];
Dan Gohman1f13c682008-04-28 17:15:20 +00001464 uint64_t SrcOff = 0, DstOff = 0;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001465
Evan Cheng4102eb52007-10-22 22:11:27 +00001466 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
1467 // same number of stores. The loads and stores will get combined into
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001468 // ldm/stm later on.
Evan Cheng4102eb52007-10-22 22:11:27 +00001469 while (EmittedNumMemOps < NumMemOps) {
1470 for (i = 0;
1471 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001472 Loads[i] = DAG.getLoad(VT, dl, Chain,
1473 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
Evan Cheng4102eb52007-10-22 22:11:27 +00001474 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001475 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001476 TFOps[i] = Loads[i].getValue(1);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001477 SrcOff += VTSize;
1478 }
Dale Johannesen0f502f62009-02-03 22:26:09 +00001479 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001480
Evan Cheng4102eb52007-10-22 22:11:27 +00001481 for (i = 0;
1482 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001483 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Bob Wilson2dc4f542009-03-20 22:42:55 +00001484 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
Evan Cheng4102eb52007-10-22 22:11:27 +00001485 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001486 DstSV, DstSVOff + DstOff);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001487 DstOff += VTSize;
1488 }
Dale Johannesen0f502f62009-02-03 22:26:09 +00001489 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00001490
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001491 EmittedNumMemOps += i;
1492 }
1493
Bob Wilson2dc4f542009-03-20 22:42:55 +00001494 if (BytesLeft == 0)
Evan Cheng4102eb52007-10-22 22:11:27 +00001495 return Chain;
1496
1497 // Issue loads / stores for the trailing (1 - 3) bytes.
1498 unsigned BytesLeftSave = BytesLeft;
1499 i = 0;
1500 while (BytesLeft) {
1501 if (BytesLeft >= 2) {
1502 VT = MVT::i16;
1503 VTSize = 2;
1504 } else {
1505 VT = MVT::i8;
1506 VTSize = 1;
1507 }
1508
Dale Johannesen0f502f62009-02-03 22:26:09 +00001509 Loads[i] = DAG.getLoad(VT, dl, Chain,
1510 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
Evan Cheng4102eb52007-10-22 22:11:27 +00001511 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001512 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001513 TFOps[i] = Loads[i].getValue(1);
1514 ++i;
1515 SrcOff += VTSize;
1516 BytesLeft -= VTSize;
1517 }
Dale Johannesen0f502f62009-02-03 22:26:09 +00001518 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00001519
1520 i = 0;
1521 BytesLeft = BytesLeftSave;
1522 while (BytesLeft) {
1523 if (BytesLeft >= 2) {
1524 VT = MVT::i16;
1525 VTSize = 2;
1526 } else {
1527 VT = MVT::i8;
1528 VTSize = 1;
1529 }
1530
Dale Johannesen0f502f62009-02-03 22:26:09 +00001531 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Bob Wilson2dc4f542009-03-20 22:42:55 +00001532 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
Evan Cheng4102eb52007-10-22 22:11:27 +00001533 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001534 DstSV, DstSVOff + DstOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001535 ++i;
1536 DstOff += VTSize;
1537 BytesLeft -= VTSize;
1538 }
Dale Johannesen0f502f62009-02-03 22:26:09 +00001539 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001540}
1541
Duncan Sands1607f052008-12-01 11:39:25 +00001542static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00001543 SDValue Op = N->getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +00001544 DebugLoc dl = N->getDebugLoc();
Evan Chengc7c77292008-11-04 19:57:48 +00001545 if (N->getValueType(0) == MVT::f64) {
1546 // Turn i64->f64 into FMDRR.
Dale Johannesende064702009-02-06 21:50:26 +00001547 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Evan Chengc7c77292008-11-04 19:57:48 +00001548 DAG.getConstant(0, MVT::i32));
Dale Johannesende064702009-02-06 21:50:26 +00001549 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Evan Chengc7c77292008-11-04 19:57:48 +00001550 DAG.getConstant(1, MVT::i32));
Dale Johannesende064702009-02-06 21:50:26 +00001551 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00001552 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00001553
Evan Chengc7c77292008-11-04 19:57:48 +00001554 // Turn f64->i64 into FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001555 SDValue Cvt = DAG.getNode(ARMISD::FMRRD, dl,
Dale Johannesende064702009-02-06 21:50:26 +00001556 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001557
Chris Lattner27a6c732007-11-24 07:07:01 +00001558 // Merge the pieces into a single i64 value.
Dale Johannesende064702009-02-06 21:50:26 +00001559 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
Chris Lattner27a6c732007-11-24 07:07:01 +00001560}
1561
Duncan Sands1607f052008-12-01 11:39:25 +00001562static SDValue ExpandSRx(SDNode *N, SelectionDAG &DAG, const ARMSubtarget *ST) {
Chris Lattner27a6c732007-11-24 07:07:01 +00001563 assert(N->getValueType(0) == MVT::i64 &&
1564 (N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
1565 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00001566
Chris Lattner27a6c732007-11-24 07:07:01 +00001567 // We only lower SRA, SRL of 1 here, all others use generic lowering.
1568 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001569 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00001570 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00001571
Chris Lattner27a6c732007-11-24 07:07:01 +00001572 // If we are in thumb mode, we don't have RRX.
Duncan Sands1607f052008-12-01 11:39:25 +00001573 if (ST->isThumb()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00001574
Chris Lattner27a6c732007-11-24 07:07:01 +00001575 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Dale Johannesende064702009-02-06 21:50:26 +00001576 DebugLoc dl = N->getDebugLoc();
1577 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Chris Lattner27a6c732007-11-24 07:07:01 +00001578 DAG.getConstant(0, MVT::i32));
Dale Johannesende064702009-02-06 21:50:26 +00001579 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Chris Lattner27a6c732007-11-24 07:07:01 +00001580 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00001581
Chris Lattner27a6c732007-11-24 07:07:01 +00001582 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
1583 // captures the result into a carry flag.
1584 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Dale Johannesende064702009-02-06 21:50:26 +00001585 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001586
Chris Lattner27a6c732007-11-24 07:07:01 +00001587 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Dale Johannesende064702009-02-06 21:50:26 +00001588 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00001589
Chris Lattner27a6c732007-11-24 07:07:01 +00001590 // Merge the pieces into a single i64 value.
Dale Johannesende064702009-02-06 21:50:26 +00001591 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00001592}
1593
Dan Gohman475871a2008-07-27 21:46:04 +00001594SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001595 switch (Op.getOpcode()) {
1596 default: assert(0 && "Don't know how to custom lower this!"); abort();
1597 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001598 case ISD::GlobalAddress:
1599 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
1600 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001601 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00001602 case ISD::CALL: return LowerCALL(Op, DAG);
1603 case ISD::RET: return LowerRET(Op, DAG);
1604 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
1605 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
1606 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
1607 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
1608 case ISD::SINT_TO_FP:
1609 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
1610 case ISD::FP_TO_SINT:
1611 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
1612 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00001613 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00001614 case ISD::RETURNADDR: break;
1615 case ISD::FRAMEADDR: break;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001616 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001617 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00001618 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00001619 case ISD::SRL:
Duncan Sands1607f052008-12-01 11:39:25 +00001620 case ISD::SRA: return ExpandSRx(Op.getNode(), DAG,Subtarget);
Evan Chenga8e29892007-01-19 07:51:42 +00001621 }
Dan Gohman475871a2008-07-27 21:46:04 +00001622 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001623}
1624
Duncan Sands1607f052008-12-01 11:39:25 +00001625/// ReplaceNodeResults - Replace the results of node with an illegal result
1626/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00001627void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
1628 SmallVectorImpl<SDValue>&Results,
1629 SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00001630 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00001631 default:
1632 assert(0 && "Don't know how to custom expand this!");
1633 return;
1634 case ISD::BIT_CONVERT:
1635 Results.push_back(ExpandBIT_CONVERT(N, DAG));
1636 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00001637 case ISD::SRL:
Duncan Sands1607f052008-12-01 11:39:25 +00001638 case ISD::SRA: {
1639 SDValue Res = ExpandSRx(N, DAG, Subtarget);
1640 if (Res.getNode())
1641 Results.push_back(Res);
1642 return;
1643 }
Chris Lattner27a6c732007-11-24 07:07:01 +00001644 }
1645}
Chris Lattner27a6c732007-11-24 07:07:01 +00001646
Evan Chenga8e29892007-01-19 07:51:42 +00001647//===----------------------------------------------------------------------===//
1648// ARM Scheduler Hooks
1649//===----------------------------------------------------------------------===//
1650
1651MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00001652ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00001653 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00001654 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00001655 DebugLoc dl = MI->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001656 switch (MI->getOpcode()) {
1657 default: assert(false && "Unexpected instr type to insert");
1658 case ARM::tMOVCCr: {
1659 // To "insert" a SELECT_CC instruction, we actually have to insert the
1660 // diamond control-flow pattern. The incoming instruction knows the
1661 // destination vreg to set, the condition code register to branch on, the
1662 // true/false values to select between, and a branch opcode to use.
1663 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001664 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00001665 ++It;
1666
1667 // thisMBB:
1668 // ...
1669 // TrueVal = ...
1670 // cmpTY ccX, r1, r2
1671 // bCC copy1MBB
1672 // fallthrough --> copy0MBB
1673 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001674 MachineFunction *F = BB->getParent();
1675 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1676 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00001677 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00001678 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001679 F->insert(It, copy0MBB);
1680 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00001681 // Update machine-CFG edges by first adding all successors of the current
1682 // block to the new block which will contain the Phi node for the select.
1683 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1684 e = BB->succ_end(); i != e; ++i)
1685 sinkMBB->addSuccessor(*i);
1686 // Next, remove all successors of the current block, and add the true
1687 // and fallthrough blocks as its successors.
1688 while(!BB->succ_empty())
1689 BB->removeSuccessor(BB->succ_begin());
1690 BB->addSuccessor(copy0MBB);
1691 BB->addSuccessor(sinkMBB);
1692
1693 // copy0MBB:
1694 // %FalseValue = ...
1695 // # fallthrough to sinkMBB
1696 BB = copy0MBB;
1697
1698 // Update machine-CFG edges
1699 BB->addSuccessor(sinkMBB);
1700
1701 // sinkMBB:
1702 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1703 // ...
1704 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00001705 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00001706 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1707 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1708
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001709 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00001710 return BB;
1711 }
1712 }
1713}
1714
1715//===----------------------------------------------------------------------===//
1716// ARM Optimization Hooks
1717//===----------------------------------------------------------------------===//
1718
Chris Lattnerd1980a52009-03-12 06:52:53 +00001719static
1720SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
1721 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00001722 SelectionDAG &DAG = DCI.DAG;
1723 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1724 MVT VT = N->getValueType(0);
1725 unsigned Opc = N->getOpcode();
1726 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
1727 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
1728 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
1729 ISD::CondCode CC = ISD::SETCC_INVALID;
1730
1731 if (isSlctCC) {
1732 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
1733 } else {
1734 SDValue CCOp = Slct.getOperand(0);
1735 if (CCOp.getOpcode() == ISD::SETCC)
1736 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
1737 }
1738
1739 bool DoXform = false;
1740 bool InvCC = false;
1741 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
1742 "Bad input!");
1743
1744 if (LHS.getOpcode() == ISD::Constant &&
1745 cast<ConstantSDNode>(LHS)->isNullValue()) {
1746 DoXform = true;
1747 } else if (CC != ISD::SETCC_INVALID &&
1748 RHS.getOpcode() == ISD::Constant &&
1749 cast<ConstantSDNode>(RHS)->isNullValue()) {
1750 std::swap(LHS, RHS);
1751 SDValue Op0 = Slct.getOperand(0);
1752 MVT OpVT = isSlctCC ? Op0.getValueType() :
1753 Op0.getOperand(0).getValueType();
1754 bool isInt = OpVT.isInteger();
1755 CC = ISD::getSetCCInverse(CC, isInt);
1756
1757 if (!TLI.isCondCodeLegal(CC, OpVT))
1758 return SDValue(); // Inverse operator isn't legal.
1759
1760 DoXform = true;
1761 InvCC = true;
1762 }
1763
1764 if (DoXform) {
1765 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
1766 if (isSlctCC)
1767 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
1768 Slct.getOperand(0), Slct.getOperand(1), CC);
1769 SDValue CCOp = Slct.getOperand(0);
1770 if (InvCC)
1771 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
1772 CCOp.getOperand(0), CCOp.getOperand(1), CC);
1773 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
1774 CCOp, OtherOp, Result);
1775 }
1776 return SDValue();
1777}
1778
1779/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
1780static SDValue PerformADDCombine(SDNode *N,
1781 TargetLowering::DAGCombinerInfo &DCI) {
1782 // added by evan in r37685 with no testcase.
1783 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001784
Chris Lattnerd1980a52009-03-12 06:52:53 +00001785 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
1786 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
1787 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
1788 if (Result.getNode()) return Result;
1789 }
1790 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
1791 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
1792 if (Result.getNode()) return Result;
1793 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00001794
Chris Lattnerd1980a52009-03-12 06:52:53 +00001795 return SDValue();
1796}
1797
1798/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
1799static SDValue PerformSUBCombine(SDNode *N,
1800 TargetLowering::DAGCombinerInfo &DCI) {
1801 // added by evan in r37685 with no testcase.
1802 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001803
Chris Lattnerd1980a52009-03-12 06:52:53 +00001804 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1805 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
1806 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
1807 if (Result.getNode()) return Result;
1808 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00001809
Chris Lattnerd1980a52009-03-12 06:52:53 +00001810 return SDValue();
1811}
1812
1813
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001814/// PerformFMRRDCombine - Target-specific dag combine xforms for ARMISD::FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001815static SDValue PerformFMRRDCombine(SDNode *N,
1816 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001817 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00001818 SDValue InDouble = N->getOperand(0);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001819 if (InDouble.getOpcode() == ARMISD::FMDRR)
1820 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001821 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001822}
1823
Dan Gohman475871a2008-07-27 21:46:04 +00001824SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001825 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001826 switch (N->getOpcode()) {
1827 default: break;
Chris Lattnerd1980a52009-03-12 06:52:53 +00001828 case ISD::ADD: return PerformADDCombine(N, DCI);
1829 case ISD::SUB: return PerformSUBCombine(N, DCI);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001830 case ARMISD::FMRRD: return PerformFMRRDCombine(N, DCI);
1831 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00001832
Dan Gohman475871a2008-07-27 21:46:04 +00001833 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001834}
1835
Evan Chengb01fad62007-03-12 23:30:29 +00001836/// isLegalAddressImmediate - Return true if the integer value can be used
1837/// as the offset of the target addressing mode for load / store of the
1838/// given type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001839static bool isLegalAddressImmediate(int64_t V, MVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00001840 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00001841 if (V == 0)
1842 return true;
1843
Evan Cheng65011532009-03-09 19:15:00 +00001844 if (!VT.isSimple())
1845 return false;
1846
Evan Chengb01fad62007-03-12 23:30:29 +00001847 if (Subtarget->isThumb()) {
1848 if (V < 0)
1849 return false;
1850
1851 unsigned Scale = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001852 switch (VT.getSimpleVT()) {
Evan Chengb01fad62007-03-12 23:30:29 +00001853 default: return false;
1854 case MVT::i1:
1855 case MVT::i8:
1856 // Scale == 1;
1857 break;
1858 case MVT::i16:
1859 // Scale == 2;
1860 Scale = 2;
1861 break;
1862 case MVT::i32:
1863 // Scale == 4;
1864 Scale = 4;
1865 break;
1866 }
1867
1868 if ((V & (Scale - 1)) != 0)
1869 return false;
1870 V /= Scale;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001871 return V == (V & ((1LL << 5) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001872 }
1873
1874 if (V < 0)
1875 V = - V;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001876 switch (VT.getSimpleVT()) {
Evan Chengb01fad62007-03-12 23:30:29 +00001877 default: return false;
1878 case MVT::i1:
1879 case MVT::i8:
1880 case MVT::i32:
1881 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001882 return V == (V & ((1LL << 12) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001883 case MVT::i16:
1884 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001885 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001886 case MVT::f32:
1887 case MVT::f64:
1888 if (!Subtarget->hasVFP2())
1889 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00001890 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00001891 return false;
1892 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001893 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001894 }
Evan Chenga8e29892007-01-19 07:51:42 +00001895}
1896
Chris Lattner37caf8c2007-04-09 23:33:39 +00001897/// isLegalAddressingMode - Return true if the addressing mode represented
1898/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001899bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00001900 const Type *Ty) const {
Bob Wilson2c7dab12009-04-08 17:55:28 +00001901 MVT VT = getValueType(Ty, true);
1902 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00001903 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00001904
Chris Lattner37caf8c2007-04-09 23:33:39 +00001905 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001906 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001907 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00001908
Chris Lattner37caf8c2007-04-09 23:33:39 +00001909 switch (AM.Scale) {
1910 case 0: // no scale reg, must be "r+i" or "r", or "i".
1911 break;
1912 case 1:
1913 if (Subtarget->isThumb())
1914 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00001915 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00001916 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00001917 // ARM doesn't support any R+R*scale+imm addr modes.
1918 if (AM.BaseOffs)
1919 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00001920
Bob Wilson2c7dab12009-04-08 17:55:28 +00001921 if (!VT.isSimple())
1922 return false;
1923
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001924 int Scale = AM.Scale;
Bob Wilson2c7dab12009-04-08 17:55:28 +00001925 switch (VT.getSimpleVT()) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00001926 default: return false;
1927 case MVT::i1:
1928 case MVT::i8:
1929 case MVT::i32:
1930 case MVT::i64:
1931 // This assumes i64 is legalized to a pair of i32. If not (i.e.
1932 // ldrd / strd are used, then its address mode is same as i16.
1933 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001934 if (Scale < 0) Scale = -Scale;
1935 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001936 return true;
1937 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00001938 return isPowerOf2_32(Scale & ~1);
Chris Lattner37caf8c2007-04-09 23:33:39 +00001939 case MVT::i16:
1940 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001941 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001942 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00001943 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00001944
Chris Lattner37caf8c2007-04-09 23:33:39 +00001945 case MVT::isVoid:
1946 // Note, we allow "void" uses (basically, uses that aren't loads or
1947 // stores), because arm allows folding a scale into many arithmetic
1948 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001949
Chris Lattner37caf8c2007-04-09 23:33:39 +00001950 // Allow r << imm, but the imm has to be a multiple of two.
1951 if (AM.Scale & 1) return false;
1952 return isPowerOf2_32(AM.Scale);
1953 }
1954 break;
Evan Chengb01fad62007-03-12 23:30:29 +00001955 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00001956 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00001957}
1958
Duncan Sands83ec4b62008-06-06 12:08:01 +00001959static bool getIndexedAddressParts(SDNode *Ptr, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001960 bool isSEXTLoad, SDValue &Base,
1961 SDValue &Offset, bool &isInc,
Evan Chenga8e29892007-01-19 07:51:42 +00001962 SelectionDAG &DAG) {
1963 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
1964 return false;
1965
1966 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
1967 // AddressingMode 3
1968 Base = Ptr->getOperand(0);
1969 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001970 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001971 if (RHSC < 0 && RHSC > -256) {
1972 isInc = false;
1973 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1974 return true;
1975 }
1976 }
1977 isInc = (Ptr->getOpcode() == ISD::ADD);
1978 Offset = Ptr->getOperand(1);
1979 return true;
1980 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
1981 // AddressingMode 2
1982 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001983 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001984 if (RHSC < 0 && RHSC > -0x1000) {
1985 isInc = false;
1986 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1987 Base = Ptr->getOperand(0);
1988 return true;
1989 }
1990 }
1991
1992 if (Ptr->getOpcode() == ISD::ADD) {
1993 isInc = true;
1994 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
1995 if (ShOpcVal != ARM_AM::no_shift) {
1996 Base = Ptr->getOperand(1);
1997 Offset = Ptr->getOperand(0);
1998 } else {
1999 Base = Ptr->getOperand(0);
2000 Offset = Ptr->getOperand(1);
2001 }
2002 return true;
2003 }
2004
2005 isInc = (Ptr->getOpcode() == ISD::ADD);
2006 Base = Ptr->getOperand(0);
2007 Offset = Ptr->getOperand(1);
2008 return true;
2009 }
2010
2011 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
2012 return false;
2013}
2014
2015/// getPreIndexedAddressParts - returns true by value, base pointer and
2016/// offset pointer and addressing mode by reference if the node's address
2017/// can be legally represented as pre-indexed load / store address.
2018bool
Dan Gohman475871a2008-07-27 21:46:04 +00002019ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
2020 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00002021 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00002022 SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002023 if (Subtarget->isThumb())
2024 return false;
2025
Duncan Sands83ec4b62008-06-06 12:08:01 +00002026 MVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00002027 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00002028 bool isSEXTLoad = false;
2029 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
2030 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002031 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00002032 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
2033 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
2034 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002035 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00002036 } else
2037 return false;
2038
2039 bool isInc;
Gabor Greifba36cb52008-08-28 21:40:38 +00002040 bool isLegal = getIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base, Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00002041 isInc, DAG);
2042 if (isLegal) {
2043 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
2044 return true;
2045 }
2046 return false;
2047}
2048
2049/// getPostIndexedAddressParts - returns true by value, base pointer and
2050/// offset pointer and addressing mode by reference if this node can be
2051/// combined with a load / store to form a post-indexed load / store.
2052bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00002053 SDValue &Base,
2054 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00002055 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00002056 SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002057 if (Subtarget->isThumb())
2058 return false;
2059
Duncan Sands83ec4b62008-06-06 12:08:01 +00002060 MVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00002061 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00002062 bool isSEXTLoad = false;
2063 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002064 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00002065 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
2066 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002067 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00002068 } else
2069 return false;
2070
2071 bool isInc;
2072 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
2073 isInc, DAG);
2074 if (isLegal) {
2075 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
2076 return true;
2077 }
2078 return false;
2079}
2080
Dan Gohman475871a2008-07-27 21:46:04 +00002081void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00002082 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00002083 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002084 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00002085 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00002086 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002087 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002088 switch (Op.getOpcode()) {
2089 default: break;
2090 case ARMISD::CMOV: {
2091 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00002092 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00002093 if (KnownZero == 0 && KnownOne == 0) return;
2094
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002095 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00002096 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
2097 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00002098 KnownZero &= KnownZeroRHS;
2099 KnownOne &= KnownOneRHS;
2100 return;
2101 }
2102 }
2103}
2104
2105//===----------------------------------------------------------------------===//
2106// ARM Inline Assembly Support
2107//===----------------------------------------------------------------------===//
2108
2109/// getConstraintType - Given a constraint letter, return the type of
2110/// constraint it is for this target.
2111ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00002112ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
2113 if (Constraint.size() == 1) {
2114 switch (Constraint[0]) {
2115 default: break;
2116 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00002117 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00002118 }
Evan Chenga8e29892007-01-19 07:51:42 +00002119 }
Chris Lattner4234f572007-03-25 02:14:49 +00002120 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00002121}
2122
Bob Wilson2dc4f542009-03-20 22:42:55 +00002123std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00002124ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002125 MVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002126 if (Constraint.size() == 1) {
2127 // GCC RS6000 Constraint Letters
2128 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00002129 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002130 if (Subtarget->isThumb())
2131 return std::make_pair(0U, ARM::tGPRRegisterClass);
2132 else
2133 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00002134 case 'r':
2135 return std::make_pair(0U, ARM::GPRRegisterClass);
2136 case 'w':
2137 if (VT == MVT::f32)
2138 return std::make_pair(0U, ARM::SPRRegisterClass);
Evan Cheng0a7baa22007-04-04 00:06:07 +00002139 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00002140 return std::make_pair(0U, ARM::DPRRegisterClass);
2141 break;
Evan Chenga8e29892007-01-19 07:51:42 +00002142 }
2143 }
2144 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2145}
2146
2147std::vector<unsigned> ARMTargetLowering::
2148getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002149 MVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002150 if (Constraint.size() != 1)
2151 return std::vector<unsigned>();
2152
2153 switch (Constraint[0]) { // GCC ARM Constraint Letters
2154 default: break;
2155 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002156 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
2157 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
2158 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002159 case 'r':
2160 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
2161 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
2162 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
2163 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00002164 case 'w':
2165 if (VT == MVT::f32)
2166 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
2167 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
2168 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
2169 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
2170 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
2171 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
2172 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
2173 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
2174 if (VT == MVT::f64)
2175 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
2176 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
2177 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
2178 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
2179 break;
Evan Chenga8e29892007-01-19 07:51:42 +00002180 }
2181
2182 return std::vector<unsigned>();
2183}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00002184
2185/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
2186/// vector. If it is invalid, don't add anything to Ops.
2187void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
2188 char Constraint,
2189 bool hasMemory,
2190 std::vector<SDValue>&Ops,
2191 SelectionDAG &DAG) const {
2192 SDValue Result(0, 0);
2193
2194 switch (Constraint) {
2195 default: break;
2196 case 'I': case 'J': case 'K': case 'L':
2197 case 'M': case 'N': case 'O':
2198 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
2199 if (!C)
2200 return;
2201
2202 int64_t CVal64 = C->getSExtValue();
2203 int CVal = (int) CVal64;
2204 // None of these constraints allow values larger than 32 bits. Check
2205 // that the value fits in an int.
2206 if (CVal != CVal64)
2207 return;
2208
2209 switch (Constraint) {
2210 case 'I':
2211 if (Subtarget->isThumb()) {
2212 // This must be a constant between 0 and 255, for ADD immediates.
2213 if (CVal >= 0 && CVal <= 255)
2214 break;
2215 } else {
2216 // A constant that can be used as an immediate value in a
2217 // data-processing instruction.
2218 if (ARM_AM::getSOImmVal(CVal) != -1)
2219 break;
2220 }
2221 return;
2222
2223 case 'J':
2224 if (Subtarget->isThumb()) {
2225 // This must be a constant between -255 and -1, for negated ADD
2226 // immediates. This can be used in GCC with an "n" modifier that
2227 // prints the negated value, for use with SUB instructions. It is
2228 // not useful otherwise but is implemented for compatibility.
2229 if (CVal >= -255 && CVal <= -1)
2230 break;
2231 } else {
2232 // This must be a constant between -4095 and 4095. It is not clear
2233 // what this constraint is intended for. Implemented for
2234 // compatibility with GCC.
2235 if (CVal >= -4095 && CVal <= 4095)
2236 break;
2237 }
2238 return;
2239
2240 case 'K':
2241 if (Subtarget->isThumb()) {
2242 // A 32-bit value where only one byte has a nonzero value. Exclude
2243 // zero to match GCC. This constraint is used by GCC internally for
2244 // constants that can be loaded with a move/shift combination.
2245 // It is not useful otherwise but is implemented for compatibility.
2246 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
2247 break;
2248 } else {
2249 // A constant whose bitwise inverse can be used as an immediate
2250 // value in a data-processing instruction. This can be used in GCC
2251 // with a "B" modifier that prints the inverted value, for use with
2252 // BIC and MVN instructions. It is not useful otherwise but is
2253 // implemented for compatibility.
2254 if (ARM_AM::getSOImmVal(~CVal) != -1)
2255 break;
2256 }
2257 return;
2258
2259 case 'L':
2260 if (Subtarget->isThumb()) {
2261 // This must be a constant between -7 and 7,
2262 // for 3-operand ADD/SUB immediate instructions.
2263 if (CVal >= -7 && CVal < 7)
2264 break;
2265 } else {
2266 // A constant whose negation can be used as an immediate value in a
2267 // data-processing instruction. This can be used in GCC with an "n"
2268 // modifier that prints the negated value, for use with SUB
2269 // instructions. It is not useful otherwise but is implemented for
2270 // compatibility.
2271 if (ARM_AM::getSOImmVal(-CVal) != -1)
2272 break;
2273 }
2274 return;
2275
2276 case 'M':
2277 if (Subtarget->isThumb()) {
2278 // This must be a multiple of 4 between 0 and 1020, for
2279 // ADD sp + immediate.
2280 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
2281 break;
2282 } else {
2283 // A power of two or a constant between 0 and 32. This is used in
2284 // GCC for the shift amount on shifted register operands, but it is
2285 // useful in general for any shift amounts.
2286 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
2287 break;
2288 }
2289 return;
2290
2291 case 'N':
2292 if (Subtarget->isThumb()) {
2293 // This must be a constant between 0 and 31, for shift amounts.
2294 if (CVal >= 0 && CVal <= 31)
2295 break;
2296 }
2297 return;
2298
2299 case 'O':
2300 if (Subtarget->isThumb()) {
2301 // This must be a multiple of 4 between -508 and 508, for
2302 // ADD/SUB sp = sp + immediate.
2303 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
2304 break;
2305 }
2306 return;
2307 }
2308 Result = DAG.getTargetConstant(CVal, Op.getValueType());
2309 break;
2310 }
2311
2312 if (Result.getNode()) {
2313 Ops.push_back(Result);
2314 return;
2315 }
2316 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
2317 Ops, DAG);
2318}