blob: 8e3aa833c63dff01a96a8942e3cfbbe7ea6d56a3 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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"
Anton Korobeynikov0a029782009-08-21 12:41:24 +000020#include "ARMPerfectShuffle.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "ARMRegisterInfo.h"
22#include "ARMSubtarget.h"
23#include "ARMTargetMachine.h"
Chris Lattnerb5c952a2009-08-02 00:34:36 +000024#include "ARMTargetObjectFile.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/CallingConv.h"
26#include "llvm/Constants.h"
Bob Wilsonfd451172009-04-17 19:07:39 +000027#include "llvm/Function.h"
Benjamin Kramerfdaec7c2009-10-20 11:44:38 +000028#include "llvm/GlobalValue.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029#include "llvm/Instruction.h"
Lauro Ramos Venancio5a87a8a2007-11-08 17:20:05 +000030#include "llvm/Intrinsics.h"
Benjamin Kramerfdaec7c2009-10-20 11:44:38 +000031#include "llvm/Type.h"
Bob Wilsonfd451172009-04-17 19:07:39 +000032#include "llvm/CodeGen/CallingConvLower.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/CodeGen/MachineBasicBlock.h"
34#include "llvm/CodeGen/MachineFrameInfo.h"
35#include "llvm/CodeGen/MachineFunction.h"
36#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner1b989192007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilsonfd451172009-04-17 19:07:39 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040#include "llvm/Target/TargetOptions.h"
41#include "llvm/ADT/VectorExtras.h"
Edwin Török3cb88482009-07-08 18:01:40 +000042#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043#include "llvm/Support/MathExtras.h"
Jim Grosbach5e0257f2009-09-01 01:57:56 +000044#include <sstream>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045using namespace llvm;
46
Owen Andersonac9de032009-08-10 22:56:29 +000047static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilsonfd451172009-04-17 19:07:39 +000048 CCValAssign::LocInfo &LocInfo,
49 ISD::ArgFlagsTy &ArgFlags,
50 CCState &State);
Owen Andersonac9de032009-08-10 22:56:29 +000051static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilsonfd451172009-04-17 19:07:39 +000052 CCValAssign::LocInfo &LocInfo,
53 ISD::ArgFlagsTy &ArgFlags,
54 CCState &State);
Owen Andersonac9de032009-08-10 22:56:29 +000055static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilsonfd451172009-04-17 19:07:39 +000056 CCValAssign::LocInfo &LocInfo,
57 ISD::ArgFlagsTy &ArgFlags,
58 CCState &State);
Owen Andersonac9de032009-08-10 22:56:29 +000059static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilsonfd451172009-04-17 19:07:39 +000060 CCValAssign::LocInfo &LocInfo,
61 ISD::ArgFlagsTy &ArgFlags,
62 CCState &State);
63
Owen Andersonac9de032009-08-10 22:56:29 +000064void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
65 EVT PromotedBitwiseVT) {
Bob Wilsone60fee02009-06-22 23:27:02 +000066 if (VT != PromotedLdStVT) {
Owen Andersona4509892009-08-10 20:18:46 +000067 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersona0c69eb2009-08-10 20:46:15 +000068 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
69 PromotedLdStVT.getSimpleVT());
Bob Wilsone60fee02009-06-22 23:27:02 +000070
Owen Andersona4509892009-08-10 20:18:46 +000071 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach770d7182009-08-11 15:33:49 +000072 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersona0c69eb2009-08-10 20:46:15 +000073 PromotedLdStVT.getSimpleVT());
Bob Wilsone60fee02009-06-22 23:27:02 +000074 }
75
Owen Andersonac9de032009-08-10 22:56:29 +000076 EVT ElemTy = VT.getVectorElementType();
Owen Anderson36e3a6e2009-08-11 20:47:22 +000077 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Andersona4509892009-08-10 20:18:46 +000078 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Owen Anderson36e3a6e2009-08-11 20:47:22 +000079 if (ElemTy == MVT::i8 || ElemTy == MVT::i16)
Owen Andersona4509892009-08-10 20:18:46 +000080 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson94ef1622009-09-16 20:20:44 +000081 if (ElemTy != MVT::i32) {
82 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
83 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
84 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
85 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
86 }
Owen Andersona4509892009-08-10 20:18:46 +000087 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
88 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Owen Andersona4509892009-08-10 20:18:46 +000089 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Custom);
Anton Korobeynikov755fa0b2009-08-21 12:40:35 +000090 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
Bob Wilsone60fee02009-06-22 23:27:02 +000091 if (VT.isInteger()) {
Owen Andersona4509892009-08-10 20:18:46 +000092 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
93 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
94 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilsone60fee02009-06-22 23:27:02 +000095 }
96
97 // Promote all bit-wise operations.
98 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Andersona4509892009-08-10 20:18:46 +000099 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersona0c69eb2009-08-10 20:46:15 +0000100 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
101 PromotedBitwiseVT.getSimpleVT());
Owen Andersona4509892009-08-10 20:18:46 +0000102 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach770d7182009-08-11 15:33:49 +0000103 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersona0c69eb2009-08-10 20:46:15 +0000104 PromotedBitwiseVT.getSimpleVT());
Owen Andersona4509892009-08-10 20:18:46 +0000105 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach770d7182009-08-11 15:33:49 +0000106 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersona0c69eb2009-08-10 20:46:15 +0000107 PromotedBitwiseVT.getSimpleVT());
Bob Wilsone60fee02009-06-22 23:27:02 +0000108 }
Bob Wilson098abb42009-09-16 00:17:28 +0000109
110 // Neon does not support vector divide/remainder operations.
111 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
112 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
113 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
114 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
115 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
116 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilsone60fee02009-06-22 23:27:02 +0000117}
118
Owen Andersonac9de032009-08-10 22:56:29 +0000119void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilsone60fee02009-06-22 23:27:02 +0000120 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000121 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilsone60fee02009-06-22 23:27:02 +0000122}
123
Owen Andersonac9de032009-08-10 22:56:29 +0000124void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilsone60fee02009-06-22 23:27:02 +0000125 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000126 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilsone60fee02009-06-22 23:27:02 +0000127}
128
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000129static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
130 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Chris Lattner01316272009-07-31 17:42:42 +0000131 return new TargetLoweringObjectFileMachO();
Chris Lattnerb5c952a2009-08-02 00:34:36 +0000132 return new ARMElfTargetObjectFile();
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000133}
134
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge4582d92009-11-06 22:24:13 +0000136 : TargetLowering(TM, createTLOF(TM)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 Subtarget = &TM.getSubtarget<ARMSubtarget>();
138
139 if (Subtarget->isTargetDarwin()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 // Uses VFP for Thumb libfuncs if available.
141 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
142 // Single-precision floating-point arithmetic.
143 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
144 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
145 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
146 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
147
148 // Double-precision floating-point arithmetic.
149 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
150 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
151 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
152 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
153
154 // Single-precision comparisons.
155 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
156 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
157 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
158 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
159 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
160 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
161 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
162 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
163
164 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
165 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
166 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
167 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
168 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
169 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
170 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
171 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
172
173 // Double-precision comparisons.
174 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
175 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
176 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
177 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
178 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
179 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
180 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
181 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
182
183 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
184 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
185 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
186 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
187 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
188 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
189 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
190 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
191
192 // Floating-point to integer conversions.
193 // i64 conversions are done via library routines even when generating VFP
194 // instructions, so use the same ones.
195 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
196 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
197 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
198 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
199
200 // Conversions between floating types.
201 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
202 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
203
204 // Integer to floating-point conversions.
205 // i64 conversions are done via library routines even when generating VFP
206 // instructions, so use the same ones.
Bob Wilson45f01d92009-03-20 23:16:43 +0000207 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
208 // e.g., __floatunsidf vs. __floatunssidfvfp.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
210 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
211 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
212 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
213 }
214 }
215
Bob Wilson75188792009-05-22 17:38:41 +0000216 // These libcalls are not available in 32-bit.
217 setLibcallName(RTLIB::SHL_I128, 0);
218 setLibcallName(RTLIB::SRL_I128, 0);
219 setLibcallName(RTLIB::SRA_I128, 0);
220
Anton Korobeynikovcdab71f2009-08-14 20:10:52 +0000221 // Libcalls should use the AAPCS base standard ABI, even if hard float
222 // is in effect, as per the ARM RTABI specification, section 4.1.2.
223 if (Subtarget->isAAPCS_ABI()) {
224 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
225 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
226 CallingConv::ARM_AAPCS);
227 }
228 }
229
David Goodwin4a897932009-07-08 23:10:31 +0000230 if (Subtarget->isThumb1Only())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000231 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach0e4e9742009-04-07 20:34:09 +0000232 else
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000233 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwin4a897932009-07-08 23:10:31 +0000234 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000235 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
236 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson0eb45b42009-03-20 22:42:55 +0000237
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000238 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239 }
Bob Wilsone60fee02009-06-22 23:27:02 +0000240
241 if (Subtarget->hasNEON()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000242 addDRTypeForNEON(MVT::v2f32);
243 addDRTypeForNEON(MVT::v8i8);
244 addDRTypeForNEON(MVT::v4i16);
245 addDRTypeForNEON(MVT::v2i32);
246 addDRTypeForNEON(MVT::v1i64);
Bob Wilsone60fee02009-06-22 23:27:02 +0000247
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000248 addQRTypeForNEON(MVT::v4f32);
249 addQRTypeForNEON(MVT::v2f64);
250 addQRTypeForNEON(MVT::v16i8);
251 addQRTypeForNEON(MVT::v8i16);
252 addQRTypeForNEON(MVT::v4i32);
253 addQRTypeForNEON(MVT::v2i64);
Bob Wilsone60fee02009-06-22 23:27:02 +0000254
Bob Wilsone8fefa02009-09-15 23:55:57 +0000255 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
256 // neither Neon nor VFP support any arithmetic operations on it.
257 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
258 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
259 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
260 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
261 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
262 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
263 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
264 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
265 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
266 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
267 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
268 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
269 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
270 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
271 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
272 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
273 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
274 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
275 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
276 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
277 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
278 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
279 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
280 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
281
Bob Wilson71dd44d2009-09-16 00:32:15 +0000282 // Neon does not support some operations on v1i64 and v2i64 types.
283 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
284 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
285 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
286 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
287
Bob Wilsone60fee02009-06-22 23:27:02 +0000288 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
289 setTargetDAGCombine(ISD::SHL);
290 setTargetDAGCombine(ISD::SRL);
291 setTargetDAGCombine(ISD::SRA);
292 setTargetDAGCombine(ISD::SIGN_EXTEND);
293 setTargetDAGCombine(ISD::ZERO_EXTEND);
294 setTargetDAGCombine(ISD::ANY_EXTEND);
295 }
296
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297 computeRegisterProperties();
298
299 // ARM does not have f32 extending load.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000300 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301
Duncan Sands082524c2008-01-23 20:39:46 +0000302 // ARM does not have i1 sign extending load.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000303 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sands082524c2008-01-23 20:39:46 +0000304
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenga90942e2009-07-02 07:28:31 +0000306 if (!Subtarget->isThumb1Only()) {
307 for (unsigned im = (unsigned)ISD::PRE_INC;
308 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000309 setIndexedLoadAction(im, MVT::i1, Legal);
310 setIndexedLoadAction(im, MVT::i8, Legal);
311 setIndexedLoadAction(im, MVT::i16, Legal);
312 setIndexedLoadAction(im, MVT::i32, Legal);
313 setIndexedStoreAction(im, MVT::i1, Legal);
314 setIndexedStoreAction(im, MVT::i8, Legal);
315 setIndexedStoreAction(im, MVT::i16, Legal);
316 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenga90942e2009-07-02 07:28:31 +0000317 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318 }
319
320 // i64 operation support.
Evan Chenga5626262009-07-07 01:17:28 +0000321 if (Subtarget->isThumb1Only()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000322 setOperationAction(ISD::MUL, MVT::i64, Expand);
323 setOperationAction(ISD::MULHU, MVT::i32, Expand);
324 setOperationAction(ISD::MULHS, MVT::i32, Expand);
325 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
326 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000328 setOperationAction(ISD::MUL, MVT::i64, Expand);
329 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng68e4b582009-08-01 00:16:10 +0000330 if (!Subtarget->hasV6Ops())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000331 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332 }
Jim Grosbach998eacc2009-10-31 19:38:01 +0000333 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach5480bad2009-10-31 21:00:56 +0000334 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbacha3bddfb2009-10-31 21:42:19 +0000335 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000336 setOperationAction(ISD::SRL, MVT::i64, Custom);
337 setOperationAction(ISD::SRA, MVT::i64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338
339 // ARM does not have ROTL.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000340 setOperationAction(ISD::ROTL, MVT::i32, Expand);
341 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
342 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin7e2af1b2009-06-26 20:47:43 +0000343 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000344 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345
346 // Only ARMv6 has BSWAP.
347 if (!Subtarget->hasV6Ops())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000348 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349
350 // These are expanded into libcalls.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000351 setOperationAction(ISD::SDIV, MVT::i32, Expand);
352 setOperationAction(ISD::UDIV, MVT::i32, Expand);
353 setOperationAction(ISD::SREM, MVT::i32, Expand);
354 setOperationAction(ISD::UREM, MVT::i32, Expand);
355 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
356 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson0eb45b42009-03-20 22:42:55 +0000357
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 // Support label based line numbers.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000359 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000360
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000361 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
362 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
363 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
364 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson8f743382009-10-30 05:45:42 +0000365 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367 // Use the default implementation.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000368 setOperationAction(ISD::VASTART, MVT::Other, Custom);
369 setOperationAction(ISD::VAARG, MVT::Other, Expand);
370 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
371 setOperationAction(ISD::VAEND, MVT::Other, Expand);
372 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
373 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachf2d915d2009-08-12 17:38:44 +0000374 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
375 // FIXME: Shouldn't need this, since no register is used, but the legalizer
376 // doesn't yet know how to not do that for SjLj.
377 setExceptionSelectorRegister(ARM::R0);
Evan Cheng815c23a2009-08-07 00:34:42 +0000378 if (Subtarget->isThumb())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000379 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Evan Cheng815c23a2009-08-07 00:34:42 +0000380 else
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000381 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
382 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383
Evan Cheng0f994ed2009-07-03 01:43:10 +0000384 if (!Subtarget->hasV6Ops() && !Subtarget->isThumb2()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000385 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
386 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000388 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000389
David Goodwin4a897932009-07-08 23:10:31 +0000390 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
Jim Grosbache2fda532009-11-09 00:11:35 +0000391 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR iff target supports vfp2.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000392 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancio5a87a8a2007-11-08 17:20:05 +0000393
394 // We want to custom lower some of our intrinsics.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000395 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Lauro Ramos Venancio5a87a8a2007-11-08 17:20:05 +0000396
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000397 setOperationAction(ISD::SETCC, MVT::i32, Expand);
398 setOperationAction(ISD::SETCC, MVT::f32, Expand);
399 setOperationAction(ISD::SETCC, MVT::f64, Expand);
400 setOperationAction(ISD::SELECT, MVT::i32, Expand);
401 setOperationAction(ISD::SELECT, MVT::f32, Expand);
402 setOperationAction(ISD::SELECT, MVT::f64, Expand);
403 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
404 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
405 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000406
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000407 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
408 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
409 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
410 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
411 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000412
Dan Gohman2f7b1982007-10-11 23:21:31 +0000413 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000414 setOperationAction(ISD::FSIN, MVT::f64, Expand);
415 setOperationAction(ISD::FSIN, MVT::f32, Expand);
416 setOperationAction(ISD::FCOS, MVT::f32, Expand);
417 setOperationAction(ISD::FCOS, MVT::f64, Expand);
418 setOperationAction(ISD::FREM, MVT::f64, Expand);
419 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwin4a897932009-07-08 23:10:31 +0000420 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000421 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
422 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Chenga8740032008-04-01 01:50:16 +0000423 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000424 setOperationAction(ISD::FPOW, MVT::f64, Expand);
425 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson0eb45b42009-03-20 22:42:55 +0000426
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
David Goodwin4a897932009-07-08 23:10:31 +0000428 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000429 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
430 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
431 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
432 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Evan Chenga8740032008-04-01 01:50:16 +0000433 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434
Chris Lattner900cddb2007-11-27 22:36:16 +0000435 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache2fda532009-11-09 00:11:35 +0000436 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnere4577dc2009-03-12 06:52:53 +0000437 setTargetDAGCombine(ISD::ADD);
438 setTargetDAGCombine(ISD::SUB);
Bob Wilson0eb45b42009-03-20 22:42:55 +0000439
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440 setStackPointerRegisterToSaveRestore(ARM::SP);
441 setSchedulingPreference(SchedulingForRegPressure);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442
Evan Chengff5c7c42009-08-15 07:59:10 +0000443 // FIXME: If-converter should use instruction latency to determine
444 // profitability rather than relying on fixed limits.
445 if (Subtarget->getCPUString() == "generic") {
446 // Generic (and overly aggressive) if-conversion limits.
447 setIfCvtBlockSizeLimit(10);
448 setIfCvtDupBlockSizeLimit(2);
449 } else if (Subtarget->hasV6Ops()) {
450 setIfCvtBlockSizeLimit(2);
451 setIfCvtDupBlockSizeLimit(1);
452 } else {
453 setIfCvtBlockSizeLimit(3);
454 setIfCvtDupBlockSizeLimit(2);
Evan Cheng88e78d22009-06-19 01:51:50 +0000455 }
456
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000457 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Bob Wilson6c05eb42009-05-18 20:55:32 +0000458 // Do not enable CodePlacementOpt for now: it currently runs after the
459 // ARMConstantIslandPass and messes up branch relaxation and placement
460 // of constant islands.
461 // benefitFromCodePlacementOpt = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000462}
463
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
465 switch (Opcode) {
466 default: return 0;
467 case ARMISD::Wrapper: return "ARMISD::Wrapper";
468 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
469 case ARMISD::CALL: return "ARMISD::CALL";
470 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
471 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
472 case ARMISD::tCALL: return "ARMISD::tCALL";
473 case ARMISD::BRCOND: return "ARMISD::BRCOND";
474 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng1b2b3e22009-07-29 02:18:14 +0000475 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000476 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
477 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
478 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwin8bdcbb32009-06-29 15:33:01 +0000479 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480 case ARMISD::CMPFP: return "ARMISD::CMPFP";
481 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
482 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
483 case ARMISD::CMOV: return "ARMISD::CMOV";
484 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson0eb45b42009-03-20 22:42:55 +0000485
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486 case ARMISD::FTOSI: return "ARMISD::FTOSI";
487 case ARMISD::FTOUI: return "ARMISD::FTOUI";
488 case ARMISD::SITOF: return "ARMISD::SITOF";
489 case ARMISD::UITOF: return "ARMISD::UITOF";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000490
491 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
492 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
493 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson0eb45b42009-03-20 22:42:55 +0000494
Jim Grosbache2fda532009-11-09 00:11:35 +0000495 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
496 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000497
Evan Chengfa281b82009-10-28 06:55:03 +0000498 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
499 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
500
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilsone60fee02009-06-22 23:27:02 +0000502
Evan Cheng815c23a2009-08-07 00:34:42 +0000503 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
504
Bob Wilsone60fee02009-06-22 23:27:02 +0000505 case ARMISD::VCEQ: return "ARMISD::VCEQ";
506 case ARMISD::VCGE: return "ARMISD::VCGE";
507 case ARMISD::VCGEU: return "ARMISD::VCGEU";
508 case ARMISD::VCGT: return "ARMISD::VCGT";
509 case ARMISD::VCGTU: return "ARMISD::VCGTU";
510 case ARMISD::VTST: return "ARMISD::VTST";
511
512 case ARMISD::VSHL: return "ARMISD::VSHL";
513 case ARMISD::VSHRs: return "ARMISD::VSHRs";
514 case ARMISD::VSHRu: return "ARMISD::VSHRu";
515 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
516 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
517 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
518 case ARMISD::VSHRN: return "ARMISD::VSHRN";
519 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
520 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
521 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
522 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
523 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
524 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
525 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
526 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
527 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
528 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
529 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
530 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
531 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
532 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonf4f1a272009-08-14 05:13:08 +0000533 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson206f6c42009-08-14 05:08:32 +0000534 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson3ac39132009-08-19 17:03:43 +0000535 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilson08479272009-08-12 22:31:50 +0000536 case ARMISD::VREV64: return "ARMISD::VREV64";
537 case ARMISD::VREV32: return "ARMISD::VREV32";
538 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov394bbb82009-08-21 12:41:42 +0000539 case ARMISD::VZIP: return "ARMISD::VZIP";
540 case ARMISD::VUZP: return "ARMISD::VUZP";
541 case ARMISD::VTRN: return "ARMISD::VTRN";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000542 }
543}
544
Bill Wendling045f2632009-07-01 18:50:55 +0000545/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling25a8ae32009-06-30 22:38:32 +0000546unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Evan Cheng6f2bf0b2009-10-02 06:57:25 +0000547 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 0 : 1;
Bill Wendling25a8ae32009-06-30 22:38:32 +0000548}
549
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000550//===----------------------------------------------------------------------===//
551// Lowering Code
552//===----------------------------------------------------------------------===//
553
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000554/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
555static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
556 switch (CC) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000557 default: llvm_unreachable("Unknown condition code!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000558 case ISD::SETNE: return ARMCC::NE;
559 case ISD::SETEQ: return ARMCC::EQ;
560 case ISD::SETGT: return ARMCC::GT;
561 case ISD::SETGE: return ARMCC::GE;
562 case ISD::SETLT: return ARMCC::LT;
563 case ISD::SETLE: return ARMCC::LE;
564 case ISD::SETUGT: return ARMCC::HI;
565 case ISD::SETUGE: return ARMCC::HS;
566 case ISD::SETULT: return ARMCC::LO;
567 case ISD::SETULE: return ARMCC::LS;
568 }
569}
570
Bob Wilson10371a52009-09-09 23:14:54 +0000571/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
572static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000573 ARMCC::CondCodes &CondCode2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574 CondCode2 = ARMCC::AL;
575 switch (CC) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000576 default: llvm_unreachable("Unknown FP condition!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000577 case ISD::SETEQ:
578 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
579 case ISD::SETGT:
580 case ISD::SETOGT: CondCode = ARMCC::GT; break;
581 case ISD::SETGE:
582 case ISD::SETOGE: CondCode = ARMCC::GE; break;
583 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilson10371a52009-09-09 23:14:54 +0000584 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000585 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
586 case ISD::SETO: CondCode = ARMCC::VC; break;
587 case ISD::SETUO: CondCode = ARMCC::VS; break;
588 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
589 case ISD::SETUGT: CondCode = ARMCC::HI; break;
590 case ISD::SETUGE: CondCode = ARMCC::PL; break;
591 case ISD::SETLT:
592 case ISD::SETULT: CondCode = ARMCC::LT; break;
593 case ISD::SETLE:
594 case ISD::SETULE: CondCode = ARMCC::LE; break;
595 case ISD::SETNE:
596 case ISD::SETUNE: CondCode = ARMCC::NE; break;
597 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598}
599
Bob Wilsonfd451172009-04-17 19:07:39 +0000600//===----------------------------------------------------------------------===//
601// Calling Convention Implementation
Bob Wilsonfd451172009-04-17 19:07:39 +0000602//===----------------------------------------------------------------------===//
603
604#include "ARMGenCallingConv.inc"
605
606// APCS f64 is in register pairs, possibly split to stack
Owen Andersonac9de032009-08-10 22:56:29 +0000607static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilsone60fee02009-06-22 23:27:02 +0000608 CCValAssign::LocInfo &LocInfo,
609 CCState &State, bool CanFail) {
610 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
611
612 // Try to get the first register.
613 if (unsigned Reg = State.AllocateReg(RegList, 4))
614 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
615 else {
616 // For the 2nd half of a v2f64, do not fail.
617 if (CanFail)
618 return false;
619
620 // Put the whole thing on the stack.
621 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
622 State.AllocateStack(8, 4),
623 LocVT, LocInfo));
624 return true;
625 }
626
627 // Try to get the second register.
628 if (unsigned Reg = State.AllocateReg(RegList, 4))
629 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
630 else
631 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
632 State.AllocateStack(4, 4),
633 LocVT, LocInfo));
634 return true;
635}
636
Owen Andersonac9de032009-08-10 22:56:29 +0000637static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilsonfd451172009-04-17 19:07:39 +0000638 CCValAssign::LocInfo &LocInfo,
639 ISD::ArgFlagsTy &ArgFlags,
640 CCState &State) {
Bob Wilsone60fee02009-06-22 23:27:02 +0000641 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
642 return false;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000643 if (LocVT == MVT::v2f64 &&
Bob Wilsone60fee02009-06-22 23:27:02 +0000644 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
645 return false;
Bob Wilson9bd06cf2009-04-17 20:40:45 +0000646 return true; // we handled it
Bob Wilsonfd451172009-04-17 19:07:39 +0000647}
648
649// AAPCS f64 is in aligned register pairs
Owen Andersonac9de032009-08-10 22:56:29 +0000650static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilsone60fee02009-06-22 23:27:02 +0000651 CCValAssign::LocInfo &LocInfo,
652 CCState &State, bool CanFail) {
653 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
654 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
655
656 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
657 if (Reg == 0) {
658 // For the 2nd half of a v2f64, do not just fail.
659 if (CanFail)
660 return false;
661
662 // Put the whole thing on the stack.
663 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
664 State.AllocateStack(8, 8),
665 LocVT, LocInfo));
666 return true;
667 }
668
669 unsigned i;
670 for (i = 0; i < 2; ++i)
671 if (HiRegList[i] == Reg)
672 break;
673
674 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
675 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
676 LocVT, LocInfo));
677 return true;
678}
679
Owen Andersonac9de032009-08-10 22:56:29 +0000680static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilsonfd451172009-04-17 19:07:39 +0000681 CCValAssign::LocInfo &LocInfo,
682 ISD::ArgFlagsTy &ArgFlags,
683 CCState &State) {
Bob Wilsone60fee02009-06-22 23:27:02 +0000684 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
685 return false;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000686 if (LocVT == MVT::v2f64 &&
Bob Wilsone60fee02009-06-22 23:27:02 +0000687 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
688 return false;
689 return true; // we handled it
690}
691
Owen Andersonac9de032009-08-10 22:56:29 +0000692static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilsone60fee02009-06-22 23:27:02 +0000693 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilsonfd451172009-04-17 19:07:39 +0000694 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
695 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
696
Bob Wilson9bd06cf2009-04-17 20:40:45 +0000697 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
698 if (Reg == 0)
699 return false; // we didn't handle it
Bob Wilsonfd451172009-04-17 19:07:39 +0000700
Bob Wilson9bd06cf2009-04-17 20:40:45 +0000701 unsigned i;
702 for (i = 0; i < 2; ++i)
703 if (HiRegList[i] == Reg)
704 break;
Bob Wilsonfd451172009-04-17 19:07:39 +0000705
Bob Wilsone60fee02009-06-22 23:27:02 +0000706 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilson9bd06cf2009-04-17 20:40:45 +0000707 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilsone60fee02009-06-22 23:27:02 +0000708 LocVT, LocInfo));
709 return true;
Bob Wilsonfd451172009-04-17 19:07:39 +0000710}
711
Owen Andersonac9de032009-08-10 22:56:29 +0000712static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilsonfd451172009-04-17 19:07:39 +0000713 CCValAssign::LocInfo &LocInfo,
714 ISD::ArgFlagsTy &ArgFlags,
715 CCState &State) {
Bob Wilsone60fee02009-06-22 23:27:02 +0000716 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
717 return false;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000718 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilsone60fee02009-06-22 23:27:02 +0000719 return false;
Bob Wilson9bd06cf2009-04-17 20:40:45 +0000720 return true; // we handled it
Bob Wilsonfd451172009-04-17 19:07:39 +0000721}
722
Owen Andersonac9de032009-08-10 22:56:29 +0000723static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilsonfd451172009-04-17 19:07:39 +0000724 CCValAssign::LocInfo &LocInfo,
725 ISD::ArgFlagsTy &ArgFlags,
726 CCState &State) {
727 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
728 State);
729}
730
Anton Korobeynikov1395c2c2009-06-16 18:50:49 +0000731/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
732/// given CallingConvention value.
Sandeep Patel5838baa2009-09-02 08:44:58 +0000733CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov02e15b82009-08-05 19:04:42 +0000734 bool Return,
735 bool isVarArg) const {
Anton Korobeynikov1395c2c2009-06-16 18:50:49 +0000736 switch (CC) {
737 default:
Anton Korobeynikov02e15b82009-08-05 19:04:42 +0000738 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov1395c2c2009-06-16 18:50:49 +0000739 case CallingConv::C:
740 case CallingConv::Fast:
Anton Korobeynikov02e15b82009-08-05 19:04:42 +0000741 // Use target triple & subtarget features to do actual dispatch.
742 if (Subtarget->isAAPCS_ABI()) {
743 if (Subtarget->hasVFP2() &&
744 FloatABIType == FloatABI::Hard && !isVarArg)
745 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
746 else
747 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
748 } else
749 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov1395c2c2009-06-16 18:50:49 +0000750 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov02e15b82009-08-05 19:04:42 +0000751 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov1395c2c2009-06-16 18:50:49 +0000752 case CallingConv::ARM_AAPCS:
Anton Korobeynikov02e15b82009-08-05 19:04:42 +0000753 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov1395c2c2009-06-16 18:50:49 +0000754 case CallingConv::ARM_APCS:
Anton Korobeynikov02e15b82009-08-05 19:04:42 +0000755 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov1395c2c2009-06-16 18:50:49 +0000756 }
757}
758
Dan Gohman9178de12009-08-05 01:29:28 +0000759/// LowerCallResult - Lower the result values of a call into the
760/// appropriate copies out of appropriate physical registers.
761SDValue
762ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel5838baa2009-09-02 08:44:58 +0000763 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman9178de12009-08-05 01:29:28 +0000764 const SmallVectorImpl<ISD::InputArg> &Ins,
765 DebugLoc dl, SelectionDAG &DAG,
766 SmallVectorImpl<SDValue> &InVals) {
Bob Wilsonfd451172009-04-17 19:07:39 +0000767
Bob Wilsonfd451172009-04-17 19:07:39 +0000768 // Assign locations to each value returned by this call.
769 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman9178de12009-08-05 01:29:28 +0000770 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Anderson175b6542009-07-22 00:24:57 +0000771 RVLocs, *DAG.getContext());
Dan Gohman9178de12009-08-05 01:29:28 +0000772 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov02e15b82009-08-05 19:04:42 +0000773 CCAssignFnForNode(CallConv, /* Return*/ true,
774 isVarArg));
Bob Wilsonfd451172009-04-17 19:07:39 +0000775
776 // Copy all of the result registers out of their specified physreg.
777 for (unsigned i = 0; i != RVLocs.size(); ++i) {
778 CCValAssign VA = RVLocs[i];
779
Bob Wilsonb5eefaa2009-04-25 00:33:20 +0000780 SDValue Val;
Bob Wilsonfd451172009-04-17 19:07:39 +0000781 if (VA.needsCustom()) {
Bob Wilsone60fee02009-06-22 23:27:02 +0000782 // Handle f64 or half of a v2f64.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000783 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonfd451172009-04-17 19:07:39 +0000784 InFlag);
Bob Wilson99194c92009-04-24 17:00:36 +0000785 Chain = Lo.getValue(1);
786 InFlag = Lo.getValue(2);
Bob Wilsonfd451172009-04-17 19:07:39 +0000787 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000788 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson99194c92009-04-24 17:00:36 +0000789 InFlag);
790 Chain = Hi.getValue(1);
791 InFlag = Hi.getValue(2);
Jim Grosbache2fda532009-11-09 00:11:35 +0000792 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilsone60fee02009-06-22 23:27:02 +0000793
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000794 if (VA.getLocVT() == MVT::v2f64) {
795 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
796 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
797 DAG.getConstant(0, MVT::i32));
Bob Wilsone60fee02009-06-22 23:27:02 +0000798
799 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000800 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilsone60fee02009-06-22 23:27:02 +0000801 Chain = Lo.getValue(1);
802 InFlag = Lo.getValue(2);
803 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000804 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilsone60fee02009-06-22 23:27:02 +0000805 Chain = Hi.getValue(1);
806 InFlag = Hi.getValue(2);
Jim Grosbache2fda532009-11-09 00:11:35 +0000807 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000808 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
809 DAG.getConstant(1, MVT::i32));
Bob Wilsone60fee02009-06-22 23:27:02 +0000810 }
Bob Wilsonfd451172009-04-17 19:07:39 +0000811 } else {
Bob Wilsonb5eefaa2009-04-25 00:33:20 +0000812 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
813 InFlag);
Bob Wilson99194c92009-04-24 17:00:36 +0000814 Chain = Val.getValue(1);
815 InFlag = Val.getValue(2);
Bob Wilsonfd451172009-04-17 19:07:39 +0000816 }
Bob Wilsonb5eefaa2009-04-25 00:33:20 +0000817
818 switch (VA.getLocInfo()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000819 default: llvm_unreachable("Unknown loc info!");
Bob Wilsonb5eefaa2009-04-25 00:33:20 +0000820 case CCValAssign::Full: break;
821 case CCValAssign::BCvt:
822 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
823 break;
824 }
825
Dan Gohman9178de12009-08-05 01:29:28 +0000826 InVals.push_back(Val);
Bob Wilsonfd451172009-04-17 19:07:39 +0000827 }
828
Dan Gohman9178de12009-08-05 01:29:28 +0000829 return Chain;
Bob Wilsonfd451172009-04-17 19:07:39 +0000830}
831
832/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
833/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilson7cd865e2009-04-17 20:35:10 +0000834/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilsonfd451172009-04-17 19:07:39 +0000835/// a byval function parameter.
836/// Sometimes what we are copying is the end of a larger object, the part that
837/// does not fit in registers.
838static SDValue
839CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
840 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
841 DebugLoc dl) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000842 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilsonfd451172009-04-17 19:07:39 +0000843 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
844 /*AlwaysInline=*/false, NULL, 0, NULL, 0);
845}
846
Bob Wilson7cd865e2009-04-17 20:35:10 +0000847/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsonfd451172009-04-17 19:07:39 +0000848SDValue
Dan Gohman9178de12009-08-05 01:29:28 +0000849ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
850 SDValue StackPtr, SDValue Arg,
851 DebugLoc dl, SelectionDAG &DAG,
852 const CCValAssign &VA,
853 ISD::ArgFlagsTy Flags) {
Bob Wilsonfd451172009-04-17 19:07:39 +0000854 unsigned LocMemOffset = VA.getLocMemOffset();
855 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
856 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
857 if (Flags.isByVal()) {
858 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
859 }
860 return DAG.getStore(Chain, dl, Arg, PtrOff,
861 PseudoSourceValue::getStack(), LocMemOffset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000862}
863
Dan Gohman9178de12009-08-05 01:29:28 +0000864void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilsone60fee02009-06-22 23:27:02 +0000865 SDValue Chain, SDValue &Arg,
866 RegsToPassVector &RegsToPass,
867 CCValAssign &VA, CCValAssign &NextVA,
868 SDValue &StackPtr,
869 SmallVector<SDValue, 8> &MemOpChains,
870 ISD::ArgFlagsTy Flags) {
Bob Wilsone60fee02009-06-22 23:27:02 +0000871
Jim Grosbache2fda532009-11-09 00:11:35 +0000872 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000873 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilsone60fee02009-06-22 23:27:02 +0000874 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
875
876 if (NextVA.isRegLoc())
877 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
878 else {
879 assert(NextVA.isMemLoc());
880 if (StackPtr.getNode() == 0)
881 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
882
Dan Gohman9178de12009-08-05 01:29:28 +0000883 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
884 dl, DAG, NextVA,
885 Flags));
Bob Wilsone60fee02009-06-22 23:27:02 +0000886 }
887}
888
Dan Gohman9178de12009-08-05 01:29:28 +0000889/// LowerCall - Lowering a call into a callseq_start <-
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000890/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
891/// nodes.
Dan Gohman9178de12009-08-05 01:29:28 +0000892SDValue
893ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel5838baa2009-09-02 08:44:58 +0000894 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman9178de12009-08-05 01:29:28 +0000895 bool isTailCall,
896 const SmallVectorImpl<ISD::OutputArg> &Outs,
897 const SmallVectorImpl<ISD::InputArg> &Ins,
898 DebugLoc dl, SelectionDAG &DAG,
899 SmallVectorImpl<SDValue> &InVals) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000900
Bob Wilsonfd451172009-04-17 19:07:39 +0000901 // Analyze operands of the call, assigning locations to each operand.
902 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman9178de12009-08-05 01:29:28 +0000903 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
904 *DAG.getContext());
905 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov02e15b82009-08-05 19:04:42 +0000906 CCAssignFnForNode(CallConv, /* Return*/ false,
907 isVarArg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000908
Bob Wilsonfd451172009-04-17 19:07:39 +0000909 // Get a count of how many bytes are to be pushed on the stack.
910 unsigned NumBytes = CCInfo.getNextStackOffset();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000911
912 // Adjust the stack pointer for the new arguments...
913 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnerfe5d4022008-10-11 22:08:30 +0000914 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000915
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000916 SDValue StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000917
Bob Wilsone60fee02009-06-22 23:27:02 +0000918 RegsToPassVector RegsToPass;
Bob Wilsonfd451172009-04-17 19:07:39 +0000919 SmallVector<SDValue, 8> MemOpChains;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000920
Bob Wilsonfd451172009-04-17 19:07:39 +0000921 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilson7cd865e2009-04-17 20:35:10 +0000922 // of tail call optimization, arguments are handled later.
Bob Wilsonfd451172009-04-17 19:07:39 +0000923 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
924 i != e;
925 ++i, ++realArgIdx) {
926 CCValAssign &VA = ArgLocs[i];
Dan Gohman9178de12009-08-05 01:29:28 +0000927 SDValue Arg = Outs[realArgIdx].Val;
928 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000929
Bob Wilsonfd451172009-04-17 19:07:39 +0000930 // Promote the value if needed.
931 switch (VA.getLocInfo()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000932 default: llvm_unreachable("Unknown loc info!");
Bob Wilsonfd451172009-04-17 19:07:39 +0000933 case CCValAssign::Full: break;
934 case CCValAssign::SExt:
935 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
936 break;
937 case CCValAssign::ZExt:
938 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
939 break;
940 case CCValAssign::AExt:
941 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
942 break;
943 case CCValAssign::BCvt:
944 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
945 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000946 }
947
Anton Korobeynikov02e15b82009-08-05 19:04:42 +0000948 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsonfd451172009-04-17 19:07:39 +0000949 if (VA.needsCustom()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000950 if (VA.getLocVT() == MVT::v2f64) {
951 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
952 DAG.getConstant(0, MVT::i32));
953 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
954 DAG.getConstant(1, MVT::i32));
Bob Wilsonfd451172009-04-17 19:07:39 +0000955
Dan Gohman9178de12009-08-05 01:29:28 +0000956 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilsone60fee02009-06-22 23:27:02 +0000957 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
958
959 VA = ArgLocs[++i]; // skip ahead to next loc
960 if (VA.isRegLoc()) {
Dan Gohman9178de12009-08-05 01:29:28 +0000961 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilsone60fee02009-06-22 23:27:02 +0000962 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
963 } else {
964 assert(VA.isMemLoc());
965 if (StackPtr.getNode() == 0)
966 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
967
Dan Gohman9178de12009-08-05 01:29:28 +0000968 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
969 dl, DAG, VA, Flags));
Bob Wilsone60fee02009-06-22 23:27:02 +0000970 }
971 } else {
Dan Gohman9178de12009-08-05 01:29:28 +0000972 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilsone60fee02009-06-22 23:27:02 +0000973 StackPtr, MemOpChains, Flags);
Bob Wilsonfd451172009-04-17 19:07:39 +0000974 }
975 } else if (VA.isRegLoc()) {
976 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
977 } else {
978 assert(VA.isMemLoc());
979 if (StackPtr.getNode() == 0)
980 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
981
Dan Gohman9178de12009-08-05 01:29:28 +0000982 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
983 dl, DAG, VA, Flags));
Bob Wilsonfd451172009-04-17 19:07:39 +0000984 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000985 }
986
987 if (!MemOpChains.empty())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000988 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000989 &MemOpChains[0], MemOpChains.size());
990
991 // Build a sequence of copy-to-reg nodes chained together with token chain
992 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman8181bd12008-07-27 21:46:04 +0000993 SDValue InFlag;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000994 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson0eb45b42009-03-20 22:42:55 +0000995 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesenea996922009-02-04 20:06:27 +0000996 RegsToPass[i].second, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000997 InFlag = Chain.getValue(1);
998 }
999
Bill Wendlingfef06052008-09-16 21:48:12 +00001000 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1001 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1002 // node so that legalize doesn't hack it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001003 bool isDirect = false;
1004 bool isARMFunc = false;
1005 bool isLocalARMFunc = false;
Evan Chenge4582d92009-11-06 22:24:13 +00001006 MachineFunction &MF = DAG.getMachineFunction();
1007 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001008 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1009 GlobalValue *GV = G->getGlobal();
1010 isDirect = true;
Chris Lattner494cb892009-07-15 04:12:33 +00001011 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001012 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
1013 getTargetMachine().getRelocationModel() != Reloc::Static;
1014 isARMFunc = !Subtarget->isThumb() || isStub;
1015 // ARM call to a local ARM function is predicable.
1016 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
1017 // tBX takes a register source operand.
David Goodwin4a897932009-07-08 23:10:31 +00001018 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge4582d92009-11-06 22:24:13 +00001019 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chengc2999142009-08-28 23:18:09 +00001020 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach5e0257f2009-09-01 01:57:56 +00001021 ARMPCLabelIndex,
1022 ARMCP::CPValue, 4);
Evan Cheng68c18682009-03-13 07:51:59 +00001023 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001024 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson0eb45b42009-03-20 22:42:55 +00001025 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng326d7242009-10-31 03:39:36 +00001026 DAG.getEntryNode(), CPAddr,
1027 PseudoSourceValue::getConstantPool(), 0);
Evan Chenge4582d92009-11-06 22:24:13 +00001028 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson0eb45b42009-03-20 22:42:55 +00001029 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesenea996922009-02-04 20:06:27 +00001030 getPointerTy(), Callee, PICLabel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001031 } else
1032 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendlingfef06052008-09-16 21:48:12 +00001033 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001034 isDirect = true;
1035 bool isStub = Subtarget->isTargetDarwin() &&
1036 getTargetMachine().getRelocationModel() != Reloc::Static;
1037 isARMFunc = !Subtarget->isThumb() || isStub;
1038 // tBX takes a register source operand.
1039 const char *Sym = S->getSymbol();
David Goodwin4a897932009-07-08 23:10:31 +00001040 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge4582d92009-11-06 22:24:13 +00001041 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Anderson35b47072009-08-13 21:58:54 +00001042 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chengc2999142009-08-28 23:18:09 +00001043 Sym, ARMPCLabelIndex, 4);
Evan Cheng68c18682009-03-13 07:51:59 +00001044 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001045 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesenea996922009-02-04 20:06:27 +00001046 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng326d7242009-10-31 03:39:36 +00001047 DAG.getEntryNode(), CPAddr,
1048 PseudoSourceValue::getConstantPool(), 0);
Evan Chenge4582d92009-11-06 22:24:13 +00001049 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson0eb45b42009-03-20 22:42:55 +00001050 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesenea996922009-02-04 20:06:27 +00001051 getPointerTy(), Callee, PICLabel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001052 } else
Bill Wendlingfef06052008-09-16 21:48:12 +00001053 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001054 }
1055
1056 // FIXME: handle tail calls differently.
1057 unsigned CallOpc;
Evan Cheng68e4b582009-08-01 00:16:10 +00001058 if (Subtarget->isThumb()) {
1059 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001060 CallOpc = ARMISD::CALL_NOLINK;
1061 else
1062 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1063 } else {
1064 CallOpc = (isDirect || Subtarget->hasV5TOps())
1065 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1066 : ARMISD::CALL_NOLINK;
1067 }
David Goodwin4a897932009-07-08 23:10:31 +00001068 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001069 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001070 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001071 InFlag = Chain.getValue(1);
1072 }
1073
Dan Gohman8181bd12008-07-27 21:46:04 +00001074 std::vector<SDValue> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001075 Ops.push_back(Chain);
1076 Ops.push_back(Callee);
1077
1078 // Add argument registers to the end of the list so that they are known live
1079 // into the call.
1080 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1081 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1082 RegsToPass[i].second.getValueType()));
1083
Gabor Greif1c80d112008-08-28 21:40:38 +00001084 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001085 Ops.push_back(InFlag);
Duncan Sands698842f2008-07-02 17:40:58 +00001086 // Returns a chain and a flag for retval copy to use.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001087 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands698842f2008-07-02 17:40:58 +00001088 &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001089 InFlag = Chain.getValue(1);
1090
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001091 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1092 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman9178de12009-08-05 01:29:28 +00001093 if (!Ins.empty())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001094 InFlag = Chain.getValue(1);
1095
Bob Wilsonfd451172009-04-17 19:07:39 +00001096 // Handle result values, copying them out of physregs into vregs that we
1097 // return.
Dan Gohman9178de12009-08-05 01:29:28 +00001098 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1099 dl, DAG, InVals);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001100}
1101
Dan Gohman9178de12009-08-05 01:29:28 +00001102SDValue
1103ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel5838baa2009-09-02 08:44:58 +00001104 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman9178de12009-08-05 01:29:28 +00001105 const SmallVectorImpl<ISD::OutputArg> &Outs,
1106 DebugLoc dl, SelectionDAG &DAG) {
Bob Wilson0eb45b42009-03-20 22:42:55 +00001107
Bob Wilson7cd865e2009-04-17 20:35:10 +00001108 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsonfd451172009-04-17 19:07:39 +00001109 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsonfd451172009-04-17 19:07:39 +00001110
Bob Wilson7cd865e2009-04-17 20:35:10 +00001111 // CCState - Info about the registers and stack slots.
Dan Gohman9178de12009-08-05 01:29:28 +00001112 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1113 *DAG.getContext());
Bob Wilsonfd451172009-04-17 19:07:39 +00001114
Dan Gohman9178de12009-08-05 01:29:28 +00001115 // Analyze outgoing return values.
Anton Korobeynikov02e15b82009-08-05 19:04:42 +00001116 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1117 isVarArg));
Bob Wilsonfd451172009-04-17 19:07:39 +00001118
1119 // If this is the first return lowered for this function, add
1120 // the regs to the liveout set for the function.
1121 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1122 for (unsigned i = 0; i != RVLocs.size(); ++i)
1123 if (RVLocs[i].isRegLoc())
1124 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001125 }
1126
Bob Wilsonfd451172009-04-17 19:07:39 +00001127 SDValue Flag;
1128
1129 // Copy the result values into the output registers.
1130 for (unsigned i = 0, realRVLocIdx = 0;
1131 i != RVLocs.size();
1132 ++i, ++realRVLocIdx) {
1133 CCValAssign &VA = RVLocs[i];
1134 assert(VA.isRegLoc() && "Can only return in registers!");
1135
Dan Gohman9178de12009-08-05 01:29:28 +00001136 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilsonfd451172009-04-17 19:07:39 +00001137
1138 switch (VA.getLocInfo()) {
Edwin Törökbd448e32009-07-14 16:55:14 +00001139 default: llvm_unreachable("Unknown loc info!");
Bob Wilsonfd451172009-04-17 19:07:39 +00001140 case CCValAssign::Full: break;
1141 case CCValAssign::BCvt:
1142 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1143 break;
1144 }
1145
Bob Wilsonfd451172009-04-17 19:07:39 +00001146 if (VA.needsCustom()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001147 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilsone60fee02009-06-22 23:27:02 +00001148 // Extract the first half and return it in two registers.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001149 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1150 DAG.getConstant(0, MVT::i32));
Jim Grosbache2fda532009-11-09 00:11:35 +00001151 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001152 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilsone60fee02009-06-22 23:27:02 +00001153
1154 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1155 Flag = Chain.getValue(1);
1156 VA = RVLocs[++i]; // skip ahead to next loc
1157 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1158 HalfGPRs.getValue(1), Flag);
1159 Flag = Chain.getValue(1);
1160 VA = RVLocs[++i]; // skip ahead to next loc
1161
1162 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001163 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1164 DAG.getConstant(1, MVT::i32));
Bob Wilsone60fee02009-06-22 23:27:02 +00001165 }
1166 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1167 // available.
Jim Grosbache2fda532009-11-09 00:11:35 +00001168 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001169 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilsonfd451172009-04-17 19:07:39 +00001170 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson99194c92009-04-24 17:00:36 +00001171 Flag = Chain.getValue(1);
Bob Wilsonfd451172009-04-17 19:07:39 +00001172 VA = RVLocs[++i]; // skip ahead to next loc
1173 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1174 Flag);
1175 } else
1176 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1177
Bob Wilson7cd865e2009-04-17 20:35:10 +00001178 // Guarantee that all emitted copies are
1179 // stuck together, avoiding something bad.
Bob Wilsonfd451172009-04-17 19:07:39 +00001180 Flag = Chain.getValue(1);
1181 }
1182
1183 SDValue result;
1184 if (Flag.getNode())
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001185 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilsonfd451172009-04-17 19:07:39 +00001186 else // Return Void
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001187 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilsonfd451172009-04-17 19:07:39 +00001188
1189 return result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001190}
1191
Bob Wilson1c9eda22009-11-03 00:02:05 +00001192// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1193// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1194// one of the above mentioned nodes. It has to be wrapped because otherwise
1195// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1196// be used to form addressing mode. These wrapped nodes will be selected
1197// into MOVi.
Dan Gohman8181bd12008-07-27 21:46:04 +00001198static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00001199 EVT PtrVT = Op.getValueType();
Dale Johannesen24dd9a52009-02-07 00:55:49 +00001200 // FIXME there is no actual debug info here
1201 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001202 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00001203 SDValue Res;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001204 if (CP->isMachineConstantPoolEntry())
1205 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1206 CP->getAlignment());
1207 else
1208 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1209 CP->getAlignment());
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001210 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001211}
1212
Bob Wilson8f743382009-10-30 05:45:42 +00001213SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) {
Evan Chenge4582d92009-11-06 22:24:13 +00001214 MachineFunction &MF = DAG.getMachineFunction();
1215 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1216 unsigned ARMPCLabelIndex = 0;
Bob Wilson8f743382009-10-30 05:45:42 +00001217 DebugLoc DL = Op.getDebugLoc();
Bob Wilsond1da9132009-11-02 20:59:23 +00001218 EVT PtrVT = getPointerTy();
Bob Wilson8f743382009-10-30 05:45:42 +00001219 BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilsond1da9132009-11-02 20:59:23 +00001220 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1221 SDValue CPAddr;
1222 if (RelocM == Reloc::Static) {
1223 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1224 } else {
1225 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge4582d92009-11-06 22:24:13 +00001226 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Bob Wilsond1da9132009-11-02 20:59:23 +00001227 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1228 ARMCP::CPBlockAddress,
1229 PCAdj);
1230 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1231 }
1232 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1233 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
1234 PseudoSourceValue::getConstantPool(), 0);
1235 if (RelocM == Reloc::Static)
1236 return Result;
Evan Chenge4582d92009-11-06 22:24:13 +00001237 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilsond1da9132009-11-02 20:59:23 +00001238 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson8f743382009-10-30 05:45:42 +00001239}
1240
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001241// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman8181bd12008-07-27 21:46:04 +00001242SDValue
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001243ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1244 SelectionDAG &DAG) {
Dale Johannesenea996922009-02-04 20:06:27 +00001245 DebugLoc dl = GA->getDebugLoc();
Owen Andersonac9de032009-08-10 22:56:29 +00001246 EVT PtrVT = getPointerTy();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001247 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge4582d92009-11-06 22:24:13 +00001248 MachineFunction &MF = DAG.getMachineFunction();
1249 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1250 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001251 ARMConstantPoolValue *CPV =
Evan Chengc2999142009-08-28 23:18:09 +00001252 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach5e0257f2009-09-01 01:57:56 +00001253 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng68c18682009-03-13 07:51:59 +00001254 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001255 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng326d7242009-10-31 03:39:36 +00001256 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
1257 PseudoSourceValue::getConstantPool(), 0);
Dan Gohman8181bd12008-07-27 21:46:04 +00001258 SDValue Chain = Argument.getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001259
Evan Chenge4582d92009-11-06 22:24:13 +00001260 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesenea996922009-02-04 20:06:27 +00001261 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001262
1263 // call __tls_get_addr.
1264 ArgListTy Args;
1265 ArgListEntry Entry;
1266 Entry.Node = Argument;
Owen Anderson35b47072009-08-13 21:58:54 +00001267 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001268 Args.push_back(Entry);
Dale Johannesenca6237b2009-01-30 23:10:59 +00001269 // FIXME: is there useful debug info available here?
Dan Gohman8181bd12008-07-27 21:46:04 +00001270 std::pair<SDValue, SDValue> CallResult =
Evan Chengbdab42c2009-08-14 19:11:20 +00001271 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1272 false, false, false, false,
Dan Gohman9178de12009-08-05 01:29:28 +00001273 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Dale Johannesenea996922009-02-04 20:06:27 +00001274 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001275 return CallResult.first;
1276}
1277
1278// Lower ISD::GlobalTLSAddress using the "initial exec" or
1279// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00001280SDValue
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001281ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Bob Wilson0eb45b42009-03-20 22:42:55 +00001282 SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001283 GlobalValue *GV = GA->getGlobal();
Dale Johannesenea996922009-02-04 20:06:27 +00001284 DebugLoc dl = GA->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00001285 SDValue Offset;
1286 SDValue Chain = DAG.getEntryNode();
Owen Andersonac9de032009-08-10 22:56:29 +00001287 EVT PtrVT = getPointerTy();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001288 // Get the Thread Pointer
Dale Johannesenea996922009-02-04 20:06:27 +00001289 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001290
Chris Lattner494cb892009-07-15 04:12:33 +00001291 if (GV->isDeclaration()) {
Evan Chenge4582d92009-11-06 22:24:13 +00001292 MachineFunction &MF = DAG.getMachineFunction();
1293 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1294 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1295 // Initial exec model.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001296 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1297 ARMConstantPoolValue *CPV =
Evan Chengc2999142009-08-28 23:18:09 +00001298 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach5e0257f2009-09-01 01:57:56 +00001299 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng68c18682009-03-13 07:51:59 +00001300 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001301 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng326d7242009-10-31 03:39:36 +00001302 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
1303 PseudoSourceValue::getConstantPool(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001304 Chain = Offset.getValue(1);
1305
Evan Chenge4582d92009-11-06 22:24:13 +00001306 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesenea996922009-02-04 20:06:27 +00001307 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001308
Evan Cheng326d7242009-10-31 03:39:36 +00001309 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
1310 PseudoSourceValue::getConstantPool(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001311 } else {
1312 // local exec model
Evan Chengc2999142009-08-28 23:18:09 +00001313 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng68c18682009-03-13 07:51:59 +00001314 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001315 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng326d7242009-10-31 03:39:36 +00001316 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
1317 PseudoSourceValue::getConstantPool(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001318 }
1319
1320 // The address of the thread local variable is the add of the thread
1321 // pointer with the offset of the variable.
Dale Johannesenea996922009-02-04 20:06:27 +00001322 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001323}
1324
Dan Gohman8181bd12008-07-27 21:46:04 +00001325SDValue
1326ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001327 // TODO: implement the "local dynamic" model
1328 assert(Subtarget->isTargetELF() &&
1329 "TLS not implemented for non-ELF targets");
1330 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1331 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1332 // otherwise use the "Local Exec" TLS Model
1333 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1334 return LowerToTLSGeneralDynamicModel(GA, DAG);
1335 else
1336 return LowerToTLSExecModels(GA, DAG);
1337}
1338
Dan Gohman8181bd12008-07-27 21:46:04 +00001339SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Bob Wilson0eb45b42009-03-20 22:42:55 +00001340 SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00001341 EVT PtrVT = getPointerTy();
Dale Johannesenea996922009-02-04 20:06:27 +00001342 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001343 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1344 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1345 if (RelocM == Reloc::PIC_) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +00001346 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001347 ARMConstantPoolValue *CPV =
Evan Chengc2999142009-08-28 23:18:09 +00001348 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng68c18682009-03-13 07:51:59 +00001349 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001350 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson0eb45b42009-03-20 22:42:55 +00001351 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikovf68604a2009-10-07 00:06:35 +00001352 CPAddr,
1353 PseudoSourceValue::getConstantPool(), 0);
Dan Gohman8181bd12008-07-27 21:46:04 +00001354 SDValue Chain = Result.getValue(1);
Dale Johannesen24dd9a52009-02-07 00:55:49 +00001355 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesenea996922009-02-04 20:06:27 +00001356 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001357 if (!UseGOTOFF)
Anton Korobeynikovf68604a2009-10-07 00:06:35 +00001358 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
1359 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001360 return Result;
1361 } else {
Evan Cheng68c18682009-03-13 07:51:59 +00001362 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001363 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikovf68604a2009-10-07 00:06:35 +00001364 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1365 PseudoSourceValue::getConstantPool(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001366 }
1367}
1368
Dan Gohman8181bd12008-07-27 21:46:04 +00001369SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Bob Wilson0eb45b42009-03-20 22:42:55 +00001370 SelectionDAG &DAG) {
Evan Chenge4582d92009-11-06 22:24:13 +00001371 MachineFunction &MF = DAG.getMachineFunction();
1372 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1373 unsigned ARMPCLabelIndex = 0;
Owen Andersonac9de032009-08-10 22:56:29 +00001374 EVT PtrVT = getPointerTy();
Dale Johannesenea996922009-02-04 20:06:27 +00001375 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001376 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1377 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman8181bd12008-07-27 21:46:04 +00001378 SDValue CPAddr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001379 if (RelocM == Reloc::Static)
Evan Cheng68c18682009-03-13 07:51:59 +00001380 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001381 else {
Evan Chenge4582d92009-11-06 22:24:13 +00001382 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chengc2999142009-08-28 23:18:09 +00001383 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1384 ARMConstantPoolValue *CPV =
Jim Grosbach5e0257f2009-09-01 01:57:56 +00001385 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng68c18682009-03-13 07:51:59 +00001386 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001387 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001388 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001389
Evan Cheng326d7242009-10-31 03:39:36 +00001390 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1391 PseudoSourceValue::getConstantPool(), 0);
Dan Gohman8181bd12008-07-27 21:46:04 +00001392 SDValue Chain = Result.getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001393
1394 if (RelocM == Reloc::PIC_) {
Evan Chenge4582d92009-11-06 22:24:13 +00001395 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesenea996922009-02-04 20:06:27 +00001396 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001397 }
Evan Chengc2999142009-08-28 23:18:09 +00001398
Evan Chengba2cf3d2009-09-03 07:04:02 +00001399 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Evan Cheng326d7242009-10-31 03:39:36 +00001400 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
1401 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001402
1403 return Result;
1404}
1405
Dan Gohman8181bd12008-07-27 21:46:04 +00001406SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Bob Wilson0eb45b42009-03-20 22:42:55 +00001407 SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001408 assert(Subtarget->isTargetELF() &&
1409 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge4582d92009-11-06 22:24:13 +00001410 MachineFunction &MF = DAG.getMachineFunction();
1411 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1412 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Andersonac9de032009-08-10 22:56:29 +00001413 EVT PtrVT = getPointerTy();
Dale Johannesenea996922009-02-04 20:06:27 +00001414 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001415 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson35b47072009-08-13 21:58:54 +00001416 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1417 "_GLOBAL_OFFSET_TABLE_",
Evan Chengc2999142009-08-28 23:18:09 +00001418 ARMPCLabelIndex, PCAdj);
Evan Cheng68c18682009-03-13 07:51:59 +00001419 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001420 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikovf68604a2009-10-07 00:06:35 +00001421 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1422 PseudoSourceValue::getConstantPool(), 0);
Evan Chenge4582d92009-11-06 22:24:13 +00001423 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesenea996922009-02-04 20:06:27 +00001424 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001425}
1426
Jim Grosbachc10915b2009-05-12 23:59:14 +00001427SDValue
1428ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001429 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbachc10915b2009-05-12 23:59:14 +00001430 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio5a87a8a2007-11-08 17:20:05 +00001431 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001432 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilsond99b1112009-08-04 00:25:01 +00001433 case Intrinsic::arm_thread_pointer: {
Owen Andersonac9de032009-08-10 22:56:29 +00001434 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilsond99b1112009-08-04 00:25:01 +00001435 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1436 }
Jim Grosbach29feb6a2009-08-11 00:09:57 +00001437 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach29feb6a2009-08-11 00:09:57 +00001438 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge4582d92009-11-06 22:24:13 +00001439 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1440 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Jim Grosbach29feb6a2009-08-11 00:09:57 +00001441 EVT PtrVT = getPointerTy();
1442 DebugLoc dl = Op.getDebugLoc();
1443 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1444 SDValue CPAddr;
1445 unsigned PCAdj = (RelocM != Reloc::PIC_)
1446 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach29feb6a2009-08-11 00:09:57 +00001447 ARMConstantPoolValue *CPV =
Jim Grosbach5e0257f2009-09-01 01:57:56 +00001448 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1449 ARMCP::CPLSDA, PCAdj);
Jim Grosbach29feb6a2009-08-11 00:09:57 +00001450 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001451 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach29feb6a2009-08-11 00:09:57 +00001452 SDValue Result =
Evan Cheng326d7242009-10-31 03:39:36 +00001453 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1454 PseudoSourceValue::getConstantPool(), 0);
Jim Grosbach29feb6a2009-08-11 00:09:57 +00001455 SDValue Chain = Result.getValue(1);
1456
1457 if (RelocM == Reloc::PIC_) {
Evan Chenge4582d92009-11-06 22:24:13 +00001458 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach29feb6a2009-08-11 00:09:57 +00001459 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1460 }
1461 return Result;
1462 }
Jim Grosbach4a9025e2009-05-14 00:46:35 +00001463 case Intrinsic::eh_sjlj_setjmp:
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001464 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1));
Lauro Ramos Venancio5a87a8a2007-11-08 17:20:05 +00001465 }
1466}
1467
Dan Gohman8181bd12008-07-27 21:46:04 +00001468static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Bob Wilson0eb45b42009-03-20 22:42:55 +00001469 unsigned VarArgsFrameIndex) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001470 // vastart just stores the address of the VarArgsFrameIndex slot into the
1471 // memory location argument.
Dale Johannesenea996922009-02-04 20:06:27 +00001472 DebugLoc dl = Op.getDebugLoc();
Owen Andersonac9de032009-08-10 22:56:29 +00001473 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00001474 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman12a9c082008-02-06 22:27:42 +00001475 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesenea996922009-02-04 20:06:27 +00001476 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001477}
1478
Dan Gohman8181bd12008-07-27 21:46:04 +00001479SDValue
Evan Cheng815c23a2009-08-07 00:34:42 +00001480ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
1481 SDNode *Node = Op.getNode();
1482 DebugLoc dl = Node->getDebugLoc();
Owen Andersonac9de032009-08-10 22:56:29 +00001483 EVT VT = Node->getValueType(0);
Evan Cheng815c23a2009-08-07 00:34:42 +00001484 SDValue Chain = Op.getOperand(0);
1485 SDValue Size = Op.getOperand(1);
1486 SDValue Align = Op.getOperand(2);
1487
1488 // Chain the dynamic stack allocation so that it doesn't modify the stack
1489 // pointer when other instructions are using the stack.
1490 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1491
1492 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1493 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1494 if (AlignVal > StackAlign)
1495 // Do this now since selection pass cannot introduce new target
1496 // independent node.
1497 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1498
1499 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1500 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1501 // do even more horrible hack later.
1502 MachineFunction &MF = DAG.getMachineFunction();
1503 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1504 if (AFI->isThumb1OnlyFunction()) {
1505 bool Negate = true;
1506 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1507 if (C) {
1508 uint32_t Val = C->getZExtValue();
1509 if (Val <= 508 && ((Val & 3) == 0))
1510 Negate = false;
1511 }
1512 if (Negate)
1513 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1514 }
1515
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001516 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng815c23a2009-08-07 00:34:42 +00001517 SDValue Ops1[] = { Chain, Size, Align };
1518 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1519 Chain = Res.getValue(1);
1520 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1521 DAG.getIntPtrConstant(0, true), SDValue());
1522 SDValue Ops2[] = { Res, Chain };
1523 return DAG.getMergeValues(Ops2, 2, dl);
1524}
1525
1526SDValue
Bob Wilsone60fee02009-06-22 23:27:02 +00001527ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1528 SDValue &Root, SelectionDAG &DAG,
1529 DebugLoc dl) {
1530 MachineFunction &MF = DAG.getMachineFunction();
1531 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1532
1533 TargetRegisterClass *RC;
David Goodwin4a897932009-07-08 23:10:31 +00001534 if (AFI->isThumb1OnlyFunction())
Bob Wilsone60fee02009-06-22 23:27:02 +00001535 RC = ARM::tGPRRegisterClass;
1536 else
1537 RC = ARM::GPRRegisterClass;
1538
1539 // Transform the arguments stored in physical registers into virtual ones.
1540 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001541 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilsone60fee02009-06-22 23:27:02 +00001542
1543 SDValue ArgValue2;
1544 if (NextVA.isMemLoc()) {
1545 unsigned ArgSize = NextVA.getLocVT().getSizeInBits()/8;
1546 MachineFrameInfo *MFI = MF.getFrameInfo();
David Greene6424ab92009-11-12 20:49:22 +00001547 int FI = MFI->CreateFixedObject(ArgSize, NextVA.getLocMemOffset(),
1548 true, false);
Bob Wilsone60fee02009-06-22 23:27:02 +00001549
1550 // Create load node to retrieve arguments from the stack.
1551 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng326d7242009-10-31 03:39:36 +00001552 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
1553 PseudoSourceValue::getFixedStack(FI), 0);
Bob Wilsone60fee02009-06-22 23:27:02 +00001554 } else {
1555 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001556 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilsone60fee02009-06-22 23:27:02 +00001557 }
1558
Jim Grosbache2fda532009-11-09 00:11:35 +00001559 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilsone60fee02009-06-22 23:27:02 +00001560}
1561
1562SDValue
Dan Gohman9178de12009-08-05 01:29:28 +00001563ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel5838baa2009-09-02 08:44:58 +00001564 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman9178de12009-08-05 01:29:28 +00001565 const SmallVectorImpl<ISD::InputArg>
1566 &Ins,
1567 DebugLoc dl, SelectionDAG &DAG,
1568 SmallVectorImpl<SDValue> &InVals) {
1569
Bob Wilsonfd451172009-04-17 19:07:39 +00001570 MachineFunction &MF = DAG.getMachineFunction();
1571 MachineFrameInfo *MFI = MF.getFrameInfo();
1572
Bob Wilsonfd451172009-04-17 19:07:39 +00001573 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1574
1575 // Assign locations to all of the incoming arguments.
1576 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman9178de12009-08-05 01:29:28 +00001577 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1578 *DAG.getContext());
1579 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov02e15b82009-08-05 19:04:42 +00001580 CCAssignFnForNode(CallConv, /* Return*/ false,
1581 isVarArg));
Bob Wilsonfd451172009-04-17 19:07:39 +00001582
1583 SmallVector<SDValue, 16> ArgValues;
1584
1585 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1586 CCValAssign &VA = ArgLocs[i];
1587
Bob Wilson7cd865e2009-04-17 20:35:10 +00001588 // Arguments stored in registers.
Bob Wilsonfd451172009-04-17 19:07:39 +00001589 if (VA.isRegLoc()) {
Owen Andersonac9de032009-08-10 22:56:29 +00001590 EVT RegVT = VA.getLocVT();
Bob Wilsonfd451172009-04-17 19:07:39 +00001591
Bob Wilsone60fee02009-06-22 23:27:02 +00001592 SDValue ArgValue;
Bob Wilsonfd451172009-04-17 19:07:39 +00001593 if (VA.needsCustom()) {
Bob Wilsone60fee02009-06-22 23:27:02 +00001594 // f64 and vector types are split up into multiple registers or
1595 // combinations of registers and stack slots.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001596 RegVT = MVT::i32;
Bob Wilsonfd451172009-04-17 19:07:39 +00001597
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001598 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilsone60fee02009-06-22 23:27:02 +00001599 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman9178de12009-08-05 01:29:28 +00001600 Chain, DAG, dl);
Bob Wilsone60fee02009-06-22 23:27:02 +00001601 VA = ArgLocs[++i]; // skip ahead to next loc
1602 SDValue ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman9178de12009-08-05 01:29:28 +00001603 Chain, DAG, dl);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001604 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1605 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilsone60fee02009-06-22 23:27:02 +00001606 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001607 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilsone60fee02009-06-22 23:27:02 +00001608 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1609 } else
Dan Gohman9178de12009-08-05 01:29:28 +00001610 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsonfd451172009-04-17 19:07:39 +00001611
Bob Wilsone60fee02009-06-22 23:27:02 +00001612 } else {
1613 TargetRegisterClass *RC;
Anton Korobeynikov02e15b82009-08-05 19:04:42 +00001614
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001615 if (RegVT == MVT::f32)
Bob Wilsone60fee02009-06-22 23:27:02 +00001616 RC = ARM::SPRRegisterClass;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001617 else if (RegVT == MVT::f64)
Bob Wilsone60fee02009-06-22 23:27:02 +00001618 RC = ARM::DPRRegisterClass;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001619 else if (RegVT == MVT::v2f64)
Anton Korobeynikov02e15b82009-08-05 19:04:42 +00001620 RC = ARM::QPRRegisterClass;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001621 else if (RegVT == MVT::i32)
Anton Korobeynikov7bd8d732009-08-05 20:15:19 +00001622 RC = (AFI->isThumb1OnlyFunction() ?
1623 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilsone60fee02009-06-22 23:27:02 +00001624 else
Anton Korobeynikov7bd8d732009-08-05 20:15:19 +00001625 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilsone60fee02009-06-22 23:27:02 +00001626
1627 // Transform the arguments in physical registers into virtual ones.
1628 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman9178de12009-08-05 01:29:28 +00001629 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsonfd451172009-04-17 19:07:39 +00001630 }
1631
1632 // If this is an 8 or 16-bit value, it is really passed promoted
1633 // to 32 bits. Insert an assert[sz]ext to capture this, then
1634 // truncate to the right size.
1635 switch (VA.getLocInfo()) {
Edwin Törökbd448e32009-07-14 16:55:14 +00001636 default: llvm_unreachable("Unknown loc info!");
Bob Wilsonfd451172009-04-17 19:07:39 +00001637 case CCValAssign::Full: break;
1638 case CCValAssign::BCvt:
1639 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1640 break;
1641 case CCValAssign::SExt:
1642 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1643 DAG.getValueType(VA.getValVT()));
1644 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1645 break;
1646 case CCValAssign::ZExt:
1647 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1648 DAG.getValueType(VA.getValVT()));
1649 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1650 break;
1651 }
1652
Dan Gohman9178de12009-08-05 01:29:28 +00001653 InVals.push_back(ArgValue);
Bob Wilsonfd451172009-04-17 19:07:39 +00001654
1655 } else { // VA.isRegLoc()
1656
1657 // sanity check
1658 assert(VA.isMemLoc());
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001659 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsonfd451172009-04-17 19:07:39 +00001660
1661 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
David Greene6424ab92009-11-12 20:49:22 +00001662 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
1663 true, false);
Bob Wilsonfd451172009-04-17 19:07:39 +00001664
Bob Wilson7cd865e2009-04-17 20:35:10 +00001665 // Create load nodes to retrieve arguments from the stack.
Bob Wilsonfd451172009-04-17 19:07:39 +00001666 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng326d7242009-10-31 03:39:36 +00001667 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
1668 PseudoSourceValue::getFixedStack(FI), 0));
Bob Wilsonfd451172009-04-17 19:07:39 +00001669 }
1670 }
1671
1672 // varargs
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001673 if (isVarArg) {
1674 static const unsigned GPRArgRegs[] = {
1675 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1676 };
1677
Bob Wilson7cd865e2009-04-17 20:35:10 +00001678 unsigned NumGPRs = CCInfo.getFirstUnallocated
1679 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilsonfd451172009-04-17 19:07:39 +00001680
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001681 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1682 unsigned VARegSize = (4 - NumGPRs) * 4;
1683 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolaeac0e8f2009-10-30 14:33:14 +00001684 unsigned ArgOffset = CCInfo.getNextStackOffset();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001685 if (VARegSaveSize) {
1686 // If this function is vararg, store any remaining integer argument regs
1687 // to their spots on the stack so that they may be loaded by deferencing
1688 // the result of va_next.
1689 AFI->setVarArgsRegSaveSize(VARegSaveSize);
1690 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
David Greene6424ab92009-11-12 20:49:22 +00001691 VARegSaveSize - VARegSize,
1692 true, false);
Dan Gohman8181bd12008-07-27 21:46:04 +00001693 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001694
Dan Gohman8181bd12008-07-27 21:46:04 +00001695 SmallVector<SDValue, 4> MemOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001696 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilsonfd451172009-04-17 19:07:39 +00001697 TargetRegisterClass *RC;
David Goodwin4a897932009-07-08 23:10:31 +00001698 if (AFI->isThumb1OnlyFunction())
Bob Wilsonfd451172009-04-17 19:07:39 +00001699 RC = ARM::tGPRRegisterClass;
Jim Grosbach0e4e9742009-04-07 20:34:09 +00001700 else
Bob Wilsonfd451172009-04-17 19:07:39 +00001701 RC = ARM::GPRRegisterClass;
1702
Bob Wilsonb6737aa2009-04-20 18:36:57 +00001703 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001704 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Evan Cheng326d7242009-10-31 03:39:36 +00001705 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
1706 PseudoSourceValue::getFixedStack(VarArgsFrameIndex), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001707 MemOps.push_back(Store);
Dale Johannesenea996922009-02-04 20:06:27 +00001708 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001709 DAG.getConstant(4, getPointerTy()));
1710 }
1711 if (!MemOps.empty())
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001712 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman9178de12009-08-05 01:29:28 +00001713 &MemOps[0], MemOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001714 } else
1715 // This will point to the next argument passed via stack.
David Greene6424ab92009-11-12 20:49:22 +00001716 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset, true, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001717 }
1718
Dan Gohman9178de12009-08-05 01:29:28 +00001719 return Chain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001720}
1721
1722/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00001723static bool isFloatingPointZero(SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001724 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesendf8a8312007-08-31 04:03:46 +00001725 return CFP->getValueAPF().isPosZero();
Gabor Greif1c80d112008-08-28 21:40:38 +00001726 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001727 // Maybe this has already been legalized into the constant pool?
1728 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001729 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001730 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1731 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesendf8a8312007-08-31 04:03:46 +00001732 return CFP->getValueAPF().isPosZero();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001733 }
1734 }
1735 return false;
1736}
1737
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001738/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1739/// the given operands.
Evan Cheng3a2ce502009-11-12 07:13:11 +00001740SDValue
1741ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
1742 SDValue &ARMCC, SelectionDAG &DAG, DebugLoc dl) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001743 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001744 unsigned C = RHSC->getZExtValue();
Evan Cheng3a2ce502009-11-12 07:13:11 +00001745 if (!isLegalICmpImmediate(C)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001746 // Constant does not fit, try adjusting it by one?
1747 switch (CC) {
1748 default: break;
1749 case ISD::SETLT:
1750 case ISD::SETGE:
Evan Cheng3a2ce502009-11-12 07:13:11 +00001751 if (isLegalICmpImmediate(C-1)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001752 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001753 RHS = DAG.getConstant(C-1, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001754 }
1755 break;
1756 case ISD::SETULT:
1757 case ISD::SETUGE:
Evan Cheng3a2ce502009-11-12 07:13:11 +00001758 if (C > 0 && isLegalICmpImmediate(C-1)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001759 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001760 RHS = DAG.getConstant(C-1, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001761 }
1762 break;
1763 case ISD::SETLE:
1764 case ISD::SETGT:
Evan Cheng3a2ce502009-11-12 07:13:11 +00001765 if (isLegalICmpImmediate(C+1)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001766 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001767 RHS = DAG.getConstant(C+1, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001768 }
1769 break;
1770 case ISD::SETULE:
1771 case ISD::SETUGT:
Evan Cheng3a2ce502009-11-12 07:13:11 +00001772 if (C < 0xffffffff && isLegalICmpImmediate(C+1)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001773 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001774 RHS = DAG.getConstant(C+1, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001775 }
1776 break;
1777 }
1778 }
1779 }
1780
1781 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
1782 ARMISD::NodeType CompareType;
1783 switch (CondCode) {
1784 default:
1785 CompareType = ARMISD::CMP;
1786 break;
1787 case ARMCC::EQ:
1788 case ARMCC::NE:
David Goodwin8bdcbb32009-06-29 15:33:01 +00001789 // Uses only Z Flag
1790 CompareType = ARMISD::CMPZ;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001791 break;
1792 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001793 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1794 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001795}
1796
1797/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson0eb45b42009-03-20 22:42:55 +00001798static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesen175fdef2009-02-06 21:50:26 +00001799 DebugLoc dl) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001800 SDValue Cmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001801 if (!isFloatingPointZero(RHS))
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001802 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001803 else
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001804 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1805 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001806}
1807
Evan Cheng3a2ce502009-11-12 07:13:11 +00001808SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00001809 EVT VT = Op.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00001810 SDValue LHS = Op.getOperand(0);
1811 SDValue RHS = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001812 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman8181bd12008-07-27 21:46:04 +00001813 SDValue TrueVal = Op.getOperand(2);
1814 SDValue FalseVal = Op.getOperand(3);
Dale Johannesen175fdef2009-02-06 21:50:26 +00001815 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001816
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001817 if (LHS.getValueType() == MVT::i32) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001818 SDValue ARMCC;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001819 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng3a2ce502009-11-12 07:13:11 +00001820 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Dale Johannesen175fdef2009-02-06 21:50:26 +00001821 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001822 }
1823
1824 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilson10371a52009-09-09 23:14:54 +00001825 FPCCToARMCC(CC, CondCode, CondCode2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001826
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001827 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1828 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesen175fdef2009-02-06 21:50:26 +00001829 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1830 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001831 ARMCC, CCR, Cmp);
1832 if (CondCode2 != ARMCC::AL) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001833 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001834 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesen175fdef2009-02-06 21:50:26 +00001835 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson0eb45b42009-03-20 22:42:55 +00001836 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesen175fdef2009-02-06 21:50:26 +00001837 Result, TrueVal, ARMCC2, CCR, Cmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001838 }
1839 return Result;
1840}
1841
Evan Cheng3a2ce502009-11-12 07:13:11 +00001842SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001843 SDValue Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001844 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman8181bd12008-07-27 21:46:04 +00001845 SDValue LHS = Op.getOperand(2);
1846 SDValue RHS = Op.getOperand(3);
1847 SDValue Dest = Op.getOperand(4);
Dale Johannesen175fdef2009-02-06 21:50:26 +00001848 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001849
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001850 if (LHS.getValueType() == MVT::i32) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001851 SDValue ARMCC;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001852 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng3a2ce502009-11-12 07:13:11 +00001853 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001854 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesen175fdef2009-02-06 21:50:26 +00001855 Chain, Dest, ARMCC, CCR,Cmp);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001856 }
1857
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001858 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001859 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilson10371a52009-09-09 23:14:54 +00001860 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson0eb45b42009-03-20 22:42:55 +00001861
Dale Johannesen175fdef2009-02-06 21:50:26 +00001862 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001863 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1864 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1865 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001866 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesen175fdef2009-02-06 21:50:26 +00001867 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001868 if (CondCode2 != ARMCC::AL) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001869 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman8181bd12008-07-27 21:46:04 +00001870 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesen175fdef2009-02-06 21:50:26 +00001871 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001872 }
1873 return Res;
1874}
1875
Dan Gohman8181bd12008-07-27 21:46:04 +00001876SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) {
1877 SDValue Chain = Op.getOperand(0);
1878 SDValue Table = Op.getOperand(1);
1879 SDValue Index = Op.getOperand(2);
Dale Johannesenea996922009-02-04 20:06:27 +00001880 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001881
Owen Andersonac9de032009-08-10 22:56:29 +00001882 EVT PTy = getPointerTy();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001883 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1884 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson53dadfc2009-07-14 18:44:34 +00001885 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman8181bd12008-07-27 21:46:04 +00001886 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001887 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Cheng5c037152009-07-28 20:53:24 +00001888 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
1889 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng6e2ebc92009-07-25 00:33:29 +00001890 if (Subtarget->isThumb2()) {
1891 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
1892 // which does another jump to the destination. This also makes it easier
1893 // to translate it to TBB / TBH later.
1894 // FIXME: This might not work if the function is extremely large.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001895 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng1b2b3e22009-07-29 02:18:14 +00001896 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng6e2ebc92009-07-25 00:33:29 +00001897 }
Evan Cheng6e2ebc92009-07-25 00:33:29 +00001898 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng326d7242009-10-31 03:39:36 +00001899 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
1900 PseudoSourceValue::getJumpTable(), 0);
Evan Cheng6e2ebc92009-07-25 00:33:29 +00001901 Chain = Addr.getValue(1);
Dale Johannesenea996922009-02-04 20:06:27 +00001902 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001903 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng6e2ebc92009-07-25 00:33:29 +00001904 } else {
Evan Cheng326d7242009-10-31 03:39:36 +00001905 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
1906 PseudoSourceValue::getJumpTable(), 0);
Evan Cheng6e2ebc92009-07-25 00:33:29 +00001907 Chain = Addr.getValue(1);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001908 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng6e2ebc92009-07-25 00:33:29 +00001909 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001910}
1911
Dan Gohman8181bd12008-07-27 21:46:04 +00001912static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen175fdef2009-02-06 21:50:26 +00001913 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001914 unsigned Opc =
1915 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001916 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
1917 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001918}
1919
Dan Gohman8181bd12008-07-27 21:46:04 +00001920static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00001921 EVT VT = Op.getValueType();
Dale Johannesen175fdef2009-02-06 21:50:26 +00001922 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001923 unsigned Opc =
1924 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1925
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001926 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
Dale Johannesen175fdef2009-02-06 21:50:26 +00001927 return DAG.getNode(Opc, dl, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001928}
1929
Dan Gohman8181bd12008-07-27 21:46:04 +00001930static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001931 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman8181bd12008-07-27 21:46:04 +00001932 SDValue Tmp0 = Op.getOperand(0);
1933 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesen175fdef2009-02-06 21:50:26 +00001934 DebugLoc dl = Op.getDebugLoc();
Owen Andersonac9de032009-08-10 22:56:29 +00001935 EVT VT = Op.getValueType();
1936 EVT SrcVT = Tmp1.getValueType();
Dale Johannesen175fdef2009-02-06 21:50:26 +00001937 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
1938 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001939 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1940 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesen175fdef2009-02-06 21:50:26 +00001941 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001942}
1943
Jim Grosbachc10915b2009-05-12 23:59:14 +00001944SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
1945 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1946 MFI->setFrameAddressIsTaken(true);
Owen Andersonac9de032009-08-10 22:56:29 +00001947 EVT VT = Op.getValueType();
Jim Grosbachc10915b2009-05-12 23:59:14 +00001948 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
1949 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng9ecae172009-06-18 23:14:30 +00001950 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbachc10915b2009-05-12 23:59:14 +00001951 ? ARM::R7 : ARM::R11;
1952 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
1953 while (Depth--)
1954 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
1955 return FrameAddr;
1956}
1957
Dan Gohman8181bd12008-07-27 21:46:04 +00001958SDValue
Dale Johannesen7f2abf42009-02-03 22:26:09 +00001959ARMTargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman8181bd12008-07-27 21:46:04 +00001960 SDValue Chain,
1961 SDValue Dst, SDValue Src,
1962 SDValue Size, unsigned Align,
Dan Gohmane8b391e2008-04-12 04:36:06 +00001963 bool AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00001964 const Value *DstSV, uint64_t DstSVOff,
1965 const Value *SrcSV, uint64_t SrcSVOff){
Evan Cheng857b89e2007-10-22 22:11:27 +00001966 // Do repeated 4-byte loads and stores. To be improved.
Dan Gohmane8b391e2008-04-12 04:36:06 +00001967 // This requires 4-byte alignment.
1968 if ((Align & 3) != 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00001969 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00001970 // This requires the copy size to be a constant, preferrably
1971 // within a subtarget-specific limit.
1972 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
1973 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00001974 return SDValue();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001975 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00001976 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00001977 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00001978
1979 unsigned BytesLeft = SizeVal & 3;
1980 unsigned NumMemOps = SizeVal >> 2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001981 unsigned EmittedNumMemOps = 0;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001982 EVT VT = MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001983 unsigned VTSize = 4;
Evan Cheng857b89e2007-10-22 22:11:27 +00001984 unsigned i = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001985 const unsigned MAX_LOADS_IN_LDM = 6;
Dan Gohman8181bd12008-07-27 21:46:04 +00001986 SDValue TFOps[MAX_LOADS_IN_LDM];
1987 SDValue Loads[MAX_LOADS_IN_LDM];
Dan Gohman65118f42008-04-28 17:15:20 +00001988 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001989
Evan Cheng857b89e2007-10-22 22:11:27 +00001990 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
1991 // same number of stores. The loads and stores will get combined into
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001992 // ldm/stm later on.
Evan Cheng857b89e2007-10-22 22:11:27 +00001993 while (EmittedNumMemOps < NumMemOps) {
1994 for (i = 0;
1995 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen7f2abf42009-02-03 22:26:09 +00001996 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001997 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
1998 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman65118f42008-04-28 17:15:20 +00001999 SrcSV, SrcSVOff + SrcOff);
Evan Cheng857b89e2007-10-22 22:11:27 +00002000 TFOps[i] = Loads[i].getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002001 SrcOff += VTSize;
2002 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002003 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002004
Evan Cheng857b89e2007-10-22 22:11:27 +00002005 for (i = 0;
2006 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen7f2abf42009-02-03 22:26:09 +00002007 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002008 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
2009 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman65118f42008-04-28 17:15:20 +00002010 DstSV, DstSVOff + DstOff);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002011 DstOff += VTSize;
2012 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002013 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng857b89e2007-10-22 22:11:27 +00002014
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002015 EmittedNumMemOps += i;
2016 }
2017
Bob Wilson0eb45b42009-03-20 22:42:55 +00002018 if (BytesLeft == 0)
Evan Cheng857b89e2007-10-22 22:11:27 +00002019 return Chain;
2020
2021 // Issue loads / stores for the trailing (1 - 3) bytes.
2022 unsigned BytesLeftSave = BytesLeft;
2023 i = 0;
2024 while (BytesLeft) {
2025 if (BytesLeft >= 2) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002026 VT = MVT::i16;
Evan Cheng857b89e2007-10-22 22:11:27 +00002027 VTSize = 2;
2028 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002029 VT = MVT::i8;
Evan Cheng857b89e2007-10-22 22:11:27 +00002030 VTSize = 1;
2031 }
2032
Dale Johannesen7f2abf42009-02-03 22:26:09 +00002033 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002034 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
2035 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman65118f42008-04-28 17:15:20 +00002036 SrcSV, SrcSVOff + SrcOff);
Evan Cheng857b89e2007-10-22 22:11:27 +00002037 TFOps[i] = Loads[i].getValue(1);
2038 ++i;
2039 SrcOff += VTSize;
2040 BytesLeft -= VTSize;
2041 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002042 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng857b89e2007-10-22 22:11:27 +00002043
2044 i = 0;
2045 BytesLeft = BytesLeftSave;
2046 while (BytesLeft) {
2047 if (BytesLeft >= 2) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002048 VT = MVT::i16;
Evan Cheng857b89e2007-10-22 22:11:27 +00002049 VTSize = 2;
2050 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002051 VT = MVT::i8;
Evan Cheng857b89e2007-10-22 22:11:27 +00002052 VTSize = 1;
2053 }
2054
Dale Johannesen7f2abf42009-02-03 22:26:09 +00002055 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002056 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
2057 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman65118f42008-04-28 17:15:20 +00002058 DstSV, DstSVOff + DstOff);
Evan Cheng857b89e2007-10-22 22:11:27 +00002059 ++i;
2060 DstOff += VTSize;
2061 BytesLeft -= VTSize;
2062 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002063 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002064}
2065
Duncan Sands7d9834b2008-12-01 11:39:25 +00002066static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002067 SDValue Op = N->getOperand(0);
Dale Johannesen175fdef2009-02-06 21:50:26 +00002068 DebugLoc dl = N->getDebugLoc();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002069 if (N->getValueType(0) == MVT::f64) {
Jim Grosbache2fda532009-11-09 00:11:35 +00002070 // Turn i64->f64 into VMOVDRR.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002071 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2072 DAG.getConstant(0, MVT::i32));
2073 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2074 DAG.getConstant(1, MVT::i32));
Jim Grosbache2fda532009-11-09 00:11:35 +00002075 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng8b37a952008-11-04 19:57:48 +00002076 }
Bob Wilson0eb45b42009-03-20 22:42:55 +00002077
Jim Grosbache2fda532009-11-09 00:11:35 +00002078 // Turn f64->i64 into VMOVRRD.
2079 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002080 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
Bob Wilson0eb45b42009-03-20 22:42:55 +00002081
Chris Lattnerdfb947d2007-11-24 07:07:01 +00002082 // Merge the pieces into a single i64 value.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002083 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
Chris Lattnerdfb947d2007-11-24 07:07:01 +00002084}
2085
Bob Wilsone60fee02009-06-22 23:27:02 +00002086/// getZeroVector - Returns a vector of specified type with all zero elements.
2087///
Owen Andersonac9de032009-08-10 22:56:29 +00002088static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilsone60fee02009-06-22 23:27:02 +00002089 assert(VT.isVector() && "Expected a vector type");
2090
2091 // Zero vectors are used to represent vector negation and in those cases
2092 // will be implemented with the NEON VNEG instruction. However, VNEG does
2093 // not support i64 elements, so sometimes the zero vectors will need to be
2094 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov14636a52009-09-08 22:51:43 +00002095 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilsone60fee02009-06-22 23:27:02 +00002096 // to their dest type. This ensures they get CSE'd.
2097 SDValue Vec;
Anton Korobeynikov14636a52009-09-08 22:51:43 +00002098 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2099 SmallVector<SDValue, 8> Ops;
2100 MVT TVT;
2101
2102 if (VT.getSizeInBits() == 64) {
2103 Ops.assign(8, Cst); TVT = MVT::v8i8;
2104 } else {
2105 Ops.assign(16, Cst); TVT = MVT::v16i8;
2106 }
2107 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilsone60fee02009-06-22 23:27:02 +00002108
2109 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2110}
2111
2112/// getOnesVector - Returns a vector of specified type with all bits set.
2113///
Owen Andersonac9de032009-08-10 22:56:29 +00002114static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilsone60fee02009-06-22 23:27:02 +00002115 assert(VT.isVector() && "Expected a vector type");
2116
Bob Wilson0d20d172009-10-30 20:13:25 +00002117 // Always build ones vectors as <16 x i8> or <8 x i8> bitcasted to their
Anton Korobeynikov14636a52009-09-08 22:51:43 +00002118 // dest type. This ensures they get CSE'd.
Bob Wilsone60fee02009-06-22 23:27:02 +00002119 SDValue Vec;
Anton Korobeynikov14636a52009-09-08 22:51:43 +00002120 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2121 SmallVector<SDValue, 8> Ops;
2122 MVT TVT;
2123
2124 if (VT.getSizeInBits() == 64) {
2125 Ops.assign(8, Cst); TVT = MVT::v8i8;
2126 } else {
2127 Ops.assign(16, Cst); TVT = MVT::v16i8;
2128 }
2129 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilsone60fee02009-06-22 23:27:02 +00002130
2131 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2132}
2133
Jim Grosbach5480bad2009-10-31 21:00:56 +00002134/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2135/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Evan Cheng3a2ce502009-11-12 07:13:11 +00002136SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op, SelectionDAG &DAG) {
Jim Grosbach5480bad2009-10-31 21:00:56 +00002137 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2138 EVT VT = Op.getValueType();
2139 unsigned VTBits = VT.getSizeInBits();
2140 DebugLoc dl = Op.getDebugLoc();
2141 SDValue ShOpLo = Op.getOperand(0);
2142 SDValue ShOpHi = Op.getOperand(1);
2143 SDValue ShAmt = Op.getOperand(2);
2144 SDValue ARMCC;
Jim Grosbacha3bddfb2009-10-31 21:42:19 +00002145 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach5480bad2009-10-31 21:00:56 +00002146
Jim Grosbacha3bddfb2009-10-31 21:42:19 +00002147 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2148
Jim Grosbach5480bad2009-10-31 21:00:56 +00002149 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2150 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2151 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2152 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2153 DAG.getConstant(VTBits, MVT::i32));
2154 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2155 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbacha3bddfb2009-10-31 21:42:19 +00002156 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach5480bad2009-10-31 21:00:56 +00002157
2158 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2159 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng3a2ce502009-11-12 07:13:11 +00002160 ARMCC, DAG, dl);
Jim Grosbacha3bddfb2009-10-31 21:42:19 +00002161 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Jim Grosbach5480bad2009-10-31 21:00:56 +00002162 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC,
2163 CCR, Cmp);
2164
2165 SDValue Ops[2] = { Lo, Hi };
2166 return DAG.getMergeValues(Ops, 2, dl);
2167}
2168
Jim Grosbach998eacc2009-10-31 19:38:01 +00002169/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2170/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Evan Cheng3a2ce502009-11-12 07:13:11 +00002171SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op, SelectionDAG &DAG) {
Jim Grosbach998eacc2009-10-31 19:38:01 +00002172 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2173 EVT VT = Op.getValueType();
2174 unsigned VTBits = VT.getSizeInBits();
2175 DebugLoc dl = Op.getDebugLoc();
2176 SDValue ShOpLo = Op.getOperand(0);
2177 SDValue ShOpHi = Op.getOperand(1);
2178 SDValue ShAmt = Op.getOperand(2);
2179 SDValue ARMCC;
2180
2181 assert(Op.getOpcode() == ISD::SHL_PARTS);
2182 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2183 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2184 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2185 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2186 DAG.getConstant(VTBits, MVT::i32));
2187 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2188 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2189
2190 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2191 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2192 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng3a2ce502009-11-12 07:13:11 +00002193 ARMCC, DAG, dl);
Jim Grosbach998eacc2009-10-31 19:38:01 +00002194 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
2195 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMCC,
2196 CCR, Cmp);
2197
2198 SDValue Ops[2] = { Lo, Hi };
2199 return DAG.getMergeValues(Ops, 2, dl);
2200}
2201
Bob Wilsone60fee02009-06-22 23:27:02 +00002202static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2203 const ARMSubtarget *ST) {
Owen Andersonac9de032009-08-10 22:56:29 +00002204 EVT VT = N->getValueType(0);
Bob Wilsone60fee02009-06-22 23:27:02 +00002205 DebugLoc dl = N->getDebugLoc();
2206
2207 // Lower vector shifts on NEON to use VSHL.
2208 if (VT.isVector()) {
2209 assert(ST->hasNEON() && "unexpected vector shift");
2210
2211 // Left shifts translate directly to the vshiftu intrinsic.
2212 if (N->getOpcode() == ISD::SHL)
2213 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002214 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilsone60fee02009-06-22 23:27:02 +00002215 N->getOperand(0), N->getOperand(1));
2216
2217 assert((N->getOpcode() == ISD::SRA ||
2218 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2219
2220 // NEON uses the same intrinsics for both left and right shifts. For
2221 // right shifts, the shift amounts are negative, so negate the vector of
2222 // shift amounts.
Owen Andersonac9de032009-08-10 22:56:29 +00002223 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilsone60fee02009-06-22 23:27:02 +00002224 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2225 getZeroVector(ShiftVT, DAG, dl),
2226 N->getOperand(1));
2227 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2228 Intrinsic::arm_neon_vshifts :
2229 Intrinsic::arm_neon_vshiftu);
2230 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002231 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilsone60fee02009-06-22 23:27:02 +00002232 N->getOperand(0), NegatedCount);
2233 }
2234
Eli Friedmanf44f5f12009-08-22 03:13:10 +00002235 // We can get here for a node like i32 = ISD::SHL i32, i64
2236 if (VT != MVT::i64)
2237 return SDValue();
2238
2239 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerdfb947d2007-11-24 07:07:01 +00002240 "Unknown shift to lower!");
Duncan Sands7d9834b2008-12-01 11:39:25 +00002241
Chris Lattnerdfb947d2007-11-24 07:07:01 +00002242 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2243 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002244 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands7d9834b2008-12-01 11:39:25 +00002245 return SDValue();
Bob Wilson0eb45b42009-03-20 22:42:55 +00002246
Chris Lattnerdfb947d2007-11-24 07:07:01 +00002247 // If we are in thumb mode, we don't have RRX.
David Goodwin4a897932009-07-08 23:10:31 +00002248 if (ST->isThumb1Only()) return SDValue();
Bob Wilson0eb45b42009-03-20 22:42:55 +00002249
Chris Lattnerdfb947d2007-11-24 07:07:01 +00002250 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002251 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2252 DAG.getConstant(0, MVT::i32));
2253 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2254 DAG.getConstant(1, MVT::i32));
Bob Wilson0eb45b42009-03-20 22:42:55 +00002255
Chris Lattnerdfb947d2007-11-24 07:07:01 +00002256 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2257 // captures the result into a carry flag.
2258 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002259 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson0eb45b42009-03-20 22:42:55 +00002260
Chris Lattnerdfb947d2007-11-24 07:07:01 +00002261 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002262 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson0eb45b42009-03-20 22:42:55 +00002263
Chris Lattnerdfb947d2007-11-24 07:07:01 +00002264 // Merge the pieces into a single i64 value.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002265 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00002266}
2267
Bob Wilsone60fee02009-06-22 23:27:02 +00002268static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2269 SDValue TmpOp0, TmpOp1;
2270 bool Invert = false;
2271 bool Swap = false;
2272 unsigned Opc = 0;
2273
2274 SDValue Op0 = Op.getOperand(0);
2275 SDValue Op1 = Op.getOperand(1);
2276 SDValue CC = Op.getOperand(2);
Owen Andersonac9de032009-08-10 22:56:29 +00002277 EVT VT = Op.getValueType();
Bob Wilsone60fee02009-06-22 23:27:02 +00002278 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2279 DebugLoc dl = Op.getDebugLoc();
2280
2281 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2282 switch (SetCCOpcode) {
Edwin Törökbd448e32009-07-14 16:55:14 +00002283 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilsone60fee02009-06-22 23:27:02 +00002284 case ISD::SETUNE:
2285 case ISD::SETNE: Invert = true; // Fallthrough
2286 case ISD::SETOEQ:
2287 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2288 case ISD::SETOLT:
2289 case ISD::SETLT: Swap = true; // Fallthrough
2290 case ISD::SETOGT:
2291 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2292 case ISD::SETOLE:
2293 case ISD::SETLE: Swap = true; // Fallthrough
2294 case ISD::SETOGE:
2295 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2296 case ISD::SETUGE: Swap = true; // Fallthrough
2297 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2298 case ISD::SETUGT: Swap = true; // Fallthrough
2299 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2300 case ISD::SETUEQ: Invert = true; // Fallthrough
2301 case ISD::SETONE:
2302 // Expand this to (OLT | OGT).
2303 TmpOp0 = Op0;
2304 TmpOp1 = Op1;
2305 Opc = ISD::OR;
2306 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2307 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2308 break;
2309 case ISD::SETUO: Invert = true; // Fallthrough
2310 case ISD::SETO:
2311 // Expand this to (OLT | OGE).
2312 TmpOp0 = Op0;
2313 TmpOp1 = Op1;
2314 Opc = ISD::OR;
2315 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2316 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2317 break;
2318 }
2319 } else {
2320 // Integer comparisons.
2321 switch (SetCCOpcode) {
Edwin Törökbd448e32009-07-14 16:55:14 +00002322 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilsone60fee02009-06-22 23:27:02 +00002323 case ISD::SETNE: Invert = true;
2324 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2325 case ISD::SETLT: Swap = true;
2326 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2327 case ISD::SETLE: Swap = true;
2328 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2329 case ISD::SETULT: Swap = true;
2330 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2331 case ISD::SETULE: Swap = true;
2332 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2333 }
2334
Nick Lewycky8f5253b2009-07-08 03:04:38 +00002335 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilsone60fee02009-06-22 23:27:02 +00002336 if (Opc == ARMISD::VCEQ) {
2337
2338 SDValue AndOp;
2339 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2340 AndOp = Op0;
2341 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2342 AndOp = Op1;
2343
2344 // Ignore bitconvert.
2345 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2346 AndOp = AndOp.getOperand(0);
2347
2348 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2349 Opc = ARMISD::VTST;
2350 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2351 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2352 Invert = !Invert;
2353 }
2354 }
2355 }
2356
2357 if (Swap)
2358 std::swap(Op0, Op1);
2359
2360 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2361
2362 if (Invert)
2363 Result = DAG.getNOT(dl, Result, VT);
2364
2365 return Result;
2366}
2367
2368/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2369/// VMOV instruction, and if so, return the constant being splatted.
2370static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2371 unsigned SplatBitSize, SelectionDAG &DAG) {
2372 switch (SplatBitSize) {
2373 case 8:
2374 // Any 1-byte value is OK.
2375 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002376 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilsone60fee02009-06-22 23:27:02 +00002377
2378 case 16:
2379 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2380 if ((SplatBits & ~0xff) == 0 ||
2381 (SplatBits & ~0xff00) == 0)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002382 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilsone60fee02009-06-22 23:27:02 +00002383 break;
2384
2385 case 32:
2386 // NEON's 32-bit VMOV supports splat values where:
2387 // * only one byte is nonzero, or
2388 // * the least significant byte is 0xff and the second byte is nonzero, or
2389 // * the least significant 2 bytes are 0xff and the third is nonzero.
2390 if ((SplatBits & ~0xff) == 0 ||
2391 (SplatBits & ~0xff00) == 0 ||
2392 (SplatBits & ~0xff0000) == 0 ||
2393 (SplatBits & ~0xff000000) == 0)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002394 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilsone60fee02009-06-22 23:27:02 +00002395
2396 if ((SplatBits & ~0xffff) == 0 &&
2397 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002398 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilsone60fee02009-06-22 23:27:02 +00002399
2400 if ((SplatBits & ~0xffffff) == 0 &&
2401 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002402 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilsone60fee02009-06-22 23:27:02 +00002403
2404 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2405 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2406 // VMOV.I32. A (very) minor optimization would be to replicate the value
2407 // and fall through here to test for a valid 64-bit splat. But, then the
2408 // caller would also need to check and handle the change in size.
2409 break;
2410
2411 case 64: {
2412 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2413 uint64_t BitMask = 0xff;
2414 uint64_t Val = 0;
2415 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2416 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2417 Val |= BitMask;
2418 else if ((SplatBits & BitMask) != 0)
2419 return SDValue();
2420 BitMask <<= 8;
2421 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002422 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilsone60fee02009-06-22 23:27:02 +00002423 }
2424
2425 default:
Edwin Törökbd448e32009-07-14 16:55:14 +00002426 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilsone60fee02009-06-22 23:27:02 +00002427 break;
2428 }
2429
2430 return SDValue();
2431}
2432
2433/// getVMOVImm - If this is a build_vector of constants which can be
2434/// formed by using a VMOV instruction of the specified element size,
2435/// return the constant being splatted. The ByteSize field indicates the
2436/// number of bytes of each element [1248].
2437SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2438 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2439 APInt SplatBits, SplatUndef;
2440 unsigned SplatBitSize;
2441 bool HasAnyUndefs;
2442 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2443 HasAnyUndefs, ByteSize * 8))
2444 return SDValue();
2445
2446 if (SplatBitSize > ByteSize * 8)
2447 return SDValue();
2448
2449 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2450 SplatBitSize, DAG);
2451}
2452
Anton Korobeynikov2a0296f2009-08-21 12:40:07 +00002453static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2454 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson3ac39132009-08-19 17:03:43 +00002455 unsigned NumElts = VT.getVectorNumElements();
2456 ReverseVEXT = false;
Anton Korobeynikov2a0296f2009-08-21 12:40:07 +00002457 Imm = M[0];
Bob Wilson3ac39132009-08-19 17:03:43 +00002458
2459 // If this is a VEXT shuffle, the immediate value is the index of the first
2460 // element. The other shuffle indices must be the successive elements after
2461 // the first one.
2462 unsigned ExpectedElt = Imm;
2463 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson3ac39132009-08-19 17:03:43 +00002464 // Increment the expected index. If it wraps around, it may still be
2465 // a VEXT but the source vectors must be swapped.
2466 ExpectedElt += 1;
2467 if (ExpectedElt == NumElts * 2) {
2468 ExpectedElt = 0;
2469 ReverseVEXT = true;
2470 }
2471
Anton Korobeynikov2a0296f2009-08-21 12:40:07 +00002472 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson3ac39132009-08-19 17:03:43 +00002473 return false;
2474 }
2475
2476 // Adjust the index value if the source operands will be swapped.
2477 if (ReverseVEXT)
2478 Imm -= NumElts;
2479
Bob Wilson3ac39132009-08-19 17:03:43 +00002480 return true;
2481}
2482
Bob Wilsonc1cd72e2009-07-26 00:39:34 +00002483/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2484/// instruction with the specified blocksize. (The order of the elements
2485/// within each block of the vector is reversed.)
Anton Korobeynikov2a0296f2009-08-21 12:40:07 +00002486static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2487 unsigned BlockSize) {
Bob Wilsonc1cd72e2009-07-26 00:39:34 +00002488 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2489 "Only possible block sizes for VREV are: 16, 32, 64");
2490
Bob Wilsonc1cd72e2009-07-26 00:39:34 +00002491 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson2b1ec382009-10-21 21:36:27 +00002492 if (EltSz == 64)
2493 return false;
2494
2495 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikov2a0296f2009-08-21 12:40:07 +00002496 unsigned BlockElts = M[0] + 1;
Bob Wilsonc1cd72e2009-07-26 00:39:34 +00002497
2498 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2499 return false;
2500
2501 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikov2a0296f2009-08-21 12:40:07 +00002502 if ((unsigned) M[i] !=
Bob Wilsonc1cd72e2009-07-26 00:39:34 +00002503 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2504 return false;
2505 }
2506
2507 return true;
2508}
2509
Bob Wilson84462762009-08-21 20:54:19 +00002510static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2511 unsigned &WhichResult) {
Bob Wilson2b1ec382009-10-21 21:36:27 +00002512 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2513 if (EltSz == 64)
2514 return false;
2515
Bob Wilson84462762009-08-21 20:54:19 +00002516 unsigned NumElts = VT.getVectorNumElements();
2517 WhichResult = (M[0] == 0 ? 0 : 1);
2518 for (unsigned i = 0; i < NumElts; i += 2) {
2519 if ((unsigned) M[i] != i + WhichResult ||
2520 (unsigned) M[i+1] != i + NumElts + WhichResult)
2521 return false;
2522 }
2523 return true;
2524}
2525
2526static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2527 unsigned &WhichResult) {
Bob Wilson2b1ec382009-10-21 21:36:27 +00002528 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2529 if (EltSz == 64)
2530 return false;
2531
Bob Wilson84462762009-08-21 20:54:19 +00002532 unsigned NumElts = VT.getVectorNumElements();
2533 WhichResult = (M[0] == 0 ? 0 : 1);
2534 for (unsigned i = 0; i != NumElts; ++i) {
2535 if ((unsigned) M[i] != 2 * i + WhichResult)
2536 return false;
2537 }
2538
2539 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson2b1ec382009-10-21 21:36:27 +00002540 if (VT.is64BitVector() && EltSz == 32)
Bob Wilson84462762009-08-21 20:54:19 +00002541 return false;
2542
2543 return true;
2544}
2545
2546static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2547 unsigned &WhichResult) {
Bob Wilson2b1ec382009-10-21 21:36:27 +00002548 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2549 if (EltSz == 64)
2550 return false;
2551
Bob Wilson84462762009-08-21 20:54:19 +00002552 unsigned NumElts = VT.getVectorNumElements();
2553 WhichResult = (M[0] == 0 ? 0 : 1);
2554 unsigned Idx = WhichResult * NumElts / 2;
2555 for (unsigned i = 0; i != NumElts; i += 2) {
2556 if ((unsigned) M[i] != Idx ||
2557 (unsigned) M[i+1] != Idx + NumElts)
2558 return false;
2559 Idx += 1;
2560 }
2561
2562 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson2b1ec382009-10-21 21:36:27 +00002563 if (VT.is64BitVector() && EltSz == 32)
Bob Wilson84462762009-08-21 20:54:19 +00002564 return false;
2565
2566 return true;
2567}
2568
Owen Andersonac9de032009-08-10 22:56:29 +00002569static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilsone60fee02009-06-22 23:27:02 +00002570 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsonbeacf652009-08-13 01:57:47 +00002571 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilsone60fee02009-06-22 23:27:02 +00002572 if (ConstVal->isNullValue())
2573 return getZeroVector(VT, DAG, dl);
2574 if (ConstVal->isAllOnesValue())
2575 return getOnesVector(VT, DAG, dl);
2576
Owen Andersonac9de032009-08-10 22:56:29 +00002577 EVT CanonicalVT;
Bob Wilsone60fee02009-06-22 23:27:02 +00002578 if (VT.is64BitVector()) {
2579 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002580 case 8: CanonicalVT = MVT::v8i8; break;
2581 case 16: CanonicalVT = MVT::v4i16; break;
2582 case 32: CanonicalVT = MVT::v2i32; break;
2583 case 64: CanonicalVT = MVT::v1i64; break;
Edwin Törökbd448e32009-07-14 16:55:14 +00002584 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilsone60fee02009-06-22 23:27:02 +00002585 }
2586 } else {
2587 assert(VT.is128BitVector() && "unknown splat vector size");
2588 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002589 case 8: CanonicalVT = MVT::v16i8; break;
2590 case 16: CanonicalVT = MVT::v8i16; break;
2591 case 32: CanonicalVT = MVT::v4i32; break;
2592 case 64: CanonicalVT = MVT::v2i64; break;
Edwin Törökbd448e32009-07-14 16:55:14 +00002593 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilsone60fee02009-06-22 23:27:02 +00002594 }
2595 }
2596
2597 // Build a canonical splat for this value.
2598 SmallVector<SDValue, 8> Ops;
2599 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2600 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2601 Ops.size());
2602 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2603}
2604
2605// If this is a case we can't handle, return null and let the default
2606// expansion code take care of it.
2607static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonbeacf652009-08-13 01:57:47 +00002608 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilsone60fee02009-06-22 23:27:02 +00002609 DebugLoc dl = Op.getDebugLoc();
Owen Andersonac9de032009-08-10 22:56:29 +00002610 EVT VT = Op.getValueType();
Bob Wilsone60fee02009-06-22 23:27:02 +00002611
2612 APInt SplatBits, SplatUndef;
2613 unsigned SplatBitSize;
2614 bool HasAnyUndefs;
2615 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovcb7c98d2009-08-29 00:08:18 +00002616 if (SplatBitSize <= 64) {
2617 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2618 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2619 if (Val.getNode())
2620 return BuildSplat(Val, VT, DAG, dl);
2621 }
Bob Wilsond9a908a2009-07-30 00:31:25 +00002622 }
2623
2624 // If there are only 2 elements in a 128-bit vector, insert them into an
2625 // undef vector. This handles the common case for 128-bit vector argument
2626 // passing, where the insertions should be translated to subreg accesses
2627 // with no real instructions.
2628 if (VT.is128BitVector() && Op.getNumOperands() == 2) {
2629 SDValue Val = DAG.getUNDEF(VT);
2630 SDValue Op0 = Op.getOperand(0);
2631 SDValue Op1 = Op.getOperand(1);
2632 if (Op0.getOpcode() != ISD::UNDEF)
2633 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op0,
2634 DAG.getIntPtrConstant(0));
2635 if (Op1.getOpcode() != ISD::UNDEF)
2636 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op1,
2637 DAG.getIntPtrConstant(1));
2638 return Val;
Bob Wilsone60fee02009-06-22 23:27:02 +00002639 }
2640
2641 return SDValue();
2642}
2643
Anton Korobeynikov2a0296f2009-08-21 12:40:07 +00002644/// isShuffleMaskLegal - Targets can use this to indicate that they only
2645/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2646/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2647/// are assumed to be legal.
2648bool
2649ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2650 EVT VT) const {
Anton Korobeynikov0a029782009-08-21 12:41:24 +00002651 if (VT.getVectorNumElements() == 4 &&
2652 (VT.is128BitVector() || VT.is64BitVector())) {
2653 unsigned PFIndexes[4];
2654 for (unsigned i = 0; i != 4; ++i) {
2655 if (M[i] < 0)
2656 PFIndexes[i] = 8;
2657 else
2658 PFIndexes[i] = M[i];
2659 }
2660
2661 // Compute the index in the perfect shuffle table.
2662 unsigned PFTableIndex =
2663 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2664 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2665 unsigned Cost = (PFEntry >> 30);
2666
2667 if (Cost <= 4)
2668 return true;
2669 }
2670
Anton Korobeynikov2a0296f2009-08-21 12:40:07 +00002671 bool ReverseVEXT;
Bob Wilson84462762009-08-21 20:54:19 +00002672 unsigned Imm, WhichResult;
Anton Korobeynikov2a0296f2009-08-21 12:40:07 +00002673
2674 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2675 isVREVMask(M, VT, 64) ||
2676 isVREVMask(M, VT, 32) ||
2677 isVREVMask(M, VT, 16) ||
Bob Wilson84462762009-08-21 20:54:19 +00002678 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2679 isVTRNMask(M, VT, WhichResult) ||
2680 isVUZPMask(M, VT, WhichResult) ||
2681 isVZIPMask(M, VT, WhichResult));
Anton Korobeynikov2a0296f2009-08-21 12:40:07 +00002682}
2683
Anton Korobeynikov0a029782009-08-21 12:41:24 +00002684/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2685/// the specified operations to build the shuffle.
2686static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2687 SDValue RHS, SelectionDAG &DAG,
2688 DebugLoc dl) {
2689 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2690 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2691 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2692
2693 enum {
2694 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2695 OP_VREV,
2696 OP_VDUP0,
2697 OP_VDUP1,
2698 OP_VDUP2,
2699 OP_VDUP3,
2700 OP_VEXT1,
2701 OP_VEXT2,
2702 OP_VEXT3,
2703 OP_VUZPL, // VUZP, left result
2704 OP_VUZPR, // VUZP, right result
2705 OP_VZIPL, // VZIP, left result
2706 OP_VZIPR, // VZIP, right result
2707 OP_VTRNL, // VTRN, left result
2708 OP_VTRNR // VTRN, right result
2709 };
2710
2711 if (OpNum == OP_COPY) {
2712 if (LHSID == (1*9+2)*9+3) return LHS;
2713 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2714 return RHS;
2715 }
2716
2717 SDValue OpLHS, OpRHS;
2718 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2719 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2720 EVT VT = OpLHS.getValueType();
2721
2722 switch (OpNum) {
2723 default: llvm_unreachable("Unknown shuffle opcode!");
2724 case OP_VREV:
2725 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2726 case OP_VDUP0:
2727 case OP_VDUP1:
2728 case OP_VDUP2:
2729 case OP_VDUP3:
2730 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov394bbb82009-08-21 12:41:42 +00002731 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov0a029782009-08-21 12:41:24 +00002732 case OP_VEXT1:
2733 case OP_VEXT2:
2734 case OP_VEXT3:
2735 return DAG.getNode(ARMISD::VEXT, dl, VT,
2736 OpLHS, OpRHS,
2737 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2738 case OP_VUZPL:
2739 case OP_VUZPR:
Anton Korobeynikov394bbb82009-08-21 12:41:42 +00002740 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov0a029782009-08-21 12:41:24 +00002741 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
2742 case OP_VZIPL:
2743 case OP_VZIPR:
Anton Korobeynikov394bbb82009-08-21 12:41:42 +00002744 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov0a029782009-08-21 12:41:24 +00002745 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
2746 case OP_VTRNL:
2747 case OP_VTRNR:
Anton Korobeynikov394bbb82009-08-21 12:41:42 +00002748 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2749 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov0a029782009-08-21 12:41:24 +00002750 }
2751}
2752
Bob Wilsone60fee02009-06-22 23:27:02 +00002753static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov0a029782009-08-21 12:41:24 +00002754 SDValue V1 = Op.getOperand(0);
2755 SDValue V2 = Op.getOperand(1);
Bob Wilson08479272009-08-12 22:31:50 +00002756 DebugLoc dl = Op.getDebugLoc();
2757 EVT VT = Op.getValueType();
Anton Korobeynikov0a029782009-08-21 12:41:24 +00002758 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikov2a0296f2009-08-21 12:40:07 +00002759 SmallVector<int, 8> ShuffleMask;
Bob Wilson08479272009-08-12 22:31:50 +00002760
Bob Wilson24e23172009-08-13 02:13:04 +00002761 // Convert shuffles that are directly supported on NEON to target-specific
2762 // DAG nodes, instead of keeping them as shuffles and matching them again
2763 // during code selection. This is more efficient and avoids the possibility
2764 // of inconsistencies between legalization and selection.
Bob Wilson628c99a2009-08-13 06:01:30 +00002765 // FIXME: floating-point vectors should be canonicalized to integer vectors
2766 // of the same time so that they get CSEd properly.
Anton Korobeynikov2a0296f2009-08-21 12:40:07 +00002767 SVN->getMask(ShuffleMask);
2768
2769 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson206f6c42009-08-14 05:08:32 +00002770 int Lane = SVN->getSplatIndex();
Anton Korobeynikovf817d7b2009-11-02 00:12:06 +00002771 // If this is undef splat, generate it via "just" vdup, if possible.
2772 if (Lane == -1) Lane = 0;
2773
Anton Korobeynikov0a029782009-08-21 12:41:24 +00002774 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
2775 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonf4f1a272009-08-14 05:13:08 +00002776 }
Anton Korobeynikov0a029782009-08-21 12:41:24 +00002777 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilson3ac39132009-08-19 17:03:43 +00002778 DAG.getConstant(Lane, MVT::i32));
Bob Wilson206f6c42009-08-14 05:08:32 +00002779 }
Bob Wilson3ac39132009-08-19 17:03:43 +00002780
2781 bool ReverseVEXT;
2782 unsigned Imm;
Anton Korobeynikov2a0296f2009-08-21 12:40:07 +00002783 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilson3ac39132009-08-19 17:03:43 +00002784 if (ReverseVEXT)
Bob Wilson84462762009-08-21 20:54:19 +00002785 std::swap(V1, V2);
2786 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilson3ac39132009-08-19 17:03:43 +00002787 DAG.getConstant(Imm, MVT::i32));
2788 }
2789
Anton Korobeynikov2a0296f2009-08-21 12:40:07 +00002790 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov0a029782009-08-21 12:41:24 +00002791 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikov2a0296f2009-08-21 12:40:07 +00002792 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov0a029782009-08-21 12:41:24 +00002793 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikov2a0296f2009-08-21 12:40:07 +00002794 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov0a029782009-08-21 12:41:24 +00002795 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
2796
Bob Wilson84462762009-08-21 20:54:19 +00002797 // Check for Neon shuffles that modify both input vectors in place.
2798 // If both results are used, i.e., if there are two shuffles with the same
2799 // source operands and with masks corresponding to both results of one of
2800 // these operations, DAG memoization will ensure that a single node is
2801 // used for both shuffles.
2802 unsigned WhichResult;
2803 if (isVTRNMask(ShuffleMask, VT, WhichResult))
2804 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2805 V1, V2).getValue(WhichResult);
2806 if (isVUZPMask(ShuffleMask, VT, WhichResult))
2807 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2808 V1, V2).getValue(WhichResult);
2809 if (isVZIPMask(ShuffleMask, VT, WhichResult))
2810 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2811 V1, V2).getValue(WhichResult);
2812
2813 // If the shuffle is not directly supported and it has 4 elements, use
2814 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Anton Korobeynikov0a029782009-08-21 12:41:24 +00002815 if (VT.getVectorNumElements() == 4 &&
2816 (VT.is128BitVector() || VT.is64BitVector())) {
2817 unsigned PFIndexes[4];
2818 for (unsigned i = 0; i != 4; ++i) {
2819 if (ShuffleMask[i] < 0)
2820 PFIndexes[i] = 8;
2821 else
2822 PFIndexes[i] = ShuffleMask[i];
2823 }
2824
2825 // Compute the index in the perfect shuffle table.
2826 unsigned PFTableIndex =
2827 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2828
2829 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2830 unsigned Cost = (PFEntry >> 30);
2831
2832 if (Cost <= 4)
2833 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
2834 }
Bob Wilson08479272009-08-12 22:31:50 +00002835
Bob Wilson4c292c32009-08-14 05:16:33 +00002836 return SDValue();
Bob Wilsone60fee02009-06-22 23:27:02 +00002837}
2838
Bob Wilsone60fee02009-06-22 23:27:02 +00002839static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00002840 EVT VT = Op.getValueType();
Bob Wilsone60fee02009-06-22 23:27:02 +00002841 DebugLoc dl = Op.getDebugLoc();
Bob Wilsone60fee02009-06-22 23:27:02 +00002842 SDValue Vec = Op.getOperand(0);
2843 SDValue Lane = Op.getOperand(1);
Bob Wilson8ab1f272009-10-15 23:12:05 +00002844 assert(VT == MVT::i32 &&
2845 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
2846 "unexpected type for custom-lowering vector extract");
2847 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilsone60fee02009-06-22 23:27:02 +00002848}
2849
Bob Wilson96ba6052009-08-03 20:36:38 +00002850static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
2851 // The only time a CONCAT_VECTORS operation can have legal types is when
2852 // two 64-bit vectors are concatenated to a 128-bit vector.
2853 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
2854 "unexpected CONCAT_VECTORS");
2855 DebugLoc dl = Op.getDebugLoc();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002856 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilson96ba6052009-08-03 20:36:38 +00002857 SDValue Op0 = Op.getOperand(0);
2858 SDValue Op1 = Op.getOperand(1);
2859 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002860 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2861 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilson96ba6052009-08-03 20:36:38 +00002862 DAG.getIntPtrConstant(0));
2863 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002864 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2865 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilson96ba6052009-08-03 20:36:38 +00002866 DAG.getIntPtrConstant(1));
2867 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilsone60fee02009-06-22 23:27:02 +00002868}
2869
Dan Gohman8181bd12008-07-27 21:46:04 +00002870SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002871 switch (Op.getOpcode()) {
Edwin Törökbd448e32009-07-14 16:55:14 +00002872 default: llvm_unreachable("Don't know how to custom lower this!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002873 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson8f743382009-10-30 05:45:42 +00002874 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002875 case ISD::GlobalAddress:
2876 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
2877 LowerGlobalAddressELF(Op, DAG);
2878 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng3a2ce502009-11-12 07:13:11 +00002879 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
2880 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002881 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng815c23a2009-08-07 00:34:42 +00002882 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002883 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
2884 case ISD::SINT_TO_FP:
2885 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
2886 case ISD::FP_TO_SINT:
2887 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
2888 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002889 case ISD::RETURNADDR: break;
Jim Grosbachc10915b2009-05-12 23:59:14 +00002890 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002891 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Lauro Ramos Venancio5a87a8a2007-11-08 17:20:05 +00002892 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Duncan Sands7d9834b2008-12-01 11:39:25 +00002893 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilsone60fee02009-06-22 23:27:02 +00002894 case ISD::SHL:
Chris Lattnerdfb947d2007-11-24 07:07:01 +00002895 case ISD::SRL:
Bob Wilsone60fee02009-06-22 23:27:02 +00002896 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng3a2ce502009-11-12 07:13:11 +00002897 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbacha3bddfb2009-10-31 21:42:19 +00002898 case ISD::SRL_PARTS:
Evan Cheng3a2ce502009-11-12 07:13:11 +00002899 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Bob Wilsone60fee02009-06-22 23:27:02 +00002900 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
2901 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2902 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilsone60fee02009-06-22 23:27:02 +00002903 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilson96ba6052009-08-03 20:36:38 +00002904 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002905 }
Dan Gohman8181bd12008-07-27 21:46:04 +00002906 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002907}
2908
Duncan Sands7d9834b2008-12-01 11:39:25 +00002909/// ReplaceNodeResults - Replace the results of node with an illegal result
2910/// type with new values built out of custom code.
Duncan Sands7d9834b2008-12-01 11:39:25 +00002911void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
2912 SmallVectorImpl<SDValue>&Results,
2913 SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00002914 switch (N->getOpcode()) {
Duncan Sands7d9834b2008-12-01 11:39:25 +00002915 default:
Edwin Törökbd448e32009-07-14 16:55:14 +00002916 llvm_unreachable("Don't know how to custom expand this!");
Duncan Sands7d9834b2008-12-01 11:39:25 +00002917 return;
2918 case ISD::BIT_CONVERT:
2919 Results.push_back(ExpandBIT_CONVERT(N, DAG));
2920 return;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00002921 case ISD::SRL:
Duncan Sands7d9834b2008-12-01 11:39:25 +00002922 case ISD::SRA: {
Bob Wilsone60fee02009-06-22 23:27:02 +00002923 SDValue Res = LowerShift(N, DAG, Subtarget);
Duncan Sands7d9834b2008-12-01 11:39:25 +00002924 if (Res.getNode())
2925 Results.push_back(Res);
2926 return;
2927 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00002928 }
2929}
Chris Lattnerdfb947d2007-11-24 07:07:01 +00002930
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002931//===----------------------------------------------------------------------===//
2932// ARM Scheduler Hooks
2933//===----------------------------------------------------------------------===//
2934
2935MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00002936ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengd7dc9832009-09-18 21:02:19 +00002937 MachineBasicBlock *BB,
2938 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002939 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesene8a10c42009-02-13 02:25:56 +00002940 DebugLoc dl = MI->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002941 switch (MI->getOpcode()) {
Evan Cheng815c23a2009-08-07 00:34:42 +00002942 default:
2943 llvm_unreachable("Unexpected instr type to insert");
Evan Chengbf4111c2009-08-12 05:17:19 +00002944 case ARM::tMOVCCr_pseudo: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002945 // To "insert" a SELECT_CC instruction, we actually have to insert the
2946 // diamond control-flow pattern. The incoming instruction knows the
2947 // destination vreg to set, the condition code register to branch on, the
2948 // true/false values to select between, and a branch opcode to use.
2949 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00002950 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002951 ++It;
2952
2953 // thisMBB:
2954 // ...
2955 // TrueVal = ...
2956 // cmpTY ccX, r1, r2
2957 // bCC copy1MBB
2958 // fallthrough --> copy0MBB
2959 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00002960 MachineFunction *F = BB->getParent();
2961 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
2962 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesene8a10c42009-02-13 02:25:56 +00002963 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002964 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman221a4372008-07-07 23:14:23 +00002965 F->insert(It, copy0MBB);
2966 F->insert(It, sinkMBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002967 // Update machine-CFG edges by first adding all successors of the current
2968 // block to the new block which will contain the Phi node for the select.
Evan Cheng5f3a5402009-09-19 09:51:03 +00002969 // Also inform sdisel of the edge changes.
2970 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
2971 E = BB->succ_end(); I != E; ++I) {
2972 EM->insert(std::make_pair(*I, sinkMBB));
2973 sinkMBB->addSuccessor(*I);
2974 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002975 // Next, remove all successors of the current block, and add the true
2976 // and fallthrough blocks as its successors.
Evan Cheng5f3a5402009-09-19 09:51:03 +00002977 while (!BB->succ_empty())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002978 BB->removeSuccessor(BB->succ_begin());
2979 BB->addSuccessor(copy0MBB);
2980 BB->addSuccessor(sinkMBB);
2981
2982 // copy0MBB:
2983 // %FalseValue = ...
2984 // # fallthrough to sinkMBB
2985 BB = copy0MBB;
2986
2987 // Update machine-CFG edges
2988 BB->addSuccessor(sinkMBB);
2989
2990 // sinkMBB:
2991 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2992 // ...
2993 BB = sinkMBB;
Dale Johannesene8a10c42009-02-13 02:25:56 +00002994 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002995 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
2996 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
2997
Dan Gohman221a4372008-07-07 23:14:23 +00002998 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002999 return BB;
3000 }
Evan Cheng815c23a2009-08-07 00:34:42 +00003001
3002 case ARM::tANDsp:
3003 case ARM::tADDspr_:
3004 case ARM::tSUBspi_:
3005 case ARM::t2SUBrSPi_:
3006 case ARM::t2SUBrSPi12_:
3007 case ARM::t2SUBrSPs_: {
3008 MachineFunction *MF = BB->getParent();
3009 unsigned DstReg = MI->getOperand(0).getReg();
3010 unsigned SrcReg = MI->getOperand(1).getReg();
3011 bool DstIsDead = MI->getOperand(0).isDead();
3012 bool SrcIsKill = MI->getOperand(1).isKill();
3013
3014 if (SrcReg != ARM::SP) {
3015 // Copy the source to SP from virtual register.
3016 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
3017 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3018 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
3019 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
3020 .addReg(SrcReg, getKillRegState(SrcIsKill));
3021 }
3022
3023 unsigned OpOpc = 0;
3024 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
3025 switch (MI->getOpcode()) {
3026 default:
3027 llvm_unreachable("Unexpected pseudo instruction!");
3028 case ARM::tANDsp:
3029 OpOpc = ARM::tAND;
3030 NeedPred = true;
3031 break;
3032 case ARM::tADDspr_:
3033 OpOpc = ARM::tADDspr;
3034 break;
3035 case ARM::tSUBspi_:
3036 OpOpc = ARM::tSUBspi;
3037 break;
3038 case ARM::t2SUBrSPi_:
3039 OpOpc = ARM::t2SUBrSPi;
3040 NeedPred = true; NeedCC = true;
3041 break;
3042 case ARM::t2SUBrSPi12_:
3043 OpOpc = ARM::t2SUBrSPi12;
3044 NeedPred = true;
3045 break;
3046 case ARM::t2SUBrSPs_:
3047 OpOpc = ARM::t2SUBrSPs;
3048 NeedPred = true; NeedCC = true; NeedOp3 = true;
3049 break;
3050 }
3051 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
3052 if (OpOpc == ARM::tAND)
3053 AddDefaultT1CC(MIB);
3054 MIB.addReg(ARM::SP);
3055 MIB.addOperand(MI->getOperand(2));
3056 if (NeedOp3)
3057 MIB.addOperand(MI->getOperand(3));
3058 if (NeedPred)
3059 AddDefaultPred(MIB);
3060 if (NeedCC)
3061 AddDefaultCC(MIB);
3062
3063 // Copy the result from SP to virtual register.
3064 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
3065 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3066 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
3067 BuildMI(BB, dl, TII->get(CopyOpc))
3068 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
3069 .addReg(ARM::SP);
3070 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3071 return BB;
3072 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003073 }
3074}
3075
3076//===----------------------------------------------------------------------===//
3077// ARM Optimization Hooks
3078//===----------------------------------------------------------------------===//
3079
Chris Lattnere4577dc2009-03-12 06:52:53 +00003080static
3081SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3082 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnere4577dc2009-03-12 06:52:53 +00003083 SelectionDAG &DAG = DCI.DAG;
3084 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersonac9de032009-08-10 22:56:29 +00003085 EVT VT = N->getValueType(0);
Chris Lattnere4577dc2009-03-12 06:52:53 +00003086 unsigned Opc = N->getOpcode();
3087 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3088 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3089 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3090 ISD::CondCode CC = ISD::SETCC_INVALID;
3091
3092 if (isSlctCC) {
3093 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
3094 } else {
3095 SDValue CCOp = Slct.getOperand(0);
3096 if (CCOp.getOpcode() == ISD::SETCC)
3097 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
3098 }
3099
3100 bool DoXform = false;
3101 bool InvCC = false;
3102 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
3103 "Bad input!");
3104
3105 if (LHS.getOpcode() == ISD::Constant &&
3106 cast<ConstantSDNode>(LHS)->isNullValue()) {
3107 DoXform = true;
3108 } else if (CC != ISD::SETCC_INVALID &&
3109 RHS.getOpcode() == ISD::Constant &&
3110 cast<ConstantSDNode>(RHS)->isNullValue()) {
3111 std::swap(LHS, RHS);
3112 SDValue Op0 = Slct.getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00003113 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnere4577dc2009-03-12 06:52:53 +00003114 Op0.getOperand(0).getValueType();
3115 bool isInt = OpVT.isInteger();
3116 CC = ISD::getSetCCInverse(CC, isInt);
3117
3118 if (!TLI.isCondCodeLegal(CC, OpVT))
3119 return SDValue(); // Inverse operator isn't legal.
3120
3121 DoXform = true;
3122 InvCC = true;
3123 }
3124
3125 if (DoXform) {
3126 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3127 if (isSlctCC)
3128 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3129 Slct.getOperand(0), Slct.getOperand(1), CC);
3130 SDValue CCOp = Slct.getOperand(0);
3131 if (InvCC)
3132 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3133 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3134 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3135 CCOp, OtherOp, Result);
3136 }
3137 return SDValue();
3138}
3139
3140/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3141static SDValue PerformADDCombine(SDNode *N,
3142 TargetLowering::DAGCombinerInfo &DCI) {
3143 // added by evan in r37685 with no testcase.
3144 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson0eb45b42009-03-20 22:42:55 +00003145
Chris Lattnere4577dc2009-03-12 06:52:53 +00003146 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3147 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3148 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3149 if (Result.getNode()) return Result;
3150 }
3151 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3152 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3153 if (Result.getNode()) return Result;
3154 }
Bob Wilson0eb45b42009-03-20 22:42:55 +00003155
Chris Lattnere4577dc2009-03-12 06:52:53 +00003156 return SDValue();
3157}
3158
3159/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3160static SDValue PerformSUBCombine(SDNode *N,
3161 TargetLowering::DAGCombinerInfo &DCI) {
3162 // added by evan in r37685 with no testcase.
3163 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson0eb45b42009-03-20 22:42:55 +00003164
Chris Lattnere4577dc2009-03-12 06:52:53 +00003165 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3166 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3167 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3168 if (Result.getNode()) return Result;
3169 }
Bob Wilson0eb45b42009-03-20 22:42:55 +00003170
Chris Lattnere4577dc2009-03-12 06:52:53 +00003171 return SDValue();
3172}
3173
Jim Grosbache2fda532009-11-09 00:11:35 +00003174/// PerformVMOVRRDCombine - Target-specific dag combine xforms for ARMISD::VMOVRRD.
3175static SDValue PerformVMOVRRDCombine(SDNode *N,
Bob Wilson0eb45b42009-03-20 22:42:55 +00003176 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattner900cddb2007-11-27 22:36:16 +00003177 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman8181bd12008-07-27 21:46:04 +00003178 SDValue InDouble = N->getOperand(0);
Jim Grosbache2fda532009-11-09 00:11:35 +00003179 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
Chris Lattner900cddb2007-11-27 22:36:16 +00003180 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003181 return SDValue();
Chris Lattner900cddb2007-11-27 22:36:16 +00003182}
3183
Bob Wilsone60fee02009-06-22 23:27:02 +00003184/// getVShiftImm - Check if this is a valid build_vector for the immediate
3185/// operand of a vector shift operation, where all the elements of the
3186/// build_vector must have the same constant integer value.
3187static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3188 // Ignore bit_converts.
3189 while (Op.getOpcode() == ISD::BIT_CONVERT)
3190 Op = Op.getOperand(0);
3191 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3192 APInt SplatBits, SplatUndef;
3193 unsigned SplatBitSize;
3194 bool HasAnyUndefs;
3195 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3196 HasAnyUndefs, ElementBits) ||
3197 SplatBitSize > ElementBits)
3198 return false;
3199 Cnt = SplatBits.getSExtValue();
3200 return true;
3201}
3202
3203/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3204/// operand of a vector shift left operation. That value must be in the range:
3205/// 0 <= Value < ElementBits for a left shift; or
3206/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersonac9de032009-08-10 22:56:29 +00003207static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilsone60fee02009-06-22 23:27:02 +00003208 assert(VT.isVector() && "vector shift count is not a vector type");
3209 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3210 if (! getVShiftImm(Op, ElementBits, Cnt))
3211 return false;
3212 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3213}
3214
3215/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3216/// operand of a vector shift right operation. For a shift opcode, the value
3217/// is positive, but for an intrinsic the value count must be negative. The
3218/// absolute value must be in the range:
3219/// 1 <= |Value| <= ElementBits for a right shift; or
3220/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersonac9de032009-08-10 22:56:29 +00003221static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilsone60fee02009-06-22 23:27:02 +00003222 int64_t &Cnt) {
3223 assert(VT.isVector() && "vector shift count is not a vector type");
3224 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3225 if (! getVShiftImm(Op, ElementBits, Cnt))
3226 return false;
3227 if (isIntrinsic)
3228 Cnt = -Cnt;
3229 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3230}
3231
3232/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3233static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3234 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3235 switch (IntNo) {
3236 default:
3237 // Don't do anything for most intrinsics.
3238 break;
3239
3240 // Vector shifts: check for immediate versions and lower them.
3241 // Note: This is done during DAG combining instead of DAG legalizing because
3242 // the build_vectors for 64-bit vector element shift counts are generally
3243 // not legal, and it is hard to see their values after they get legalized to
3244 // loads from a constant pool.
3245 case Intrinsic::arm_neon_vshifts:
3246 case Intrinsic::arm_neon_vshiftu:
3247 case Intrinsic::arm_neon_vshiftls:
3248 case Intrinsic::arm_neon_vshiftlu:
3249 case Intrinsic::arm_neon_vshiftn:
3250 case Intrinsic::arm_neon_vrshifts:
3251 case Intrinsic::arm_neon_vrshiftu:
3252 case Intrinsic::arm_neon_vrshiftn:
3253 case Intrinsic::arm_neon_vqshifts:
3254 case Intrinsic::arm_neon_vqshiftu:
3255 case Intrinsic::arm_neon_vqshiftsu:
3256 case Intrinsic::arm_neon_vqshiftns:
3257 case Intrinsic::arm_neon_vqshiftnu:
3258 case Intrinsic::arm_neon_vqshiftnsu:
3259 case Intrinsic::arm_neon_vqrshiftns:
3260 case Intrinsic::arm_neon_vqrshiftnu:
3261 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersonac9de032009-08-10 22:56:29 +00003262 EVT VT = N->getOperand(1).getValueType();
Bob Wilsone60fee02009-06-22 23:27:02 +00003263 int64_t Cnt;
3264 unsigned VShiftOpc = 0;
3265
3266 switch (IntNo) {
3267 case Intrinsic::arm_neon_vshifts:
3268 case Intrinsic::arm_neon_vshiftu:
3269 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3270 VShiftOpc = ARMISD::VSHL;
3271 break;
3272 }
3273 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3274 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3275 ARMISD::VSHRs : ARMISD::VSHRu);
3276 break;
3277 }
3278 return SDValue();
3279
3280 case Intrinsic::arm_neon_vshiftls:
3281 case Intrinsic::arm_neon_vshiftlu:
3282 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3283 break;
Edwin Törökbd448e32009-07-14 16:55:14 +00003284 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilsone60fee02009-06-22 23:27:02 +00003285
3286 case Intrinsic::arm_neon_vrshifts:
3287 case Intrinsic::arm_neon_vrshiftu:
3288 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3289 break;
3290 return SDValue();
3291
3292 case Intrinsic::arm_neon_vqshifts:
3293 case Intrinsic::arm_neon_vqshiftu:
3294 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3295 break;
3296 return SDValue();
3297
3298 case Intrinsic::arm_neon_vqshiftsu:
3299 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3300 break;
Edwin Törökbd448e32009-07-14 16:55:14 +00003301 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilsone60fee02009-06-22 23:27:02 +00003302
3303 case Intrinsic::arm_neon_vshiftn:
3304 case Intrinsic::arm_neon_vrshiftn:
3305 case Intrinsic::arm_neon_vqshiftns:
3306 case Intrinsic::arm_neon_vqshiftnu:
3307 case Intrinsic::arm_neon_vqshiftnsu:
3308 case Intrinsic::arm_neon_vqrshiftns:
3309 case Intrinsic::arm_neon_vqrshiftnu:
3310 case Intrinsic::arm_neon_vqrshiftnsu:
3311 // Narrowing shifts require an immediate right shift.
3312 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3313 break;
Edwin Törökbd448e32009-07-14 16:55:14 +00003314 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilsone60fee02009-06-22 23:27:02 +00003315
3316 default:
Edwin Törökbd448e32009-07-14 16:55:14 +00003317 llvm_unreachable("unhandled vector shift");
Bob Wilsone60fee02009-06-22 23:27:02 +00003318 }
3319
3320 switch (IntNo) {
3321 case Intrinsic::arm_neon_vshifts:
3322 case Intrinsic::arm_neon_vshiftu:
3323 // Opcode already set above.
3324 break;
3325 case Intrinsic::arm_neon_vshiftls:
3326 case Intrinsic::arm_neon_vshiftlu:
3327 if (Cnt == VT.getVectorElementType().getSizeInBits())
3328 VShiftOpc = ARMISD::VSHLLi;
3329 else
3330 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3331 ARMISD::VSHLLs : ARMISD::VSHLLu);
3332 break;
3333 case Intrinsic::arm_neon_vshiftn:
3334 VShiftOpc = ARMISD::VSHRN; break;
3335 case Intrinsic::arm_neon_vrshifts:
3336 VShiftOpc = ARMISD::VRSHRs; break;
3337 case Intrinsic::arm_neon_vrshiftu:
3338 VShiftOpc = ARMISD::VRSHRu; break;
3339 case Intrinsic::arm_neon_vrshiftn:
3340 VShiftOpc = ARMISD::VRSHRN; break;
3341 case Intrinsic::arm_neon_vqshifts:
3342 VShiftOpc = ARMISD::VQSHLs; break;
3343 case Intrinsic::arm_neon_vqshiftu:
3344 VShiftOpc = ARMISD::VQSHLu; break;
3345 case Intrinsic::arm_neon_vqshiftsu:
3346 VShiftOpc = ARMISD::VQSHLsu; break;
3347 case Intrinsic::arm_neon_vqshiftns:
3348 VShiftOpc = ARMISD::VQSHRNs; break;
3349 case Intrinsic::arm_neon_vqshiftnu:
3350 VShiftOpc = ARMISD::VQSHRNu; break;
3351 case Intrinsic::arm_neon_vqshiftnsu:
3352 VShiftOpc = ARMISD::VQSHRNsu; break;
3353 case Intrinsic::arm_neon_vqrshiftns:
3354 VShiftOpc = ARMISD::VQRSHRNs; break;
3355 case Intrinsic::arm_neon_vqrshiftnu:
3356 VShiftOpc = ARMISD::VQRSHRNu; break;
3357 case Intrinsic::arm_neon_vqrshiftnsu:
3358 VShiftOpc = ARMISD::VQRSHRNsu; break;
3359 }
3360
3361 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003362 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilsone60fee02009-06-22 23:27:02 +00003363 }
3364
3365 case Intrinsic::arm_neon_vshiftins: {
Owen Andersonac9de032009-08-10 22:56:29 +00003366 EVT VT = N->getOperand(1).getValueType();
Bob Wilsone60fee02009-06-22 23:27:02 +00003367 int64_t Cnt;
3368 unsigned VShiftOpc = 0;
3369
3370 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3371 VShiftOpc = ARMISD::VSLI;
3372 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3373 VShiftOpc = ARMISD::VSRI;
3374 else {
Edwin Törökbd448e32009-07-14 16:55:14 +00003375 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilsone60fee02009-06-22 23:27:02 +00003376 }
3377
3378 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3379 N->getOperand(1), N->getOperand(2),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003380 DAG.getConstant(Cnt, MVT::i32));
Bob Wilsone60fee02009-06-22 23:27:02 +00003381 }
3382
3383 case Intrinsic::arm_neon_vqrshifts:
3384 case Intrinsic::arm_neon_vqrshiftu:
3385 // No immediate versions of these to check for.
3386 break;
3387 }
3388
3389 return SDValue();
3390}
3391
3392/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3393/// lowers them. As with the vector shift intrinsics, this is done during DAG
3394/// combining instead of DAG legalizing because the build_vectors for 64-bit
3395/// vector element shift counts are generally not legal, and it is hard to see
3396/// their values after they get legalized to loads from a constant pool.
3397static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3398 const ARMSubtarget *ST) {
Owen Andersonac9de032009-08-10 22:56:29 +00003399 EVT VT = N->getValueType(0);
Bob Wilsone60fee02009-06-22 23:27:02 +00003400
3401 // Nothing to be done for scalar shifts.
3402 if (! VT.isVector())
3403 return SDValue();
3404
3405 assert(ST->hasNEON() && "unexpected vector shift");
3406 int64_t Cnt;
3407
3408 switch (N->getOpcode()) {
Edwin Törökbd448e32009-07-14 16:55:14 +00003409 default: llvm_unreachable("unexpected shift opcode");
Bob Wilsone60fee02009-06-22 23:27:02 +00003410
3411 case ISD::SHL:
3412 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3413 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003414 DAG.getConstant(Cnt, MVT::i32));
Bob Wilsone60fee02009-06-22 23:27:02 +00003415 break;
3416
3417 case ISD::SRA:
3418 case ISD::SRL:
3419 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3420 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3421 ARMISD::VSHRs : ARMISD::VSHRu);
3422 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003423 DAG.getConstant(Cnt, MVT::i32));
Bob Wilsone60fee02009-06-22 23:27:02 +00003424 }
3425 }
3426 return SDValue();
3427}
3428
3429/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3430/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3431static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3432 const ARMSubtarget *ST) {
3433 SDValue N0 = N->getOperand(0);
3434
3435 // Check for sign- and zero-extensions of vector extract operations of 8-
3436 // and 16-bit vector elements. NEON supports these directly. They are
3437 // handled during DAG combining because type legalization will promote them
3438 // to 32-bit types and it is messy to recognize the operations after that.
3439 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3440 SDValue Vec = N0.getOperand(0);
3441 SDValue Lane = N0.getOperand(1);
Owen Andersonac9de032009-08-10 22:56:29 +00003442 EVT VT = N->getValueType(0);
3443 EVT EltVT = N0.getValueType();
Bob Wilsone60fee02009-06-22 23:27:02 +00003444 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3445
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003446 if (VT == MVT::i32 &&
3447 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsone60fee02009-06-22 23:27:02 +00003448 TLI.isTypeLegal(Vec.getValueType())) {
3449
3450 unsigned Opc = 0;
3451 switch (N->getOpcode()) {
Edwin Törökbd448e32009-07-14 16:55:14 +00003452 default: llvm_unreachable("unexpected opcode");
Bob Wilsone60fee02009-06-22 23:27:02 +00003453 case ISD::SIGN_EXTEND:
3454 Opc = ARMISD::VGETLANEs;
3455 break;
3456 case ISD::ZERO_EXTEND:
3457 case ISD::ANY_EXTEND:
3458 Opc = ARMISD::VGETLANEu;
3459 break;
3460 }
3461 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
3462 }
3463 }
3464
3465 return SDValue();
3466}
3467
Dan Gohman8181bd12008-07-27 21:46:04 +00003468SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson0eb45b42009-03-20 22:42:55 +00003469 DAGCombinerInfo &DCI) const {
Chris Lattner900cddb2007-11-27 22:36:16 +00003470 switch (N->getOpcode()) {
3471 default: break;
Chris Lattnere4577dc2009-03-12 06:52:53 +00003472 case ISD::ADD: return PerformADDCombine(N, DCI);
3473 case ISD::SUB: return PerformSUBCombine(N, DCI);
Jim Grosbache2fda532009-11-09 00:11:35 +00003474 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilsone60fee02009-06-22 23:27:02 +00003475 case ISD::INTRINSIC_WO_CHAIN:
3476 return PerformIntrinsicCombine(N, DCI.DAG);
3477 case ISD::SHL:
3478 case ISD::SRA:
3479 case ISD::SRL:
3480 return PerformShiftCombine(N, DCI.DAG, Subtarget);
3481 case ISD::SIGN_EXTEND:
3482 case ISD::ZERO_EXTEND:
3483 case ISD::ANY_EXTEND:
3484 return PerformExtendCombine(N, DCI.DAG, Subtarget);
Chris Lattner900cddb2007-11-27 22:36:16 +00003485 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003486 return SDValue();
Chris Lattner900cddb2007-11-27 22:36:16 +00003487}
3488
Bill Wendling5c433f32009-08-15 21:21:19 +00003489bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
3490 if (!Subtarget->hasV6Ops())
3491 // Pre-v6 does not support unaligned mem access.
3492 return false;
3493 else if (!Subtarget->hasV6Ops()) {
3494 // v6 may or may not support unaligned mem access.
3495 if (!Subtarget->isTargetDarwin())
3496 return false;
3497 }
3498
3499 switch (VT.getSimpleVT().SimpleTy) {
3500 default:
3501 return false;
3502 case MVT::i8:
3503 case MVT::i16:
3504 case MVT::i32:
3505 return true;
3506 // FIXME: VLD1 etc with standard alignment is legal.
3507 }
3508}
3509
Evan Chenga71c2b62009-08-14 20:09:37 +00003510static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
3511 if (V < 0)
3512 return false;
3513
3514 unsigned Scale = 1;
3515 switch (VT.getSimpleVT().SimpleTy) {
3516 default: return false;
3517 case MVT::i1:
3518 case MVT::i8:
3519 // Scale == 1;
3520 break;
3521 case MVT::i16:
3522 // Scale == 2;
3523 Scale = 2;
3524 break;
3525 case MVT::i32:
3526 // Scale == 4;
3527 Scale = 4;
3528 break;
3529 }
3530
3531 if ((V & (Scale - 1)) != 0)
3532 return false;
3533 V /= Scale;
3534 return V == (V & ((1LL << 5) - 1));
3535}
3536
3537static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
3538 const ARMSubtarget *Subtarget) {
3539 bool isNeg = false;
3540 if (V < 0) {
3541 isNeg = true;
3542 V = - V;
3543 }
3544
3545 switch (VT.getSimpleVT().SimpleTy) {
3546 default: return false;
3547 case MVT::i1:
3548 case MVT::i8:
3549 case MVT::i16:
3550 case MVT::i32:
3551 // + imm12 or - imm8
3552 if (isNeg)
3553 return V == (V & ((1LL << 8) - 1));
3554 return V == (V & ((1LL << 12) - 1));
3555 case MVT::f32:
3556 case MVT::f64:
3557 // Same as ARM mode. FIXME: NEON?
3558 if (!Subtarget->hasVFP2())
3559 return false;
3560 if ((V & 3) != 0)
3561 return false;
3562 V >>= 2;
3563 return V == (V & ((1LL << 8) - 1));
3564 }
3565}
3566
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003567/// isLegalAddressImmediate - Return true if the integer value can be used
3568/// as the offset of the target addressing mode for load / store of the
3569/// given type.
Owen Andersonac9de032009-08-10 22:56:29 +00003570static bool isLegalAddressImmediate(int64_t V, EVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003571 const ARMSubtarget *Subtarget) {
3572 if (V == 0)
3573 return true;
3574
Evan Chengb882875f2009-03-09 19:15:00 +00003575 if (!VT.isSimple())
3576 return false;
3577
Evan Chenga71c2b62009-08-14 20:09:37 +00003578 if (Subtarget->isThumb1Only())
3579 return isLegalT1AddressImmediate(V, VT);
3580 else if (Subtarget->isThumb2())
3581 return isLegalT2AddressImmediate(V, VT, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003582
Evan Chenga71c2b62009-08-14 20:09:37 +00003583 // ARM mode.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003584 if (V < 0)
3585 V = - V;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003586 switch (VT.getSimpleVT().SimpleTy) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003587 default: return false;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003588 case MVT::i1:
3589 case MVT::i8:
3590 case MVT::i32:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003591 // +- imm12
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003592 return V == (V & ((1LL << 12) - 1));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003593 case MVT::i16:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003594 // +- imm8
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003595 return V == (V & ((1LL << 8) - 1));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003596 case MVT::f32:
3597 case MVT::f64:
Evan Chenga71c2b62009-08-14 20:09:37 +00003598 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003599 return false;
3600 if ((V & 3) != 0)
3601 return false;
3602 V >>= 2;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003603 return V == (V & ((1LL << 8) - 1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003604 }
3605}
3606
Evan Chenga71c2b62009-08-14 20:09:37 +00003607bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
3608 EVT VT) const {
3609 int Scale = AM.Scale;
3610 if (Scale < 0)
3611 return false;
3612
3613 switch (VT.getSimpleVT().SimpleTy) {
3614 default: return false;
3615 case MVT::i1:
3616 case MVT::i8:
3617 case MVT::i16:
3618 case MVT::i32:
3619 if (Scale == 1)
3620 return true;
3621 // r + r << imm
3622 Scale = Scale & ~1;
3623 return Scale == 2 || Scale == 4 || Scale == 8;
3624 case MVT::i64:
3625 // r + r
3626 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
3627 return true;
3628 return false;
3629 case MVT::isVoid:
3630 // Note, we allow "void" uses (basically, uses that aren't loads or
3631 // stores), because arm allows folding a scale into many arithmetic
3632 // operations. This should be made more precise and revisited later.
3633
3634 // Allow r << imm, but the imm has to be a multiple of two.
3635 if (Scale & 1) return false;
3636 return isPowerOf2_32(Scale);
3637 }
3638}
3639
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003640/// isLegalAddressingMode - Return true if the addressing mode represented
3641/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson0eb45b42009-03-20 22:42:55 +00003642bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003643 const Type *Ty) const {
Owen Andersonac9de032009-08-10 22:56:29 +00003644 EVT VT = getValueType(Ty, true);
Bob Wilsone69efe62009-04-08 17:55:28 +00003645 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003646 return false;
Bob Wilson0eb45b42009-03-20 22:42:55 +00003647
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003648 // Can never fold addr of global into load/store.
Bob Wilson0eb45b42009-03-20 22:42:55 +00003649 if (AM.BaseGV)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003650 return false;
Bob Wilson0eb45b42009-03-20 22:42:55 +00003651
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003652 switch (AM.Scale) {
3653 case 0: // no scale reg, must be "r+i" or "r", or "i".
3654 break;
3655 case 1:
Evan Chenga71c2b62009-08-14 20:09:37 +00003656 if (Subtarget->isThumb1Only())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003657 return false;
3658 // FALL THROUGH.
3659 default:
3660 // ARM doesn't support any R+R*scale+imm addr modes.
3661 if (AM.BaseOffs)
3662 return false;
Bob Wilson0eb45b42009-03-20 22:42:55 +00003663
Bob Wilsone69efe62009-04-08 17:55:28 +00003664 if (!VT.isSimple())
3665 return false;
3666
Evan Chenga71c2b62009-08-14 20:09:37 +00003667 if (Subtarget->isThumb2())
3668 return isLegalT2ScaledAddressingMode(AM, VT);
3669
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003670 int Scale = AM.Scale;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003671 switch (VT.getSimpleVT().SimpleTy) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003672 default: return false;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003673 case MVT::i1:
3674 case MVT::i8:
3675 case MVT::i32:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003676 if (Scale < 0) Scale = -Scale;
3677 if (Scale == 1)
3678 return true;
3679 // r + r << imm
3680 return isPowerOf2_32(Scale & ~1);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003681 case MVT::i16:
Evan Chenga71c2b62009-08-14 20:09:37 +00003682 case MVT::i64:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003683 // r + r
3684 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
3685 return true;
3686 return false;
Bob Wilson0eb45b42009-03-20 22:42:55 +00003687
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003688 case MVT::isVoid:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003689 // Note, we allow "void" uses (basically, uses that aren't loads or
3690 // stores), because arm allows folding a scale into many arithmetic
3691 // operations. This should be made more precise and revisited later.
Bob Wilson0eb45b42009-03-20 22:42:55 +00003692
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003693 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenga71c2b62009-08-14 20:09:37 +00003694 if (Scale & 1) return false;
3695 return isPowerOf2_32(Scale);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003696 }
3697 break;
3698 }
3699 return true;
3700}
3701
Evan Cheng26214692009-11-11 19:05:52 +00003702/// isLegalICmpImmediate - Return true if the specified immediate is legal
3703/// icmp immediate, that is the target has icmp instructions which can compare
3704/// a register against the immediate without having to materialize the
3705/// immediate into a register.
Evan Cheng3a2ce502009-11-12 07:13:11 +00003706bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng26214692009-11-11 19:05:52 +00003707 if (!Subtarget->isThumb())
3708 return ARM_AM::getSOImmVal(Imm) != -1;
3709 if (Subtarget->isThumb2())
3710 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng3a2ce502009-11-12 07:13:11 +00003711 return Imm >= 0 && Imm <= 255;
Evan Cheng26214692009-11-11 19:05:52 +00003712}
3713
Owen Andersonac9de032009-08-10 22:56:29 +00003714static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenga90942e2009-07-02 07:28:31 +00003715 bool isSEXTLoad, SDValue &Base,
3716 SDValue &Offset, bool &isInc,
3717 SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003718 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3719 return false;
3720
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003721 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003722 // AddressingMode 3
3723 Base = Ptr->getOperand(0);
3724 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003725 int RHSC = (int)RHS->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003726 if (RHSC < 0 && RHSC > -256) {
Evan Chenga90942e2009-07-02 07:28:31 +00003727 assert(Ptr->getOpcode() == ISD::ADD);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003728 isInc = false;
3729 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3730 return true;
3731 }
3732 }
3733 isInc = (Ptr->getOpcode() == ISD::ADD);
3734 Offset = Ptr->getOperand(1);
3735 return true;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003736 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003737 // AddressingMode 2
3738 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003739 int RHSC = (int)RHS->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003740 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenga90942e2009-07-02 07:28:31 +00003741 assert(Ptr->getOpcode() == ISD::ADD);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003742 isInc = false;
3743 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3744 Base = Ptr->getOperand(0);
3745 return true;
3746 }
3747 }
3748
3749 if (Ptr->getOpcode() == ISD::ADD) {
3750 isInc = true;
3751 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
3752 if (ShOpcVal != ARM_AM::no_shift) {
3753 Base = Ptr->getOperand(1);
3754 Offset = Ptr->getOperand(0);
3755 } else {
3756 Base = Ptr->getOperand(0);
3757 Offset = Ptr->getOperand(1);
3758 }
3759 return true;
3760 }
3761
3762 isInc = (Ptr->getOpcode() == ISD::ADD);
3763 Base = Ptr->getOperand(0);
3764 Offset = Ptr->getOperand(1);
3765 return true;
3766 }
3767
Jim Grosbache2fda532009-11-09 00:11:35 +00003768 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003769 return false;
3770}
3771
Owen Andersonac9de032009-08-10 22:56:29 +00003772static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenga90942e2009-07-02 07:28:31 +00003773 bool isSEXTLoad, SDValue &Base,
3774 SDValue &Offset, bool &isInc,
3775 SelectionDAG &DAG) {
3776 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3777 return false;
3778
3779 Base = Ptr->getOperand(0);
3780 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
3781 int RHSC = (int)RHS->getZExtValue();
3782 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
3783 assert(Ptr->getOpcode() == ISD::ADD);
3784 isInc = false;
3785 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3786 return true;
3787 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
3788 isInc = Ptr->getOpcode() == ISD::ADD;
3789 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
3790 return true;
3791 }
3792 }
3793
3794 return false;
3795}
3796
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003797/// getPreIndexedAddressParts - returns true by value, base pointer and
3798/// offset pointer and addressing mode by reference if the node's address
3799/// can be legally represented as pre-indexed load / store address.
3800bool
Dan Gohman8181bd12008-07-27 21:46:04 +00003801ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
3802 SDValue &Offset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003803 ISD::MemIndexedMode &AM,
Dan Gohmanb9e10262009-01-15 16:29:45 +00003804 SelectionDAG &DAG) const {
Evan Chenga90942e2009-07-02 07:28:31 +00003805 if (Subtarget->isThumb1Only())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003806 return false;
3807
Owen Andersonac9de032009-08-10 22:56:29 +00003808 EVT VT;
Dan Gohman8181bd12008-07-27 21:46:04 +00003809 SDValue Ptr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003810 bool isSEXTLoad = false;
3811 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
3812 Ptr = LD->getBasePtr();
Dan Gohman9a4c92c2008-01-30 00:15:11 +00003813 VT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003814 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3815 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
3816 Ptr = ST->getBasePtr();
Dan Gohman9a4c92c2008-01-30 00:15:11 +00003817 VT = ST->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003818 } else
3819 return false;
3820
3821 bool isInc;
Evan Chenga90942e2009-07-02 07:28:31 +00003822 bool isLegal = false;
Evan Chenga71c2b62009-08-14 20:09:37 +00003823 if (Subtarget->isThumb2())
Evan Chenga90942e2009-07-02 07:28:31 +00003824 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
3825 Offset, isInc, DAG);
Jim Grosbach770d7182009-08-11 15:33:49 +00003826 else
Evan Chenga90942e2009-07-02 07:28:31 +00003827 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng89191fe2009-07-02 06:44:30 +00003828 Offset, isInc, DAG);
Evan Chenga90942e2009-07-02 07:28:31 +00003829 if (!isLegal)
3830 return false;
3831
3832 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
3833 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003834}
3835
3836/// getPostIndexedAddressParts - returns true by value, base pointer and
3837/// offset pointer and addressing mode by reference if this node can be
3838/// combined with a load / store to form a post-indexed load / store.
3839bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman8181bd12008-07-27 21:46:04 +00003840 SDValue &Base,
3841 SDValue &Offset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003842 ISD::MemIndexedMode &AM,
Dan Gohmanb9e10262009-01-15 16:29:45 +00003843 SelectionDAG &DAG) const {
Evan Chenga90942e2009-07-02 07:28:31 +00003844 if (Subtarget->isThumb1Only())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003845 return false;
3846
Owen Andersonac9de032009-08-10 22:56:29 +00003847 EVT VT;
Dan Gohman8181bd12008-07-27 21:46:04 +00003848 SDValue Ptr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003849 bool isSEXTLoad = false;
3850 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman9a4c92c2008-01-30 00:15:11 +00003851 VT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003852 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3853 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman9a4c92c2008-01-30 00:15:11 +00003854 VT = ST->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003855 } else
3856 return false;
3857
3858 bool isInc;
Evan Chenga90942e2009-07-02 07:28:31 +00003859 bool isLegal = false;
Evan Chenga71c2b62009-08-14 20:09:37 +00003860 if (Subtarget->isThumb2())
Evan Chenga90942e2009-07-02 07:28:31 +00003861 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003862 isInc, DAG);
Jim Grosbach770d7182009-08-11 15:33:49 +00003863 else
Evan Chenga90942e2009-07-02 07:28:31 +00003864 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
3865 isInc, DAG);
3866 if (!isLegal)
3867 return false;
3868
3869 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
3870 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003871}
3872
Dan Gohman8181bd12008-07-27 21:46:04 +00003873void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00003874 const APInt &Mask,
Bob Wilson0eb45b42009-03-20 22:42:55 +00003875 APInt &KnownZero,
Dan Gohman229fa052008-02-13 00:35:47 +00003876 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003877 const SelectionDAG &DAG,
3878 unsigned Depth) const {
Dan Gohman229fa052008-02-13 00:35:47 +00003879 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003880 switch (Op.getOpcode()) {
3881 default: break;
3882 case ARMISD::CMOV: {
3883 // Bits are known zero/one if known on the LHS and RHS.
3884 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
3885 if (KnownZero == 0 && KnownOne == 0) return;
3886
Dan Gohman229fa052008-02-13 00:35:47 +00003887 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003888 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
3889 KnownZeroRHS, KnownOneRHS, Depth+1);
3890 KnownZero &= KnownZeroRHS;
3891 KnownOne &= KnownOneRHS;
3892 return;
3893 }
3894 }
3895}
3896
3897//===----------------------------------------------------------------------===//
3898// ARM Inline Assembly Support
3899//===----------------------------------------------------------------------===//
3900
3901/// getConstraintType - Given a constraint letter, return the type of
3902/// constraint it is for this target.
3903ARMTargetLowering::ConstraintType
3904ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
3905 if (Constraint.size() == 1) {
3906 switch (Constraint[0]) {
3907 default: break;
3908 case 'l': return C_RegisterClass;
3909 case 'w': return C_RegisterClass;
3910 }
3911 }
3912 return TargetLowering::getConstraintType(Constraint);
3913}
3914
Bob Wilson0eb45b42009-03-20 22:42:55 +00003915std::pair<unsigned, const TargetRegisterClass*>
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003916ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersonac9de032009-08-10 22:56:29 +00003917 EVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003918 if (Constraint.size() == 1) {
3919 // GCC RS6000 Constraint Letters
3920 switch (Constraint[0]) {
3921 case 'l':
David Goodwin4a897932009-07-08 23:10:31 +00003922 if (Subtarget->isThumb1Only())
Jim Grosbach0e4e9742009-04-07 20:34:09 +00003923 return std::make_pair(0U, ARM::tGPRRegisterClass);
3924 else
3925 return std::make_pair(0U, ARM::GPRRegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003926 case 'r':
3927 return std::make_pair(0U, ARM::GPRRegisterClass);
3928 case 'w':
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003929 if (VT == MVT::f32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003930 return std::make_pair(0U, ARM::SPRRegisterClass);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003931 if (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003932 return std::make_pair(0U, ARM::DPRRegisterClass);
3933 break;
3934 }
3935 }
3936 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3937}
3938
3939std::vector<unsigned> ARMTargetLowering::
3940getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersonac9de032009-08-10 22:56:29 +00003941 EVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003942 if (Constraint.size() != 1)
3943 return std::vector<unsigned>();
3944
3945 switch (Constraint[0]) { // GCC ARM Constraint Letters
3946 default: break;
3947 case 'l':
Jim Grosbach0e4e9742009-04-07 20:34:09 +00003948 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3949 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3950 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003951 case 'r':
3952 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3953 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3954 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
3955 ARM::R12, ARM::LR, 0);
3956 case 'w':
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003957 if (VT == MVT::f32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003958 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
3959 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
3960 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
3961 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
3962 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
3963 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
3964 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
3965 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003966 if (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003967 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
3968 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
3969 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
3970 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
3971 break;
3972 }
3973
3974 return std::vector<unsigned>();
3975}
Bob Wilson221511d2009-04-01 17:58:54 +00003976
3977/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3978/// vector. If it is invalid, don't add anything to Ops.
3979void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3980 char Constraint,
3981 bool hasMemory,
3982 std::vector<SDValue>&Ops,
3983 SelectionDAG &DAG) const {
3984 SDValue Result(0, 0);
3985
3986 switch (Constraint) {
3987 default: break;
3988 case 'I': case 'J': case 'K': case 'L':
3989 case 'M': case 'N': case 'O':
3990 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
3991 if (!C)
3992 return;
3993
3994 int64_t CVal64 = C->getSExtValue();
3995 int CVal = (int) CVal64;
3996 // None of these constraints allow values larger than 32 bits. Check
3997 // that the value fits in an int.
3998 if (CVal != CVal64)
3999 return;
4000
4001 switch (Constraint) {
4002 case 'I':
David Goodwin4a897932009-07-08 23:10:31 +00004003 if (Subtarget->isThumb1Only()) {
4004 // This must be a constant between 0 and 255, for ADD
4005 // immediates.
Bob Wilson221511d2009-04-01 17:58:54 +00004006 if (CVal >= 0 && CVal <= 255)
4007 break;
David Goodwin4a897932009-07-08 23:10:31 +00004008 } else if (Subtarget->isThumb2()) {
4009 // A constant that can be used as an immediate value in a
4010 // data-processing instruction.
4011 if (ARM_AM::getT2SOImmVal(CVal) != -1)
4012 break;
Bob Wilson221511d2009-04-01 17:58:54 +00004013 } else {
4014 // A constant that can be used as an immediate value in a
4015 // data-processing instruction.
4016 if (ARM_AM::getSOImmVal(CVal) != -1)
4017 break;
4018 }
4019 return;
4020
4021 case 'J':
David Goodwin4a897932009-07-08 23:10:31 +00004022 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilson221511d2009-04-01 17:58:54 +00004023 // This must be a constant between -255 and -1, for negated ADD
4024 // immediates. This can be used in GCC with an "n" modifier that
4025 // prints the negated value, for use with SUB instructions. It is
4026 // not useful otherwise but is implemented for compatibility.
4027 if (CVal >= -255 && CVal <= -1)
4028 break;
4029 } else {
4030 // This must be a constant between -4095 and 4095. It is not clear
4031 // what this constraint is intended for. Implemented for
4032 // compatibility with GCC.
4033 if (CVal >= -4095 && CVal <= 4095)
4034 break;
4035 }
4036 return;
4037
4038 case 'K':
David Goodwin4a897932009-07-08 23:10:31 +00004039 if (Subtarget->isThumb1Only()) {
Bob Wilson221511d2009-04-01 17:58:54 +00004040 // A 32-bit value where only one byte has a nonzero value. Exclude
4041 // zero to match GCC. This constraint is used by GCC internally for
4042 // constants that can be loaded with a move/shift combination.
4043 // It is not useful otherwise but is implemented for compatibility.
4044 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
4045 break;
David Goodwin4a897932009-07-08 23:10:31 +00004046 } else if (Subtarget->isThumb2()) {
4047 // A constant whose bitwise inverse can be used as an immediate
4048 // value in a data-processing instruction. This can be used in GCC
4049 // with a "B" modifier that prints the inverted value, for use with
4050 // BIC and MVN instructions. It is not useful otherwise but is
4051 // implemented for compatibility.
4052 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
4053 break;
Bob Wilson221511d2009-04-01 17:58:54 +00004054 } else {
4055 // A constant whose bitwise inverse can be used as an immediate
4056 // value in a data-processing instruction. This can be used in GCC
4057 // with a "B" modifier that prints the inverted value, for use with
4058 // BIC and MVN instructions. It is not useful otherwise but is
4059 // implemented for compatibility.
4060 if (ARM_AM::getSOImmVal(~CVal) != -1)
4061 break;
4062 }
4063 return;
4064
4065 case 'L':
David Goodwin4a897932009-07-08 23:10:31 +00004066 if (Subtarget->isThumb1Only()) {
Bob Wilson221511d2009-04-01 17:58:54 +00004067 // This must be a constant between -7 and 7,
4068 // for 3-operand ADD/SUB immediate instructions.
4069 if (CVal >= -7 && CVal < 7)
4070 break;
David Goodwin4a897932009-07-08 23:10:31 +00004071 } else if (Subtarget->isThumb2()) {
4072 // A constant whose negation can be used as an immediate value in a
4073 // data-processing instruction. This can be used in GCC with an "n"
4074 // modifier that prints the negated value, for use with SUB
4075 // instructions. It is not useful otherwise but is implemented for
4076 // compatibility.
4077 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
4078 break;
Bob Wilson221511d2009-04-01 17:58:54 +00004079 } else {
4080 // A constant whose negation can be used as an immediate value in a
4081 // data-processing instruction. This can be used in GCC with an "n"
4082 // modifier that prints the negated value, for use with SUB
4083 // instructions. It is not useful otherwise but is implemented for
4084 // compatibility.
4085 if (ARM_AM::getSOImmVal(-CVal) != -1)
4086 break;
4087 }
4088 return;
4089
4090 case 'M':
David Goodwin4a897932009-07-08 23:10:31 +00004091 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilson221511d2009-04-01 17:58:54 +00004092 // This must be a multiple of 4 between 0 and 1020, for
4093 // ADD sp + immediate.
4094 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
4095 break;
4096 } else {
4097 // A power of two or a constant between 0 and 32. This is used in
4098 // GCC for the shift amount on shifted register operands, but it is
4099 // useful in general for any shift amounts.
4100 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
4101 break;
4102 }
4103 return;
4104
4105 case 'N':
David Goodwin4a897932009-07-08 23:10:31 +00004106 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilson221511d2009-04-01 17:58:54 +00004107 // This must be a constant between 0 and 31, for shift amounts.
4108 if (CVal >= 0 && CVal <= 31)
4109 break;
4110 }
4111 return;
4112
4113 case 'O':
David Goodwin4a897932009-07-08 23:10:31 +00004114 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilson221511d2009-04-01 17:58:54 +00004115 // This must be a multiple of 4 between -508 and 508, for
4116 // ADD/SUB sp = sp + immediate.
4117 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
4118 break;
4119 }
4120 return;
4121 }
4122 Result = DAG.getTargetConstant(CVal, Op.getValueType());
4123 break;
4124 }
4125
4126 if (Result.getNode()) {
4127 Ops.push_back(Result);
4128 return;
4129 }
4130 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
4131 Ops, DAG);
4132}
Anton Korobeynikov3fad5522009-09-23 19:04:09 +00004133
4134bool
4135ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4136 // The ARM target isn't yet aware of offsets.
4137 return false;
4138}
Evan Cheng7c7a3ff2009-10-28 01:44:26 +00004139
4140int ARM::getVFPf32Imm(const APFloat &FPImm) {
4141 APInt Imm = FPImm.bitcastToAPInt();
4142 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
4143 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
4144 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
4145
4146 // We can handle 4 bits of mantissa.
4147 // mantissa = (16+UInt(e:f:g:h))/16.
4148 if (Mantissa & 0x7ffff)
4149 return -1;
4150 Mantissa >>= 19;
4151 if ((Mantissa & 0xf) != Mantissa)
4152 return -1;
4153
4154 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4155 if (Exp < -3 || Exp > 4)
4156 return -1;
4157 Exp = ((Exp+3) & 0x7) ^ 4;
4158
4159 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4160}
4161
4162int ARM::getVFPf64Imm(const APFloat &FPImm) {
4163 APInt Imm = FPImm.bitcastToAPInt();
4164 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
4165 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
4166 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
4167
4168 // We can handle 4 bits of mantissa.
4169 // mantissa = (16+UInt(e:f:g:h))/16.
4170 if (Mantissa & 0xffffffffffffLL)
4171 return -1;
4172 Mantissa >>= 48;
4173 if ((Mantissa & 0xf) != Mantissa)
4174 return -1;
4175
4176 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4177 if (Exp < -3 || Exp > 4)
4178 return -1;
4179 Exp = ((Exp+3) & 0x7) ^ 4;
4180
4181 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4182}
4183
4184/// isFPImmLegal - Returns true if the target can instruction select the
4185/// specified FP immediate natively. If false, the legalizer will
4186/// materialize the FP immediate as a load from a constant pool.
4187bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
4188 if (!Subtarget->hasVFP3())
4189 return false;
4190 if (VT == MVT::f32)
4191 return ARM::getVFPf32Imm(Imm) != -1;
4192 if (VT == MVT::f64)
4193 return ARM::getVFPf64Imm(Imm) != -1;
4194 return false;
4195}