blob: b8126a3c5d1801196cb17d3e3124954fbc843df2 [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"
Benjamin Kramer174101e2009-10-20 11:44:38 +000028#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000029#include "llvm/Instruction.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000030#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000031#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000032#include "llvm/CodeGen/CallingConvLower.h"
Evan Chenga8e29892007-01-19 07:51:42 +000033#include "llvm/CodeGen/MachineBasicBlock.h"
34#include "llvm/CodeGen/MachineFrameInfo.h"
35#include "llvm/CodeGen/MachineFunction.h"
36#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000039#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000040#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000041#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000042#include "llvm/ADT/VectorExtras.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000043#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000044#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000045#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000046#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000047#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000048using namespace llvm;
49
Jim Grosbache7b52522010-04-14 22:28:31 +000050static cl::opt<bool>
51EnableARMLongCalls("arm-long-calls", cl::Hidden,
52 cl::desc("Generate calls via indirect call instructions."),
53 cl::init(false));
54
Owen Andersone50ed302009-08-10 22:56:29 +000055static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000056 CCValAssign::LocInfo &LocInfo,
57 ISD::ArgFlagsTy &ArgFlags,
58 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000059static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000060 CCValAssign::LocInfo &LocInfo,
61 ISD::ArgFlagsTy &ArgFlags,
62 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000063static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000064 CCValAssign::LocInfo &LocInfo,
65 ISD::ArgFlagsTy &ArgFlags,
66 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000067static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000068 CCValAssign::LocInfo &LocInfo,
69 ISD::ArgFlagsTy &ArgFlags,
70 CCState &State);
71
Owen Andersone50ed302009-08-10 22:56:29 +000072void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
73 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000074 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000075 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000076 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
77 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000078
Owen Anderson70671842009-08-10 20:18:46 +000079 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000080 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000081 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000082 }
83
Owen Andersone50ed302009-08-10 22:56:29 +000084 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +000085 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +000086 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +000087 if (ElemTy == MVT::i8 || ElemTy == MVT::i16)
Owen Anderson70671842009-08-10 20:18:46 +000088 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +000089 if (ElemTy != MVT::i32) {
90 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
91 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
92 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
93 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
94 }
Owen Anderson70671842009-08-10 20:18:46 +000095 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
96 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Evan Chengde8aa4e2010-05-05 18:28:36 +000097 if (llvm::ModelWithRegSequence())
98 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
99 else
100 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Custom);
Anton Korobeynikov8e6c2b92009-08-21 12:40:35 +0000101 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
Bob Wilsond0910c42010-04-06 22:02:24 +0000102 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
103 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000104 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000105 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
106 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
107 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000108 }
109
110 // Promote all bit-wise operations.
111 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000112 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000113 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
114 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000115 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000116 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000117 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000118 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000119 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000120 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000121 }
Bob Wilson16330762009-09-16 00:17:28 +0000122
123 // Neon does not support vector divide/remainder operations.
124 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
125 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
126 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
127 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
128 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
129 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000130}
131
Owen Andersone50ed302009-08-10 22:56:29 +0000132void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000133 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000135}
136
Owen Andersone50ed302009-08-10 22:56:29 +0000137void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000138 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000140}
141
Chris Lattnerf0144122009-07-28 03:13:23 +0000142static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
143 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000144 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000145
Chris Lattner80ec2792009-08-02 00:34:36 +0000146 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000147}
148
Evan Chenga8e29892007-01-19 07:51:42 +0000149ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000150 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000151 Subtarget = &TM.getSubtarget<ARMSubtarget>();
152
Evan Chengb1df8f22007-04-27 08:15:43 +0000153 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000154 // Uses VFP for Thumb libfuncs if available.
155 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
156 // Single-precision floating-point arithmetic.
157 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
158 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
159 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
160 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000161
Evan Chengb1df8f22007-04-27 08:15:43 +0000162 // Double-precision floating-point arithmetic.
163 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
164 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
165 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
166 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000167
Evan Chengb1df8f22007-04-27 08:15:43 +0000168 // Single-precision comparisons.
169 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
170 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
171 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
172 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
173 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
174 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
175 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
176 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000177
Evan Chengb1df8f22007-04-27 08:15:43 +0000178 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
179 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
180 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
181 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
182 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
183 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
184 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
185 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000186
Evan Chengb1df8f22007-04-27 08:15:43 +0000187 // Double-precision comparisons.
188 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
189 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
190 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
191 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
192 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
193 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
194 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
195 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000196
Evan Chengb1df8f22007-04-27 08:15:43 +0000197 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
198 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
199 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
200 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
201 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
202 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
203 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
204 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000205
Evan Chengb1df8f22007-04-27 08:15:43 +0000206 // Floating-point to integer conversions.
207 // i64 conversions are done via library routines even when generating VFP
208 // instructions, so use the same ones.
209 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
210 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
211 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
212 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000213
Evan Chengb1df8f22007-04-27 08:15:43 +0000214 // Conversions between floating types.
215 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
216 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
217
218 // Integer to floating-point conversions.
219 // i64 conversions are done via library routines even when generating VFP
220 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000221 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
222 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000223 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
224 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
225 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
226 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
227 }
Evan Chenga8e29892007-01-19 07:51:42 +0000228 }
229
Bob Wilson2f954612009-05-22 17:38:41 +0000230 // These libcalls are not available in 32-bit.
231 setLibcallName(RTLIB::SHL_I128, 0);
232 setLibcallName(RTLIB::SRL_I128, 0);
233 setLibcallName(RTLIB::SRA_I128, 0);
234
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000235 // Libcalls should use the AAPCS base standard ABI, even if hard float
236 // is in effect, as per the ARM RTABI specification, section 4.1.2.
237 if (Subtarget->isAAPCS_ABI()) {
238 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
239 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
240 CallingConv::ARM_AAPCS);
241 }
242 }
243
David Goodwinf1daf7d2009-07-08 23:10:31 +0000244 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000245 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000246 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000247 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000248 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
250 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000251
Owen Anderson825b72b2009-08-11 20:47:22 +0000252 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000253 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000254
255 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000256 addDRTypeForNEON(MVT::v2f32);
257 addDRTypeForNEON(MVT::v8i8);
258 addDRTypeForNEON(MVT::v4i16);
259 addDRTypeForNEON(MVT::v2i32);
260 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000261
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 addQRTypeForNEON(MVT::v4f32);
263 addQRTypeForNEON(MVT::v2f64);
264 addQRTypeForNEON(MVT::v16i8);
265 addQRTypeForNEON(MVT::v8i16);
266 addQRTypeForNEON(MVT::v4i32);
267 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000268
Bob Wilson74dc72e2009-09-15 23:55:57 +0000269 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
270 // neither Neon nor VFP support any arithmetic operations on it.
271 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
272 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
273 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
274 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
275 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
276 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
277 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
278 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
279 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
280 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
281 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
282 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
283 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
284 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
285 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
286 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
287 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
288 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
289 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
290 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
291 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
292 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
293 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
294 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
295
Bob Wilson642b3292009-09-16 00:32:15 +0000296 // Neon does not support some operations on v1i64 and v2i64 types.
297 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
298 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
299 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
300 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
301
Bob Wilson5bafff32009-06-22 23:27:02 +0000302 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
303 setTargetDAGCombine(ISD::SHL);
304 setTargetDAGCombine(ISD::SRL);
305 setTargetDAGCombine(ISD::SRA);
306 setTargetDAGCombine(ISD::SIGN_EXTEND);
307 setTargetDAGCombine(ISD::ZERO_EXTEND);
308 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000309 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson5bafff32009-06-22 23:27:02 +0000310 }
311
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000312 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000313
314 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000315 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000316
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000317 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000319
Evan Chenga8e29892007-01-19 07:51:42 +0000320 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000321 if (!Subtarget->isThumb1Only()) {
322 for (unsigned im = (unsigned)ISD::PRE_INC;
323 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setIndexedLoadAction(im, MVT::i1, Legal);
325 setIndexedLoadAction(im, MVT::i8, Legal);
326 setIndexedLoadAction(im, MVT::i16, Legal);
327 setIndexedLoadAction(im, MVT::i32, Legal);
328 setIndexedStoreAction(im, MVT::i1, Legal);
329 setIndexedStoreAction(im, MVT::i8, Legal);
330 setIndexedStoreAction(im, MVT::i16, Legal);
331 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000332 }
Evan Chenga8e29892007-01-19 07:51:42 +0000333 }
334
335 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000336 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 setOperationAction(ISD::MUL, MVT::i64, Expand);
338 setOperationAction(ISD::MULHU, MVT::i32, Expand);
339 setOperationAction(ISD::MULHS, MVT::i32, Expand);
340 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
341 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000342 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 setOperationAction(ISD::MUL, MVT::i64, Expand);
344 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000345 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000346 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000347 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000348 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000349 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000350 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000351 setOperationAction(ISD::SRL, MVT::i64, Custom);
352 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000353
354 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000356 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000357 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000358 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000359 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000360
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000361 // Only ARMv6 has BSWAP.
362 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000363 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000364
Evan Chenga8e29892007-01-19 07:51:42 +0000365 // These are expanded into libcalls.
Jim Grosbach29402132010-05-05 23:44:43 +0000366 if (!Subtarget->hasDivide()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000367 // v7M has a hardware divider
368 setOperationAction(ISD::SDIV, MVT::i32, Expand);
369 setOperationAction(ISD::UDIV, MVT::i32, Expand);
370 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 setOperationAction(ISD::SREM, MVT::i32, Expand);
372 setOperationAction(ISD::UREM, MVT::i32, Expand);
373 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
374 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000375
Owen Anderson825b72b2009-08-11 20:47:22 +0000376 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
377 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
378 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
379 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000380 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000381
Evan Chengfb3611d2010-05-11 07:26:32 +0000382 setOperationAction(ISD::TRAP, MVT::Other, Legal);
383
Evan Chenga8e29892007-01-19 07:51:42 +0000384 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 setOperationAction(ISD::VASTART, MVT::Other, Custom);
386 setOperationAction(ISD::VAARG, MVT::Other, Expand);
387 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
388 setOperationAction(ISD::VAEND, MVT::Other, Expand);
389 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
390 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000391 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
392 // FIXME: Shouldn't need this, since no register is used, but the legalizer
393 // doesn't yet know how to not do that for SjLj.
394 setExceptionSelectorRegister(ARM::R0);
Evan Cheng3a1588a2010-04-15 22:20:34 +0000395 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Jim Grosbach3728e962009-12-10 00:11:09 +0000396 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000397
Jim Grosbach4b77f6a2010-05-07 18:34:55 +0000398 // If the subtarget does not have extract instructions, sign_extend_inreg
399 // needs to be expanded. Extract is available in ARM mode on v6 and up,
400 // and on most Thumb2 implementations.
401 if ((!Subtarget->isThumb() && !Subtarget->hasV6Ops())
402 || (Subtarget->isThumb2() && !Subtarget->hasT2ExtractPack())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000403 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
404 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000405 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000406 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000407
David Goodwinf1daf7d2009-07-08 23:10:31 +0000408 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000409 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
410 // iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000412
413 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +0000415 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
Jim Grosbach5eb19512010-05-22 01:06:18 +0000416 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000417
Owen Anderson825b72b2009-08-11 20:47:22 +0000418 setOperationAction(ISD::SETCC, MVT::i32, Expand);
419 setOperationAction(ISD::SETCC, MVT::f32, Expand);
420 setOperationAction(ISD::SETCC, MVT::f64, Expand);
421 setOperationAction(ISD::SELECT, MVT::i32, Expand);
422 setOperationAction(ISD::SELECT, MVT::f32, Expand);
423 setOperationAction(ISD::SELECT, MVT::f64, Expand);
424 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
425 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
426 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000427
Owen Anderson825b72b2009-08-11 20:47:22 +0000428 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
429 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
430 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
431 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
432 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000433
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000434 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000435 setOperationAction(ISD::FSIN, MVT::f64, Expand);
436 setOperationAction(ISD::FSIN, MVT::f32, Expand);
437 setOperationAction(ISD::FCOS, MVT::f32, Expand);
438 setOperationAction(ISD::FCOS, MVT::f64, Expand);
439 setOperationAction(ISD::FREM, MVT::f64, Expand);
440 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000441 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000442 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
443 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000444 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000445 setOperationAction(ISD::FPOW, MVT::f64, Expand);
446 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000447
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000448 // Various VFP goodness
449 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000450 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
451 if (Subtarget->hasVFP2()) {
452 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
453 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
454 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
455 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
456 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000457 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000458 if (!Subtarget->hasFP16()) {
459 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
460 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000461 }
Evan Cheng110cf482008-04-01 01:50:16 +0000462 }
Evan Chenga8e29892007-01-19 07:51:42 +0000463
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000464 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000465 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000466 setTargetDAGCombine(ISD::ADD);
467 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000468 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000469
Evan Chenga8e29892007-01-19 07:51:42 +0000470 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000471
Evan Chengf7d87ee2010-05-21 00:43:17 +0000472 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
473 setSchedulingPreference(Sched::RegPressure);
474 else
475 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000476
Evan Chengbc9b7542009-08-15 07:59:10 +0000477 // FIXME: If-converter should use instruction latency to determine
478 // profitability rather than relying on fixed limits.
479 if (Subtarget->getCPUString() == "generic") {
480 // Generic (and overly aggressive) if-conversion limits.
481 setIfCvtBlockSizeLimit(10);
482 setIfCvtDupBlockSizeLimit(2);
Jim Grosbach35075a72010-03-24 16:15:14 +0000483 } else if (Subtarget->hasV7Ops()) {
Jim Grosbachfceabef2010-03-24 00:03:13 +0000484 setIfCvtBlockSizeLimit(3);
485 setIfCvtDupBlockSizeLimit(1);
Evan Chengbc9b7542009-08-15 07:59:10 +0000486 } else if (Subtarget->hasV6Ops()) {
487 setIfCvtBlockSizeLimit(2);
488 setIfCvtDupBlockSizeLimit(1);
489 } else {
490 setIfCvtBlockSizeLimit(3);
491 setIfCvtDupBlockSizeLimit(2);
Evan Cheng8557c2b2009-06-19 01:51:50 +0000492 }
493
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000494 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Bob Wilsone6abdff2009-05-18 20:55:32 +0000495 // Do not enable CodePlacementOpt for now: it currently runs after the
496 // ARMConstantIslandPass and messes up branch relaxation and placement
497 // of constant islands.
498 // benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000499}
500
Evan Chenga8e29892007-01-19 07:51:42 +0000501const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
502 switch (Opcode) {
503 default: return 0;
504 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000505 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
506 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000507 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000508 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
509 case ARMISD::tCALL: return "ARMISD::tCALL";
510 case ARMISD::BRCOND: return "ARMISD::BRCOND";
511 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000512 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000513 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
514 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
515 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000516 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000517 case ARMISD::CMPFP: return "ARMISD::CMPFP";
518 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
519 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
520 case ARMISD::CMOV: return "ARMISD::CMOV";
521 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000522
Jim Grosbach3482c802010-01-18 19:58:49 +0000523 case ARMISD::RBIT: return "ARMISD::RBIT";
524
Bob Wilson76a312b2010-03-19 22:51:32 +0000525 case ARMISD::FTOSI: return "ARMISD::FTOSI";
526 case ARMISD::FTOUI: return "ARMISD::FTOUI";
527 case ARMISD::SITOF: return "ARMISD::SITOF";
528 case ARMISD::UITOF: return "ARMISD::UITOF";
529
Evan Chenga8e29892007-01-19 07:51:42 +0000530 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
531 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
532 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000533
Jim Grosbache5165492009-11-09 00:11:35 +0000534 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
535 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000536
Evan Chengc5942082009-10-28 06:55:03 +0000537 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
538 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
539
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000540 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000541
Evan Cheng86198642009-08-07 00:34:42 +0000542 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
543
Jim Grosbach3728e962009-12-10 00:11:09 +0000544 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
545 case ARMISD::SYNCBARRIER: return "ARMISD::SYNCBARRIER";
546
Bob Wilson5bafff32009-06-22 23:27:02 +0000547 case ARMISD::VCEQ: return "ARMISD::VCEQ";
548 case ARMISD::VCGE: return "ARMISD::VCGE";
549 case ARMISD::VCGEU: return "ARMISD::VCGEU";
550 case ARMISD::VCGT: return "ARMISD::VCGT";
551 case ARMISD::VCGTU: return "ARMISD::VCGTU";
552 case ARMISD::VTST: return "ARMISD::VTST";
553
554 case ARMISD::VSHL: return "ARMISD::VSHL";
555 case ARMISD::VSHRs: return "ARMISD::VSHRs";
556 case ARMISD::VSHRu: return "ARMISD::VSHRu";
557 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
558 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
559 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
560 case ARMISD::VSHRN: return "ARMISD::VSHRN";
561 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
562 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
563 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
564 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
565 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
566 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
567 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
568 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
569 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
570 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
571 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
572 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
573 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
574 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000575 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000576 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000577 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000578 case ARMISD::VREV64: return "ARMISD::VREV64";
579 case ARMISD::VREV32: return "ARMISD::VREV32";
580 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000581 case ARMISD::VZIP: return "ARMISD::VZIP";
582 case ARMISD::VUZP: return "ARMISD::VUZP";
583 case ARMISD::VTRN: return "ARMISD::VTRN";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000584 case ARMISD::FMAX: return "ARMISD::FMAX";
585 case ARMISD::FMIN: return "ARMISD::FMIN";
Evan Chenga8e29892007-01-19 07:51:42 +0000586 }
587}
588
Evan Cheng06b666c2010-05-15 02:18:07 +0000589/// getRegClassFor - Return the register class that should be used for the
590/// specified value type.
591TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
592 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
593 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
594 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000595 if (Subtarget->hasNEON()) {
596 if (VT == MVT::v4i64)
597 return ARM::QQPRRegisterClass;
598 else if (VT == MVT::v8i64)
599 return ARM::QQQQPRRegisterClass;
600 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000601 return TargetLowering::getRegClassFor(VT);
602}
603
Bill Wendlingb4202b82009-07-01 18:50:55 +0000604/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000605unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Evan Cheng048e36f2009-10-02 06:57:25 +0000606 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 0 : 1;
Bill Wendling20c568f2009-06-30 22:38:32 +0000607}
608
Evan Cheng1cc39842010-05-20 23:26:43 +0000609Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
610 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
611 EVT VT = N->getValueType(i);
612 if (VT.isFloatingPoint() || VT.isVector())
613 return Sched::Latency;
614 }
615 return Sched::RegPressure;
616}
617
Evan Chenga8e29892007-01-19 07:51:42 +0000618//===----------------------------------------------------------------------===//
619// Lowering Code
620//===----------------------------------------------------------------------===//
621
Evan Chenga8e29892007-01-19 07:51:42 +0000622/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
623static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
624 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000625 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000626 case ISD::SETNE: return ARMCC::NE;
627 case ISD::SETEQ: return ARMCC::EQ;
628 case ISD::SETGT: return ARMCC::GT;
629 case ISD::SETGE: return ARMCC::GE;
630 case ISD::SETLT: return ARMCC::LT;
631 case ISD::SETLE: return ARMCC::LE;
632 case ISD::SETUGT: return ARMCC::HI;
633 case ISD::SETUGE: return ARMCC::HS;
634 case ISD::SETULT: return ARMCC::LO;
635 case ISD::SETULE: return ARMCC::LS;
636 }
637}
638
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000639/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
640static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000641 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000642 CondCode2 = ARMCC::AL;
643 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000644 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000645 case ISD::SETEQ:
646 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
647 case ISD::SETGT:
648 case ISD::SETOGT: CondCode = ARMCC::GT; break;
649 case ISD::SETGE:
650 case ISD::SETOGE: CondCode = ARMCC::GE; break;
651 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000652 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000653 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
654 case ISD::SETO: CondCode = ARMCC::VC; break;
655 case ISD::SETUO: CondCode = ARMCC::VS; break;
656 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
657 case ISD::SETUGT: CondCode = ARMCC::HI; break;
658 case ISD::SETUGE: CondCode = ARMCC::PL; break;
659 case ISD::SETLT:
660 case ISD::SETULT: CondCode = ARMCC::LT; break;
661 case ISD::SETLE:
662 case ISD::SETULE: CondCode = ARMCC::LE; break;
663 case ISD::SETNE:
664 case ISD::SETUNE: CondCode = ARMCC::NE; break;
665 }
Evan Chenga8e29892007-01-19 07:51:42 +0000666}
667
Bob Wilson1f595bb2009-04-17 19:07:39 +0000668//===----------------------------------------------------------------------===//
669// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000670//===----------------------------------------------------------------------===//
671
672#include "ARMGenCallingConv.inc"
673
674// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000675static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000676 CCValAssign::LocInfo &LocInfo,
677 CCState &State, bool CanFail) {
678 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
679
680 // Try to get the first register.
681 if (unsigned Reg = State.AllocateReg(RegList, 4))
682 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
683 else {
684 // For the 2nd half of a v2f64, do not fail.
685 if (CanFail)
686 return false;
687
688 // Put the whole thing on the stack.
689 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
690 State.AllocateStack(8, 4),
691 LocVT, LocInfo));
692 return true;
693 }
694
695 // Try to get the second register.
696 if (unsigned Reg = State.AllocateReg(RegList, 4))
697 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
698 else
699 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
700 State.AllocateStack(4, 4),
701 LocVT, LocInfo));
702 return true;
703}
704
Owen Andersone50ed302009-08-10 22:56:29 +0000705static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000706 CCValAssign::LocInfo &LocInfo,
707 ISD::ArgFlagsTy &ArgFlags,
708 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000709 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
710 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000711 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000712 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
713 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000714 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000715}
716
717// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000718static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000719 CCValAssign::LocInfo &LocInfo,
720 CCState &State, bool CanFail) {
721 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
722 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
723
724 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
725 if (Reg == 0) {
726 // For the 2nd half of a v2f64, do not just fail.
727 if (CanFail)
728 return false;
729
730 // Put the whole thing on the stack.
731 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
732 State.AllocateStack(8, 8),
733 LocVT, LocInfo));
734 return true;
735 }
736
737 unsigned i;
738 for (i = 0; i < 2; ++i)
739 if (HiRegList[i] == Reg)
740 break;
741
742 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
743 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
744 LocVT, LocInfo));
745 return true;
746}
747
Owen Andersone50ed302009-08-10 22:56:29 +0000748static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000749 CCValAssign::LocInfo &LocInfo,
750 ISD::ArgFlagsTy &ArgFlags,
751 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000752 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
753 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000754 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000755 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
756 return false;
757 return true; // we handled it
758}
759
Owen Andersone50ed302009-08-10 22:56:29 +0000760static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000761 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000762 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
763 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
764
Bob Wilsone65586b2009-04-17 20:40:45 +0000765 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
766 if (Reg == 0)
767 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000768
Bob Wilsone65586b2009-04-17 20:40:45 +0000769 unsigned i;
770 for (i = 0; i < 2; ++i)
771 if (HiRegList[i] == Reg)
772 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000773
Bob Wilson5bafff32009-06-22 23:27:02 +0000774 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000775 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000776 LocVT, LocInfo));
777 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000778}
779
Owen Andersone50ed302009-08-10 22:56:29 +0000780static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000781 CCValAssign::LocInfo &LocInfo,
782 ISD::ArgFlagsTy &ArgFlags,
783 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000784 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
785 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000786 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000787 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000788 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000789}
790
Owen Andersone50ed302009-08-10 22:56:29 +0000791static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000792 CCValAssign::LocInfo &LocInfo,
793 ISD::ArgFlagsTy &ArgFlags,
794 CCState &State) {
795 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
796 State);
797}
798
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000799/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
800/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000801CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000802 bool Return,
803 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000804 switch (CC) {
805 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000806 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000807 case CallingConv::C:
808 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000809 // Use target triple & subtarget features to do actual dispatch.
810 if (Subtarget->isAAPCS_ABI()) {
811 if (Subtarget->hasVFP2() &&
812 FloatABIType == FloatABI::Hard && !isVarArg)
813 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
814 else
815 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
816 } else
817 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000818 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000819 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000820 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000821 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000822 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000823 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000824 }
825}
826
Dan Gohman98ca4f22009-08-05 01:29:28 +0000827/// LowerCallResult - Lower the result values of a call into the
828/// appropriate copies out of appropriate physical registers.
829SDValue
830ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000831 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000832 const SmallVectorImpl<ISD::InputArg> &Ins,
833 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000834 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000835
Bob Wilson1f595bb2009-04-17 19:07:39 +0000836 // Assign locations to each value returned by this call.
837 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000838 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000839 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000840 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000841 CCAssignFnForNode(CallConv, /* Return*/ true,
842 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000843
844 // Copy all of the result registers out of their specified physreg.
845 for (unsigned i = 0; i != RVLocs.size(); ++i) {
846 CCValAssign VA = RVLocs[i];
847
Bob Wilson80915242009-04-25 00:33:20 +0000848 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000849 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000850 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000851 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000852 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000853 Chain = Lo.getValue(1);
854 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000855 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000856 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000857 InFlag);
858 Chain = Hi.getValue(1);
859 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000860 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000861
Owen Anderson825b72b2009-08-11 20:47:22 +0000862 if (VA.getLocVT() == MVT::v2f64) {
863 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
864 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
865 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000866
867 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000868 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000869 Chain = Lo.getValue(1);
870 InFlag = Lo.getValue(2);
871 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000872 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000873 Chain = Hi.getValue(1);
874 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000875 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +0000876 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
877 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000878 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000879 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000880 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
881 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000882 Chain = Val.getValue(1);
883 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000884 }
Bob Wilson80915242009-04-25 00:33:20 +0000885
886 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000887 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000888 case CCValAssign::Full: break;
889 case CCValAssign::BCvt:
890 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
891 break;
892 }
893
Dan Gohman98ca4f22009-08-05 01:29:28 +0000894 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000895 }
896
Dan Gohman98ca4f22009-08-05 01:29:28 +0000897 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000898}
899
900/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
901/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000902/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000903/// a byval function parameter.
904/// Sometimes what we are copying is the end of a larger object, the part that
905/// does not fit in registers.
906static SDValue
907CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
908 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
909 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000910 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000911 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +0000912 /*isVolatile=*/false, /*AlwaysInline=*/false,
913 NULL, 0, NULL, 0);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000914}
915
Bob Wilsondee46d72009-04-17 20:35:10 +0000916/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000917SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000918ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
919 SDValue StackPtr, SDValue Arg,
920 DebugLoc dl, SelectionDAG &DAG,
921 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +0000922 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000923 unsigned LocMemOffset = VA.getLocMemOffset();
924 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
925 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
926 if (Flags.isByVal()) {
927 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
928 }
929 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene1b58cab2010-02-15 16:55:24 +0000930 PseudoSourceValue::getStack(), LocMemOffset,
931 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +0000932}
933
Dan Gohman98ca4f22009-08-05 01:29:28 +0000934void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000935 SDValue Chain, SDValue &Arg,
936 RegsToPassVector &RegsToPass,
937 CCValAssign &VA, CCValAssign &NextVA,
938 SDValue &StackPtr,
939 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +0000940 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +0000941
Jim Grosbache5165492009-11-09 00:11:35 +0000942 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000943 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000944 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
945
946 if (NextVA.isRegLoc())
947 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
948 else {
949 assert(NextVA.isMemLoc());
950 if (StackPtr.getNode() == 0)
951 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
952
Dan Gohman98ca4f22009-08-05 01:29:28 +0000953 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
954 dl, DAG, NextVA,
955 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000956 }
957}
958
Dan Gohman98ca4f22009-08-05 01:29:28 +0000959/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000960/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
961/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000962SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000963ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000964 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000965 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000966 const SmallVectorImpl<ISD::OutputArg> &Outs,
967 const SmallVectorImpl<ISD::InputArg> &Ins,
968 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000969 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +0000970 // ARM target does not yet support tail call optimization.
971 isTailCall = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000972
Bob Wilson1f595bb2009-04-17 19:07:39 +0000973 // Analyze operands of the call, assigning locations to each operand.
974 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000975 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
976 *DAG.getContext());
977 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000978 CCAssignFnForNode(CallConv, /* Return*/ false,
979 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +0000980
Bob Wilson1f595bb2009-04-17 19:07:39 +0000981 // Get a count of how many bytes are to be pushed on the stack.
982 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000983
984 // Adjust the stack pointer for the new arguments...
985 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000986 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000987
Jim Grosbachf9a4b762010-02-24 01:43:03 +0000988 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000989
Bob Wilson5bafff32009-06-22 23:27:02 +0000990 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000991 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000992
Bob Wilson1f595bb2009-04-17 19:07:39 +0000993 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +0000994 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000995 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
996 i != e;
997 ++i, ++realArgIdx) {
998 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000999 SDValue Arg = Outs[realArgIdx].Val;
1000 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +00001001
Bob Wilson1f595bb2009-04-17 19:07:39 +00001002 // Promote the value if needed.
1003 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001004 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001005 case CCValAssign::Full: break;
1006 case CCValAssign::SExt:
1007 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1008 break;
1009 case CCValAssign::ZExt:
1010 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1011 break;
1012 case CCValAssign::AExt:
1013 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1014 break;
1015 case CCValAssign::BCvt:
1016 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1017 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001018 }
1019
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001020 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001021 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001022 if (VA.getLocVT() == MVT::v2f64) {
1023 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1024 DAG.getConstant(0, MVT::i32));
1025 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1026 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001027
Dan Gohman98ca4f22009-08-05 01:29:28 +00001028 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001029 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1030
1031 VA = ArgLocs[++i]; // skip ahead to next loc
1032 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001033 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001034 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1035 } else {
1036 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001037
Dan Gohman98ca4f22009-08-05 01:29:28 +00001038 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1039 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001040 }
1041 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001042 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001043 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001044 }
1045 } else if (VA.isRegLoc()) {
1046 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1047 } else {
1048 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001049
Dan Gohman98ca4f22009-08-05 01:29:28 +00001050 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1051 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001052 }
Evan Chenga8e29892007-01-19 07:51:42 +00001053 }
1054
1055 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001056 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001057 &MemOpChains[0], MemOpChains.size());
1058
1059 // Build a sequence of copy-to-reg nodes chained together with token chain
1060 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001061 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +00001062 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001063 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001064 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +00001065 InFlag = Chain.getValue(1);
1066 }
1067
Bill Wendling056292f2008-09-16 21:48:12 +00001068 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1069 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1070 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001071 bool isDirect = false;
1072 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001073 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001074 MachineFunction &MF = DAG.getMachineFunction();
1075 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001076
1077 if (EnableARMLongCalls) {
1078 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1079 && "long-calls with non-static relocation model!");
1080 // Handle a global address or an external symbol. If it's not one of
1081 // those, the target's already in a register, so we don't need to do
1082 // anything extra.
1083 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001084 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001085 // Create a constant pool entry for the callee address
1086 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1087 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1088 ARMPCLabelIndex,
1089 ARMCP::CPValue, 0);
1090 // Get the address of the callee into a register
1091 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1092 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1093 Callee = DAG.getLoad(getPointerTy(), dl,
1094 DAG.getEntryNode(), CPAddr,
1095 PseudoSourceValue::getConstantPool(), 0,
1096 false, false, 0);
1097 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1098 const char *Sym = S->getSymbol();
1099
1100 // Create a constant pool entry for the callee address
1101 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1102 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1103 Sym, ARMPCLabelIndex, 0);
1104 // Get the address of the callee into a register
1105 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1106 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1107 Callee = DAG.getLoad(getPointerTy(), dl,
1108 DAG.getEntryNode(), CPAddr,
1109 PseudoSourceValue::getConstantPool(), 0,
1110 false, false, 0);
1111 }
1112 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001113 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001114 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001115 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001116 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001117 getTargetMachine().getRelocationModel() != Reloc::Static;
1118 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001119 // ARM call to a local ARM function is predicable.
1120 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +00001121 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001122 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001123 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001124 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001125 ARMPCLabelIndex,
1126 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001127 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001128 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001129 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001130 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001131 PseudoSourceValue::getConstantPool(), 0,
1132 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001133 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001134 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001135 getPointerTy(), Callee, PICLabel);
Jim Grosbache7b52522010-04-14 22:28:31 +00001136 } else
Evan Chengc60e76d2007-01-30 20:37:08 +00001137 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001138 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001139 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001140 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001141 getTargetMachine().getRelocationModel() != Reloc::Static;
1142 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001143 // tBX takes a register source operand.
1144 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001145 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001146 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001147 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001148 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001149 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001150 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001151 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001152 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001153 PseudoSourceValue::getConstantPool(), 0,
1154 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001155 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001156 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001157 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001158 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001159 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001160 }
1161
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001162 // FIXME: handle tail calls differently.
1163 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001164 if (Subtarget->isThumb()) {
1165 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001166 CallOpc = ARMISD::CALL_NOLINK;
1167 else
1168 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1169 } else {
1170 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001171 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1172 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001173 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001174 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001175 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001176 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001177 InFlag = Chain.getValue(1);
1178 }
1179
Dan Gohman475871a2008-07-27 21:46:04 +00001180 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001181 Ops.push_back(Chain);
1182 Ops.push_back(Callee);
1183
1184 // Add argument registers to the end of the list so that they are known live
1185 // into the call.
1186 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1187 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1188 RegsToPass[i].second.getValueType()));
1189
Gabor Greifba36cb52008-08-28 21:40:38 +00001190 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001191 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +00001192 // Returns a chain and a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00001193 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +00001194 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001195 InFlag = Chain.getValue(1);
1196
Chris Lattnere563bbc2008-10-11 22:08:30 +00001197 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1198 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001199 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001200 InFlag = Chain.getValue(1);
1201
Bob Wilson1f595bb2009-04-17 19:07:39 +00001202 // Handle result values, copying them out of physregs into vregs that we
1203 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001204 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1205 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001206}
1207
Dan Gohman98ca4f22009-08-05 01:29:28 +00001208SDValue
1209ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001210 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001211 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmand858e902010-04-17 15:26:15 +00001212 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001213
Bob Wilsondee46d72009-04-17 20:35:10 +00001214 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001215 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001216
Bob Wilsondee46d72009-04-17 20:35:10 +00001217 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001218 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1219 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001220
Dan Gohman98ca4f22009-08-05 01:29:28 +00001221 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001222 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1223 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001224
1225 // If this is the first return lowered for this function, add
1226 // the regs to the liveout set for the function.
1227 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1228 for (unsigned i = 0; i != RVLocs.size(); ++i)
1229 if (RVLocs[i].isRegLoc())
1230 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001231 }
1232
Bob Wilson1f595bb2009-04-17 19:07:39 +00001233 SDValue Flag;
1234
1235 // Copy the result values into the output registers.
1236 for (unsigned i = 0, realRVLocIdx = 0;
1237 i != RVLocs.size();
1238 ++i, ++realRVLocIdx) {
1239 CCValAssign &VA = RVLocs[i];
1240 assert(VA.isRegLoc() && "Can only return in registers!");
1241
Dan Gohman98ca4f22009-08-05 01:29:28 +00001242 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001243
1244 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001245 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001246 case CCValAssign::Full: break;
1247 case CCValAssign::BCvt:
1248 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1249 break;
1250 }
1251
Bob Wilson1f595bb2009-04-17 19:07:39 +00001252 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001253 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001254 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001255 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1256 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001257 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001258 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001259
1260 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1261 Flag = Chain.getValue(1);
1262 VA = RVLocs[++i]; // skip ahead to next loc
1263 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1264 HalfGPRs.getValue(1), Flag);
1265 Flag = Chain.getValue(1);
1266 VA = RVLocs[++i]; // skip ahead to next loc
1267
1268 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001269 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1270 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001271 }
1272 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1273 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001274 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001275 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001276 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001277 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001278 VA = RVLocs[++i]; // skip ahead to next loc
1279 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1280 Flag);
1281 } else
1282 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1283
Bob Wilsondee46d72009-04-17 20:35:10 +00001284 // Guarantee that all emitted copies are
1285 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001286 Flag = Chain.getValue(1);
1287 }
1288
1289 SDValue result;
1290 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001291 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001292 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001293 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001294
1295 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001296}
1297
Bob Wilsonb62d2572009-11-03 00:02:05 +00001298// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1299// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1300// one of the above mentioned nodes. It has to be wrapped because otherwise
1301// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1302// be used to form addressing mode. These wrapped nodes will be selected
1303// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001304static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001305 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001306 // FIXME there is no actual debug info here
1307 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001308 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001309 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001310 if (CP->isMachineConstantPoolEntry())
1311 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1312 CP->getAlignment());
1313 else
1314 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1315 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001316 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001317}
1318
Dan Gohmand858e902010-04-17 15:26:15 +00001319SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1320 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001321 MachineFunction &MF = DAG.getMachineFunction();
1322 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1323 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001324 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001325 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001326 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001327 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1328 SDValue CPAddr;
1329 if (RelocM == Reloc::Static) {
1330 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1331 } else {
1332 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001333 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001334 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1335 ARMCP::CPBlockAddress,
1336 PCAdj);
1337 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1338 }
1339 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1340 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001341 PseudoSourceValue::getConstantPool(), 0,
1342 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001343 if (RelocM == Reloc::Static)
1344 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001345 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001346 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001347}
1348
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001349// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001350SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001351ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001352 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001353 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001354 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001355 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001356 MachineFunction &MF = DAG.getMachineFunction();
1357 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1358 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001359 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001360 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001361 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001362 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001363 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001364 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
David Greene1b58cab2010-02-15 16:55:24 +00001365 PseudoSourceValue::getConstantPool(), 0,
1366 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001367 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001368
Evan Chenge7e0d622009-11-06 22:24:13 +00001369 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001370 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001371
1372 // call __tls_get_addr.
1373 ArgListTy Args;
1374 ArgListEntry Entry;
1375 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001376 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001377 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001378 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001379 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001380 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1381 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001382 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001383 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001384 return CallResult.first;
1385}
1386
1387// Lower ISD::GlobalTLSAddress using the "initial exec" or
1388// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001389SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001390ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001391 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001392 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001393 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001394 SDValue Offset;
1395 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001396 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001397 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001398 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001399
Chris Lattner4fb63d02009-07-15 04:12:33 +00001400 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001401 MachineFunction &MF = DAG.getMachineFunction();
1402 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1403 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1404 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001405 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1406 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001407 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001408 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001409 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001410 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001411 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001412 PseudoSourceValue::getConstantPool(), 0,
1413 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001414 Chain = Offset.getValue(1);
1415
Evan Chenge7e0d622009-11-06 22:24:13 +00001416 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001417 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001418
Evan Cheng9eda6892009-10-31 03:39:36 +00001419 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001420 PseudoSourceValue::getConstantPool(), 0,
1421 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001422 } else {
1423 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001424 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001425 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001426 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001427 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001428 PseudoSourceValue::getConstantPool(), 0,
1429 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001430 }
1431
1432 // The address of the thread local variable is the add of the thread
1433 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001434 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001435}
1436
Dan Gohman475871a2008-07-27 21:46:04 +00001437SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001438ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001439 // TODO: implement the "local dynamic" model
1440 assert(Subtarget->isTargetELF() &&
1441 "TLS not implemented for non-ELF targets");
1442 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1443 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1444 // otherwise use the "Local Exec" TLS Model
1445 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1446 return LowerToTLSGeneralDynamicModel(GA, DAG);
1447 else
1448 return LowerToTLSExecModels(GA, DAG);
1449}
1450
Dan Gohman475871a2008-07-27 21:46:04 +00001451SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001452 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001453 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001454 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001455 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001456 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1457 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001458 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001459 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001460 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001461 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001462 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001463 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001464 CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001465 PseudoSourceValue::getConstantPool(), 0,
1466 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001467 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001468 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001469 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001470 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001471 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001472 PseudoSourceValue::getGOT(), 0,
1473 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001474 return Result;
1475 } else {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001476 // If we have T2 ops, we can materialize the address directly via movt/movw
1477 // pair. This is always cheaper.
1478 if (Subtarget->useMovt()) {
1479 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
1480 DAG.getTargetGlobalAddress(GV, PtrVT));
1481 } else {
1482 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1483 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1484 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001485 PseudoSourceValue::getConstantPool(), 0,
1486 false, false, 0);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001487 }
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001488 }
1489}
1490
Dan Gohman475871a2008-07-27 21:46:04 +00001491SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001492 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001493 MachineFunction &MF = DAG.getMachineFunction();
1494 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1495 unsigned ARMPCLabelIndex = 0;
Owen Andersone50ed302009-08-10 22:56:29 +00001496 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001497 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001498 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001499 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001500 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001501 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001502 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001503 else {
Evan Chenge7e0d622009-11-06 22:24:13 +00001504 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001505 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1506 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001507 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001508 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001509 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001510 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001511
Evan Cheng9eda6892009-10-31 03:39:36 +00001512 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001513 PseudoSourceValue::getConstantPool(), 0,
1514 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001515 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001516
1517 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001518 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001519 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001520 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001521
Evan Cheng63476a82009-09-03 07:04:02 +00001522 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Evan Cheng9eda6892009-10-31 03:39:36 +00001523 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001524 PseudoSourceValue::getGOT(), 0,
1525 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001526
1527 return Result;
1528}
1529
Dan Gohman475871a2008-07-27 21:46:04 +00001530SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001531 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001532 assert(Subtarget->isTargetELF() &&
1533 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00001534 MachineFunction &MF = DAG.getMachineFunction();
1535 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1536 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Andersone50ed302009-08-10 22:56:29 +00001537 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001538 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001539 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001540 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1541 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001542 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001543 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001544 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001545 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001546 PseudoSourceValue::getConstantPool(), 0,
1547 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001548 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001549 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001550}
1551
Jim Grosbach0e0da732009-05-12 23:59:14 +00001552SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00001553ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
1554 DebugLoc dl = Op.getDebugLoc();
1555 SDValue Val = Subtarget->isThumb() ?
1556 DAG.getCopyFromReg(DAG.getEntryNode(), dl, ARM::SP, MVT::i32) :
1557 DAG.getConstant(0, MVT::i32);
1558 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
1559 Op.getOperand(1), Val);
1560}
1561
1562SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00001563ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
1564 DebugLoc dl = Op.getDebugLoc();
1565 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
1566 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
1567}
1568
1569SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00001570ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001571 const ARMSubtarget *Subtarget)
1572 const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001573 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001574 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001575 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001576 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001577 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001578 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001579 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1580 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001581 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001582 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00001583 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1584 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001585 EVT PtrVT = getPointerTy();
1586 DebugLoc dl = Op.getDebugLoc();
1587 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1588 SDValue CPAddr;
1589 unsigned PCAdj = (RelocM != Reloc::PIC_)
1590 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001591 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001592 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1593 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001594 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001595 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001596 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00001597 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001598 PseudoSourceValue::getConstantPool(), 0,
1599 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001600 SDValue Chain = Result.getValue(1);
1601
1602 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001603 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001604 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1605 }
1606 return Result;
1607 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001608 }
1609}
1610
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001611static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
1612 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00001613 DebugLoc dl = Op.getDebugLoc();
1614 SDValue Op5 = Op.getOperand(5);
1615 SDValue Res;
1616 unsigned isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue();
1617 if (isDeviceBarrier) {
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001618 if (Subtarget->hasV7Ops())
1619 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0));
1620 else
1621 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0),
1622 DAG.getConstant(0, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00001623 } else {
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001624 if (Subtarget->hasV7Ops())
1625 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
1626 else
1627 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
1628 DAG.getConstant(0, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00001629 }
1630 return Res;
1631}
1632
Dan Gohman1e93df62010-04-17 14:41:14 +00001633static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
1634 MachineFunction &MF = DAG.getMachineFunction();
1635 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
1636
Evan Chenga8e29892007-01-19 07:51:42 +00001637 // vastart just stores the address of the VarArgsFrameIndex slot into the
1638 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001639 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001640 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00001641 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001642 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
David Greene1b58cab2010-02-15 16:55:24 +00001643 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
1644 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001645}
1646
Dan Gohman475871a2008-07-27 21:46:04 +00001647SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001648ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1649 SelectionDAG &DAG) const {
Evan Cheng86198642009-08-07 00:34:42 +00001650 SDNode *Node = Op.getNode();
1651 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001652 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001653 SDValue Chain = Op.getOperand(0);
1654 SDValue Size = Op.getOperand(1);
1655 SDValue Align = Op.getOperand(2);
1656
1657 // Chain the dynamic stack allocation so that it doesn't modify the stack
1658 // pointer when other instructions are using the stack.
1659 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1660
1661 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1662 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1663 if (AlignVal > StackAlign)
1664 // Do this now since selection pass cannot introduce new target
1665 // independent node.
1666 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1667
1668 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1669 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1670 // do even more horrible hack later.
1671 MachineFunction &MF = DAG.getMachineFunction();
1672 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1673 if (AFI->isThumb1OnlyFunction()) {
1674 bool Negate = true;
1675 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1676 if (C) {
1677 uint32_t Val = C->getZExtValue();
1678 if (Val <= 508 && ((Val & 3) == 0))
1679 Negate = false;
1680 }
1681 if (Negate)
1682 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1683 }
1684
Owen Anderson825b72b2009-08-11 20:47:22 +00001685 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001686 SDValue Ops1[] = { Chain, Size, Align };
1687 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1688 Chain = Res.getValue(1);
1689 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1690 DAG.getIntPtrConstant(0, true), SDValue());
1691 SDValue Ops2[] = { Res, Chain };
1692 return DAG.getMergeValues(Ops2, 2, dl);
1693}
1694
1695SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001696ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1697 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001698 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001699 MachineFunction &MF = DAG.getMachineFunction();
1700 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1701
1702 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001703 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001704 RC = ARM::tGPRRegisterClass;
1705 else
1706 RC = ARM::GPRRegisterClass;
1707
1708 // Transform the arguments stored in physical registers into virtual ones.
Evan Cheng2457f2c2010-05-22 01:47:14 +00001709 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001710 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001711
1712 SDValue ArgValue2;
1713 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001714 MachineFrameInfo *MFI = MF.getFrameInfo();
Bob Wilson6a234f02010-04-13 22:03:22 +00001715 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true, false);
Bob Wilson5bafff32009-06-22 23:27:02 +00001716
1717 // Create load node to retrieve arguments from the stack.
1718 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001719 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00001720 PseudoSourceValue::getFixedStack(FI), 0,
1721 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001722 } else {
1723 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001724 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001725 }
1726
Jim Grosbache5165492009-11-09 00:11:35 +00001727 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001728}
1729
1730SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001731ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001732 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001733 const SmallVectorImpl<ISD::InputArg>
1734 &Ins,
1735 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001736 SmallVectorImpl<SDValue> &InVals)
1737 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001738
Bob Wilson1f595bb2009-04-17 19:07:39 +00001739 MachineFunction &MF = DAG.getMachineFunction();
1740 MachineFrameInfo *MFI = MF.getFrameInfo();
1741
Bob Wilson1f595bb2009-04-17 19:07:39 +00001742 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1743
1744 // Assign locations to all of the incoming arguments.
1745 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001746 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1747 *DAG.getContext());
1748 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001749 CCAssignFnForNode(CallConv, /* Return*/ false,
1750 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001751
1752 SmallVector<SDValue, 16> ArgValues;
1753
1754 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1755 CCValAssign &VA = ArgLocs[i];
1756
Bob Wilsondee46d72009-04-17 20:35:10 +00001757 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001758 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001759 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00001760
Bob Wilson5bafff32009-06-22 23:27:02 +00001761 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001762 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001763 // f64 and vector types are split up into multiple registers or
1764 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00001765 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001766 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001767 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00001768 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00001769 SDValue ArgValue2;
1770 if (VA.isMemLoc()) {
1771 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(),
1772 true, false);
1773 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1774 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
1775 PseudoSourceValue::getFixedStack(FI), 0,
1776 false, false, 0);
1777 } else {
1778 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
1779 Chain, DAG, dl);
1780 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001781 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1782 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001783 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00001784 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001785 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1786 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001787 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001788
Bob Wilson5bafff32009-06-22 23:27:02 +00001789 } else {
1790 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001791
Owen Anderson825b72b2009-08-11 20:47:22 +00001792 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00001793 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001794 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00001795 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001796 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001797 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001798 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001799 RC = (AFI->isThumb1OnlyFunction() ?
1800 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00001801 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001802 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00001803
1804 // Transform the arguments in physical registers into virtual ones.
1805 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001806 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001807 }
1808
1809 // If this is an 8 or 16-bit value, it is really passed promoted
1810 // to 32 bits. Insert an assert[sz]ext to capture this, then
1811 // truncate to the right size.
1812 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001813 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001814 case CCValAssign::Full: break;
1815 case CCValAssign::BCvt:
1816 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1817 break;
1818 case CCValAssign::SExt:
1819 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1820 DAG.getValueType(VA.getValVT()));
1821 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1822 break;
1823 case CCValAssign::ZExt:
1824 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1825 DAG.getValueType(VA.getValVT()));
1826 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1827 break;
1828 }
1829
Dan Gohman98ca4f22009-08-05 01:29:28 +00001830 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001831
1832 } else { // VA.isRegLoc()
1833
1834 // sanity check
1835 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00001836 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001837
1838 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001839 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
1840 true, false);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001841
Bob Wilsondee46d72009-04-17 20:35:10 +00001842 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001843 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001844 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00001845 PseudoSourceValue::getFixedStack(FI), 0,
1846 false, false, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001847 }
1848 }
1849
1850 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001851 if (isVarArg) {
1852 static const unsigned GPRArgRegs[] = {
1853 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1854 };
1855
Bob Wilsondee46d72009-04-17 20:35:10 +00001856 unsigned NumGPRs = CCInfo.getFirstUnallocated
1857 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001858
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001859 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1860 unsigned VARegSize = (4 - NumGPRs) * 4;
1861 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00001862 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001863 if (VARegSaveSize) {
1864 // If this function is vararg, store any remaining integer argument regs
1865 // to their spots on the stack so that they may be loaded by deferencing
1866 // the result of va_next.
1867 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00001868 AFI->setVarArgsFrameIndex(
1869 MFI->CreateFixedObject(VARegSaveSize,
1870 ArgOffset + VARegSaveSize - VARegSize,
1871 true, false));
1872 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
1873 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001874
Dan Gohman475871a2008-07-27 21:46:04 +00001875 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001876 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001877 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001878 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001879 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001880 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001881 RC = ARM::GPRRegisterClass;
1882
Bob Wilson998e1252009-04-20 18:36:57 +00001883 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001884 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00001885 SDValue Store =
1886 DAG.getStore(Val.getValue(1), dl, Val, FIN,
1887 PseudoSourceValue::getFixedStack(AFI->getVarArgsFrameIndex()), 0,
1888 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001889 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001890 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001891 DAG.getConstant(4, getPointerTy()));
1892 }
1893 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001894 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001895 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001896 } else
1897 // This will point to the next argument passed via stack.
Dan Gohman1e93df62010-04-17 14:41:14 +00001898 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset,
1899 true, false));
Evan Chenga8e29892007-01-19 07:51:42 +00001900 }
1901
Dan Gohman98ca4f22009-08-05 01:29:28 +00001902 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00001903}
1904
1905/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001906static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001907 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001908 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001909 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001910 // Maybe this has already been legalized into the constant pool?
1911 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001912 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001913 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00001914 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001915 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001916 }
1917 }
1918 return false;
1919}
1920
Evan Chenga8e29892007-01-19 07:51:42 +00001921/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1922/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00001923SDValue
1924ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Dan Gohmand858e902010-04-17 15:26:15 +00001925 SDValue &ARMCC, SelectionDAG &DAG,
1926 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00001927 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001928 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00001929 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001930 // Constant does not fit, try adjusting it by one?
1931 switch (CC) {
1932 default: break;
1933 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001934 case ISD::SETGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00001935 if (isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001936 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001937 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001938 }
1939 break;
1940 case ISD::SETULT:
1941 case ISD::SETUGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00001942 if (C > 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001943 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001944 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001945 }
1946 break;
1947 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001948 case ISD::SETGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00001949 if (isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001950 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001951 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001952 }
1953 break;
1954 case ISD::SETULE:
1955 case ISD::SETUGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00001956 if (C < 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001957 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001958 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001959 }
1960 break;
1961 }
1962 }
1963 }
1964
1965 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001966 ARMISD::NodeType CompareType;
1967 switch (CondCode) {
1968 default:
1969 CompareType = ARMISD::CMP;
1970 break;
1971 case ARMCC::EQ:
1972 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00001973 // Uses only Z Flag
1974 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001975 break;
1976 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001977 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1978 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001979}
1980
1981/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001982static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001983 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001984 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001985 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00001986 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001987 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001988 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1989 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001990}
1991
Dan Gohmand858e902010-04-17 15:26:15 +00001992SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001993 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001994 SDValue LHS = Op.getOperand(0);
1995 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001996 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001997 SDValue TrueVal = Op.getOperand(2);
1998 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001999 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002000
Owen Anderson825b72b2009-08-11 20:47:22 +00002001 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00002002 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002003 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00002004 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Dale Johannesende064702009-02-06 21:50:26 +00002005 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002006 }
2007
2008 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002009 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002010
Owen Anderson825b72b2009-08-11 20:47:22 +00002011 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
2012 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002013 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
2014 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00002015 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002016 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002017 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002018 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00002019 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002020 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00002021 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002022 }
2023 return Result;
2024}
2025
Dan Gohmand858e902010-04-17 15:26:15 +00002026SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002027 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002028 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002029 SDValue LHS = Op.getOperand(2);
2030 SDValue RHS = Op.getOperand(3);
2031 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002032 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002033
Owen Anderson825b72b2009-08-11 20:47:22 +00002034 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00002035 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002036 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00002037 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002038 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00002039 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002040 }
2041
Owen Anderson825b72b2009-08-11 20:47:22 +00002042 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00002043 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002044 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002045
Dale Johannesende064702009-02-06 21:50:26 +00002046 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002047 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
2048 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2049 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002050 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002051 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002052 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002053 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00002054 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002055 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002056 }
2057 return Res;
2058}
2059
Dan Gohmand858e902010-04-17 15:26:15 +00002060SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002061 SDValue Chain = Op.getOperand(0);
2062 SDValue Table = Op.getOperand(1);
2063 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002064 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002065
Owen Andersone50ed302009-08-10 22:56:29 +00002066 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002067 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2068 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002069 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002070 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002071 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002072 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2073 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002074 if (Subtarget->isThumb2()) {
2075 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2076 // which does another jump to the destination. This also makes it easier
2077 // to translate it to TBB / TBH later.
2078 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002079 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002080 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002081 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002082 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002083 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002084 PseudoSourceValue::getJumpTable(), 0,
2085 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002086 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002087 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002088 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002089 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002090 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002091 PseudoSourceValue::getJumpTable(), 0, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002092 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002093 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002094 }
Evan Chenga8e29892007-01-19 07:51:42 +00002095}
2096
Bob Wilson76a312b2010-03-19 22:51:32 +00002097static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2098 DebugLoc dl = Op.getDebugLoc();
2099 unsigned Opc;
2100
2101 switch (Op.getOpcode()) {
2102 default:
2103 assert(0 && "Invalid opcode!");
2104 case ISD::FP_TO_SINT:
2105 Opc = ARMISD::FTOSI;
2106 break;
2107 case ISD::FP_TO_UINT:
2108 Opc = ARMISD::FTOUI;
2109 break;
2110 }
2111 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
2112 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
2113}
2114
2115static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2116 EVT VT = Op.getValueType();
2117 DebugLoc dl = Op.getDebugLoc();
2118 unsigned Opc;
2119
2120 switch (Op.getOpcode()) {
2121 default:
2122 assert(0 && "Invalid opcode!");
2123 case ISD::SINT_TO_FP:
2124 Opc = ARMISD::SITOF;
2125 break;
2126 case ISD::UINT_TO_FP:
2127 Opc = ARMISD::UITOF;
2128 break;
2129 }
2130
2131 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
2132 return DAG.getNode(Opc, dl, VT, Op);
2133}
2134
Dan Gohman475871a2008-07-27 21:46:04 +00002135static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002136 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002137 SDValue Tmp0 = Op.getOperand(0);
2138 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002139 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002140 EVT VT = Op.getValueType();
2141 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00002142 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
2143 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002144 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
2145 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002146 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002147}
2148
Evan Cheng2457f2c2010-05-22 01:47:14 +00002149SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
2150 MachineFunction &MF = DAG.getMachineFunction();
2151 MachineFrameInfo *MFI = MF.getFrameInfo();
2152 MFI->setReturnAddressIsTaken(true);
2153
2154 EVT VT = Op.getValueType();
2155 DebugLoc dl = Op.getDebugLoc();
2156 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2157 if (Depth) {
2158 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
2159 SDValue Offset = DAG.getConstant(4, MVT::i32);
2160 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
2161 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
2162 NULL, 0, false, false, 0);
2163 }
2164
2165 // Return LR, which contains the return address. Mark it an implicit live-in.
Evan Chengc7cf10c2010-05-24 18:00:18 +00002166 unsigned Reg = MF.addLiveIn(ARM::LR, ARM::GPRRegisterClass);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002167 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
2168}
2169
Dan Gohmand858e902010-04-17 15:26:15 +00002170SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002171 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2172 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002173
Owen Andersone50ed302009-08-10 22:56:29 +00002174 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002175 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2176 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002177 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002178 ? ARM::R7 : ARM::R11;
2179 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2180 while (Depth--)
David Greene1b58cab2010-02-15 16:55:24 +00002181 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
2182 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002183 return FrameAddr;
2184}
2185
Bob Wilson9f3f0612010-04-17 05:30:19 +00002186/// ExpandBIT_CONVERT - If the target supports VFP, this function is called to
2187/// expand a bit convert where either the source or destination type is i64 to
2188/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2189/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2190/// vectors), since the legalizer won't know what to do with that.
Duncan Sands1607f052008-12-01 11:39:25 +00002191static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002192 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2193 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002194 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002195
Bob Wilson9f3f0612010-04-17 05:30:19 +00002196 // This function is only supposed to be called for i64 types, either as the
2197 // source or destination of the bit convert.
2198 EVT SrcVT = Op.getValueType();
2199 EVT DstVT = N->getValueType(0);
2200 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
2201 "ExpandBIT_CONVERT called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002202
Bob Wilson9f3f0612010-04-17 05:30:19 +00002203 // Turn i64->f64 into VMOVDRR.
2204 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002205 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2206 DAG.getConstant(0, MVT::i32));
2207 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2208 DAG.getConstant(1, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00002209 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002210 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002211
Jim Grosbache5165492009-11-09 00:11:35 +00002212 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002213 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2214 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2215 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2216 // Merge the pieces into a single i64 value.
2217 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2218 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002219
Bob Wilson9f3f0612010-04-17 05:30:19 +00002220 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002221}
2222
Bob Wilson5bafff32009-06-22 23:27:02 +00002223/// getZeroVector - Returns a vector of specified type with all zero elements.
2224///
Owen Andersone50ed302009-08-10 22:56:29 +00002225static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002226 assert(VT.isVector() && "Expected a vector type");
2227
2228 // Zero vectors are used to represent vector negation and in those cases
2229 // will be implemented with the NEON VNEG instruction. However, VNEG does
2230 // not support i64 elements, so sometimes the zero vectors will need to be
2231 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002232 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilson5bafff32009-06-22 23:27:02 +00002233 // to their dest type. This ensures they get CSE'd.
2234 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002235 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2236 SmallVector<SDValue, 8> Ops;
2237 MVT TVT;
2238
2239 if (VT.getSizeInBits() == 64) {
2240 Ops.assign(8, Cst); TVT = MVT::v8i8;
2241 } else {
2242 Ops.assign(16, Cst); TVT = MVT::v16i8;
2243 }
2244 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002245
2246 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2247}
2248
2249/// getOnesVector - Returns a vector of specified type with all bits set.
2250///
Owen Andersone50ed302009-08-10 22:56:29 +00002251static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002252 assert(VT.isVector() && "Expected a vector type");
2253
Bob Wilson929ffa22009-10-30 20:13:25 +00002254 // Always build ones vectors as <16 x i8> or <8 x i8> bitcasted to their
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002255 // dest type. This ensures they get CSE'd.
Bob Wilson5bafff32009-06-22 23:27:02 +00002256 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002257 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2258 SmallVector<SDValue, 8> Ops;
2259 MVT TVT;
2260
2261 if (VT.getSizeInBits() == 64) {
2262 Ops.assign(8, Cst); TVT = MVT::v8i8;
2263 } else {
2264 Ops.assign(16, Cst); TVT = MVT::v16i8;
2265 }
2266 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002267
2268 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2269}
2270
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002271/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2272/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002273SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2274 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002275 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2276 EVT VT = Op.getValueType();
2277 unsigned VTBits = VT.getSizeInBits();
2278 DebugLoc dl = Op.getDebugLoc();
2279 SDValue ShOpLo = Op.getOperand(0);
2280 SDValue ShOpHi = Op.getOperand(1);
2281 SDValue ShAmt = Op.getOperand(2);
2282 SDValue ARMCC;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002283 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002284
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002285 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2286
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002287 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2288 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2289 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2290 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2291 DAG.getConstant(VTBits, MVT::i32));
2292 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2293 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002294 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002295
2296 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2297 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002298 ARMCC, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002299 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002300 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC,
2301 CCR, Cmp);
2302
2303 SDValue Ops[2] = { Lo, Hi };
2304 return DAG.getMergeValues(Ops, 2, dl);
2305}
2306
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002307/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2308/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002309SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2310 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002311 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2312 EVT VT = Op.getValueType();
2313 unsigned VTBits = VT.getSizeInBits();
2314 DebugLoc dl = Op.getDebugLoc();
2315 SDValue ShOpLo = Op.getOperand(0);
2316 SDValue ShOpHi = Op.getOperand(1);
2317 SDValue ShAmt = Op.getOperand(2);
2318 SDValue ARMCC;
2319
2320 assert(Op.getOpcode() == ISD::SHL_PARTS);
2321 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2322 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2323 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2324 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2325 DAG.getConstant(VTBits, MVT::i32));
2326 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2327 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2328
2329 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2330 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2331 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002332 ARMCC, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002333 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
2334 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMCC,
2335 CCR, Cmp);
2336
2337 SDValue Ops[2] = { Lo, Hi };
2338 return DAG.getMergeValues(Ops, 2, dl);
2339}
2340
Jim Grosbach3482c802010-01-18 19:58:49 +00002341static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
2342 const ARMSubtarget *ST) {
2343 EVT VT = N->getValueType(0);
2344 DebugLoc dl = N->getDebugLoc();
2345
2346 if (!ST->hasV6T2Ops())
2347 return SDValue();
2348
2349 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
2350 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
2351}
2352
Bob Wilson5bafff32009-06-22 23:27:02 +00002353static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2354 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002355 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002356 DebugLoc dl = N->getDebugLoc();
2357
2358 // Lower vector shifts on NEON to use VSHL.
2359 if (VT.isVector()) {
2360 assert(ST->hasNEON() && "unexpected vector shift");
2361
2362 // Left shifts translate directly to the vshiftu intrinsic.
2363 if (N->getOpcode() == ISD::SHL)
2364 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002365 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002366 N->getOperand(0), N->getOperand(1));
2367
2368 assert((N->getOpcode() == ISD::SRA ||
2369 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2370
2371 // NEON uses the same intrinsics for both left and right shifts. For
2372 // right shifts, the shift amounts are negative, so negate the vector of
2373 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002374 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002375 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2376 getZeroVector(ShiftVT, DAG, dl),
2377 N->getOperand(1));
2378 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2379 Intrinsic::arm_neon_vshifts :
2380 Intrinsic::arm_neon_vshiftu);
2381 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002382 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002383 N->getOperand(0), NegatedCount);
2384 }
2385
Eli Friedmance392eb2009-08-22 03:13:10 +00002386 // We can get here for a node like i32 = ISD::SHL i32, i64
2387 if (VT != MVT::i64)
2388 return SDValue();
2389
2390 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002391 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002392
Chris Lattner27a6c732007-11-24 07:07:01 +00002393 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2394 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002395 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002396 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002397
Chris Lattner27a6c732007-11-24 07:07:01 +00002398 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002399 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002400
Chris Lattner27a6c732007-11-24 07:07:01 +00002401 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002402 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00002403 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00002404 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00002405 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002406
Chris Lattner27a6c732007-11-24 07:07:01 +00002407 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2408 // captures the result into a carry flag.
2409 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002410 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002411
Chris Lattner27a6c732007-11-24 07:07:01 +00002412 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002413 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002414
Chris Lattner27a6c732007-11-24 07:07:01 +00002415 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002416 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002417}
2418
Bob Wilson5bafff32009-06-22 23:27:02 +00002419static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2420 SDValue TmpOp0, TmpOp1;
2421 bool Invert = false;
2422 bool Swap = false;
2423 unsigned Opc = 0;
2424
2425 SDValue Op0 = Op.getOperand(0);
2426 SDValue Op1 = Op.getOperand(1);
2427 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002428 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002429 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2430 DebugLoc dl = Op.getDebugLoc();
2431
2432 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2433 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002434 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002435 case ISD::SETUNE:
2436 case ISD::SETNE: Invert = true; // Fallthrough
2437 case ISD::SETOEQ:
2438 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2439 case ISD::SETOLT:
2440 case ISD::SETLT: Swap = true; // Fallthrough
2441 case ISD::SETOGT:
2442 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2443 case ISD::SETOLE:
2444 case ISD::SETLE: Swap = true; // Fallthrough
2445 case ISD::SETOGE:
2446 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2447 case ISD::SETUGE: Swap = true; // Fallthrough
2448 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2449 case ISD::SETUGT: Swap = true; // Fallthrough
2450 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2451 case ISD::SETUEQ: Invert = true; // Fallthrough
2452 case ISD::SETONE:
2453 // Expand this to (OLT | OGT).
2454 TmpOp0 = Op0;
2455 TmpOp1 = Op1;
2456 Opc = ISD::OR;
2457 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2458 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2459 break;
2460 case ISD::SETUO: Invert = true; // Fallthrough
2461 case ISD::SETO:
2462 // Expand this to (OLT | OGE).
2463 TmpOp0 = Op0;
2464 TmpOp1 = Op1;
2465 Opc = ISD::OR;
2466 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2467 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2468 break;
2469 }
2470 } else {
2471 // Integer comparisons.
2472 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002473 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002474 case ISD::SETNE: Invert = true;
2475 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2476 case ISD::SETLT: Swap = true;
2477 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2478 case ISD::SETLE: Swap = true;
2479 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2480 case ISD::SETULT: Swap = true;
2481 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2482 case ISD::SETULE: Swap = true;
2483 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2484 }
2485
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002486 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002487 if (Opc == ARMISD::VCEQ) {
2488
2489 SDValue AndOp;
2490 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2491 AndOp = Op0;
2492 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2493 AndOp = Op1;
2494
2495 // Ignore bitconvert.
2496 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2497 AndOp = AndOp.getOperand(0);
2498
2499 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2500 Opc = ARMISD::VTST;
2501 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2502 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2503 Invert = !Invert;
2504 }
2505 }
2506 }
2507
2508 if (Swap)
2509 std::swap(Op0, Op1);
2510
2511 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2512
2513 if (Invert)
2514 Result = DAG.getNOT(dl, Result, VT);
2515
2516 return Result;
2517}
2518
2519/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2520/// VMOV instruction, and if so, return the constant being splatted.
2521static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2522 unsigned SplatBitSize, SelectionDAG &DAG) {
2523 switch (SplatBitSize) {
2524 case 8:
2525 // Any 1-byte value is OK.
2526 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson825b72b2009-08-11 20:47:22 +00002527 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilson5bafff32009-06-22 23:27:02 +00002528
2529 case 16:
2530 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2531 if ((SplatBits & ~0xff) == 0 ||
2532 (SplatBits & ~0xff00) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002533 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilson5bafff32009-06-22 23:27:02 +00002534 break;
2535
2536 case 32:
2537 // NEON's 32-bit VMOV supports splat values where:
2538 // * only one byte is nonzero, or
2539 // * the least significant byte is 0xff and the second byte is nonzero, or
2540 // * the least significant 2 bytes are 0xff and the third is nonzero.
2541 if ((SplatBits & ~0xff) == 0 ||
2542 (SplatBits & ~0xff00) == 0 ||
2543 (SplatBits & ~0xff0000) == 0 ||
2544 (SplatBits & ~0xff000000) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002545 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002546
2547 if ((SplatBits & ~0xffff) == 0 &&
2548 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002549 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002550
2551 if ((SplatBits & ~0xffffff) == 0 &&
2552 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002553 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002554
2555 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2556 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2557 // VMOV.I32. A (very) minor optimization would be to replicate the value
2558 // and fall through here to test for a valid 64-bit splat. But, then the
2559 // caller would also need to check and handle the change in size.
2560 break;
2561
2562 case 64: {
2563 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2564 uint64_t BitMask = 0xff;
2565 uint64_t Val = 0;
2566 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2567 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2568 Val |= BitMask;
2569 else if ((SplatBits & BitMask) != 0)
2570 return SDValue();
2571 BitMask <<= 8;
2572 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002573 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilson5bafff32009-06-22 23:27:02 +00002574 }
2575
2576 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002577 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilson5bafff32009-06-22 23:27:02 +00002578 break;
2579 }
2580
2581 return SDValue();
2582}
2583
2584/// getVMOVImm - If this is a build_vector of constants which can be
2585/// formed by using a VMOV instruction of the specified element size,
2586/// return the constant being splatted. The ByteSize field indicates the
2587/// number of bytes of each element [1248].
2588SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2589 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2590 APInt SplatBits, SplatUndef;
2591 unsigned SplatBitSize;
2592 bool HasAnyUndefs;
2593 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2594 HasAnyUndefs, ByteSize * 8))
2595 return SDValue();
2596
2597 if (SplatBitSize > ByteSize * 8)
2598 return SDValue();
2599
2600 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2601 SplatBitSize, DAG);
2602}
2603
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002604static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2605 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002606 unsigned NumElts = VT.getVectorNumElements();
2607 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002608 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002609
2610 // If this is a VEXT shuffle, the immediate value is the index of the first
2611 // element. The other shuffle indices must be the successive elements after
2612 // the first one.
2613 unsigned ExpectedElt = Imm;
2614 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002615 // Increment the expected index. If it wraps around, it may still be
2616 // a VEXT but the source vectors must be swapped.
2617 ExpectedElt += 1;
2618 if (ExpectedElt == NumElts * 2) {
2619 ExpectedElt = 0;
2620 ReverseVEXT = true;
2621 }
2622
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002623 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002624 return false;
2625 }
2626
2627 // Adjust the index value if the source operands will be swapped.
2628 if (ReverseVEXT)
2629 Imm -= NumElts;
2630
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002631 return true;
2632}
2633
Bob Wilson8bb9e482009-07-26 00:39:34 +00002634/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2635/// instruction with the specified blocksize. (The order of the elements
2636/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002637static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2638 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002639 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2640 "Only possible block sizes for VREV are: 16, 32, 64");
2641
Bob Wilson8bb9e482009-07-26 00:39:34 +00002642 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00002643 if (EltSz == 64)
2644 return false;
2645
2646 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002647 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002648
2649 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2650 return false;
2651
2652 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002653 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002654 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2655 return false;
2656 }
2657
2658 return true;
2659}
2660
Bob Wilsonc692cb72009-08-21 20:54:19 +00002661static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2662 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002663 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2664 if (EltSz == 64)
2665 return false;
2666
Bob Wilsonc692cb72009-08-21 20:54:19 +00002667 unsigned NumElts = VT.getVectorNumElements();
2668 WhichResult = (M[0] == 0 ? 0 : 1);
2669 for (unsigned i = 0; i < NumElts; i += 2) {
2670 if ((unsigned) M[i] != i + WhichResult ||
2671 (unsigned) M[i+1] != i + NumElts + WhichResult)
2672 return false;
2673 }
2674 return true;
2675}
2676
Bob Wilson324f4f12009-12-03 06:40:55 +00002677/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
2678/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2679/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
2680static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2681 unsigned &WhichResult) {
2682 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2683 if (EltSz == 64)
2684 return false;
2685
2686 unsigned NumElts = VT.getVectorNumElements();
2687 WhichResult = (M[0] == 0 ? 0 : 1);
2688 for (unsigned i = 0; i < NumElts; i += 2) {
2689 if ((unsigned) M[i] != i + WhichResult ||
2690 (unsigned) M[i+1] != i + WhichResult)
2691 return false;
2692 }
2693 return true;
2694}
2695
Bob Wilsonc692cb72009-08-21 20:54:19 +00002696static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2697 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002698 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2699 if (EltSz == 64)
2700 return false;
2701
Bob Wilsonc692cb72009-08-21 20:54:19 +00002702 unsigned NumElts = VT.getVectorNumElements();
2703 WhichResult = (M[0] == 0 ? 0 : 1);
2704 for (unsigned i = 0; i != NumElts; ++i) {
2705 if ((unsigned) M[i] != 2 * i + WhichResult)
2706 return false;
2707 }
2708
2709 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002710 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002711 return false;
2712
2713 return true;
2714}
2715
Bob Wilson324f4f12009-12-03 06:40:55 +00002716/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
2717/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2718/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
2719static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2720 unsigned &WhichResult) {
2721 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2722 if (EltSz == 64)
2723 return false;
2724
2725 unsigned Half = VT.getVectorNumElements() / 2;
2726 WhichResult = (M[0] == 0 ? 0 : 1);
2727 for (unsigned j = 0; j != 2; ++j) {
2728 unsigned Idx = WhichResult;
2729 for (unsigned i = 0; i != Half; ++i) {
2730 if ((unsigned) M[i + j * Half] != Idx)
2731 return false;
2732 Idx += 2;
2733 }
2734 }
2735
2736 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2737 if (VT.is64BitVector() && EltSz == 32)
2738 return false;
2739
2740 return true;
2741}
2742
Bob Wilsonc692cb72009-08-21 20:54:19 +00002743static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2744 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002745 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2746 if (EltSz == 64)
2747 return false;
2748
Bob Wilsonc692cb72009-08-21 20:54:19 +00002749 unsigned NumElts = VT.getVectorNumElements();
2750 WhichResult = (M[0] == 0 ? 0 : 1);
2751 unsigned Idx = WhichResult * NumElts / 2;
2752 for (unsigned i = 0; i != NumElts; i += 2) {
2753 if ((unsigned) M[i] != Idx ||
2754 (unsigned) M[i+1] != Idx + NumElts)
2755 return false;
2756 Idx += 1;
2757 }
2758
2759 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002760 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002761 return false;
2762
2763 return true;
2764}
2765
Bob Wilson324f4f12009-12-03 06:40:55 +00002766/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
2767/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2768/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
2769static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2770 unsigned &WhichResult) {
2771 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2772 if (EltSz == 64)
2773 return false;
2774
2775 unsigned NumElts = VT.getVectorNumElements();
2776 WhichResult = (M[0] == 0 ? 0 : 1);
2777 unsigned Idx = WhichResult * NumElts / 2;
2778 for (unsigned i = 0; i != NumElts; i += 2) {
2779 if ((unsigned) M[i] != Idx ||
2780 (unsigned) M[i+1] != Idx)
2781 return false;
2782 Idx += 1;
2783 }
2784
2785 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2786 if (VT.is64BitVector() && EltSz == 32)
2787 return false;
2788
2789 return true;
2790}
2791
2792
Owen Andersone50ed302009-08-10 22:56:29 +00002793static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002794 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00002795 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002796 if (ConstVal->isNullValue())
2797 return getZeroVector(VT, DAG, dl);
2798 if (ConstVal->isAllOnesValue())
2799 return getOnesVector(VT, DAG, dl);
2800
Owen Andersone50ed302009-08-10 22:56:29 +00002801 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00002802 if (VT.is64BitVector()) {
2803 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002804 case 8: CanonicalVT = MVT::v8i8; break;
2805 case 16: CanonicalVT = MVT::v4i16; break;
2806 case 32: CanonicalVT = MVT::v2i32; break;
2807 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002808 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002809 }
2810 } else {
2811 assert(VT.is128BitVector() && "unknown splat vector size");
2812 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002813 case 8: CanonicalVT = MVT::v16i8; break;
2814 case 16: CanonicalVT = MVT::v8i16; break;
2815 case 32: CanonicalVT = MVT::v4i32; break;
2816 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002817 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002818 }
2819 }
2820
2821 // Build a canonical splat for this value.
2822 SmallVector<SDValue, 8> Ops;
2823 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2824 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2825 Ops.size());
2826 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2827}
2828
2829// If this is a case we can't handle, return null and let the default
2830// expansion code take care of it.
2831static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00002832 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002833 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002834 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002835
2836 APInt SplatBits, SplatUndef;
2837 unsigned SplatBitSize;
2838 bool HasAnyUndefs;
2839 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00002840 if (SplatBitSize <= 64) {
2841 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2842 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2843 if (Val.getNode())
2844 return BuildSplat(Val, VT, DAG, dl);
2845 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00002846 }
2847
Bob Wilsonbe751cf2010-05-22 00:23:12 +00002848 // Scan through the operands to see if only one value is used.
2849 unsigned NumElts = VT.getVectorNumElements();
2850 bool isOnlyLowElement = true;
2851 bool usesOnlyOneValue = true;
2852 bool isConstant = true;
2853 SDValue Value;
2854 for (unsigned i = 0; i < NumElts; ++i) {
2855 SDValue V = Op.getOperand(i);
2856 if (V.getOpcode() == ISD::UNDEF)
2857 continue;
2858 if (i > 0)
2859 isOnlyLowElement = false;
2860 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
2861 isConstant = false;
2862
2863 if (!Value.getNode())
2864 Value = V;
2865 else if (V != Value)
2866 usesOnlyOneValue = false;
2867 }
2868
2869 if (!Value.getNode())
2870 return DAG.getUNDEF(VT);
2871
2872 if (isOnlyLowElement)
2873 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
2874
2875 // If all elements are constants, fall back to the default expansion, which
2876 // will generate a load from the constant pool.
2877 if (isConstant)
2878 return SDValue();
2879
2880 // Use VDUP for non-constant splats.
Bob Wilson069e4342010-05-23 05:42:31 +00002881 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
2882 if (usesOnlyOneValue && EltSize <= 32)
Bob Wilsonbe751cf2010-05-22 00:23:12 +00002883 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
2884
2885 // Vectors with 32- or 64-bit elements can be built by directly assigning
2886 // the subregisters.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00002887 if (EltSize >= 32) {
2888 // Do the expansion with floating-point types, since that is what the VFP
2889 // registers are defined to use, and since i64 is not legal.
2890 EVT EltVT = EVT::getFloatingPointVT(EltSize);
2891 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
2892 SDValue Val = DAG.getUNDEF(VecVT);
2893 for (unsigned i = 0; i < NumElts; ++i) {
2894 SDValue Elt = Op.getOperand(i);
2895 if (Elt.getOpcode() == ISD::UNDEF)
2896 continue;
2897 Elt = DAG.getNode(ISD::BIT_CONVERT, dl, EltVT, Elt);
2898 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Val, Elt,
2899 DAG.getConstant(i, MVT::i32));
2900 }
2901 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00002902 }
2903
2904 return SDValue();
2905}
2906
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002907/// isShuffleMaskLegal - Targets can use this to indicate that they only
2908/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2909/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2910/// are assumed to be legal.
2911bool
2912ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2913 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002914 if (VT.getVectorNumElements() == 4 &&
2915 (VT.is128BitVector() || VT.is64BitVector())) {
2916 unsigned PFIndexes[4];
2917 for (unsigned i = 0; i != 4; ++i) {
2918 if (M[i] < 0)
2919 PFIndexes[i] = 8;
2920 else
2921 PFIndexes[i] = M[i];
2922 }
2923
2924 // Compute the index in the perfect shuffle table.
2925 unsigned PFTableIndex =
2926 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2927 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2928 unsigned Cost = (PFEntry >> 30);
2929
2930 if (Cost <= 4)
2931 return true;
2932 }
2933
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002934 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00002935 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002936
2937 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2938 isVREVMask(M, VT, 64) ||
2939 isVREVMask(M, VT, 32) ||
2940 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00002941 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2942 isVTRNMask(M, VT, WhichResult) ||
2943 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00002944 isVZIPMask(M, VT, WhichResult) ||
2945 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
2946 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
2947 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002948}
2949
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002950/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2951/// the specified operations to build the shuffle.
2952static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2953 SDValue RHS, SelectionDAG &DAG,
2954 DebugLoc dl) {
2955 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2956 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2957 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2958
2959 enum {
2960 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2961 OP_VREV,
2962 OP_VDUP0,
2963 OP_VDUP1,
2964 OP_VDUP2,
2965 OP_VDUP3,
2966 OP_VEXT1,
2967 OP_VEXT2,
2968 OP_VEXT3,
2969 OP_VUZPL, // VUZP, left result
2970 OP_VUZPR, // VUZP, right result
2971 OP_VZIPL, // VZIP, left result
2972 OP_VZIPR, // VZIP, right result
2973 OP_VTRNL, // VTRN, left result
2974 OP_VTRNR // VTRN, right result
2975 };
2976
2977 if (OpNum == OP_COPY) {
2978 if (LHSID == (1*9+2)*9+3) return LHS;
2979 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2980 return RHS;
2981 }
2982
2983 SDValue OpLHS, OpRHS;
2984 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2985 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2986 EVT VT = OpLHS.getValueType();
2987
2988 switch (OpNum) {
2989 default: llvm_unreachable("Unknown shuffle opcode!");
2990 case OP_VREV:
2991 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2992 case OP_VDUP0:
2993 case OP_VDUP1:
2994 case OP_VDUP2:
2995 case OP_VDUP3:
2996 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002997 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002998 case OP_VEXT1:
2999 case OP_VEXT2:
3000 case OP_VEXT3:
3001 return DAG.getNode(ARMISD::VEXT, dl, VT,
3002 OpLHS, OpRHS,
3003 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
3004 case OP_VUZPL:
3005 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003006 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003007 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
3008 case OP_VZIPL:
3009 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003010 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003011 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
3012 case OP_VTRNL:
3013 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003014 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3015 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003016 }
3017}
3018
Bob Wilson5bafff32009-06-22 23:27:02 +00003019static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003020 SDValue V1 = Op.getOperand(0);
3021 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00003022 DebugLoc dl = Op.getDebugLoc();
3023 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003024 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003025 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00003026
Bob Wilson28865062009-08-13 02:13:04 +00003027 // Convert shuffles that are directly supported on NEON to target-specific
3028 // DAG nodes, instead of keeping them as shuffles and matching them again
3029 // during code selection. This is more efficient and avoids the possibility
3030 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00003031 // FIXME: floating-point vectors should be canonicalized to integer vectors
3032 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003033 SVN->getMask(ShuffleMask);
3034
3035 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson0ce37102009-08-14 05:08:32 +00003036 int Lane = SVN->getSplatIndex();
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00003037 // If this is undef splat, generate it via "just" vdup, if possible.
3038 if (Lane == -1) Lane = 0;
3039
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003040 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3041 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00003042 }
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003043 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003044 DAG.getConstant(Lane, MVT::i32));
Bob Wilson0ce37102009-08-14 05:08:32 +00003045 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003046
3047 bool ReverseVEXT;
3048 unsigned Imm;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003049 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003050 if (ReverseVEXT)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003051 std::swap(V1, V2);
3052 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003053 DAG.getConstant(Imm, MVT::i32));
3054 }
3055
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003056 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003057 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003058 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003059 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003060 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003061 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
3062
Bob Wilsonc692cb72009-08-21 20:54:19 +00003063 // Check for Neon shuffles that modify both input vectors in place.
3064 // If both results are used, i.e., if there are two shuffles with the same
3065 // source operands and with masks corresponding to both results of one of
3066 // these operations, DAG memoization will ensure that a single node is
3067 // used for both shuffles.
3068 unsigned WhichResult;
3069 if (isVTRNMask(ShuffleMask, VT, WhichResult))
3070 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3071 V1, V2).getValue(WhichResult);
3072 if (isVUZPMask(ShuffleMask, VT, WhichResult))
3073 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3074 V1, V2).getValue(WhichResult);
3075 if (isVZIPMask(ShuffleMask, VT, WhichResult))
3076 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3077 V1, V2).getValue(WhichResult);
3078
Bob Wilson324f4f12009-12-03 06:40:55 +00003079 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
3080 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3081 V1, V1).getValue(WhichResult);
3082 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3083 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3084 V1, V1).getValue(WhichResult);
3085 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3086 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3087 V1, V1).getValue(WhichResult);
3088
Bob Wilsonc692cb72009-08-21 20:54:19 +00003089 // If the shuffle is not directly supported and it has 4 elements, use
3090 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003091 unsigned NumElts = VT.getVectorNumElements();
3092 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003093 unsigned PFIndexes[4];
3094 for (unsigned i = 0; i != 4; ++i) {
3095 if (ShuffleMask[i] < 0)
3096 PFIndexes[i] = 8;
3097 else
3098 PFIndexes[i] = ShuffleMask[i];
3099 }
3100
3101 // Compute the index in the perfect shuffle table.
3102 unsigned PFTableIndex =
3103 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003104 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3105 unsigned Cost = (PFEntry >> 30);
3106
3107 if (Cost <= 4)
3108 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
3109 }
Bob Wilsond8e17572009-08-12 22:31:50 +00003110
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003111 // Implement shuffles with 32- or 64-bit elements as subreg copies.
3112 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3113 if (EltSize >= 32) {
3114 // Do the expansion with floating-point types, since that is what the VFP
3115 // registers are defined to use, and since i64 is not legal.
3116 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3117 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
3118 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V1);
3119 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V2);
3120 SDValue Val = DAG.getUNDEF(VecVT);
3121 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00003122 if (ShuffleMask[i] < 0)
3123 continue;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003124 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
3125 ShuffleMask[i] < (int)NumElts ? V1 : V2,
3126 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
3127 MVT::i32));
3128 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Val,
Bob Wilson63b88452010-05-20 18:39:53 +00003129 Elt, DAG.getConstant(i, MVT::i32));
3130 }
3131 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
3132 }
3133
Bob Wilson22cac0d2009-08-14 05:16:33 +00003134 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003135}
3136
Bob Wilson5bafff32009-06-22 23:27:02 +00003137static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003138 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003139 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003140 SDValue Vec = Op.getOperand(0);
3141 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00003142 assert(VT == MVT::i32 &&
3143 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
3144 "unexpected type for custom-lowering vector extract");
3145 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00003146}
3147
Bob Wilsona6d65862009-08-03 20:36:38 +00003148static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3149 // The only time a CONCAT_VECTORS operation can have legal types is when
3150 // two 64-bit vectors are concatenated to a 128-bit vector.
3151 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
3152 "unexpected CONCAT_VECTORS");
3153 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00003154 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00003155 SDValue Op0 = Op.getOperand(0);
3156 SDValue Op1 = Op.getOperand(1);
3157 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003158 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3159 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00003160 DAG.getIntPtrConstant(0));
3161 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003162 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3163 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00003164 DAG.getIntPtrConstant(1));
3165 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003166}
3167
Dan Gohmand858e902010-04-17 15:26:15 +00003168SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003169 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003170 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00003171 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00003172 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003173 case ISD::GlobalAddress:
3174 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
3175 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00003176 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00003177 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
3178 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003179 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00003180 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00003181 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00003182 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00003183 case ISD::SINT_TO_FP:
3184 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
3185 case ISD::FP_TO_SINT:
3186 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003187 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003188 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003189 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003190 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00003191 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00003192 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00003193 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
3194 Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00003195 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003196 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00003197 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00003198 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00003199 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003200 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00003201 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00003202 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003203 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
3204 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3205 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003206 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00003207 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003208 }
Dan Gohman475871a2008-07-27 21:46:04 +00003209 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003210}
3211
Duncan Sands1607f052008-12-01 11:39:25 +00003212/// ReplaceNodeResults - Replace the results of node with an illegal result
3213/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00003214void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
3215 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00003216 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00003217 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00003218 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00003219 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003220 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003221 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003222 case ISD::BIT_CONVERT:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003223 Res = ExpandBIT_CONVERT(N, DAG);
3224 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00003225 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003226 case ISD::SRA:
3227 Res = LowerShift(N, DAG, Subtarget);
3228 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003229 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00003230 if (Res.getNode())
3231 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00003232}
Chris Lattner27a6c732007-11-24 07:07:01 +00003233
Evan Chenga8e29892007-01-19 07:51:42 +00003234//===----------------------------------------------------------------------===//
3235// ARM Scheduler Hooks
3236//===----------------------------------------------------------------------===//
3237
3238MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003239ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
3240 MachineBasicBlock *BB,
3241 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00003242 unsigned dest = MI->getOperand(0).getReg();
3243 unsigned ptr = MI->getOperand(1).getReg();
3244 unsigned oldval = MI->getOperand(2).getReg();
3245 unsigned newval = MI->getOperand(3).getReg();
3246 unsigned scratch = BB->getParent()->getRegInfo()
3247 .createVirtualRegister(ARM::GPRRegisterClass);
3248 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3249 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003250 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00003251
3252 unsigned ldrOpc, strOpc;
3253 switch (Size) {
3254 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003255 case 1:
3256 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
3257 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
3258 break;
3259 case 2:
3260 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3261 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3262 break;
3263 case 4:
3264 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3265 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3266 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00003267 }
3268
3269 MachineFunction *MF = BB->getParent();
3270 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3271 MachineFunction::iterator It = BB;
3272 ++It; // insert the new blocks after the current block
3273
3274 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3275 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3276 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3277 MF->insert(It, loop1MBB);
3278 MF->insert(It, loop2MBB);
3279 MF->insert(It, exitMBB);
3280 exitMBB->transferSuccessors(BB);
3281
3282 // thisMBB:
3283 // ...
3284 // fallthrough --> loop1MBB
3285 BB->addSuccessor(loop1MBB);
3286
3287 // loop1MBB:
3288 // ldrex dest, [ptr]
3289 // cmp dest, oldval
3290 // bne exitMBB
3291 BB = loop1MBB;
3292 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003293 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003294 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003295 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3296 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003297 BB->addSuccessor(loop2MBB);
3298 BB->addSuccessor(exitMBB);
3299
3300 // loop2MBB:
3301 // strex scratch, newval, [ptr]
3302 // cmp scratch, #0
3303 // bne loop1MBB
3304 BB = loop2MBB;
3305 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
3306 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003307 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003308 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003309 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3310 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003311 BB->addSuccessor(loop1MBB);
3312 BB->addSuccessor(exitMBB);
3313
3314 // exitMBB:
3315 // ...
3316 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00003317
3318 MF->DeleteMachineInstr(MI); // The instruction is gone now.
3319
Jim Grosbach5278eb82009-12-11 01:42:04 +00003320 return BB;
3321}
3322
3323MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003324ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
3325 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00003326 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
3327 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3328
3329 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003330 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003331 MachineFunction::iterator It = BB;
3332 ++It;
3333
3334 unsigned dest = MI->getOperand(0).getReg();
3335 unsigned ptr = MI->getOperand(1).getReg();
3336 unsigned incr = MI->getOperand(2).getReg();
3337 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00003338
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003339 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003340 unsigned ldrOpc, strOpc;
3341 switch (Size) {
3342 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003343 case 1:
3344 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00003345 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003346 break;
3347 case 2:
3348 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3349 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3350 break;
3351 case 4:
3352 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3353 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3354 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00003355 }
3356
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003357 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3358 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3359 MF->insert(It, loopMBB);
3360 MF->insert(It, exitMBB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003361 exitMBB->transferSuccessors(BB);
3362
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003363 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003364 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3365 unsigned scratch2 = (!BinOpcode) ? incr :
3366 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3367
3368 // thisMBB:
3369 // ...
3370 // fallthrough --> loopMBB
3371 BB->addSuccessor(loopMBB);
3372
3373 // loopMBB:
3374 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003375 // <binop> scratch2, dest, incr
3376 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00003377 // cmp scratch, #0
3378 // bne- loopMBB
3379 // fallthrough --> exitMBB
3380 BB = loopMBB;
3381 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00003382 if (BinOpcode) {
3383 // operand order needs to go the other way for NAND
3384 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
3385 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3386 addReg(incr).addReg(dest)).addReg(0);
3387 else
3388 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3389 addReg(dest).addReg(incr)).addReg(0);
3390 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00003391
3392 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
3393 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003394 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00003395 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003396 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3397 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003398
3399 BB->addSuccessor(loopMBB);
3400 BB->addSuccessor(exitMBB);
3401
3402 // exitMBB:
3403 // ...
3404 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00003405
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003406 MF->DeleteMachineInstr(MI); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00003407
Jim Grosbachc3c23542009-12-14 04:22:04 +00003408 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00003409}
3410
3411MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00003412ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003413 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003414 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00003415 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003416 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00003417 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00003418 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00003419 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00003420 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00003421
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003422 case ARM::ATOMIC_LOAD_ADD_I8:
3423 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3424 case ARM::ATOMIC_LOAD_ADD_I16:
3425 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3426 case ARM::ATOMIC_LOAD_ADD_I32:
3427 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003428
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003429 case ARM::ATOMIC_LOAD_AND_I8:
3430 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3431 case ARM::ATOMIC_LOAD_AND_I16:
3432 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3433 case ARM::ATOMIC_LOAD_AND_I32:
3434 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003435
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003436 case ARM::ATOMIC_LOAD_OR_I8:
3437 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3438 case ARM::ATOMIC_LOAD_OR_I16:
3439 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3440 case ARM::ATOMIC_LOAD_OR_I32:
3441 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003442
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003443 case ARM::ATOMIC_LOAD_XOR_I8:
3444 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3445 case ARM::ATOMIC_LOAD_XOR_I16:
3446 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3447 case ARM::ATOMIC_LOAD_XOR_I32:
3448 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003449
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003450 case ARM::ATOMIC_LOAD_NAND_I8:
3451 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3452 case ARM::ATOMIC_LOAD_NAND_I16:
3453 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3454 case ARM::ATOMIC_LOAD_NAND_I32:
3455 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003456
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003457 case ARM::ATOMIC_LOAD_SUB_I8:
3458 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3459 case ARM::ATOMIC_LOAD_SUB_I16:
3460 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3461 case ARM::ATOMIC_LOAD_SUB_I32:
3462 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003463
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003464 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
3465 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
3466 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00003467
3468 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
3469 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
3470 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003471
Evan Cheng007ea272009-08-12 05:17:19 +00003472 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00003473 // To "insert" a SELECT_CC instruction, we actually have to insert the
3474 // diamond control-flow pattern. The incoming instruction knows the
3475 // destination vreg to set, the condition code register to branch on, the
3476 // true/false values to select between, and a branch opcode to use.
3477 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003478 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00003479 ++It;
3480
3481 // thisMBB:
3482 // ...
3483 // TrueVal = ...
3484 // cmpTY ccX, r1, r2
3485 // bCC copy1MBB
3486 // fallthrough --> copy0MBB
3487 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003488 MachineFunction *F = BB->getParent();
3489 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3490 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00003491 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00003492 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003493 F->insert(It, copy0MBB);
3494 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00003495 // Update machine-CFG edges by first adding all successors of the current
3496 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00003497 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003498 E = BB->succ_end(); I != E; ++I)
Evan Chengce319102009-09-19 09:51:03 +00003499 sinkMBB->addSuccessor(*I);
Evan Chenga8e29892007-01-19 07:51:42 +00003500 // Next, remove all successors of the current block, and add the true
3501 // and fallthrough blocks as its successors.
Evan Chengce319102009-09-19 09:51:03 +00003502 while (!BB->succ_empty())
Evan Chenga8e29892007-01-19 07:51:42 +00003503 BB->removeSuccessor(BB->succ_begin());
3504 BB->addSuccessor(copy0MBB);
3505 BB->addSuccessor(sinkMBB);
3506
3507 // copy0MBB:
3508 // %FalseValue = ...
3509 // # fallthrough to sinkMBB
3510 BB = copy0MBB;
3511
3512 // Update machine-CFG edges
3513 BB->addSuccessor(sinkMBB);
3514
3515 // sinkMBB:
3516 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3517 // ...
3518 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00003519 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00003520 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
3521 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
3522
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003523 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00003524 return BB;
3525 }
Evan Cheng86198642009-08-07 00:34:42 +00003526
3527 case ARM::tANDsp:
3528 case ARM::tADDspr_:
3529 case ARM::tSUBspi_:
3530 case ARM::t2SUBrSPi_:
3531 case ARM::t2SUBrSPi12_:
3532 case ARM::t2SUBrSPs_: {
3533 MachineFunction *MF = BB->getParent();
3534 unsigned DstReg = MI->getOperand(0).getReg();
3535 unsigned SrcReg = MI->getOperand(1).getReg();
3536 bool DstIsDead = MI->getOperand(0).isDead();
3537 bool SrcIsKill = MI->getOperand(1).isKill();
3538
3539 if (SrcReg != ARM::SP) {
3540 // Copy the source to SP from virtual register.
3541 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
3542 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3543 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
3544 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
3545 .addReg(SrcReg, getKillRegState(SrcIsKill));
3546 }
3547
3548 unsigned OpOpc = 0;
3549 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
3550 switch (MI->getOpcode()) {
3551 default:
3552 llvm_unreachable("Unexpected pseudo instruction!");
3553 case ARM::tANDsp:
3554 OpOpc = ARM::tAND;
3555 NeedPred = true;
3556 break;
3557 case ARM::tADDspr_:
3558 OpOpc = ARM::tADDspr;
3559 break;
3560 case ARM::tSUBspi_:
3561 OpOpc = ARM::tSUBspi;
3562 break;
3563 case ARM::t2SUBrSPi_:
3564 OpOpc = ARM::t2SUBrSPi;
3565 NeedPred = true; NeedCC = true;
3566 break;
3567 case ARM::t2SUBrSPi12_:
3568 OpOpc = ARM::t2SUBrSPi12;
3569 NeedPred = true;
3570 break;
3571 case ARM::t2SUBrSPs_:
3572 OpOpc = ARM::t2SUBrSPs;
3573 NeedPred = true; NeedCC = true; NeedOp3 = true;
3574 break;
3575 }
3576 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
3577 if (OpOpc == ARM::tAND)
3578 AddDefaultT1CC(MIB);
3579 MIB.addReg(ARM::SP);
3580 MIB.addOperand(MI->getOperand(2));
3581 if (NeedOp3)
3582 MIB.addOperand(MI->getOperand(3));
3583 if (NeedPred)
3584 AddDefaultPred(MIB);
3585 if (NeedCC)
3586 AddDefaultCC(MIB);
3587
3588 // Copy the result from SP to virtual register.
3589 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
3590 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3591 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
3592 BuildMI(BB, dl, TII->get(CopyOpc))
3593 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
3594 .addReg(ARM::SP);
3595 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3596 return BB;
3597 }
Evan Chenga8e29892007-01-19 07:51:42 +00003598 }
3599}
3600
3601//===----------------------------------------------------------------------===//
3602// ARM Optimization Hooks
3603//===----------------------------------------------------------------------===//
3604
Chris Lattnerd1980a52009-03-12 06:52:53 +00003605static
3606SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3607 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00003608 SelectionDAG &DAG = DCI.DAG;
3609 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003610 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00003611 unsigned Opc = N->getOpcode();
3612 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3613 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3614 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3615 ISD::CondCode CC = ISD::SETCC_INVALID;
3616
3617 if (isSlctCC) {
3618 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
3619 } else {
3620 SDValue CCOp = Slct.getOperand(0);
3621 if (CCOp.getOpcode() == ISD::SETCC)
3622 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
3623 }
3624
3625 bool DoXform = false;
3626 bool InvCC = false;
3627 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
3628 "Bad input!");
3629
3630 if (LHS.getOpcode() == ISD::Constant &&
3631 cast<ConstantSDNode>(LHS)->isNullValue()) {
3632 DoXform = true;
3633 } else if (CC != ISD::SETCC_INVALID &&
3634 RHS.getOpcode() == ISD::Constant &&
3635 cast<ConstantSDNode>(RHS)->isNullValue()) {
3636 std::swap(LHS, RHS);
3637 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003638 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00003639 Op0.getOperand(0).getValueType();
3640 bool isInt = OpVT.isInteger();
3641 CC = ISD::getSetCCInverse(CC, isInt);
3642
3643 if (!TLI.isCondCodeLegal(CC, OpVT))
3644 return SDValue(); // Inverse operator isn't legal.
3645
3646 DoXform = true;
3647 InvCC = true;
3648 }
3649
3650 if (DoXform) {
3651 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3652 if (isSlctCC)
3653 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3654 Slct.getOperand(0), Slct.getOperand(1), CC);
3655 SDValue CCOp = Slct.getOperand(0);
3656 if (InvCC)
3657 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3658 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3659 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3660 CCOp, OtherOp, Result);
3661 }
3662 return SDValue();
3663}
3664
3665/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3666static SDValue PerformADDCombine(SDNode *N,
3667 TargetLowering::DAGCombinerInfo &DCI) {
3668 // added by evan in r37685 with no testcase.
3669 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003670
Chris Lattnerd1980a52009-03-12 06:52:53 +00003671 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3672 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3673 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3674 if (Result.getNode()) return Result;
3675 }
3676 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3677 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3678 if (Result.getNode()) return Result;
3679 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003680
Chris Lattnerd1980a52009-03-12 06:52:53 +00003681 return SDValue();
3682}
3683
3684/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3685static SDValue PerformSUBCombine(SDNode *N,
3686 TargetLowering::DAGCombinerInfo &DCI) {
3687 // added by evan in r37685 with no testcase.
3688 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003689
Chris Lattnerd1980a52009-03-12 06:52:53 +00003690 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3691 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3692 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3693 if (Result.getNode()) return Result;
3694 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003695
Chris Lattnerd1980a52009-03-12 06:52:53 +00003696 return SDValue();
3697}
3698
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003699static SDValue PerformMULCombine(SDNode *N,
3700 TargetLowering::DAGCombinerInfo &DCI,
3701 const ARMSubtarget *Subtarget) {
3702 SelectionDAG &DAG = DCI.DAG;
3703
3704 if (Subtarget->isThumb1Only())
3705 return SDValue();
3706
3707 if (DAG.getMachineFunction().
3708 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
3709 return SDValue();
3710
3711 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
3712 return SDValue();
3713
3714 EVT VT = N->getValueType(0);
3715 if (VT != MVT::i32)
3716 return SDValue();
3717
3718 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
3719 if (!C)
3720 return SDValue();
3721
3722 uint64_t MulAmt = C->getZExtValue();
3723 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
3724 ShiftAmt = ShiftAmt & (32 - 1);
3725 SDValue V = N->getOperand(0);
3726 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003727
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003728 SDValue Res;
3729 MulAmt >>= ShiftAmt;
3730 if (isPowerOf2_32(MulAmt - 1)) {
3731 // (mul x, 2^N + 1) => (add (shl x, N), x)
3732 Res = DAG.getNode(ISD::ADD, DL, VT,
3733 V, DAG.getNode(ISD::SHL, DL, VT,
3734 V, DAG.getConstant(Log2_32(MulAmt-1),
3735 MVT::i32)));
3736 } else if (isPowerOf2_32(MulAmt + 1)) {
3737 // (mul x, 2^N - 1) => (sub (shl x, N), x)
3738 Res = DAG.getNode(ISD::SUB, DL, VT,
3739 DAG.getNode(ISD::SHL, DL, VT,
3740 V, DAG.getConstant(Log2_32(MulAmt+1),
3741 MVT::i32)),
3742 V);
3743 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003744 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003745
3746 if (ShiftAmt != 0)
3747 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
3748 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003749
3750 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003751 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003752 return SDValue();
3753}
3754
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +00003755/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
3756/// ARMISD::VMOVRRD.
Jim Grosbache5165492009-11-09 00:11:35 +00003757static SDValue PerformVMOVRRDCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003758 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003759 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003760 SDValue InDouble = N->getOperand(0);
Jim Grosbache5165492009-11-09 00:11:35 +00003761 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003762 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003763 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003764}
3765
Bob Wilson5bafff32009-06-22 23:27:02 +00003766/// getVShiftImm - Check if this is a valid build_vector for the immediate
3767/// operand of a vector shift operation, where all the elements of the
3768/// build_vector must have the same constant integer value.
3769static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3770 // Ignore bit_converts.
3771 while (Op.getOpcode() == ISD::BIT_CONVERT)
3772 Op = Op.getOperand(0);
3773 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3774 APInt SplatBits, SplatUndef;
3775 unsigned SplatBitSize;
3776 bool HasAnyUndefs;
3777 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3778 HasAnyUndefs, ElementBits) ||
3779 SplatBitSize > ElementBits)
3780 return false;
3781 Cnt = SplatBits.getSExtValue();
3782 return true;
3783}
3784
3785/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3786/// operand of a vector shift left operation. That value must be in the range:
3787/// 0 <= Value < ElementBits for a left shift; or
3788/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003789static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003790 assert(VT.isVector() && "vector shift count is not a vector type");
3791 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3792 if (! getVShiftImm(Op, ElementBits, Cnt))
3793 return false;
3794 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3795}
3796
3797/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3798/// operand of a vector shift right operation. For a shift opcode, the value
3799/// is positive, but for an intrinsic the value count must be negative. The
3800/// absolute value must be in the range:
3801/// 1 <= |Value| <= ElementBits for a right shift; or
3802/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003803static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00003804 int64_t &Cnt) {
3805 assert(VT.isVector() && "vector shift count is not a vector type");
3806 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3807 if (! getVShiftImm(Op, ElementBits, Cnt))
3808 return false;
3809 if (isIntrinsic)
3810 Cnt = -Cnt;
3811 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3812}
3813
3814/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3815static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3816 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3817 switch (IntNo) {
3818 default:
3819 // Don't do anything for most intrinsics.
3820 break;
3821
3822 // Vector shifts: check for immediate versions and lower them.
3823 // Note: This is done during DAG combining instead of DAG legalizing because
3824 // the build_vectors for 64-bit vector element shift counts are generally
3825 // not legal, and it is hard to see their values after they get legalized to
3826 // loads from a constant pool.
3827 case Intrinsic::arm_neon_vshifts:
3828 case Intrinsic::arm_neon_vshiftu:
3829 case Intrinsic::arm_neon_vshiftls:
3830 case Intrinsic::arm_neon_vshiftlu:
3831 case Intrinsic::arm_neon_vshiftn:
3832 case Intrinsic::arm_neon_vrshifts:
3833 case Intrinsic::arm_neon_vrshiftu:
3834 case Intrinsic::arm_neon_vrshiftn:
3835 case Intrinsic::arm_neon_vqshifts:
3836 case Intrinsic::arm_neon_vqshiftu:
3837 case Intrinsic::arm_neon_vqshiftsu:
3838 case Intrinsic::arm_neon_vqshiftns:
3839 case Intrinsic::arm_neon_vqshiftnu:
3840 case Intrinsic::arm_neon_vqshiftnsu:
3841 case Intrinsic::arm_neon_vqrshiftns:
3842 case Intrinsic::arm_neon_vqrshiftnu:
3843 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00003844 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003845 int64_t Cnt;
3846 unsigned VShiftOpc = 0;
3847
3848 switch (IntNo) {
3849 case Intrinsic::arm_neon_vshifts:
3850 case Intrinsic::arm_neon_vshiftu:
3851 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3852 VShiftOpc = ARMISD::VSHL;
3853 break;
3854 }
3855 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3856 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3857 ARMISD::VSHRs : ARMISD::VSHRu);
3858 break;
3859 }
3860 return SDValue();
3861
3862 case Intrinsic::arm_neon_vshiftls:
3863 case Intrinsic::arm_neon_vshiftlu:
3864 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3865 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003866 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003867
3868 case Intrinsic::arm_neon_vrshifts:
3869 case Intrinsic::arm_neon_vrshiftu:
3870 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3871 break;
3872 return SDValue();
3873
3874 case Intrinsic::arm_neon_vqshifts:
3875 case Intrinsic::arm_neon_vqshiftu:
3876 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3877 break;
3878 return SDValue();
3879
3880 case Intrinsic::arm_neon_vqshiftsu:
3881 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3882 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003883 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003884
3885 case Intrinsic::arm_neon_vshiftn:
3886 case Intrinsic::arm_neon_vrshiftn:
3887 case Intrinsic::arm_neon_vqshiftns:
3888 case Intrinsic::arm_neon_vqshiftnu:
3889 case Intrinsic::arm_neon_vqshiftnsu:
3890 case Intrinsic::arm_neon_vqrshiftns:
3891 case Intrinsic::arm_neon_vqrshiftnu:
3892 case Intrinsic::arm_neon_vqrshiftnsu:
3893 // Narrowing shifts require an immediate right shift.
3894 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3895 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003896 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003897
3898 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003899 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003900 }
3901
3902 switch (IntNo) {
3903 case Intrinsic::arm_neon_vshifts:
3904 case Intrinsic::arm_neon_vshiftu:
3905 // Opcode already set above.
3906 break;
3907 case Intrinsic::arm_neon_vshiftls:
3908 case Intrinsic::arm_neon_vshiftlu:
3909 if (Cnt == VT.getVectorElementType().getSizeInBits())
3910 VShiftOpc = ARMISD::VSHLLi;
3911 else
3912 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3913 ARMISD::VSHLLs : ARMISD::VSHLLu);
3914 break;
3915 case Intrinsic::arm_neon_vshiftn:
3916 VShiftOpc = ARMISD::VSHRN; break;
3917 case Intrinsic::arm_neon_vrshifts:
3918 VShiftOpc = ARMISD::VRSHRs; break;
3919 case Intrinsic::arm_neon_vrshiftu:
3920 VShiftOpc = ARMISD::VRSHRu; break;
3921 case Intrinsic::arm_neon_vrshiftn:
3922 VShiftOpc = ARMISD::VRSHRN; break;
3923 case Intrinsic::arm_neon_vqshifts:
3924 VShiftOpc = ARMISD::VQSHLs; break;
3925 case Intrinsic::arm_neon_vqshiftu:
3926 VShiftOpc = ARMISD::VQSHLu; break;
3927 case Intrinsic::arm_neon_vqshiftsu:
3928 VShiftOpc = ARMISD::VQSHLsu; break;
3929 case Intrinsic::arm_neon_vqshiftns:
3930 VShiftOpc = ARMISD::VQSHRNs; break;
3931 case Intrinsic::arm_neon_vqshiftnu:
3932 VShiftOpc = ARMISD::VQSHRNu; break;
3933 case Intrinsic::arm_neon_vqshiftnsu:
3934 VShiftOpc = ARMISD::VQSHRNsu; break;
3935 case Intrinsic::arm_neon_vqrshiftns:
3936 VShiftOpc = ARMISD::VQRSHRNs; break;
3937 case Intrinsic::arm_neon_vqrshiftnu:
3938 VShiftOpc = ARMISD::VQRSHRNu; break;
3939 case Intrinsic::arm_neon_vqrshiftnsu:
3940 VShiftOpc = ARMISD::VQRSHRNsu; break;
3941 }
3942
3943 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003944 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003945 }
3946
3947 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00003948 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003949 int64_t Cnt;
3950 unsigned VShiftOpc = 0;
3951
3952 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3953 VShiftOpc = ARMISD::VSLI;
3954 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3955 VShiftOpc = ARMISD::VSRI;
3956 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003957 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003958 }
3959
3960 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3961 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00003962 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003963 }
3964
3965 case Intrinsic::arm_neon_vqrshifts:
3966 case Intrinsic::arm_neon_vqrshiftu:
3967 // No immediate versions of these to check for.
3968 break;
3969 }
3970
3971 return SDValue();
3972}
3973
3974/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3975/// lowers them. As with the vector shift intrinsics, this is done during DAG
3976/// combining instead of DAG legalizing because the build_vectors for 64-bit
3977/// vector element shift counts are generally not legal, and it is hard to see
3978/// their values after they get legalized to loads from a constant pool.
3979static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3980 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003981 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003982
3983 // Nothing to be done for scalar shifts.
3984 if (! VT.isVector())
3985 return SDValue();
3986
3987 assert(ST->hasNEON() && "unexpected vector shift");
3988 int64_t Cnt;
3989
3990 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003991 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003992
3993 case ISD::SHL:
3994 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3995 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003996 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003997 break;
3998
3999 case ISD::SRA:
4000 case ISD::SRL:
4001 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
4002 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
4003 ARMISD::VSHRs : ARMISD::VSHRu);
4004 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004005 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004006 }
4007 }
4008 return SDValue();
4009}
4010
4011/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
4012/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
4013static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
4014 const ARMSubtarget *ST) {
4015 SDValue N0 = N->getOperand(0);
4016
4017 // Check for sign- and zero-extensions of vector extract operations of 8-
4018 // and 16-bit vector elements. NEON supports these directly. They are
4019 // handled during DAG combining because type legalization will promote them
4020 // to 32-bit types and it is messy to recognize the operations after that.
4021 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
4022 SDValue Vec = N0.getOperand(0);
4023 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004024 EVT VT = N->getValueType(0);
4025 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004026 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4027
Owen Anderson825b72b2009-08-11 20:47:22 +00004028 if (VT == MVT::i32 &&
4029 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00004030 TLI.isTypeLegal(Vec.getValueType())) {
4031
4032 unsigned Opc = 0;
4033 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004034 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00004035 case ISD::SIGN_EXTEND:
4036 Opc = ARMISD::VGETLANEs;
4037 break;
4038 case ISD::ZERO_EXTEND:
4039 case ISD::ANY_EXTEND:
4040 Opc = ARMISD::VGETLANEu;
4041 break;
4042 }
4043 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
4044 }
4045 }
4046
4047 return SDValue();
4048}
4049
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004050/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
4051/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
4052static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
4053 const ARMSubtarget *ST) {
4054 // If the target supports NEON, try to use vmax/vmin instructions for f32
4055 // selects like "x < y ? x : y". Unless the FiniteOnlyFPMath option is set,
4056 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
4057 // a NaN; only do the transformation when it matches that behavior.
4058
4059 // For now only do this when using NEON for FP operations; if using VFP, it
4060 // is not obvious that the benefit outweighs the cost of switching to the
4061 // NEON pipeline.
4062 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
4063 N->getValueType(0) != MVT::f32)
4064 return SDValue();
4065
4066 SDValue CondLHS = N->getOperand(0);
4067 SDValue CondRHS = N->getOperand(1);
4068 SDValue LHS = N->getOperand(2);
4069 SDValue RHS = N->getOperand(3);
4070 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
4071
4072 unsigned Opcode = 0;
4073 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00004074 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004075 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00004076 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004077 IsReversed = true ; // x CC y ? y : x
4078 } else {
4079 return SDValue();
4080 }
4081
Bob Wilsone742bb52010-02-24 22:15:53 +00004082 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004083 switch (CC) {
4084 default: break;
4085 case ISD::SETOLT:
4086 case ISD::SETOLE:
4087 case ISD::SETLT:
4088 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004089 case ISD::SETULT:
4090 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004091 // If LHS is NaN, an ordered comparison will be false and the result will
4092 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
4093 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4094 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
4095 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4096 break;
4097 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
4098 // will return -0, so vmin can only be used for unsafe math or if one of
4099 // the operands is known to be nonzero.
4100 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
4101 !UnsafeFPMath &&
4102 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4103 break;
4104 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004105 break;
4106
4107 case ISD::SETOGT:
4108 case ISD::SETOGE:
4109 case ISD::SETGT:
4110 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004111 case ISD::SETUGT:
4112 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004113 // If LHS is NaN, an ordered comparison will be false and the result will
4114 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
4115 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4116 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
4117 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4118 break;
4119 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
4120 // will return +0, so vmax can only be used for unsafe math or if one of
4121 // the operands is known to be nonzero.
4122 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
4123 !UnsafeFPMath &&
4124 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4125 break;
4126 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004127 break;
4128 }
4129
4130 if (!Opcode)
4131 return SDValue();
4132 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
4133}
4134
Dan Gohman475871a2008-07-27 21:46:04 +00004135SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004136 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004137 switch (N->getOpcode()) {
4138 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004139 case ISD::ADD: return PerformADDCombine(N, DCI);
4140 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004141 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbache5165492009-11-09 00:11:35 +00004142 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004143 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004144 case ISD::SHL:
4145 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004146 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004147 case ISD::SIGN_EXTEND:
4148 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004149 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
4150 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004151 }
Dan Gohman475871a2008-07-27 21:46:04 +00004152 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004153}
4154
Bill Wendlingaf566342009-08-15 21:21:19 +00004155bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
4156 if (!Subtarget->hasV6Ops())
4157 // Pre-v6 does not support unaligned mem access.
4158 return false;
Anton Korobeynikov90cfc132010-01-30 14:08:12 +00004159 else {
4160 // v6+ may or may not support unaligned mem access depending on the system
4161 // configuration.
4162 // FIXME: This is pretty conservative. Should we provide cmdline option to
4163 // control the behaviour?
Bill Wendlingaf566342009-08-15 21:21:19 +00004164 if (!Subtarget->isTargetDarwin())
4165 return false;
4166 }
4167
4168 switch (VT.getSimpleVT().SimpleTy) {
4169 default:
4170 return false;
4171 case MVT::i8:
4172 case MVT::i16:
4173 case MVT::i32:
4174 return true;
4175 // FIXME: VLD1 etc with standard alignment is legal.
4176 }
4177}
4178
Evan Chenge6c835f2009-08-14 20:09:37 +00004179static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
4180 if (V < 0)
4181 return false;
4182
4183 unsigned Scale = 1;
4184 switch (VT.getSimpleVT().SimpleTy) {
4185 default: return false;
4186 case MVT::i1:
4187 case MVT::i8:
4188 // Scale == 1;
4189 break;
4190 case MVT::i16:
4191 // Scale == 2;
4192 Scale = 2;
4193 break;
4194 case MVT::i32:
4195 // Scale == 4;
4196 Scale = 4;
4197 break;
4198 }
4199
4200 if ((V & (Scale - 1)) != 0)
4201 return false;
4202 V /= Scale;
4203 return V == (V & ((1LL << 5) - 1));
4204}
4205
4206static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
4207 const ARMSubtarget *Subtarget) {
4208 bool isNeg = false;
4209 if (V < 0) {
4210 isNeg = true;
4211 V = - V;
4212 }
4213
4214 switch (VT.getSimpleVT().SimpleTy) {
4215 default: return false;
4216 case MVT::i1:
4217 case MVT::i8:
4218 case MVT::i16:
4219 case MVT::i32:
4220 // + imm12 or - imm8
4221 if (isNeg)
4222 return V == (V & ((1LL << 8) - 1));
4223 return V == (V & ((1LL << 12) - 1));
4224 case MVT::f32:
4225 case MVT::f64:
4226 // Same as ARM mode. FIXME: NEON?
4227 if (!Subtarget->hasVFP2())
4228 return false;
4229 if ((V & 3) != 0)
4230 return false;
4231 V >>= 2;
4232 return V == (V & ((1LL << 8) - 1));
4233 }
4234}
4235
Evan Chengb01fad62007-03-12 23:30:29 +00004236/// isLegalAddressImmediate - Return true if the integer value can be used
4237/// as the offset of the target addressing mode for load / store of the
4238/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00004239static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004240 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00004241 if (V == 0)
4242 return true;
4243
Evan Cheng65011532009-03-09 19:15:00 +00004244 if (!VT.isSimple())
4245 return false;
4246
Evan Chenge6c835f2009-08-14 20:09:37 +00004247 if (Subtarget->isThumb1Only())
4248 return isLegalT1AddressImmediate(V, VT);
4249 else if (Subtarget->isThumb2())
4250 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00004251
Evan Chenge6c835f2009-08-14 20:09:37 +00004252 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00004253 if (V < 0)
4254 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00004255 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00004256 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004257 case MVT::i1:
4258 case MVT::i8:
4259 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00004260 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004261 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004262 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00004263 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004264 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004265 case MVT::f32:
4266 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00004267 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00004268 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00004269 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00004270 return false;
4271 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004272 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00004273 }
Evan Chenga8e29892007-01-19 07:51:42 +00004274}
4275
Evan Chenge6c835f2009-08-14 20:09:37 +00004276bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
4277 EVT VT) const {
4278 int Scale = AM.Scale;
4279 if (Scale < 0)
4280 return false;
4281
4282 switch (VT.getSimpleVT().SimpleTy) {
4283 default: return false;
4284 case MVT::i1:
4285 case MVT::i8:
4286 case MVT::i16:
4287 case MVT::i32:
4288 if (Scale == 1)
4289 return true;
4290 // r + r << imm
4291 Scale = Scale & ~1;
4292 return Scale == 2 || Scale == 4 || Scale == 8;
4293 case MVT::i64:
4294 // r + r
4295 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
4296 return true;
4297 return false;
4298 case MVT::isVoid:
4299 // Note, we allow "void" uses (basically, uses that aren't loads or
4300 // stores), because arm allows folding a scale into many arithmetic
4301 // operations. This should be made more precise and revisited later.
4302
4303 // Allow r << imm, but the imm has to be a multiple of two.
4304 if (Scale & 1) return false;
4305 return isPowerOf2_32(Scale);
4306 }
4307}
4308
Chris Lattner37caf8c2007-04-09 23:33:39 +00004309/// isLegalAddressingMode - Return true if the addressing mode represented
4310/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004311bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004312 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004313 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00004314 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00004315 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004316
Chris Lattner37caf8c2007-04-09 23:33:39 +00004317 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004318 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004319 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004320
Chris Lattner37caf8c2007-04-09 23:33:39 +00004321 switch (AM.Scale) {
4322 case 0: // no scale reg, must be "r+i" or "r", or "i".
4323 break;
4324 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00004325 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00004326 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004327 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00004328 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004329 // ARM doesn't support any R+R*scale+imm addr modes.
4330 if (AM.BaseOffs)
4331 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004332
Bob Wilson2c7dab12009-04-08 17:55:28 +00004333 if (!VT.isSimple())
4334 return false;
4335
Evan Chenge6c835f2009-08-14 20:09:37 +00004336 if (Subtarget->isThumb2())
4337 return isLegalT2ScaledAddressingMode(AM, VT);
4338
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004339 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00004340 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00004341 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004342 case MVT::i1:
4343 case MVT::i8:
4344 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004345 if (Scale < 0) Scale = -Scale;
4346 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004347 return true;
4348 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00004349 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00004350 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00004351 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004352 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004353 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004354 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00004355 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004356
Owen Anderson825b72b2009-08-11 20:47:22 +00004357 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004358 // Note, we allow "void" uses (basically, uses that aren't loads or
4359 // stores), because arm allows folding a scale into many arithmetic
4360 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004361
Chris Lattner37caf8c2007-04-09 23:33:39 +00004362 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00004363 if (Scale & 1) return false;
4364 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00004365 }
4366 break;
Evan Chengb01fad62007-03-12 23:30:29 +00004367 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00004368 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00004369}
4370
Evan Cheng77e47512009-11-11 19:05:52 +00004371/// isLegalICmpImmediate - Return true if the specified immediate is legal
4372/// icmp immediate, that is the target has icmp instructions which can compare
4373/// a register against the immediate without having to materialize the
4374/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00004375bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00004376 if (!Subtarget->isThumb())
4377 return ARM_AM::getSOImmVal(Imm) != -1;
4378 if (Subtarget->isThumb2())
4379 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00004380 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00004381}
4382
Owen Andersone50ed302009-08-10 22:56:29 +00004383static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004384 bool isSEXTLoad, SDValue &Base,
4385 SDValue &Offset, bool &isInc,
4386 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00004387 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4388 return false;
4389
Owen Anderson825b72b2009-08-11 20:47:22 +00004390 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00004391 // AddressingMode 3
4392 Base = Ptr->getOperand(0);
4393 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004394 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004395 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004396 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004397 isInc = false;
4398 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4399 return true;
4400 }
4401 }
4402 isInc = (Ptr->getOpcode() == ISD::ADD);
4403 Offset = Ptr->getOperand(1);
4404 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004405 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00004406 // AddressingMode 2
4407 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004408 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004409 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004410 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004411 isInc = false;
4412 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4413 Base = Ptr->getOperand(0);
4414 return true;
4415 }
4416 }
4417
4418 if (Ptr->getOpcode() == ISD::ADD) {
4419 isInc = true;
4420 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
4421 if (ShOpcVal != ARM_AM::no_shift) {
4422 Base = Ptr->getOperand(1);
4423 Offset = Ptr->getOperand(0);
4424 } else {
4425 Base = Ptr->getOperand(0);
4426 Offset = Ptr->getOperand(1);
4427 }
4428 return true;
4429 }
4430
4431 isInc = (Ptr->getOpcode() == ISD::ADD);
4432 Base = Ptr->getOperand(0);
4433 Offset = Ptr->getOperand(1);
4434 return true;
4435 }
4436
Jim Grosbache5165492009-11-09 00:11:35 +00004437 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00004438 return false;
4439}
4440
Owen Andersone50ed302009-08-10 22:56:29 +00004441static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004442 bool isSEXTLoad, SDValue &Base,
4443 SDValue &Offset, bool &isInc,
4444 SelectionDAG &DAG) {
4445 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4446 return false;
4447
4448 Base = Ptr->getOperand(0);
4449 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
4450 int RHSC = (int)RHS->getZExtValue();
4451 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
4452 assert(Ptr->getOpcode() == ISD::ADD);
4453 isInc = false;
4454 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4455 return true;
4456 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
4457 isInc = Ptr->getOpcode() == ISD::ADD;
4458 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
4459 return true;
4460 }
4461 }
4462
4463 return false;
4464}
4465
Evan Chenga8e29892007-01-19 07:51:42 +00004466/// getPreIndexedAddressParts - returns true by value, base pointer and
4467/// offset pointer and addressing mode by reference if the node's address
4468/// can be legally represented as pre-indexed load / store address.
4469bool
Dan Gohman475871a2008-07-27 21:46:04 +00004470ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
4471 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004472 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004473 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004474 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004475 return false;
4476
Owen Andersone50ed302009-08-10 22:56:29 +00004477 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004478 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004479 bool isSEXTLoad = false;
4480 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4481 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004482 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004483 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4484 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
4485 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004486 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004487 } else
4488 return false;
4489
4490 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004491 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004492 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004493 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
4494 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004495 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004496 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00004497 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00004498 if (!isLegal)
4499 return false;
4500
4501 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
4502 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004503}
4504
4505/// getPostIndexedAddressParts - returns true by value, base pointer and
4506/// offset pointer and addressing mode by reference if this node can be
4507/// combined with a load / store to form a post-indexed load / store.
4508bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00004509 SDValue &Base,
4510 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004511 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004512 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004513 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004514 return false;
4515
Owen Andersone50ed302009-08-10 22:56:29 +00004516 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004517 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004518 bool isSEXTLoad = false;
4519 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004520 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00004521 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00004522 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4523 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004524 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00004525 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00004526 } else
4527 return false;
4528
4529 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004530 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004531 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004532 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00004533 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004534 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004535 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
4536 isInc, DAG);
4537 if (!isLegal)
4538 return false;
4539
Evan Cheng28dad2a2010-05-18 21:31:17 +00004540 if (Ptr != Base) {
4541 // Swap base ptr and offset to catch more post-index load / store when
4542 // it's legal. In Thumb2 mode, offset must be an immediate.
4543 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
4544 !Subtarget->isThumb2())
4545 std::swap(Base, Offset);
4546
4547 // Post-indexed load / store update the base pointer.
4548 if (Ptr != Base)
4549 return false;
4550 }
4551
Evan Chenge88d5ce2009-07-02 07:28:31 +00004552 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
4553 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004554}
4555
Dan Gohman475871a2008-07-27 21:46:04 +00004556void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00004557 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004558 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004559 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00004560 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00004561 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004562 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004563 switch (Op.getOpcode()) {
4564 default: break;
4565 case ARMISD::CMOV: {
4566 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00004567 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004568 if (KnownZero == 0 && KnownOne == 0) return;
4569
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004570 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00004571 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
4572 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004573 KnownZero &= KnownZeroRHS;
4574 KnownOne &= KnownOneRHS;
4575 return;
4576 }
4577 }
4578}
4579
4580//===----------------------------------------------------------------------===//
4581// ARM Inline Assembly Support
4582//===----------------------------------------------------------------------===//
4583
4584/// getConstraintType - Given a constraint letter, return the type of
4585/// constraint it is for this target.
4586ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00004587ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
4588 if (Constraint.size() == 1) {
4589 switch (Constraint[0]) {
4590 default: break;
4591 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004592 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00004593 }
Evan Chenga8e29892007-01-19 07:51:42 +00004594 }
Chris Lattner4234f572007-03-25 02:14:49 +00004595 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00004596}
4597
Bob Wilson2dc4f542009-03-20 22:42:55 +00004598std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00004599ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004600 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004601 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004602 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00004603 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004604 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004605 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004606 return std::make_pair(0U, ARM::tGPRRegisterClass);
4607 else
4608 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004609 case 'r':
4610 return std::make_pair(0U, ARM::GPRRegisterClass);
4611 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004612 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004613 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00004614 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004615 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00004616 if (VT.getSizeInBits() == 128)
4617 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004618 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004619 }
4620 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00004621 if (StringRef("{cc}").equals_lower(Constraint))
4622 return std::make_pair(0U, ARM::CCRRegisterClass);
4623
Evan Chenga8e29892007-01-19 07:51:42 +00004624 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
4625}
4626
4627std::vector<unsigned> ARMTargetLowering::
4628getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004629 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004630 if (Constraint.size() != 1)
4631 return std::vector<unsigned>();
4632
4633 switch (Constraint[0]) { // GCC ARM Constraint Letters
4634 default: break;
4635 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004636 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4637 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4638 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004639 case 'r':
4640 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4641 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4642 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
4643 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004644 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004645 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004646 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
4647 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
4648 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
4649 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
4650 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
4651 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
4652 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
4653 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00004654 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004655 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
4656 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
4657 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
4658 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00004659 if (VT.getSizeInBits() == 128)
4660 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
4661 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004662 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004663 }
4664
4665 return std::vector<unsigned>();
4666}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004667
4668/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
4669/// vector. If it is invalid, don't add anything to Ops.
4670void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
4671 char Constraint,
4672 bool hasMemory,
4673 std::vector<SDValue>&Ops,
4674 SelectionDAG &DAG) const {
4675 SDValue Result(0, 0);
4676
4677 switch (Constraint) {
4678 default: break;
4679 case 'I': case 'J': case 'K': case 'L':
4680 case 'M': case 'N': case 'O':
4681 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
4682 if (!C)
4683 return;
4684
4685 int64_t CVal64 = C->getSExtValue();
4686 int CVal = (int) CVal64;
4687 // None of these constraints allow values larger than 32 bits. Check
4688 // that the value fits in an int.
4689 if (CVal != CVal64)
4690 return;
4691
4692 switch (Constraint) {
4693 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004694 if (Subtarget->isThumb1Only()) {
4695 // This must be a constant between 0 and 255, for ADD
4696 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004697 if (CVal >= 0 && CVal <= 255)
4698 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004699 } else if (Subtarget->isThumb2()) {
4700 // A constant that can be used as an immediate value in a
4701 // data-processing instruction.
4702 if (ARM_AM::getT2SOImmVal(CVal) != -1)
4703 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004704 } else {
4705 // A constant that can be used as an immediate value in a
4706 // data-processing instruction.
4707 if (ARM_AM::getSOImmVal(CVal) != -1)
4708 break;
4709 }
4710 return;
4711
4712 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004713 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004714 // This must be a constant between -255 and -1, for negated ADD
4715 // immediates. This can be used in GCC with an "n" modifier that
4716 // prints the negated value, for use with SUB instructions. It is
4717 // not useful otherwise but is implemented for compatibility.
4718 if (CVal >= -255 && CVal <= -1)
4719 break;
4720 } else {
4721 // This must be a constant between -4095 and 4095. It is not clear
4722 // what this constraint is intended for. Implemented for
4723 // compatibility with GCC.
4724 if (CVal >= -4095 && CVal <= 4095)
4725 break;
4726 }
4727 return;
4728
4729 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004730 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004731 // A 32-bit value where only one byte has a nonzero value. Exclude
4732 // zero to match GCC. This constraint is used by GCC internally for
4733 // constants that can be loaded with a move/shift combination.
4734 // It is not useful otherwise but is implemented for compatibility.
4735 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
4736 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004737 } else if (Subtarget->isThumb2()) {
4738 // A constant whose bitwise inverse can be used as an immediate
4739 // value in a data-processing instruction. This can be used in GCC
4740 // with a "B" modifier that prints the inverted value, for use with
4741 // BIC and MVN instructions. It is not useful otherwise but is
4742 // implemented for compatibility.
4743 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
4744 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004745 } else {
4746 // A constant whose bitwise inverse can be used as an immediate
4747 // value in a data-processing instruction. This can be used in GCC
4748 // with a "B" modifier that prints the inverted value, for use with
4749 // BIC and MVN instructions. It is not useful otherwise but is
4750 // implemented for compatibility.
4751 if (ARM_AM::getSOImmVal(~CVal) != -1)
4752 break;
4753 }
4754 return;
4755
4756 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004757 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004758 // This must be a constant between -7 and 7,
4759 // for 3-operand ADD/SUB immediate instructions.
4760 if (CVal >= -7 && CVal < 7)
4761 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004762 } else if (Subtarget->isThumb2()) {
4763 // A constant whose negation can be used as an immediate value in a
4764 // data-processing instruction. This can be used in GCC with an "n"
4765 // modifier that prints the negated value, for use with SUB
4766 // instructions. It is not useful otherwise but is implemented for
4767 // compatibility.
4768 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
4769 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004770 } else {
4771 // A constant whose negation can be used as an immediate value in a
4772 // data-processing instruction. This can be used in GCC with an "n"
4773 // modifier that prints the negated value, for use with SUB
4774 // instructions. It is not useful otherwise but is implemented for
4775 // compatibility.
4776 if (ARM_AM::getSOImmVal(-CVal) != -1)
4777 break;
4778 }
4779 return;
4780
4781 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004782 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004783 // This must be a multiple of 4 between 0 and 1020, for
4784 // ADD sp + immediate.
4785 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
4786 break;
4787 } else {
4788 // A power of two or a constant between 0 and 32. This is used in
4789 // GCC for the shift amount on shifted register operands, but it is
4790 // useful in general for any shift amounts.
4791 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
4792 break;
4793 }
4794 return;
4795
4796 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004797 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004798 // This must be a constant between 0 and 31, for shift amounts.
4799 if (CVal >= 0 && CVal <= 31)
4800 break;
4801 }
4802 return;
4803
4804 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004805 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004806 // This must be a multiple of 4 between -508 and 508, for
4807 // ADD/SUB sp = sp + immediate.
4808 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
4809 break;
4810 }
4811 return;
4812 }
4813 Result = DAG.getTargetConstant(CVal, Op.getValueType());
4814 break;
4815 }
4816
4817 if (Result.getNode()) {
4818 Ops.push_back(Result);
4819 return;
4820 }
4821 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
4822 Ops, DAG);
4823}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00004824
4825bool
4826ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4827 // The ARM target isn't yet aware of offsets.
4828 return false;
4829}
Evan Cheng39382422009-10-28 01:44:26 +00004830
4831int ARM::getVFPf32Imm(const APFloat &FPImm) {
4832 APInt Imm = FPImm.bitcastToAPInt();
4833 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
4834 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
4835 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
4836
4837 // We can handle 4 bits of mantissa.
4838 // mantissa = (16+UInt(e:f:g:h))/16.
4839 if (Mantissa & 0x7ffff)
4840 return -1;
4841 Mantissa >>= 19;
4842 if ((Mantissa & 0xf) != Mantissa)
4843 return -1;
4844
4845 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4846 if (Exp < -3 || Exp > 4)
4847 return -1;
4848 Exp = ((Exp+3) & 0x7) ^ 4;
4849
4850 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4851}
4852
4853int ARM::getVFPf64Imm(const APFloat &FPImm) {
4854 APInt Imm = FPImm.bitcastToAPInt();
4855 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
4856 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
4857 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
4858
4859 // We can handle 4 bits of mantissa.
4860 // mantissa = (16+UInt(e:f:g:h))/16.
4861 if (Mantissa & 0xffffffffffffLL)
4862 return -1;
4863 Mantissa >>= 48;
4864 if ((Mantissa & 0xf) != Mantissa)
4865 return -1;
4866
4867 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4868 if (Exp < -3 || Exp > 4)
4869 return -1;
4870 Exp = ((Exp+3) & 0x7) ^ 4;
4871
4872 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4873}
4874
4875/// isFPImmLegal - Returns true if the target can instruction select the
4876/// specified FP immediate natively. If false, the legalizer will
4877/// materialize the FP immediate as a load from a constant pool.
4878bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
4879 if (!Subtarget->hasVFP3())
4880 return false;
4881 if (VT == MVT::f32)
4882 return ARM::getVFPf32Imm(Imm) != -1;
4883 if (VT == MVT::f64)
4884 return ARM::getVFPf64Imm(Imm) != -1;
4885 return false;
4886}