blob: 8d38d808d4faa1bc92ec6ddc9db1d00ea80d55e0 [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 {
608 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
609 EVT VT = N->getValueType(i);
610 if (VT.isFloatingPoint() || VT.isVector())
611 return Sched::Latency;
612 }
613 return Sched::RegPressure;
614}
615
Evan Chenga8e29892007-01-19 07:51:42 +0000616//===----------------------------------------------------------------------===//
617// Lowering Code
618//===----------------------------------------------------------------------===//
619
Evan Chenga8e29892007-01-19 07:51:42 +0000620/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
621static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
622 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000623 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000624 case ISD::SETNE: return ARMCC::NE;
625 case ISD::SETEQ: return ARMCC::EQ;
626 case ISD::SETGT: return ARMCC::GT;
627 case ISD::SETGE: return ARMCC::GE;
628 case ISD::SETLT: return ARMCC::LT;
629 case ISD::SETLE: return ARMCC::LE;
630 case ISD::SETUGT: return ARMCC::HI;
631 case ISD::SETUGE: return ARMCC::HS;
632 case ISD::SETULT: return ARMCC::LO;
633 case ISD::SETULE: return ARMCC::LS;
634 }
635}
636
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000637/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
638static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000639 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000640 CondCode2 = ARMCC::AL;
641 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000642 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000643 case ISD::SETEQ:
644 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
645 case ISD::SETGT:
646 case ISD::SETOGT: CondCode = ARMCC::GT; break;
647 case ISD::SETGE:
648 case ISD::SETOGE: CondCode = ARMCC::GE; break;
649 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000650 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000651 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
652 case ISD::SETO: CondCode = ARMCC::VC; break;
653 case ISD::SETUO: CondCode = ARMCC::VS; break;
654 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
655 case ISD::SETUGT: CondCode = ARMCC::HI; break;
656 case ISD::SETUGE: CondCode = ARMCC::PL; break;
657 case ISD::SETLT:
658 case ISD::SETULT: CondCode = ARMCC::LT; break;
659 case ISD::SETLE:
660 case ISD::SETULE: CondCode = ARMCC::LE; break;
661 case ISD::SETNE:
662 case ISD::SETUNE: CondCode = ARMCC::NE; break;
663 }
Evan Chenga8e29892007-01-19 07:51:42 +0000664}
665
Bob Wilson1f595bb2009-04-17 19:07:39 +0000666//===----------------------------------------------------------------------===//
667// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000668//===----------------------------------------------------------------------===//
669
670#include "ARMGenCallingConv.inc"
671
672// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000673static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000674 CCValAssign::LocInfo &LocInfo,
675 CCState &State, bool CanFail) {
676 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
677
678 // Try to get the first register.
679 if (unsigned Reg = State.AllocateReg(RegList, 4))
680 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
681 else {
682 // For the 2nd half of a v2f64, do not fail.
683 if (CanFail)
684 return false;
685
686 // Put the whole thing on the stack.
687 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
688 State.AllocateStack(8, 4),
689 LocVT, LocInfo));
690 return true;
691 }
692
693 // Try to get the second register.
694 if (unsigned Reg = State.AllocateReg(RegList, 4))
695 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
696 else
697 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
698 State.AllocateStack(4, 4),
699 LocVT, LocInfo));
700 return true;
701}
702
Owen Andersone50ed302009-08-10 22:56:29 +0000703static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000704 CCValAssign::LocInfo &LocInfo,
705 ISD::ArgFlagsTy &ArgFlags,
706 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000707 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
708 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000709 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000710 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
711 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000712 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000713}
714
715// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000716static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000717 CCValAssign::LocInfo &LocInfo,
718 CCState &State, bool CanFail) {
719 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
720 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
721
722 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
723 if (Reg == 0) {
724 // For the 2nd half of a v2f64, do not just fail.
725 if (CanFail)
726 return false;
727
728 // Put the whole thing on the stack.
729 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
730 State.AllocateStack(8, 8),
731 LocVT, LocInfo));
732 return true;
733 }
734
735 unsigned i;
736 for (i = 0; i < 2; ++i)
737 if (HiRegList[i] == Reg)
738 break;
739
740 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
741 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
742 LocVT, LocInfo));
743 return true;
744}
745
Owen Andersone50ed302009-08-10 22:56:29 +0000746static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000747 CCValAssign::LocInfo &LocInfo,
748 ISD::ArgFlagsTy &ArgFlags,
749 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000750 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
751 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000752 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000753 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
754 return false;
755 return true; // we handled it
756}
757
Owen Andersone50ed302009-08-10 22:56:29 +0000758static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000759 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000760 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
761 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
762
Bob Wilsone65586b2009-04-17 20:40:45 +0000763 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
764 if (Reg == 0)
765 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000766
Bob Wilsone65586b2009-04-17 20:40:45 +0000767 unsigned i;
768 for (i = 0; i < 2; ++i)
769 if (HiRegList[i] == Reg)
770 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000771
Bob Wilson5bafff32009-06-22 23:27:02 +0000772 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000773 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000774 LocVT, LocInfo));
775 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000776}
777
Owen Andersone50ed302009-08-10 22:56:29 +0000778static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000779 CCValAssign::LocInfo &LocInfo,
780 ISD::ArgFlagsTy &ArgFlags,
781 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000782 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
783 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000784 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000785 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000786 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000787}
788
Owen Andersone50ed302009-08-10 22:56:29 +0000789static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000790 CCValAssign::LocInfo &LocInfo,
791 ISD::ArgFlagsTy &ArgFlags,
792 CCState &State) {
793 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
794 State);
795}
796
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000797/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
798/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000799CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000800 bool Return,
801 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000802 switch (CC) {
803 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000804 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000805 case CallingConv::C:
806 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000807 // Use target triple & subtarget features to do actual dispatch.
808 if (Subtarget->isAAPCS_ABI()) {
809 if (Subtarget->hasVFP2() &&
810 FloatABIType == FloatABI::Hard && !isVarArg)
811 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
812 else
813 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
814 } else
815 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000816 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000817 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000818 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000819 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000820 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000821 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000822 }
823}
824
Dan Gohman98ca4f22009-08-05 01:29:28 +0000825/// LowerCallResult - Lower the result values of a call into the
826/// appropriate copies out of appropriate physical registers.
827SDValue
828ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000829 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000830 const SmallVectorImpl<ISD::InputArg> &Ins,
831 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000832 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000833
Bob Wilson1f595bb2009-04-17 19:07:39 +0000834 // Assign locations to each value returned by this call.
835 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000836 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000837 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000838 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000839 CCAssignFnForNode(CallConv, /* Return*/ true,
840 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000841
842 // Copy all of the result registers out of their specified physreg.
843 for (unsigned i = 0; i != RVLocs.size(); ++i) {
844 CCValAssign VA = RVLocs[i];
845
Bob Wilson80915242009-04-25 00:33:20 +0000846 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000847 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000848 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000849 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000850 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000851 Chain = Lo.getValue(1);
852 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000853 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000854 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000855 InFlag);
856 Chain = Hi.getValue(1);
857 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000858 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000859
Owen Anderson825b72b2009-08-11 20:47:22 +0000860 if (VA.getLocVT() == MVT::v2f64) {
861 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
862 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
863 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000864
865 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000866 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000867 Chain = Lo.getValue(1);
868 InFlag = Lo.getValue(2);
869 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000870 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000871 Chain = Hi.getValue(1);
872 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000873 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +0000874 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
875 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000876 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000877 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000878 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
879 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000880 Chain = Val.getValue(1);
881 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000882 }
Bob Wilson80915242009-04-25 00:33:20 +0000883
884 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000885 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000886 case CCValAssign::Full: break;
887 case CCValAssign::BCvt:
888 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
889 break;
890 }
891
Dan Gohman98ca4f22009-08-05 01:29:28 +0000892 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000893 }
894
Dan Gohman98ca4f22009-08-05 01:29:28 +0000895 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000896}
897
898/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
899/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000900/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000901/// a byval function parameter.
902/// Sometimes what we are copying is the end of a larger object, the part that
903/// does not fit in registers.
904static SDValue
905CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
906 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
907 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000908 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000909 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +0000910 /*isVolatile=*/false, /*AlwaysInline=*/false,
911 NULL, 0, NULL, 0);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000912}
913
Bob Wilsondee46d72009-04-17 20:35:10 +0000914/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000915SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000916ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
917 SDValue StackPtr, SDValue Arg,
918 DebugLoc dl, SelectionDAG &DAG,
919 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +0000920 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000921 unsigned LocMemOffset = VA.getLocMemOffset();
922 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
923 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
924 if (Flags.isByVal()) {
925 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
926 }
927 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene1b58cab2010-02-15 16:55:24 +0000928 PseudoSourceValue::getStack(), LocMemOffset,
929 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +0000930}
931
Dan Gohman98ca4f22009-08-05 01:29:28 +0000932void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000933 SDValue Chain, SDValue &Arg,
934 RegsToPassVector &RegsToPass,
935 CCValAssign &VA, CCValAssign &NextVA,
936 SDValue &StackPtr,
937 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +0000938 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +0000939
Jim Grosbache5165492009-11-09 00:11:35 +0000940 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000941 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000942 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
943
944 if (NextVA.isRegLoc())
945 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
946 else {
947 assert(NextVA.isMemLoc());
948 if (StackPtr.getNode() == 0)
949 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
950
Dan Gohman98ca4f22009-08-05 01:29:28 +0000951 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
952 dl, DAG, NextVA,
953 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000954 }
955}
956
Dan Gohman98ca4f22009-08-05 01:29:28 +0000957/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000958/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
959/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000960SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000961ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000962 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000963 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000964 const SmallVectorImpl<ISD::OutputArg> &Outs,
965 const SmallVectorImpl<ISD::InputArg> &Ins,
966 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000967 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +0000968 // ARM target does not yet support tail call optimization.
969 isTailCall = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000970
Bob Wilson1f595bb2009-04-17 19:07:39 +0000971 // Analyze operands of the call, assigning locations to each operand.
972 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000973 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
974 *DAG.getContext());
975 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000976 CCAssignFnForNode(CallConv, /* Return*/ false,
977 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +0000978
Bob Wilson1f595bb2009-04-17 19:07:39 +0000979 // Get a count of how many bytes are to be pushed on the stack.
980 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000981
982 // Adjust the stack pointer for the new arguments...
983 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000984 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000985
Jim Grosbachf9a4b762010-02-24 01:43:03 +0000986 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000987
Bob Wilson5bafff32009-06-22 23:27:02 +0000988 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000989 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000990
Bob Wilson1f595bb2009-04-17 19:07:39 +0000991 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +0000992 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000993 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
994 i != e;
995 ++i, ++realArgIdx) {
996 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000997 SDValue Arg = Outs[realArgIdx].Val;
998 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +0000999
Bob Wilson1f595bb2009-04-17 19:07:39 +00001000 // Promote the value if needed.
1001 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001002 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001003 case CCValAssign::Full: break;
1004 case CCValAssign::SExt:
1005 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1006 break;
1007 case CCValAssign::ZExt:
1008 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1009 break;
1010 case CCValAssign::AExt:
1011 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1012 break;
1013 case CCValAssign::BCvt:
1014 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1015 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001016 }
1017
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001018 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001019 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001020 if (VA.getLocVT() == MVT::v2f64) {
1021 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1022 DAG.getConstant(0, MVT::i32));
1023 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1024 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001025
Dan Gohman98ca4f22009-08-05 01:29:28 +00001026 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001027 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1028
1029 VA = ArgLocs[++i]; // skip ahead to next loc
1030 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001031 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001032 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1033 } else {
1034 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001035
Dan Gohman98ca4f22009-08-05 01:29:28 +00001036 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1037 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001038 }
1039 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001040 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001041 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001042 }
1043 } else if (VA.isRegLoc()) {
1044 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1045 } else {
1046 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001047
Dan Gohman98ca4f22009-08-05 01:29:28 +00001048 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1049 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001050 }
Evan Chenga8e29892007-01-19 07:51:42 +00001051 }
1052
1053 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001054 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001055 &MemOpChains[0], MemOpChains.size());
1056
1057 // Build a sequence of copy-to-reg nodes chained together with token chain
1058 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001059 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +00001060 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001061 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001062 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +00001063 InFlag = Chain.getValue(1);
1064 }
1065
Bill Wendling056292f2008-09-16 21:48:12 +00001066 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1067 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1068 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001069 bool isDirect = false;
1070 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001071 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001072 MachineFunction &MF = DAG.getMachineFunction();
1073 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001074
1075 if (EnableARMLongCalls) {
1076 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1077 && "long-calls with non-static relocation model!");
1078 // Handle a global address or an external symbol. If it's not one of
1079 // those, the target's already in a register, so we don't need to do
1080 // anything extra.
1081 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001082 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001083 // Create a constant pool entry for the callee address
1084 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1085 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1086 ARMPCLabelIndex,
1087 ARMCP::CPValue, 0);
1088 // Get the address of the callee into a register
1089 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1090 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1091 Callee = DAG.getLoad(getPointerTy(), dl,
1092 DAG.getEntryNode(), CPAddr,
1093 PseudoSourceValue::getConstantPool(), 0,
1094 false, false, 0);
1095 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1096 const char *Sym = S->getSymbol();
1097
1098 // Create a constant pool entry for the callee address
1099 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1100 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1101 Sym, ARMPCLabelIndex, 0);
1102 // Get the address of the callee into a register
1103 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1104 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1105 Callee = DAG.getLoad(getPointerTy(), dl,
1106 DAG.getEntryNode(), CPAddr,
1107 PseudoSourceValue::getConstantPool(), 0,
1108 false, false, 0);
1109 }
1110 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001111 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001112 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001113 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001114 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001115 getTargetMachine().getRelocationModel() != Reloc::Static;
1116 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001117 // ARM call to a local ARM function is predicable.
1118 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +00001119 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001120 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001121 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001122 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001123 ARMPCLabelIndex,
1124 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001125 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001126 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001127 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001128 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001129 PseudoSourceValue::getConstantPool(), 0,
1130 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001131 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001132 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001133 getPointerTy(), Callee, PICLabel);
Jim Grosbache7b52522010-04-14 22:28:31 +00001134 } else
Evan Chengc60e76d2007-01-30 20:37:08 +00001135 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001136 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001137 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001138 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001139 getTargetMachine().getRelocationModel() != Reloc::Static;
1140 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001141 // tBX takes a register source operand.
1142 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001143 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001144 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001145 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001146 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001147 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001148 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001149 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001150 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001151 PseudoSourceValue::getConstantPool(), 0,
1152 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001153 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001154 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001155 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001156 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001157 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001158 }
1159
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001160 // FIXME: handle tail calls differently.
1161 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001162 if (Subtarget->isThumb()) {
1163 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001164 CallOpc = ARMISD::CALL_NOLINK;
1165 else
1166 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1167 } else {
1168 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001169 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1170 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001171 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001172 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001173 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001174 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001175 InFlag = Chain.getValue(1);
1176 }
1177
Dan Gohman475871a2008-07-27 21:46:04 +00001178 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001179 Ops.push_back(Chain);
1180 Ops.push_back(Callee);
1181
1182 // Add argument registers to the end of the list so that they are known live
1183 // into the call.
1184 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1185 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1186 RegsToPass[i].second.getValueType()));
1187
Gabor Greifba36cb52008-08-28 21:40:38 +00001188 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001189 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +00001190 // Returns a chain and a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00001191 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +00001192 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001193 InFlag = Chain.getValue(1);
1194
Chris Lattnere563bbc2008-10-11 22:08:30 +00001195 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1196 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001197 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001198 InFlag = Chain.getValue(1);
1199
Bob Wilson1f595bb2009-04-17 19:07:39 +00001200 // Handle result values, copying them out of physregs into vregs that we
1201 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001202 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1203 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001204}
1205
Dan Gohman98ca4f22009-08-05 01:29:28 +00001206SDValue
1207ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001208 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001209 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmand858e902010-04-17 15:26:15 +00001210 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001211
Bob Wilsondee46d72009-04-17 20:35:10 +00001212 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001213 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001214
Bob Wilsondee46d72009-04-17 20:35:10 +00001215 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001216 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1217 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001218
Dan Gohman98ca4f22009-08-05 01:29:28 +00001219 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001220 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1221 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001222
1223 // If this is the first return lowered for this function, add
1224 // the regs to the liveout set for the function.
1225 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1226 for (unsigned i = 0; i != RVLocs.size(); ++i)
1227 if (RVLocs[i].isRegLoc())
1228 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001229 }
1230
Bob Wilson1f595bb2009-04-17 19:07:39 +00001231 SDValue Flag;
1232
1233 // Copy the result values into the output registers.
1234 for (unsigned i = 0, realRVLocIdx = 0;
1235 i != RVLocs.size();
1236 ++i, ++realRVLocIdx) {
1237 CCValAssign &VA = RVLocs[i];
1238 assert(VA.isRegLoc() && "Can only return in registers!");
1239
Dan Gohman98ca4f22009-08-05 01:29:28 +00001240 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001241
1242 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001243 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001244 case CCValAssign::Full: break;
1245 case CCValAssign::BCvt:
1246 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1247 break;
1248 }
1249
Bob Wilson1f595bb2009-04-17 19:07:39 +00001250 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001251 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001252 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001253 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1254 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001255 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001256 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001257
1258 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1259 Flag = Chain.getValue(1);
1260 VA = RVLocs[++i]; // skip ahead to next loc
1261 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1262 HalfGPRs.getValue(1), Flag);
1263 Flag = Chain.getValue(1);
1264 VA = RVLocs[++i]; // skip ahead to next loc
1265
1266 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001267 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1268 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001269 }
1270 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1271 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001272 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001273 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001274 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001275 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001276 VA = RVLocs[++i]; // skip ahead to next loc
1277 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1278 Flag);
1279 } else
1280 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1281
Bob Wilsondee46d72009-04-17 20:35:10 +00001282 // Guarantee that all emitted copies are
1283 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001284 Flag = Chain.getValue(1);
1285 }
1286
1287 SDValue result;
1288 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001289 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001290 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001291 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001292
1293 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001294}
1295
Bob Wilsonb62d2572009-11-03 00:02:05 +00001296// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1297// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1298// one of the above mentioned nodes. It has to be wrapped because otherwise
1299// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1300// be used to form addressing mode. These wrapped nodes will be selected
1301// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001302static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001303 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001304 // FIXME there is no actual debug info here
1305 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001306 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001307 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001308 if (CP->isMachineConstantPoolEntry())
1309 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1310 CP->getAlignment());
1311 else
1312 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1313 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001314 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001315}
1316
Dan Gohmand858e902010-04-17 15:26:15 +00001317SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1318 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001319 MachineFunction &MF = DAG.getMachineFunction();
1320 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1321 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001322 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001323 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001324 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001325 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1326 SDValue CPAddr;
1327 if (RelocM == Reloc::Static) {
1328 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1329 } else {
1330 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001331 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001332 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1333 ARMCP::CPBlockAddress,
1334 PCAdj);
1335 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1336 }
1337 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1338 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001339 PseudoSourceValue::getConstantPool(), 0,
1340 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001341 if (RelocM == Reloc::Static)
1342 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001343 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001344 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001345}
1346
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001347// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001348SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001349ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001350 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001351 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001352 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001353 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001354 MachineFunction &MF = DAG.getMachineFunction();
1355 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1356 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001357 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001358 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001359 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001360 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001361 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001362 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
David Greene1b58cab2010-02-15 16:55:24 +00001363 PseudoSourceValue::getConstantPool(), 0,
1364 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001365 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001366
Evan Chenge7e0d622009-11-06 22:24:13 +00001367 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001368 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001369
1370 // call __tls_get_addr.
1371 ArgListTy Args;
1372 ArgListEntry Entry;
1373 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001374 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001375 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001376 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001377 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001378 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1379 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001380 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001381 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001382 return CallResult.first;
1383}
1384
1385// Lower ISD::GlobalTLSAddress using the "initial exec" or
1386// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001387SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001388ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001389 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001390 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001391 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001392 SDValue Offset;
1393 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001394 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001395 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001396 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001397
Chris Lattner4fb63d02009-07-15 04:12:33 +00001398 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001399 MachineFunction &MF = DAG.getMachineFunction();
1400 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1401 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1402 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001403 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1404 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001405 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001406 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001407 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001408 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001409 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001410 PseudoSourceValue::getConstantPool(), 0,
1411 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001412 Chain = Offset.getValue(1);
1413
Evan Chenge7e0d622009-11-06 22:24:13 +00001414 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001415 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001416
Evan Cheng9eda6892009-10-31 03:39:36 +00001417 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001418 PseudoSourceValue::getConstantPool(), 0,
1419 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001420 } else {
1421 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001422 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001423 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001424 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001425 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001426 PseudoSourceValue::getConstantPool(), 0,
1427 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001428 }
1429
1430 // The address of the thread local variable is the add of the thread
1431 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001432 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001433}
1434
Dan Gohman475871a2008-07-27 21:46:04 +00001435SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001436ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001437 // TODO: implement the "local dynamic" model
1438 assert(Subtarget->isTargetELF() &&
1439 "TLS not implemented for non-ELF targets");
1440 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1441 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1442 // otherwise use the "Local Exec" TLS Model
1443 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1444 return LowerToTLSGeneralDynamicModel(GA, DAG);
1445 else
1446 return LowerToTLSExecModels(GA, DAG);
1447}
1448
Dan Gohman475871a2008-07-27 21:46:04 +00001449SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001450 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001451 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001452 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001453 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001454 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1455 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001456 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001457 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001458 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001459 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001460 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001461 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001462 CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001463 PseudoSourceValue::getConstantPool(), 0,
1464 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001465 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001466 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001467 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001468 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001469 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001470 PseudoSourceValue::getGOT(), 0,
1471 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001472 return Result;
1473 } else {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001474 // If we have T2 ops, we can materialize the address directly via movt/movw
1475 // pair. This is always cheaper.
1476 if (Subtarget->useMovt()) {
1477 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
1478 DAG.getTargetGlobalAddress(GV, PtrVT));
1479 } else {
1480 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1481 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1482 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001483 PseudoSourceValue::getConstantPool(), 0,
1484 false, false, 0);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001485 }
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001486 }
1487}
1488
Dan Gohman475871a2008-07-27 21:46:04 +00001489SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001490 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001491 MachineFunction &MF = DAG.getMachineFunction();
1492 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1493 unsigned ARMPCLabelIndex = 0;
Owen Andersone50ed302009-08-10 22:56:29 +00001494 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001495 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001496 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001497 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001498 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001499 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001500 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001501 else {
Evan Chenge7e0d622009-11-06 22:24:13 +00001502 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001503 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1504 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001505 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001506 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001507 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001508 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001509
Evan Cheng9eda6892009-10-31 03:39:36 +00001510 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001511 PseudoSourceValue::getConstantPool(), 0,
1512 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001513 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001514
1515 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001516 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001517 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001518 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001519
Evan Cheng63476a82009-09-03 07:04:02 +00001520 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Evan Cheng9eda6892009-10-31 03:39:36 +00001521 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001522 PseudoSourceValue::getGOT(), 0,
1523 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001524
1525 return Result;
1526}
1527
Dan Gohman475871a2008-07-27 21:46:04 +00001528SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001529 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001530 assert(Subtarget->isTargetELF() &&
1531 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00001532 MachineFunction &MF = DAG.getMachineFunction();
1533 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1534 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Andersone50ed302009-08-10 22:56:29 +00001535 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001536 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001537 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001538 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1539 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001540 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001541 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001542 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001543 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001544 PseudoSourceValue::getConstantPool(), 0,
1545 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001546 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001547 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001548}
1549
Jim Grosbach0e0da732009-05-12 23:59:14 +00001550SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00001551ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001552 const ARMSubtarget *Subtarget)
1553 const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001554 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001555 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001556 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001557 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001558 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001559 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001560 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1561 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001562 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001563 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00001564 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1565 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001566 EVT PtrVT = getPointerTy();
1567 DebugLoc dl = Op.getDebugLoc();
1568 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1569 SDValue CPAddr;
1570 unsigned PCAdj = (RelocM != Reloc::PIC_)
1571 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001572 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001573 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1574 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001575 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001576 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001577 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00001578 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001579 PseudoSourceValue::getConstantPool(), 0,
1580 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001581 SDValue Chain = Result.getValue(1);
1582
1583 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001584 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001585 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1586 }
1587 return Result;
1588 }
Jim Grosbachf9570122009-05-14 00:46:35 +00001589 case Intrinsic::eh_sjlj_setjmp:
Jim Grosbacha87ded22010-02-08 23:22:00 +00001590 SDValue Val = Subtarget->isThumb() ?
1591 DAG.getCopyFromReg(DAG.getEntryNode(), dl, ARM::SP, MVT::i32) :
1592 DAG.getConstant(0, MVT::i32);
1593 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1),
1594 Val);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001595 }
1596}
1597
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001598static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
1599 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00001600 DebugLoc dl = Op.getDebugLoc();
1601 SDValue Op5 = Op.getOperand(5);
1602 SDValue Res;
1603 unsigned isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue();
1604 if (isDeviceBarrier) {
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001605 if (Subtarget->hasV7Ops())
1606 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0));
1607 else
1608 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0),
1609 DAG.getConstant(0, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00001610 } else {
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001611 if (Subtarget->hasV7Ops())
1612 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
1613 else
1614 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
1615 DAG.getConstant(0, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00001616 }
1617 return Res;
1618}
1619
Dan Gohman1e93df62010-04-17 14:41:14 +00001620static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
1621 MachineFunction &MF = DAG.getMachineFunction();
1622 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
1623
Evan Chenga8e29892007-01-19 07:51:42 +00001624 // vastart just stores the address of the VarArgsFrameIndex slot into the
1625 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001626 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001627 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00001628 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001629 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
David Greene1b58cab2010-02-15 16:55:24 +00001630 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
1631 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001632}
1633
Dan Gohman475871a2008-07-27 21:46:04 +00001634SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001635ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1636 SelectionDAG &DAG) const {
Evan Cheng86198642009-08-07 00:34:42 +00001637 SDNode *Node = Op.getNode();
1638 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001639 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001640 SDValue Chain = Op.getOperand(0);
1641 SDValue Size = Op.getOperand(1);
1642 SDValue Align = Op.getOperand(2);
1643
1644 // Chain the dynamic stack allocation so that it doesn't modify the stack
1645 // pointer when other instructions are using the stack.
1646 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1647
1648 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1649 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1650 if (AlignVal > StackAlign)
1651 // Do this now since selection pass cannot introduce new target
1652 // independent node.
1653 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1654
1655 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1656 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1657 // do even more horrible hack later.
1658 MachineFunction &MF = DAG.getMachineFunction();
1659 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1660 if (AFI->isThumb1OnlyFunction()) {
1661 bool Negate = true;
1662 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1663 if (C) {
1664 uint32_t Val = C->getZExtValue();
1665 if (Val <= 508 && ((Val & 3) == 0))
1666 Negate = false;
1667 }
1668 if (Negate)
1669 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1670 }
1671
Owen Anderson825b72b2009-08-11 20:47:22 +00001672 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001673 SDValue Ops1[] = { Chain, Size, Align };
1674 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1675 Chain = Res.getValue(1);
1676 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1677 DAG.getIntPtrConstant(0, true), SDValue());
1678 SDValue Ops2[] = { Res, Chain };
1679 return DAG.getMergeValues(Ops2, 2, dl);
1680}
1681
1682SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001683ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1684 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001685 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001686 MachineFunction &MF = DAG.getMachineFunction();
1687 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1688
1689 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001690 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001691 RC = ARM::tGPRRegisterClass;
1692 else
1693 RC = ARM::GPRRegisterClass;
1694
1695 // Transform the arguments stored in physical registers into virtual ones.
1696 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001697 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001698
1699 SDValue ArgValue2;
1700 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001701 MachineFrameInfo *MFI = MF.getFrameInfo();
Bob Wilson6a234f02010-04-13 22:03:22 +00001702 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true, false);
Bob Wilson5bafff32009-06-22 23:27:02 +00001703
1704 // Create load node to retrieve arguments from the stack.
1705 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001706 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00001707 PseudoSourceValue::getFixedStack(FI), 0,
1708 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001709 } else {
1710 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001711 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001712 }
1713
Jim Grosbache5165492009-11-09 00:11:35 +00001714 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001715}
1716
1717SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001718ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001719 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001720 const SmallVectorImpl<ISD::InputArg>
1721 &Ins,
1722 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001723 SmallVectorImpl<SDValue> &InVals)
1724 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001725
Bob Wilson1f595bb2009-04-17 19:07:39 +00001726 MachineFunction &MF = DAG.getMachineFunction();
1727 MachineFrameInfo *MFI = MF.getFrameInfo();
1728
Bob Wilson1f595bb2009-04-17 19:07:39 +00001729 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1730
1731 // Assign locations to all of the incoming arguments.
1732 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001733 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1734 *DAG.getContext());
1735 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001736 CCAssignFnForNode(CallConv, /* Return*/ false,
1737 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001738
1739 SmallVector<SDValue, 16> ArgValues;
1740
1741 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1742 CCValAssign &VA = ArgLocs[i];
1743
Bob Wilsondee46d72009-04-17 20:35:10 +00001744 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001745 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001746 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00001747
Bob Wilson5bafff32009-06-22 23:27:02 +00001748 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001749 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001750 // f64 and vector types are split up into multiple registers or
1751 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00001752 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001753 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001754 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00001755 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00001756 SDValue ArgValue2;
1757 if (VA.isMemLoc()) {
1758 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(),
1759 true, false);
1760 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1761 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
1762 PseudoSourceValue::getFixedStack(FI), 0,
1763 false, false, 0);
1764 } else {
1765 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
1766 Chain, DAG, dl);
1767 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001768 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1769 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001770 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00001771 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001772 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1773 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001774 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001775
Bob Wilson5bafff32009-06-22 23:27:02 +00001776 } else {
1777 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001778
Owen Anderson825b72b2009-08-11 20:47:22 +00001779 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00001780 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001781 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00001782 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001783 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001784 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001785 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001786 RC = (AFI->isThumb1OnlyFunction() ?
1787 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00001788 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001789 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00001790
1791 // Transform the arguments in physical registers into virtual ones.
1792 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001793 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001794 }
1795
1796 // If this is an 8 or 16-bit value, it is really passed promoted
1797 // to 32 bits. Insert an assert[sz]ext to capture this, then
1798 // truncate to the right size.
1799 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001800 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001801 case CCValAssign::Full: break;
1802 case CCValAssign::BCvt:
1803 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1804 break;
1805 case CCValAssign::SExt:
1806 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1807 DAG.getValueType(VA.getValVT()));
1808 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1809 break;
1810 case CCValAssign::ZExt:
1811 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1812 DAG.getValueType(VA.getValVT()));
1813 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1814 break;
1815 }
1816
Dan Gohman98ca4f22009-08-05 01:29:28 +00001817 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001818
1819 } else { // VA.isRegLoc()
1820
1821 // sanity check
1822 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00001823 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001824
1825 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001826 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
1827 true, false);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001828
Bob Wilsondee46d72009-04-17 20:35:10 +00001829 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001830 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001831 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00001832 PseudoSourceValue::getFixedStack(FI), 0,
1833 false, false, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001834 }
1835 }
1836
1837 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001838 if (isVarArg) {
1839 static const unsigned GPRArgRegs[] = {
1840 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1841 };
1842
Bob Wilsondee46d72009-04-17 20:35:10 +00001843 unsigned NumGPRs = CCInfo.getFirstUnallocated
1844 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001845
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001846 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1847 unsigned VARegSize = (4 - NumGPRs) * 4;
1848 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00001849 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001850 if (VARegSaveSize) {
1851 // If this function is vararg, store any remaining integer argument regs
1852 // to their spots on the stack so that they may be loaded by deferencing
1853 // the result of va_next.
1854 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00001855 AFI->setVarArgsFrameIndex(
1856 MFI->CreateFixedObject(VARegSaveSize,
1857 ArgOffset + VARegSaveSize - VARegSize,
1858 true, false));
1859 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
1860 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001861
Dan Gohman475871a2008-07-27 21:46:04 +00001862 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001863 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001864 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001865 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001866 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001867 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001868 RC = ARM::GPRRegisterClass;
1869
Bob Wilson998e1252009-04-20 18:36:57 +00001870 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001871 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00001872 SDValue Store =
1873 DAG.getStore(Val.getValue(1), dl, Val, FIN,
1874 PseudoSourceValue::getFixedStack(AFI->getVarArgsFrameIndex()), 0,
1875 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001876 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001877 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001878 DAG.getConstant(4, getPointerTy()));
1879 }
1880 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001881 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001882 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001883 } else
1884 // This will point to the next argument passed via stack.
Dan Gohman1e93df62010-04-17 14:41:14 +00001885 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset,
1886 true, false));
Evan Chenga8e29892007-01-19 07:51:42 +00001887 }
1888
Dan Gohman98ca4f22009-08-05 01:29:28 +00001889 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00001890}
1891
1892/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001893static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001894 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001895 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001896 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001897 // Maybe this has already been legalized into the constant pool?
1898 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001899 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001900 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00001901 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001902 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001903 }
1904 }
1905 return false;
1906}
1907
Evan Chenga8e29892007-01-19 07:51:42 +00001908/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1909/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00001910SDValue
1911ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Dan Gohmand858e902010-04-17 15:26:15 +00001912 SDValue &ARMCC, SelectionDAG &DAG,
1913 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00001914 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001915 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00001916 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001917 // Constant does not fit, try adjusting it by one?
1918 switch (CC) {
1919 default: break;
1920 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001921 case ISD::SETGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00001922 if (isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001923 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001924 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001925 }
1926 break;
1927 case ISD::SETULT:
1928 case ISD::SETUGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00001929 if (C > 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001930 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001931 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001932 }
1933 break;
1934 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001935 case ISD::SETGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00001936 if (isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001937 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001938 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001939 }
1940 break;
1941 case ISD::SETULE:
1942 case ISD::SETUGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00001943 if (C < 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001944 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001945 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001946 }
1947 break;
1948 }
1949 }
1950 }
1951
1952 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001953 ARMISD::NodeType CompareType;
1954 switch (CondCode) {
1955 default:
1956 CompareType = ARMISD::CMP;
1957 break;
1958 case ARMCC::EQ:
1959 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00001960 // Uses only Z Flag
1961 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001962 break;
1963 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001964 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1965 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001966}
1967
1968/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001969static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001970 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001971 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001972 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00001973 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001974 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001975 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1976 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001977}
1978
Dan Gohmand858e902010-04-17 15:26:15 +00001979SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001980 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001981 SDValue LHS = Op.getOperand(0);
1982 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001983 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001984 SDValue TrueVal = Op.getOperand(2);
1985 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001986 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001987
Owen Anderson825b72b2009-08-11 20:47:22 +00001988 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001989 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001990 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00001991 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Dale Johannesende064702009-02-06 21:50:26 +00001992 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001993 }
1994
1995 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001996 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00001997
Owen Anderson825b72b2009-08-11 20:47:22 +00001998 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1999 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002000 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
2001 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00002002 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002003 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002004 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002005 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00002006 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002007 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00002008 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002009 }
2010 return Result;
2011}
2012
Dan Gohmand858e902010-04-17 15:26:15 +00002013SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002014 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002015 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002016 SDValue LHS = Op.getOperand(2);
2017 SDValue RHS = Op.getOperand(3);
2018 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002019 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002020
Owen Anderson825b72b2009-08-11 20:47:22 +00002021 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00002022 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002023 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00002024 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002025 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00002026 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002027 }
2028
Owen Anderson825b72b2009-08-11 20:47:22 +00002029 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00002030 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002031 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002032
Dale Johannesende064702009-02-06 21:50:26 +00002033 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002034 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
2035 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2036 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002037 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002038 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002039 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002040 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00002041 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002042 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002043 }
2044 return Res;
2045}
2046
Dan Gohmand858e902010-04-17 15:26:15 +00002047SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002048 SDValue Chain = Op.getOperand(0);
2049 SDValue Table = Op.getOperand(1);
2050 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002051 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002052
Owen Andersone50ed302009-08-10 22:56:29 +00002053 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002054 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2055 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002056 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002057 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002058 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002059 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2060 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002061 if (Subtarget->isThumb2()) {
2062 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2063 // which does another jump to the destination. This also makes it easier
2064 // to translate it to TBB / TBH later.
2065 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002066 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002067 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002068 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002069 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002070 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002071 PseudoSourceValue::getJumpTable(), 0,
2072 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002073 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002074 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002075 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002076 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002077 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002078 PseudoSourceValue::getJumpTable(), 0, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002079 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002080 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002081 }
Evan Chenga8e29892007-01-19 07:51:42 +00002082}
2083
Bob Wilson76a312b2010-03-19 22:51:32 +00002084static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2085 DebugLoc dl = Op.getDebugLoc();
2086 unsigned Opc;
2087
2088 switch (Op.getOpcode()) {
2089 default:
2090 assert(0 && "Invalid opcode!");
2091 case ISD::FP_TO_SINT:
2092 Opc = ARMISD::FTOSI;
2093 break;
2094 case ISD::FP_TO_UINT:
2095 Opc = ARMISD::FTOUI;
2096 break;
2097 }
2098 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
2099 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
2100}
2101
2102static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2103 EVT VT = Op.getValueType();
2104 DebugLoc dl = Op.getDebugLoc();
2105 unsigned Opc;
2106
2107 switch (Op.getOpcode()) {
2108 default:
2109 assert(0 && "Invalid opcode!");
2110 case ISD::SINT_TO_FP:
2111 Opc = ARMISD::SITOF;
2112 break;
2113 case ISD::UINT_TO_FP:
2114 Opc = ARMISD::UITOF;
2115 break;
2116 }
2117
2118 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
2119 return DAG.getNode(Opc, dl, VT, Op);
2120}
2121
Dan Gohman475871a2008-07-27 21:46:04 +00002122static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002123 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002124 SDValue Tmp0 = Op.getOperand(0);
2125 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002126 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002127 EVT VT = Op.getValueType();
2128 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00002129 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
2130 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002131 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
2132 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002133 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002134}
2135
Dan Gohmand858e902010-04-17 15:26:15 +00002136SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002137 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2138 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00002139 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002140 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2141 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002142 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002143 ? ARM::R7 : ARM::R11;
2144 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2145 while (Depth--)
David Greene1b58cab2010-02-15 16:55:24 +00002146 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
2147 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002148 return FrameAddr;
2149}
2150
Bob Wilson9f3f0612010-04-17 05:30:19 +00002151/// ExpandBIT_CONVERT - If the target supports VFP, this function is called to
2152/// expand a bit convert where either the source or destination type is i64 to
2153/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2154/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2155/// vectors), since the legalizer won't know what to do with that.
Duncan Sands1607f052008-12-01 11:39:25 +00002156static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002157 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2158 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002159 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002160
Bob Wilson9f3f0612010-04-17 05:30:19 +00002161 // This function is only supposed to be called for i64 types, either as the
2162 // source or destination of the bit convert.
2163 EVT SrcVT = Op.getValueType();
2164 EVT DstVT = N->getValueType(0);
2165 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
2166 "ExpandBIT_CONVERT called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002167
Bob Wilson9f3f0612010-04-17 05:30:19 +00002168 // Turn i64->f64 into VMOVDRR.
2169 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002170 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2171 DAG.getConstant(0, MVT::i32));
2172 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2173 DAG.getConstant(1, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00002174 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002175 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002176
Jim Grosbache5165492009-11-09 00:11:35 +00002177 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002178 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2179 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2180 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2181 // Merge the pieces into a single i64 value.
2182 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2183 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002184
Bob Wilson9f3f0612010-04-17 05:30:19 +00002185 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002186}
2187
Bob Wilson5bafff32009-06-22 23:27:02 +00002188/// getZeroVector - Returns a vector of specified type with all zero elements.
2189///
Owen Andersone50ed302009-08-10 22:56:29 +00002190static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002191 assert(VT.isVector() && "Expected a vector type");
2192
2193 // Zero vectors are used to represent vector negation and in those cases
2194 // will be implemented with the NEON VNEG instruction. However, VNEG does
2195 // not support i64 elements, so sometimes the zero vectors will need to be
2196 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002197 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilson5bafff32009-06-22 23:27:02 +00002198 // to their dest type. This ensures they get CSE'd.
2199 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002200 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2201 SmallVector<SDValue, 8> Ops;
2202 MVT TVT;
2203
2204 if (VT.getSizeInBits() == 64) {
2205 Ops.assign(8, Cst); TVT = MVT::v8i8;
2206 } else {
2207 Ops.assign(16, Cst); TVT = MVT::v16i8;
2208 }
2209 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002210
2211 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2212}
2213
2214/// getOnesVector - Returns a vector of specified type with all bits set.
2215///
Owen Andersone50ed302009-08-10 22:56:29 +00002216static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002217 assert(VT.isVector() && "Expected a vector type");
2218
Bob Wilson929ffa22009-10-30 20:13:25 +00002219 // Always build ones vectors as <16 x i8> or <8 x i8> bitcasted to their
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002220 // dest type. This ensures they get CSE'd.
Bob Wilson5bafff32009-06-22 23:27:02 +00002221 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002222 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2223 SmallVector<SDValue, 8> Ops;
2224 MVT TVT;
2225
2226 if (VT.getSizeInBits() == 64) {
2227 Ops.assign(8, Cst); TVT = MVT::v8i8;
2228 } else {
2229 Ops.assign(16, Cst); TVT = MVT::v16i8;
2230 }
2231 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002232
2233 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2234}
2235
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002236/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2237/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002238SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2239 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002240 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2241 EVT VT = Op.getValueType();
2242 unsigned VTBits = VT.getSizeInBits();
2243 DebugLoc dl = Op.getDebugLoc();
2244 SDValue ShOpLo = Op.getOperand(0);
2245 SDValue ShOpHi = Op.getOperand(1);
2246 SDValue ShAmt = Op.getOperand(2);
2247 SDValue ARMCC;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002248 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002249
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002250 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2251
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002252 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2253 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2254 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2255 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2256 DAG.getConstant(VTBits, MVT::i32));
2257 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2258 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002259 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002260
2261 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2262 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002263 ARMCC, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002264 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002265 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC,
2266 CCR, Cmp);
2267
2268 SDValue Ops[2] = { Lo, Hi };
2269 return DAG.getMergeValues(Ops, 2, dl);
2270}
2271
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002272/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2273/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002274SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2275 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002276 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2277 EVT VT = Op.getValueType();
2278 unsigned VTBits = VT.getSizeInBits();
2279 DebugLoc dl = Op.getDebugLoc();
2280 SDValue ShOpLo = Op.getOperand(0);
2281 SDValue ShOpHi = Op.getOperand(1);
2282 SDValue ShAmt = Op.getOperand(2);
2283 SDValue ARMCC;
2284
2285 assert(Op.getOpcode() == ISD::SHL_PARTS);
2286 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2287 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2288 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2289 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2290 DAG.getConstant(VTBits, MVT::i32));
2291 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2292 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2293
2294 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2295 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2296 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002297 ARMCC, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002298 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
2299 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMCC,
2300 CCR, Cmp);
2301
2302 SDValue Ops[2] = { Lo, Hi };
2303 return DAG.getMergeValues(Ops, 2, dl);
2304}
2305
Jim Grosbach3482c802010-01-18 19:58:49 +00002306static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
2307 const ARMSubtarget *ST) {
2308 EVT VT = N->getValueType(0);
2309 DebugLoc dl = N->getDebugLoc();
2310
2311 if (!ST->hasV6T2Ops())
2312 return SDValue();
2313
2314 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
2315 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
2316}
2317
Bob Wilson5bafff32009-06-22 23:27:02 +00002318static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2319 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002320 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002321 DebugLoc dl = N->getDebugLoc();
2322
2323 // Lower vector shifts on NEON to use VSHL.
2324 if (VT.isVector()) {
2325 assert(ST->hasNEON() && "unexpected vector shift");
2326
2327 // Left shifts translate directly to the vshiftu intrinsic.
2328 if (N->getOpcode() == ISD::SHL)
2329 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002330 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002331 N->getOperand(0), N->getOperand(1));
2332
2333 assert((N->getOpcode() == ISD::SRA ||
2334 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2335
2336 // NEON uses the same intrinsics for both left and right shifts. For
2337 // right shifts, the shift amounts are negative, so negate the vector of
2338 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002339 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002340 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2341 getZeroVector(ShiftVT, DAG, dl),
2342 N->getOperand(1));
2343 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2344 Intrinsic::arm_neon_vshifts :
2345 Intrinsic::arm_neon_vshiftu);
2346 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002347 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002348 N->getOperand(0), NegatedCount);
2349 }
2350
Eli Friedmance392eb2009-08-22 03:13:10 +00002351 // We can get here for a node like i32 = ISD::SHL i32, i64
2352 if (VT != MVT::i64)
2353 return SDValue();
2354
2355 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002356 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002357
Chris Lattner27a6c732007-11-24 07:07:01 +00002358 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2359 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002360 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002361 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002362
Chris Lattner27a6c732007-11-24 07:07:01 +00002363 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002364 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002365
Chris Lattner27a6c732007-11-24 07:07:01 +00002366 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002367 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2368 DAG.getConstant(0, MVT::i32));
2369 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2370 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002371
Chris Lattner27a6c732007-11-24 07:07:01 +00002372 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2373 // captures the result into a carry flag.
2374 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002375 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002376
Chris Lattner27a6c732007-11-24 07:07:01 +00002377 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002378 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002379
Chris Lattner27a6c732007-11-24 07:07:01 +00002380 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002381 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002382}
2383
Bob Wilson5bafff32009-06-22 23:27:02 +00002384static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2385 SDValue TmpOp0, TmpOp1;
2386 bool Invert = false;
2387 bool Swap = false;
2388 unsigned Opc = 0;
2389
2390 SDValue Op0 = Op.getOperand(0);
2391 SDValue Op1 = Op.getOperand(1);
2392 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002393 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002394 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2395 DebugLoc dl = Op.getDebugLoc();
2396
2397 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2398 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002399 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002400 case ISD::SETUNE:
2401 case ISD::SETNE: Invert = true; // Fallthrough
2402 case ISD::SETOEQ:
2403 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2404 case ISD::SETOLT:
2405 case ISD::SETLT: Swap = true; // Fallthrough
2406 case ISD::SETOGT:
2407 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2408 case ISD::SETOLE:
2409 case ISD::SETLE: Swap = true; // Fallthrough
2410 case ISD::SETOGE:
2411 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2412 case ISD::SETUGE: Swap = true; // Fallthrough
2413 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2414 case ISD::SETUGT: Swap = true; // Fallthrough
2415 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2416 case ISD::SETUEQ: Invert = true; // Fallthrough
2417 case ISD::SETONE:
2418 // Expand this to (OLT | OGT).
2419 TmpOp0 = Op0;
2420 TmpOp1 = Op1;
2421 Opc = ISD::OR;
2422 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2423 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2424 break;
2425 case ISD::SETUO: Invert = true; // Fallthrough
2426 case ISD::SETO:
2427 // Expand this to (OLT | OGE).
2428 TmpOp0 = Op0;
2429 TmpOp1 = Op1;
2430 Opc = ISD::OR;
2431 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2432 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2433 break;
2434 }
2435 } else {
2436 // Integer comparisons.
2437 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002438 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002439 case ISD::SETNE: Invert = true;
2440 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2441 case ISD::SETLT: Swap = true;
2442 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2443 case ISD::SETLE: Swap = true;
2444 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2445 case ISD::SETULT: Swap = true;
2446 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2447 case ISD::SETULE: Swap = true;
2448 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2449 }
2450
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002451 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002452 if (Opc == ARMISD::VCEQ) {
2453
2454 SDValue AndOp;
2455 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2456 AndOp = Op0;
2457 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2458 AndOp = Op1;
2459
2460 // Ignore bitconvert.
2461 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2462 AndOp = AndOp.getOperand(0);
2463
2464 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2465 Opc = ARMISD::VTST;
2466 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2467 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2468 Invert = !Invert;
2469 }
2470 }
2471 }
2472
2473 if (Swap)
2474 std::swap(Op0, Op1);
2475
2476 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2477
2478 if (Invert)
2479 Result = DAG.getNOT(dl, Result, VT);
2480
2481 return Result;
2482}
2483
2484/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2485/// VMOV instruction, and if so, return the constant being splatted.
2486static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2487 unsigned SplatBitSize, SelectionDAG &DAG) {
2488 switch (SplatBitSize) {
2489 case 8:
2490 // Any 1-byte value is OK.
2491 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson825b72b2009-08-11 20:47:22 +00002492 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilson5bafff32009-06-22 23:27:02 +00002493
2494 case 16:
2495 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2496 if ((SplatBits & ~0xff) == 0 ||
2497 (SplatBits & ~0xff00) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002498 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilson5bafff32009-06-22 23:27:02 +00002499 break;
2500
2501 case 32:
2502 // NEON's 32-bit VMOV supports splat values where:
2503 // * only one byte is nonzero, or
2504 // * the least significant byte is 0xff and the second byte is nonzero, or
2505 // * the least significant 2 bytes are 0xff and the third is nonzero.
2506 if ((SplatBits & ~0xff) == 0 ||
2507 (SplatBits & ~0xff00) == 0 ||
2508 (SplatBits & ~0xff0000) == 0 ||
2509 (SplatBits & ~0xff000000) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002510 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002511
2512 if ((SplatBits & ~0xffff) == 0 &&
2513 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002514 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002515
2516 if ((SplatBits & ~0xffffff) == 0 &&
2517 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002518 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002519
2520 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2521 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2522 // VMOV.I32. A (very) minor optimization would be to replicate the value
2523 // and fall through here to test for a valid 64-bit splat. But, then the
2524 // caller would also need to check and handle the change in size.
2525 break;
2526
2527 case 64: {
2528 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2529 uint64_t BitMask = 0xff;
2530 uint64_t Val = 0;
2531 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2532 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2533 Val |= BitMask;
2534 else if ((SplatBits & BitMask) != 0)
2535 return SDValue();
2536 BitMask <<= 8;
2537 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002538 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilson5bafff32009-06-22 23:27:02 +00002539 }
2540
2541 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002542 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilson5bafff32009-06-22 23:27:02 +00002543 break;
2544 }
2545
2546 return SDValue();
2547}
2548
2549/// getVMOVImm - If this is a build_vector of constants which can be
2550/// formed by using a VMOV instruction of the specified element size,
2551/// return the constant being splatted. The ByteSize field indicates the
2552/// number of bytes of each element [1248].
2553SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2554 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2555 APInt SplatBits, SplatUndef;
2556 unsigned SplatBitSize;
2557 bool HasAnyUndefs;
2558 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2559 HasAnyUndefs, ByteSize * 8))
2560 return SDValue();
2561
2562 if (SplatBitSize > ByteSize * 8)
2563 return SDValue();
2564
2565 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2566 SplatBitSize, DAG);
2567}
2568
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002569static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2570 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002571 unsigned NumElts = VT.getVectorNumElements();
2572 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002573 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002574
2575 // If this is a VEXT shuffle, the immediate value is the index of the first
2576 // element. The other shuffle indices must be the successive elements after
2577 // the first one.
2578 unsigned ExpectedElt = Imm;
2579 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002580 // Increment the expected index. If it wraps around, it may still be
2581 // a VEXT but the source vectors must be swapped.
2582 ExpectedElt += 1;
2583 if (ExpectedElt == NumElts * 2) {
2584 ExpectedElt = 0;
2585 ReverseVEXT = true;
2586 }
2587
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002588 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002589 return false;
2590 }
2591
2592 // Adjust the index value if the source operands will be swapped.
2593 if (ReverseVEXT)
2594 Imm -= NumElts;
2595
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002596 return true;
2597}
2598
Bob Wilson8bb9e482009-07-26 00:39:34 +00002599/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2600/// instruction with the specified blocksize. (The order of the elements
2601/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002602static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2603 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002604 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2605 "Only possible block sizes for VREV are: 16, 32, 64");
2606
Bob Wilson8bb9e482009-07-26 00:39:34 +00002607 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00002608 if (EltSz == 64)
2609 return false;
2610
2611 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002612 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002613
2614 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2615 return false;
2616
2617 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002618 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002619 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2620 return false;
2621 }
2622
2623 return true;
2624}
2625
Bob Wilsonc692cb72009-08-21 20:54:19 +00002626static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2627 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002628 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2629 if (EltSz == 64)
2630 return false;
2631
Bob Wilsonc692cb72009-08-21 20:54:19 +00002632 unsigned NumElts = VT.getVectorNumElements();
2633 WhichResult = (M[0] == 0 ? 0 : 1);
2634 for (unsigned i = 0; i < NumElts; i += 2) {
2635 if ((unsigned) M[i] != i + WhichResult ||
2636 (unsigned) M[i+1] != i + NumElts + WhichResult)
2637 return false;
2638 }
2639 return true;
2640}
2641
Bob Wilson324f4f12009-12-03 06:40:55 +00002642/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
2643/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2644/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
2645static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2646 unsigned &WhichResult) {
2647 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2648 if (EltSz == 64)
2649 return false;
2650
2651 unsigned NumElts = VT.getVectorNumElements();
2652 WhichResult = (M[0] == 0 ? 0 : 1);
2653 for (unsigned i = 0; i < NumElts; i += 2) {
2654 if ((unsigned) M[i] != i + WhichResult ||
2655 (unsigned) M[i+1] != i + WhichResult)
2656 return false;
2657 }
2658 return true;
2659}
2660
Bob Wilsonc692cb72009-08-21 20:54:19 +00002661static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2662 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002663 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2664 if (EltSz == 64)
2665 return false;
2666
Bob Wilsonc692cb72009-08-21 20:54:19 +00002667 unsigned NumElts = VT.getVectorNumElements();
2668 WhichResult = (M[0] == 0 ? 0 : 1);
2669 for (unsigned i = 0; i != NumElts; ++i) {
2670 if ((unsigned) M[i] != 2 * i + WhichResult)
2671 return false;
2672 }
2673
2674 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002675 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002676 return false;
2677
2678 return true;
2679}
2680
Bob Wilson324f4f12009-12-03 06:40:55 +00002681/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
2682/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2683/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
2684static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2685 unsigned &WhichResult) {
2686 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2687 if (EltSz == 64)
2688 return false;
2689
2690 unsigned Half = VT.getVectorNumElements() / 2;
2691 WhichResult = (M[0] == 0 ? 0 : 1);
2692 for (unsigned j = 0; j != 2; ++j) {
2693 unsigned Idx = WhichResult;
2694 for (unsigned i = 0; i != Half; ++i) {
2695 if ((unsigned) M[i + j * Half] != Idx)
2696 return false;
2697 Idx += 2;
2698 }
2699 }
2700
2701 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2702 if (VT.is64BitVector() && EltSz == 32)
2703 return false;
2704
2705 return true;
2706}
2707
Bob Wilsonc692cb72009-08-21 20:54:19 +00002708static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2709 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002710 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2711 if (EltSz == 64)
2712 return false;
2713
Bob Wilsonc692cb72009-08-21 20:54:19 +00002714 unsigned NumElts = VT.getVectorNumElements();
2715 WhichResult = (M[0] == 0 ? 0 : 1);
2716 unsigned Idx = WhichResult * NumElts / 2;
2717 for (unsigned i = 0; i != NumElts; i += 2) {
2718 if ((unsigned) M[i] != Idx ||
2719 (unsigned) M[i+1] != Idx + NumElts)
2720 return false;
2721 Idx += 1;
2722 }
2723
2724 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002725 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002726 return false;
2727
2728 return true;
2729}
2730
Bob Wilson324f4f12009-12-03 06:40:55 +00002731/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
2732/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2733/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
2734static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2735 unsigned &WhichResult) {
2736 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2737 if (EltSz == 64)
2738 return false;
2739
2740 unsigned NumElts = VT.getVectorNumElements();
2741 WhichResult = (M[0] == 0 ? 0 : 1);
2742 unsigned Idx = WhichResult * NumElts / 2;
2743 for (unsigned i = 0; i != NumElts; i += 2) {
2744 if ((unsigned) M[i] != Idx ||
2745 (unsigned) M[i+1] != Idx)
2746 return false;
2747 Idx += 1;
2748 }
2749
2750 // VZIP.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
2757
Owen Andersone50ed302009-08-10 22:56:29 +00002758static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002759 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00002760 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002761 if (ConstVal->isNullValue())
2762 return getZeroVector(VT, DAG, dl);
2763 if (ConstVal->isAllOnesValue())
2764 return getOnesVector(VT, DAG, dl);
2765
Owen Andersone50ed302009-08-10 22:56:29 +00002766 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00002767 if (VT.is64BitVector()) {
2768 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002769 case 8: CanonicalVT = MVT::v8i8; break;
2770 case 16: CanonicalVT = MVT::v4i16; break;
2771 case 32: CanonicalVT = MVT::v2i32; break;
2772 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002773 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002774 }
2775 } else {
2776 assert(VT.is128BitVector() && "unknown splat vector size");
2777 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002778 case 8: CanonicalVT = MVT::v16i8; break;
2779 case 16: CanonicalVT = MVT::v8i16; break;
2780 case 32: CanonicalVT = MVT::v4i32; break;
2781 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002782 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002783 }
2784 }
2785
2786 // Build a canonical splat for this value.
2787 SmallVector<SDValue, 8> Ops;
2788 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2789 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2790 Ops.size());
2791 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2792}
2793
2794// If this is a case we can't handle, return null and let the default
2795// expansion code take care of it.
2796static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00002797 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002798 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002799 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002800
2801 APInt SplatBits, SplatUndef;
2802 unsigned SplatBitSize;
2803 bool HasAnyUndefs;
2804 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00002805 if (SplatBitSize <= 64) {
2806 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2807 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2808 if (Val.getNode())
2809 return BuildSplat(Val, VT, DAG, dl);
2810 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00002811 }
2812
Bob Wilsonbe751cf2010-05-22 00:23:12 +00002813 // Scan through the operands to see if only one value is used.
2814 unsigned NumElts = VT.getVectorNumElements();
2815 bool isOnlyLowElement = true;
2816 bool usesOnlyOneValue = true;
2817 bool isConstant = true;
2818 SDValue Value;
2819 for (unsigned i = 0; i < NumElts; ++i) {
2820 SDValue V = Op.getOperand(i);
2821 if (V.getOpcode() == ISD::UNDEF)
2822 continue;
2823 if (i > 0)
2824 isOnlyLowElement = false;
2825 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
2826 isConstant = false;
2827
2828 if (!Value.getNode())
2829 Value = V;
2830 else if (V != Value)
2831 usesOnlyOneValue = false;
2832 }
2833
2834 if (!Value.getNode())
2835 return DAG.getUNDEF(VT);
2836
2837 if (isOnlyLowElement)
2838 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
2839
2840 // If all elements are constants, fall back to the default expansion, which
2841 // will generate a load from the constant pool.
2842 if (isConstant)
2843 return SDValue();
2844
2845 // Use VDUP for non-constant splats.
2846 if (usesOnlyOneValue)
2847 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
2848
2849 // Vectors with 32- or 64-bit elements can be built by directly assigning
2850 // the subregisters.
2851 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
2852 if (EltSize >= 32) {
2853 // Do the expansion with floating-point types, since that is what the VFP
2854 // registers are defined to use, and since i64 is not legal.
2855 EVT EltVT = EVT::getFloatingPointVT(EltSize);
2856 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
2857 SDValue Val = DAG.getUNDEF(VecVT);
2858 for (unsigned i = 0; i < NumElts; ++i) {
2859 SDValue Elt = Op.getOperand(i);
2860 if (Elt.getOpcode() == ISD::UNDEF)
2861 continue;
2862 Elt = DAG.getNode(ISD::BIT_CONVERT, dl, EltVT, Elt);
2863 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Val, Elt,
2864 DAG.getConstant(i, MVT::i32));
2865 }
2866 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00002867 }
2868
2869 return SDValue();
2870}
2871
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002872/// isShuffleMaskLegal - Targets can use this to indicate that they only
2873/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2874/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2875/// are assumed to be legal.
2876bool
2877ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2878 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002879 if (VT.getVectorNumElements() == 4 &&
2880 (VT.is128BitVector() || VT.is64BitVector())) {
2881 unsigned PFIndexes[4];
2882 for (unsigned i = 0; i != 4; ++i) {
2883 if (M[i] < 0)
2884 PFIndexes[i] = 8;
2885 else
2886 PFIndexes[i] = M[i];
2887 }
2888
2889 // Compute the index in the perfect shuffle table.
2890 unsigned PFTableIndex =
2891 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2892 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2893 unsigned Cost = (PFEntry >> 30);
2894
2895 if (Cost <= 4)
2896 return true;
2897 }
2898
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002899 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00002900 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002901
2902 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2903 isVREVMask(M, VT, 64) ||
2904 isVREVMask(M, VT, 32) ||
2905 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00002906 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2907 isVTRNMask(M, VT, WhichResult) ||
2908 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00002909 isVZIPMask(M, VT, WhichResult) ||
2910 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
2911 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
2912 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002913}
2914
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002915/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2916/// the specified operations to build the shuffle.
2917static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2918 SDValue RHS, SelectionDAG &DAG,
2919 DebugLoc dl) {
2920 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2921 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2922 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2923
2924 enum {
2925 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2926 OP_VREV,
2927 OP_VDUP0,
2928 OP_VDUP1,
2929 OP_VDUP2,
2930 OP_VDUP3,
2931 OP_VEXT1,
2932 OP_VEXT2,
2933 OP_VEXT3,
2934 OP_VUZPL, // VUZP, left result
2935 OP_VUZPR, // VUZP, right result
2936 OP_VZIPL, // VZIP, left result
2937 OP_VZIPR, // VZIP, right result
2938 OP_VTRNL, // VTRN, left result
2939 OP_VTRNR // VTRN, right result
2940 };
2941
2942 if (OpNum == OP_COPY) {
2943 if (LHSID == (1*9+2)*9+3) return LHS;
2944 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2945 return RHS;
2946 }
2947
2948 SDValue OpLHS, OpRHS;
2949 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2950 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2951 EVT VT = OpLHS.getValueType();
2952
2953 switch (OpNum) {
2954 default: llvm_unreachable("Unknown shuffle opcode!");
2955 case OP_VREV:
2956 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2957 case OP_VDUP0:
2958 case OP_VDUP1:
2959 case OP_VDUP2:
2960 case OP_VDUP3:
2961 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002962 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002963 case OP_VEXT1:
2964 case OP_VEXT2:
2965 case OP_VEXT3:
2966 return DAG.getNode(ARMISD::VEXT, dl, VT,
2967 OpLHS, OpRHS,
2968 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2969 case OP_VUZPL:
2970 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002971 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002972 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
2973 case OP_VZIPL:
2974 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002975 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002976 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
2977 case OP_VTRNL:
2978 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002979 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2980 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002981 }
2982}
2983
Bob Wilson5bafff32009-06-22 23:27:02 +00002984static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002985 SDValue V1 = Op.getOperand(0);
2986 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00002987 DebugLoc dl = Op.getDebugLoc();
2988 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002989 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002990 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00002991
Bob Wilson28865062009-08-13 02:13:04 +00002992 // Convert shuffles that are directly supported on NEON to target-specific
2993 // DAG nodes, instead of keeping them as shuffles and matching them again
2994 // during code selection. This is more efficient and avoids the possibility
2995 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00002996 // FIXME: floating-point vectors should be canonicalized to integer vectors
2997 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002998 SVN->getMask(ShuffleMask);
2999
3000 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson0ce37102009-08-14 05:08:32 +00003001 int Lane = SVN->getSplatIndex();
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00003002 // If this is undef splat, generate it via "just" vdup, if possible.
3003 if (Lane == -1) Lane = 0;
3004
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003005 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3006 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00003007 }
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003008 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003009 DAG.getConstant(Lane, MVT::i32));
Bob Wilson0ce37102009-08-14 05:08:32 +00003010 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003011
3012 bool ReverseVEXT;
3013 unsigned Imm;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003014 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003015 if (ReverseVEXT)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003016 std::swap(V1, V2);
3017 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003018 DAG.getConstant(Imm, MVT::i32));
3019 }
3020
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003021 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003022 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003023 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003024 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003025 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003026 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
3027
Bob Wilsonc692cb72009-08-21 20:54:19 +00003028 // Check for Neon shuffles that modify both input vectors in place.
3029 // If both results are used, i.e., if there are two shuffles with the same
3030 // source operands and with masks corresponding to both results of one of
3031 // these operations, DAG memoization will ensure that a single node is
3032 // used for both shuffles.
3033 unsigned WhichResult;
3034 if (isVTRNMask(ShuffleMask, VT, WhichResult))
3035 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3036 V1, V2).getValue(WhichResult);
3037 if (isVUZPMask(ShuffleMask, VT, WhichResult))
3038 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3039 V1, V2).getValue(WhichResult);
3040 if (isVZIPMask(ShuffleMask, VT, WhichResult))
3041 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3042 V1, V2).getValue(WhichResult);
3043
Bob Wilson324f4f12009-12-03 06:40:55 +00003044 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
3045 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3046 V1, V1).getValue(WhichResult);
3047 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3048 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3049 V1, V1).getValue(WhichResult);
3050 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3051 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3052 V1, V1).getValue(WhichResult);
3053
Bob Wilsonc692cb72009-08-21 20:54:19 +00003054 // If the shuffle is not directly supported and it has 4 elements, use
3055 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003056 unsigned NumElts = VT.getVectorNumElements();
3057 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003058 unsigned PFIndexes[4];
3059 for (unsigned i = 0; i != 4; ++i) {
3060 if (ShuffleMask[i] < 0)
3061 PFIndexes[i] = 8;
3062 else
3063 PFIndexes[i] = ShuffleMask[i];
3064 }
3065
3066 // Compute the index in the perfect shuffle table.
3067 unsigned PFTableIndex =
3068 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003069 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3070 unsigned Cost = (PFEntry >> 30);
3071
3072 if (Cost <= 4)
3073 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
3074 }
Bob Wilsond8e17572009-08-12 22:31:50 +00003075
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003076 // Implement shuffles with 32- or 64-bit elements as subreg copies.
3077 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3078 if (EltSize >= 32) {
3079 // Do the expansion with floating-point types, since that is what the VFP
3080 // registers are defined to use, and since i64 is not legal.
3081 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3082 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
3083 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V1);
3084 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V2);
3085 SDValue Val = DAG.getUNDEF(VecVT);
3086 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00003087 if (ShuffleMask[i] < 0)
3088 continue;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003089 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
3090 ShuffleMask[i] < (int)NumElts ? V1 : V2,
3091 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
3092 MVT::i32));
3093 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Val,
Bob Wilson63b88452010-05-20 18:39:53 +00003094 Elt, DAG.getConstant(i, MVT::i32));
3095 }
3096 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
3097 }
3098
Bob Wilson22cac0d2009-08-14 05:16:33 +00003099 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003100}
3101
Bob Wilson5bafff32009-06-22 23:27:02 +00003102static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003103 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003104 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003105 SDValue Vec = Op.getOperand(0);
3106 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00003107 assert(VT == MVT::i32 &&
3108 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
3109 "unexpected type for custom-lowering vector extract");
3110 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00003111}
3112
Bob Wilsona6d65862009-08-03 20:36:38 +00003113static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3114 // The only time a CONCAT_VECTORS operation can have legal types is when
3115 // two 64-bit vectors are concatenated to a 128-bit vector.
3116 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
3117 "unexpected CONCAT_VECTORS");
3118 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00003119 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00003120 SDValue Op0 = Op.getOperand(0);
3121 SDValue Op1 = Op.getOperand(1);
3122 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003123 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3124 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00003125 DAG.getIntPtrConstant(0));
3126 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003127 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3128 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00003129 DAG.getIntPtrConstant(1));
3130 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003131}
3132
Dan Gohmand858e902010-04-17 15:26:15 +00003133SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003134 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003135 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00003136 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00003137 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003138 case ISD::GlobalAddress:
3139 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
3140 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00003141 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00003142 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
3143 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003144 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00003145 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00003146 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00003147 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00003148 case ISD::SINT_TO_FP:
3149 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
3150 case ISD::FP_TO_SINT:
3151 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003152 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00003153 case ISD::RETURNADDR: break;
Jim Grosbach0e0da732009-05-12 23:59:14 +00003154 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003155 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00003156 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
3157 Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00003158 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003159 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00003160 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00003161 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00003162 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003163 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00003164 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00003165 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003166 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
3167 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3168 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003169 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00003170 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003171 }
Dan Gohman475871a2008-07-27 21:46:04 +00003172 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003173}
3174
Duncan Sands1607f052008-12-01 11:39:25 +00003175/// ReplaceNodeResults - Replace the results of node with an illegal result
3176/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00003177void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
3178 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00003179 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00003180 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00003181 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00003182 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003183 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003184 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003185 case ISD::BIT_CONVERT:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003186 Res = ExpandBIT_CONVERT(N, DAG);
3187 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00003188 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003189 case ISD::SRA:
3190 Res = LowerShift(N, DAG, Subtarget);
3191 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003192 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00003193 if (Res.getNode())
3194 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00003195}
Chris Lattner27a6c732007-11-24 07:07:01 +00003196
Evan Chenga8e29892007-01-19 07:51:42 +00003197//===----------------------------------------------------------------------===//
3198// ARM Scheduler Hooks
3199//===----------------------------------------------------------------------===//
3200
3201MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003202ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
3203 MachineBasicBlock *BB,
3204 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00003205 unsigned dest = MI->getOperand(0).getReg();
3206 unsigned ptr = MI->getOperand(1).getReg();
3207 unsigned oldval = MI->getOperand(2).getReg();
3208 unsigned newval = MI->getOperand(3).getReg();
3209 unsigned scratch = BB->getParent()->getRegInfo()
3210 .createVirtualRegister(ARM::GPRRegisterClass);
3211 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3212 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003213 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00003214
3215 unsigned ldrOpc, strOpc;
3216 switch (Size) {
3217 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003218 case 1:
3219 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
3220 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
3221 break;
3222 case 2:
3223 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3224 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3225 break;
3226 case 4:
3227 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3228 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3229 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00003230 }
3231
3232 MachineFunction *MF = BB->getParent();
3233 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3234 MachineFunction::iterator It = BB;
3235 ++It; // insert the new blocks after the current block
3236
3237 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3238 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3239 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3240 MF->insert(It, loop1MBB);
3241 MF->insert(It, loop2MBB);
3242 MF->insert(It, exitMBB);
3243 exitMBB->transferSuccessors(BB);
3244
3245 // thisMBB:
3246 // ...
3247 // fallthrough --> loop1MBB
3248 BB->addSuccessor(loop1MBB);
3249
3250 // loop1MBB:
3251 // ldrex dest, [ptr]
3252 // cmp dest, oldval
3253 // bne exitMBB
3254 BB = loop1MBB;
3255 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003256 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003257 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003258 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3259 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003260 BB->addSuccessor(loop2MBB);
3261 BB->addSuccessor(exitMBB);
3262
3263 // loop2MBB:
3264 // strex scratch, newval, [ptr]
3265 // cmp scratch, #0
3266 // bne loop1MBB
3267 BB = loop2MBB;
3268 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
3269 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003270 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003271 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003272 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3273 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003274 BB->addSuccessor(loop1MBB);
3275 BB->addSuccessor(exitMBB);
3276
3277 // exitMBB:
3278 // ...
3279 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00003280
3281 MF->DeleteMachineInstr(MI); // The instruction is gone now.
3282
Jim Grosbach5278eb82009-12-11 01:42:04 +00003283 return BB;
3284}
3285
3286MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003287ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
3288 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00003289 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
3290 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3291
3292 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003293 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003294 MachineFunction::iterator It = BB;
3295 ++It;
3296
3297 unsigned dest = MI->getOperand(0).getReg();
3298 unsigned ptr = MI->getOperand(1).getReg();
3299 unsigned incr = MI->getOperand(2).getReg();
3300 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00003301
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003302 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003303 unsigned ldrOpc, strOpc;
3304 switch (Size) {
3305 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003306 case 1:
3307 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00003308 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003309 break;
3310 case 2:
3311 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3312 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3313 break;
3314 case 4:
3315 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3316 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3317 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00003318 }
3319
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003320 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3321 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3322 MF->insert(It, loopMBB);
3323 MF->insert(It, exitMBB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003324 exitMBB->transferSuccessors(BB);
3325
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003326 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003327 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3328 unsigned scratch2 = (!BinOpcode) ? incr :
3329 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3330
3331 // thisMBB:
3332 // ...
3333 // fallthrough --> loopMBB
3334 BB->addSuccessor(loopMBB);
3335
3336 // loopMBB:
3337 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003338 // <binop> scratch2, dest, incr
3339 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00003340 // cmp scratch, #0
3341 // bne- loopMBB
3342 // fallthrough --> exitMBB
3343 BB = loopMBB;
3344 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00003345 if (BinOpcode) {
3346 // operand order needs to go the other way for NAND
3347 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
3348 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3349 addReg(incr).addReg(dest)).addReg(0);
3350 else
3351 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3352 addReg(dest).addReg(incr)).addReg(0);
3353 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00003354
3355 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
3356 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003357 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00003358 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003359 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3360 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003361
3362 BB->addSuccessor(loopMBB);
3363 BB->addSuccessor(exitMBB);
3364
3365 // exitMBB:
3366 // ...
3367 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00003368
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003369 MF->DeleteMachineInstr(MI); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00003370
Jim Grosbachc3c23542009-12-14 04:22:04 +00003371 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00003372}
3373
3374MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00003375ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003376 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003377 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00003378 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003379 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00003380 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00003381 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00003382 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00003383 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00003384
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003385 case ARM::ATOMIC_LOAD_ADD_I8:
3386 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3387 case ARM::ATOMIC_LOAD_ADD_I16:
3388 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3389 case ARM::ATOMIC_LOAD_ADD_I32:
3390 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003391
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003392 case ARM::ATOMIC_LOAD_AND_I8:
3393 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3394 case ARM::ATOMIC_LOAD_AND_I16:
3395 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3396 case ARM::ATOMIC_LOAD_AND_I32:
3397 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003398
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003399 case ARM::ATOMIC_LOAD_OR_I8:
3400 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3401 case ARM::ATOMIC_LOAD_OR_I16:
3402 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3403 case ARM::ATOMIC_LOAD_OR_I32:
3404 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003405
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003406 case ARM::ATOMIC_LOAD_XOR_I8:
3407 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3408 case ARM::ATOMIC_LOAD_XOR_I16:
3409 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3410 case ARM::ATOMIC_LOAD_XOR_I32:
3411 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003412
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003413 case ARM::ATOMIC_LOAD_NAND_I8:
3414 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3415 case ARM::ATOMIC_LOAD_NAND_I16:
3416 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3417 case ARM::ATOMIC_LOAD_NAND_I32:
3418 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003419
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003420 case ARM::ATOMIC_LOAD_SUB_I8:
3421 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3422 case ARM::ATOMIC_LOAD_SUB_I16:
3423 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3424 case ARM::ATOMIC_LOAD_SUB_I32:
3425 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003426
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003427 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
3428 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
3429 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00003430
3431 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
3432 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
3433 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003434
Evan Cheng007ea272009-08-12 05:17:19 +00003435 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00003436 // To "insert" a SELECT_CC instruction, we actually have to insert the
3437 // diamond control-flow pattern. The incoming instruction knows the
3438 // destination vreg to set, the condition code register to branch on, the
3439 // true/false values to select between, and a branch opcode to use.
3440 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003441 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00003442 ++It;
3443
3444 // thisMBB:
3445 // ...
3446 // TrueVal = ...
3447 // cmpTY ccX, r1, r2
3448 // bCC copy1MBB
3449 // fallthrough --> copy0MBB
3450 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003451 MachineFunction *F = BB->getParent();
3452 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3453 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00003454 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00003455 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003456 F->insert(It, copy0MBB);
3457 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00003458 // Update machine-CFG edges by first adding all successors of the current
3459 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00003460 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003461 E = BB->succ_end(); I != E; ++I)
Evan Chengce319102009-09-19 09:51:03 +00003462 sinkMBB->addSuccessor(*I);
Evan Chenga8e29892007-01-19 07:51:42 +00003463 // Next, remove all successors of the current block, and add the true
3464 // and fallthrough blocks as its successors.
Evan Chengce319102009-09-19 09:51:03 +00003465 while (!BB->succ_empty())
Evan Chenga8e29892007-01-19 07:51:42 +00003466 BB->removeSuccessor(BB->succ_begin());
3467 BB->addSuccessor(copy0MBB);
3468 BB->addSuccessor(sinkMBB);
3469
3470 // copy0MBB:
3471 // %FalseValue = ...
3472 // # fallthrough to sinkMBB
3473 BB = copy0MBB;
3474
3475 // Update machine-CFG edges
3476 BB->addSuccessor(sinkMBB);
3477
3478 // sinkMBB:
3479 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3480 // ...
3481 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00003482 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00003483 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
3484 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
3485
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003486 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00003487 return BB;
3488 }
Evan Cheng86198642009-08-07 00:34:42 +00003489
3490 case ARM::tANDsp:
3491 case ARM::tADDspr_:
3492 case ARM::tSUBspi_:
3493 case ARM::t2SUBrSPi_:
3494 case ARM::t2SUBrSPi12_:
3495 case ARM::t2SUBrSPs_: {
3496 MachineFunction *MF = BB->getParent();
3497 unsigned DstReg = MI->getOperand(0).getReg();
3498 unsigned SrcReg = MI->getOperand(1).getReg();
3499 bool DstIsDead = MI->getOperand(0).isDead();
3500 bool SrcIsKill = MI->getOperand(1).isKill();
3501
3502 if (SrcReg != ARM::SP) {
3503 // Copy the source to SP from virtual register.
3504 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
3505 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3506 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
3507 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
3508 .addReg(SrcReg, getKillRegState(SrcIsKill));
3509 }
3510
3511 unsigned OpOpc = 0;
3512 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
3513 switch (MI->getOpcode()) {
3514 default:
3515 llvm_unreachable("Unexpected pseudo instruction!");
3516 case ARM::tANDsp:
3517 OpOpc = ARM::tAND;
3518 NeedPred = true;
3519 break;
3520 case ARM::tADDspr_:
3521 OpOpc = ARM::tADDspr;
3522 break;
3523 case ARM::tSUBspi_:
3524 OpOpc = ARM::tSUBspi;
3525 break;
3526 case ARM::t2SUBrSPi_:
3527 OpOpc = ARM::t2SUBrSPi;
3528 NeedPred = true; NeedCC = true;
3529 break;
3530 case ARM::t2SUBrSPi12_:
3531 OpOpc = ARM::t2SUBrSPi12;
3532 NeedPred = true;
3533 break;
3534 case ARM::t2SUBrSPs_:
3535 OpOpc = ARM::t2SUBrSPs;
3536 NeedPred = true; NeedCC = true; NeedOp3 = true;
3537 break;
3538 }
3539 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
3540 if (OpOpc == ARM::tAND)
3541 AddDefaultT1CC(MIB);
3542 MIB.addReg(ARM::SP);
3543 MIB.addOperand(MI->getOperand(2));
3544 if (NeedOp3)
3545 MIB.addOperand(MI->getOperand(3));
3546 if (NeedPred)
3547 AddDefaultPred(MIB);
3548 if (NeedCC)
3549 AddDefaultCC(MIB);
3550
3551 // Copy the result from SP to virtual register.
3552 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
3553 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3554 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
3555 BuildMI(BB, dl, TII->get(CopyOpc))
3556 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
3557 .addReg(ARM::SP);
3558 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3559 return BB;
3560 }
Evan Chenga8e29892007-01-19 07:51:42 +00003561 }
3562}
3563
3564//===----------------------------------------------------------------------===//
3565// ARM Optimization Hooks
3566//===----------------------------------------------------------------------===//
3567
Chris Lattnerd1980a52009-03-12 06:52:53 +00003568static
3569SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3570 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00003571 SelectionDAG &DAG = DCI.DAG;
3572 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003573 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00003574 unsigned Opc = N->getOpcode();
3575 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3576 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3577 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3578 ISD::CondCode CC = ISD::SETCC_INVALID;
3579
3580 if (isSlctCC) {
3581 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
3582 } else {
3583 SDValue CCOp = Slct.getOperand(0);
3584 if (CCOp.getOpcode() == ISD::SETCC)
3585 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
3586 }
3587
3588 bool DoXform = false;
3589 bool InvCC = false;
3590 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
3591 "Bad input!");
3592
3593 if (LHS.getOpcode() == ISD::Constant &&
3594 cast<ConstantSDNode>(LHS)->isNullValue()) {
3595 DoXform = true;
3596 } else if (CC != ISD::SETCC_INVALID &&
3597 RHS.getOpcode() == ISD::Constant &&
3598 cast<ConstantSDNode>(RHS)->isNullValue()) {
3599 std::swap(LHS, RHS);
3600 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003601 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00003602 Op0.getOperand(0).getValueType();
3603 bool isInt = OpVT.isInteger();
3604 CC = ISD::getSetCCInverse(CC, isInt);
3605
3606 if (!TLI.isCondCodeLegal(CC, OpVT))
3607 return SDValue(); // Inverse operator isn't legal.
3608
3609 DoXform = true;
3610 InvCC = true;
3611 }
3612
3613 if (DoXform) {
3614 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3615 if (isSlctCC)
3616 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3617 Slct.getOperand(0), Slct.getOperand(1), CC);
3618 SDValue CCOp = Slct.getOperand(0);
3619 if (InvCC)
3620 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3621 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3622 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3623 CCOp, OtherOp, Result);
3624 }
3625 return SDValue();
3626}
3627
3628/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3629static SDValue PerformADDCombine(SDNode *N,
3630 TargetLowering::DAGCombinerInfo &DCI) {
3631 // added by evan in r37685 with no testcase.
3632 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003633
Chris Lattnerd1980a52009-03-12 06:52:53 +00003634 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3635 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3636 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3637 if (Result.getNode()) return Result;
3638 }
3639 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3640 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3641 if (Result.getNode()) return Result;
3642 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003643
Chris Lattnerd1980a52009-03-12 06:52:53 +00003644 return SDValue();
3645}
3646
3647/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3648static SDValue PerformSUBCombine(SDNode *N,
3649 TargetLowering::DAGCombinerInfo &DCI) {
3650 // added by evan in r37685 with no testcase.
3651 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003652
Chris Lattnerd1980a52009-03-12 06:52:53 +00003653 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3654 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3655 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3656 if (Result.getNode()) return Result;
3657 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003658
Chris Lattnerd1980a52009-03-12 06:52:53 +00003659 return SDValue();
3660}
3661
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003662static SDValue PerformMULCombine(SDNode *N,
3663 TargetLowering::DAGCombinerInfo &DCI,
3664 const ARMSubtarget *Subtarget) {
3665 SelectionDAG &DAG = DCI.DAG;
3666
3667 if (Subtarget->isThumb1Only())
3668 return SDValue();
3669
3670 if (DAG.getMachineFunction().
3671 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
3672 return SDValue();
3673
3674 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
3675 return SDValue();
3676
3677 EVT VT = N->getValueType(0);
3678 if (VT != MVT::i32)
3679 return SDValue();
3680
3681 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
3682 if (!C)
3683 return SDValue();
3684
3685 uint64_t MulAmt = C->getZExtValue();
3686 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
3687 ShiftAmt = ShiftAmt & (32 - 1);
3688 SDValue V = N->getOperand(0);
3689 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003690
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003691 SDValue Res;
3692 MulAmt >>= ShiftAmt;
3693 if (isPowerOf2_32(MulAmt - 1)) {
3694 // (mul x, 2^N + 1) => (add (shl x, N), x)
3695 Res = DAG.getNode(ISD::ADD, DL, VT,
3696 V, DAG.getNode(ISD::SHL, DL, VT,
3697 V, DAG.getConstant(Log2_32(MulAmt-1),
3698 MVT::i32)));
3699 } else if (isPowerOf2_32(MulAmt + 1)) {
3700 // (mul x, 2^N - 1) => (sub (shl x, N), x)
3701 Res = DAG.getNode(ISD::SUB, DL, VT,
3702 DAG.getNode(ISD::SHL, DL, VT,
3703 V, DAG.getConstant(Log2_32(MulAmt+1),
3704 MVT::i32)),
3705 V);
3706 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003707 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003708
3709 if (ShiftAmt != 0)
3710 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
3711 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003712
3713 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003714 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003715 return SDValue();
3716}
3717
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +00003718/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
3719/// ARMISD::VMOVRRD.
Jim Grosbache5165492009-11-09 00:11:35 +00003720static SDValue PerformVMOVRRDCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003721 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003722 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003723 SDValue InDouble = N->getOperand(0);
Jim Grosbache5165492009-11-09 00:11:35 +00003724 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003725 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003726 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003727}
3728
Bob Wilson5bafff32009-06-22 23:27:02 +00003729/// getVShiftImm - Check if this is a valid build_vector for the immediate
3730/// operand of a vector shift operation, where all the elements of the
3731/// build_vector must have the same constant integer value.
3732static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3733 // Ignore bit_converts.
3734 while (Op.getOpcode() == ISD::BIT_CONVERT)
3735 Op = Op.getOperand(0);
3736 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3737 APInt SplatBits, SplatUndef;
3738 unsigned SplatBitSize;
3739 bool HasAnyUndefs;
3740 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3741 HasAnyUndefs, ElementBits) ||
3742 SplatBitSize > ElementBits)
3743 return false;
3744 Cnt = SplatBits.getSExtValue();
3745 return true;
3746}
3747
3748/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3749/// operand of a vector shift left operation. That value must be in the range:
3750/// 0 <= Value < ElementBits for a left shift; or
3751/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003752static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003753 assert(VT.isVector() && "vector shift count is not a vector type");
3754 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3755 if (! getVShiftImm(Op, ElementBits, Cnt))
3756 return false;
3757 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3758}
3759
3760/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3761/// operand of a vector shift right operation. For a shift opcode, the value
3762/// is positive, but for an intrinsic the value count must be negative. The
3763/// absolute value must be in the range:
3764/// 1 <= |Value| <= ElementBits for a right shift; or
3765/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003766static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00003767 int64_t &Cnt) {
3768 assert(VT.isVector() && "vector shift count is not a vector type");
3769 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3770 if (! getVShiftImm(Op, ElementBits, Cnt))
3771 return false;
3772 if (isIntrinsic)
3773 Cnt = -Cnt;
3774 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3775}
3776
3777/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3778static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3779 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3780 switch (IntNo) {
3781 default:
3782 // Don't do anything for most intrinsics.
3783 break;
3784
3785 // Vector shifts: check for immediate versions and lower them.
3786 // Note: This is done during DAG combining instead of DAG legalizing because
3787 // the build_vectors for 64-bit vector element shift counts are generally
3788 // not legal, and it is hard to see their values after they get legalized to
3789 // loads from a constant pool.
3790 case Intrinsic::arm_neon_vshifts:
3791 case Intrinsic::arm_neon_vshiftu:
3792 case Intrinsic::arm_neon_vshiftls:
3793 case Intrinsic::arm_neon_vshiftlu:
3794 case Intrinsic::arm_neon_vshiftn:
3795 case Intrinsic::arm_neon_vrshifts:
3796 case Intrinsic::arm_neon_vrshiftu:
3797 case Intrinsic::arm_neon_vrshiftn:
3798 case Intrinsic::arm_neon_vqshifts:
3799 case Intrinsic::arm_neon_vqshiftu:
3800 case Intrinsic::arm_neon_vqshiftsu:
3801 case Intrinsic::arm_neon_vqshiftns:
3802 case Intrinsic::arm_neon_vqshiftnu:
3803 case Intrinsic::arm_neon_vqshiftnsu:
3804 case Intrinsic::arm_neon_vqrshiftns:
3805 case Intrinsic::arm_neon_vqrshiftnu:
3806 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00003807 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003808 int64_t Cnt;
3809 unsigned VShiftOpc = 0;
3810
3811 switch (IntNo) {
3812 case Intrinsic::arm_neon_vshifts:
3813 case Intrinsic::arm_neon_vshiftu:
3814 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3815 VShiftOpc = ARMISD::VSHL;
3816 break;
3817 }
3818 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3819 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3820 ARMISD::VSHRs : ARMISD::VSHRu);
3821 break;
3822 }
3823 return SDValue();
3824
3825 case Intrinsic::arm_neon_vshiftls:
3826 case Intrinsic::arm_neon_vshiftlu:
3827 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3828 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003829 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003830
3831 case Intrinsic::arm_neon_vrshifts:
3832 case Intrinsic::arm_neon_vrshiftu:
3833 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3834 break;
3835 return SDValue();
3836
3837 case Intrinsic::arm_neon_vqshifts:
3838 case Intrinsic::arm_neon_vqshiftu:
3839 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3840 break;
3841 return SDValue();
3842
3843 case Intrinsic::arm_neon_vqshiftsu:
3844 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3845 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003846 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003847
3848 case Intrinsic::arm_neon_vshiftn:
3849 case Intrinsic::arm_neon_vrshiftn:
3850 case Intrinsic::arm_neon_vqshiftns:
3851 case Intrinsic::arm_neon_vqshiftnu:
3852 case Intrinsic::arm_neon_vqshiftnsu:
3853 case Intrinsic::arm_neon_vqrshiftns:
3854 case Intrinsic::arm_neon_vqrshiftnu:
3855 case Intrinsic::arm_neon_vqrshiftnsu:
3856 // Narrowing shifts require an immediate right shift.
3857 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3858 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003859 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003860
3861 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003862 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003863 }
3864
3865 switch (IntNo) {
3866 case Intrinsic::arm_neon_vshifts:
3867 case Intrinsic::arm_neon_vshiftu:
3868 // Opcode already set above.
3869 break;
3870 case Intrinsic::arm_neon_vshiftls:
3871 case Intrinsic::arm_neon_vshiftlu:
3872 if (Cnt == VT.getVectorElementType().getSizeInBits())
3873 VShiftOpc = ARMISD::VSHLLi;
3874 else
3875 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3876 ARMISD::VSHLLs : ARMISD::VSHLLu);
3877 break;
3878 case Intrinsic::arm_neon_vshiftn:
3879 VShiftOpc = ARMISD::VSHRN; break;
3880 case Intrinsic::arm_neon_vrshifts:
3881 VShiftOpc = ARMISD::VRSHRs; break;
3882 case Intrinsic::arm_neon_vrshiftu:
3883 VShiftOpc = ARMISD::VRSHRu; break;
3884 case Intrinsic::arm_neon_vrshiftn:
3885 VShiftOpc = ARMISD::VRSHRN; break;
3886 case Intrinsic::arm_neon_vqshifts:
3887 VShiftOpc = ARMISD::VQSHLs; break;
3888 case Intrinsic::arm_neon_vqshiftu:
3889 VShiftOpc = ARMISD::VQSHLu; break;
3890 case Intrinsic::arm_neon_vqshiftsu:
3891 VShiftOpc = ARMISD::VQSHLsu; break;
3892 case Intrinsic::arm_neon_vqshiftns:
3893 VShiftOpc = ARMISD::VQSHRNs; break;
3894 case Intrinsic::arm_neon_vqshiftnu:
3895 VShiftOpc = ARMISD::VQSHRNu; break;
3896 case Intrinsic::arm_neon_vqshiftnsu:
3897 VShiftOpc = ARMISD::VQSHRNsu; break;
3898 case Intrinsic::arm_neon_vqrshiftns:
3899 VShiftOpc = ARMISD::VQRSHRNs; break;
3900 case Intrinsic::arm_neon_vqrshiftnu:
3901 VShiftOpc = ARMISD::VQRSHRNu; break;
3902 case Intrinsic::arm_neon_vqrshiftnsu:
3903 VShiftOpc = ARMISD::VQRSHRNsu; break;
3904 }
3905
3906 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003907 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003908 }
3909
3910 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00003911 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003912 int64_t Cnt;
3913 unsigned VShiftOpc = 0;
3914
3915 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3916 VShiftOpc = ARMISD::VSLI;
3917 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3918 VShiftOpc = ARMISD::VSRI;
3919 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003920 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003921 }
3922
3923 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3924 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00003925 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003926 }
3927
3928 case Intrinsic::arm_neon_vqrshifts:
3929 case Intrinsic::arm_neon_vqrshiftu:
3930 // No immediate versions of these to check for.
3931 break;
3932 }
3933
3934 return SDValue();
3935}
3936
3937/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3938/// lowers them. As with the vector shift intrinsics, this is done during DAG
3939/// combining instead of DAG legalizing because the build_vectors for 64-bit
3940/// vector element shift counts are generally not legal, and it is hard to see
3941/// their values after they get legalized to loads from a constant pool.
3942static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3943 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003944 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003945
3946 // Nothing to be done for scalar shifts.
3947 if (! VT.isVector())
3948 return SDValue();
3949
3950 assert(ST->hasNEON() && "unexpected vector shift");
3951 int64_t Cnt;
3952
3953 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003954 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003955
3956 case ISD::SHL:
3957 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3958 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003959 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003960 break;
3961
3962 case ISD::SRA:
3963 case ISD::SRL:
3964 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3965 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3966 ARMISD::VSHRs : ARMISD::VSHRu);
3967 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003968 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003969 }
3970 }
3971 return SDValue();
3972}
3973
3974/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3975/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3976static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3977 const ARMSubtarget *ST) {
3978 SDValue N0 = N->getOperand(0);
3979
3980 // Check for sign- and zero-extensions of vector extract operations of 8-
3981 // and 16-bit vector elements. NEON supports these directly. They are
3982 // handled during DAG combining because type legalization will promote them
3983 // to 32-bit types and it is messy to recognize the operations after that.
3984 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3985 SDValue Vec = N0.getOperand(0);
3986 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003987 EVT VT = N->getValueType(0);
3988 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003989 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3990
Owen Anderson825b72b2009-08-11 20:47:22 +00003991 if (VT == MVT::i32 &&
3992 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00003993 TLI.isTypeLegal(Vec.getValueType())) {
3994
3995 unsigned Opc = 0;
3996 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003997 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003998 case ISD::SIGN_EXTEND:
3999 Opc = ARMISD::VGETLANEs;
4000 break;
4001 case ISD::ZERO_EXTEND:
4002 case ISD::ANY_EXTEND:
4003 Opc = ARMISD::VGETLANEu;
4004 break;
4005 }
4006 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
4007 }
4008 }
4009
4010 return SDValue();
4011}
4012
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004013/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
4014/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
4015static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
4016 const ARMSubtarget *ST) {
4017 // If the target supports NEON, try to use vmax/vmin instructions for f32
4018 // selects like "x < y ? x : y". Unless the FiniteOnlyFPMath option is set,
4019 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
4020 // a NaN; only do the transformation when it matches that behavior.
4021
4022 // For now only do this when using NEON for FP operations; if using VFP, it
4023 // is not obvious that the benefit outweighs the cost of switching to the
4024 // NEON pipeline.
4025 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
4026 N->getValueType(0) != MVT::f32)
4027 return SDValue();
4028
4029 SDValue CondLHS = N->getOperand(0);
4030 SDValue CondRHS = N->getOperand(1);
4031 SDValue LHS = N->getOperand(2);
4032 SDValue RHS = N->getOperand(3);
4033 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
4034
4035 unsigned Opcode = 0;
4036 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00004037 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004038 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00004039 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004040 IsReversed = true ; // x CC y ? y : x
4041 } else {
4042 return SDValue();
4043 }
4044
Bob Wilsone742bb52010-02-24 22:15:53 +00004045 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004046 switch (CC) {
4047 default: break;
4048 case ISD::SETOLT:
4049 case ISD::SETOLE:
4050 case ISD::SETLT:
4051 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004052 case ISD::SETULT:
4053 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004054 // If LHS is NaN, an ordered comparison will be false and the result will
4055 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
4056 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4057 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
4058 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4059 break;
4060 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
4061 // will return -0, so vmin can only be used for unsafe math or if one of
4062 // the operands is known to be nonzero.
4063 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
4064 !UnsafeFPMath &&
4065 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4066 break;
4067 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004068 break;
4069
4070 case ISD::SETOGT:
4071 case ISD::SETOGE:
4072 case ISD::SETGT:
4073 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004074 case ISD::SETUGT:
4075 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004076 // If LHS is NaN, an ordered comparison will be false and the result will
4077 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
4078 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4079 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
4080 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4081 break;
4082 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
4083 // will return +0, so vmax can only be used for unsafe math or if one of
4084 // the operands is known to be nonzero.
4085 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
4086 !UnsafeFPMath &&
4087 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4088 break;
4089 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004090 break;
4091 }
4092
4093 if (!Opcode)
4094 return SDValue();
4095 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
4096}
4097
Dan Gohman475871a2008-07-27 21:46:04 +00004098SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004099 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004100 switch (N->getOpcode()) {
4101 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004102 case ISD::ADD: return PerformADDCombine(N, DCI);
4103 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004104 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbache5165492009-11-09 00:11:35 +00004105 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004106 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004107 case ISD::SHL:
4108 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004109 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004110 case ISD::SIGN_EXTEND:
4111 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004112 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
4113 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004114 }
Dan Gohman475871a2008-07-27 21:46:04 +00004115 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004116}
4117
Bill Wendlingaf566342009-08-15 21:21:19 +00004118bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
4119 if (!Subtarget->hasV6Ops())
4120 // Pre-v6 does not support unaligned mem access.
4121 return false;
Anton Korobeynikov90cfc132010-01-30 14:08:12 +00004122 else {
4123 // v6+ may or may not support unaligned mem access depending on the system
4124 // configuration.
4125 // FIXME: This is pretty conservative. Should we provide cmdline option to
4126 // control the behaviour?
Bill Wendlingaf566342009-08-15 21:21:19 +00004127 if (!Subtarget->isTargetDarwin())
4128 return false;
4129 }
4130
4131 switch (VT.getSimpleVT().SimpleTy) {
4132 default:
4133 return false;
4134 case MVT::i8:
4135 case MVT::i16:
4136 case MVT::i32:
4137 return true;
4138 // FIXME: VLD1 etc with standard alignment is legal.
4139 }
4140}
4141
Evan Chenge6c835f2009-08-14 20:09:37 +00004142static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
4143 if (V < 0)
4144 return false;
4145
4146 unsigned Scale = 1;
4147 switch (VT.getSimpleVT().SimpleTy) {
4148 default: return false;
4149 case MVT::i1:
4150 case MVT::i8:
4151 // Scale == 1;
4152 break;
4153 case MVT::i16:
4154 // Scale == 2;
4155 Scale = 2;
4156 break;
4157 case MVT::i32:
4158 // Scale == 4;
4159 Scale = 4;
4160 break;
4161 }
4162
4163 if ((V & (Scale - 1)) != 0)
4164 return false;
4165 V /= Scale;
4166 return V == (V & ((1LL << 5) - 1));
4167}
4168
4169static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
4170 const ARMSubtarget *Subtarget) {
4171 bool isNeg = false;
4172 if (V < 0) {
4173 isNeg = true;
4174 V = - V;
4175 }
4176
4177 switch (VT.getSimpleVT().SimpleTy) {
4178 default: return false;
4179 case MVT::i1:
4180 case MVT::i8:
4181 case MVT::i16:
4182 case MVT::i32:
4183 // + imm12 or - imm8
4184 if (isNeg)
4185 return V == (V & ((1LL << 8) - 1));
4186 return V == (V & ((1LL << 12) - 1));
4187 case MVT::f32:
4188 case MVT::f64:
4189 // Same as ARM mode. FIXME: NEON?
4190 if (!Subtarget->hasVFP2())
4191 return false;
4192 if ((V & 3) != 0)
4193 return false;
4194 V >>= 2;
4195 return V == (V & ((1LL << 8) - 1));
4196 }
4197}
4198
Evan Chengb01fad62007-03-12 23:30:29 +00004199/// isLegalAddressImmediate - Return true if the integer value can be used
4200/// as the offset of the target addressing mode for load / store of the
4201/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00004202static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004203 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00004204 if (V == 0)
4205 return true;
4206
Evan Cheng65011532009-03-09 19:15:00 +00004207 if (!VT.isSimple())
4208 return false;
4209
Evan Chenge6c835f2009-08-14 20:09:37 +00004210 if (Subtarget->isThumb1Only())
4211 return isLegalT1AddressImmediate(V, VT);
4212 else if (Subtarget->isThumb2())
4213 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00004214
Evan Chenge6c835f2009-08-14 20:09:37 +00004215 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00004216 if (V < 0)
4217 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00004218 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00004219 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004220 case MVT::i1:
4221 case MVT::i8:
4222 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00004223 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004224 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004225 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00004226 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004227 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004228 case MVT::f32:
4229 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00004230 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00004231 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00004232 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00004233 return false;
4234 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004235 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00004236 }
Evan Chenga8e29892007-01-19 07:51:42 +00004237}
4238
Evan Chenge6c835f2009-08-14 20:09:37 +00004239bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
4240 EVT VT) const {
4241 int Scale = AM.Scale;
4242 if (Scale < 0)
4243 return false;
4244
4245 switch (VT.getSimpleVT().SimpleTy) {
4246 default: return false;
4247 case MVT::i1:
4248 case MVT::i8:
4249 case MVT::i16:
4250 case MVT::i32:
4251 if (Scale == 1)
4252 return true;
4253 // r + r << imm
4254 Scale = Scale & ~1;
4255 return Scale == 2 || Scale == 4 || Scale == 8;
4256 case MVT::i64:
4257 // r + r
4258 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
4259 return true;
4260 return false;
4261 case MVT::isVoid:
4262 // Note, we allow "void" uses (basically, uses that aren't loads or
4263 // stores), because arm allows folding a scale into many arithmetic
4264 // operations. This should be made more precise and revisited later.
4265
4266 // Allow r << imm, but the imm has to be a multiple of two.
4267 if (Scale & 1) return false;
4268 return isPowerOf2_32(Scale);
4269 }
4270}
4271
Chris Lattner37caf8c2007-04-09 23:33:39 +00004272/// isLegalAddressingMode - Return true if the addressing mode represented
4273/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004274bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004275 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004276 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00004277 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00004278 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004279
Chris Lattner37caf8c2007-04-09 23:33:39 +00004280 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004281 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004282 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004283
Chris Lattner37caf8c2007-04-09 23:33:39 +00004284 switch (AM.Scale) {
4285 case 0: // no scale reg, must be "r+i" or "r", or "i".
4286 break;
4287 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00004288 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00004289 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004290 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00004291 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004292 // ARM doesn't support any R+R*scale+imm addr modes.
4293 if (AM.BaseOffs)
4294 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004295
Bob Wilson2c7dab12009-04-08 17:55:28 +00004296 if (!VT.isSimple())
4297 return false;
4298
Evan Chenge6c835f2009-08-14 20:09:37 +00004299 if (Subtarget->isThumb2())
4300 return isLegalT2ScaledAddressingMode(AM, VT);
4301
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004302 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00004303 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00004304 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004305 case MVT::i1:
4306 case MVT::i8:
4307 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004308 if (Scale < 0) Scale = -Scale;
4309 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004310 return true;
4311 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00004312 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00004313 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00004314 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004315 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004316 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004317 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00004318 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004319
Owen Anderson825b72b2009-08-11 20:47:22 +00004320 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004321 // Note, we allow "void" uses (basically, uses that aren't loads or
4322 // stores), because arm allows folding a scale into many arithmetic
4323 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004324
Chris Lattner37caf8c2007-04-09 23:33:39 +00004325 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00004326 if (Scale & 1) return false;
4327 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00004328 }
4329 break;
Evan Chengb01fad62007-03-12 23:30:29 +00004330 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00004331 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00004332}
4333
Evan Cheng77e47512009-11-11 19:05:52 +00004334/// isLegalICmpImmediate - Return true if the specified immediate is legal
4335/// icmp immediate, that is the target has icmp instructions which can compare
4336/// a register against the immediate without having to materialize the
4337/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00004338bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00004339 if (!Subtarget->isThumb())
4340 return ARM_AM::getSOImmVal(Imm) != -1;
4341 if (Subtarget->isThumb2())
4342 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00004343 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00004344}
4345
Owen Andersone50ed302009-08-10 22:56:29 +00004346static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004347 bool isSEXTLoad, SDValue &Base,
4348 SDValue &Offset, bool &isInc,
4349 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00004350 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4351 return false;
4352
Owen Anderson825b72b2009-08-11 20:47:22 +00004353 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00004354 // AddressingMode 3
4355 Base = Ptr->getOperand(0);
4356 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004357 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004358 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004359 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004360 isInc = false;
4361 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4362 return true;
4363 }
4364 }
4365 isInc = (Ptr->getOpcode() == ISD::ADD);
4366 Offset = Ptr->getOperand(1);
4367 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004368 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00004369 // AddressingMode 2
4370 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004371 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004372 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004373 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004374 isInc = false;
4375 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4376 Base = Ptr->getOperand(0);
4377 return true;
4378 }
4379 }
4380
4381 if (Ptr->getOpcode() == ISD::ADD) {
4382 isInc = true;
4383 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
4384 if (ShOpcVal != ARM_AM::no_shift) {
4385 Base = Ptr->getOperand(1);
4386 Offset = Ptr->getOperand(0);
4387 } else {
4388 Base = Ptr->getOperand(0);
4389 Offset = Ptr->getOperand(1);
4390 }
4391 return true;
4392 }
4393
4394 isInc = (Ptr->getOpcode() == ISD::ADD);
4395 Base = Ptr->getOperand(0);
4396 Offset = Ptr->getOperand(1);
4397 return true;
4398 }
4399
Jim Grosbache5165492009-11-09 00:11:35 +00004400 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00004401 return false;
4402}
4403
Owen Andersone50ed302009-08-10 22:56:29 +00004404static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004405 bool isSEXTLoad, SDValue &Base,
4406 SDValue &Offset, bool &isInc,
4407 SelectionDAG &DAG) {
4408 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4409 return false;
4410
4411 Base = Ptr->getOperand(0);
4412 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
4413 int RHSC = (int)RHS->getZExtValue();
4414 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
4415 assert(Ptr->getOpcode() == ISD::ADD);
4416 isInc = false;
4417 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4418 return true;
4419 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
4420 isInc = Ptr->getOpcode() == ISD::ADD;
4421 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
4422 return true;
4423 }
4424 }
4425
4426 return false;
4427}
4428
Evan Chenga8e29892007-01-19 07:51:42 +00004429/// getPreIndexedAddressParts - returns true by value, base pointer and
4430/// offset pointer and addressing mode by reference if the node's address
4431/// can be legally represented as pre-indexed load / store address.
4432bool
Dan Gohman475871a2008-07-27 21:46:04 +00004433ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
4434 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004435 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004436 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004437 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004438 return false;
4439
Owen Andersone50ed302009-08-10 22:56:29 +00004440 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004441 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004442 bool isSEXTLoad = false;
4443 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4444 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004445 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004446 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4447 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
4448 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004449 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004450 } else
4451 return false;
4452
4453 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004454 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004455 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004456 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
4457 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004458 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004459 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00004460 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00004461 if (!isLegal)
4462 return false;
4463
4464 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
4465 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004466}
4467
4468/// getPostIndexedAddressParts - returns true by value, base pointer and
4469/// offset pointer and addressing mode by reference if this node can be
4470/// combined with a load / store to form a post-indexed load / store.
4471bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00004472 SDValue &Base,
4473 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004474 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004475 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004476 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004477 return false;
4478
Owen Andersone50ed302009-08-10 22:56:29 +00004479 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004480 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004481 bool isSEXTLoad = false;
4482 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004483 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00004484 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00004485 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4486 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004487 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00004488 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00004489 } else
4490 return false;
4491
4492 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004493 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004494 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004495 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00004496 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004497 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004498 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
4499 isInc, DAG);
4500 if (!isLegal)
4501 return false;
4502
Evan Cheng28dad2a2010-05-18 21:31:17 +00004503 if (Ptr != Base) {
4504 // Swap base ptr and offset to catch more post-index load / store when
4505 // it's legal. In Thumb2 mode, offset must be an immediate.
4506 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
4507 !Subtarget->isThumb2())
4508 std::swap(Base, Offset);
4509
4510 // Post-indexed load / store update the base pointer.
4511 if (Ptr != Base)
4512 return false;
4513 }
4514
Evan Chenge88d5ce2009-07-02 07:28:31 +00004515 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
4516 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004517}
4518
Dan Gohman475871a2008-07-27 21:46:04 +00004519void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00004520 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004521 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004522 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00004523 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00004524 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004525 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004526 switch (Op.getOpcode()) {
4527 default: break;
4528 case ARMISD::CMOV: {
4529 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00004530 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004531 if (KnownZero == 0 && KnownOne == 0) return;
4532
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004533 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00004534 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
4535 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004536 KnownZero &= KnownZeroRHS;
4537 KnownOne &= KnownOneRHS;
4538 return;
4539 }
4540 }
4541}
4542
4543//===----------------------------------------------------------------------===//
4544// ARM Inline Assembly Support
4545//===----------------------------------------------------------------------===//
4546
4547/// getConstraintType - Given a constraint letter, return the type of
4548/// constraint it is for this target.
4549ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00004550ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
4551 if (Constraint.size() == 1) {
4552 switch (Constraint[0]) {
4553 default: break;
4554 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004555 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00004556 }
Evan Chenga8e29892007-01-19 07:51:42 +00004557 }
Chris Lattner4234f572007-03-25 02:14:49 +00004558 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00004559}
4560
Bob Wilson2dc4f542009-03-20 22:42:55 +00004561std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00004562ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004563 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004564 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004565 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00004566 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004567 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004568 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004569 return std::make_pair(0U, ARM::tGPRRegisterClass);
4570 else
4571 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004572 case 'r':
4573 return std::make_pair(0U, ARM::GPRRegisterClass);
4574 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004575 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004576 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00004577 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004578 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00004579 if (VT.getSizeInBits() == 128)
4580 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004581 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004582 }
4583 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00004584 if (StringRef("{cc}").equals_lower(Constraint))
4585 return std::make_pair(0U, ARM::CCRRegisterClass);
4586
Evan Chenga8e29892007-01-19 07:51:42 +00004587 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
4588}
4589
4590std::vector<unsigned> ARMTargetLowering::
4591getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004592 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004593 if (Constraint.size() != 1)
4594 return std::vector<unsigned>();
4595
4596 switch (Constraint[0]) { // GCC ARM Constraint Letters
4597 default: break;
4598 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004599 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4600 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4601 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004602 case 'r':
4603 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4604 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4605 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
4606 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004607 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004608 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004609 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
4610 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
4611 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
4612 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
4613 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
4614 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
4615 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
4616 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00004617 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004618 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
4619 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
4620 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
4621 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00004622 if (VT.getSizeInBits() == 128)
4623 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
4624 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004625 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004626 }
4627
4628 return std::vector<unsigned>();
4629}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004630
4631/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
4632/// vector. If it is invalid, don't add anything to Ops.
4633void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
4634 char Constraint,
4635 bool hasMemory,
4636 std::vector<SDValue>&Ops,
4637 SelectionDAG &DAG) const {
4638 SDValue Result(0, 0);
4639
4640 switch (Constraint) {
4641 default: break;
4642 case 'I': case 'J': case 'K': case 'L':
4643 case 'M': case 'N': case 'O':
4644 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
4645 if (!C)
4646 return;
4647
4648 int64_t CVal64 = C->getSExtValue();
4649 int CVal = (int) CVal64;
4650 // None of these constraints allow values larger than 32 bits. Check
4651 // that the value fits in an int.
4652 if (CVal != CVal64)
4653 return;
4654
4655 switch (Constraint) {
4656 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004657 if (Subtarget->isThumb1Only()) {
4658 // This must be a constant between 0 and 255, for ADD
4659 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004660 if (CVal >= 0 && CVal <= 255)
4661 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004662 } else if (Subtarget->isThumb2()) {
4663 // A constant that can be used as an immediate value in a
4664 // data-processing instruction.
4665 if (ARM_AM::getT2SOImmVal(CVal) != -1)
4666 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004667 } else {
4668 // A constant that can be used as an immediate value in a
4669 // data-processing instruction.
4670 if (ARM_AM::getSOImmVal(CVal) != -1)
4671 break;
4672 }
4673 return;
4674
4675 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004676 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004677 // This must be a constant between -255 and -1, for negated ADD
4678 // immediates. This can be used in GCC with an "n" modifier that
4679 // prints the negated value, for use with SUB instructions. It is
4680 // not useful otherwise but is implemented for compatibility.
4681 if (CVal >= -255 && CVal <= -1)
4682 break;
4683 } else {
4684 // This must be a constant between -4095 and 4095. It is not clear
4685 // what this constraint is intended for. Implemented for
4686 // compatibility with GCC.
4687 if (CVal >= -4095 && CVal <= 4095)
4688 break;
4689 }
4690 return;
4691
4692 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004693 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004694 // A 32-bit value where only one byte has a nonzero value. Exclude
4695 // zero to match GCC. This constraint is used by GCC internally for
4696 // constants that can be loaded with a move/shift combination.
4697 // It is not useful otherwise but is implemented for compatibility.
4698 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
4699 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004700 } else if (Subtarget->isThumb2()) {
4701 // A constant whose bitwise inverse can be used as an immediate
4702 // value in a data-processing instruction. This can be used in GCC
4703 // with a "B" modifier that prints the inverted value, for use with
4704 // BIC and MVN instructions. It is not useful otherwise but is
4705 // implemented for compatibility.
4706 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
4707 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004708 } else {
4709 // A constant whose bitwise inverse can be used as an immediate
4710 // value in a data-processing instruction. This can be used in GCC
4711 // with a "B" modifier that prints the inverted value, for use with
4712 // BIC and MVN instructions. It is not useful otherwise but is
4713 // implemented for compatibility.
4714 if (ARM_AM::getSOImmVal(~CVal) != -1)
4715 break;
4716 }
4717 return;
4718
4719 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004720 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004721 // This must be a constant between -7 and 7,
4722 // for 3-operand ADD/SUB immediate instructions.
4723 if (CVal >= -7 && CVal < 7)
4724 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004725 } else if (Subtarget->isThumb2()) {
4726 // A constant whose negation can be used as an immediate value in a
4727 // data-processing instruction. This can be used in GCC with an "n"
4728 // modifier that prints the negated value, for use with SUB
4729 // instructions. It is not useful otherwise but is implemented for
4730 // compatibility.
4731 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
4732 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004733 } else {
4734 // A constant whose negation can be used as an immediate value in a
4735 // data-processing instruction. This can be used in GCC with an "n"
4736 // modifier that prints the negated value, for use with SUB
4737 // instructions. It is not useful otherwise but is implemented for
4738 // compatibility.
4739 if (ARM_AM::getSOImmVal(-CVal) != -1)
4740 break;
4741 }
4742 return;
4743
4744 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004745 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004746 // This must be a multiple of 4 between 0 and 1020, for
4747 // ADD sp + immediate.
4748 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
4749 break;
4750 } else {
4751 // A power of two or a constant between 0 and 32. This is used in
4752 // GCC for the shift amount on shifted register operands, but it is
4753 // useful in general for any shift amounts.
4754 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
4755 break;
4756 }
4757 return;
4758
4759 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004760 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004761 // This must be a constant between 0 and 31, for shift amounts.
4762 if (CVal >= 0 && CVal <= 31)
4763 break;
4764 }
4765 return;
4766
4767 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004768 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004769 // This must be a multiple of 4 between -508 and 508, for
4770 // ADD/SUB sp = sp + immediate.
4771 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
4772 break;
4773 }
4774 return;
4775 }
4776 Result = DAG.getTargetConstant(CVal, Op.getValueType());
4777 break;
4778 }
4779
4780 if (Result.getNode()) {
4781 Ops.push_back(Result);
4782 return;
4783 }
4784 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
4785 Ops, DAG);
4786}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00004787
4788bool
4789ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4790 // The ARM target isn't yet aware of offsets.
4791 return false;
4792}
Evan Cheng39382422009-10-28 01:44:26 +00004793
4794int ARM::getVFPf32Imm(const APFloat &FPImm) {
4795 APInt Imm = FPImm.bitcastToAPInt();
4796 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
4797 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
4798 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
4799
4800 // We can handle 4 bits of mantissa.
4801 // mantissa = (16+UInt(e:f:g:h))/16.
4802 if (Mantissa & 0x7ffff)
4803 return -1;
4804 Mantissa >>= 19;
4805 if ((Mantissa & 0xf) != Mantissa)
4806 return -1;
4807
4808 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4809 if (Exp < -3 || Exp > 4)
4810 return -1;
4811 Exp = ((Exp+3) & 0x7) ^ 4;
4812
4813 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4814}
4815
4816int ARM::getVFPf64Imm(const APFloat &FPImm) {
4817 APInt Imm = FPImm.bitcastToAPInt();
4818 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
4819 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
4820 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
4821
4822 // We can handle 4 bits of mantissa.
4823 // mantissa = (16+UInt(e:f:g:h))/16.
4824 if (Mantissa & 0xffffffffffffLL)
4825 return -1;
4826 Mantissa >>= 48;
4827 if ((Mantissa & 0xf) != Mantissa)
4828 return -1;
4829
4830 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4831 if (Exp < -3 || Exp > 4)
4832 return -1;
4833 Exp = ((Exp+3) & 0x7) ^ 4;
4834
4835 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4836}
4837
4838/// isFPImmLegal - Returns true if the target can instruction select the
4839/// specified FP immediate natively. If false, the legalizer will
4840/// materialize the FP immediate as a load from a constant pool.
4841bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
4842 if (!Subtarget->hasVFP3())
4843 return false;
4844 if (VT == MVT::f32)
4845 return ARM::getVFPf32Imm(Imm) != -1;
4846 if (VT == MVT::f64)
4847 return ARM::getVFPf64Imm(Imm) != -1;
4848 return false;
4849}