blob: ef2f07b70edfcb1fc95b0adf2b411959afd99705 [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);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000415
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::SETCC, MVT::i32, Expand);
417 setOperationAction(ISD::SETCC, MVT::f32, Expand);
418 setOperationAction(ISD::SETCC, MVT::f64, Expand);
419 setOperationAction(ISD::SELECT, MVT::i32, Expand);
420 setOperationAction(ISD::SELECT, MVT::f32, Expand);
421 setOperationAction(ISD::SELECT, MVT::f64, Expand);
422 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
423 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
424 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000425
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
427 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
428 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
429 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
430 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000431
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000432 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setOperationAction(ISD::FSIN, MVT::f64, Expand);
434 setOperationAction(ISD::FSIN, MVT::f32, Expand);
435 setOperationAction(ISD::FCOS, MVT::f32, Expand);
436 setOperationAction(ISD::FCOS, MVT::f64, Expand);
437 setOperationAction(ISD::FREM, MVT::f64, Expand);
438 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000439 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000440 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
441 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000442 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setOperationAction(ISD::FPOW, MVT::f64, Expand);
444 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000445
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000446 // Various VFP goodness
447 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000448 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
449 if (Subtarget->hasVFP2()) {
450 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
451 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
452 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
453 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
454 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000455 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000456 if (!Subtarget->hasFP16()) {
457 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
458 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000459 }
Evan Cheng110cf482008-04-01 01:50:16 +0000460 }
Evan Chenga8e29892007-01-19 07:51:42 +0000461
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000462 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000463 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000464 setTargetDAGCombine(ISD::ADD);
465 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000466 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000467
Evan Chenga8e29892007-01-19 07:51:42 +0000468 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000469
Evan Chengf7d87ee2010-05-21 00:43:17 +0000470 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
471 setSchedulingPreference(Sched::RegPressure);
472 else
473 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000474
Evan Chengbc9b7542009-08-15 07:59:10 +0000475 // FIXME: If-converter should use instruction latency to determine
476 // profitability rather than relying on fixed limits.
477 if (Subtarget->getCPUString() == "generic") {
478 // Generic (and overly aggressive) if-conversion limits.
479 setIfCvtBlockSizeLimit(10);
480 setIfCvtDupBlockSizeLimit(2);
Jim Grosbach35075a72010-03-24 16:15:14 +0000481 } else if (Subtarget->hasV7Ops()) {
Jim Grosbachfceabef2010-03-24 00:03:13 +0000482 setIfCvtBlockSizeLimit(3);
483 setIfCvtDupBlockSizeLimit(1);
Evan Chengbc9b7542009-08-15 07:59:10 +0000484 } else if (Subtarget->hasV6Ops()) {
485 setIfCvtBlockSizeLimit(2);
486 setIfCvtDupBlockSizeLimit(1);
487 } else {
488 setIfCvtBlockSizeLimit(3);
489 setIfCvtDupBlockSizeLimit(2);
Evan Cheng8557c2b2009-06-19 01:51:50 +0000490 }
491
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000492 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Bob Wilsone6abdff2009-05-18 20:55:32 +0000493 // Do not enable CodePlacementOpt for now: it currently runs after the
494 // ARMConstantIslandPass and messes up branch relaxation and placement
495 // of constant islands.
496 // benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000497}
498
Evan Chenga8e29892007-01-19 07:51:42 +0000499const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
500 switch (Opcode) {
501 default: return 0;
502 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000503 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
504 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000505 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000506 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
507 case ARMISD::tCALL: return "ARMISD::tCALL";
508 case ARMISD::BRCOND: return "ARMISD::BRCOND";
509 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000510 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000511 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
512 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
513 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000514 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000515 case ARMISD::CMPFP: return "ARMISD::CMPFP";
516 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
517 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
518 case ARMISD::CMOV: return "ARMISD::CMOV";
519 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000520
Jim Grosbach3482c802010-01-18 19:58:49 +0000521 case ARMISD::RBIT: return "ARMISD::RBIT";
522
Bob Wilson76a312b2010-03-19 22:51:32 +0000523 case ARMISD::FTOSI: return "ARMISD::FTOSI";
524 case ARMISD::FTOUI: return "ARMISD::FTOUI";
525 case ARMISD::SITOF: return "ARMISD::SITOF";
526 case ARMISD::UITOF: return "ARMISD::UITOF";
527
Evan Chenga8e29892007-01-19 07:51:42 +0000528 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
529 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
530 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000531
Jim Grosbache5165492009-11-09 00:11:35 +0000532 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
533 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000534
Evan Chengc5942082009-10-28 06:55:03 +0000535 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
536 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
537
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000538 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000539
Evan Cheng86198642009-08-07 00:34:42 +0000540 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
541
Jim Grosbach3728e962009-12-10 00:11:09 +0000542 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
543 case ARMISD::SYNCBARRIER: return "ARMISD::SYNCBARRIER";
544
Bob Wilson5bafff32009-06-22 23:27:02 +0000545 case ARMISD::VCEQ: return "ARMISD::VCEQ";
546 case ARMISD::VCGE: return "ARMISD::VCGE";
547 case ARMISD::VCGEU: return "ARMISD::VCGEU";
548 case ARMISD::VCGT: return "ARMISD::VCGT";
549 case ARMISD::VCGTU: return "ARMISD::VCGTU";
550 case ARMISD::VTST: return "ARMISD::VTST";
551
552 case ARMISD::VSHL: return "ARMISD::VSHL";
553 case ARMISD::VSHRs: return "ARMISD::VSHRs";
554 case ARMISD::VSHRu: return "ARMISD::VSHRu";
555 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
556 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
557 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
558 case ARMISD::VSHRN: return "ARMISD::VSHRN";
559 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
560 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
561 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
562 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
563 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
564 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
565 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
566 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
567 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
568 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
569 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
570 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
571 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
572 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000573 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000574 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000575 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000576 case ARMISD::VREV64: return "ARMISD::VREV64";
577 case ARMISD::VREV32: return "ARMISD::VREV32";
578 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000579 case ARMISD::VZIP: return "ARMISD::VZIP";
580 case ARMISD::VUZP: return "ARMISD::VUZP";
581 case ARMISD::VTRN: return "ARMISD::VTRN";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000582 case ARMISD::FMAX: return "ARMISD::FMAX";
583 case ARMISD::FMIN: return "ARMISD::FMIN";
Evan Chenga8e29892007-01-19 07:51:42 +0000584 }
585}
586
Evan Cheng06b666c2010-05-15 02:18:07 +0000587/// getRegClassFor - Return the register class that should be used for the
588/// specified value type.
589TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
590 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
591 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
592 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000593 if (Subtarget->hasNEON()) {
594 if (VT == MVT::v4i64)
595 return ARM::QQPRRegisterClass;
596 else if (VT == MVT::v8i64)
597 return ARM::QQQQPRRegisterClass;
598 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000599 return TargetLowering::getRegClassFor(VT);
600}
601
Bill Wendlingb4202b82009-07-01 18:50:55 +0000602/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000603unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Evan Cheng048e36f2009-10-02 06:57:25 +0000604 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 0 : 1;
Bill Wendling20c568f2009-06-30 22:38:32 +0000605}
606
Evan Cheng1cc39842010-05-20 23:26:43 +0000607Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000608 unsigned NumVals = N->getNumValues();
609 if (!NumVals)
610 return Sched::RegPressure;
611
612 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000613 EVT VT = N->getValueType(i);
614 if (VT.isFloatingPoint() || VT.isVector())
615 return Sched::Latency;
616 }
Evan Chengc10f5432010-05-28 23:25:23 +0000617
618 if (!N->isMachineOpcode())
619 return Sched::RegPressure;
620
621 // Load are scheduled for latency even if there instruction itinerary
622 // is not available.
623 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
624 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
625 if (TID.mayLoad())
626 return Sched::Latency;
627
628 const InstrItineraryData &Itins = getTargetMachine().getInstrItineraryData();
629 if (!Itins.isEmpty() && Itins.getStageLatency(TID.getSchedClass()) > 2)
630 return Sched::Latency;
Evan Cheng1cc39842010-05-20 23:26:43 +0000631 return Sched::RegPressure;
632}
633
Evan Chenga8e29892007-01-19 07:51:42 +0000634//===----------------------------------------------------------------------===//
635// Lowering Code
636//===----------------------------------------------------------------------===//
637
Evan Chenga8e29892007-01-19 07:51:42 +0000638/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
639static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
640 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000641 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000642 case ISD::SETNE: return ARMCC::NE;
643 case ISD::SETEQ: return ARMCC::EQ;
644 case ISD::SETGT: return ARMCC::GT;
645 case ISD::SETGE: return ARMCC::GE;
646 case ISD::SETLT: return ARMCC::LT;
647 case ISD::SETLE: return ARMCC::LE;
648 case ISD::SETUGT: return ARMCC::HI;
649 case ISD::SETUGE: return ARMCC::HS;
650 case ISD::SETULT: return ARMCC::LO;
651 case ISD::SETULE: return ARMCC::LS;
652 }
653}
654
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000655/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
656static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000657 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000658 CondCode2 = ARMCC::AL;
659 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000660 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000661 case ISD::SETEQ:
662 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
663 case ISD::SETGT:
664 case ISD::SETOGT: CondCode = ARMCC::GT; break;
665 case ISD::SETGE:
666 case ISD::SETOGE: CondCode = ARMCC::GE; break;
667 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000668 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000669 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
670 case ISD::SETO: CondCode = ARMCC::VC; break;
671 case ISD::SETUO: CondCode = ARMCC::VS; break;
672 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
673 case ISD::SETUGT: CondCode = ARMCC::HI; break;
674 case ISD::SETUGE: CondCode = ARMCC::PL; break;
675 case ISD::SETLT:
676 case ISD::SETULT: CondCode = ARMCC::LT; break;
677 case ISD::SETLE:
678 case ISD::SETULE: CondCode = ARMCC::LE; break;
679 case ISD::SETNE:
680 case ISD::SETUNE: CondCode = ARMCC::NE; break;
681 }
Evan Chenga8e29892007-01-19 07:51:42 +0000682}
683
Bob Wilson1f595bb2009-04-17 19:07:39 +0000684//===----------------------------------------------------------------------===//
685// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000686//===----------------------------------------------------------------------===//
687
688#include "ARMGenCallingConv.inc"
689
690// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000691static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000692 CCValAssign::LocInfo &LocInfo,
693 CCState &State, bool CanFail) {
694 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
695
696 // Try to get the first register.
697 if (unsigned Reg = State.AllocateReg(RegList, 4))
698 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
699 else {
700 // For the 2nd half of a v2f64, do not fail.
701 if (CanFail)
702 return false;
703
704 // Put the whole thing on the stack.
705 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
706 State.AllocateStack(8, 4),
707 LocVT, LocInfo));
708 return true;
709 }
710
711 // Try to get the second register.
712 if (unsigned Reg = State.AllocateReg(RegList, 4))
713 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
714 else
715 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
716 State.AllocateStack(4, 4),
717 LocVT, LocInfo));
718 return true;
719}
720
Owen Andersone50ed302009-08-10 22:56:29 +0000721static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000722 CCValAssign::LocInfo &LocInfo,
723 ISD::ArgFlagsTy &ArgFlags,
724 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000725 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
726 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000727 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000728 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
729 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000730 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000731}
732
733// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000734static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000735 CCValAssign::LocInfo &LocInfo,
736 CCState &State, bool CanFail) {
737 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
738 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
739
740 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
741 if (Reg == 0) {
742 // For the 2nd half of a v2f64, do not just fail.
743 if (CanFail)
744 return false;
745
746 // Put the whole thing on the stack.
747 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
748 State.AllocateStack(8, 8),
749 LocVT, LocInfo));
750 return true;
751 }
752
753 unsigned i;
754 for (i = 0; i < 2; ++i)
755 if (HiRegList[i] == Reg)
756 break;
757
758 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
759 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
760 LocVT, LocInfo));
761 return true;
762}
763
Owen Andersone50ed302009-08-10 22:56:29 +0000764static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000765 CCValAssign::LocInfo &LocInfo,
766 ISD::ArgFlagsTy &ArgFlags,
767 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000768 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
769 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000770 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000771 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
772 return false;
773 return true; // we handled it
774}
775
Owen Andersone50ed302009-08-10 22:56:29 +0000776static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000777 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000778 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
779 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
780
Bob Wilsone65586b2009-04-17 20:40:45 +0000781 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
782 if (Reg == 0)
783 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000784
Bob Wilsone65586b2009-04-17 20:40:45 +0000785 unsigned i;
786 for (i = 0; i < 2; ++i)
787 if (HiRegList[i] == Reg)
788 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000789
Bob Wilson5bafff32009-06-22 23:27:02 +0000790 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000791 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000792 LocVT, LocInfo));
793 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000794}
795
Owen Andersone50ed302009-08-10 22:56:29 +0000796static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000797 CCValAssign::LocInfo &LocInfo,
798 ISD::ArgFlagsTy &ArgFlags,
799 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000800 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
801 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000802 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000803 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000804 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000805}
806
Owen Andersone50ed302009-08-10 22:56:29 +0000807static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000808 CCValAssign::LocInfo &LocInfo,
809 ISD::ArgFlagsTy &ArgFlags,
810 CCState &State) {
811 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
812 State);
813}
814
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000815/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
816/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000817CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000818 bool Return,
819 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000820 switch (CC) {
821 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000822 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000823 case CallingConv::C:
824 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000825 // Use target triple & subtarget features to do actual dispatch.
826 if (Subtarget->isAAPCS_ABI()) {
827 if (Subtarget->hasVFP2() &&
828 FloatABIType == FloatABI::Hard && !isVarArg)
829 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
830 else
831 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
832 } else
833 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000834 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000835 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000836 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000837 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000838 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000839 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000840 }
841}
842
Dan Gohman98ca4f22009-08-05 01:29:28 +0000843/// LowerCallResult - Lower the result values of a call into the
844/// appropriate copies out of appropriate physical registers.
845SDValue
846ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000847 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000848 const SmallVectorImpl<ISD::InputArg> &Ins,
849 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000850 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000851
Bob Wilson1f595bb2009-04-17 19:07:39 +0000852 // Assign locations to each value returned by this call.
853 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000854 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000855 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000856 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000857 CCAssignFnForNode(CallConv, /* Return*/ true,
858 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000859
860 // Copy all of the result registers out of their specified physreg.
861 for (unsigned i = 0; i != RVLocs.size(); ++i) {
862 CCValAssign VA = RVLocs[i];
863
Bob Wilson80915242009-04-25 00:33:20 +0000864 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000865 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000866 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000867 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000868 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000869 Chain = Lo.getValue(1);
870 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000871 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000872 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000873 InFlag);
874 Chain = Hi.getValue(1);
875 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000876 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000877
Owen Anderson825b72b2009-08-11 20:47:22 +0000878 if (VA.getLocVT() == MVT::v2f64) {
879 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
880 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
881 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000882
883 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000884 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000885 Chain = Lo.getValue(1);
886 InFlag = Lo.getValue(2);
887 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000888 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000889 Chain = Hi.getValue(1);
890 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000891 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +0000892 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
893 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000894 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000895 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000896 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
897 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000898 Chain = Val.getValue(1);
899 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000900 }
Bob Wilson80915242009-04-25 00:33:20 +0000901
902 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000903 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000904 case CCValAssign::Full: break;
905 case CCValAssign::BCvt:
906 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
907 break;
908 }
909
Dan Gohman98ca4f22009-08-05 01:29:28 +0000910 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000911 }
912
Dan Gohman98ca4f22009-08-05 01:29:28 +0000913 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000914}
915
916/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
917/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000918/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000919/// a byval function parameter.
920/// Sometimes what we are copying is the end of a larger object, the part that
921/// does not fit in registers.
922static SDValue
923CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
924 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
925 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000926 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000927 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +0000928 /*isVolatile=*/false, /*AlwaysInline=*/false,
929 NULL, 0, NULL, 0);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000930}
931
Bob Wilsondee46d72009-04-17 20:35:10 +0000932/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000933SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000934ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
935 SDValue StackPtr, SDValue Arg,
936 DebugLoc dl, SelectionDAG &DAG,
937 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +0000938 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000939 unsigned LocMemOffset = VA.getLocMemOffset();
940 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
941 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
942 if (Flags.isByVal()) {
943 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
944 }
945 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene1b58cab2010-02-15 16:55:24 +0000946 PseudoSourceValue::getStack(), LocMemOffset,
947 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +0000948}
949
Dan Gohman98ca4f22009-08-05 01:29:28 +0000950void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000951 SDValue Chain, SDValue &Arg,
952 RegsToPassVector &RegsToPass,
953 CCValAssign &VA, CCValAssign &NextVA,
954 SDValue &StackPtr,
955 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +0000956 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +0000957
Jim Grosbache5165492009-11-09 00:11:35 +0000958 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000959 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000960 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
961
962 if (NextVA.isRegLoc())
963 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
964 else {
965 assert(NextVA.isMemLoc());
966 if (StackPtr.getNode() == 0)
967 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
968
Dan Gohman98ca4f22009-08-05 01:29:28 +0000969 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
970 dl, DAG, NextVA,
971 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000972 }
973}
974
Dan Gohman98ca4f22009-08-05 01:29:28 +0000975/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000976/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
977/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000978SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000979ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000980 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000981 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000982 const SmallVectorImpl<ISD::OutputArg> &Outs,
983 const SmallVectorImpl<ISD::InputArg> &Ins,
984 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000985 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +0000986 // ARM target does not yet support tail call optimization.
987 isTailCall = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000988
Bob Wilson1f595bb2009-04-17 19:07:39 +0000989 // Analyze operands of the call, assigning locations to each operand.
990 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000991 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
992 *DAG.getContext());
993 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000994 CCAssignFnForNode(CallConv, /* Return*/ false,
995 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +0000996
Bob Wilson1f595bb2009-04-17 19:07:39 +0000997 // Get a count of how many bytes are to be pushed on the stack.
998 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000999
1000 // Adjust the stack pointer for the new arguments...
1001 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +00001002 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001003
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001004 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001005
Bob Wilson5bafff32009-06-22 23:27:02 +00001006 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001007 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001008
Bob Wilson1f595bb2009-04-17 19:07:39 +00001009 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001010 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001011 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1012 i != e;
1013 ++i, ++realArgIdx) {
1014 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001015 SDValue Arg = Outs[realArgIdx].Val;
1016 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +00001017
Bob Wilson1f595bb2009-04-17 19:07:39 +00001018 // Promote the value if needed.
1019 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001020 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001021 case CCValAssign::Full: break;
1022 case CCValAssign::SExt:
1023 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1024 break;
1025 case CCValAssign::ZExt:
1026 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1027 break;
1028 case CCValAssign::AExt:
1029 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1030 break;
1031 case CCValAssign::BCvt:
1032 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1033 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001034 }
1035
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001036 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001037 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001038 if (VA.getLocVT() == MVT::v2f64) {
1039 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1040 DAG.getConstant(0, MVT::i32));
1041 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1042 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001043
Dan Gohman98ca4f22009-08-05 01:29:28 +00001044 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001045 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1046
1047 VA = ArgLocs[++i]; // skip ahead to next loc
1048 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001049 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001050 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1051 } else {
1052 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001053
Dan Gohman98ca4f22009-08-05 01:29:28 +00001054 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1055 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001056 }
1057 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001058 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001059 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001060 }
1061 } else if (VA.isRegLoc()) {
1062 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1063 } else {
1064 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001065
Dan Gohman98ca4f22009-08-05 01:29:28 +00001066 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1067 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001068 }
Evan Chenga8e29892007-01-19 07:51:42 +00001069 }
1070
1071 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001072 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001073 &MemOpChains[0], MemOpChains.size());
1074
1075 // Build a sequence of copy-to-reg nodes chained together with token chain
1076 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001077 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +00001078 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001079 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001080 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +00001081 InFlag = Chain.getValue(1);
1082 }
1083
Bill Wendling056292f2008-09-16 21:48:12 +00001084 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1085 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1086 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001087 bool isDirect = false;
1088 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001089 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001090 MachineFunction &MF = DAG.getMachineFunction();
1091 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001092
1093 if (EnableARMLongCalls) {
1094 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1095 && "long-calls with non-static relocation model!");
1096 // Handle a global address or an external symbol. If it's not one of
1097 // those, the target's already in a register, so we don't need to do
1098 // anything extra.
1099 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001100 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001101 // Create a constant pool entry for the callee address
1102 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1103 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1104 ARMPCLabelIndex,
1105 ARMCP::CPValue, 0);
1106 // Get the address of the callee into a register
1107 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1108 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1109 Callee = DAG.getLoad(getPointerTy(), dl,
1110 DAG.getEntryNode(), CPAddr,
1111 PseudoSourceValue::getConstantPool(), 0,
1112 false, false, 0);
1113 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1114 const char *Sym = S->getSymbol();
1115
1116 // Create a constant pool entry for the callee address
1117 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1118 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1119 Sym, ARMPCLabelIndex, 0);
1120 // Get the address of the callee into a register
1121 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1122 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1123 Callee = DAG.getLoad(getPointerTy(), dl,
1124 DAG.getEntryNode(), CPAddr,
1125 PseudoSourceValue::getConstantPool(), 0,
1126 false, false, 0);
1127 }
1128 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001129 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001130 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001131 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001132 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001133 getTargetMachine().getRelocationModel() != Reloc::Static;
1134 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001135 // ARM call to a local ARM function is predicable.
1136 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +00001137 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001138 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001139 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001140 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001141 ARMPCLabelIndex,
1142 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001143 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001144 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001145 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001146 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001147 PseudoSourceValue::getConstantPool(), 0,
1148 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001149 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001150 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001151 getPointerTy(), Callee, PICLabel);
Jim Grosbache7b52522010-04-14 22:28:31 +00001152 } else
Evan Chengc60e76d2007-01-30 20:37:08 +00001153 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001154 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001155 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001156 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001157 getTargetMachine().getRelocationModel() != Reloc::Static;
1158 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001159 // tBX takes a register source operand.
1160 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001161 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001162 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001163 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001164 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001165 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001166 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001167 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001168 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001169 PseudoSourceValue::getConstantPool(), 0,
1170 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001171 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001172 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001173 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001174 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001175 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001176 }
1177
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001178 // FIXME: handle tail calls differently.
1179 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001180 if (Subtarget->isThumb()) {
1181 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001182 CallOpc = ARMISD::CALL_NOLINK;
1183 else
1184 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1185 } else {
1186 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001187 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1188 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001189 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001190 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001191 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001192 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001193 InFlag = Chain.getValue(1);
1194 }
1195
Dan Gohman475871a2008-07-27 21:46:04 +00001196 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001197 Ops.push_back(Chain);
1198 Ops.push_back(Callee);
1199
1200 // Add argument registers to the end of the list so that they are known live
1201 // into the call.
1202 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1203 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1204 RegsToPass[i].second.getValueType()));
1205
Gabor Greifba36cb52008-08-28 21:40:38 +00001206 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001207 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +00001208 // Returns a chain and a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00001209 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +00001210 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001211 InFlag = Chain.getValue(1);
1212
Chris Lattnere563bbc2008-10-11 22:08:30 +00001213 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1214 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001215 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001216 InFlag = Chain.getValue(1);
1217
Bob Wilson1f595bb2009-04-17 19:07:39 +00001218 // Handle result values, copying them out of physregs into vregs that we
1219 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001220 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1221 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001222}
1223
Dan Gohman98ca4f22009-08-05 01:29:28 +00001224SDValue
1225ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001226 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001227 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmand858e902010-04-17 15:26:15 +00001228 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001229
Bob Wilsondee46d72009-04-17 20:35:10 +00001230 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001231 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001232
Bob Wilsondee46d72009-04-17 20:35:10 +00001233 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001234 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1235 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001236
Dan Gohman98ca4f22009-08-05 01:29:28 +00001237 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001238 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1239 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001240
1241 // If this is the first return lowered for this function, add
1242 // the regs to the liveout set for the function.
1243 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1244 for (unsigned i = 0; i != RVLocs.size(); ++i)
1245 if (RVLocs[i].isRegLoc())
1246 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001247 }
1248
Bob Wilson1f595bb2009-04-17 19:07:39 +00001249 SDValue Flag;
1250
1251 // Copy the result values into the output registers.
1252 for (unsigned i = 0, realRVLocIdx = 0;
1253 i != RVLocs.size();
1254 ++i, ++realRVLocIdx) {
1255 CCValAssign &VA = RVLocs[i];
1256 assert(VA.isRegLoc() && "Can only return in registers!");
1257
Dan Gohman98ca4f22009-08-05 01:29:28 +00001258 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001259
1260 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001261 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001262 case CCValAssign::Full: break;
1263 case CCValAssign::BCvt:
1264 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1265 break;
1266 }
1267
Bob Wilson1f595bb2009-04-17 19:07:39 +00001268 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001269 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001270 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001271 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1272 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001273 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001274 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001275
1276 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1277 Flag = Chain.getValue(1);
1278 VA = RVLocs[++i]; // skip ahead to next loc
1279 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1280 HalfGPRs.getValue(1), Flag);
1281 Flag = Chain.getValue(1);
1282 VA = RVLocs[++i]; // skip ahead to next loc
1283
1284 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001285 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1286 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001287 }
1288 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1289 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001290 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001291 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001292 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001293 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001294 VA = RVLocs[++i]; // skip ahead to next loc
1295 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1296 Flag);
1297 } else
1298 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1299
Bob Wilsondee46d72009-04-17 20:35:10 +00001300 // Guarantee that all emitted copies are
1301 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001302 Flag = Chain.getValue(1);
1303 }
1304
1305 SDValue result;
1306 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001307 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001308 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001309 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001310
1311 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001312}
1313
Bob Wilsonb62d2572009-11-03 00:02:05 +00001314// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1315// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1316// one of the above mentioned nodes. It has to be wrapped because otherwise
1317// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1318// be used to form addressing mode. These wrapped nodes will be selected
1319// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001320static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001321 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001322 // FIXME there is no actual debug info here
1323 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001324 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001325 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001326 if (CP->isMachineConstantPoolEntry())
1327 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1328 CP->getAlignment());
1329 else
1330 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1331 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001332 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001333}
1334
Dan Gohmand858e902010-04-17 15:26:15 +00001335SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1336 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001337 MachineFunction &MF = DAG.getMachineFunction();
1338 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1339 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001340 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001341 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001342 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001343 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1344 SDValue CPAddr;
1345 if (RelocM == Reloc::Static) {
1346 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1347 } else {
1348 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001349 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001350 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1351 ARMCP::CPBlockAddress,
1352 PCAdj);
1353 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1354 }
1355 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1356 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001357 PseudoSourceValue::getConstantPool(), 0,
1358 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001359 if (RelocM == Reloc::Static)
1360 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001361 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001362 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001363}
1364
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001365// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001366SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001367ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001368 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001369 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001370 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001371 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001372 MachineFunction &MF = DAG.getMachineFunction();
1373 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1374 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001375 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001376 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001377 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001378 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001379 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001380 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
David Greene1b58cab2010-02-15 16:55:24 +00001381 PseudoSourceValue::getConstantPool(), 0,
1382 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001383 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001384
Evan Chenge7e0d622009-11-06 22:24:13 +00001385 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001386 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001387
1388 // call __tls_get_addr.
1389 ArgListTy Args;
1390 ArgListEntry Entry;
1391 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001392 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001393 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001394 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001395 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001396 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1397 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001398 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001399 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001400 return CallResult.first;
1401}
1402
1403// Lower ISD::GlobalTLSAddress using the "initial exec" or
1404// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001405SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001406ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001407 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001408 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001409 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001410 SDValue Offset;
1411 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001412 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001413 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001414 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001415
Chris Lattner4fb63d02009-07-15 04:12:33 +00001416 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001417 MachineFunction &MF = DAG.getMachineFunction();
1418 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1419 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1420 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001421 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1422 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001423 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001424 ARMCP::CPValue, PCAdj, "gottpoff", true);
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 Chain = Offset.getValue(1);
1431
Evan Chenge7e0d622009-11-06 22:24:13 +00001432 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001433 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001434
Evan Cheng9eda6892009-10-31 03:39:36 +00001435 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001436 PseudoSourceValue::getConstantPool(), 0,
1437 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001438 } else {
1439 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001440 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001441 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001442 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001443 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001444 PseudoSourceValue::getConstantPool(), 0,
1445 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001446 }
1447
1448 // The address of the thread local variable is the add of the thread
1449 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001450 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001451}
1452
Dan Gohman475871a2008-07-27 21:46:04 +00001453SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001454ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001455 // TODO: implement the "local dynamic" model
1456 assert(Subtarget->isTargetELF() &&
1457 "TLS not implemented for non-ELF targets");
1458 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1459 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1460 // otherwise use the "Local Exec" TLS Model
1461 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1462 return LowerToTLSGeneralDynamicModel(GA, DAG);
1463 else
1464 return LowerToTLSExecModels(GA, DAG);
1465}
1466
Dan Gohman475871a2008-07-27 21:46:04 +00001467SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001468 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001469 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001470 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001471 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001472 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1473 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001474 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001475 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001476 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001477 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001478 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001479 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001480 CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001481 PseudoSourceValue::getConstantPool(), 0,
1482 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001483 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001484 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001485 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001486 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001487 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001488 PseudoSourceValue::getGOT(), 0,
1489 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001490 return Result;
1491 } else {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001492 // If we have T2 ops, we can materialize the address directly via movt/movw
1493 // pair. This is always cheaper.
1494 if (Subtarget->useMovt()) {
1495 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
1496 DAG.getTargetGlobalAddress(GV, PtrVT));
1497 } else {
1498 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1499 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1500 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001501 PseudoSourceValue::getConstantPool(), 0,
1502 false, false, 0);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001503 }
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001504 }
1505}
1506
Dan Gohman475871a2008-07-27 21:46:04 +00001507SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001508 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001509 MachineFunction &MF = DAG.getMachineFunction();
1510 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1511 unsigned ARMPCLabelIndex = 0;
Owen Andersone50ed302009-08-10 22:56:29 +00001512 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001513 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001514 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001515 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001516 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001517 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001518 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001519 else {
Evan Chenge7e0d622009-11-06 22:24:13 +00001520 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001521 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1522 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001523 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001524 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001525 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001526 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001527
Evan Cheng9eda6892009-10-31 03:39:36 +00001528 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001529 PseudoSourceValue::getConstantPool(), 0,
1530 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001531 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001532
1533 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001534 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001535 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001536 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001537
Evan Cheng63476a82009-09-03 07:04:02 +00001538 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Evan Cheng9eda6892009-10-31 03:39:36 +00001539 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001540 PseudoSourceValue::getGOT(), 0,
1541 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001542
1543 return Result;
1544}
1545
Dan Gohman475871a2008-07-27 21:46:04 +00001546SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001547 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001548 assert(Subtarget->isTargetELF() &&
1549 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00001550 MachineFunction &MF = DAG.getMachineFunction();
1551 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1552 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Andersone50ed302009-08-10 22:56:29 +00001553 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001554 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001555 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001556 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1557 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001558 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001559 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001560 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001561 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001562 PseudoSourceValue::getConstantPool(), 0,
1563 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001564 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001565 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001566}
1567
Jim Grosbach0e0da732009-05-12 23:59:14 +00001568SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00001569ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
1570 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00001571 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00001572 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
1573 Op.getOperand(1), Val);
1574}
1575
1576SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00001577ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
1578 DebugLoc dl = Op.getDebugLoc();
1579 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
1580 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
1581}
1582
1583SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00001584ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001585 const ARMSubtarget *Subtarget)
1586 const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001587 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001588 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001589 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001590 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001591 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001592 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001593 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1594 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001595 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001596 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00001597 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1598 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001599 EVT PtrVT = getPointerTy();
1600 DebugLoc dl = Op.getDebugLoc();
1601 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1602 SDValue CPAddr;
1603 unsigned PCAdj = (RelocM != Reloc::PIC_)
1604 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001605 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001606 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1607 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001608 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001609 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001610 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00001611 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001612 PseudoSourceValue::getConstantPool(), 0,
1613 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001614 SDValue Chain = Result.getValue(1);
1615
1616 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001617 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001618 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1619 }
1620 return Result;
1621 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001622 }
1623}
1624
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001625static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
1626 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00001627 DebugLoc dl = Op.getDebugLoc();
1628 SDValue Op5 = Op.getOperand(5);
1629 SDValue Res;
1630 unsigned isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue();
1631 if (isDeviceBarrier) {
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001632 if (Subtarget->hasV7Ops())
1633 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0));
1634 else
1635 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0),
1636 DAG.getConstant(0, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00001637 } else {
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001638 if (Subtarget->hasV7Ops())
1639 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
1640 else
1641 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
1642 DAG.getConstant(0, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00001643 }
1644 return Res;
1645}
1646
Dan Gohman1e93df62010-04-17 14:41:14 +00001647static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
1648 MachineFunction &MF = DAG.getMachineFunction();
1649 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
1650
Evan Chenga8e29892007-01-19 07:51:42 +00001651 // vastart just stores the address of the VarArgsFrameIndex slot into the
1652 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001653 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001654 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00001655 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001656 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
David Greene1b58cab2010-02-15 16:55:24 +00001657 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
1658 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001659}
1660
Dan Gohman475871a2008-07-27 21:46:04 +00001661SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001662ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1663 SelectionDAG &DAG) const {
Evan Cheng86198642009-08-07 00:34:42 +00001664 SDNode *Node = Op.getNode();
1665 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001666 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001667 SDValue Chain = Op.getOperand(0);
1668 SDValue Size = Op.getOperand(1);
1669 SDValue Align = Op.getOperand(2);
1670
1671 // Chain the dynamic stack allocation so that it doesn't modify the stack
1672 // pointer when other instructions are using the stack.
1673 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1674
1675 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1676 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1677 if (AlignVal > StackAlign)
1678 // Do this now since selection pass cannot introduce new target
1679 // independent node.
1680 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1681
1682 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1683 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1684 // do even more horrible hack later.
1685 MachineFunction &MF = DAG.getMachineFunction();
1686 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1687 if (AFI->isThumb1OnlyFunction()) {
1688 bool Negate = true;
1689 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1690 if (C) {
1691 uint32_t Val = C->getZExtValue();
1692 if (Val <= 508 && ((Val & 3) == 0))
1693 Negate = false;
1694 }
1695 if (Negate)
1696 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1697 }
1698
Owen Anderson825b72b2009-08-11 20:47:22 +00001699 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001700 SDValue Ops1[] = { Chain, Size, Align };
1701 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1702 Chain = Res.getValue(1);
1703 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1704 DAG.getIntPtrConstant(0, true), SDValue());
1705 SDValue Ops2[] = { Res, Chain };
1706 return DAG.getMergeValues(Ops2, 2, dl);
1707}
1708
1709SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001710ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1711 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001712 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001713 MachineFunction &MF = DAG.getMachineFunction();
1714 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1715
1716 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001717 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001718 RC = ARM::tGPRRegisterClass;
1719 else
1720 RC = ARM::GPRRegisterClass;
1721
1722 // Transform the arguments stored in physical registers into virtual ones.
Evan Cheng2457f2c2010-05-22 01:47:14 +00001723 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001724 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001725
1726 SDValue ArgValue2;
1727 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001728 MachineFrameInfo *MFI = MF.getFrameInfo();
Bob Wilson6a234f02010-04-13 22:03:22 +00001729 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true, false);
Bob Wilson5bafff32009-06-22 23:27:02 +00001730
1731 // Create load node to retrieve arguments from the stack.
1732 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001733 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00001734 PseudoSourceValue::getFixedStack(FI), 0,
1735 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001736 } else {
1737 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001738 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001739 }
1740
Jim Grosbache5165492009-11-09 00:11:35 +00001741 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001742}
1743
1744SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001745ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001746 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001747 const SmallVectorImpl<ISD::InputArg>
1748 &Ins,
1749 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001750 SmallVectorImpl<SDValue> &InVals)
1751 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001752
Bob Wilson1f595bb2009-04-17 19:07:39 +00001753 MachineFunction &MF = DAG.getMachineFunction();
1754 MachineFrameInfo *MFI = MF.getFrameInfo();
1755
Bob Wilson1f595bb2009-04-17 19:07:39 +00001756 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1757
1758 // Assign locations to all of the incoming arguments.
1759 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001760 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1761 *DAG.getContext());
1762 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001763 CCAssignFnForNode(CallConv, /* Return*/ false,
1764 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001765
1766 SmallVector<SDValue, 16> ArgValues;
1767
1768 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1769 CCValAssign &VA = ArgLocs[i];
1770
Bob Wilsondee46d72009-04-17 20:35:10 +00001771 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001772 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001773 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00001774
Bob Wilson5bafff32009-06-22 23:27:02 +00001775 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001776 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001777 // f64 and vector types are split up into multiple registers or
1778 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00001779 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001780 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001781 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00001782 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00001783 SDValue ArgValue2;
1784 if (VA.isMemLoc()) {
1785 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(),
1786 true, false);
1787 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1788 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
1789 PseudoSourceValue::getFixedStack(FI), 0,
1790 false, false, 0);
1791 } else {
1792 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
1793 Chain, DAG, dl);
1794 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001795 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1796 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001797 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00001798 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001799 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1800 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001801 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001802
Bob Wilson5bafff32009-06-22 23:27:02 +00001803 } else {
1804 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001805
Owen Anderson825b72b2009-08-11 20:47:22 +00001806 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00001807 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001808 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00001809 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001810 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001811 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001812 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001813 RC = (AFI->isThumb1OnlyFunction() ?
1814 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00001815 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001816 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00001817
1818 // Transform the arguments in physical registers into virtual ones.
1819 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001820 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001821 }
1822
1823 // If this is an 8 or 16-bit value, it is really passed promoted
1824 // to 32 bits. Insert an assert[sz]ext to capture this, then
1825 // truncate to the right size.
1826 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001827 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001828 case CCValAssign::Full: break;
1829 case CCValAssign::BCvt:
1830 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1831 break;
1832 case CCValAssign::SExt:
1833 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1834 DAG.getValueType(VA.getValVT()));
1835 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1836 break;
1837 case CCValAssign::ZExt:
1838 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1839 DAG.getValueType(VA.getValVT()));
1840 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1841 break;
1842 }
1843
Dan Gohman98ca4f22009-08-05 01:29:28 +00001844 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001845
1846 } else { // VA.isRegLoc()
1847
1848 // sanity check
1849 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00001850 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001851
1852 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001853 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
1854 true, false);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001855
Bob Wilsondee46d72009-04-17 20:35:10 +00001856 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001857 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001858 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00001859 PseudoSourceValue::getFixedStack(FI), 0,
1860 false, false, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001861 }
1862 }
1863
1864 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001865 if (isVarArg) {
1866 static const unsigned GPRArgRegs[] = {
1867 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1868 };
1869
Bob Wilsondee46d72009-04-17 20:35:10 +00001870 unsigned NumGPRs = CCInfo.getFirstUnallocated
1871 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001872
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001873 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1874 unsigned VARegSize = (4 - NumGPRs) * 4;
1875 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00001876 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001877 if (VARegSaveSize) {
1878 // If this function is vararg, store any remaining integer argument regs
1879 // to their spots on the stack so that they may be loaded by deferencing
1880 // the result of va_next.
1881 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00001882 AFI->setVarArgsFrameIndex(
1883 MFI->CreateFixedObject(VARegSaveSize,
1884 ArgOffset + VARegSaveSize - VARegSize,
1885 true, false));
1886 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
1887 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001888
Dan Gohman475871a2008-07-27 21:46:04 +00001889 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001890 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001891 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001892 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001893 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001894 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001895 RC = ARM::GPRRegisterClass;
1896
Bob Wilson998e1252009-04-20 18:36:57 +00001897 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001898 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00001899 SDValue Store =
1900 DAG.getStore(Val.getValue(1), dl, Val, FIN,
1901 PseudoSourceValue::getFixedStack(AFI->getVarArgsFrameIndex()), 0,
1902 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001903 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001904 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001905 DAG.getConstant(4, getPointerTy()));
1906 }
1907 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001908 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001909 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001910 } else
1911 // This will point to the next argument passed via stack.
Dan Gohman1e93df62010-04-17 14:41:14 +00001912 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset,
1913 true, false));
Evan Chenga8e29892007-01-19 07:51:42 +00001914 }
1915
Dan Gohman98ca4f22009-08-05 01:29:28 +00001916 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00001917}
1918
1919/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001920static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001921 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001922 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001923 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001924 // Maybe this has already been legalized into the constant pool?
1925 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001926 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001927 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00001928 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001929 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001930 }
1931 }
1932 return false;
1933}
1934
Evan Chenga8e29892007-01-19 07:51:42 +00001935/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1936/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00001937SDValue
1938ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Dan Gohmand858e902010-04-17 15:26:15 +00001939 SDValue &ARMCC, SelectionDAG &DAG,
1940 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00001941 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001942 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00001943 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001944 // Constant does not fit, try adjusting it by one?
1945 switch (CC) {
1946 default: break;
1947 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001948 case ISD::SETGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00001949 if (isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001950 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
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::SETULT:
1955 case ISD::SETUGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00001956 if (C > 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001957 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
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 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001962 case ISD::SETGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00001963 if (isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001964 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001965 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001966 }
1967 break;
1968 case ISD::SETULE:
1969 case ISD::SETUGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00001970 if (C < 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001971 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001972 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001973 }
1974 break;
1975 }
1976 }
1977 }
1978
1979 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001980 ARMISD::NodeType CompareType;
1981 switch (CondCode) {
1982 default:
1983 CompareType = ARMISD::CMP;
1984 break;
1985 case ARMCC::EQ:
1986 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00001987 // Uses only Z Flag
1988 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001989 break;
1990 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001991 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1992 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001993}
1994
1995/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001996static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001997 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001998 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001999 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00002000 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002001 else
Owen Anderson825b72b2009-08-11 20:47:22 +00002002 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
2003 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002004}
2005
Dan Gohmand858e902010-04-17 15:26:15 +00002006SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002007 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002008 SDValue LHS = Op.getOperand(0);
2009 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002010 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002011 SDValue TrueVal = Op.getOperand(2);
2012 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002013 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002014
Owen Anderson825b72b2009-08-11 20:47:22 +00002015 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00002016 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002017 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00002018 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Dale Johannesende064702009-02-06 21:50:26 +00002019 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002020 }
2021
2022 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002023 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002024
Owen Anderson825b72b2009-08-11 20:47:22 +00002025 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
2026 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002027 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
2028 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00002029 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002030 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002031 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002032 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00002033 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002034 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00002035 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002036 }
2037 return Result;
2038}
2039
Dan Gohmand858e902010-04-17 15:26:15 +00002040SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002041 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002042 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002043 SDValue LHS = Op.getOperand(2);
2044 SDValue RHS = Op.getOperand(3);
2045 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002046 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002047
Owen Anderson825b72b2009-08-11 20:47:22 +00002048 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00002049 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002050 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00002051 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002052 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00002053 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002054 }
2055
Owen Anderson825b72b2009-08-11 20:47:22 +00002056 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00002057 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002058 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002059
Dale Johannesende064702009-02-06 21:50:26 +00002060 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002061 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
2062 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2063 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002064 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002065 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002066 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002067 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00002068 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002069 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002070 }
2071 return Res;
2072}
2073
Dan Gohmand858e902010-04-17 15:26:15 +00002074SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002075 SDValue Chain = Op.getOperand(0);
2076 SDValue Table = Op.getOperand(1);
2077 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002078 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002079
Owen Andersone50ed302009-08-10 22:56:29 +00002080 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002081 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2082 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002083 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002084 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002085 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002086 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2087 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002088 if (Subtarget->isThumb2()) {
2089 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2090 // which does another jump to the destination. This also makes it easier
2091 // to translate it to TBB / TBH later.
2092 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002093 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002094 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002095 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002096 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002097 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002098 PseudoSourceValue::getJumpTable(), 0,
2099 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002100 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002101 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002102 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002103 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002104 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002105 PseudoSourceValue::getJumpTable(), 0, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002106 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002107 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002108 }
Evan Chenga8e29892007-01-19 07:51:42 +00002109}
2110
Bob Wilson76a312b2010-03-19 22:51:32 +00002111static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2112 DebugLoc dl = Op.getDebugLoc();
2113 unsigned Opc;
2114
2115 switch (Op.getOpcode()) {
2116 default:
2117 assert(0 && "Invalid opcode!");
2118 case ISD::FP_TO_SINT:
2119 Opc = ARMISD::FTOSI;
2120 break;
2121 case ISD::FP_TO_UINT:
2122 Opc = ARMISD::FTOUI;
2123 break;
2124 }
2125 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
2126 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
2127}
2128
2129static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2130 EVT VT = Op.getValueType();
2131 DebugLoc dl = Op.getDebugLoc();
2132 unsigned Opc;
2133
2134 switch (Op.getOpcode()) {
2135 default:
2136 assert(0 && "Invalid opcode!");
2137 case ISD::SINT_TO_FP:
2138 Opc = ARMISD::SITOF;
2139 break;
2140 case ISD::UINT_TO_FP:
2141 Opc = ARMISD::UITOF;
2142 break;
2143 }
2144
2145 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
2146 return DAG.getNode(Opc, dl, VT, Op);
2147}
2148
Dan Gohman475871a2008-07-27 21:46:04 +00002149static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002150 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002151 SDValue Tmp0 = Op.getOperand(0);
2152 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002153 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002154 EVT VT = Op.getValueType();
2155 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00002156 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
2157 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002158 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
2159 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002160 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002161}
2162
Evan Cheng2457f2c2010-05-22 01:47:14 +00002163SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
2164 MachineFunction &MF = DAG.getMachineFunction();
2165 MachineFrameInfo *MFI = MF.getFrameInfo();
2166 MFI->setReturnAddressIsTaken(true);
2167
2168 EVT VT = Op.getValueType();
2169 DebugLoc dl = Op.getDebugLoc();
2170 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2171 if (Depth) {
2172 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
2173 SDValue Offset = DAG.getConstant(4, MVT::i32);
2174 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
2175 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
2176 NULL, 0, false, false, 0);
2177 }
2178
2179 // Return LR, which contains the return address. Mark it an implicit live-in.
Evan Chengc7cf10c2010-05-24 18:00:18 +00002180 unsigned Reg = MF.addLiveIn(ARM::LR, ARM::GPRRegisterClass);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002181 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
2182}
2183
Dan Gohmand858e902010-04-17 15:26:15 +00002184SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002185 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2186 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002187
Owen Andersone50ed302009-08-10 22:56:29 +00002188 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002189 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2190 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002191 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002192 ? ARM::R7 : ARM::R11;
2193 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2194 while (Depth--)
David Greene1b58cab2010-02-15 16:55:24 +00002195 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
2196 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002197 return FrameAddr;
2198}
2199
Bob Wilson9f3f0612010-04-17 05:30:19 +00002200/// ExpandBIT_CONVERT - If the target supports VFP, this function is called to
2201/// expand a bit convert where either the source or destination type is i64 to
2202/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2203/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2204/// vectors), since the legalizer won't know what to do with that.
Duncan Sands1607f052008-12-01 11:39:25 +00002205static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002206 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2207 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002208 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002209
Bob Wilson9f3f0612010-04-17 05:30:19 +00002210 // This function is only supposed to be called for i64 types, either as the
2211 // source or destination of the bit convert.
2212 EVT SrcVT = Op.getValueType();
2213 EVT DstVT = N->getValueType(0);
2214 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
2215 "ExpandBIT_CONVERT called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002216
Bob Wilson9f3f0612010-04-17 05:30:19 +00002217 // Turn i64->f64 into VMOVDRR.
2218 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002219 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2220 DAG.getConstant(0, MVT::i32));
2221 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2222 DAG.getConstant(1, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00002223 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002224 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002225
Jim Grosbache5165492009-11-09 00:11:35 +00002226 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002227 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2228 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2229 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2230 // Merge the pieces into a single i64 value.
2231 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2232 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002233
Bob Wilson9f3f0612010-04-17 05:30:19 +00002234 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002235}
2236
Bob Wilson5bafff32009-06-22 23:27:02 +00002237/// getZeroVector - Returns a vector of specified type with all zero elements.
2238///
Owen Andersone50ed302009-08-10 22:56:29 +00002239static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002240 assert(VT.isVector() && "Expected a vector type");
2241
2242 // Zero vectors are used to represent vector negation and in those cases
2243 // will be implemented with the NEON VNEG instruction. However, VNEG does
2244 // not support i64 elements, so sometimes the zero vectors will need to be
2245 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002246 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilson5bafff32009-06-22 23:27:02 +00002247 // to their dest type. This ensures they get CSE'd.
2248 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002249 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2250 SmallVector<SDValue, 8> Ops;
2251 MVT TVT;
2252
2253 if (VT.getSizeInBits() == 64) {
2254 Ops.assign(8, Cst); TVT = MVT::v8i8;
2255 } else {
2256 Ops.assign(16, Cst); TVT = MVT::v16i8;
2257 }
2258 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002259
2260 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2261}
2262
2263/// getOnesVector - Returns a vector of specified type with all bits set.
2264///
Owen Andersone50ed302009-08-10 22:56:29 +00002265static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002266 assert(VT.isVector() && "Expected a vector type");
2267
Bob Wilson929ffa22009-10-30 20:13:25 +00002268 // Always build ones vectors as <16 x i8> or <8 x i8> bitcasted to their
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002269 // dest type. This ensures they get CSE'd.
Bob Wilson5bafff32009-06-22 23:27:02 +00002270 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002271 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2272 SmallVector<SDValue, 8> Ops;
2273 MVT TVT;
2274
2275 if (VT.getSizeInBits() == 64) {
2276 Ops.assign(8, Cst); TVT = MVT::v8i8;
2277 } else {
2278 Ops.assign(16, Cst); TVT = MVT::v16i8;
2279 }
2280 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002281
2282 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2283}
2284
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002285/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2286/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002287SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2288 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002289 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2290 EVT VT = Op.getValueType();
2291 unsigned VTBits = VT.getSizeInBits();
2292 DebugLoc dl = Op.getDebugLoc();
2293 SDValue ShOpLo = Op.getOperand(0);
2294 SDValue ShOpHi = Op.getOperand(1);
2295 SDValue ShAmt = Op.getOperand(2);
2296 SDValue ARMCC;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002297 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002298
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002299 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2300
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002301 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2302 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2303 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2304 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2305 DAG.getConstant(VTBits, MVT::i32));
2306 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2307 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002308 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002309
2310 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2311 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002312 ARMCC, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002313 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002314 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC,
2315 CCR, Cmp);
2316
2317 SDValue Ops[2] = { Lo, Hi };
2318 return DAG.getMergeValues(Ops, 2, dl);
2319}
2320
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002321/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2322/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002323SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2324 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002325 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2326 EVT VT = Op.getValueType();
2327 unsigned VTBits = VT.getSizeInBits();
2328 DebugLoc dl = Op.getDebugLoc();
2329 SDValue ShOpLo = Op.getOperand(0);
2330 SDValue ShOpHi = Op.getOperand(1);
2331 SDValue ShAmt = Op.getOperand(2);
2332 SDValue ARMCC;
2333
2334 assert(Op.getOpcode() == ISD::SHL_PARTS);
2335 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2336 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2337 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2338 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2339 DAG.getConstant(VTBits, MVT::i32));
2340 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2341 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2342
2343 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2344 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2345 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002346 ARMCC, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002347 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
2348 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMCC,
2349 CCR, Cmp);
2350
2351 SDValue Ops[2] = { Lo, Hi };
2352 return DAG.getMergeValues(Ops, 2, dl);
2353}
2354
Jim Grosbach3482c802010-01-18 19:58:49 +00002355static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
2356 const ARMSubtarget *ST) {
2357 EVT VT = N->getValueType(0);
2358 DebugLoc dl = N->getDebugLoc();
2359
2360 if (!ST->hasV6T2Ops())
2361 return SDValue();
2362
2363 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
2364 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
2365}
2366
Bob Wilson5bafff32009-06-22 23:27:02 +00002367static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2368 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002369 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002370 DebugLoc dl = N->getDebugLoc();
2371
2372 // Lower vector shifts on NEON to use VSHL.
2373 if (VT.isVector()) {
2374 assert(ST->hasNEON() && "unexpected vector shift");
2375
2376 // Left shifts translate directly to the vshiftu intrinsic.
2377 if (N->getOpcode() == ISD::SHL)
2378 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002379 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002380 N->getOperand(0), N->getOperand(1));
2381
2382 assert((N->getOpcode() == ISD::SRA ||
2383 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2384
2385 // NEON uses the same intrinsics for both left and right shifts. For
2386 // right shifts, the shift amounts are negative, so negate the vector of
2387 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002388 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002389 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2390 getZeroVector(ShiftVT, DAG, dl),
2391 N->getOperand(1));
2392 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2393 Intrinsic::arm_neon_vshifts :
2394 Intrinsic::arm_neon_vshiftu);
2395 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002396 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002397 N->getOperand(0), NegatedCount);
2398 }
2399
Eli Friedmance392eb2009-08-22 03:13:10 +00002400 // We can get here for a node like i32 = ISD::SHL i32, i64
2401 if (VT != MVT::i64)
2402 return SDValue();
2403
2404 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002405 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002406
Chris Lattner27a6c732007-11-24 07:07:01 +00002407 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2408 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002409 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002410 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002411
Chris Lattner27a6c732007-11-24 07:07:01 +00002412 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002413 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002414
Chris Lattner27a6c732007-11-24 07:07:01 +00002415 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002416 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00002417 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00002418 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00002419 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002420
Chris Lattner27a6c732007-11-24 07:07:01 +00002421 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2422 // captures the result into a carry flag.
2423 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002424 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002425
Chris Lattner27a6c732007-11-24 07:07:01 +00002426 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002427 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002428
Chris Lattner27a6c732007-11-24 07:07:01 +00002429 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002430 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002431}
2432
Bob Wilson5bafff32009-06-22 23:27:02 +00002433static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2434 SDValue TmpOp0, TmpOp1;
2435 bool Invert = false;
2436 bool Swap = false;
2437 unsigned Opc = 0;
2438
2439 SDValue Op0 = Op.getOperand(0);
2440 SDValue Op1 = Op.getOperand(1);
2441 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002442 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002443 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2444 DebugLoc dl = Op.getDebugLoc();
2445
2446 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2447 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002448 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002449 case ISD::SETUNE:
2450 case ISD::SETNE: Invert = true; // Fallthrough
2451 case ISD::SETOEQ:
2452 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2453 case ISD::SETOLT:
2454 case ISD::SETLT: Swap = true; // Fallthrough
2455 case ISD::SETOGT:
2456 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2457 case ISD::SETOLE:
2458 case ISD::SETLE: Swap = true; // Fallthrough
2459 case ISD::SETOGE:
2460 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2461 case ISD::SETUGE: Swap = true; // Fallthrough
2462 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2463 case ISD::SETUGT: Swap = true; // Fallthrough
2464 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2465 case ISD::SETUEQ: Invert = true; // Fallthrough
2466 case ISD::SETONE:
2467 // Expand this to (OLT | OGT).
2468 TmpOp0 = Op0;
2469 TmpOp1 = Op1;
2470 Opc = ISD::OR;
2471 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2472 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2473 break;
2474 case ISD::SETUO: Invert = true; // Fallthrough
2475 case ISD::SETO:
2476 // Expand this to (OLT | OGE).
2477 TmpOp0 = Op0;
2478 TmpOp1 = Op1;
2479 Opc = ISD::OR;
2480 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2481 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2482 break;
2483 }
2484 } else {
2485 // Integer comparisons.
2486 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002487 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002488 case ISD::SETNE: Invert = true;
2489 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2490 case ISD::SETLT: Swap = true;
2491 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2492 case ISD::SETLE: Swap = true;
2493 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2494 case ISD::SETULT: Swap = true;
2495 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2496 case ISD::SETULE: Swap = true;
2497 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2498 }
2499
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002500 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002501 if (Opc == ARMISD::VCEQ) {
2502
2503 SDValue AndOp;
2504 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2505 AndOp = Op0;
2506 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2507 AndOp = Op1;
2508
2509 // Ignore bitconvert.
2510 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2511 AndOp = AndOp.getOperand(0);
2512
2513 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2514 Opc = ARMISD::VTST;
2515 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2516 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2517 Invert = !Invert;
2518 }
2519 }
2520 }
2521
2522 if (Swap)
2523 std::swap(Op0, Op1);
2524
2525 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2526
2527 if (Invert)
2528 Result = DAG.getNOT(dl, Result, VT);
2529
2530 return Result;
2531}
2532
2533/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2534/// VMOV instruction, and if so, return the constant being splatted.
2535static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2536 unsigned SplatBitSize, SelectionDAG &DAG) {
2537 switch (SplatBitSize) {
2538 case 8:
2539 // Any 1-byte value is OK.
2540 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson825b72b2009-08-11 20:47:22 +00002541 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilson5bafff32009-06-22 23:27:02 +00002542
2543 case 16:
2544 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2545 if ((SplatBits & ~0xff) == 0 ||
2546 (SplatBits & ~0xff00) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002547 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilson5bafff32009-06-22 23:27:02 +00002548 break;
2549
2550 case 32:
2551 // NEON's 32-bit VMOV supports splat values where:
2552 // * only one byte is nonzero, or
2553 // * the least significant byte is 0xff and the second byte is nonzero, or
2554 // * the least significant 2 bytes are 0xff and the third is nonzero.
2555 if ((SplatBits & ~0xff) == 0 ||
2556 (SplatBits & ~0xff00) == 0 ||
2557 (SplatBits & ~0xff0000) == 0 ||
2558 (SplatBits & ~0xff000000) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002559 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002560
2561 if ((SplatBits & ~0xffff) == 0 &&
2562 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002563 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002564
2565 if ((SplatBits & ~0xffffff) == 0 &&
2566 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002567 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002568
2569 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2570 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2571 // VMOV.I32. A (very) minor optimization would be to replicate the value
2572 // and fall through here to test for a valid 64-bit splat. But, then the
2573 // caller would also need to check and handle the change in size.
2574 break;
2575
2576 case 64: {
2577 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2578 uint64_t BitMask = 0xff;
2579 uint64_t Val = 0;
2580 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2581 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2582 Val |= BitMask;
2583 else if ((SplatBits & BitMask) != 0)
2584 return SDValue();
2585 BitMask <<= 8;
2586 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002587 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilson5bafff32009-06-22 23:27:02 +00002588 }
2589
2590 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002591 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilson5bafff32009-06-22 23:27:02 +00002592 break;
2593 }
2594
2595 return SDValue();
2596}
2597
2598/// getVMOVImm - If this is a build_vector of constants which can be
2599/// formed by using a VMOV instruction of the specified element size,
2600/// return the constant being splatted. The ByteSize field indicates the
2601/// number of bytes of each element [1248].
2602SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2603 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2604 APInt SplatBits, SplatUndef;
2605 unsigned SplatBitSize;
2606 bool HasAnyUndefs;
2607 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2608 HasAnyUndefs, ByteSize * 8))
2609 return SDValue();
2610
2611 if (SplatBitSize > ByteSize * 8)
2612 return SDValue();
2613
2614 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2615 SplatBitSize, DAG);
2616}
2617
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002618static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2619 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002620 unsigned NumElts = VT.getVectorNumElements();
2621 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002622 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002623
2624 // If this is a VEXT shuffle, the immediate value is the index of the first
2625 // element. The other shuffle indices must be the successive elements after
2626 // the first one.
2627 unsigned ExpectedElt = Imm;
2628 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002629 // Increment the expected index. If it wraps around, it may still be
2630 // a VEXT but the source vectors must be swapped.
2631 ExpectedElt += 1;
2632 if (ExpectedElt == NumElts * 2) {
2633 ExpectedElt = 0;
2634 ReverseVEXT = true;
2635 }
2636
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002637 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002638 return false;
2639 }
2640
2641 // Adjust the index value if the source operands will be swapped.
2642 if (ReverseVEXT)
2643 Imm -= NumElts;
2644
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002645 return true;
2646}
2647
Bob Wilson8bb9e482009-07-26 00:39:34 +00002648/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2649/// instruction with the specified blocksize. (The order of the elements
2650/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002651static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2652 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002653 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2654 "Only possible block sizes for VREV are: 16, 32, 64");
2655
Bob Wilson8bb9e482009-07-26 00:39:34 +00002656 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00002657 if (EltSz == 64)
2658 return false;
2659
2660 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002661 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002662
2663 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2664 return false;
2665
2666 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002667 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002668 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2669 return false;
2670 }
2671
2672 return true;
2673}
2674
Bob Wilsonc692cb72009-08-21 20:54:19 +00002675static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2676 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002677 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2678 if (EltSz == 64)
2679 return false;
2680
Bob Wilsonc692cb72009-08-21 20:54:19 +00002681 unsigned NumElts = VT.getVectorNumElements();
2682 WhichResult = (M[0] == 0 ? 0 : 1);
2683 for (unsigned i = 0; i < NumElts; i += 2) {
2684 if ((unsigned) M[i] != i + WhichResult ||
2685 (unsigned) M[i+1] != i + NumElts + WhichResult)
2686 return false;
2687 }
2688 return true;
2689}
2690
Bob Wilson324f4f12009-12-03 06:40:55 +00002691/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
2692/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2693/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
2694static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2695 unsigned &WhichResult) {
2696 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2697 if (EltSz == 64)
2698 return false;
2699
2700 unsigned NumElts = VT.getVectorNumElements();
2701 WhichResult = (M[0] == 0 ? 0 : 1);
2702 for (unsigned i = 0; i < NumElts; i += 2) {
2703 if ((unsigned) M[i] != i + WhichResult ||
2704 (unsigned) M[i+1] != i + WhichResult)
2705 return false;
2706 }
2707 return true;
2708}
2709
Bob Wilsonc692cb72009-08-21 20:54:19 +00002710static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2711 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002712 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2713 if (EltSz == 64)
2714 return false;
2715
Bob Wilsonc692cb72009-08-21 20:54:19 +00002716 unsigned NumElts = VT.getVectorNumElements();
2717 WhichResult = (M[0] == 0 ? 0 : 1);
2718 for (unsigned i = 0; i != NumElts; ++i) {
2719 if ((unsigned) M[i] != 2 * i + WhichResult)
2720 return false;
2721 }
2722
2723 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002724 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002725 return false;
2726
2727 return true;
2728}
2729
Bob Wilson324f4f12009-12-03 06:40:55 +00002730/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
2731/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2732/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
2733static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2734 unsigned &WhichResult) {
2735 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2736 if (EltSz == 64)
2737 return false;
2738
2739 unsigned Half = VT.getVectorNumElements() / 2;
2740 WhichResult = (M[0] == 0 ? 0 : 1);
2741 for (unsigned j = 0; j != 2; ++j) {
2742 unsigned Idx = WhichResult;
2743 for (unsigned i = 0; i != Half; ++i) {
2744 if ((unsigned) M[i + j * Half] != Idx)
2745 return false;
2746 Idx += 2;
2747 }
2748 }
2749
2750 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2751 if (VT.is64BitVector() && EltSz == 32)
2752 return false;
2753
2754 return true;
2755}
2756
Bob Wilsonc692cb72009-08-21 20:54:19 +00002757static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2758 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002759 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2760 if (EltSz == 64)
2761 return false;
2762
Bob Wilsonc692cb72009-08-21 20:54:19 +00002763 unsigned NumElts = VT.getVectorNumElements();
2764 WhichResult = (M[0] == 0 ? 0 : 1);
2765 unsigned Idx = WhichResult * NumElts / 2;
2766 for (unsigned i = 0; i != NumElts; i += 2) {
2767 if ((unsigned) M[i] != Idx ||
2768 (unsigned) M[i+1] != Idx + NumElts)
2769 return false;
2770 Idx += 1;
2771 }
2772
2773 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002774 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002775 return false;
2776
2777 return true;
2778}
2779
Bob Wilson324f4f12009-12-03 06:40:55 +00002780/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
2781/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2782/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
2783static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2784 unsigned &WhichResult) {
2785 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2786 if (EltSz == 64)
2787 return false;
2788
2789 unsigned NumElts = VT.getVectorNumElements();
2790 WhichResult = (M[0] == 0 ? 0 : 1);
2791 unsigned Idx = WhichResult * NumElts / 2;
2792 for (unsigned i = 0; i != NumElts; i += 2) {
2793 if ((unsigned) M[i] != Idx ||
2794 (unsigned) M[i+1] != Idx)
2795 return false;
2796 Idx += 1;
2797 }
2798
2799 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2800 if (VT.is64BitVector() && EltSz == 32)
2801 return false;
2802
2803 return true;
2804}
2805
2806
Owen Andersone50ed302009-08-10 22:56:29 +00002807static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002808 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00002809 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002810 if (ConstVal->isNullValue())
2811 return getZeroVector(VT, DAG, dl);
2812 if (ConstVal->isAllOnesValue())
2813 return getOnesVector(VT, DAG, dl);
2814
Owen Andersone50ed302009-08-10 22:56:29 +00002815 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00002816 if (VT.is64BitVector()) {
2817 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002818 case 8: CanonicalVT = MVT::v8i8; break;
2819 case 16: CanonicalVT = MVT::v4i16; break;
2820 case 32: CanonicalVT = MVT::v2i32; break;
2821 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002822 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002823 }
2824 } else {
2825 assert(VT.is128BitVector() && "unknown splat vector size");
2826 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002827 case 8: CanonicalVT = MVT::v16i8; break;
2828 case 16: CanonicalVT = MVT::v8i16; break;
2829 case 32: CanonicalVT = MVT::v4i32; break;
2830 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002831 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002832 }
2833 }
2834
2835 // Build a canonical splat for this value.
2836 SmallVector<SDValue, 8> Ops;
2837 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2838 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2839 Ops.size());
2840 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2841}
2842
2843// If this is a case we can't handle, return null and let the default
2844// expansion code take care of it.
2845static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00002846 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002847 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002848 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002849
2850 APInt SplatBits, SplatUndef;
2851 unsigned SplatBitSize;
2852 bool HasAnyUndefs;
2853 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00002854 if (SplatBitSize <= 64) {
2855 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2856 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2857 if (Val.getNode())
2858 return BuildSplat(Val, VT, DAG, dl);
2859 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00002860 }
2861
Bob Wilsonbe751cf2010-05-22 00:23:12 +00002862 // Scan through the operands to see if only one value is used.
2863 unsigned NumElts = VT.getVectorNumElements();
2864 bool isOnlyLowElement = true;
2865 bool usesOnlyOneValue = true;
2866 bool isConstant = true;
2867 SDValue Value;
2868 for (unsigned i = 0; i < NumElts; ++i) {
2869 SDValue V = Op.getOperand(i);
2870 if (V.getOpcode() == ISD::UNDEF)
2871 continue;
2872 if (i > 0)
2873 isOnlyLowElement = false;
2874 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
2875 isConstant = false;
2876
2877 if (!Value.getNode())
2878 Value = V;
2879 else if (V != Value)
2880 usesOnlyOneValue = false;
2881 }
2882
2883 if (!Value.getNode())
2884 return DAG.getUNDEF(VT);
2885
2886 if (isOnlyLowElement)
2887 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
2888
2889 // If all elements are constants, fall back to the default expansion, which
2890 // will generate a load from the constant pool.
2891 if (isConstant)
2892 return SDValue();
2893
2894 // Use VDUP for non-constant splats.
Bob Wilson069e4342010-05-23 05:42:31 +00002895 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
2896 if (usesOnlyOneValue && EltSize <= 32)
Bob Wilsonbe751cf2010-05-22 00:23:12 +00002897 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
2898
2899 // Vectors with 32- or 64-bit elements can be built by directly assigning
2900 // the subregisters.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00002901 if (EltSize >= 32) {
2902 // Do the expansion with floating-point types, since that is what the VFP
2903 // registers are defined to use, and since i64 is not legal.
2904 EVT EltVT = EVT::getFloatingPointVT(EltSize);
2905 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
2906 SDValue Val = DAG.getUNDEF(VecVT);
2907 for (unsigned i = 0; i < NumElts; ++i) {
2908 SDValue Elt = Op.getOperand(i);
2909 if (Elt.getOpcode() == ISD::UNDEF)
2910 continue;
2911 Elt = DAG.getNode(ISD::BIT_CONVERT, dl, EltVT, Elt);
2912 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Val, Elt,
2913 DAG.getConstant(i, MVT::i32));
2914 }
2915 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00002916 }
2917
2918 return SDValue();
2919}
2920
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002921/// isShuffleMaskLegal - Targets can use this to indicate that they only
2922/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2923/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2924/// are assumed to be legal.
2925bool
2926ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2927 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002928 if (VT.getVectorNumElements() == 4 &&
2929 (VT.is128BitVector() || VT.is64BitVector())) {
2930 unsigned PFIndexes[4];
2931 for (unsigned i = 0; i != 4; ++i) {
2932 if (M[i] < 0)
2933 PFIndexes[i] = 8;
2934 else
2935 PFIndexes[i] = M[i];
2936 }
2937
2938 // Compute the index in the perfect shuffle table.
2939 unsigned PFTableIndex =
2940 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2941 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2942 unsigned Cost = (PFEntry >> 30);
2943
2944 if (Cost <= 4)
2945 return true;
2946 }
2947
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002948 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00002949 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002950
2951 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2952 isVREVMask(M, VT, 64) ||
2953 isVREVMask(M, VT, 32) ||
2954 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00002955 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2956 isVTRNMask(M, VT, WhichResult) ||
2957 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00002958 isVZIPMask(M, VT, WhichResult) ||
2959 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
2960 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
2961 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002962}
2963
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002964/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2965/// the specified operations to build the shuffle.
2966static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2967 SDValue RHS, SelectionDAG &DAG,
2968 DebugLoc dl) {
2969 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2970 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2971 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2972
2973 enum {
2974 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2975 OP_VREV,
2976 OP_VDUP0,
2977 OP_VDUP1,
2978 OP_VDUP2,
2979 OP_VDUP3,
2980 OP_VEXT1,
2981 OP_VEXT2,
2982 OP_VEXT3,
2983 OP_VUZPL, // VUZP, left result
2984 OP_VUZPR, // VUZP, right result
2985 OP_VZIPL, // VZIP, left result
2986 OP_VZIPR, // VZIP, right result
2987 OP_VTRNL, // VTRN, left result
2988 OP_VTRNR // VTRN, right result
2989 };
2990
2991 if (OpNum == OP_COPY) {
2992 if (LHSID == (1*9+2)*9+3) return LHS;
2993 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2994 return RHS;
2995 }
2996
2997 SDValue OpLHS, OpRHS;
2998 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2999 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
3000 EVT VT = OpLHS.getValueType();
3001
3002 switch (OpNum) {
3003 default: llvm_unreachable("Unknown shuffle opcode!");
3004 case OP_VREV:
3005 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
3006 case OP_VDUP0:
3007 case OP_VDUP1:
3008 case OP_VDUP2:
3009 case OP_VDUP3:
3010 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003011 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003012 case OP_VEXT1:
3013 case OP_VEXT2:
3014 case OP_VEXT3:
3015 return DAG.getNode(ARMISD::VEXT, dl, VT,
3016 OpLHS, OpRHS,
3017 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
3018 case OP_VUZPL:
3019 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003020 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003021 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
3022 case OP_VZIPL:
3023 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003024 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003025 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
3026 case OP_VTRNL:
3027 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003028 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3029 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003030 }
3031}
3032
Bob Wilson5bafff32009-06-22 23:27:02 +00003033static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003034 SDValue V1 = Op.getOperand(0);
3035 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00003036 DebugLoc dl = Op.getDebugLoc();
3037 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003038 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003039 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00003040
Bob Wilson28865062009-08-13 02:13:04 +00003041 // Convert shuffles that are directly supported on NEON to target-specific
3042 // DAG nodes, instead of keeping them as shuffles and matching them again
3043 // during code selection. This is more efficient and avoids the possibility
3044 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00003045 // FIXME: floating-point vectors should be canonicalized to integer vectors
3046 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003047 SVN->getMask(ShuffleMask);
3048
3049 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson0ce37102009-08-14 05:08:32 +00003050 int Lane = SVN->getSplatIndex();
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00003051 // If this is undef splat, generate it via "just" vdup, if possible.
3052 if (Lane == -1) Lane = 0;
3053
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003054 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3055 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00003056 }
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003057 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003058 DAG.getConstant(Lane, MVT::i32));
Bob Wilson0ce37102009-08-14 05:08:32 +00003059 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003060
3061 bool ReverseVEXT;
3062 unsigned Imm;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003063 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003064 if (ReverseVEXT)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003065 std::swap(V1, V2);
3066 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003067 DAG.getConstant(Imm, MVT::i32));
3068 }
3069
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003070 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003071 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003072 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003073 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003074 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003075 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
3076
Bob Wilsonc692cb72009-08-21 20:54:19 +00003077 // Check for Neon shuffles that modify both input vectors in place.
3078 // If both results are used, i.e., if there are two shuffles with the same
3079 // source operands and with masks corresponding to both results of one of
3080 // these operations, DAG memoization will ensure that a single node is
3081 // used for both shuffles.
3082 unsigned WhichResult;
3083 if (isVTRNMask(ShuffleMask, VT, WhichResult))
3084 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3085 V1, V2).getValue(WhichResult);
3086 if (isVUZPMask(ShuffleMask, VT, WhichResult))
3087 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3088 V1, V2).getValue(WhichResult);
3089 if (isVZIPMask(ShuffleMask, VT, WhichResult))
3090 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3091 V1, V2).getValue(WhichResult);
3092
Bob Wilson324f4f12009-12-03 06:40:55 +00003093 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
3094 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3095 V1, V1).getValue(WhichResult);
3096 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3097 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3098 V1, V1).getValue(WhichResult);
3099 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3100 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3101 V1, V1).getValue(WhichResult);
3102
Bob Wilsonc692cb72009-08-21 20:54:19 +00003103 // If the shuffle is not directly supported and it has 4 elements, use
3104 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003105 unsigned NumElts = VT.getVectorNumElements();
3106 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003107 unsigned PFIndexes[4];
3108 for (unsigned i = 0; i != 4; ++i) {
3109 if (ShuffleMask[i] < 0)
3110 PFIndexes[i] = 8;
3111 else
3112 PFIndexes[i] = ShuffleMask[i];
3113 }
3114
3115 // Compute the index in the perfect shuffle table.
3116 unsigned PFTableIndex =
3117 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003118 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3119 unsigned Cost = (PFEntry >> 30);
3120
3121 if (Cost <= 4)
3122 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
3123 }
Bob Wilsond8e17572009-08-12 22:31:50 +00003124
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003125 // Implement shuffles with 32- or 64-bit elements as subreg copies.
3126 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3127 if (EltSize >= 32) {
3128 // Do the expansion with floating-point types, since that is what the VFP
3129 // registers are defined to use, and since i64 is not legal.
3130 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3131 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
3132 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V1);
3133 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V2);
3134 SDValue Val = DAG.getUNDEF(VecVT);
3135 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00003136 if (ShuffleMask[i] < 0)
3137 continue;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003138 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
3139 ShuffleMask[i] < (int)NumElts ? V1 : V2,
3140 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
3141 MVT::i32));
3142 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Val,
Bob Wilson63b88452010-05-20 18:39:53 +00003143 Elt, DAG.getConstant(i, MVT::i32));
3144 }
3145 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
3146 }
3147
Bob Wilson22cac0d2009-08-14 05:16:33 +00003148 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003149}
3150
Bob Wilson5bafff32009-06-22 23:27:02 +00003151static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003152 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003153 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003154 SDValue Vec = Op.getOperand(0);
3155 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00003156 assert(VT == MVT::i32 &&
3157 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
3158 "unexpected type for custom-lowering vector extract");
3159 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00003160}
3161
Bob Wilsona6d65862009-08-03 20:36:38 +00003162static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3163 // The only time a CONCAT_VECTORS operation can have legal types is when
3164 // two 64-bit vectors are concatenated to a 128-bit vector.
3165 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
3166 "unexpected CONCAT_VECTORS");
3167 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00003168 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00003169 SDValue Op0 = Op.getOperand(0);
3170 SDValue Op1 = Op.getOperand(1);
3171 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003172 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3173 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00003174 DAG.getIntPtrConstant(0));
3175 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003176 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3177 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00003178 DAG.getIntPtrConstant(1));
3179 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003180}
3181
Dan Gohmand858e902010-04-17 15:26:15 +00003182SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003183 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003184 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00003185 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00003186 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003187 case ISD::GlobalAddress:
3188 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
3189 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00003190 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00003191 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
3192 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003193 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00003194 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00003195 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00003196 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00003197 case ISD::SINT_TO_FP:
3198 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
3199 case ISD::FP_TO_SINT:
3200 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003201 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003202 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003203 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003204 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00003205 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00003206 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00003207 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
3208 Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00003209 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003210 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00003211 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00003212 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00003213 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003214 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00003215 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00003216 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003217 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
3218 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3219 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003220 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00003221 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003222 }
Dan Gohman475871a2008-07-27 21:46:04 +00003223 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003224}
3225
Duncan Sands1607f052008-12-01 11:39:25 +00003226/// ReplaceNodeResults - Replace the results of node with an illegal result
3227/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00003228void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
3229 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00003230 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00003231 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00003232 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00003233 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003234 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003235 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003236 case ISD::BIT_CONVERT:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003237 Res = ExpandBIT_CONVERT(N, DAG);
3238 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00003239 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003240 case ISD::SRA:
3241 Res = LowerShift(N, DAG, Subtarget);
3242 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003243 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00003244 if (Res.getNode())
3245 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00003246}
Chris Lattner27a6c732007-11-24 07:07:01 +00003247
Evan Chenga8e29892007-01-19 07:51:42 +00003248//===----------------------------------------------------------------------===//
3249// ARM Scheduler Hooks
3250//===----------------------------------------------------------------------===//
3251
3252MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003253ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
3254 MachineBasicBlock *BB,
3255 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00003256 unsigned dest = MI->getOperand(0).getReg();
3257 unsigned ptr = MI->getOperand(1).getReg();
3258 unsigned oldval = MI->getOperand(2).getReg();
3259 unsigned newval = MI->getOperand(3).getReg();
3260 unsigned scratch = BB->getParent()->getRegInfo()
3261 .createVirtualRegister(ARM::GPRRegisterClass);
3262 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3263 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003264 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00003265
3266 unsigned ldrOpc, strOpc;
3267 switch (Size) {
3268 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003269 case 1:
3270 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
3271 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
3272 break;
3273 case 2:
3274 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3275 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3276 break;
3277 case 4:
3278 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3279 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3280 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00003281 }
3282
3283 MachineFunction *MF = BB->getParent();
3284 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3285 MachineFunction::iterator It = BB;
3286 ++It; // insert the new blocks after the current block
3287
3288 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3289 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3290 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3291 MF->insert(It, loop1MBB);
3292 MF->insert(It, loop2MBB);
3293 MF->insert(It, exitMBB);
3294 exitMBB->transferSuccessors(BB);
3295
3296 // thisMBB:
3297 // ...
3298 // fallthrough --> loop1MBB
3299 BB->addSuccessor(loop1MBB);
3300
3301 // loop1MBB:
3302 // ldrex dest, [ptr]
3303 // cmp dest, oldval
3304 // bne exitMBB
3305 BB = loop1MBB;
3306 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003307 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003308 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003309 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3310 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003311 BB->addSuccessor(loop2MBB);
3312 BB->addSuccessor(exitMBB);
3313
3314 // loop2MBB:
3315 // strex scratch, newval, [ptr]
3316 // cmp scratch, #0
3317 // bne loop1MBB
3318 BB = loop2MBB;
3319 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
3320 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003321 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003322 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003323 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3324 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003325 BB->addSuccessor(loop1MBB);
3326 BB->addSuccessor(exitMBB);
3327
3328 // exitMBB:
3329 // ...
3330 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00003331
3332 MF->DeleteMachineInstr(MI); // The instruction is gone now.
3333
Jim Grosbach5278eb82009-12-11 01:42:04 +00003334 return BB;
3335}
3336
3337MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003338ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
3339 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00003340 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
3341 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3342
3343 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003344 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003345 MachineFunction::iterator It = BB;
3346 ++It;
3347
3348 unsigned dest = MI->getOperand(0).getReg();
3349 unsigned ptr = MI->getOperand(1).getReg();
3350 unsigned incr = MI->getOperand(2).getReg();
3351 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00003352
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003353 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003354 unsigned ldrOpc, strOpc;
3355 switch (Size) {
3356 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003357 case 1:
3358 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00003359 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003360 break;
3361 case 2:
3362 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3363 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3364 break;
3365 case 4:
3366 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3367 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3368 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00003369 }
3370
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003371 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3372 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3373 MF->insert(It, loopMBB);
3374 MF->insert(It, exitMBB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003375 exitMBB->transferSuccessors(BB);
3376
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003377 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003378 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3379 unsigned scratch2 = (!BinOpcode) ? incr :
3380 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3381
3382 // thisMBB:
3383 // ...
3384 // fallthrough --> loopMBB
3385 BB->addSuccessor(loopMBB);
3386
3387 // loopMBB:
3388 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003389 // <binop> scratch2, dest, incr
3390 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00003391 // cmp scratch, #0
3392 // bne- loopMBB
3393 // fallthrough --> exitMBB
3394 BB = loopMBB;
3395 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00003396 if (BinOpcode) {
3397 // operand order needs to go the other way for NAND
3398 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
3399 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3400 addReg(incr).addReg(dest)).addReg(0);
3401 else
3402 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3403 addReg(dest).addReg(incr)).addReg(0);
3404 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00003405
3406 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
3407 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003408 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00003409 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003410 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3411 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003412
3413 BB->addSuccessor(loopMBB);
3414 BB->addSuccessor(exitMBB);
3415
3416 // exitMBB:
3417 // ...
3418 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00003419
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003420 MF->DeleteMachineInstr(MI); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00003421
Jim Grosbachc3c23542009-12-14 04:22:04 +00003422 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00003423}
3424
3425MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00003426ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003427 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003428 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00003429 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003430 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00003431 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00003432 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00003433 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00003434 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00003435
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003436 case ARM::ATOMIC_LOAD_ADD_I8:
3437 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3438 case ARM::ATOMIC_LOAD_ADD_I16:
3439 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3440 case ARM::ATOMIC_LOAD_ADD_I32:
3441 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003442
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003443 case ARM::ATOMIC_LOAD_AND_I8:
3444 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3445 case ARM::ATOMIC_LOAD_AND_I16:
3446 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3447 case ARM::ATOMIC_LOAD_AND_I32:
3448 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003449
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003450 case ARM::ATOMIC_LOAD_OR_I8:
3451 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3452 case ARM::ATOMIC_LOAD_OR_I16:
3453 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3454 case ARM::ATOMIC_LOAD_OR_I32:
3455 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003456
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003457 case ARM::ATOMIC_LOAD_XOR_I8:
3458 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3459 case ARM::ATOMIC_LOAD_XOR_I16:
3460 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3461 case ARM::ATOMIC_LOAD_XOR_I32:
3462 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003463
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003464 case ARM::ATOMIC_LOAD_NAND_I8:
3465 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3466 case ARM::ATOMIC_LOAD_NAND_I16:
3467 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3468 case ARM::ATOMIC_LOAD_NAND_I32:
3469 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003470
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003471 case ARM::ATOMIC_LOAD_SUB_I8:
3472 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3473 case ARM::ATOMIC_LOAD_SUB_I16:
3474 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3475 case ARM::ATOMIC_LOAD_SUB_I32:
3476 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003477
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003478 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
3479 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
3480 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00003481
3482 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
3483 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
3484 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003485
Evan Cheng007ea272009-08-12 05:17:19 +00003486 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00003487 // To "insert" a SELECT_CC instruction, we actually have to insert the
3488 // diamond control-flow pattern. The incoming instruction knows the
3489 // destination vreg to set, the condition code register to branch on, the
3490 // true/false values to select between, and a branch opcode to use.
3491 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003492 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00003493 ++It;
3494
3495 // thisMBB:
3496 // ...
3497 // TrueVal = ...
3498 // cmpTY ccX, r1, r2
3499 // bCC copy1MBB
3500 // fallthrough --> copy0MBB
3501 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003502 MachineFunction *F = BB->getParent();
3503 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3504 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00003505 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00003506 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003507 F->insert(It, copy0MBB);
3508 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00003509 // Update machine-CFG edges by first adding all successors of the current
3510 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00003511 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003512 E = BB->succ_end(); I != E; ++I)
Evan Chengce319102009-09-19 09:51:03 +00003513 sinkMBB->addSuccessor(*I);
Evan Chenga8e29892007-01-19 07:51:42 +00003514 // Next, remove all successors of the current block, and add the true
3515 // and fallthrough blocks as its successors.
Evan Chengce319102009-09-19 09:51:03 +00003516 while (!BB->succ_empty())
Evan Chenga8e29892007-01-19 07:51:42 +00003517 BB->removeSuccessor(BB->succ_begin());
3518 BB->addSuccessor(copy0MBB);
3519 BB->addSuccessor(sinkMBB);
3520
3521 // copy0MBB:
3522 // %FalseValue = ...
3523 // # fallthrough to sinkMBB
3524 BB = copy0MBB;
3525
3526 // Update machine-CFG edges
3527 BB->addSuccessor(sinkMBB);
3528
3529 // sinkMBB:
3530 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3531 // ...
3532 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00003533 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00003534 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
3535 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
3536
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003537 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00003538 return BB;
3539 }
Evan Cheng86198642009-08-07 00:34:42 +00003540
3541 case ARM::tANDsp:
3542 case ARM::tADDspr_:
3543 case ARM::tSUBspi_:
3544 case ARM::t2SUBrSPi_:
3545 case ARM::t2SUBrSPi12_:
3546 case ARM::t2SUBrSPs_: {
3547 MachineFunction *MF = BB->getParent();
3548 unsigned DstReg = MI->getOperand(0).getReg();
3549 unsigned SrcReg = MI->getOperand(1).getReg();
3550 bool DstIsDead = MI->getOperand(0).isDead();
3551 bool SrcIsKill = MI->getOperand(1).isKill();
3552
3553 if (SrcReg != ARM::SP) {
3554 // Copy the source to SP from virtual register.
3555 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
3556 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3557 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
3558 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
3559 .addReg(SrcReg, getKillRegState(SrcIsKill));
3560 }
3561
3562 unsigned OpOpc = 0;
3563 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
3564 switch (MI->getOpcode()) {
3565 default:
3566 llvm_unreachable("Unexpected pseudo instruction!");
3567 case ARM::tANDsp:
3568 OpOpc = ARM::tAND;
3569 NeedPred = true;
3570 break;
3571 case ARM::tADDspr_:
3572 OpOpc = ARM::tADDspr;
3573 break;
3574 case ARM::tSUBspi_:
3575 OpOpc = ARM::tSUBspi;
3576 break;
3577 case ARM::t2SUBrSPi_:
3578 OpOpc = ARM::t2SUBrSPi;
3579 NeedPred = true; NeedCC = true;
3580 break;
3581 case ARM::t2SUBrSPi12_:
3582 OpOpc = ARM::t2SUBrSPi12;
3583 NeedPred = true;
3584 break;
3585 case ARM::t2SUBrSPs_:
3586 OpOpc = ARM::t2SUBrSPs;
3587 NeedPred = true; NeedCC = true; NeedOp3 = true;
3588 break;
3589 }
3590 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
3591 if (OpOpc == ARM::tAND)
3592 AddDefaultT1CC(MIB);
3593 MIB.addReg(ARM::SP);
3594 MIB.addOperand(MI->getOperand(2));
3595 if (NeedOp3)
3596 MIB.addOperand(MI->getOperand(3));
3597 if (NeedPred)
3598 AddDefaultPred(MIB);
3599 if (NeedCC)
3600 AddDefaultCC(MIB);
3601
3602 // Copy the result from SP to virtual register.
3603 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
3604 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3605 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
3606 BuildMI(BB, dl, TII->get(CopyOpc))
3607 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
3608 .addReg(ARM::SP);
3609 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3610 return BB;
3611 }
Evan Chenga8e29892007-01-19 07:51:42 +00003612 }
3613}
3614
3615//===----------------------------------------------------------------------===//
3616// ARM Optimization Hooks
3617//===----------------------------------------------------------------------===//
3618
Chris Lattnerd1980a52009-03-12 06:52:53 +00003619static
3620SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3621 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00003622 SelectionDAG &DAG = DCI.DAG;
3623 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003624 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00003625 unsigned Opc = N->getOpcode();
3626 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3627 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3628 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3629 ISD::CondCode CC = ISD::SETCC_INVALID;
3630
3631 if (isSlctCC) {
3632 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
3633 } else {
3634 SDValue CCOp = Slct.getOperand(0);
3635 if (CCOp.getOpcode() == ISD::SETCC)
3636 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
3637 }
3638
3639 bool DoXform = false;
3640 bool InvCC = false;
3641 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
3642 "Bad input!");
3643
3644 if (LHS.getOpcode() == ISD::Constant &&
3645 cast<ConstantSDNode>(LHS)->isNullValue()) {
3646 DoXform = true;
3647 } else if (CC != ISD::SETCC_INVALID &&
3648 RHS.getOpcode() == ISD::Constant &&
3649 cast<ConstantSDNode>(RHS)->isNullValue()) {
3650 std::swap(LHS, RHS);
3651 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003652 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00003653 Op0.getOperand(0).getValueType();
3654 bool isInt = OpVT.isInteger();
3655 CC = ISD::getSetCCInverse(CC, isInt);
3656
3657 if (!TLI.isCondCodeLegal(CC, OpVT))
3658 return SDValue(); // Inverse operator isn't legal.
3659
3660 DoXform = true;
3661 InvCC = true;
3662 }
3663
3664 if (DoXform) {
3665 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3666 if (isSlctCC)
3667 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3668 Slct.getOperand(0), Slct.getOperand(1), CC);
3669 SDValue CCOp = Slct.getOperand(0);
3670 if (InvCC)
3671 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3672 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3673 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3674 CCOp, OtherOp, Result);
3675 }
3676 return SDValue();
3677}
3678
3679/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3680static SDValue PerformADDCombine(SDNode *N,
3681 TargetLowering::DAGCombinerInfo &DCI) {
3682 // added by evan in r37685 with no testcase.
3683 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003684
Chris Lattnerd1980a52009-03-12 06:52:53 +00003685 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3686 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3687 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3688 if (Result.getNode()) return Result;
3689 }
3690 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3691 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3692 if (Result.getNode()) return Result;
3693 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003694
Chris Lattnerd1980a52009-03-12 06:52:53 +00003695 return SDValue();
3696}
3697
3698/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3699static SDValue PerformSUBCombine(SDNode *N,
3700 TargetLowering::DAGCombinerInfo &DCI) {
3701 // added by evan in r37685 with no testcase.
3702 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003703
Chris Lattnerd1980a52009-03-12 06:52:53 +00003704 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3705 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3706 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3707 if (Result.getNode()) return Result;
3708 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003709
Chris Lattnerd1980a52009-03-12 06:52:53 +00003710 return SDValue();
3711}
3712
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003713static SDValue PerformMULCombine(SDNode *N,
3714 TargetLowering::DAGCombinerInfo &DCI,
3715 const ARMSubtarget *Subtarget) {
3716 SelectionDAG &DAG = DCI.DAG;
3717
3718 if (Subtarget->isThumb1Only())
3719 return SDValue();
3720
3721 if (DAG.getMachineFunction().
3722 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
3723 return SDValue();
3724
3725 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
3726 return SDValue();
3727
3728 EVT VT = N->getValueType(0);
3729 if (VT != MVT::i32)
3730 return SDValue();
3731
3732 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
3733 if (!C)
3734 return SDValue();
3735
3736 uint64_t MulAmt = C->getZExtValue();
3737 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
3738 ShiftAmt = ShiftAmt & (32 - 1);
3739 SDValue V = N->getOperand(0);
3740 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003741
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003742 SDValue Res;
3743 MulAmt >>= ShiftAmt;
3744 if (isPowerOf2_32(MulAmt - 1)) {
3745 // (mul x, 2^N + 1) => (add (shl x, N), x)
3746 Res = DAG.getNode(ISD::ADD, DL, VT,
3747 V, DAG.getNode(ISD::SHL, DL, VT,
3748 V, DAG.getConstant(Log2_32(MulAmt-1),
3749 MVT::i32)));
3750 } else if (isPowerOf2_32(MulAmt + 1)) {
3751 // (mul x, 2^N - 1) => (sub (shl x, N), x)
3752 Res = DAG.getNode(ISD::SUB, DL, VT,
3753 DAG.getNode(ISD::SHL, DL, VT,
3754 V, DAG.getConstant(Log2_32(MulAmt+1),
3755 MVT::i32)),
3756 V);
3757 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003758 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003759
3760 if (ShiftAmt != 0)
3761 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
3762 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003763
3764 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003765 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003766 return SDValue();
3767}
3768
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +00003769/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
3770/// ARMISD::VMOVRRD.
Jim Grosbache5165492009-11-09 00:11:35 +00003771static SDValue PerformVMOVRRDCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003772 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003773 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003774 SDValue InDouble = N->getOperand(0);
Jim Grosbache5165492009-11-09 00:11:35 +00003775 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003776 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003777 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003778}
3779
Bob Wilson5bafff32009-06-22 23:27:02 +00003780/// getVShiftImm - Check if this is a valid build_vector for the immediate
3781/// operand of a vector shift operation, where all the elements of the
3782/// build_vector must have the same constant integer value.
3783static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3784 // Ignore bit_converts.
3785 while (Op.getOpcode() == ISD::BIT_CONVERT)
3786 Op = Op.getOperand(0);
3787 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3788 APInt SplatBits, SplatUndef;
3789 unsigned SplatBitSize;
3790 bool HasAnyUndefs;
3791 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3792 HasAnyUndefs, ElementBits) ||
3793 SplatBitSize > ElementBits)
3794 return false;
3795 Cnt = SplatBits.getSExtValue();
3796 return true;
3797}
3798
3799/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3800/// operand of a vector shift left operation. That value must be in the range:
3801/// 0 <= Value < ElementBits for a left shift; or
3802/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003803static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003804 assert(VT.isVector() && "vector shift count is not a vector type");
3805 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3806 if (! getVShiftImm(Op, ElementBits, Cnt))
3807 return false;
3808 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3809}
3810
3811/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3812/// operand of a vector shift right operation. For a shift opcode, the value
3813/// is positive, but for an intrinsic the value count must be negative. The
3814/// absolute value must be in the range:
3815/// 1 <= |Value| <= ElementBits for a right shift; or
3816/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003817static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00003818 int64_t &Cnt) {
3819 assert(VT.isVector() && "vector shift count is not a vector type");
3820 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3821 if (! getVShiftImm(Op, ElementBits, Cnt))
3822 return false;
3823 if (isIntrinsic)
3824 Cnt = -Cnt;
3825 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3826}
3827
3828/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3829static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3830 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3831 switch (IntNo) {
3832 default:
3833 // Don't do anything for most intrinsics.
3834 break;
3835
3836 // Vector shifts: check for immediate versions and lower them.
3837 // Note: This is done during DAG combining instead of DAG legalizing because
3838 // the build_vectors for 64-bit vector element shift counts are generally
3839 // not legal, and it is hard to see their values after they get legalized to
3840 // loads from a constant pool.
3841 case Intrinsic::arm_neon_vshifts:
3842 case Intrinsic::arm_neon_vshiftu:
3843 case Intrinsic::arm_neon_vshiftls:
3844 case Intrinsic::arm_neon_vshiftlu:
3845 case Intrinsic::arm_neon_vshiftn:
3846 case Intrinsic::arm_neon_vrshifts:
3847 case Intrinsic::arm_neon_vrshiftu:
3848 case Intrinsic::arm_neon_vrshiftn:
3849 case Intrinsic::arm_neon_vqshifts:
3850 case Intrinsic::arm_neon_vqshiftu:
3851 case Intrinsic::arm_neon_vqshiftsu:
3852 case Intrinsic::arm_neon_vqshiftns:
3853 case Intrinsic::arm_neon_vqshiftnu:
3854 case Intrinsic::arm_neon_vqshiftnsu:
3855 case Intrinsic::arm_neon_vqrshiftns:
3856 case Intrinsic::arm_neon_vqrshiftnu:
3857 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00003858 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003859 int64_t Cnt;
3860 unsigned VShiftOpc = 0;
3861
3862 switch (IntNo) {
3863 case Intrinsic::arm_neon_vshifts:
3864 case Intrinsic::arm_neon_vshiftu:
3865 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3866 VShiftOpc = ARMISD::VSHL;
3867 break;
3868 }
3869 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3870 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3871 ARMISD::VSHRs : ARMISD::VSHRu);
3872 break;
3873 }
3874 return SDValue();
3875
3876 case Intrinsic::arm_neon_vshiftls:
3877 case Intrinsic::arm_neon_vshiftlu:
3878 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3879 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003880 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003881
3882 case Intrinsic::arm_neon_vrshifts:
3883 case Intrinsic::arm_neon_vrshiftu:
3884 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3885 break;
3886 return SDValue();
3887
3888 case Intrinsic::arm_neon_vqshifts:
3889 case Intrinsic::arm_neon_vqshiftu:
3890 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3891 break;
3892 return SDValue();
3893
3894 case Intrinsic::arm_neon_vqshiftsu:
3895 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3896 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003897 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003898
3899 case Intrinsic::arm_neon_vshiftn:
3900 case Intrinsic::arm_neon_vrshiftn:
3901 case Intrinsic::arm_neon_vqshiftns:
3902 case Intrinsic::arm_neon_vqshiftnu:
3903 case Intrinsic::arm_neon_vqshiftnsu:
3904 case Intrinsic::arm_neon_vqrshiftns:
3905 case Intrinsic::arm_neon_vqrshiftnu:
3906 case Intrinsic::arm_neon_vqrshiftnsu:
3907 // Narrowing shifts require an immediate right shift.
3908 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3909 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003910 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003911
3912 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003913 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003914 }
3915
3916 switch (IntNo) {
3917 case Intrinsic::arm_neon_vshifts:
3918 case Intrinsic::arm_neon_vshiftu:
3919 // Opcode already set above.
3920 break;
3921 case Intrinsic::arm_neon_vshiftls:
3922 case Intrinsic::arm_neon_vshiftlu:
3923 if (Cnt == VT.getVectorElementType().getSizeInBits())
3924 VShiftOpc = ARMISD::VSHLLi;
3925 else
3926 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3927 ARMISD::VSHLLs : ARMISD::VSHLLu);
3928 break;
3929 case Intrinsic::arm_neon_vshiftn:
3930 VShiftOpc = ARMISD::VSHRN; break;
3931 case Intrinsic::arm_neon_vrshifts:
3932 VShiftOpc = ARMISD::VRSHRs; break;
3933 case Intrinsic::arm_neon_vrshiftu:
3934 VShiftOpc = ARMISD::VRSHRu; break;
3935 case Intrinsic::arm_neon_vrshiftn:
3936 VShiftOpc = ARMISD::VRSHRN; break;
3937 case Intrinsic::arm_neon_vqshifts:
3938 VShiftOpc = ARMISD::VQSHLs; break;
3939 case Intrinsic::arm_neon_vqshiftu:
3940 VShiftOpc = ARMISD::VQSHLu; break;
3941 case Intrinsic::arm_neon_vqshiftsu:
3942 VShiftOpc = ARMISD::VQSHLsu; break;
3943 case Intrinsic::arm_neon_vqshiftns:
3944 VShiftOpc = ARMISD::VQSHRNs; break;
3945 case Intrinsic::arm_neon_vqshiftnu:
3946 VShiftOpc = ARMISD::VQSHRNu; break;
3947 case Intrinsic::arm_neon_vqshiftnsu:
3948 VShiftOpc = ARMISD::VQSHRNsu; break;
3949 case Intrinsic::arm_neon_vqrshiftns:
3950 VShiftOpc = ARMISD::VQRSHRNs; break;
3951 case Intrinsic::arm_neon_vqrshiftnu:
3952 VShiftOpc = ARMISD::VQRSHRNu; break;
3953 case Intrinsic::arm_neon_vqrshiftnsu:
3954 VShiftOpc = ARMISD::VQRSHRNsu; break;
3955 }
3956
3957 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003958 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003959 }
3960
3961 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00003962 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003963 int64_t Cnt;
3964 unsigned VShiftOpc = 0;
3965
3966 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3967 VShiftOpc = ARMISD::VSLI;
3968 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3969 VShiftOpc = ARMISD::VSRI;
3970 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003971 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003972 }
3973
3974 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3975 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00003976 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003977 }
3978
3979 case Intrinsic::arm_neon_vqrshifts:
3980 case Intrinsic::arm_neon_vqrshiftu:
3981 // No immediate versions of these to check for.
3982 break;
3983 }
3984
3985 return SDValue();
3986}
3987
3988/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3989/// lowers them. As with the vector shift intrinsics, this is done during DAG
3990/// combining instead of DAG legalizing because the build_vectors for 64-bit
3991/// vector element shift counts are generally not legal, and it is hard to see
3992/// their values after they get legalized to loads from a constant pool.
3993static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3994 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003995 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003996
3997 // Nothing to be done for scalar shifts.
3998 if (! VT.isVector())
3999 return SDValue();
4000
4001 assert(ST->hasNEON() && "unexpected vector shift");
4002 int64_t Cnt;
4003
4004 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004005 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00004006
4007 case ISD::SHL:
4008 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
4009 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004010 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004011 break;
4012
4013 case ISD::SRA:
4014 case ISD::SRL:
4015 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
4016 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
4017 ARMISD::VSHRs : ARMISD::VSHRu);
4018 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004019 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004020 }
4021 }
4022 return SDValue();
4023}
4024
4025/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
4026/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
4027static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
4028 const ARMSubtarget *ST) {
4029 SDValue N0 = N->getOperand(0);
4030
4031 // Check for sign- and zero-extensions of vector extract operations of 8-
4032 // and 16-bit vector elements. NEON supports these directly. They are
4033 // handled during DAG combining because type legalization will promote them
4034 // to 32-bit types and it is messy to recognize the operations after that.
4035 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
4036 SDValue Vec = N0.getOperand(0);
4037 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004038 EVT VT = N->getValueType(0);
4039 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004040 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4041
Owen Anderson825b72b2009-08-11 20:47:22 +00004042 if (VT == MVT::i32 &&
4043 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00004044 TLI.isTypeLegal(Vec.getValueType())) {
4045
4046 unsigned Opc = 0;
4047 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004048 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00004049 case ISD::SIGN_EXTEND:
4050 Opc = ARMISD::VGETLANEs;
4051 break;
4052 case ISD::ZERO_EXTEND:
4053 case ISD::ANY_EXTEND:
4054 Opc = ARMISD::VGETLANEu;
4055 break;
4056 }
4057 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
4058 }
4059 }
4060
4061 return SDValue();
4062}
4063
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004064/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
4065/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
4066static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
4067 const ARMSubtarget *ST) {
4068 // If the target supports NEON, try to use vmax/vmin instructions for f32
4069 // selects like "x < y ? x : y". Unless the FiniteOnlyFPMath option is set,
4070 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
4071 // a NaN; only do the transformation when it matches that behavior.
4072
4073 // For now only do this when using NEON for FP operations; if using VFP, it
4074 // is not obvious that the benefit outweighs the cost of switching to the
4075 // NEON pipeline.
4076 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
4077 N->getValueType(0) != MVT::f32)
4078 return SDValue();
4079
4080 SDValue CondLHS = N->getOperand(0);
4081 SDValue CondRHS = N->getOperand(1);
4082 SDValue LHS = N->getOperand(2);
4083 SDValue RHS = N->getOperand(3);
4084 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
4085
4086 unsigned Opcode = 0;
4087 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00004088 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004089 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00004090 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004091 IsReversed = true ; // x CC y ? y : x
4092 } else {
4093 return SDValue();
4094 }
4095
Bob Wilsone742bb52010-02-24 22:15:53 +00004096 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004097 switch (CC) {
4098 default: break;
4099 case ISD::SETOLT:
4100 case ISD::SETOLE:
4101 case ISD::SETLT:
4102 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004103 case ISD::SETULT:
4104 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004105 // If LHS is NaN, an ordered comparison will be false and the result will
4106 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
4107 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4108 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
4109 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4110 break;
4111 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
4112 // will return -0, so vmin can only be used for unsafe math or if one of
4113 // the operands is known to be nonzero.
4114 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
4115 !UnsafeFPMath &&
4116 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4117 break;
4118 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004119 break;
4120
4121 case ISD::SETOGT:
4122 case ISD::SETOGE:
4123 case ISD::SETGT:
4124 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004125 case ISD::SETUGT:
4126 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004127 // If LHS is NaN, an ordered comparison will be false and the result will
4128 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
4129 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4130 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
4131 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4132 break;
4133 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
4134 // will return +0, so vmax can only be used for unsafe math or if one of
4135 // the operands is known to be nonzero.
4136 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
4137 !UnsafeFPMath &&
4138 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4139 break;
4140 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004141 break;
4142 }
4143
4144 if (!Opcode)
4145 return SDValue();
4146 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
4147}
4148
Dan Gohman475871a2008-07-27 21:46:04 +00004149SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004150 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004151 switch (N->getOpcode()) {
4152 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004153 case ISD::ADD: return PerformADDCombine(N, DCI);
4154 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004155 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbache5165492009-11-09 00:11:35 +00004156 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004157 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004158 case ISD::SHL:
4159 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004160 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004161 case ISD::SIGN_EXTEND:
4162 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004163 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
4164 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004165 }
Dan Gohman475871a2008-07-27 21:46:04 +00004166 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004167}
4168
Bill Wendlingaf566342009-08-15 21:21:19 +00004169bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
4170 if (!Subtarget->hasV6Ops())
4171 // Pre-v6 does not support unaligned mem access.
4172 return false;
Anton Korobeynikov90cfc132010-01-30 14:08:12 +00004173 else {
4174 // v6+ may or may not support unaligned mem access depending on the system
4175 // configuration.
4176 // FIXME: This is pretty conservative. Should we provide cmdline option to
4177 // control the behaviour?
Bill Wendlingaf566342009-08-15 21:21:19 +00004178 if (!Subtarget->isTargetDarwin())
4179 return false;
4180 }
4181
4182 switch (VT.getSimpleVT().SimpleTy) {
4183 default:
4184 return false;
4185 case MVT::i8:
4186 case MVT::i16:
4187 case MVT::i32:
4188 return true;
4189 // FIXME: VLD1 etc with standard alignment is legal.
4190 }
4191}
4192
Evan Chenge6c835f2009-08-14 20:09:37 +00004193static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
4194 if (V < 0)
4195 return false;
4196
4197 unsigned Scale = 1;
4198 switch (VT.getSimpleVT().SimpleTy) {
4199 default: return false;
4200 case MVT::i1:
4201 case MVT::i8:
4202 // Scale == 1;
4203 break;
4204 case MVT::i16:
4205 // Scale == 2;
4206 Scale = 2;
4207 break;
4208 case MVT::i32:
4209 // Scale == 4;
4210 Scale = 4;
4211 break;
4212 }
4213
4214 if ((V & (Scale - 1)) != 0)
4215 return false;
4216 V /= Scale;
4217 return V == (V & ((1LL << 5) - 1));
4218}
4219
4220static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
4221 const ARMSubtarget *Subtarget) {
4222 bool isNeg = false;
4223 if (V < 0) {
4224 isNeg = true;
4225 V = - V;
4226 }
4227
4228 switch (VT.getSimpleVT().SimpleTy) {
4229 default: return false;
4230 case MVT::i1:
4231 case MVT::i8:
4232 case MVT::i16:
4233 case MVT::i32:
4234 // + imm12 or - imm8
4235 if (isNeg)
4236 return V == (V & ((1LL << 8) - 1));
4237 return V == (V & ((1LL << 12) - 1));
4238 case MVT::f32:
4239 case MVT::f64:
4240 // Same as ARM mode. FIXME: NEON?
4241 if (!Subtarget->hasVFP2())
4242 return false;
4243 if ((V & 3) != 0)
4244 return false;
4245 V >>= 2;
4246 return V == (V & ((1LL << 8) - 1));
4247 }
4248}
4249
Evan Chengb01fad62007-03-12 23:30:29 +00004250/// isLegalAddressImmediate - Return true if the integer value can be used
4251/// as the offset of the target addressing mode for load / store of the
4252/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00004253static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004254 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00004255 if (V == 0)
4256 return true;
4257
Evan Cheng65011532009-03-09 19:15:00 +00004258 if (!VT.isSimple())
4259 return false;
4260
Evan Chenge6c835f2009-08-14 20:09:37 +00004261 if (Subtarget->isThumb1Only())
4262 return isLegalT1AddressImmediate(V, VT);
4263 else if (Subtarget->isThumb2())
4264 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00004265
Evan Chenge6c835f2009-08-14 20:09:37 +00004266 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00004267 if (V < 0)
4268 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00004269 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00004270 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004271 case MVT::i1:
4272 case MVT::i8:
4273 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00004274 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004275 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004276 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00004277 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004278 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004279 case MVT::f32:
4280 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00004281 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00004282 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00004283 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00004284 return false;
4285 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004286 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00004287 }
Evan Chenga8e29892007-01-19 07:51:42 +00004288}
4289
Evan Chenge6c835f2009-08-14 20:09:37 +00004290bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
4291 EVT VT) const {
4292 int Scale = AM.Scale;
4293 if (Scale < 0)
4294 return false;
4295
4296 switch (VT.getSimpleVT().SimpleTy) {
4297 default: return false;
4298 case MVT::i1:
4299 case MVT::i8:
4300 case MVT::i16:
4301 case MVT::i32:
4302 if (Scale == 1)
4303 return true;
4304 // r + r << imm
4305 Scale = Scale & ~1;
4306 return Scale == 2 || Scale == 4 || Scale == 8;
4307 case MVT::i64:
4308 // r + r
4309 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
4310 return true;
4311 return false;
4312 case MVT::isVoid:
4313 // Note, we allow "void" uses (basically, uses that aren't loads or
4314 // stores), because arm allows folding a scale into many arithmetic
4315 // operations. This should be made more precise and revisited later.
4316
4317 // Allow r << imm, but the imm has to be a multiple of two.
4318 if (Scale & 1) return false;
4319 return isPowerOf2_32(Scale);
4320 }
4321}
4322
Chris Lattner37caf8c2007-04-09 23:33:39 +00004323/// isLegalAddressingMode - Return true if the addressing mode represented
4324/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004325bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004326 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004327 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00004328 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00004329 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004330
Chris Lattner37caf8c2007-04-09 23:33:39 +00004331 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004332 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004333 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004334
Chris Lattner37caf8c2007-04-09 23:33:39 +00004335 switch (AM.Scale) {
4336 case 0: // no scale reg, must be "r+i" or "r", or "i".
4337 break;
4338 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00004339 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00004340 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004341 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00004342 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004343 // ARM doesn't support any R+R*scale+imm addr modes.
4344 if (AM.BaseOffs)
4345 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004346
Bob Wilson2c7dab12009-04-08 17:55:28 +00004347 if (!VT.isSimple())
4348 return false;
4349
Evan Chenge6c835f2009-08-14 20:09:37 +00004350 if (Subtarget->isThumb2())
4351 return isLegalT2ScaledAddressingMode(AM, VT);
4352
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004353 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00004354 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00004355 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004356 case MVT::i1:
4357 case MVT::i8:
4358 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004359 if (Scale < 0) Scale = -Scale;
4360 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004361 return true;
4362 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00004363 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00004364 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00004365 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004366 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004367 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004368 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00004369 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004370
Owen Anderson825b72b2009-08-11 20:47:22 +00004371 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004372 // Note, we allow "void" uses (basically, uses that aren't loads or
4373 // stores), because arm allows folding a scale into many arithmetic
4374 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004375
Chris Lattner37caf8c2007-04-09 23:33:39 +00004376 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00004377 if (Scale & 1) return false;
4378 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00004379 }
4380 break;
Evan Chengb01fad62007-03-12 23:30:29 +00004381 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00004382 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00004383}
4384
Evan Cheng77e47512009-11-11 19:05:52 +00004385/// isLegalICmpImmediate - Return true if the specified immediate is legal
4386/// icmp immediate, that is the target has icmp instructions which can compare
4387/// a register against the immediate without having to materialize the
4388/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00004389bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00004390 if (!Subtarget->isThumb())
4391 return ARM_AM::getSOImmVal(Imm) != -1;
4392 if (Subtarget->isThumb2())
4393 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00004394 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00004395}
4396
Owen Andersone50ed302009-08-10 22:56:29 +00004397static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004398 bool isSEXTLoad, SDValue &Base,
4399 SDValue &Offset, bool &isInc,
4400 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00004401 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4402 return false;
4403
Owen Anderson825b72b2009-08-11 20:47:22 +00004404 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00004405 // AddressingMode 3
4406 Base = Ptr->getOperand(0);
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 > -256) {
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 return true;
4414 }
4415 }
4416 isInc = (Ptr->getOpcode() == ISD::ADD);
4417 Offset = Ptr->getOperand(1);
4418 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004419 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00004420 // AddressingMode 2
4421 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004422 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004423 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004424 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004425 isInc = false;
4426 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4427 Base = Ptr->getOperand(0);
4428 return true;
4429 }
4430 }
4431
4432 if (Ptr->getOpcode() == ISD::ADD) {
4433 isInc = true;
4434 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
4435 if (ShOpcVal != ARM_AM::no_shift) {
4436 Base = Ptr->getOperand(1);
4437 Offset = Ptr->getOperand(0);
4438 } else {
4439 Base = Ptr->getOperand(0);
4440 Offset = Ptr->getOperand(1);
4441 }
4442 return true;
4443 }
4444
4445 isInc = (Ptr->getOpcode() == ISD::ADD);
4446 Base = Ptr->getOperand(0);
4447 Offset = Ptr->getOperand(1);
4448 return true;
4449 }
4450
Jim Grosbache5165492009-11-09 00:11:35 +00004451 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00004452 return false;
4453}
4454
Owen Andersone50ed302009-08-10 22:56:29 +00004455static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004456 bool isSEXTLoad, SDValue &Base,
4457 SDValue &Offset, bool &isInc,
4458 SelectionDAG &DAG) {
4459 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4460 return false;
4461
4462 Base = Ptr->getOperand(0);
4463 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
4464 int RHSC = (int)RHS->getZExtValue();
4465 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
4466 assert(Ptr->getOpcode() == ISD::ADD);
4467 isInc = false;
4468 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4469 return true;
4470 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
4471 isInc = Ptr->getOpcode() == ISD::ADD;
4472 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
4473 return true;
4474 }
4475 }
4476
4477 return false;
4478}
4479
Evan Chenga8e29892007-01-19 07:51:42 +00004480/// getPreIndexedAddressParts - returns true by value, base pointer and
4481/// offset pointer and addressing mode by reference if the node's address
4482/// can be legally represented as pre-indexed load / store address.
4483bool
Dan Gohman475871a2008-07-27 21:46:04 +00004484ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
4485 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004486 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004487 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004488 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004489 return false;
4490
Owen Andersone50ed302009-08-10 22:56:29 +00004491 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004492 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004493 bool isSEXTLoad = false;
4494 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4495 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004496 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004497 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4498 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
4499 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004500 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004501 } else
4502 return false;
4503
4504 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004505 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004506 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004507 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
4508 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004509 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004510 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00004511 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00004512 if (!isLegal)
4513 return false;
4514
4515 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
4516 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004517}
4518
4519/// getPostIndexedAddressParts - returns true by value, base pointer and
4520/// offset pointer and addressing mode by reference if this node can be
4521/// combined with a load / store to form a post-indexed load / store.
4522bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00004523 SDValue &Base,
4524 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004525 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004526 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004527 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004528 return false;
4529
Owen Andersone50ed302009-08-10 22:56:29 +00004530 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004531 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004532 bool isSEXTLoad = false;
4533 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004534 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00004535 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00004536 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4537 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004538 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00004539 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00004540 } else
4541 return false;
4542
4543 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004544 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004545 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004546 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00004547 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004548 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004549 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
4550 isInc, DAG);
4551 if (!isLegal)
4552 return false;
4553
Evan Cheng28dad2a2010-05-18 21:31:17 +00004554 if (Ptr != Base) {
4555 // Swap base ptr and offset to catch more post-index load / store when
4556 // it's legal. In Thumb2 mode, offset must be an immediate.
4557 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
4558 !Subtarget->isThumb2())
4559 std::swap(Base, Offset);
4560
4561 // Post-indexed load / store update the base pointer.
4562 if (Ptr != Base)
4563 return false;
4564 }
4565
Evan Chenge88d5ce2009-07-02 07:28:31 +00004566 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
4567 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004568}
4569
Dan Gohman475871a2008-07-27 21:46:04 +00004570void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00004571 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004572 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004573 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00004574 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00004575 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004576 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004577 switch (Op.getOpcode()) {
4578 default: break;
4579 case ARMISD::CMOV: {
4580 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00004581 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004582 if (KnownZero == 0 && KnownOne == 0) return;
4583
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004584 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00004585 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
4586 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004587 KnownZero &= KnownZeroRHS;
4588 KnownOne &= KnownOneRHS;
4589 return;
4590 }
4591 }
4592}
4593
4594//===----------------------------------------------------------------------===//
4595// ARM Inline Assembly Support
4596//===----------------------------------------------------------------------===//
4597
4598/// getConstraintType - Given a constraint letter, return the type of
4599/// constraint it is for this target.
4600ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00004601ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
4602 if (Constraint.size() == 1) {
4603 switch (Constraint[0]) {
4604 default: break;
4605 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004606 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00004607 }
Evan Chenga8e29892007-01-19 07:51:42 +00004608 }
Chris Lattner4234f572007-03-25 02:14:49 +00004609 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00004610}
4611
Bob Wilson2dc4f542009-03-20 22:42:55 +00004612std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00004613ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004614 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004615 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004616 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00004617 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004618 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004619 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004620 return std::make_pair(0U, ARM::tGPRRegisterClass);
4621 else
4622 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004623 case 'r':
4624 return std::make_pair(0U, ARM::GPRRegisterClass);
4625 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004626 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004627 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00004628 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004629 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00004630 if (VT.getSizeInBits() == 128)
4631 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004632 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004633 }
4634 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00004635 if (StringRef("{cc}").equals_lower(Constraint))
4636 return std::make_pair(0U, ARM::CCRRegisterClass);
4637
Evan Chenga8e29892007-01-19 07:51:42 +00004638 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
4639}
4640
4641std::vector<unsigned> ARMTargetLowering::
4642getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004643 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004644 if (Constraint.size() != 1)
4645 return std::vector<unsigned>();
4646
4647 switch (Constraint[0]) { // GCC ARM Constraint Letters
4648 default: break;
4649 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004650 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4651 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4652 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004653 case 'r':
4654 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4655 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4656 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
4657 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004658 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004659 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004660 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
4661 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
4662 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
4663 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
4664 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
4665 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
4666 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
4667 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00004668 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004669 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
4670 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
4671 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
4672 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00004673 if (VT.getSizeInBits() == 128)
4674 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
4675 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004676 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004677 }
4678
4679 return std::vector<unsigned>();
4680}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004681
4682/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
4683/// vector. If it is invalid, don't add anything to Ops.
4684void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
4685 char Constraint,
4686 bool hasMemory,
4687 std::vector<SDValue>&Ops,
4688 SelectionDAG &DAG) const {
4689 SDValue Result(0, 0);
4690
4691 switch (Constraint) {
4692 default: break;
4693 case 'I': case 'J': case 'K': case 'L':
4694 case 'M': case 'N': case 'O':
4695 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
4696 if (!C)
4697 return;
4698
4699 int64_t CVal64 = C->getSExtValue();
4700 int CVal = (int) CVal64;
4701 // None of these constraints allow values larger than 32 bits. Check
4702 // that the value fits in an int.
4703 if (CVal != CVal64)
4704 return;
4705
4706 switch (Constraint) {
4707 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004708 if (Subtarget->isThumb1Only()) {
4709 // This must be a constant between 0 and 255, for ADD
4710 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004711 if (CVal >= 0 && CVal <= 255)
4712 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004713 } else if (Subtarget->isThumb2()) {
4714 // A constant that can be used as an immediate value in a
4715 // data-processing instruction.
4716 if (ARM_AM::getT2SOImmVal(CVal) != -1)
4717 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004718 } else {
4719 // A constant that can be used as an immediate value in a
4720 // data-processing instruction.
4721 if (ARM_AM::getSOImmVal(CVal) != -1)
4722 break;
4723 }
4724 return;
4725
4726 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004727 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004728 // This must be a constant between -255 and -1, for negated ADD
4729 // immediates. This can be used in GCC with an "n" modifier that
4730 // prints the negated value, for use with SUB instructions. It is
4731 // not useful otherwise but is implemented for compatibility.
4732 if (CVal >= -255 && CVal <= -1)
4733 break;
4734 } else {
4735 // This must be a constant between -4095 and 4095. It is not clear
4736 // what this constraint is intended for. Implemented for
4737 // compatibility with GCC.
4738 if (CVal >= -4095 && CVal <= 4095)
4739 break;
4740 }
4741 return;
4742
4743 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004744 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004745 // A 32-bit value where only one byte has a nonzero value. Exclude
4746 // zero to match GCC. This constraint is used by GCC internally for
4747 // constants that can be loaded with a move/shift combination.
4748 // It is not useful otherwise but is implemented for compatibility.
4749 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
4750 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004751 } else if (Subtarget->isThumb2()) {
4752 // A constant whose bitwise inverse can be used as an immediate
4753 // value in a data-processing instruction. This can be used in GCC
4754 // with a "B" modifier that prints the inverted value, for use with
4755 // BIC and MVN instructions. It is not useful otherwise but is
4756 // implemented for compatibility.
4757 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
4758 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004759 } else {
4760 // A constant whose bitwise inverse can be used as an immediate
4761 // value in a data-processing instruction. This can be used in GCC
4762 // with a "B" modifier that prints the inverted value, for use with
4763 // BIC and MVN instructions. It is not useful otherwise but is
4764 // implemented for compatibility.
4765 if (ARM_AM::getSOImmVal(~CVal) != -1)
4766 break;
4767 }
4768 return;
4769
4770 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004771 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004772 // This must be a constant between -7 and 7,
4773 // for 3-operand ADD/SUB immediate instructions.
4774 if (CVal >= -7 && CVal < 7)
4775 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004776 } else if (Subtarget->isThumb2()) {
4777 // A constant whose negation can be used as an immediate value in a
4778 // data-processing instruction. This can be used in GCC with an "n"
4779 // modifier that prints the negated value, for use with SUB
4780 // instructions. It is not useful otherwise but is implemented for
4781 // compatibility.
4782 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
4783 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004784 } else {
4785 // A constant whose negation can be used as an immediate value in a
4786 // data-processing instruction. This can be used in GCC with an "n"
4787 // modifier that prints the negated value, for use with SUB
4788 // instructions. It is not useful otherwise but is implemented for
4789 // compatibility.
4790 if (ARM_AM::getSOImmVal(-CVal) != -1)
4791 break;
4792 }
4793 return;
4794
4795 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004796 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004797 // This must be a multiple of 4 between 0 and 1020, for
4798 // ADD sp + immediate.
4799 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
4800 break;
4801 } else {
4802 // A power of two or a constant between 0 and 32. This is used in
4803 // GCC for the shift amount on shifted register operands, but it is
4804 // useful in general for any shift amounts.
4805 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
4806 break;
4807 }
4808 return;
4809
4810 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004811 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004812 // This must be a constant between 0 and 31, for shift amounts.
4813 if (CVal >= 0 && CVal <= 31)
4814 break;
4815 }
4816 return;
4817
4818 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004819 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004820 // This must be a multiple of 4 between -508 and 508, for
4821 // ADD/SUB sp = sp + immediate.
4822 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
4823 break;
4824 }
4825 return;
4826 }
4827 Result = DAG.getTargetConstant(CVal, Op.getValueType());
4828 break;
4829 }
4830
4831 if (Result.getNode()) {
4832 Ops.push_back(Result);
4833 return;
4834 }
4835 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
4836 Ops, DAG);
4837}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00004838
4839bool
4840ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4841 // The ARM target isn't yet aware of offsets.
4842 return false;
4843}
Evan Cheng39382422009-10-28 01:44:26 +00004844
4845int ARM::getVFPf32Imm(const APFloat &FPImm) {
4846 APInt Imm = FPImm.bitcastToAPInt();
4847 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
4848 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
4849 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
4850
4851 // We can handle 4 bits of mantissa.
4852 // mantissa = (16+UInt(e:f:g:h))/16.
4853 if (Mantissa & 0x7ffff)
4854 return -1;
4855 Mantissa >>= 19;
4856 if ((Mantissa & 0xf) != Mantissa)
4857 return -1;
4858
4859 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4860 if (Exp < -3 || Exp > 4)
4861 return -1;
4862 Exp = ((Exp+3) & 0x7) ^ 4;
4863
4864 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4865}
4866
4867int ARM::getVFPf64Imm(const APFloat &FPImm) {
4868 APInt Imm = FPImm.bitcastToAPInt();
4869 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
4870 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
4871 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
4872
4873 // We can handle 4 bits of mantissa.
4874 // mantissa = (16+UInt(e:f:g:h))/16.
4875 if (Mantissa & 0xffffffffffffLL)
4876 return -1;
4877 Mantissa >>= 48;
4878 if ((Mantissa & 0xf) != Mantissa)
4879 return -1;
4880
4881 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4882 if (Exp < -3 || Exp > 4)
4883 return -1;
4884 Exp = ((Exp+3) & 0x7) ^ 4;
4885
4886 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4887}
4888
4889/// isFPImmLegal - Returns true if the target can instruction select the
4890/// specified FP immediate natively. If false, the legalizer will
4891/// materialize the FP immediate as a load from a constant pool.
4892bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
4893 if (!Subtarget->hasVFP3())
4894 return false;
4895 if (VT == MVT::f32)
4896 return ARM::getVFPf32Imm(Imm) != -1;
4897 if (VT == MVT::f64)
4898 return ARM::getVFPf64Imm(Imm) != -1;
4899 return false;
4900}