blob: 1b24a58840a308d86e8f190de6f37d1b644c6152 [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "ARM.h"
16#include "ARMAddressingModes.h"
17#include "ARMConstantPoolValue.h"
18#include "ARMISelLowering.h"
19#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000020#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000021#include "ARMRegisterInfo.h"
22#include "ARMSubtarget.h"
23#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000024#include "ARMTargetObjectFile.h"
Evan Chenga8e29892007-01-19 07:51:42 +000025#include "llvm/CallingConv.h"
26#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000027#include "llvm/Function.h"
Evan Cheng27707472007-03-16 08:43:56 +000028#include "llvm/Instruction.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000029#include "llvm/Intrinsics.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000030#include "llvm/GlobalValue.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000031#include "llvm/CodeGen/CallingConvLower.h"
Evan Chenga8e29892007-01-19 07:51:42 +000032#include "llvm/CodeGen/MachineBasicBlock.h"
33#include "llvm/CodeGen/MachineFrameInfo.h"
34#include "llvm/CodeGen/MachineFunction.h"
35#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000036#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000037#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000038#include "llvm/CodeGen/SelectionDAG.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000039#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000040#include "llvm/ADT/VectorExtras.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000041#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000042#include "llvm/Support/MathExtras.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000043#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000044using namespace llvm;
45
Owen Andersone50ed302009-08-10 22:56:29 +000046static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000047 CCValAssign::LocInfo &LocInfo,
48 ISD::ArgFlagsTy &ArgFlags,
49 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000050static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000051 CCValAssign::LocInfo &LocInfo,
52 ISD::ArgFlagsTy &ArgFlags,
53 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000054static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000055 CCValAssign::LocInfo &LocInfo,
56 ISD::ArgFlagsTy &ArgFlags,
57 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000058static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000059 CCValAssign::LocInfo &LocInfo,
60 ISD::ArgFlagsTy &ArgFlags,
61 CCState &State);
62
Owen Andersone50ed302009-08-10 22:56:29 +000063void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
64 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000065 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000066 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000067 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
68 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000069
Owen Anderson70671842009-08-10 20:18:46 +000070 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000071 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000072 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000073 }
74
Owen Andersone50ed302009-08-10 22:56:29 +000075 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +000076 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +000077 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +000078 if (ElemTy == MVT::i8 || ElemTy == MVT::i16)
Owen Anderson70671842009-08-10 20:18:46 +000079 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
80 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
81 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Owen Anderson70671842009-08-10 20:18:46 +000082 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Custom);
Anton Korobeynikov8e6c2b92009-08-21 12:40:35 +000083 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +000084 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +000085 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
86 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
87 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +000088 }
89
90 // Promote all bit-wise operations.
91 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +000092 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000093 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
94 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +000095 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000096 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000097 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +000098 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000099 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000100 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000101 }
Bob Wilson16330762009-09-16 00:17:28 +0000102
103 // Neon does not support vector divide/remainder operations.
104 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
105 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
106 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
107 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
108 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
109 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000110}
111
Owen Andersone50ed302009-08-10 22:56:29 +0000112void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000113 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000114 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000115}
116
Owen Andersone50ed302009-08-10 22:56:29 +0000117void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000118 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000119 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000120}
121
Chris Lattnerf0144122009-07-28 03:13:23 +0000122static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
123 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000124 return new TargetLoweringObjectFileMachO();
Chris Lattner80ec2792009-08-02 00:34:36 +0000125 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000126}
127
Evan Chenga8e29892007-01-19 07:51:42 +0000128ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000129 : TargetLowering(TM, createTLOF(TM)), ARMPCLabelIndex(0) {
Evan Chenga8e29892007-01-19 07:51:42 +0000130 Subtarget = &TM.getSubtarget<ARMSubtarget>();
131
Evan Chengb1df8f22007-04-27 08:15:43 +0000132 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000133 // Uses VFP for Thumb libfuncs if available.
134 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
135 // Single-precision floating-point arithmetic.
136 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
137 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
138 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
139 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000140
Evan Chengb1df8f22007-04-27 08:15:43 +0000141 // Double-precision floating-point arithmetic.
142 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
143 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
144 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
145 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000146
Evan Chengb1df8f22007-04-27 08:15:43 +0000147 // Single-precision comparisons.
148 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
149 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
150 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
151 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
152 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
153 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
154 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
155 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000156
Evan Chengb1df8f22007-04-27 08:15:43 +0000157 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
158 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
159 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
160 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
161 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
162 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
163 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
164 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000165
Evan Chengb1df8f22007-04-27 08:15:43 +0000166 // Double-precision comparisons.
167 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
168 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
169 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
170 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
171 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
172 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
173 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
174 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000175
Evan Chengb1df8f22007-04-27 08:15:43 +0000176 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
177 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
178 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
179 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
180 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
181 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
182 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
183 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000184
Evan Chengb1df8f22007-04-27 08:15:43 +0000185 // Floating-point to integer conversions.
186 // i64 conversions are done via library routines even when generating VFP
187 // instructions, so use the same ones.
188 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
189 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
190 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
191 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000192
Evan Chengb1df8f22007-04-27 08:15:43 +0000193 // Conversions between floating types.
194 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
195 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
196
197 // Integer to floating-point conversions.
198 // i64 conversions are done via library routines even when generating VFP
199 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000200 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
201 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000202 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
203 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
204 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
205 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
206 }
Evan Chenga8e29892007-01-19 07:51:42 +0000207 }
208
Bob Wilson2f954612009-05-22 17:38:41 +0000209 // These libcalls are not available in 32-bit.
210 setLibcallName(RTLIB::SHL_I128, 0);
211 setLibcallName(RTLIB::SRL_I128, 0);
212 setLibcallName(RTLIB::SRA_I128, 0);
213
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000214 // Libcalls should use the AAPCS base standard ABI, even if hard float
215 // is in effect, as per the ARM RTABI specification, section 4.1.2.
216 if (Subtarget->isAAPCS_ABI()) {
217 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
218 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
219 CallingConv::ARM_AAPCS);
220 }
221 }
222
David Goodwinf1daf7d2009-07-08 23:10:31 +0000223 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000224 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000225 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000226 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000227 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000228 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
229 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000230
Owen Anderson825b72b2009-08-11 20:47:22 +0000231 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000232 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000233
234 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 addDRTypeForNEON(MVT::v2f32);
236 addDRTypeForNEON(MVT::v8i8);
237 addDRTypeForNEON(MVT::v4i16);
238 addDRTypeForNEON(MVT::v2i32);
239 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000240
Owen Anderson825b72b2009-08-11 20:47:22 +0000241 addQRTypeForNEON(MVT::v4f32);
242 addQRTypeForNEON(MVT::v2f64);
243 addQRTypeForNEON(MVT::v16i8);
244 addQRTypeForNEON(MVT::v8i16);
245 addQRTypeForNEON(MVT::v4i32);
246 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000247
Bob Wilson74dc72e2009-09-15 23:55:57 +0000248 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
249 // neither Neon nor VFP support any arithmetic operations on it.
250 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
251 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
252 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
253 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
254 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
255 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
256 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
257 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
258 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
259 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
260 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
261 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
262 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
263 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
264 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
265 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
266 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
267 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
268 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
269 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
270 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
271 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
272 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
273 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
274
Bob Wilson5bafff32009-06-22 23:27:02 +0000275 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
276 setTargetDAGCombine(ISD::SHL);
277 setTargetDAGCombine(ISD::SRL);
278 setTargetDAGCombine(ISD::SRA);
279 setTargetDAGCombine(ISD::SIGN_EXTEND);
280 setTargetDAGCombine(ISD::ZERO_EXTEND);
281 setTargetDAGCombine(ISD::ANY_EXTEND);
282 }
283
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000284 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000285
286 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000287 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000288
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000289 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000290 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000291
Evan Chenga8e29892007-01-19 07:51:42 +0000292 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000293 if (!Subtarget->isThumb1Only()) {
294 for (unsigned im = (unsigned)ISD::PRE_INC;
295 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 setIndexedLoadAction(im, MVT::i1, Legal);
297 setIndexedLoadAction(im, MVT::i8, Legal);
298 setIndexedLoadAction(im, MVT::i16, Legal);
299 setIndexedLoadAction(im, MVT::i32, Legal);
300 setIndexedStoreAction(im, MVT::i1, Legal);
301 setIndexedStoreAction(im, MVT::i8, Legal);
302 setIndexedStoreAction(im, MVT::i16, Legal);
303 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000304 }
Evan Chenga8e29892007-01-19 07:51:42 +0000305 }
306
307 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000308 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setOperationAction(ISD::MUL, MVT::i64, Expand);
310 setOperationAction(ISD::MULHU, MVT::i32, Expand);
311 setOperationAction(ISD::MULHS, MVT::i32, Expand);
312 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
313 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000314 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000315 setOperationAction(ISD::MUL, MVT::i64, Expand);
316 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000317 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000319 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000320 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
321 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
322 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
323 setOperationAction(ISD::SRL, MVT::i64, Custom);
324 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000325
326 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000327 setOperationAction(ISD::ROTL, MVT::i32, Expand);
328 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
329 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000330 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000331 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000332
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000333 // Only ARMv6 has BSWAP.
334 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000335 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000336
Evan Chenga8e29892007-01-19 07:51:42 +0000337 // These are expanded into libcalls.
Owen Anderson825b72b2009-08-11 20:47:22 +0000338 setOperationAction(ISD::SDIV, MVT::i32, Expand);
339 setOperationAction(ISD::UDIV, MVT::i32, Expand);
340 setOperationAction(ISD::SREM, MVT::i32, Expand);
341 setOperationAction(ISD::UREM, MVT::i32, Expand);
342 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
343 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000344
Evan Chenga8e29892007-01-19 07:51:42 +0000345 // Support label based line numbers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000346 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
347 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000348
Owen Anderson825b72b2009-08-11 20:47:22 +0000349 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
350 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
351 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
352 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000353
Evan Chenga8e29892007-01-19 07:51:42 +0000354 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 setOperationAction(ISD::VASTART, MVT::Other, Custom);
356 setOperationAction(ISD::VAARG, MVT::Other, Expand);
357 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
358 setOperationAction(ISD::VAEND, MVT::Other, Expand);
359 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
360 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000361 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
362 // FIXME: Shouldn't need this, since no register is used, but the legalizer
363 // doesn't yet know how to not do that for SjLj.
364 setExceptionSelectorRegister(ARM::R0);
Evan Cheng86198642009-08-07 00:34:42 +0000365 if (Subtarget->isThumb())
Owen Anderson825b72b2009-08-11 20:47:22 +0000366 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Evan Cheng86198642009-08-07 00:34:42 +0000367 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000368 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
369 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000370
Evan Chengd27c9fc2009-07-03 01:43:10 +0000371 if (!Subtarget->hasV6Ops() && !Subtarget->isThumb2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
373 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000374 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000375 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000376
David Goodwinf1daf7d2009-07-08 23:10:31 +0000377 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
Evan Chengc7c77292008-11-04 19:57:48 +0000378 // Turn f64->i64 into FMRRD, i64 -> f64 to FMDRR iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000379 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000380
381 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
383 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
384 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000385
Owen Anderson825b72b2009-08-11 20:47:22 +0000386 setOperationAction(ISD::SETCC, MVT::i32, Expand);
387 setOperationAction(ISD::SETCC, MVT::f32, Expand);
388 setOperationAction(ISD::SETCC, MVT::f64, Expand);
389 setOperationAction(ISD::SELECT, MVT::i32, Expand);
390 setOperationAction(ISD::SELECT, MVT::f32, Expand);
391 setOperationAction(ISD::SELECT, MVT::f64, Expand);
392 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
393 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
394 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000395
Owen Anderson825b72b2009-08-11 20:47:22 +0000396 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
397 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
398 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
399 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
400 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000401
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000402 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000403 setOperationAction(ISD::FSIN, MVT::f64, Expand);
404 setOperationAction(ISD::FSIN, MVT::f32, Expand);
405 setOperationAction(ISD::FCOS, MVT::f32, Expand);
406 setOperationAction(ISD::FCOS, MVT::f64, Expand);
407 setOperationAction(ISD::FREM, MVT::f64, Expand);
408 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000409 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000410 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
411 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000412 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000413 setOperationAction(ISD::FPOW, MVT::f64, Expand);
414 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000415
Evan Chenga8e29892007-01-19 07:51:42 +0000416 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
David Goodwinf1daf7d2009-07-08 23:10:31 +0000417 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000418 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
419 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
420 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
421 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000422 }
Evan Chenga8e29892007-01-19 07:51:42 +0000423
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000424 // We have target-specific dag combine patterns for the following nodes:
425 // ARMISD::FMRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000426 setTargetDAGCombine(ISD::ADD);
427 setTargetDAGCombine(ISD::SUB);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000428
Evan Chenga8e29892007-01-19 07:51:42 +0000429 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Chenga8e29892007-01-19 07:51:42 +0000430 setSchedulingPreference(SchedulingForRegPressure);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000431
Evan Chengbc9b7542009-08-15 07:59:10 +0000432 // FIXME: If-converter should use instruction latency to determine
433 // profitability rather than relying on fixed limits.
434 if (Subtarget->getCPUString() == "generic") {
435 // Generic (and overly aggressive) if-conversion limits.
436 setIfCvtBlockSizeLimit(10);
437 setIfCvtDupBlockSizeLimit(2);
438 } else if (Subtarget->hasV6Ops()) {
439 setIfCvtBlockSizeLimit(2);
440 setIfCvtDupBlockSizeLimit(1);
441 } else {
442 setIfCvtBlockSizeLimit(3);
443 setIfCvtDupBlockSizeLimit(2);
Evan Cheng8557c2b2009-06-19 01:51:50 +0000444 }
445
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000446 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Bob Wilsone6abdff2009-05-18 20:55:32 +0000447 // Do not enable CodePlacementOpt for now: it currently runs after the
448 // ARMConstantIslandPass and messes up branch relaxation and placement
449 // of constant islands.
450 // benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000451}
452
Evan Chenga8e29892007-01-19 07:51:42 +0000453const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
454 switch (Opcode) {
455 default: return 0;
456 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000457 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
458 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000459 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000460 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
461 case ARMISD::tCALL: return "ARMISD::tCALL";
462 case ARMISD::BRCOND: return "ARMISD::BRCOND";
463 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000464 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000465 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
466 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
467 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000468 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000469 case ARMISD::CMPFP: return "ARMISD::CMPFP";
470 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
471 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
472 case ARMISD::CMOV: return "ARMISD::CMOV";
473 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000474
Evan Chenga8e29892007-01-19 07:51:42 +0000475 case ARMISD::FTOSI: return "ARMISD::FTOSI";
476 case ARMISD::FTOUI: return "ARMISD::FTOUI";
477 case ARMISD::SITOF: return "ARMISD::SITOF";
478 case ARMISD::UITOF: return "ARMISD::UITOF";
Evan Chenga8e29892007-01-19 07:51:42 +0000479
480 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
481 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
482 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000483
Evan Chenga8e29892007-01-19 07:51:42 +0000484 case ARMISD::FMRRD: return "ARMISD::FMRRD";
485 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000486
487 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000488
Evan Cheng86198642009-08-07 00:34:42 +0000489 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
490
Bob Wilson5bafff32009-06-22 23:27:02 +0000491 case ARMISD::VCEQ: return "ARMISD::VCEQ";
492 case ARMISD::VCGE: return "ARMISD::VCGE";
493 case ARMISD::VCGEU: return "ARMISD::VCGEU";
494 case ARMISD::VCGT: return "ARMISD::VCGT";
495 case ARMISD::VCGTU: return "ARMISD::VCGTU";
496 case ARMISD::VTST: return "ARMISD::VTST";
497
498 case ARMISD::VSHL: return "ARMISD::VSHL";
499 case ARMISD::VSHRs: return "ARMISD::VSHRs";
500 case ARMISD::VSHRu: return "ARMISD::VSHRu";
501 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
502 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
503 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
504 case ARMISD::VSHRN: return "ARMISD::VSHRN";
505 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
506 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
507 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
508 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
509 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
510 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
511 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
512 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
513 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
514 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
515 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
516 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
517 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
518 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000519 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000520 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000521 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000522 case ARMISD::VREV64: return "ARMISD::VREV64";
523 case ARMISD::VREV32: return "ARMISD::VREV32";
524 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000525 case ARMISD::VZIP: return "ARMISD::VZIP";
526 case ARMISD::VUZP: return "ARMISD::VUZP";
527 case ARMISD::VTRN: return "ARMISD::VTRN";
Evan Chenga8e29892007-01-19 07:51:42 +0000528 }
529}
530
Bill Wendlingb4202b82009-07-01 18:50:55 +0000531/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000532unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
533 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 1 : 2;
534}
535
Evan Chenga8e29892007-01-19 07:51:42 +0000536//===----------------------------------------------------------------------===//
537// Lowering Code
538//===----------------------------------------------------------------------===//
539
Evan Chenga8e29892007-01-19 07:51:42 +0000540/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
541static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
542 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000543 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000544 case ISD::SETNE: return ARMCC::NE;
545 case ISD::SETEQ: return ARMCC::EQ;
546 case ISD::SETGT: return ARMCC::GT;
547 case ISD::SETGE: return ARMCC::GE;
548 case ISD::SETLT: return ARMCC::LT;
549 case ISD::SETLE: return ARMCC::LE;
550 case ISD::SETUGT: return ARMCC::HI;
551 case ISD::SETUGE: return ARMCC::HS;
552 case ISD::SETULT: return ARMCC::LO;
553 case ISD::SETULE: return ARMCC::LS;
554 }
555}
556
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000557/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
558static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000559 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000560 CondCode2 = ARMCC::AL;
561 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000562 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000563 case ISD::SETEQ:
564 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
565 case ISD::SETGT:
566 case ISD::SETOGT: CondCode = ARMCC::GT; break;
567 case ISD::SETGE:
568 case ISD::SETOGE: CondCode = ARMCC::GE; break;
569 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000570 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000571 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
572 case ISD::SETO: CondCode = ARMCC::VC; break;
573 case ISD::SETUO: CondCode = ARMCC::VS; break;
574 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
575 case ISD::SETUGT: CondCode = ARMCC::HI; break;
576 case ISD::SETUGE: CondCode = ARMCC::PL; break;
577 case ISD::SETLT:
578 case ISD::SETULT: CondCode = ARMCC::LT; break;
579 case ISD::SETLE:
580 case ISD::SETULE: CondCode = ARMCC::LE; break;
581 case ISD::SETNE:
582 case ISD::SETUNE: CondCode = ARMCC::NE; break;
583 }
Evan Chenga8e29892007-01-19 07:51:42 +0000584}
585
Bob Wilson1f595bb2009-04-17 19:07:39 +0000586//===----------------------------------------------------------------------===//
587// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000588//===----------------------------------------------------------------------===//
589
590#include "ARMGenCallingConv.inc"
591
592// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000593static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000594 CCValAssign::LocInfo &LocInfo,
595 CCState &State, bool CanFail) {
596 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
597
598 // Try to get the first register.
599 if (unsigned Reg = State.AllocateReg(RegList, 4))
600 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
601 else {
602 // For the 2nd half of a v2f64, do not fail.
603 if (CanFail)
604 return false;
605
606 // Put the whole thing on the stack.
607 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
608 State.AllocateStack(8, 4),
609 LocVT, LocInfo));
610 return true;
611 }
612
613 // Try to get the second register.
614 if (unsigned Reg = State.AllocateReg(RegList, 4))
615 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
616 else
617 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
618 State.AllocateStack(4, 4),
619 LocVT, LocInfo));
620 return true;
621}
622
Owen Andersone50ed302009-08-10 22:56:29 +0000623static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000624 CCValAssign::LocInfo &LocInfo,
625 ISD::ArgFlagsTy &ArgFlags,
626 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000627 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
628 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000629 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000630 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
631 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000632 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000633}
634
635// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000636static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000637 CCValAssign::LocInfo &LocInfo,
638 CCState &State, bool CanFail) {
639 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
640 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
641
642 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
643 if (Reg == 0) {
644 // For the 2nd half of a v2f64, do not just fail.
645 if (CanFail)
646 return false;
647
648 // Put the whole thing on the stack.
649 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
650 State.AllocateStack(8, 8),
651 LocVT, LocInfo));
652 return true;
653 }
654
655 unsigned i;
656 for (i = 0; i < 2; ++i)
657 if (HiRegList[i] == Reg)
658 break;
659
660 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
661 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
662 LocVT, LocInfo));
663 return true;
664}
665
Owen Andersone50ed302009-08-10 22:56:29 +0000666static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000667 CCValAssign::LocInfo &LocInfo,
668 ISD::ArgFlagsTy &ArgFlags,
669 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000670 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
671 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000673 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
674 return false;
675 return true; // we handled it
676}
677
Owen Andersone50ed302009-08-10 22:56:29 +0000678static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000679 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000680 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
681 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
682
Bob Wilsone65586b2009-04-17 20:40:45 +0000683 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
684 if (Reg == 0)
685 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000686
Bob Wilsone65586b2009-04-17 20:40:45 +0000687 unsigned i;
688 for (i = 0; i < 2; ++i)
689 if (HiRegList[i] == Reg)
690 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000691
Bob Wilson5bafff32009-06-22 23:27:02 +0000692 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000693 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000694 LocVT, LocInfo));
695 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000696}
697
Owen Andersone50ed302009-08-10 22:56:29 +0000698static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000699 CCValAssign::LocInfo &LocInfo,
700 ISD::ArgFlagsTy &ArgFlags,
701 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000702 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
703 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000704 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000705 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000706 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000707}
708
Owen Andersone50ed302009-08-10 22:56:29 +0000709static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000710 CCValAssign::LocInfo &LocInfo,
711 ISD::ArgFlagsTy &ArgFlags,
712 CCState &State) {
713 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
714 State);
715}
716
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000717/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
718/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000719CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000720 bool Return,
721 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000722 switch (CC) {
723 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000724 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000725 case CallingConv::C:
726 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000727 // Use target triple & subtarget features to do actual dispatch.
728 if (Subtarget->isAAPCS_ABI()) {
729 if (Subtarget->hasVFP2() &&
730 FloatABIType == FloatABI::Hard && !isVarArg)
731 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
732 else
733 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
734 } else
735 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000736 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000737 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000738 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000739 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000740 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000741 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000742 }
743}
744
Dan Gohman98ca4f22009-08-05 01:29:28 +0000745/// LowerCallResult - Lower the result values of a call into the
746/// appropriate copies out of appropriate physical registers.
747SDValue
748ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000749 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000750 const SmallVectorImpl<ISD::InputArg> &Ins,
751 DebugLoc dl, SelectionDAG &DAG,
752 SmallVectorImpl<SDValue> &InVals) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000753
Bob Wilson1f595bb2009-04-17 19:07:39 +0000754 // Assign locations to each value returned by this call.
755 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000756 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000757 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000758 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000759 CCAssignFnForNode(CallConv, /* Return*/ true,
760 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000761
762 // Copy all of the result registers out of their specified physreg.
763 for (unsigned i = 0; i != RVLocs.size(); ++i) {
764 CCValAssign VA = RVLocs[i];
765
Bob Wilson80915242009-04-25 00:33:20 +0000766 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000767 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000768 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000770 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000771 Chain = Lo.getValue(1);
772 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000773 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000774 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000775 InFlag);
776 Chain = Hi.getValue(1);
777 InFlag = Hi.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +0000778 Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000779
Owen Anderson825b72b2009-08-11 20:47:22 +0000780 if (VA.getLocVT() == MVT::v2f64) {
781 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
782 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
783 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000784
785 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000786 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000787 Chain = Lo.getValue(1);
788 InFlag = Lo.getValue(2);
789 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000790 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000791 Chain = Hi.getValue(1);
792 InFlag = Hi.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +0000793 Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
794 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
795 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000796 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000797 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000798 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
799 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000800 Chain = Val.getValue(1);
801 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000802 }
Bob Wilson80915242009-04-25 00:33:20 +0000803
804 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000805 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000806 case CCValAssign::Full: break;
807 case CCValAssign::BCvt:
808 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
809 break;
810 }
811
Dan Gohman98ca4f22009-08-05 01:29:28 +0000812 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000813 }
814
Dan Gohman98ca4f22009-08-05 01:29:28 +0000815 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000816}
817
818/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
819/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000820/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000821/// a byval function parameter.
822/// Sometimes what we are copying is the end of a larger object, the part that
823/// does not fit in registers.
824static SDValue
825CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
826 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
827 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000828 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000829 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
830 /*AlwaysInline=*/false, NULL, 0, NULL, 0);
831}
832
Bob Wilsondee46d72009-04-17 20:35:10 +0000833/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000834SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000835ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
836 SDValue StackPtr, SDValue Arg,
837 DebugLoc dl, SelectionDAG &DAG,
838 const CCValAssign &VA,
839 ISD::ArgFlagsTy Flags) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000840 unsigned LocMemOffset = VA.getLocMemOffset();
841 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
842 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
843 if (Flags.isByVal()) {
844 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
845 }
846 return DAG.getStore(Chain, dl, Arg, PtrOff,
847 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chenga8e29892007-01-19 07:51:42 +0000848}
849
Dan Gohman98ca4f22009-08-05 01:29:28 +0000850void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000851 SDValue Chain, SDValue &Arg,
852 RegsToPassVector &RegsToPass,
853 CCValAssign &VA, CCValAssign &NextVA,
854 SDValue &StackPtr,
855 SmallVector<SDValue, 8> &MemOpChains,
856 ISD::ArgFlagsTy Flags) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000857
858 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000859 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000860 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
861
862 if (NextVA.isRegLoc())
863 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
864 else {
865 assert(NextVA.isMemLoc());
866 if (StackPtr.getNode() == 0)
867 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
868
Dan Gohman98ca4f22009-08-05 01:29:28 +0000869 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
870 dl, DAG, NextVA,
871 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000872 }
873}
874
Dan Gohman98ca4f22009-08-05 01:29:28 +0000875/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000876/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
877/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000878SDValue
879ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000880 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000881 bool isTailCall,
882 const SmallVectorImpl<ISD::OutputArg> &Outs,
883 const SmallVectorImpl<ISD::InputArg> &Ins,
884 DebugLoc dl, SelectionDAG &DAG,
885 SmallVectorImpl<SDValue> &InVals) {
Evan Chenga8e29892007-01-19 07:51:42 +0000886
Bob Wilson1f595bb2009-04-17 19:07:39 +0000887 // Analyze operands of the call, assigning locations to each operand.
888 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000889 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
890 *DAG.getContext());
891 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000892 CCAssignFnForNode(CallConv, /* Return*/ false,
893 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +0000894
Bob Wilson1f595bb2009-04-17 19:07:39 +0000895 // Get a count of how many bytes are to be pushed on the stack.
896 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000897
898 // Adjust the stack pointer for the new arguments...
899 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000900 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000901
Owen Anderson825b72b2009-08-11 20:47:22 +0000902 SDValue StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000903
Bob Wilson5bafff32009-06-22 23:27:02 +0000904 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000905 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000906
Bob Wilson1f595bb2009-04-17 19:07:39 +0000907 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +0000908 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000909 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
910 i != e;
911 ++i, ++realArgIdx) {
912 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000913 SDValue Arg = Outs[realArgIdx].Val;
914 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +0000915
Bob Wilson1f595bb2009-04-17 19:07:39 +0000916 // Promote the value if needed.
917 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000918 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +0000919 case CCValAssign::Full: break;
920 case CCValAssign::SExt:
921 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
922 break;
923 case CCValAssign::ZExt:
924 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
925 break;
926 case CCValAssign::AExt:
927 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
928 break;
929 case CCValAssign::BCvt:
930 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
931 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000932 }
933
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000934 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +0000935 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000936 if (VA.getLocVT() == MVT::v2f64) {
937 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
938 DAG.getConstant(0, MVT::i32));
939 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
940 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000941
Dan Gohman98ca4f22009-08-05 01:29:28 +0000942 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000943 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
944
945 VA = ArgLocs[++i]; // skip ahead to next loc
946 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000947 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000948 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
949 } else {
950 assert(VA.isMemLoc());
951 if (StackPtr.getNode() == 0)
952 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
953
Dan Gohman98ca4f22009-08-05 01:29:28 +0000954 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
955 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000956 }
957 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000958 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000959 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000960 }
961 } else if (VA.isRegLoc()) {
962 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
963 } else {
964 assert(VA.isMemLoc());
965 if (StackPtr.getNode() == 0)
966 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
967
Dan Gohman98ca4f22009-08-05 01:29:28 +0000968 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
969 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000970 }
Evan Chenga8e29892007-01-19 07:51:42 +0000971 }
972
973 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000974 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +0000975 &MemOpChains[0], MemOpChains.size());
976
977 // Build a sequence of copy-to-reg nodes chained together with token chain
978 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +0000979 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +0000980 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +0000981 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000982 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000983 InFlag = Chain.getValue(1);
984 }
985
Bill Wendling056292f2008-09-16 21:48:12 +0000986 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
987 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
988 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +0000989 bool isDirect = false;
990 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +0000991 bool isLocalARMFunc = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000992 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
993 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +0000994 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +0000995 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +0000996 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +0000997 getTargetMachine().getRelocationModel() != Reloc::Static;
998 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +0000999 // ARM call to a local ARM function is predicable.
1000 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +00001001 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001002 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge4e4ed32009-08-28 23:18:09 +00001003 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001004 ARMPCLabelIndex,
1005 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001006 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001007 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001008 Callee = DAG.getLoad(getPointerTy(), dl,
1009 DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001010 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001011 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001012 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001013 } else
1014 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001015 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001016 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001017 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001018 getTargetMachine().getRelocationModel() != Reloc::Static;
1019 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001020 // tBX takes a register source operand.
1021 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001022 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Owen Anderson1d0be152009-08-13 21:58:54 +00001023 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001024 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001025 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001026 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001027 Callee = DAG.getLoad(getPointerTy(), dl,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001028 DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001029 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001030 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001031 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001032 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001033 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001034 }
1035
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001036 // FIXME: handle tail calls differently.
1037 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001038 if (Subtarget->isThumb()) {
1039 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001040 CallOpc = ARMISD::CALL_NOLINK;
1041 else
1042 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1043 } else {
1044 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001045 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1046 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001047 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001048 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001049 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001050 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001051 InFlag = Chain.getValue(1);
1052 }
1053
Dan Gohman475871a2008-07-27 21:46:04 +00001054 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001055 Ops.push_back(Chain);
1056 Ops.push_back(Callee);
1057
1058 // Add argument registers to the end of the list so that they are known live
1059 // into the call.
1060 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1061 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1062 RegsToPass[i].second.getValueType()));
1063
Gabor Greifba36cb52008-08-28 21:40:38 +00001064 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001065 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +00001066 // Returns a chain and a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00001067 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +00001068 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001069 InFlag = Chain.getValue(1);
1070
Chris Lattnere563bbc2008-10-11 22:08:30 +00001071 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1072 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001073 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001074 InFlag = Chain.getValue(1);
1075
Bob Wilson1f595bb2009-04-17 19:07:39 +00001076 // Handle result values, copying them out of physregs into vregs that we
1077 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001078 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1079 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001080}
1081
Dan Gohman98ca4f22009-08-05 01:29:28 +00001082SDValue
1083ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001084 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001085 const SmallVectorImpl<ISD::OutputArg> &Outs,
1086 DebugLoc dl, SelectionDAG &DAG) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001087
Bob Wilsondee46d72009-04-17 20:35:10 +00001088 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001089 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001090
Bob Wilsondee46d72009-04-17 20:35:10 +00001091 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001092 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1093 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001094
Dan Gohman98ca4f22009-08-05 01:29:28 +00001095 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001096 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1097 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001098
1099 // If this is the first return lowered for this function, add
1100 // the regs to the liveout set for the function.
1101 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1102 for (unsigned i = 0; i != RVLocs.size(); ++i)
1103 if (RVLocs[i].isRegLoc())
1104 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001105 }
1106
Bob Wilson1f595bb2009-04-17 19:07:39 +00001107 SDValue Flag;
1108
1109 // Copy the result values into the output registers.
1110 for (unsigned i = 0, realRVLocIdx = 0;
1111 i != RVLocs.size();
1112 ++i, ++realRVLocIdx) {
1113 CCValAssign &VA = RVLocs[i];
1114 assert(VA.isRegLoc() && "Can only return in registers!");
1115
Dan Gohman98ca4f22009-08-05 01:29:28 +00001116 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001117
1118 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001119 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001120 case CCValAssign::Full: break;
1121 case CCValAssign::BCvt:
1122 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1123 break;
1124 }
1125
Bob Wilson1f595bb2009-04-17 19:07:39 +00001126 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001127 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001128 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001129 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1130 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001131 SDValue HalfGPRs = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001132 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001133
1134 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1135 Flag = Chain.getValue(1);
1136 VA = RVLocs[++i]; // skip ahead to next loc
1137 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1138 HalfGPRs.getValue(1), Flag);
1139 Flag = Chain.getValue(1);
1140 VA = RVLocs[++i]; // skip ahead to next loc
1141
1142 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001143 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1144 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001145 }
1146 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1147 // available.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001148 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001149 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001150 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001151 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001152 VA = RVLocs[++i]; // skip ahead to next loc
1153 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1154 Flag);
1155 } else
1156 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1157
Bob Wilsondee46d72009-04-17 20:35:10 +00001158 // Guarantee that all emitted copies are
1159 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001160 Flag = Chain.getValue(1);
1161 }
1162
1163 SDValue result;
1164 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001165 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001166 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001167 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001168
1169 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001170}
1171
Bob Wilson2dc4f542009-03-20 22:42:55 +00001172// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Bob Wilsond2559bf2009-07-13 18:11:36 +00001173// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
Bill Wendling056292f2008-09-16 21:48:12 +00001174// one of the above mentioned nodes. It has to be wrapped because otherwise
1175// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1176// be used to form addressing mode. These wrapped nodes will be selected
1177// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001178static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001179 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001180 // FIXME there is no actual debug info here
1181 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001182 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001183 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001184 if (CP->isMachineConstantPoolEntry())
1185 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1186 CP->getAlignment());
1187 else
1188 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1189 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001190 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001191}
1192
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001193// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001194SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001195ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1196 SelectionDAG &DAG) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001197 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001198 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001199 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1200 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001201 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001202 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001203 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001204 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001205 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001206 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001207
Owen Anderson825b72b2009-08-11 20:47:22 +00001208 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001209 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001210
1211 // call __tls_get_addr.
1212 ArgListTy Args;
1213 ArgListEntry Entry;
1214 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001215 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001216 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001217 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001218 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001219 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1220 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001221 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001222 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001223 return CallResult.first;
1224}
1225
1226// Lower ISD::GlobalTLSAddress using the "initial exec" or
1227// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001228SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001229ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001230 SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001231 GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001232 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001233 SDValue Offset;
1234 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001235 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001236 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001237 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001238
Chris Lattner4fb63d02009-07-15 04:12:33 +00001239 if (GV->isDeclaration()) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001240 // initial exec model
1241 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1242 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001243 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001244 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001245 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001246 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001247 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001248 Chain = Offset.getValue(1);
1249
Owen Anderson825b72b2009-08-11 20:47:22 +00001250 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001251 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001252
Dale Johannesen33c960f2009-02-04 20:06:27 +00001253 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001254 } else {
1255 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001256 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001257 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001258 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001259 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001260 }
1261
1262 // The address of the thread local variable is the add of the thread
1263 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001264 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001265}
1266
Dan Gohman475871a2008-07-27 21:46:04 +00001267SDValue
1268ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001269 // TODO: implement the "local dynamic" model
1270 assert(Subtarget->isTargetELF() &&
1271 "TLS not implemented for non-ELF targets");
1272 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1273 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1274 // otherwise use the "Local Exec" TLS Model
1275 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1276 return LowerToTLSGeneralDynamicModel(GA, DAG);
1277 else
1278 return LowerToTLSExecModels(GA, DAG);
1279}
1280
Dan Gohman475871a2008-07-27 21:46:04 +00001281SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001282 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001283 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001284 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001285 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1286 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1287 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001288 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001289 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001290 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001291 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001292 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001293 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Dale Johannesen33c960f2009-02-04 20:06:27 +00001294 CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001295 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001296 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001297 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001298 if (!UseGOTOFF)
Dale Johannesen33c960f2009-02-04 20:06:27 +00001299 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001300 return Result;
1301 } else {
Evan Cheng1606e8e2009-03-13 07:51:59 +00001302 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001303 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001304 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001305 }
1306}
1307
Dan Gohman475871a2008-07-27 21:46:04 +00001308SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001309 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001310 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001311 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001312 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1313 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001314 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001315 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001316 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001317 else {
Evan Chenge4e4ed32009-08-28 23:18:09 +00001318 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1319 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001320 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001321 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001322 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001323 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001324
Dale Johannesen33c960f2009-02-04 20:06:27 +00001325 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001326 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001327
1328 if (RelocM == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001329 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001330 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001331 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001332
Evan Cheng63476a82009-09-03 07:04:02 +00001333 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Dale Johannesen33c960f2009-02-04 20:06:27 +00001334 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001335
1336 return Result;
1337}
1338
Dan Gohman475871a2008-07-27 21:46:04 +00001339SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001340 SelectionDAG &DAG){
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001341 assert(Subtarget->isTargetELF() &&
1342 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Owen Andersone50ed302009-08-10 22:56:29 +00001343 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001344 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001345 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001346 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1347 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001348 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001349 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001350 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001351 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001352 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001353 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001354}
1355
Bob Wilsona599bff2009-08-04 00:36:16 +00001356static SDValue LowerNeonVLDIntrinsic(SDValue Op, SelectionDAG &DAG,
Bob Wilson31fb12f2009-08-26 17:39:53 +00001357 unsigned NumVecs) {
Bob Wilsona599bff2009-08-04 00:36:16 +00001358 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001359 EVT VT = Node->getValueType(0);
Bob Wilsona599bff2009-08-04 00:36:16 +00001360
Bob Wilson31fb12f2009-08-26 17:39:53 +00001361 // No expansion needed for 64-bit vectors.
1362 if (VT.is64BitVector())
1363 return SDValue();
Bob Wilsona599bff2009-08-04 00:36:16 +00001364
Bob Wilson31fb12f2009-08-26 17:39:53 +00001365 // FIXME: We need to expand VLD3 and VLD4 of 128-bit vectors into separate
1366 // operations to load the even and odd registers.
1367 return SDValue();
Bob Wilsona599bff2009-08-04 00:36:16 +00001368}
1369
Bob Wilsonb36ec862009-08-06 18:47:44 +00001370static SDValue LowerNeonVSTIntrinsic(SDValue Op, SelectionDAG &DAG,
Bob Wilson31fb12f2009-08-26 17:39:53 +00001371 unsigned NumVecs) {
Bob Wilsonb36ec862009-08-06 18:47:44 +00001372 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001373 EVT VT = Node->getOperand(3).getValueType();
Bob Wilsonb36ec862009-08-06 18:47:44 +00001374
Bob Wilson31fb12f2009-08-26 17:39:53 +00001375 // No expansion needed for 64-bit vectors.
1376 if (VT.is64BitVector())
1377 return SDValue();
Bob Wilsonb36ec862009-08-06 18:47:44 +00001378
Bob Wilson31fb12f2009-08-26 17:39:53 +00001379 // FIXME: We need to expand VST3 and VST4 of 128-bit vectors into separate
1380 // operations to store the even and odd registers.
1381 return SDValue();
Bob Wilsonb36ec862009-08-06 18:47:44 +00001382}
1383
Bob Wilson243fcc52009-09-01 04:26:28 +00001384static SDValue LowerNeonVLDLaneIntrinsic(SDValue Op, SelectionDAG &DAG,
1385 unsigned NumVecs) {
1386 SDNode *Node = Op.getNode();
1387 EVT VT = Node->getValueType(0);
1388
1389 if (!VT.is64BitVector())
1390 return SDValue(); // unimplemented
1391
1392 // Change the lane number operand to be a TargetConstant; otherwise it
1393 // will be legalized into a register.
1394 ConstantSDNode *Lane = dyn_cast<ConstantSDNode>(Node->getOperand(NumVecs+3));
1395 if (!Lane) {
1396 assert(false && "vld lane number must be a constant");
1397 return SDValue();
1398 }
1399 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
1400 Ops[NumVecs+3] = DAG.getTargetConstant(Lane->getZExtValue(), MVT::i32);
1401 return DAG.UpdateNodeOperands(Op, &Ops[0], Ops.size());
1402}
1403
Bob Wilson8a3198b2009-09-01 18:51:56 +00001404static SDValue LowerNeonVSTLaneIntrinsic(SDValue Op, SelectionDAG &DAG,
1405 unsigned NumVecs) {
1406 SDNode *Node = Op.getNode();
1407 EVT VT = Node->getOperand(3).getValueType();
1408
1409 if (!VT.is64BitVector())
1410 return SDValue(); // unimplemented
1411
1412 // Change the lane number operand to be a TargetConstant; otherwise it
1413 // will be legalized into a register.
1414 ConstantSDNode *Lane = dyn_cast<ConstantSDNode>(Node->getOperand(NumVecs+3));
1415 if (!Lane) {
1416 assert(false && "vst lane number must be a constant");
1417 return SDValue();
1418 }
1419 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
1420 Ops[NumVecs+3] = DAG.getTargetConstant(Lane->getZExtValue(), MVT::i32);
1421 return DAG.UpdateNodeOperands(Op, &Ops[0], Ops.size());
1422}
1423
Bob Wilsona599bff2009-08-04 00:36:16 +00001424SDValue
1425ARMTargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
1426 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1427 switch (IntNo) {
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001428 case Intrinsic::arm_neon_vld3:
Bob Wilson31fb12f2009-08-26 17:39:53 +00001429 return LowerNeonVLDIntrinsic(Op, DAG, 3);
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001430 case Intrinsic::arm_neon_vld4:
Bob Wilson31fb12f2009-08-26 17:39:53 +00001431 return LowerNeonVLDIntrinsic(Op, DAG, 4);
Bob Wilson243fcc52009-09-01 04:26:28 +00001432 case Intrinsic::arm_neon_vld2lane:
1433 return LowerNeonVLDLaneIntrinsic(Op, DAG, 2);
1434 case Intrinsic::arm_neon_vld3lane:
1435 return LowerNeonVLDLaneIntrinsic(Op, DAG, 3);
1436 case Intrinsic::arm_neon_vld4lane:
1437 return LowerNeonVLDLaneIntrinsic(Op, DAG, 4);
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001438 case Intrinsic::arm_neon_vst3:
Bob Wilson31fb12f2009-08-26 17:39:53 +00001439 return LowerNeonVSTIntrinsic(Op, DAG, 3);
Bob Wilsonb0abb4d2009-08-11 05:39:44 +00001440 case Intrinsic::arm_neon_vst4:
Bob Wilson31fb12f2009-08-26 17:39:53 +00001441 return LowerNeonVSTIntrinsic(Op, DAG, 4);
Bob Wilson8a3198b2009-09-01 18:51:56 +00001442 case Intrinsic::arm_neon_vst2lane:
1443 return LowerNeonVSTLaneIntrinsic(Op, DAG, 2);
1444 case Intrinsic::arm_neon_vst3lane:
1445 return LowerNeonVSTLaneIntrinsic(Op, DAG, 3);
1446 case Intrinsic::arm_neon_vst4lane:
1447 return LowerNeonVSTLaneIntrinsic(Op, DAG, 4);
Bob Wilsona599bff2009-08-04 00:36:16 +00001448 default: return SDValue(); // Don't custom lower most intrinsics.
1449 }
1450}
1451
Jim Grosbach0e0da732009-05-12 23:59:14 +00001452SDValue
1453ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001454 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001455 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001456 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001457 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001458 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001459 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001460 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1461 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001462 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001463 MachineFunction &MF = DAG.getMachineFunction();
1464 EVT PtrVT = getPointerTy();
1465 DebugLoc dl = Op.getDebugLoc();
1466 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1467 SDValue CPAddr;
1468 unsigned PCAdj = (RelocM != Reloc::PIC_)
1469 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001470 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001471 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1472 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001473 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001474 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001475 SDValue Result =
1476 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
1477 SDValue Chain = Result.getValue(1);
1478
1479 if (RelocM == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001480 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001481 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1482 }
1483 return Result;
1484 }
Jim Grosbachf9570122009-05-14 00:46:35 +00001485 case Intrinsic::eh_sjlj_setjmp:
Owen Anderson825b72b2009-08-11 20:47:22 +00001486 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1));
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001487 }
1488}
1489
Dan Gohman475871a2008-07-27 21:46:04 +00001490static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001491 unsigned VarArgsFrameIndex) {
Evan Chenga8e29892007-01-19 07:51:42 +00001492 // vastart just stores the address of the VarArgsFrameIndex slot into the
1493 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001494 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001495 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001496 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001497 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001498 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001499}
1500
Dan Gohman475871a2008-07-27 21:46:04 +00001501SDValue
Evan Cheng86198642009-08-07 00:34:42 +00001502ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
1503 SDNode *Node = Op.getNode();
1504 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001505 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001506 SDValue Chain = Op.getOperand(0);
1507 SDValue Size = Op.getOperand(1);
1508 SDValue Align = Op.getOperand(2);
1509
1510 // Chain the dynamic stack allocation so that it doesn't modify the stack
1511 // pointer when other instructions are using the stack.
1512 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1513
1514 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1515 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1516 if (AlignVal > StackAlign)
1517 // Do this now since selection pass cannot introduce new target
1518 // independent node.
1519 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1520
1521 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1522 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1523 // do even more horrible hack later.
1524 MachineFunction &MF = DAG.getMachineFunction();
1525 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1526 if (AFI->isThumb1OnlyFunction()) {
1527 bool Negate = true;
1528 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1529 if (C) {
1530 uint32_t Val = C->getZExtValue();
1531 if (Val <= 508 && ((Val & 3) == 0))
1532 Negate = false;
1533 }
1534 if (Negate)
1535 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1536 }
1537
Owen Anderson825b72b2009-08-11 20:47:22 +00001538 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001539 SDValue Ops1[] = { Chain, Size, Align };
1540 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1541 Chain = Res.getValue(1);
1542 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1543 DAG.getIntPtrConstant(0, true), SDValue());
1544 SDValue Ops2[] = { Res, Chain };
1545 return DAG.getMergeValues(Ops2, 2, dl);
1546}
1547
1548SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001549ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1550 SDValue &Root, SelectionDAG &DAG,
1551 DebugLoc dl) {
1552 MachineFunction &MF = DAG.getMachineFunction();
1553 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1554
1555 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001556 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001557 RC = ARM::tGPRRegisterClass;
1558 else
1559 RC = ARM::GPRRegisterClass;
1560
1561 // Transform the arguments stored in physical registers into virtual ones.
1562 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001563 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001564
1565 SDValue ArgValue2;
1566 if (NextVA.isMemLoc()) {
1567 unsigned ArgSize = NextVA.getLocVT().getSizeInBits()/8;
1568 MachineFrameInfo *MFI = MF.getFrameInfo();
1569 int FI = MFI->CreateFixedObject(ArgSize, NextVA.getLocMemOffset());
1570
1571 // Create load node to retrieve arguments from the stack.
1572 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00001573 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN, NULL, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001574 } else {
1575 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001576 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001577 }
1578
Owen Anderson825b72b2009-08-11 20:47:22 +00001579 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001580}
1581
1582SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001583ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001584 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001585 const SmallVectorImpl<ISD::InputArg>
1586 &Ins,
1587 DebugLoc dl, SelectionDAG &DAG,
1588 SmallVectorImpl<SDValue> &InVals) {
1589
Bob Wilson1f595bb2009-04-17 19:07:39 +00001590 MachineFunction &MF = DAG.getMachineFunction();
1591 MachineFrameInfo *MFI = MF.getFrameInfo();
1592
Bob Wilson1f595bb2009-04-17 19:07:39 +00001593 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1594
1595 // Assign locations to all of the incoming arguments.
1596 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001597 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1598 *DAG.getContext());
1599 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001600 CCAssignFnForNode(CallConv, /* Return*/ false,
1601 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001602
1603 SmallVector<SDValue, 16> ArgValues;
1604
1605 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1606 CCValAssign &VA = ArgLocs[i];
1607
Bob Wilsondee46d72009-04-17 20:35:10 +00001608 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001609 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001610 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00001611
Bob Wilson5bafff32009-06-22 23:27:02 +00001612 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001613 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001614 // f64 and vector types are split up into multiple registers or
1615 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00001616 RegVT = MVT::i32;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001617
Owen Anderson825b72b2009-08-11 20:47:22 +00001618 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001619 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001620 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00001621 VA = ArgLocs[++i]; // skip ahead to next loc
1622 SDValue ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001623 Chain, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001624 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1625 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001626 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00001627 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001628 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1629 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001630 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001631
Bob Wilson5bafff32009-06-22 23:27:02 +00001632 } else {
1633 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001634
Owen Anderson825b72b2009-08-11 20:47:22 +00001635 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00001636 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001637 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00001638 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001639 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001640 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001641 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001642 RC = (AFI->isThumb1OnlyFunction() ?
1643 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00001644 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001645 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00001646
1647 // Transform the arguments in physical registers into virtual ones.
1648 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001649 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001650 }
1651
1652 // If this is an 8 or 16-bit value, it is really passed promoted
1653 // to 32 bits. Insert an assert[sz]ext to capture this, then
1654 // truncate to the right size.
1655 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001656 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001657 case CCValAssign::Full: break;
1658 case CCValAssign::BCvt:
1659 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1660 break;
1661 case CCValAssign::SExt:
1662 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1663 DAG.getValueType(VA.getValVT()));
1664 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1665 break;
1666 case CCValAssign::ZExt:
1667 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1668 DAG.getValueType(VA.getValVT()));
1669 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1670 break;
1671 }
1672
Dan Gohman98ca4f22009-08-05 01:29:28 +00001673 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001674
1675 } else { // VA.isRegLoc()
1676
1677 // sanity check
1678 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00001679 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001680
1681 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1682 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset());
1683
Bob Wilsondee46d72009-04-17 20:35:10 +00001684 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001685 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001686 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, NULL, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001687 }
1688 }
1689
1690 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001691 if (isVarArg) {
1692 static const unsigned GPRArgRegs[] = {
1693 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1694 };
1695
Bob Wilsondee46d72009-04-17 20:35:10 +00001696 unsigned NumGPRs = CCInfo.getFirstUnallocated
1697 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001698
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001699 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1700 unsigned VARegSize = (4 - NumGPRs) * 4;
1701 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001702 unsigned ArgOffset = 0;
Evan Chenga8e29892007-01-19 07:51:42 +00001703 if (VARegSaveSize) {
1704 // If this function is vararg, store any remaining integer argument regs
1705 // to their spots on the stack so that they may be loaded by deferencing
1706 // the result of va_next.
1707 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001708 ArgOffset = CCInfo.getNextStackOffset();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001709 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1710 VARegSaveSize - VARegSize);
Dan Gohman475871a2008-07-27 21:46:04 +00001711 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001712
Dan Gohman475871a2008-07-27 21:46:04 +00001713 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001714 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001715 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001716 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001717 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001718 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001719 RC = ARM::GPRRegisterClass;
1720
Bob Wilson998e1252009-04-20 18:36:57 +00001721 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001722 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001723 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001724 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001725 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001726 DAG.getConstant(4, getPointerTy()));
1727 }
1728 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001729 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001730 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001731 } else
1732 // This will point to the next argument passed via stack.
1733 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1734 }
1735
Dan Gohman98ca4f22009-08-05 01:29:28 +00001736 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00001737}
1738
1739/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001740static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001741 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001742 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001743 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001744 // Maybe this has already been legalized into the constant pool?
1745 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001746 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001747 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1748 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001749 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001750 }
1751 }
1752 return false;
1753}
1754
David Goodwinf1daf7d2009-07-08 23:10:31 +00001755static bool isLegalCmpImmediate(unsigned C, bool isThumb1Only) {
1756 return ( isThumb1Only && (C & ~255U) == 0) ||
1757 (!isThumb1Only && ARM_AM::getSOImmVal(C) != -1);
Evan Chenga8e29892007-01-19 07:51:42 +00001758}
1759
1760/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1761/// the given operands.
Dan Gohman475871a2008-07-27 21:46:04 +00001762static SDValue getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
David Goodwinf1daf7d2009-07-08 23:10:31 +00001763 SDValue &ARMCC, SelectionDAG &DAG, bool isThumb1Only,
Dale Johannesende064702009-02-06 21:50:26 +00001764 DebugLoc dl) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001765 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001766 unsigned C = RHSC->getZExtValue();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001767 if (!isLegalCmpImmediate(C, isThumb1Only)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001768 // Constant does not fit, try adjusting it by one?
1769 switch (CC) {
1770 default: break;
1771 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001772 case ISD::SETGE:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001773 if (isLegalCmpImmediate(C-1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001774 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001775 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001776 }
1777 break;
1778 case ISD::SETULT:
1779 case ISD::SETUGE:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001780 if (C > 0 && isLegalCmpImmediate(C-1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001781 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001782 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001783 }
1784 break;
1785 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001786 case ISD::SETGT:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001787 if (isLegalCmpImmediate(C+1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001788 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001789 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001790 }
1791 break;
1792 case ISD::SETULE:
1793 case ISD::SETUGT:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001794 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001795 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001796 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001797 }
1798 break;
1799 }
1800 }
1801 }
1802
1803 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001804 ARMISD::NodeType CompareType;
1805 switch (CondCode) {
1806 default:
1807 CompareType = ARMISD::CMP;
1808 break;
1809 case ARMCC::EQ:
1810 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00001811 // Uses only Z Flag
1812 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001813 break;
1814 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001815 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1816 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001817}
1818
1819/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001820static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001821 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001822 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001823 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00001824 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001825 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001826 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1827 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001828}
1829
Dan Gohman475871a2008-07-27 21:46:04 +00001830static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001831 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00001832 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001833 SDValue LHS = Op.getOperand(0);
1834 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001835 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001836 SDValue TrueVal = Op.getOperand(2);
1837 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001838 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001839
Owen Anderson825b72b2009-08-11 20:47:22 +00001840 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001841 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001842 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
David Goodwinf1daf7d2009-07-08 23:10:31 +00001843 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
Dale Johannesende064702009-02-06 21:50:26 +00001844 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001845 }
1846
1847 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001848 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00001849
Owen Anderson825b72b2009-08-11 20:47:22 +00001850 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1851 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001852 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1853 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001854 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001855 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001856 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001857 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00001858 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001859 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00001860 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001861 }
1862 return Result;
1863}
1864
Dan Gohman475871a2008-07-27 21:46:04 +00001865static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001866 const ARMSubtarget *ST) {
Dan Gohman475871a2008-07-27 21:46:04 +00001867 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001868 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001869 SDValue LHS = Op.getOperand(2);
1870 SDValue RHS = Op.getOperand(3);
1871 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00001872 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001873
Owen Anderson825b72b2009-08-11 20:47:22 +00001874 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001875 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001876 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
David Goodwinf1daf7d2009-07-08 23:10:31 +00001877 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001878 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00001879 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001880 }
1881
Owen Anderson825b72b2009-08-11 20:47:22 +00001882 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00001883 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001884 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001885
Dale Johannesende064702009-02-06 21:50:26 +00001886 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001887 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1888 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1889 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001890 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00001891 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001892 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001893 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001894 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00001895 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001896 }
1897 return Res;
1898}
1899
Dan Gohman475871a2008-07-27 21:46:04 +00001900SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) {
1901 SDValue Chain = Op.getOperand(0);
1902 SDValue Table = Op.getOperand(1);
1903 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001904 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001905
Owen Andersone50ed302009-08-10 22:56:29 +00001906 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00001907 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1908 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00001909 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00001910 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00001911 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00001912 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
1913 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00001914 if (Subtarget->isThumb2()) {
1915 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
1916 // which does another jump to the destination. This also makes it easier
1917 // to translate it to TBB / TBH later.
1918 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00001919 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00001920 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001921 }
Evan Cheng66ac5312009-07-25 00:33:29 +00001922 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001923 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr, NULL, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00001924 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001925 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00001926 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001927 } else {
1928 Addr = DAG.getLoad(PTy, dl, Chain, Addr, NULL, 0);
1929 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00001930 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001931 }
Evan Chenga8e29892007-01-19 07:51:42 +00001932}
1933
Dan Gohman475871a2008-07-27 21:46:04 +00001934static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00001935 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001936 unsigned Opc =
1937 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
Owen Anderson825b72b2009-08-11 20:47:22 +00001938 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
1939 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001940}
1941
Dan Gohman475871a2008-07-27 21:46:04 +00001942static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001943 EVT VT = Op.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001944 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001945 unsigned Opc =
1946 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1947
Owen Anderson825b72b2009-08-11 20:47:22 +00001948 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
Dale Johannesende064702009-02-06 21:50:26 +00001949 return DAG.getNode(Opc, dl, VT, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001950}
1951
Dan Gohman475871a2008-07-27 21:46:04 +00001952static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001953 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00001954 SDValue Tmp0 = Op.getOperand(0);
1955 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00001956 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001957 EVT VT = Op.getValueType();
1958 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001959 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
1960 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001961 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1962 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001963 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001964}
1965
Jim Grosbach0e0da732009-05-12 23:59:14 +00001966SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
1967 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1968 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00001969 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001970 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
1971 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00001972 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00001973 ? ARM::R7 : ARM::R11;
1974 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
1975 while (Depth--)
1976 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
1977 return FrameAddr;
1978}
1979
Dan Gohman475871a2008-07-27 21:46:04 +00001980SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00001981ARMTargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001982 SDValue Chain,
1983 SDValue Dst, SDValue Src,
1984 SDValue Size, unsigned Align,
Dan Gohman707e0182008-04-12 04:36:06 +00001985 bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00001986 const Value *DstSV, uint64_t DstSVOff,
1987 const Value *SrcSV, uint64_t SrcSVOff){
Evan Cheng4102eb52007-10-22 22:11:27 +00001988 // Do repeated 4-byte loads and stores. To be improved.
Dan Gohman707e0182008-04-12 04:36:06 +00001989 // This requires 4-byte alignment.
1990 if ((Align & 3) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00001991 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001992 // This requires the copy size to be a constant, preferrably
1993 // within a subtarget-specific limit.
1994 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
1995 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00001996 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001997 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001998 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00001999 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00002000
2001 unsigned BytesLeft = SizeVal & 3;
2002 unsigned NumMemOps = SizeVal >> 2;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002003 unsigned EmittedNumMemOps = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00002004 EVT VT = MVT::i32;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002005 unsigned VTSize = 4;
Evan Cheng4102eb52007-10-22 22:11:27 +00002006 unsigned i = 0;
Evan Chenge5e7ce42007-05-18 01:19:57 +00002007 const unsigned MAX_LOADS_IN_LDM = 6;
Dan Gohman475871a2008-07-27 21:46:04 +00002008 SDValue TFOps[MAX_LOADS_IN_LDM];
2009 SDValue Loads[MAX_LOADS_IN_LDM];
Dan Gohman1f13c682008-04-28 17:15:20 +00002010 uint64_t SrcOff = 0, DstOff = 0;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002011
Evan Cheng4102eb52007-10-22 22:11:27 +00002012 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
2013 // same number of stores. The loads and stores will get combined into
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002014 // ldm/stm later on.
Evan Cheng4102eb52007-10-22 22:11:27 +00002015 while (EmittedNumMemOps < NumMemOps) {
2016 for (i = 0;
2017 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00002018 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00002019 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
2020 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002021 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002022 TFOps[i] = Loads[i].getValue(1);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002023 SrcOff += VTSize;
2024 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002025 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002026
Evan Cheng4102eb52007-10-22 22:11:27 +00002027 for (i = 0;
2028 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00002029 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00002030 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
2031 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002032 DstSV, DstSVOff + DstOff);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002033 DstOff += VTSize;
2034 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002035 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00002036
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002037 EmittedNumMemOps += i;
2038 }
2039
Bob Wilson2dc4f542009-03-20 22:42:55 +00002040 if (BytesLeft == 0)
Evan Cheng4102eb52007-10-22 22:11:27 +00002041 return Chain;
2042
2043 // Issue loads / stores for the trailing (1 - 3) bytes.
2044 unsigned BytesLeftSave = BytesLeft;
2045 i = 0;
2046 while (BytesLeft) {
2047 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002048 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00002049 VTSize = 2;
2050 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00002051 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00002052 VTSize = 1;
2053 }
2054
Dale Johannesen0f502f62009-02-03 22:26:09 +00002055 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00002056 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
2057 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002058 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002059 TFOps[i] = Loads[i].getValue(1);
2060 ++i;
2061 SrcOff += VTSize;
2062 BytesLeft -= VTSize;
2063 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002064 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00002065
2066 i = 0;
2067 BytesLeft = BytesLeftSave;
2068 while (BytesLeft) {
2069 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002070 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00002071 VTSize = 2;
2072 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00002073 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00002074 VTSize = 1;
2075 }
2076
Dale Johannesen0f502f62009-02-03 22:26:09 +00002077 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00002078 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
2079 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002080 DstSV, DstSVOff + DstOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002081 ++i;
2082 DstOff += VTSize;
2083 BytesLeft -= VTSize;
2084 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002085 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002086}
2087
Duncan Sands1607f052008-12-01 11:39:25 +00002088static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00002089 SDValue Op = N->getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +00002090 DebugLoc dl = N->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002091 if (N->getValueType(0) == MVT::f64) {
Evan Chengc7c77292008-11-04 19:57:48 +00002092 // Turn i64->f64 into FMDRR.
Owen Anderson825b72b2009-08-11 20:47:22 +00002093 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2094 DAG.getConstant(0, MVT::i32));
2095 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2096 DAG.getConstant(1, MVT::i32));
2097 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002098 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002099
Evan Chengc7c77292008-11-04 19:57:48 +00002100 // Turn f64->i64 into FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00002101 SDValue Cvt = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002102 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002103
Chris Lattner27a6c732007-11-24 07:07:01 +00002104 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002105 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
Chris Lattner27a6c732007-11-24 07:07:01 +00002106}
2107
Bob Wilson5bafff32009-06-22 23:27:02 +00002108/// getZeroVector - Returns a vector of specified type with all zero elements.
2109///
Owen Andersone50ed302009-08-10 22:56:29 +00002110static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002111 assert(VT.isVector() && "Expected a vector type");
2112
2113 // Zero vectors are used to represent vector negation and in those cases
2114 // will be implemented with the NEON VNEG instruction. However, VNEG does
2115 // not support i64 elements, so sometimes the zero vectors will need to be
2116 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002117 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilson5bafff32009-06-22 23:27:02 +00002118 // to their dest type. This ensures they get CSE'd.
2119 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002120 SDValue Cst = DAG.getTargetConstant(0, 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 Wilson5bafff32009-06-22 23:27:02 +00002130
2131 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2132}
2133
2134/// getOnesVector - Returns a vector of specified type with all bits set.
2135///
Owen Andersone50ed302009-08-10 22:56:29 +00002136static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002137 assert(VT.isVector() && "Expected a vector type");
2138
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002139 // Always build ones vectors as <16 x i32> or <8 x i32> bitcasted to their
2140 // dest type. This ensures they get CSE'd.
Bob Wilson5bafff32009-06-22 23:27:02 +00002141 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002142 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2143 SmallVector<SDValue, 8> Ops;
2144 MVT TVT;
2145
2146 if (VT.getSizeInBits() == 64) {
2147 Ops.assign(8, Cst); TVT = MVT::v8i8;
2148 } else {
2149 Ops.assign(16, Cst); TVT = MVT::v16i8;
2150 }
2151 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002152
2153 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2154}
2155
2156static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2157 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002158 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002159 DebugLoc dl = N->getDebugLoc();
2160
2161 // Lower vector shifts on NEON to use VSHL.
2162 if (VT.isVector()) {
2163 assert(ST->hasNEON() && "unexpected vector shift");
2164
2165 // Left shifts translate directly to the vshiftu intrinsic.
2166 if (N->getOpcode() == ISD::SHL)
2167 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002168 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002169 N->getOperand(0), N->getOperand(1));
2170
2171 assert((N->getOpcode() == ISD::SRA ||
2172 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2173
2174 // NEON uses the same intrinsics for both left and right shifts. For
2175 // right shifts, the shift amounts are negative, so negate the vector of
2176 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002177 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002178 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2179 getZeroVector(ShiftVT, DAG, dl),
2180 N->getOperand(1));
2181 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2182 Intrinsic::arm_neon_vshifts :
2183 Intrinsic::arm_neon_vshiftu);
2184 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002185 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002186 N->getOperand(0), NegatedCount);
2187 }
2188
Eli Friedmance392eb2009-08-22 03:13:10 +00002189 // We can get here for a node like i32 = ISD::SHL i32, i64
2190 if (VT != MVT::i64)
2191 return SDValue();
2192
2193 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002194 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002195
Chris Lattner27a6c732007-11-24 07:07:01 +00002196 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2197 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002198 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002199 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002200
Chris Lattner27a6c732007-11-24 07:07:01 +00002201 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002202 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002203
Chris Lattner27a6c732007-11-24 07:07:01 +00002204 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002205 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2206 DAG.getConstant(0, MVT::i32));
2207 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2208 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002209
Chris Lattner27a6c732007-11-24 07:07:01 +00002210 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2211 // captures the result into a carry flag.
2212 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002213 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002214
Chris Lattner27a6c732007-11-24 07:07:01 +00002215 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002216 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002217
Chris Lattner27a6c732007-11-24 07:07:01 +00002218 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002219 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002220}
2221
Bob Wilson5bafff32009-06-22 23:27:02 +00002222static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2223 SDValue TmpOp0, TmpOp1;
2224 bool Invert = false;
2225 bool Swap = false;
2226 unsigned Opc = 0;
2227
2228 SDValue Op0 = Op.getOperand(0);
2229 SDValue Op1 = Op.getOperand(1);
2230 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002231 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002232 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2233 DebugLoc dl = Op.getDebugLoc();
2234
2235 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2236 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002237 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002238 case ISD::SETUNE:
2239 case ISD::SETNE: Invert = true; // Fallthrough
2240 case ISD::SETOEQ:
2241 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2242 case ISD::SETOLT:
2243 case ISD::SETLT: Swap = true; // Fallthrough
2244 case ISD::SETOGT:
2245 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2246 case ISD::SETOLE:
2247 case ISD::SETLE: Swap = true; // Fallthrough
2248 case ISD::SETOGE:
2249 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2250 case ISD::SETUGE: Swap = true; // Fallthrough
2251 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2252 case ISD::SETUGT: Swap = true; // Fallthrough
2253 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2254 case ISD::SETUEQ: Invert = true; // Fallthrough
2255 case ISD::SETONE:
2256 // Expand this to (OLT | OGT).
2257 TmpOp0 = Op0;
2258 TmpOp1 = Op1;
2259 Opc = ISD::OR;
2260 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2261 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2262 break;
2263 case ISD::SETUO: Invert = true; // Fallthrough
2264 case ISD::SETO:
2265 // Expand this to (OLT | OGE).
2266 TmpOp0 = Op0;
2267 TmpOp1 = Op1;
2268 Opc = ISD::OR;
2269 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2270 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2271 break;
2272 }
2273 } else {
2274 // Integer comparisons.
2275 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002276 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002277 case ISD::SETNE: Invert = true;
2278 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2279 case ISD::SETLT: Swap = true;
2280 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2281 case ISD::SETLE: Swap = true;
2282 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2283 case ISD::SETULT: Swap = true;
2284 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2285 case ISD::SETULE: Swap = true;
2286 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2287 }
2288
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002289 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002290 if (Opc == ARMISD::VCEQ) {
2291
2292 SDValue AndOp;
2293 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2294 AndOp = Op0;
2295 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2296 AndOp = Op1;
2297
2298 // Ignore bitconvert.
2299 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2300 AndOp = AndOp.getOperand(0);
2301
2302 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2303 Opc = ARMISD::VTST;
2304 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2305 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2306 Invert = !Invert;
2307 }
2308 }
2309 }
2310
2311 if (Swap)
2312 std::swap(Op0, Op1);
2313
2314 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2315
2316 if (Invert)
2317 Result = DAG.getNOT(dl, Result, VT);
2318
2319 return Result;
2320}
2321
2322/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2323/// VMOV instruction, and if so, return the constant being splatted.
2324static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2325 unsigned SplatBitSize, SelectionDAG &DAG) {
2326 switch (SplatBitSize) {
2327 case 8:
2328 // Any 1-byte value is OK.
2329 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson825b72b2009-08-11 20:47:22 +00002330 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilson5bafff32009-06-22 23:27:02 +00002331
2332 case 16:
2333 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2334 if ((SplatBits & ~0xff) == 0 ||
2335 (SplatBits & ~0xff00) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002336 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilson5bafff32009-06-22 23:27:02 +00002337 break;
2338
2339 case 32:
2340 // NEON's 32-bit VMOV supports splat values where:
2341 // * only one byte is nonzero, or
2342 // * the least significant byte is 0xff and the second byte is nonzero, or
2343 // * the least significant 2 bytes are 0xff and the third is nonzero.
2344 if ((SplatBits & ~0xff) == 0 ||
2345 (SplatBits & ~0xff00) == 0 ||
2346 (SplatBits & ~0xff0000) == 0 ||
2347 (SplatBits & ~0xff000000) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002348 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002349
2350 if ((SplatBits & ~0xffff) == 0 &&
2351 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002352 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002353
2354 if ((SplatBits & ~0xffffff) == 0 &&
2355 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002356 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002357
2358 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2359 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2360 // VMOV.I32. A (very) minor optimization would be to replicate the value
2361 // and fall through here to test for a valid 64-bit splat. But, then the
2362 // caller would also need to check and handle the change in size.
2363 break;
2364
2365 case 64: {
2366 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2367 uint64_t BitMask = 0xff;
2368 uint64_t Val = 0;
2369 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2370 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2371 Val |= BitMask;
2372 else if ((SplatBits & BitMask) != 0)
2373 return SDValue();
2374 BitMask <<= 8;
2375 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002376 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilson5bafff32009-06-22 23:27:02 +00002377 }
2378
2379 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002380 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilson5bafff32009-06-22 23:27:02 +00002381 break;
2382 }
2383
2384 return SDValue();
2385}
2386
2387/// getVMOVImm - If this is a build_vector of constants which can be
2388/// formed by using a VMOV instruction of the specified element size,
2389/// return the constant being splatted. The ByteSize field indicates the
2390/// number of bytes of each element [1248].
2391SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2392 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2393 APInt SplatBits, SplatUndef;
2394 unsigned SplatBitSize;
2395 bool HasAnyUndefs;
2396 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2397 HasAnyUndefs, ByteSize * 8))
2398 return SDValue();
2399
2400 if (SplatBitSize > ByteSize * 8)
2401 return SDValue();
2402
2403 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2404 SplatBitSize, DAG);
2405}
2406
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002407static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2408 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002409 unsigned NumElts = VT.getVectorNumElements();
2410 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002411 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002412
2413 // If this is a VEXT shuffle, the immediate value is the index of the first
2414 // element. The other shuffle indices must be the successive elements after
2415 // the first one.
2416 unsigned ExpectedElt = Imm;
2417 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002418 // Increment the expected index. If it wraps around, it may still be
2419 // a VEXT but the source vectors must be swapped.
2420 ExpectedElt += 1;
2421 if (ExpectedElt == NumElts * 2) {
2422 ExpectedElt = 0;
2423 ReverseVEXT = true;
2424 }
2425
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002426 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002427 return false;
2428 }
2429
2430 // Adjust the index value if the source operands will be swapped.
2431 if (ReverseVEXT)
2432 Imm -= NumElts;
2433
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002434 return true;
2435}
2436
Bob Wilson8bb9e482009-07-26 00:39:34 +00002437/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2438/// instruction with the specified blocksize. (The order of the elements
2439/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002440static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2441 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002442 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2443 "Only possible block sizes for VREV are: 16, 32, 64");
2444
Bob Wilson8bb9e482009-07-26 00:39:34 +00002445 unsigned NumElts = VT.getVectorNumElements();
2446 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002447 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002448
2449 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2450 return false;
2451
2452 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002453 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002454 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2455 return false;
2456 }
2457
2458 return true;
2459}
2460
Bob Wilsonc692cb72009-08-21 20:54:19 +00002461static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2462 unsigned &WhichResult) {
2463 unsigned NumElts = VT.getVectorNumElements();
2464 WhichResult = (M[0] == 0 ? 0 : 1);
2465 for (unsigned i = 0; i < NumElts; i += 2) {
2466 if ((unsigned) M[i] != i + WhichResult ||
2467 (unsigned) M[i+1] != i + NumElts + WhichResult)
2468 return false;
2469 }
2470 return true;
2471}
2472
2473static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2474 unsigned &WhichResult) {
2475 unsigned NumElts = VT.getVectorNumElements();
2476 WhichResult = (M[0] == 0 ? 0 : 1);
2477 for (unsigned i = 0; i != NumElts; ++i) {
2478 if ((unsigned) M[i] != 2 * i + WhichResult)
2479 return false;
2480 }
2481
2482 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2483 if (VT.is64BitVector() && VT.getVectorElementType().getSizeInBits() == 32)
2484 return false;
2485
2486 return true;
2487}
2488
2489static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2490 unsigned &WhichResult) {
2491 unsigned NumElts = VT.getVectorNumElements();
2492 WhichResult = (M[0] == 0 ? 0 : 1);
2493 unsigned Idx = WhichResult * NumElts / 2;
2494 for (unsigned i = 0; i != NumElts; i += 2) {
2495 if ((unsigned) M[i] != Idx ||
2496 (unsigned) M[i+1] != Idx + NumElts)
2497 return false;
2498 Idx += 1;
2499 }
2500
2501 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2502 if (VT.is64BitVector() && VT.getVectorElementType().getSizeInBits() == 32)
2503 return false;
2504
2505 return true;
2506}
2507
Owen Andersone50ed302009-08-10 22:56:29 +00002508static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002509 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00002510 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002511 if (ConstVal->isNullValue())
2512 return getZeroVector(VT, DAG, dl);
2513 if (ConstVal->isAllOnesValue())
2514 return getOnesVector(VT, DAG, dl);
2515
Owen Andersone50ed302009-08-10 22:56:29 +00002516 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00002517 if (VT.is64BitVector()) {
2518 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002519 case 8: CanonicalVT = MVT::v8i8; break;
2520 case 16: CanonicalVT = MVT::v4i16; break;
2521 case 32: CanonicalVT = MVT::v2i32; break;
2522 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002523 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002524 }
2525 } else {
2526 assert(VT.is128BitVector() && "unknown splat vector size");
2527 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002528 case 8: CanonicalVT = MVT::v16i8; break;
2529 case 16: CanonicalVT = MVT::v8i16; break;
2530 case 32: CanonicalVT = MVT::v4i32; break;
2531 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002532 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002533 }
2534 }
2535
2536 // Build a canonical splat for this value.
2537 SmallVector<SDValue, 8> Ops;
2538 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2539 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2540 Ops.size());
2541 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2542}
2543
2544// If this is a case we can't handle, return null and let the default
2545// expansion code take care of it.
2546static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00002547 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002548 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002549 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002550
2551 APInt SplatBits, SplatUndef;
2552 unsigned SplatBitSize;
2553 bool HasAnyUndefs;
2554 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00002555 if (SplatBitSize <= 64) {
2556 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2557 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2558 if (Val.getNode())
2559 return BuildSplat(Val, VT, DAG, dl);
2560 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00002561 }
2562
2563 // If there are only 2 elements in a 128-bit vector, insert them into an
2564 // undef vector. This handles the common case for 128-bit vector argument
2565 // passing, where the insertions should be translated to subreg accesses
2566 // with no real instructions.
2567 if (VT.is128BitVector() && Op.getNumOperands() == 2) {
2568 SDValue Val = DAG.getUNDEF(VT);
2569 SDValue Op0 = Op.getOperand(0);
2570 SDValue Op1 = Op.getOperand(1);
2571 if (Op0.getOpcode() != ISD::UNDEF)
2572 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op0,
2573 DAG.getIntPtrConstant(0));
2574 if (Op1.getOpcode() != ISD::UNDEF)
2575 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op1,
2576 DAG.getIntPtrConstant(1));
2577 return Val;
Bob Wilson5bafff32009-06-22 23:27:02 +00002578 }
2579
2580 return SDValue();
2581}
2582
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002583/// isShuffleMaskLegal - Targets can use this to indicate that they only
2584/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2585/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2586/// are assumed to be legal.
2587bool
2588ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2589 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002590 if (VT.getVectorNumElements() == 4 &&
2591 (VT.is128BitVector() || VT.is64BitVector())) {
2592 unsigned PFIndexes[4];
2593 for (unsigned i = 0; i != 4; ++i) {
2594 if (M[i] < 0)
2595 PFIndexes[i] = 8;
2596 else
2597 PFIndexes[i] = M[i];
2598 }
2599
2600 // Compute the index in the perfect shuffle table.
2601 unsigned PFTableIndex =
2602 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2603 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2604 unsigned Cost = (PFEntry >> 30);
2605
2606 if (Cost <= 4)
2607 return true;
2608 }
2609
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002610 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00002611 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002612
2613 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2614 isVREVMask(M, VT, 64) ||
2615 isVREVMask(M, VT, 32) ||
2616 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00002617 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2618 isVTRNMask(M, VT, WhichResult) ||
2619 isVUZPMask(M, VT, WhichResult) ||
2620 isVZIPMask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002621}
2622
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002623/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2624/// the specified operations to build the shuffle.
2625static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2626 SDValue RHS, SelectionDAG &DAG,
2627 DebugLoc dl) {
2628 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2629 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2630 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2631
2632 enum {
2633 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2634 OP_VREV,
2635 OP_VDUP0,
2636 OP_VDUP1,
2637 OP_VDUP2,
2638 OP_VDUP3,
2639 OP_VEXT1,
2640 OP_VEXT2,
2641 OP_VEXT3,
2642 OP_VUZPL, // VUZP, left result
2643 OP_VUZPR, // VUZP, right result
2644 OP_VZIPL, // VZIP, left result
2645 OP_VZIPR, // VZIP, right result
2646 OP_VTRNL, // VTRN, left result
2647 OP_VTRNR // VTRN, right result
2648 };
2649
2650 if (OpNum == OP_COPY) {
2651 if (LHSID == (1*9+2)*9+3) return LHS;
2652 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2653 return RHS;
2654 }
2655
2656 SDValue OpLHS, OpRHS;
2657 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2658 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2659 EVT VT = OpLHS.getValueType();
2660
2661 switch (OpNum) {
2662 default: llvm_unreachable("Unknown shuffle opcode!");
2663 case OP_VREV:
2664 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2665 case OP_VDUP0:
2666 case OP_VDUP1:
2667 case OP_VDUP2:
2668 case OP_VDUP3:
2669 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002670 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002671 case OP_VEXT1:
2672 case OP_VEXT2:
2673 case OP_VEXT3:
2674 return DAG.getNode(ARMISD::VEXT, dl, VT,
2675 OpLHS, OpRHS,
2676 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2677 case OP_VUZPL:
2678 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002679 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002680 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
2681 case OP_VZIPL:
2682 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002683 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002684 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
2685 case OP_VTRNL:
2686 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002687 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2688 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002689 }
2690}
2691
Bob Wilson5bafff32009-06-22 23:27:02 +00002692static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002693 SDValue V1 = Op.getOperand(0);
2694 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00002695 DebugLoc dl = Op.getDebugLoc();
2696 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002697 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002698 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00002699
Bob Wilson28865062009-08-13 02:13:04 +00002700 // Convert shuffles that are directly supported on NEON to target-specific
2701 // DAG nodes, instead of keeping them as shuffles and matching them again
2702 // during code selection. This is more efficient and avoids the possibility
2703 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00002704 // FIXME: floating-point vectors should be canonicalized to integer vectors
2705 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002706 SVN->getMask(ShuffleMask);
2707
2708 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson0ce37102009-08-14 05:08:32 +00002709 int Lane = SVN->getSplatIndex();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002710 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
2711 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00002712 }
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002713 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002714 DAG.getConstant(Lane, MVT::i32));
Bob Wilson0ce37102009-08-14 05:08:32 +00002715 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002716
2717 bool ReverseVEXT;
2718 unsigned Imm;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002719 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002720 if (ReverseVEXT)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002721 std::swap(V1, V2);
2722 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002723 DAG.getConstant(Imm, MVT::i32));
2724 }
2725
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002726 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002727 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002728 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002729 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002730 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002731 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
2732
Bob Wilsonc692cb72009-08-21 20:54:19 +00002733 // Check for Neon shuffles that modify both input vectors in place.
2734 // If both results are used, i.e., if there are two shuffles with the same
2735 // source operands and with masks corresponding to both results of one of
2736 // these operations, DAG memoization will ensure that a single node is
2737 // used for both shuffles.
2738 unsigned WhichResult;
2739 if (isVTRNMask(ShuffleMask, VT, WhichResult))
2740 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2741 V1, V2).getValue(WhichResult);
2742 if (isVUZPMask(ShuffleMask, VT, WhichResult))
2743 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2744 V1, V2).getValue(WhichResult);
2745 if (isVZIPMask(ShuffleMask, VT, WhichResult))
2746 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2747 V1, V2).getValue(WhichResult);
2748
2749 // If the shuffle is not directly supported and it has 4 elements, use
2750 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002751 if (VT.getVectorNumElements() == 4 &&
2752 (VT.is128BitVector() || VT.is64BitVector())) {
2753 unsigned PFIndexes[4];
2754 for (unsigned i = 0; i != 4; ++i) {
2755 if (ShuffleMask[i] < 0)
2756 PFIndexes[i] = 8;
2757 else
2758 PFIndexes[i] = ShuffleMask[i];
2759 }
2760
2761 // Compute the index in the perfect shuffle table.
2762 unsigned PFTableIndex =
2763 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2764
2765 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2766 unsigned Cost = (PFEntry >> 30);
2767
2768 if (Cost <= 4)
2769 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
2770 }
Bob Wilsond8e17572009-08-12 22:31:50 +00002771
Bob Wilson22cac0d2009-08-14 05:16:33 +00002772 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002773}
2774
Bob Wilson5bafff32009-06-22 23:27:02 +00002775static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002776 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002777 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00002778 SDValue Vec = Op.getOperand(0);
2779 SDValue Lane = Op.getOperand(1);
Anton Korobeynikovb00c03b2009-08-30 17:14:54 +00002780
2781 // FIXME: This is invalid for 8 and 16-bit elements - the information about
2782 // sign / zero extension is lost!
Owen Anderson825b72b2009-08-11 20:47:22 +00002783 Op = DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
2784 Op = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Op, DAG.getValueType(VT));
Anton Korobeynikovb00c03b2009-08-30 17:14:54 +00002785
2786 if (VT.bitsLT(MVT::i32))
2787 Op = DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
2788 else if (VT.bitsGT(MVT::i32))
2789 Op = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Op);
2790
2791 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00002792}
2793
Bob Wilsona6d65862009-08-03 20:36:38 +00002794static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
2795 // The only time a CONCAT_VECTORS operation can have legal types is when
2796 // two 64-bit vectors are concatenated to a 128-bit vector.
2797 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
2798 "unexpected CONCAT_VECTORS");
2799 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002800 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00002801 SDValue Op0 = Op.getOperand(0);
2802 SDValue Op1 = Op.getOperand(1);
2803 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002804 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2805 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00002806 DAG.getIntPtrConstant(0));
2807 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002808 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2809 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00002810 DAG.getIntPtrConstant(1));
2811 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00002812}
2813
Dan Gohman475871a2008-07-27 21:46:04 +00002814SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002815 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002816 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00002817 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002818 case ISD::GlobalAddress:
2819 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
2820 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002821 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002822 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
2823 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
2824 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00002825 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002826 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
2827 case ISD::SINT_TO_FP:
2828 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
2829 case ISD::FP_TO_SINT:
2830 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
2831 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00002832 case ISD::RETURNADDR: break;
Jim Grosbach0e0da732009-05-12 23:59:14 +00002833 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002834 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Bob Wilsonb36ec862009-08-06 18:47:44 +00002835 case ISD::INTRINSIC_VOID:
Bob Wilsona599bff2009-08-04 00:36:16 +00002836 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002837 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00002838 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002839 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00002840 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00002841 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
2842 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
2843 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2844 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002845 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00002846 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002847 }
Dan Gohman475871a2008-07-27 21:46:04 +00002848 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00002849}
2850
Duncan Sands1607f052008-12-01 11:39:25 +00002851/// ReplaceNodeResults - Replace the results of node with an illegal result
2852/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00002853void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
2854 SmallVectorImpl<SDValue>&Results,
2855 SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00002856 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00002857 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002858 llvm_unreachable("Don't know how to custom expand this!");
Duncan Sands1607f052008-12-01 11:39:25 +00002859 return;
2860 case ISD::BIT_CONVERT:
2861 Results.push_back(ExpandBIT_CONVERT(N, DAG));
2862 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00002863 case ISD::SRL:
Duncan Sands1607f052008-12-01 11:39:25 +00002864 case ISD::SRA: {
Bob Wilson5bafff32009-06-22 23:27:02 +00002865 SDValue Res = LowerShift(N, DAG, Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00002866 if (Res.getNode())
2867 Results.push_back(Res);
2868 return;
2869 }
Chris Lattner27a6c732007-11-24 07:07:01 +00002870 }
2871}
Chris Lattner27a6c732007-11-24 07:07:01 +00002872
Evan Chenga8e29892007-01-19 07:51:42 +00002873//===----------------------------------------------------------------------===//
2874// ARM Scheduler Hooks
2875//===----------------------------------------------------------------------===//
2876
2877MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00002878ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00002879 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002880 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00002881 DebugLoc dl = MI->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002882 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00002883 default:
2884 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng007ea272009-08-12 05:17:19 +00002885 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00002886 // To "insert" a SELECT_CC instruction, we actually have to insert the
2887 // diamond control-flow pattern. The incoming instruction knows the
2888 // destination vreg to set, the condition code register to branch on, the
2889 // true/false values to select between, and a branch opcode to use.
2890 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002891 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00002892 ++It;
2893
2894 // thisMBB:
2895 // ...
2896 // TrueVal = ...
2897 // cmpTY ccX, r1, r2
2898 // bCC copy1MBB
2899 // fallthrough --> copy0MBB
2900 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002901 MachineFunction *F = BB->getParent();
2902 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
2903 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00002904 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00002905 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002906 F->insert(It, copy0MBB);
2907 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00002908 // Update machine-CFG edges by first adding all successors of the current
2909 // block to the new block which will contain the Phi node for the select.
2910 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
2911 e = BB->succ_end(); i != e; ++i)
2912 sinkMBB->addSuccessor(*i);
2913 // Next, remove all successors of the current block, and add the true
2914 // and fallthrough blocks as its successors.
2915 while(!BB->succ_empty())
2916 BB->removeSuccessor(BB->succ_begin());
2917 BB->addSuccessor(copy0MBB);
2918 BB->addSuccessor(sinkMBB);
2919
2920 // copy0MBB:
2921 // %FalseValue = ...
2922 // # fallthrough to sinkMBB
2923 BB = copy0MBB;
2924
2925 // Update machine-CFG edges
2926 BB->addSuccessor(sinkMBB);
2927
2928 // sinkMBB:
2929 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2930 // ...
2931 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00002932 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00002933 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
2934 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
2935
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002936 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00002937 return BB;
2938 }
Evan Cheng86198642009-08-07 00:34:42 +00002939
2940 case ARM::tANDsp:
2941 case ARM::tADDspr_:
2942 case ARM::tSUBspi_:
2943 case ARM::t2SUBrSPi_:
2944 case ARM::t2SUBrSPi12_:
2945 case ARM::t2SUBrSPs_: {
2946 MachineFunction *MF = BB->getParent();
2947 unsigned DstReg = MI->getOperand(0).getReg();
2948 unsigned SrcReg = MI->getOperand(1).getReg();
2949 bool DstIsDead = MI->getOperand(0).isDead();
2950 bool SrcIsKill = MI->getOperand(1).isKill();
2951
2952 if (SrcReg != ARM::SP) {
2953 // Copy the source to SP from virtual register.
2954 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
2955 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
2956 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
2957 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
2958 .addReg(SrcReg, getKillRegState(SrcIsKill));
2959 }
2960
2961 unsigned OpOpc = 0;
2962 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
2963 switch (MI->getOpcode()) {
2964 default:
2965 llvm_unreachable("Unexpected pseudo instruction!");
2966 case ARM::tANDsp:
2967 OpOpc = ARM::tAND;
2968 NeedPred = true;
2969 break;
2970 case ARM::tADDspr_:
2971 OpOpc = ARM::tADDspr;
2972 break;
2973 case ARM::tSUBspi_:
2974 OpOpc = ARM::tSUBspi;
2975 break;
2976 case ARM::t2SUBrSPi_:
2977 OpOpc = ARM::t2SUBrSPi;
2978 NeedPred = true; NeedCC = true;
2979 break;
2980 case ARM::t2SUBrSPi12_:
2981 OpOpc = ARM::t2SUBrSPi12;
2982 NeedPred = true;
2983 break;
2984 case ARM::t2SUBrSPs_:
2985 OpOpc = ARM::t2SUBrSPs;
2986 NeedPred = true; NeedCC = true; NeedOp3 = true;
2987 break;
2988 }
2989 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
2990 if (OpOpc == ARM::tAND)
2991 AddDefaultT1CC(MIB);
2992 MIB.addReg(ARM::SP);
2993 MIB.addOperand(MI->getOperand(2));
2994 if (NeedOp3)
2995 MIB.addOperand(MI->getOperand(3));
2996 if (NeedPred)
2997 AddDefaultPred(MIB);
2998 if (NeedCC)
2999 AddDefaultCC(MIB);
3000
3001 // Copy the result from SP to virtual register.
3002 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
3003 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3004 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
3005 BuildMI(BB, dl, TII->get(CopyOpc))
3006 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
3007 .addReg(ARM::SP);
3008 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3009 return BB;
3010 }
Evan Chenga8e29892007-01-19 07:51:42 +00003011 }
3012}
3013
3014//===----------------------------------------------------------------------===//
3015// ARM Optimization Hooks
3016//===----------------------------------------------------------------------===//
3017
Chris Lattnerd1980a52009-03-12 06:52:53 +00003018static
3019SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3020 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00003021 SelectionDAG &DAG = DCI.DAG;
3022 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003023 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00003024 unsigned Opc = N->getOpcode();
3025 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3026 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3027 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3028 ISD::CondCode CC = ISD::SETCC_INVALID;
3029
3030 if (isSlctCC) {
3031 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
3032 } else {
3033 SDValue CCOp = Slct.getOperand(0);
3034 if (CCOp.getOpcode() == ISD::SETCC)
3035 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
3036 }
3037
3038 bool DoXform = false;
3039 bool InvCC = false;
3040 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
3041 "Bad input!");
3042
3043 if (LHS.getOpcode() == ISD::Constant &&
3044 cast<ConstantSDNode>(LHS)->isNullValue()) {
3045 DoXform = true;
3046 } else if (CC != ISD::SETCC_INVALID &&
3047 RHS.getOpcode() == ISD::Constant &&
3048 cast<ConstantSDNode>(RHS)->isNullValue()) {
3049 std::swap(LHS, RHS);
3050 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003051 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00003052 Op0.getOperand(0).getValueType();
3053 bool isInt = OpVT.isInteger();
3054 CC = ISD::getSetCCInverse(CC, isInt);
3055
3056 if (!TLI.isCondCodeLegal(CC, OpVT))
3057 return SDValue(); // Inverse operator isn't legal.
3058
3059 DoXform = true;
3060 InvCC = true;
3061 }
3062
3063 if (DoXform) {
3064 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3065 if (isSlctCC)
3066 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3067 Slct.getOperand(0), Slct.getOperand(1), CC);
3068 SDValue CCOp = Slct.getOperand(0);
3069 if (InvCC)
3070 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3071 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3072 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3073 CCOp, OtherOp, Result);
3074 }
3075 return SDValue();
3076}
3077
3078/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3079static SDValue PerformADDCombine(SDNode *N,
3080 TargetLowering::DAGCombinerInfo &DCI) {
3081 // added by evan in r37685 with no testcase.
3082 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003083
Chris Lattnerd1980a52009-03-12 06:52:53 +00003084 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3085 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3086 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3087 if (Result.getNode()) return Result;
3088 }
3089 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3090 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3091 if (Result.getNode()) return Result;
3092 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003093
Chris Lattnerd1980a52009-03-12 06:52:53 +00003094 return SDValue();
3095}
3096
3097/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3098static SDValue PerformSUBCombine(SDNode *N,
3099 TargetLowering::DAGCombinerInfo &DCI) {
3100 // added by evan in r37685 with no testcase.
3101 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003102
Chris Lattnerd1980a52009-03-12 06:52:53 +00003103 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3104 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3105 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3106 if (Result.getNode()) return Result;
3107 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003108
Chris Lattnerd1980a52009-03-12 06:52:53 +00003109 return SDValue();
3110}
3111
3112
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003113/// PerformFMRRDCombine - Target-specific dag combine xforms for ARMISD::FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003114static SDValue PerformFMRRDCombine(SDNode *N,
3115 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003116 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003117 SDValue InDouble = N->getOperand(0);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003118 if (InDouble.getOpcode() == ARMISD::FMDRR)
3119 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003120 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003121}
3122
Bob Wilson5bafff32009-06-22 23:27:02 +00003123/// getVShiftImm - Check if this is a valid build_vector for the immediate
3124/// operand of a vector shift operation, where all the elements of the
3125/// build_vector must have the same constant integer value.
3126static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3127 // Ignore bit_converts.
3128 while (Op.getOpcode() == ISD::BIT_CONVERT)
3129 Op = Op.getOperand(0);
3130 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3131 APInt SplatBits, SplatUndef;
3132 unsigned SplatBitSize;
3133 bool HasAnyUndefs;
3134 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3135 HasAnyUndefs, ElementBits) ||
3136 SplatBitSize > ElementBits)
3137 return false;
3138 Cnt = SplatBits.getSExtValue();
3139 return true;
3140}
3141
3142/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3143/// operand of a vector shift left operation. That value must be in the range:
3144/// 0 <= Value < ElementBits for a left shift; or
3145/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003146static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003147 assert(VT.isVector() && "vector shift count is not a vector type");
3148 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3149 if (! getVShiftImm(Op, ElementBits, Cnt))
3150 return false;
3151 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3152}
3153
3154/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3155/// operand of a vector shift right operation. For a shift opcode, the value
3156/// is positive, but for an intrinsic the value count must be negative. The
3157/// absolute value must be in the range:
3158/// 1 <= |Value| <= ElementBits for a right shift; or
3159/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003160static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00003161 int64_t &Cnt) {
3162 assert(VT.isVector() && "vector shift count is not a vector type");
3163 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3164 if (! getVShiftImm(Op, ElementBits, Cnt))
3165 return false;
3166 if (isIntrinsic)
3167 Cnt = -Cnt;
3168 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3169}
3170
3171/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3172static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3173 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3174 switch (IntNo) {
3175 default:
3176 // Don't do anything for most intrinsics.
3177 break;
3178
3179 // Vector shifts: check for immediate versions and lower them.
3180 // Note: This is done during DAG combining instead of DAG legalizing because
3181 // the build_vectors for 64-bit vector element shift counts are generally
3182 // not legal, and it is hard to see their values after they get legalized to
3183 // loads from a constant pool.
3184 case Intrinsic::arm_neon_vshifts:
3185 case Intrinsic::arm_neon_vshiftu:
3186 case Intrinsic::arm_neon_vshiftls:
3187 case Intrinsic::arm_neon_vshiftlu:
3188 case Intrinsic::arm_neon_vshiftn:
3189 case Intrinsic::arm_neon_vrshifts:
3190 case Intrinsic::arm_neon_vrshiftu:
3191 case Intrinsic::arm_neon_vrshiftn:
3192 case Intrinsic::arm_neon_vqshifts:
3193 case Intrinsic::arm_neon_vqshiftu:
3194 case Intrinsic::arm_neon_vqshiftsu:
3195 case Intrinsic::arm_neon_vqshiftns:
3196 case Intrinsic::arm_neon_vqshiftnu:
3197 case Intrinsic::arm_neon_vqshiftnsu:
3198 case Intrinsic::arm_neon_vqrshiftns:
3199 case Intrinsic::arm_neon_vqrshiftnu:
3200 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00003201 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003202 int64_t Cnt;
3203 unsigned VShiftOpc = 0;
3204
3205 switch (IntNo) {
3206 case Intrinsic::arm_neon_vshifts:
3207 case Intrinsic::arm_neon_vshiftu:
3208 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3209 VShiftOpc = ARMISD::VSHL;
3210 break;
3211 }
3212 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3213 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3214 ARMISD::VSHRs : ARMISD::VSHRu);
3215 break;
3216 }
3217 return SDValue();
3218
3219 case Intrinsic::arm_neon_vshiftls:
3220 case Intrinsic::arm_neon_vshiftlu:
3221 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3222 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003223 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003224
3225 case Intrinsic::arm_neon_vrshifts:
3226 case Intrinsic::arm_neon_vrshiftu:
3227 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3228 break;
3229 return SDValue();
3230
3231 case Intrinsic::arm_neon_vqshifts:
3232 case Intrinsic::arm_neon_vqshiftu:
3233 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3234 break;
3235 return SDValue();
3236
3237 case Intrinsic::arm_neon_vqshiftsu:
3238 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3239 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003240 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003241
3242 case Intrinsic::arm_neon_vshiftn:
3243 case Intrinsic::arm_neon_vrshiftn:
3244 case Intrinsic::arm_neon_vqshiftns:
3245 case Intrinsic::arm_neon_vqshiftnu:
3246 case Intrinsic::arm_neon_vqshiftnsu:
3247 case Intrinsic::arm_neon_vqrshiftns:
3248 case Intrinsic::arm_neon_vqrshiftnu:
3249 case Intrinsic::arm_neon_vqrshiftnsu:
3250 // Narrowing shifts require an immediate right shift.
3251 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3252 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003253 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003254
3255 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003256 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003257 }
3258
3259 switch (IntNo) {
3260 case Intrinsic::arm_neon_vshifts:
3261 case Intrinsic::arm_neon_vshiftu:
3262 // Opcode already set above.
3263 break;
3264 case Intrinsic::arm_neon_vshiftls:
3265 case Intrinsic::arm_neon_vshiftlu:
3266 if (Cnt == VT.getVectorElementType().getSizeInBits())
3267 VShiftOpc = ARMISD::VSHLLi;
3268 else
3269 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3270 ARMISD::VSHLLs : ARMISD::VSHLLu);
3271 break;
3272 case Intrinsic::arm_neon_vshiftn:
3273 VShiftOpc = ARMISD::VSHRN; break;
3274 case Intrinsic::arm_neon_vrshifts:
3275 VShiftOpc = ARMISD::VRSHRs; break;
3276 case Intrinsic::arm_neon_vrshiftu:
3277 VShiftOpc = ARMISD::VRSHRu; break;
3278 case Intrinsic::arm_neon_vrshiftn:
3279 VShiftOpc = ARMISD::VRSHRN; break;
3280 case Intrinsic::arm_neon_vqshifts:
3281 VShiftOpc = ARMISD::VQSHLs; break;
3282 case Intrinsic::arm_neon_vqshiftu:
3283 VShiftOpc = ARMISD::VQSHLu; break;
3284 case Intrinsic::arm_neon_vqshiftsu:
3285 VShiftOpc = ARMISD::VQSHLsu; break;
3286 case Intrinsic::arm_neon_vqshiftns:
3287 VShiftOpc = ARMISD::VQSHRNs; break;
3288 case Intrinsic::arm_neon_vqshiftnu:
3289 VShiftOpc = ARMISD::VQSHRNu; break;
3290 case Intrinsic::arm_neon_vqshiftnsu:
3291 VShiftOpc = ARMISD::VQSHRNsu; break;
3292 case Intrinsic::arm_neon_vqrshiftns:
3293 VShiftOpc = ARMISD::VQRSHRNs; break;
3294 case Intrinsic::arm_neon_vqrshiftnu:
3295 VShiftOpc = ARMISD::VQRSHRNu; break;
3296 case Intrinsic::arm_neon_vqrshiftnsu:
3297 VShiftOpc = ARMISD::VQRSHRNsu; break;
3298 }
3299
3300 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003301 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003302 }
3303
3304 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00003305 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003306 int64_t Cnt;
3307 unsigned VShiftOpc = 0;
3308
3309 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3310 VShiftOpc = ARMISD::VSLI;
3311 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3312 VShiftOpc = ARMISD::VSRI;
3313 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003314 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003315 }
3316
3317 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3318 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00003319 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003320 }
3321
3322 case Intrinsic::arm_neon_vqrshifts:
3323 case Intrinsic::arm_neon_vqrshiftu:
3324 // No immediate versions of these to check for.
3325 break;
3326 }
3327
3328 return SDValue();
3329}
3330
3331/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3332/// lowers them. As with the vector shift intrinsics, this is done during DAG
3333/// combining instead of DAG legalizing because the build_vectors for 64-bit
3334/// vector element shift counts are generally not legal, and it is hard to see
3335/// their values after they get legalized to loads from a constant pool.
3336static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3337 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003338 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003339
3340 // Nothing to be done for scalar shifts.
3341 if (! VT.isVector())
3342 return SDValue();
3343
3344 assert(ST->hasNEON() && "unexpected vector shift");
3345 int64_t Cnt;
3346
3347 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003348 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003349
3350 case ISD::SHL:
3351 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3352 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003353 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003354 break;
3355
3356 case ISD::SRA:
3357 case ISD::SRL:
3358 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3359 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3360 ARMISD::VSHRs : ARMISD::VSHRu);
3361 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003362 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003363 }
3364 }
3365 return SDValue();
3366}
3367
3368/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3369/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3370static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3371 const ARMSubtarget *ST) {
3372 SDValue N0 = N->getOperand(0);
3373
3374 // Check for sign- and zero-extensions of vector extract operations of 8-
3375 // and 16-bit vector elements. NEON supports these directly. They are
3376 // handled during DAG combining because type legalization will promote them
3377 // to 32-bit types and it is messy to recognize the operations after that.
3378 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3379 SDValue Vec = N0.getOperand(0);
3380 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003381 EVT VT = N->getValueType(0);
3382 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003383 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3384
Owen Anderson825b72b2009-08-11 20:47:22 +00003385 if (VT == MVT::i32 &&
3386 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00003387 TLI.isTypeLegal(Vec.getValueType())) {
3388
3389 unsigned Opc = 0;
3390 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003391 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003392 case ISD::SIGN_EXTEND:
3393 Opc = ARMISD::VGETLANEs;
3394 break;
3395 case ISD::ZERO_EXTEND:
3396 case ISD::ANY_EXTEND:
3397 Opc = ARMISD::VGETLANEu;
3398 break;
3399 }
3400 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
3401 }
3402 }
3403
3404 return SDValue();
3405}
3406
Dan Gohman475871a2008-07-27 21:46:04 +00003407SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003408 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003409 switch (N->getOpcode()) {
3410 default: break;
Chris Lattnerd1980a52009-03-12 06:52:53 +00003411 case ISD::ADD: return PerformADDCombine(N, DCI);
3412 case ISD::SUB: return PerformSUBCombine(N, DCI);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003413 case ARMISD::FMRRD: return PerformFMRRDCombine(N, DCI);
Bob Wilson5bafff32009-06-22 23:27:02 +00003414 case ISD::INTRINSIC_WO_CHAIN:
3415 return PerformIntrinsicCombine(N, DCI.DAG);
3416 case ISD::SHL:
3417 case ISD::SRA:
3418 case ISD::SRL:
3419 return PerformShiftCombine(N, DCI.DAG, Subtarget);
3420 case ISD::SIGN_EXTEND:
3421 case ISD::ZERO_EXTEND:
3422 case ISD::ANY_EXTEND:
3423 return PerformExtendCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003424 }
Dan Gohman475871a2008-07-27 21:46:04 +00003425 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003426}
3427
Bill Wendlingaf566342009-08-15 21:21:19 +00003428bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
3429 if (!Subtarget->hasV6Ops())
3430 // Pre-v6 does not support unaligned mem access.
3431 return false;
3432 else if (!Subtarget->hasV6Ops()) {
3433 // v6 may or may not support unaligned mem access.
3434 if (!Subtarget->isTargetDarwin())
3435 return false;
3436 }
3437
3438 switch (VT.getSimpleVT().SimpleTy) {
3439 default:
3440 return false;
3441 case MVT::i8:
3442 case MVT::i16:
3443 case MVT::i32:
3444 return true;
3445 // FIXME: VLD1 etc with standard alignment is legal.
3446 }
3447}
3448
Evan Chenge6c835f2009-08-14 20:09:37 +00003449static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
3450 if (V < 0)
3451 return false;
3452
3453 unsigned Scale = 1;
3454 switch (VT.getSimpleVT().SimpleTy) {
3455 default: return false;
3456 case MVT::i1:
3457 case MVT::i8:
3458 // Scale == 1;
3459 break;
3460 case MVT::i16:
3461 // Scale == 2;
3462 Scale = 2;
3463 break;
3464 case MVT::i32:
3465 // Scale == 4;
3466 Scale = 4;
3467 break;
3468 }
3469
3470 if ((V & (Scale - 1)) != 0)
3471 return false;
3472 V /= Scale;
3473 return V == (V & ((1LL << 5) - 1));
3474}
3475
3476static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
3477 const ARMSubtarget *Subtarget) {
3478 bool isNeg = false;
3479 if (V < 0) {
3480 isNeg = true;
3481 V = - V;
3482 }
3483
3484 switch (VT.getSimpleVT().SimpleTy) {
3485 default: return false;
3486 case MVT::i1:
3487 case MVT::i8:
3488 case MVT::i16:
3489 case MVT::i32:
3490 // + imm12 or - imm8
3491 if (isNeg)
3492 return V == (V & ((1LL << 8) - 1));
3493 return V == (V & ((1LL << 12) - 1));
3494 case MVT::f32:
3495 case MVT::f64:
3496 // Same as ARM mode. FIXME: NEON?
3497 if (!Subtarget->hasVFP2())
3498 return false;
3499 if ((V & 3) != 0)
3500 return false;
3501 V >>= 2;
3502 return V == (V & ((1LL << 8) - 1));
3503 }
3504}
3505
Evan Chengb01fad62007-03-12 23:30:29 +00003506/// isLegalAddressImmediate - Return true if the integer value can be used
3507/// as the offset of the target addressing mode for load / store of the
3508/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00003509static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003510 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00003511 if (V == 0)
3512 return true;
3513
Evan Cheng65011532009-03-09 19:15:00 +00003514 if (!VT.isSimple())
3515 return false;
3516
Evan Chenge6c835f2009-08-14 20:09:37 +00003517 if (Subtarget->isThumb1Only())
3518 return isLegalT1AddressImmediate(V, VT);
3519 else if (Subtarget->isThumb2())
3520 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00003521
Evan Chenge6c835f2009-08-14 20:09:37 +00003522 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00003523 if (V < 0)
3524 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00003525 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00003526 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003527 case MVT::i1:
3528 case MVT::i8:
3529 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00003530 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003531 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003532 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00003533 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003534 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003535 case MVT::f32:
3536 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00003537 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00003538 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00003539 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00003540 return false;
3541 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003542 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00003543 }
Evan Chenga8e29892007-01-19 07:51:42 +00003544}
3545
Evan Chenge6c835f2009-08-14 20:09:37 +00003546bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
3547 EVT VT) const {
3548 int Scale = AM.Scale;
3549 if (Scale < 0)
3550 return false;
3551
3552 switch (VT.getSimpleVT().SimpleTy) {
3553 default: return false;
3554 case MVT::i1:
3555 case MVT::i8:
3556 case MVT::i16:
3557 case MVT::i32:
3558 if (Scale == 1)
3559 return true;
3560 // r + r << imm
3561 Scale = Scale & ~1;
3562 return Scale == 2 || Scale == 4 || Scale == 8;
3563 case MVT::i64:
3564 // r + r
3565 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
3566 return true;
3567 return false;
3568 case MVT::isVoid:
3569 // Note, we allow "void" uses (basically, uses that aren't loads or
3570 // stores), because arm allows folding a scale into many arithmetic
3571 // operations. This should be made more precise and revisited later.
3572
3573 // Allow r << imm, but the imm has to be a multiple of two.
3574 if (Scale & 1) return false;
3575 return isPowerOf2_32(Scale);
3576 }
3577}
3578
Chris Lattner37caf8c2007-04-09 23:33:39 +00003579/// isLegalAddressingMode - Return true if the addressing mode represented
3580/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003581bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003582 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00003583 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00003584 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00003585 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003586
Chris Lattner37caf8c2007-04-09 23:33:39 +00003587 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003588 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003589 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003590
Chris Lattner37caf8c2007-04-09 23:33:39 +00003591 switch (AM.Scale) {
3592 case 0: // no scale reg, must be "r+i" or "r", or "i".
3593 break;
3594 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00003595 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00003596 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003597 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00003598 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003599 // ARM doesn't support any R+R*scale+imm addr modes.
3600 if (AM.BaseOffs)
3601 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003602
Bob Wilson2c7dab12009-04-08 17:55:28 +00003603 if (!VT.isSimple())
3604 return false;
3605
Evan Chenge6c835f2009-08-14 20:09:37 +00003606 if (Subtarget->isThumb2())
3607 return isLegalT2ScaledAddressingMode(AM, VT);
3608
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003609 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00003610 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00003611 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003612 case MVT::i1:
3613 case MVT::i8:
3614 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003615 if (Scale < 0) Scale = -Scale;
3616 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003617 return true;
3618 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00003619 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003620 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00003621 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003622 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003623 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003624 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00003625 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003626
Owen Anderson825b72b2009-08-11 20:47:22 +00003627 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003628 // Note, we allow "void" uses (basically, uses that aren't loads or
3629 // stores), because arm allows folding a scale into many arithmetic
3630 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003631
Chris Lattner37caf8c2007-04-09 23:33:39 +00003632 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00003633 if (Scale & 1) return false;
3634 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00003635 }
3636 break;
Evan Chengb01fad62007-03-12 23:30:29 +00003637 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00003638 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00003639}
3640
Owen Andersone50ed302009-08-10 22:56:29 +00003641static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003642 bool isSEXTLoad, SDValue &Base,
3643 SDValue &Offset, bool &isInc,
3644 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00003645 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3646 return false;
3647
Owen Anderson825b72b2009-08-11 20:47:22 +00003648 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00003649 // AddressingMode 3
3650 Base = Ptr->getOperand(0);
3651 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003652 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003653 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003654 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003655 isInc = false;
3656 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3657 return true;
3658 }
3659 }
3660 isInc = (Ptr->getOpcode() == ISD::ADD);
3661 Offset = Ptr->getOperand(1);
3662 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00003663 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00003664 // AddressingMode 2
3665 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003666 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003667 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003668 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003669 isInc = false;
3670 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3671 Base = Ptr->getOperand(0);
3672 return true;
3673 }
3674 }
3675
3676 if (Ptr->getOpcode() == ISD::ADD) {
3677 isInc = true;
3678 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
3679 if (ShOpcVal != ARM_AM::no_shift) {
3680 Base = Ptr->getOperand(1);
3681 Offset = Ptr->getOperand(0);
3682 } else {
3683 Base = Ptr->getOperand(0);
3684 Offset = Ptr->getOperand(1);
3685 }
3686 return true;
3687 }
3688
3689 isInc = (Ptr->getOpcode() == ISD::ADD);
3690 Base = Ptr->getOperand(0);
3691 Offset = Ptr->getOperand(1);
3692 return true;
3693 }
3694
3695 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
3696 return false;
3697}
3698
Owen Andersone50ed302009-08-10 22:56:29 +00003699static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003700 bool isSEXTLoad, SDValue &Base,
3701 SDValue &Offset, bool &isInc,
3702 SelectionDAG &DAG) {
3703 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3704 return false;
3705
3706 Base = Ptr->getOperand(0);
3707 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
3708 int RHSC = (int)RHS->getZExtValue();
3709 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
3710 assert(Ptr->getOpcode() == ISD::ADD);
3711 isInc = false;
3712 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3713 return true;
3714 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
3715 isInc = Ptr->getOpcode() == ISD::ADD;
3716 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
3717 return true;
3718 }
3719 }
3720
3721 return false;
3722}
3723
Evan Chenga8e29892007-01-19 07:51:42 +00003724/// getPreIndexedAddressParts - returns true by value, base pointer and
3725/// offset pointer and addressing mode by reference if the node's address
3726/// can be legally represented as pre-indexed load / store address.
3727bool
Dan Gohman475871a2008-07-27 21:46:04 +00003728ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
3729 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003730 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003731 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003732 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003733 return false;
3734
Owen Andersone50ed302009-08-10 22:56:29 +00003735 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003736 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003737 bool isSEXTLoad = false;
3738 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
3739 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003740 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003741 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3742 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
3743 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003744 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003745 } else
3746 return false;
3747
3748 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003749 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003750 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003751 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
3752 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003753 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003754 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00003755 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00003756 if (!isLegal)
3757 return false;
3758
3759 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
3760 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003761}
3762
3763/// getPostIndexedAddressParts - returns true by value, base pointer and
3764/// offset pointer and addressing mode by reference if this node can be
3765/// combined with a load / store to form a post-indexed load / store.
3766bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00003767 SDValue &Base,
3768 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003769 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003770 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003771 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003772 return false;
3773
Owen Andersone50ed302009-08-10 22:56:29 +00003774 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003775 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003776 bool isSEXTLoad = false;
3777 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003778 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003779 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3780 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003781 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003782 } else
3783 return false;
3784
3785 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003786 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003787 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003788 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003789 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003790 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003791 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
3792 isInc, DAG);
3793 if (!isLegal)
3794 return false;
3795
3796 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
3797 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003798}
3799
Dan Gohman475871a2008-07-27 21:46:04 +00003800void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00003801 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003802 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003803 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00003804 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00003805 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003806 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003807 switch (Op.getOpcode()) {
3808 default: break;
3809 case ARMISD::CMOV: {
3810 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00003811 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003812 if (KnownZero == 0 && KnownOne == 0) return;
3813
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003814 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00003815 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
3816 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003817 KnownZero &= KnownZeroRHS;
3818 KnownOne &= KnownOneRHS;
3819 return;
3820 }
3821 }
3822}
3823
3824//===----------------------------------------------------------------------===//
3825// ARM Inline Assembly Support
3826//===----------------------------------------------------------------------===//
3827
3828/// getConstraintType - Given a constraint letter, return the type of
3829/// constraint it is for this target.
3830ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00003831ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
3832 if (Constraint.size() == 1) {
3833 switch (Constraint[0]) {
3834 default: break;
3835 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003836 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00003837 }
Evan Chenga8e29892007-01-19 07:51:42 +00003838 }
Chris Lattner4234f572007-03-25 02:14:49 +00003839 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00003840}
3841
Bob Wilson2dc4f542009-03-20 22:42:55 +00003842std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00003843ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003844 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003845 if (Constraint.size() == 1) {
3846 // GCC RS6000 Constraint Letters
3847 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003848 case 'l':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003849 if (Subtarget->isThumb1Only())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003850 return std::make_pair(0U, ARM::tGPRRegisterClass);
3851 else
3852 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003853 case 'r':
3854 return std::make_pair(0U, ARM::GPRRegisterClass);
3855 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003856 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003857 return std::make_pair(0U, ARM::SPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00003858 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003859 return std::make_pair(0U, ARM::DPRRegisterClass);
3860 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003861 }
3862 }
3863 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3864}
3865
3866std::vector<unsigned> ARMTargetLowering::
3867getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003868 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003869 if (Constraint.size() != 1)
3870 return std::vector<unsigned>();
3871
3872 switch (Constraint[0]) { // GCC ARM Constraint Letters
3873 default: break;
3874 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003875 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3876 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3877 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003878 case 'r':
3879 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3880 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3881 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
3882 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003883 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003884 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003885 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
3886 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
3887 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
3888 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
3889 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
3890 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
3891 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
3892 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00003893 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003894 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
3895 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
3896 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
3897 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
3898 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003899 }
3900
3901 return std::vector<unsigned>();
3902}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003903
3904/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3905/// vector. If it is invalid, don't add anything to Ops.
3906void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3907 char Constraint,
3908 bool hasMemory,
3909 std::vector<SDValue>&Ops,
3910 SelectionDAG &DAG) const {
3911 SDValue Result(0, 0);
3912
3913 switch (Constraint) {
3914 default: break;
3915 case 'I': case 'J': case 'K': case 'L':
3916 case 'M': case 'N': case 'O':
3917 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
3918 if (!C)
3919 return;
3920
3921 int64_t CVal64 = C->getSExtValue();
3922 int CVal = (int) CVal64;
3923 // None of these constraints allow values larger than 32 bits. Check
3924 // that the value fits in an int.
3925 if (CVal != CVal64)
3926 return;
3927
3928 switch (Constraint) {
3929 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003930 if (Subtarget->isThumb1Only()) {
3931 // This must be a constant between 0 and 255, for ADD
3932 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003933 if (CVal >= 0 && CVal <= 255)
3934 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003935 } else if (Subtarget->isThumb2()) {
3936 // A constant that can be used as an immediate value in a
3937 // data-processing instruction.
3938 if (ARM_AM::getT2SOImmVal(CVal) != -1)
3939 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003940 } else {
3941 // A constant that can be used as an immediate value in a
3942 // data-processing instruction.
3943 if (ARM_AM::getSOImmVal(CVal) != -1)
3944 break;
3945 }
3946 return;
3947
3948 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003949 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003950 // This must be a constant between -255 and -1, for negated ADD
3951 // immediates. This can be used in GCC with an "n" modifier that
3952 // prints the negated value, for use with SUB instructions. It is
3953 // not useful otherwise but is implemented for compatibility.
3954 if (CVal >= -255 && CVal <= -1)
3955 break;
3956 } else {
3957 // This must be a constant between -4095 and 4095. It is not clear
3958 // what this constraint is intended for. Implemented for
3959 // compatibility with GCC.
3960 if (CVal >= -4095 && CVal <= 4095)
3961 break;
3962 }
3963 return;
3964
3965 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003966 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003967 // A 32-bit value where only one byte has a nonzero value. Exclude
3968 // zero to match GCC. This constraint is used by GCC internally for
3969 // constants that can be loaded with a move/shift combination.
3970 // It is not useful otherwise but is implemented for compatibility.
3971 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
3972 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003973 } else if (Subtarget->isThumb2()) {
3974 // A constant whose bitwise inverse can be used as an immediate
3975 // value in a data-processing instruction. This can be used in GCC
3976 // with a "B" modifier that prints the inverted value, for use with
3977 // BIC and MVN instructions. It is not useful otherwise but is
3978 // implemented for compatibility.
3979 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
3980 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003981 } else {
3982 // A constant whose bitwise inverse can be used as an immediate
3983 // value in a data-processing instruction. This can be used in GCC
3984 // with a "B" modifier that prints the inverted value, for use with
3985 // BIC and MVN instructions. It is not useful otherwise but is
3986 // implemented for compatibility.
3987 if (ARM_AM::getSOImmVal(~CVal) != -1)
3988 break;
3989 }
3990 return;
3991
3992 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003993 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003994 // This must be a constant between -7 and 7,
3995 // for 3-operand ADD/SUB immediate instructions.
3996 if (CVal >= -7 && CVal < 7)
3997 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003998 } else if (Subtarget->isThumb2()) {
3999 // A constant whose negation can be used as an immediate value in a
4000 // data-processing instruction. This can be used in GCC with an "n"
4001 // modifier that prints the negated value, for use with SUB
4002 // instructions. It is not useful otherwise but is implemented for
4003 // compatibility.
4004 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
4005 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004006 } else {
4007 // A constant whose negation can be used as an immediate value in a
4008 // data-processing instruction. This can be used in GCC with an "n"
4009 // modifier that prints the negated value, for use with SUB
4010 // instructions. It is not useful otherwise but is implemented for
4011 // compatibility.
4012 if (ARM_AM::getSOImmVal(-CVal) != -1)
4013 break;
4014 }
4015 return;
4016
4017 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004018 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004019 // This must be a multiple of 4 between 0 and 1020, for
4020 // ADD sp + immediate.
4021 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
4022 break;
4023 } else {
4024 // A power of two or a constant between 0 and 32. This is used in
4025 // GCC for the shift amount on shifted register operands, but it is
4026 // useful in general for any shift amounts.
4027 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
4028 break;
4029 }
4030 return;
4031
4032 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004033 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004034 // This must be a constant between 0 and 31, for shift amounts.
4035 if (CVal >= 0 && CVal <= 31)
4036 break;
4037 }
4038 return;
4039
4040 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004041 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004042 // This must be a multiple of 4 between -508 and 508, for
4043 // ADD/SUB sp = sp + immediate.
4044 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
4045 break;
4046 }
4047 return;
4048 }
4049 Result = DAG.getTargetConstant(CVal, Op.getValueType());
4050 break;
4051 }
4052
4053 if (Result.getNode()) {
4054 Ops.push_back(Result);
4055 return;
4056 }
4057 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
4058 Ops, DAG);
4059}