blob: a0d88e11a04c9168203e83f48b9e729cb2920c10 [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
Evan Cheng603afbf2010-05-10 17:34:18 +0000269 // Map v4i64 to QQ registers but do not make the type legal for any
270 // operations. v4i64 is only used for REG_SEQUENCE to load / store quad
271 // D registers.
272 addRegisterClass(MVT::v4i64, ARM::QQPRRegisterClass);
273
Bob Wilson74dc72e2009-09-15 23:55:57 +0000274 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
275 // neither Neon nor VFP support any arithmetic operations on it.
276 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
277 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
278 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
279 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
280 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
281 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
282 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
283 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
284 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
285 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
286 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
287 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
288 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
289 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
290 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
291 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
292 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
293 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
294 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
295 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
296 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
297 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
298 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
299 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
300
Bob Wilson642b3292009-09-16 00:32:15 +0000301 // Neon does not support some operations on v1i64 and v2i64 types.
302 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
303 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
304 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
305 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
306
Bob Wilson5bafff32009-06-22 23:27:02 +0000307 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
308 setTargetDAGCombine(ISD::SHL);
309 setTargetDAGCombine(ISD::SRL);
310 setTargetDAGCombine(ISD::SRA);
311 setTargetDAGCombine(ISD::SIGN_EXTEND);
312 setTargetDAGCombine(ISD::ZERO_EXTEND);
313 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000314 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson5bafff32009-06-22 23:27:02 +0000315 }
316
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000317 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000318
319 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000320 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000321
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000322 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000324
Evan Chenga8e29892007-01-19 07:51:42 +0000325 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000326 if (!Subtarget->isThumb1Only()) {
327 for (unsigned im = (unsigned)ISD::PRE_INC;
328 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000329 setIndexedLoadAction(im, MVT::i1, Legal);
330 setIndexedLoadAction(im, MVT::i8, Legal);
331 setIndexedLoadAction(im, MVT::i16, Legal);
332 setIndexedLoadAction(im, MVT::i32, Legal);
333 setIndexedStoreAction(im, MVT::i1, Legal);
334 setIndexedStoreAction(im, MVT::i8, Legal);
335 setIndexedStoreAction(im, MVT::i16, Legal);
336 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000337 }
Evan Chenga8e29892007-01-19 07:51:42 +0000338 }
339
340 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000341 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000342 setOperationAction(ISD::MUL, MVT::i64, Expand);
343 setOperationAction(ISD::MULHU, MVT::i32, Expand);
344 setOperationAction(ISD::MULHS, MVT::i32, Expand);
345 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
346 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000347 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000348 setOperationAction(ISD::MUL, MVT::i64, Expand);
349 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000350 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000351 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000352 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000353 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000354 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000355 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000356 setOperationAction(ISD::SRL, MVT::i64, Custom);
357 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000358
359 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000360 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000361 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000362 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000363 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000364 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000365
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000366 // Only ARMv6 has BSWAP.
367 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000368 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000369
Evan Chenga8e29892007-01-19 07:51:42 +0000370 // These are expanded into libcalls.
Jim Grosbach29402132010-05-05 23:44:43 +0000371 if (!Subtarget->hasDivide()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000372 // v7M has a hardware divider
373 setOperationAction(ISD::SDIV, MVT::i32, Expand);
374 setOperationAction(ISD::UDIV, MVT::i32, Expand);
375 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000376 setOperationAction(ISD::SREM, MVT::i32, Expand);
377 setOperationAction(ISD::UREM, MVT::i32, Expand);
378 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
379 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000380
Owen Anderson825b72b2009-08-11 20:47:22 +0000381 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
382 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
383 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
384 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000385 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000386
Evan Chengfb3611d2010-05-11 07:26:32 +0000387 setOperationAction(ISD::TRAP, MVT::Other, Legal);
388
Evan Chenga8e29892007-01-19 07:51:42 +0000389 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000390 setOperationAction(ISD::VASTART, MVT::Other, Custom);
391 setOperationAction(ISD::VAARG, MVT::Other, Expand);
392 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
393 setOperationAction(ISD::VAEND, MVT::Other, Expand);
394 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
395 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000396 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
397 // FIXME: Shouldn't need this, since no register is used, but the legalizer
398 // doesn't yet know how to not do that for SjLj.
399 setExceptionSelectorRegister(ARM::R0);
Evan Cheng3a1588a2010-04-15 22:20:34 +0000400 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Jim Grosbach3728e962009-12-10 00:11:09 +0000401 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000402
Jim Grosbach4b77f6a2010-05-07 18:34:55 +0000403 // If the subtarget does not have extract instructions, sign_extend_inreg
404 // needs to be expanded. Extract is available in ARM mode on v6 and up,
405 // and on most Thumb2 implementations.
406 if ((!Subtarget->isThumb() && !Subtarget->hasV6Ops())
407 || (Subtarget->isThumb2() && !Subtarget->hasT2ExtractPack())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000408 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
409 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000410 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000412
David Goodwinf1daf7d2009-07-08 23:10:31 +0000413 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000414 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
415 // iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000417
418 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000420
Owen Anderson825b72b2009-08-11 20:47:22 +0000421 setOperationAction(ISD::SETCC, MVT::i32, Expand);
422 setOperationAction(ISD::SETCC, MVT::f32, Expand);
423 setOperationAction(ISD::SETCC, MVT::f64, Expand);
424 setOperationAction(ISD::SELECT, MVT::i32, Expand);
425 setOperationAction(ISD::SELECT, MVT::f32, Expand);
426 setOperationAction(ISD::SELECT, MVT::f64, Expand);
427 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
428 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
429 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000430
Owen Anderson825b72b2009-08-11 20:47:22 +0000431 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
432 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
433 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
434 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
435 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000436
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000437 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000438 setOperationAction(ISD::FSIN, MVT::f64, Expand);
439 setOperationAction(ISD::FSIN, MVT::f32, Expand);
440 setOperationAction(ISD::FCOS, MVT::f32, Expand);
441 setOperationAction(ISD::FCOS, MVT::f64, Expand);
442 setOperationAction(ISD::FREM, MVT::f64, Expand);
443 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000444 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000445 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
446 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000447 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000448 setOperationAction(ISD::FPOW, MVT::f64, Expand);
449 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000450
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000451 // Various VFP goodness
452 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000453 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
454 if (Subtarget->hasVFP2()) {
455 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
456 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
457 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
458 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
459 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000460 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000461 if (!Subtarget->hasFP16()) {
462 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
463 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000464 }
Evan Cheng110cf482008-04-01 01:50:16 +0000465 }
Evan Chenga8e29892007-01-19 07:51:42 +0000466
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000467 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000468 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000469 setTargetDAGCombine(ISD::ADD);
470 setTargetDAGCombine(ISD::SUB);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000471
Evan Chenga8e29892007-01-19 07:51:42 +0000472 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Chenga8e29892007-01-19 07:51:42 +0000473 setSchedulingPreference(SchedulingForRegPressure);
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
Bill Wendlingb4202b82009-07-01 18:50:55 +0000587/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000588unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Evan Cheng048e36f2009-10-02 06:57:25 +0000589 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 0 : 1;
Bill Wendling20c568f2009-06-30 22:38:32 +0000590}
591
Evan Chenga8e29892007-01-19 07:51:42 +0000592//===----------------------------------------------------------------------===//
593// Lowering Code
594//===----------------------------------------------------------------------===//
595
Evan Chenga8e29892007-01-19 07:51:42 +0000596/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
597static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
598 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000599 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000600 case ISD::SETNE: return ARMCC::NE;
601 case ISD::SETEQ: return ARMCC::EQ;
602 case ISD::SETGT: return ARMCC::GT;
603 case ISD::SETGE: return ARMCC::GE;
604 case ISD::SETLT: return ARMCC::LT;
605 case ISD::SETLE: return ARMCC::LE;
606 case ISD::SETUGT: return ARMCC::HI;
607 case ISD::SETUGE: return ARMCC::HS;
608 case ISD::SETULT: return ARMCC::LO;
609 case ISD::SETULE: return ARMCC::LS;
610 }
611}
612
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000613/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
614static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000615 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000616 CondCode2 = ARMCC::AL;
617 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000618 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000619 case ISD::SETEQ:
620 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
621 case ISD::SETGT:
622 case ISD::SETOGT: CondCode = ARMCC::GT; break;
623 case ISD::SETGE:
624 case ISD::SETOGE: CondCode = ARMCC::GE; break;
625 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000626 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000627 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
628 case ISD::SETO: CondCode = ARMCC::VC; break;
629 case ISD::SETUO: CondCode = ARMCC::VS; break;
630 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
631 case ISD::SETUGT: CondCode = ARMCC::HI; break;
632 case ISD::SETUGE: CondCode = ARMCC::PL; break;
633 case ISD::SETLT:
634 case ISD::SETULT: CondCode = ARMCC::LT; break;
635 case ISD::SETLE:
636 case ISD::SETULE: CondCode = ARMCC::LE; break;
637 case ISD::SETNE:
638 case ISD::SETUNE: CondCode = ARMCC::NE; break;
639 }
Evan Chenga8e29892007-01-19 07:51:42 +0000640}
641
Bob Wilson1f595bb2009-04-17 19:07:39 +0000642//===----------------------------------------------------------------------===//
643// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000644//===----------------------------------------------------------------------===//
645
646#include "ARMGenCallingConv.inc"
647
648// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000649static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000650 CCValAssign::LocInfo &LocInfo,
651 CCState &State, bool CanFail) {
652 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
653
654 // Try to get the first register.
655 if (unsigned Reg = State.AllocateReg(RegList, 4))
656 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
657 else {
658 // For the 2nd half of a v2f64, do not fail.
659 if (CanFail)
660 return false;
661
662 // Put the whole thing on the stack.
663 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
664 State.AllocateStack(8, 4),
665 LocVT, LocInfo));
666 return true;
667 }
668
669 // Try to get the second register.
670 if (unsigned Reg = State.AllocateReg(RegList, 4))
671 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
672 else
673 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
674 State.AllocateStack(4, 4),
675 LocVT, LocInfo));
676 return true;
677}
678
Owen Andersone50ed302009-08-10 22:56:29 +0000679static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000680 CCValAssign::LocInfo &LocInfo,
681 ISD::ArgFlagsTy &ArgFlags,
682 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000683 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
684 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000686 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
687 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000688 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000689}
690
691// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000692static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000693 CCValAssign::LocInfo &LocInfo,
694 CCState &State, bool CanFail) {
695 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
696 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
697
698 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
699 if (Reg == 0) {
700 // For the 2nd half of a v2f64, do not just fail.
701 if (CanFail)
702 return false;
703
704 // Put the whole thing on the stack.
705 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
706 State.AllocateStack(8, 8),
707 LocVT, LocInfo));
708 return true;
709 }
710
711 unsigned i;
712 for (i = 0; i < 2; ++i)
713 if (HiRegList[i] == Reg)
714 break;
715
716 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
717 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
718 LocVT, LocInfo));
719 return true;
720}
721
Owen Andersone50ed302009-08-10 22:56:29 +0000722static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000723 CCValAssign::LocInfo &LocInfo,
724 ISD::ArgFlagsTy &ArgFlags,
725 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000726 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
727 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000728 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000729 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
730 return false;
731 return true; // we handled it
732}
733
Owen Andersone50ed302009-08-10 22:56:29 +0000734static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000735 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000736 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
737 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
738
Bob Wilsone65586b2009-04-17 20:40:45 +0000739 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
740 if (Reg == 0)
741 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000742
Bob Wilsone65586b2009-04-17 20:40:45 +0000743 unsigned i;
744 for (i = 0; i < 2; ++i)
745 if (HiRegList[i] == Reg)
746 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000747
Bob Wilson5bafff32009-06-22 23:27:02 +0000748 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000749 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000750 LocVT, LocInfo));
751 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000752}
753
Owen Andersone50ed302009-08-10 22:56:29 +0000754static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000755 CCValAssign::LocInfo &LocInfo,
756 ISD::ArgFlagsTy &ArgFlags,
757 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000758 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
759 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000760 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000761 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000762 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000763}
764
Owen Andersone50ed302009-08-10 22:56:29 +0000765static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000766 CCValAssign::LocInfo &LocInfo,
767 ISD::ArgFlagsTy &ArgFlags,
768 CCState &State) {
769 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
770 State);
771}
772
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000773/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
774/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000775CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000776 bool Return,
777 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000778 switch (CC) {
779 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000780 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000781 case CallingConv::C:
782 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000783 // Use target triple & subtarget features to do actual dispatch.
784 if (Subtarget->isAAPCS_ABI()) {
785 if (Subtarget->hasVFP2() &&
786 FloatABIType == FloatABI::Hard && !isVarArg)
787 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
788 else
789 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
790 } else
791 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000792 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000793 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000794 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000795 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000796 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000797 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000798 }
799}
800
Dan Gohman98ca4f22009-08-05 01:29:28 +0000801/// LowerCallResult - Lower the result values of a call into the
802/// appropriate copies out of appropriate physical registers.
803SDValue
804ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000805 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000806 const SmallVectorImpl<ISD::InputArg> &Ins,
807 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000808 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000809
Bob Wilson1f595bb2009-04-17 19:07:39 +0000810 // Assign locations to each value returned by this call.
811 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000812 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000813 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000814 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000815 CCAssignFnForNode(CallConv, /* Return*/ true,
816 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000817
818 // Copy all of the result registers out of their specified physreg.
819 for (unsigned i = 0; i != RVLocs.size(); ++i) {
820 CCValAssign VA = RVLocs[i];
821
Bob Wilson80915242009-04-25 00:33:20 +0000822 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000823 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000824 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000825 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000826 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000827 Chain = Lo.getValue(1);
828 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000829 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000830 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000831 InFlag);
832 Chain = Hi.getValue(1);
833 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000834 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000835
Owen Anderson825b72b2009-08-11 20:47:22 +0000836 if (VA.getLocVT() == MVT::v2f64) {
837 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
838 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
839 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000840
841 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000842 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000843 Chain = Lo.getValue(1);
844 InFlag = Lo.getValue(2);
845 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000846 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000847 Chain = Hi.getValue(1);
848 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000849 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +0000850 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
851 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000852 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000853 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000854 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
855 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000856 Chain = Val.getValue(1);
857 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000858 }
Bob Wilson80915242009-04-25 00:33:20 +0000859
860 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000861 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000862 case CCValAssign::Full: break;
863 case CCValAssign::BCvt:
864 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
865 break;
866 }
867
Dan Gohman98ca4f22009-08-05 01:29:28 +0000868 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000869 }
870
Dan Gohman98ca4f22009-08-05 01:29:28 +0000871 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000872}
873
874/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
875/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000876/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000877/// a byval function parameter.
878/// Sometimes what we are copying is the end of a larger object, the part that
879/// does not fit in registers.
880static SDValue
881CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
882 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
883 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000884 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000885 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +0000886 /*isVolatile=*/false, /*AlwaysInline=*/false,
887 NULL, 0, NULL, 0);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000888}
889
Bob Wilsondee46d72009-04-17 20:35:10 +0000890/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000891SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000892ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
893 SDValue StackPtr, SDValue Arg,
894 DebugLoc dl, SelectionDAG &DAG,
895 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +0000896 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000897 unsigned LocMemOffset = VA.getLocMemOffset();
898 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
899 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
900 if (Flags.isByVal()) {
901 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
902 }
903 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene1b58cab2010-02-15 16:55:24 +0000904 PseudoSourceValue::getStack(), LocMemOffset,
905 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +0000906}
907
Dan Gohman98ca4f22009-08-05 01:29:28 +0000908void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000909 SDValue Chain, SDValue &Arg,
910 RegsToPassVector &RegsToPass,
911 CCValAssign &VA, CCValAssign &NextVA,
912 SDValue &StackPtr,
913 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +0000914 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +0000915
Jim Grosbache5165492009-11-09 00:11:35 +0000916 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000917 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000918 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
919
920 if (NextVA.isRegLoc())
921 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
922 else {
923 assert(NextVA.isMemLoc());
924 if (StackPtr.getNode() == 0)
925 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
926
Dan Gohman98ca4f22009-08-05 01:29:28 +0000927 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
928 dl, DAG, NextVA,
929 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000930 }
931}
932
Dan Gohman98ca4f22009-08-05 01:29:28 +0000933/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000934/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
935/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000936SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000937ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000938 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000939 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000940 const SmallVectorImpl<ISD::OutputArg> &Outs,
941 const SmallVectorImpl<ISD::InputArg> &Ins,
942 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000943 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +0000944 // ARM target does not yet support tail call optimization.
945 isTailCall = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000946
Bob Wilson1f595bb2009-04-17 19:07:39 +0000947 // Analyze operands of the call, assigning locations to each operand.
948 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000949 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
950 *DAG.getContext());
951 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000952 CCAssignFnForNode(CallConv, /* Return*/ false,
953 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +0000954
Bob Wilson1f595bb2009-04-17 19:07:39 +0000955 // Get a count of how many bytes are to be pushed on the stack.
956 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000957
958 // Adjust the stack pointer for the new arguments...
959 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000960 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000961
Jim Grosbachf9a4b762010-02-24 01:43:03 +0000962 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000963
Bob Wilson5bafff32009-06-22 23:27:02 +0000964 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000965 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000966
Bob Wilson1f595bb2009-04-17 19:07:39 +0000967 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +0000968 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000969 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
970 i != e;
971 ++i, ++realArgIdx) {
972 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000973 SDValue Arg = Outs[realArgIdx].Val;
974 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +0000975
Bob Wilson1f595bb2009-04-17 19:07:39 +0000976 // Promote the value if needed.
977 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000978 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +0000979 case CCValAssign::Full: break;
980 case CCValAssign::SExt:
981 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
982 break;
983 case CCValAssign::ZExt:
984 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
985 break;
986 case CCValAssign::AExt:
987 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
988 break;
989 case CCValAssign::BCvt:
990 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
991 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000992 }
993
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000994 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +0000995 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000996 if (VA.getLocVT() == MVT::v2f64) {
997 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
998 DAG.getConstant(0, MVT::i32));
999 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1000 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001001
Dan Gohman98ca4f22009-08-05 01:29:28 +00001002 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001003 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1004
1005 VA = ArgLocs[++i]; // skip ahead to next loc
1006 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001007 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001008 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1009 } else {
1010 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001011
Dan Gohman98ca4f22009-08-05 01:29:28 +00001012 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1013 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001014 }
1015 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001016 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001017 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001018 }
1019 } else if (VA.isRegLoc()) {
1020 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1021 } else {
1022 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001023
Dan Gohman98ca4f22009-08-05 01:29:28 +00001024 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1025 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001026 }
Evan Chenga8e29892007-01-19 07:51:42 +00001027 }
1028
1029 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001030 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001031 &MemOpChains[0], MemOpChains.size());
1032
1033 // Build a sequence of copy-to-reg nodes chained together with token chain
1034 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001035 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +00001036 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001037 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001038 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +00001039 InFlag = Chain.getValue(1);
1040 }
1041
Bill Wendling056292f2008-09-16 21:48:12 +00001042 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1043 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1044 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001045 bool isDirect = false;
1046 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001047 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001048 MachineFunction &MF = DAG.getMachineFunction();
1049 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001050
1051 if (EnableARMLongCalls) {
1052 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1053 && "long-calls with non-static relocation model!");
1054 // Handle a global address or an external symbol. If it's not one of
1055 // those, the target's already in a register, so we don't need to do
1056 // anything extra.
1057 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001058 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001059 // Create a constant pool entry for the callee address
1060 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1061 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1062 ARMPCLabelIndex,
1063 ARMCP::CPValue, 0);
1064 // Get the address of the callee into a register
1065 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1066 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1067 Callee = DAG.getLoad(getPointerTy(), dl,
1068 DAG.getEntryNode(), CPAddr,
1069 PseudoSourceValue::getConstantPool(), 0,
1070 false, false, 0);
1071 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1072 const char *Sym = S->getSymbol();
1073
1074 // Create a constant pool entry for the callee address
1075 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1076 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1077 Sym, ARMPCLabelIndex, 0);
1078 // Get the address of the callee into a register
1079 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1080 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1081 Callee = DAG.getLoad(getPointerTy(), dl,
1082 DAG.getEntryNode(), CPAddr,
1083 PseudoSourceValue::getConstantPool(), 0,
1084 false, false, 0);
1085 }
1086 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001087 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001088 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001089 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001090 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001091 getTargetMachine().getRelocationModel() != Reloc::Static;
1092 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001093 // ARM call to a local ARM function is predicable.
1094 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +00001095 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001096 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001097 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001098 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001099 ARMPCLabelIndex,
1100 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001101 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001102 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001103 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001104 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001105 PseudoSourceValue::getConstantPool(), 0,
1106 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001107 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001108 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001109 getPointerTy(), Callee, PICLabel);
Jim Grosbache7b52522010-04-14 22:28:31 +00001110 } else
Evan Chengc60e76d2007-01-30 20:37:08 +00001111 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001112 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001113 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001114 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001115 getTargetMachine().getRelocationModel() != Reloc::Static;
1116 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001117 // tBX takes a register source operand.
1118 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001119 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001120 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001121 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001122 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001123 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001124 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001125 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001126 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001127 PseudoSourceValue::getConstantPool(), 0,
1128 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001129 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001130 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001131 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001132 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001133 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001134 }
1135
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001136 // FIXME: handle tail calls differently.
1137 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001138 if (Subtarget->isThumb()) {
1139 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001140 CallOpc = ARMISD::CALL_NOLINK;
1141 else
1142 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1143 } else {
1144 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001145 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1146 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001147 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001148 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001149 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001150 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001151 InFlag = Chain.getValue(1);
1152 }
1153
Dan Gohman475871a2008-07-27 21:46:04 +00001154 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001155 Ops.push_back(Chain);
1156 Ops.push_back(Callee);
1157
1158 // Add argument registers to the end of the list so that they are known live
1159 // into the call.
1160 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1161 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1162 RegsToPass[i].second.getValueType()));
1163
Gabor Greifba36cb52008-08-28 21:40:38 +00001164 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001165 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +00001166 // Returns a chain and a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00001167 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +00001168 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001169 InFlag = Chain.getValue(1);
1170
Chris Lattnere563bbc2008-10-11 22:08:30 +00001171 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1172 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001173 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001174 InFlag = Chain.getValue(1);
1175
Bob Wilson1f595bb2009-04-17 19:07:39 +00001176 // Handle result values, copying them out of physregs into vregs that we
1177 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001178 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1179 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001180}
1181
Dan Gohman98ca4f22009-08-05 01:29:28 +00001182SDValue
1183ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001184 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001185 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmand858e902010-04-17 15:26:15 +00001186 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001187
Bob Wilsondee46d72009-04-17 20:35:10 +00001188 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001189 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001190
Bob Wilsondee46d72009-04-17 20:35:10 +00001191 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001192 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1193 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001194
Dan Gohman98ca4f22009-08-05 01:29:28 +00001195 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001196 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1197 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001198
1199 // If this is the first return lowered for this function, add
1200 // the regs to the liveout set for the function.
1201 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1202 for (unsigned i = 0; i != RVLocs.size(); ++i)
1203 if (RVLocs[i].isRegLoc())
1204 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001205 }
1206
Bob Wilson1f595bb2009-04-17 19:07:39 +00001207 SDValue Flag;
1208
1209 // Copy the result values into the output registers.
1210 for (unsigned i = 0, realRVLocIdx = 0;
1211 i != RVLocs.size();
1212 ++i, ++realRVLocIdx) {
1213 CCValAssign &VA = RVLocs[i];
1214 assert(VA.isRegLoc() && "Can only return in registers!");
1215
Dan Gohman98ca4f22009-08-05 01:29:28 +00001216 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001217
1218 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001219 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001220 case CCValAssign::Full: break;
1221 case CCValAssign::BCvt:
1222 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1223 break;
1224 }
1225
Bob Wilson1f595bb2009-04-17 19:07:39 +00001226 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001227 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001228 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001229 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1230 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001231 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001232 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001233
1234 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1235 Flag = Chain.getValue(1);
1236 VA = RVLocs[++i]; // skip ahead to next loc
1237 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1238 HalfGPRs.getValue(1), Flag);
1239 Flag = Chain.getValue(1);
1240 VA = RVLocs[++i]; // skip ahead to next loc
1241
1242 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001243 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1244 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001245 }
1246 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1247 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001248 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001249 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001250 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001251 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001252 VA = RVLocs[++i]; // skip ahead to next loc
1253 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1254 Flag);
1255 } else
1256 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1257
Bob Wilsondee46d72009-04-17 20:35:10 +00001258 // Guarantee that all emitted copies are
1259 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001260 Flag = Chain.getValue(1);
1261 }
1262
1263 SDValue result;
1264 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001265 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001266 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001267 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001268
1269 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001270}
1271
Bob Wilsonb62d2572009-11-03 00:02:05 +00001272// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1273// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1274// one of the above mentioned nodes. It has to be wrapped because otherwise
1275// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1276// be used to form addressing mode. These wrapped nodes will be selected
1277// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001278static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001279 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001280 // FIXME there is no actual debug info here
1281 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001282 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001283 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001284 if (CP->isMachineConstantPoolEntry())
1285 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1286 CP->getAlignment());
1287 else
1288 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1289 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001290 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001291}
1292
Dan Gohmand858e902010-04-17 15:26:15 +00001293SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1294 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001295 MachineFunction &MF = DAG.getMachineFunction();
1296 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1297 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001298 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001299 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001300 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001301 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1302 SDValue CPAddr;
1303 if (RelocM == Reloc::Static) {
1304 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1305 } else {
1306 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001307 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001308 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1309 ARMCP::CPBlockAddress,
1310 PCAdj);
1311 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1312 }
1313 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1314 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001315 PseudoSourceValue::getConstantPool(), 0,
1316 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001317 if (RelocM == Reloc::Static)
1318 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001319 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001320 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001321}
1322
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001323// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001324SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001325ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001326 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001327 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001328 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001329 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001330 MachineFunction &MF = DAG.getMachineFunction();
1331 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1332 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001333 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001334 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001335 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001336 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001337 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001338 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
David Greene1b58cab2010-02-15 16:55:24 +00001339 PseudoSourceValue::getConstantPool(), 0,
1340 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001341 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001342
Evan Chenge7e0d622009-11-06 22:24:13 +00001343 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001344 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001345
1346 // call __tls_get_addr.
1347 ArgListTy Args;
1348 ArgListEntry Entry;
1349 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001350 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001351 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001352 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001353 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001354 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1355 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001356 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001357 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001358 return CallResult.first;
1359}
1360
1361// Lower ISD::GlobalTLSAddress using the "initial exec" or
1362// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001363SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001364ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001365 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001366 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001367 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001368 SDValue Offset;
1369 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001370 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001371 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001372 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001373
Chris Lattner4fb63d02009-07-15 04:12:33 +00001374 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001375 MachineFunction &MF = DAG.getMachineFunction();
1376 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1377 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1378 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001379 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1380 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001381 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001382 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001383 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001384 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001385 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001386 PseudoSourceValue::getConstantPool(), 0,
1387 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001388 Chain = Offset.getValue(1);
1389
Evan Chenge7e0d622009-11-06 22:24:13 +00001390 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001391 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001392
Evan Cheng9eda6892009-10-31 03:39:36 +00001393 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001394 PseudoSourceValue::getConstantPool(), 0,
1395 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001396 } else {
1397 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001398 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001399 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001400 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001401 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001402 PseudoSourceValue::getConstantPool(), 0,
1403 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001404 }
1405
1406 // The address of the thread local variable is the add of the thread
1407 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001408 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001409}
1410
Dan Gohman475871a2008-07-27 21:46:04 +00001411SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001412ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001413 // TODO: implement the "local dynamic" model
1414 assert(Subtarget->isTargetELF() &&
1415 "TLS not implemented for non-ELF targets");
1416 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1417 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1418 // otherwise use the "Local Exec" TLS Model
1419 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1420 return LowerToTLSGeneralDynamicModel(GA, DAG);
1421 else
1422 return LowerToTLSExecModels(GA, DAG);
1423}
1424
Dan Gohman475871a2008-07-27 21:46:04 +00001425SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001426 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001427 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001428 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001429 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001430 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1431 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001432 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001433 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001434 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001435 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001436 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001437 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001438 CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001439 PseudoSourceValue::getConstantPool(), 0,
1440 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001441 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001442 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001443 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001444 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001445 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001446 PseudoSourceValue::getGOT(), 0,
1447 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001448 return Result;
1449 } else {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001450 // If we have T2 ops, we can materialize the address directly via movt/movw
1451 // pair. This is always cheaper.
1452 if (Subtarget->useMovt()) {
1453 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
1454 DAG.getTargetGlobalAddress(GV, PtrVT));
1455 } else {
1456 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1457 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1458 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001459 PseudoSourceValue::getConstantPool(), 0,
1460 false, false, 0);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001461 }
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001462 }
1463}
1464
Dan Gohman475871a2008-07-27 21:46:04 +00001465SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001466 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001467 MachineFunction &MF = DAG.getMachineFunction();
1468 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1469 unsigned ARMPCLabelIndex = 0;
Owen Andersone50ed302009-08-10 22:56:29 +00001470 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001471 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001472 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001473 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001474 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001475 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001476 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001477 else {
Evan Chenge7e0d622009-11-06 22:24:13 +00001478 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001479 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1480 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001481 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001482 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001483 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001484 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001485
Evan Cheng9eda6892009-10-31 03:39:36 +00001486 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001487 PseudoSourceValue::getConstantPool(), 0,
1488 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001489 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001490
1491 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001492 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001493 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001494 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001495
Evan Cheng63476a82009-09-03 07:04:02 +00001496 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Evan Cheng9eda6892009-10-31 03:39:36 +00001497 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001498 PseudoSourceValue::getGOT(), 0,
1499 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001500
1501 return Result;
1502}
1503
Dan Gohman475871a2008-07-27 21:46:04 +00001504SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001505 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001506 assert(Subtarget->isTargetELF() &&
1507 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00001508 MachineFunction &MF = DAG.getMachineFunction();
1509 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1510 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Andersone50ed302009-08-10 22:56:29 +00001511 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001512 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001513 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001514 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1515 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001516 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001517 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001518 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001519 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001520 PseudoSourceValue::getConstantPool(), 0,
1521 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001522 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001523 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001524}
1525
Jim Grosbach0e0da732009-05-12 23:59:14 +00001526SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00001527ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001528 const ARMSubtarget *Subtarget)
1529 const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001530 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001531 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001532 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001533 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001534 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001535 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001536 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1537 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001538 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001539 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00001540 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1541 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001542 EVT PtrVT = getPointerTy();
1543 DebugLoc dl = Op.getDebugLoc();
1544 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1545 SDValue CPAddr;
1546 unsigned PCAdj = (RelocM != Reloc::PIC_)
1547 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001548 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001549 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1550 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001551 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001552 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001553 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00001554 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001555 PseudoSourceValue::getConstantPool(), 0,
1556 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001557 SDValue Chain = Result.getValue(1);
1558
1559 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001560 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001561 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1562 }
1563 return Result;
1564 }
Jim Grosbachf9570122009-05-14 00:46:35 +00001565 case Intrinsic::eh_sjlj_setjmp:
Jim Grosbacha87ded22010-02-08 23:22:00 +00001566 SDValue Val = Subtarget->isThumb() ?
1567 DAG.getCopyFromReg(DAG.getEntryNode(), dl, ARM::SP, MVT::i32) :
1568 DAG.getConstant(0, MVT::i32);
1569 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1),
1570 Val);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001571 }
1572}
1573
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001574static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
1575 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00001576 DebugLoc dl = Op.getDebugLoc();
1577 SDValue Op5 = Op.getOperand(5);
1578 SDValue Res;
1579 unsigned isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue();
1580 if (isDeviceBarrier) {
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001581 if (Subtarget->hasV7Ops())
1582 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0));
1583 else
1584 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0),
1585 DAG.getConstant(0, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00001586 } else {
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001587 if (Subtarget->hasV7Ops())
1588 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
1589 else
1590 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
1591 DAG.getConstant(0, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00001592 }
1593 return Res;
1594}
1595
Dan Gohman1e93df62010-04-17 14:41:14 +00001596static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
1597 MachineFunction &MF = DAG.getMachineFunction();
1598 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
1599
Evan Chenga8e29892007-01-19 07:51:42 +00001600 // vastart just stores the address of the VarArgsFrameIndex slot into the
1601 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001602 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001603 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00001604 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001605 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
David Greene1b58cab2010-02-15 16:55:24 +00001606 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
1607 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001608}
1609
Dan Gohman475871a2008-07-27 21:46:04 +00001610SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001611ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1612 SelectionDAG &DAG) const {
Evan Cheng86198642009-08-07 00:34:42 +00001613 SDNode *Node = Op.getNode();
1614 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001615 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001616 SDValue Chain = Op.getOperand(0);
1617 SDValue Size = Op.getOperand(1);
1618 SDValue Align = Op.getOperand(2);
1619
1620 // Chain the dynamic stack allocation so that it doesn't modify the stack
1621 // pointer when other instructions are using the stack.
1622 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1623
1624 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1625 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1626 if (AlignVal > StackAlign)
1627 // Do this now since selection pass cannot introduce new target
1628 // independent node.
1629 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1630
1631 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1632 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1633 // do even more horrible hack later.
1634 MachineFunction &MF = DAG.getMachineFunction();
1635 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1636 if (AFI->isThumb1OnlyFunction()) {
1637 bool Negate = true;
1638 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1639 if (C) {
1640 uint32_t Val = C->getZExtValue();
1641 if (Val <= 508 && ((Val & 3) == 0))
1642 Negate = false;
1643 }
1644 if (Negate)
1645 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1646 }
1647
Owen Anderson825b72b2009-08-11 20:47:22 +00001648 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001649 SDValue Ops1[] = { Chain, Size, Align };
1650 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1651 Chain = Res.getValue(1);
1652 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1653 DAG.getIntPtrConstant(0, true), SDValue());
1654 SDValue Ops2[] = { Res, Chain };
1655 return DAG.getMergeValues(Ops2, 2, dl);
1656}
1657
1658SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001659ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1660 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001661 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001662 MachineFunction &MF = DAG.getMachineFunction();
1663 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1664
1665 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001666 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001667 RC = ARM::tGPRRegisterClass;
1668 else
1669 RC = ARM::GPRRegisterClass;
1670
1671 // Transform the arguments stored in physical registers into virtual ones.
1672 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001673 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001674
1675 SDValue ArgValue2;
1676 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001677 MachineFrameInfo *MFI = MF.getFrameInfo();
Bob Wilson6a234f02010-04-13 22:03:22 +00001678 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true, false);
Bob Wilson5bafff32009-06-22 23:27:02 +00001679
1680 // Create load node to retrieve arguments from the stack.
1681 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001682 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00001683 PseudoSourceValue::getFixedStack(FI), 0,
1684 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001685 } else {
1686 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001687 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001688 }
1689
Jim Grosbache5165492009-11-09 00:11:35 +00001690 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001691}
1692
1693SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001694ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001695 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001696 const SmallVectorImpl<ISD::InputArg>
1697 &Ins,
1698 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001699 SmallVectorImpl<SDValue> &InVals)
1700 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001701
Bob Wilson1f595bb2009-04-17 19:07:39 +00001702 MachineFunction &MF = DAG.getMachineFunction();
1703 MachineFrameInfo *MFI = MF.getFrameInfo();
1704
Bob Wilson1f595bb2009-04-17 19:07:39 +00001705 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1706
1707 // Assign locations to all of the incoming arguments.
1708 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001709 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1710 *DAG.getContext());
1711 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001712 CCAssignFnForNode(CallConv, /* Return*/ false,
1713 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001714
1715 SmallVector<SDValue, 16> ArgValues;
1716
1717 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1718 CCValAssign &VA = ArgLocs[i];
1719
Bob Wilsondee46d72009-04-17 20:35:10 +00001720 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001721 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001722 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00001723
Bob Wilson5bafff32009-06-22 23:27:02 +00001724 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001725 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001726 // f64 and vector types are split up into multiple registers or
1727 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00001728 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001729 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001730 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00001731 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00001732 SDValue ArgValue2;
1733 if (VA.isMemLoc()) {
1734 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(),
1735 true, false);
1736 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1737 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
1738 PseudoSourceValue::getFixedStack(FI), 0,
1739 false, false, 0);
1740 } else {
1741 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
1742 Chain, DAG, dl);
1743 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001744 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1745 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001746 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00001747 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001748 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1749 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001750 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001751
Bob Wilson5bafff32009-06-22 23:27:02 +00001752 } else {
1753 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001754
Owen Anderson825b72b2009-08-11 20:47:22 +00001755 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00001756 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001757 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00001758 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001759 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001760 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001761 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001762 RC = (AFI->isThumb1OnlyFunction() ?
1763 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00001764 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001765 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00001766
1767 // Transform the arguments in physical registers into virtual ones.
1768 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001769 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001770 }
1771
1772 // If this is an 8 or 16-bit value, it is really passed promoted
1773 // to 32 bits. Insert an assert[sz]ext to capture this, then
1774 // truncate to the right size.
1775 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001776 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001777 case CCValAssign::Full: break;
1778 case CCValAssign::BCvt:
1779 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1780 break;
1781 case CCValAssign::SExt:
1782 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1783 DAG.getValueType(VA.getValVT()));
1784 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1785 break;
1786 case CCValAssign::ZExt:
1787 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1788 DAG.getValueType(VA.getValVT()));
1789 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1790 break;
1791 }
1792
Dan Gohman98ca4f22009-08-05 01:29:28 +00001793 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001794
1795 } else { // VA.isRegLoc()
1796
1797 // sanity check
1798 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00001799 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001800
1801 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001802 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
1803 true, false);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001804
Bob Wilsondee46d72009-04-17 20:35:10 +00001805 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001806 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001807 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00001808 PseudoSourceValue::getFixedStack(FI), 0,
1809 false, false, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001810 }
1811 }
1812
1813 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001814 if (isVarArg) {
1815 static const unsigned GPRArgRegs[] = {
1816 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1817 };
1818
Bob Wilsondee46d72009-04-17 20:35:10 +00001819 unsigned NumGPRs = CCInfo.getFirstUnallocated
1820 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001821
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001822 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1823 unsigned VARegSize = (4 - NumGPRs) * 4;
1824 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00001825 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001826 if (VARegSaveSize) {
1827 // If this function is vararg, store any remaining integer argument regs
1828 // to their spots on the stack so that they may be loaded by deferencing
1829 // the result of va_next.
1830 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00001831 AFI->setVarArgsFrameIndex(
1832 MFI->CreateFixedObject(VARegSaveSize,
1833 ArgOffset + VARegSaveSize - VARegSize,
1834 true, false));
1835 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
1836 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001837
Dan Gohman475871a2008-07-27 21:46:04 +00001838 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001839 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001840 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001841 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001842 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001843 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001844 RC = ARM::GPRRegisterClass;
1845
Bob Wilson998e1252009-04-20 18:36:57 +00001846 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001847 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00001848 SDValue Store =
1849 DAG.getStore(Val.getValue(1), dl, Val, FIN,
1850 PseudoSourceValue::getFixedStack(AFI->getVarArgsFrameIndex()), 0,
1851 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001852 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001853 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001854 DAG.getConstant(4, getPointerTy()));
1855 }
1856 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001857 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001858 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001859 } else
1860 // This will point to the next argument passed via stack.
Dan Gohman1e93df62010-04-17 14:41:14 +00001861 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset,
1862 true, false));
Evan Chenga8e29892007-01-19 07:51:42 +00001863 }
1864
Dan Gohman98ca4f22009-08-05 01:29:28 +00001865 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00001866}
1867
1868/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001869static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001870 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001871 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001872 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001873 // Maybe this has already been legalized into the constant pool?
1874 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001875 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001876 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00001877 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001878 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001879 }
1880 }
1881 return false;
1882}
1883
Evan Chenga8e29892007-01-19 07:51:42 +00001884/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1885/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00001886SDValue
1887ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Dan Gohmand858e902010-04-17 15:26:15 +00001888 SDValue &ARMCC, SelectionDAG &DAG,
1889 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00001890 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001891 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00001892 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001893 // Constant does not fit, try adjusting it by one?
1894 switch (CC) {
1895 default: break;
1896 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001897 case ISD::SETGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00001898 if (isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001899 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001900 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001901 }
1902 break;
1903 case ISD::SETULT:
1904 case ISD::SETUGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00001905 if (C > 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001906 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001907 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001908 }
1909 break;
1910 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001911 case ISD::SETGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00001912 if (isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001913 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001914 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001915 }
1916 break;
1917 case ISD::SETULE:
1918 case ISD::SETUGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00001919 if (C < 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001920 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001921 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001922 }
1923 break;
1924 }
1925 }
1926 }
1927
1928 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001929 ARMISD::NodeType CompareType;
1930 switch (CondCode) {
1931 default:
1932 CompareType = ARMISD::CMP;
1933 break;
1934 case ARMCC::EQ:
1935 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00001936 // Uses only Z Flag
1937 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001938 break;
1939 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001940 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1941 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001942}
1943
1944/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001945static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001946 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001947 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001948 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00001949 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001950 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001951 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1952 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001953}
1954
Dan Gohmand858e902010-04-17 15:26:15 +00001955SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001956 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001957 SDValue LHS = Op.getOperand(0);
1958 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001959 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001960 SDValue TrueVal = Op.getOperand(2);
1961 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001962 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001963
Owen Anderson825b72b2009-08-11 20:47:22 +00001964 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001965 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001966 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00001967 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Dale Johannesende064702009-02-06 21:50:26 +00001968 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001969 }
1970
1971 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001972 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00001973
Owen Anderson825b72b2009-08-11 20:47:22 +00001974 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1975 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001976 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1977 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001978 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001979 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001980 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001981 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00001982 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001983 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00001984 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001985 }
1986 return Result;
1987}
1988
Dan Gohmand858e902010-04-17 15:26:15 +00001989SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00001990 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001991 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001992 SDValue LHS = Op.getOperand(2);
1993 SDValue RHS = Op.getOperand(3);
1994 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00001995 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001996
Owen Anderson825b72b2009-08-11 20:47:22 +00001997 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001998 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001999 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00002000 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002001 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00002002 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002003 }
2004
Owen Anderson825b72b2009-08-11 20:47:22 +00002005 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00002006 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002007 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002008
Dale Johannesende064702009-02-06 21:50:26 +00002009 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002010 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
2011 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2012 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002013 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002014 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002015 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002016 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00002017 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002018 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002019 }
2020 return Res;
2021}
2022
Dan Gohmand858e902010-04-17 15:26:15 +00002023SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002024 SDValue Chain = Op.getOperand(0);
2025 SDValue Table = Op.getOperand(1);
2026 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002027 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002028
Owen Andersone50ed302009-08-10 22:56:29 +00002029 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002030 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2031 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002032 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002033 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002034 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002035 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2036 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002037 if (Subtarget->isThumb2()) {
2038 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2039 // which does another jump to the destination. This also makes it easier
2040 // to translate it to TBB / TBH later.
2041 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002042 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002043 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002044 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002045 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002046 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002047 PseudoSourceValue::getJumpTable(), 0,
2048 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002049 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002050 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002051 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002052 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002053 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002054 PseudoSourceValue::getJumpTable(), 0, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002055 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002056 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002057 }
Evan Chenga8e29892007-01-19 07:51:42 +00002058}
2059
Bob Wilson76a312b2010-03-19 22:51:32 +00002060static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2061 DebugLoc dl = Op.getDebugLoc();
2062 unsigned Opc;
2063
2064 switch (Op.getOpcode()) {
2065 default:
2066 assert(0 && "Invalid opcode!");
2067 case ISD::FP_TO_SINT:
2068 Opc = ARMISD::FTOSI;
2069 break;
2070 case ISD::FP_TO_UINT:
2071 Opc = ARMISD::FTOUI;
2072 break;
2073 }
2074 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
2075 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
2076}
2077
2078static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2079 EVT VT = Op.getValueType();
2080 DebugLoc dl = Op.getDebugLoc();
2081 unsigned Opc;
2082
2083 switch (Op.getOpcode()) {
2084 default:
2085 assert(0 && "Invalid opcode!");
2086 case ISD::SINT_TO_FP:
2087 Opc = ARMISD::SITOF;
2088 break;
2089 case ISD::UINT_TO_FP:
2090 Opc = ARMISD::UITOF;
2091 break;
2092 }
2093
2094 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
2095 return DAG.getNode(Opc, dl, VT, Op);
2096}
2097
Dan Gohman475871a2008-07-27 21:46:04 +00002098static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002099 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002100 SDValue Tmp0 = Op.getOperand(0);
2101 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002102 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002103 EVT VT = Op.getValueType();
2104 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00002105 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
2106 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002107 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
2108 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002109 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002110}
2111
Dan Gohmand858e902010-04-17 15:26:15 +00002112SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002113 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2114 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00002115 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002116 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2117 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002118 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002119 ? ARM::R7 : ARM::R11;
2120 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2121 while (Depth--)
David Greene1b58cab2010-02-15 16:55:24 +00002122 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
2123 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002124 return FrameAddr;
2125}
2126
Bob Wilson9f3f0612010-04-17 05:30:19 +00002127/// ExpandBIT_CONVERT - If the target supports VFP, this function is called to
2128/// expand a bit convert where either the source or destination type is i64 to
2129/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2130/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2131/// vectors), since the legalizer won't know what to do with that.
Duncan Sands1607f052008-12-01 11:39:25 +00002132static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002133 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2134 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002135 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002136
Bob Wilson9f3f0612010-04-17 05:30:19 +00002137 // This function is only supposed to be called for i64 types, either as the
2138 // source or destination of the bit convert.
2139 EVT SrcVT = Op.getValueType();
2140 EVT DstVT = N->getValueType(0);
2141 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
2142 "ExpandBIT_CONVERT called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002143
Bob Wilson9f3f0612010-04-17 05:30:19 +00002144 // Turn i64->f64 into VMOVDRR.
2145 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002146 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2147 DAG.getConstant(0, MVT::i32));
2148 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2149 DAG.getConstant(1, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00002150 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002151 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002152
Jim Grosbache5165492009-11-09 00:11:35 +00002153 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002154 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2155 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2156 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2157 // Merge the pieces into a single i64 value.
2158 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2159 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002160
Bob Wilson9f3f0612010-04-17 05:30:19 +00002161 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002162}
2163
Bob Wilson5bafff32009-06-22 23:27:02 +00002164/// getZeroVector - Returns a vector of specified type with all zero elements.
2165///
Owen Andersone50ed302009-08-10 22:56:29 +00002166static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002167 assert(VT.isVector() && "Expected a vector type");
2168
2169 // Zero vectors are used to represent vector negation and in those cases
2170 // will be implemented with the NEON VNEG instruction. However, VNEG does
2171 // not support i64 elements, so sometimes the zero vectors will need to be
2172 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002173 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilson5bafff32009-06-22 23:27:02 +00002174 // to their dest type. This ensures they get CSE'd.
2175 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002176 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2177 SmallVector<SDValue, 8> Ops;
2178 MVT TVT;
2179
2180 if (VT.getSizeInBits() == 64) {
2181 Ops.assign(8, Cst); TVT = MVT::v8i8;
2182 } else {
2183 Ops.assign(16, Cst); TVT = MVT::v16i8;
2184 }
2185 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002186
2187 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2188}
2189
2190/// getOnesVector - Returns a vector of specified type with all bits set.
2191///
Owen Andersone50ed302009-08-10 22:56:29 +00002192static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002193 assert(VT.isVector() && "Expected a vector type");
2194
Bob Wilson929ffa22009-10-30 20:13:25 +00002195 // Always build ones vectors as <16 x i8> or <8 x i8> bitcasted to their
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002196 // dest type. This ensures they get CSE'd.
Bob Wilson5bafff32009-06-22 23:27:02 +00002197 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002198 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2199 SmallVector<SDValue, 8> Ops;
2200 MVT TVT;
2201
2202 if (VT.getSizeInBits() == 64) {
2203 Ops.assign(8, Cst); TVT = MVT::v8i8;
2204 } else {
2205 Ops.assign(16, Cst); TVT = MVT::v16i8;
2206 }
2207 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002208
2209 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2210}
2211
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002212/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2213/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002214SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2215 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002216 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2217 EVT VT = Op.getValueType();
2218 unsigned VTBits = VT.getSizeInBits();
2219 DebugLoc dl = Op.getDebugLoc();
2220 SDValue ShOpLo = Op.getOperand(0);
2221 SDValue ShOpHi = Op.getOperand(1);
2222 SDValue ShAmt = Op.getOperand(2);
2223 SDValue ARMCC;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002224 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002225
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002226 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2227
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002228 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2229 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2230 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2231 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2232 DAG.getConstant(VTBits, MVT::i32));
2233 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2234 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002235 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002236
2237 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2238 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002239 ARMCC, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002240 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002241 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC,
2242 CCR, Cmp);
2243
2244 SDValue Ops[2] = { Lo, Hi };
2245 return DAG.getMergeValues(Ops, 2, dl);
2246}
2247
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002248/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2249/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002250SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2251 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002252 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2253 EVT VT = Op.getValueType();
2254 unsigned VTBits = VT.getSizeInBits();
2255 DebugLoc dl = Op.getDebugLoc();
2256 SDValue ShOpLo = Op.getOperand(0);
2257 SDValue ShOpHi = Op.getOperand(1);
2258 SDValue ShAmt = Op.getOperand(2);
2259 SDValue ARMCC;
2260
2261 assert(Op.getOpcode() == ISD::SHL_PARTS);
2262 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2263 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2264 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2265 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2266 DAG.getConstant(VTBits, MVT::i32));
2267 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2268 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2269
2270 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2271 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2272 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002273 ARMCC, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002274 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
2275 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMCC,
2276 CCR, Cmp);
2277
2278 SDValue Ops[2] = { Lo, Hi };
2279 return DAG.getMergeValues(Ops, 2, dl);
2280}
2281
Jim Grosbach3482c802010-01-18 19:58:49 +00002282static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
2283 const ARMSubtarget *ST) {
2284 EVT VT = N->getValueType(0);
2285 DebugLoc dl = N->getDebugLoc();
2286
2287 if (!ST->hasV6T2Ops())
2288 return SDValue();
2289
2290 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
2291 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
2292}
2293
Bob Wilson5bafff32009-06-22 23:27:02 +00002294static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2295 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002296 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002297 DebugLoc dl = N->getDebugLoc();
2298
2299 // Lower vector shifts on NEON to use VSHL.
2300 if (VT.isVector()) {
2301 assert(ST->hasNEON() && "unexpected vector shift");
2302
2303 // Left shifts translate directly to the vshiftu intrinsic.
2304 if (N->getOpcode() == ISD::SHL)
2305 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002306 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002307 N->getOperand(0), N->getOperand(1));
2308
2309 assert((N->getOpcode() == ISD::SRA ||
2310 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2311
2312 // NEON uses the same intrinsics for both left and right shifts. For
2313 // right shifts, the shift amounts are negative, so negate the vector of
2314 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002315 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002316 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2317 getZeroVector(ShiftVT, DAG, dl),
2318 N->getOperand(1));
2319 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2320 Intrinsic::arm_neon_vshifts :
2321 Intrinsic::arm_neon_vshiftu);
2322 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002323 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002324 N->getOperand(0), NegatedCount);
2325 }
2326
Eli Friedmance392eb2009-08-22 03:13:10 +00002327 // We can get here for a node like i32 = ISD::SHL i32, i64
2328 if (VT != MVT::i64)
2329 return SDValue();
2330
2331 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002332 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002333
Chris Lattner27a6c732007-11-24 07:07:01 +00002334 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2335 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002336 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002337 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002338
Chris Lattner27a6c732007-11-24 07:07:01 +00002339 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002340 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002341
Chris Lattner27a6c732007-11-24 07:07:01 +00002342 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002343 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2344 DAG.getConstant(0, MVT::i32));
2345 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2346 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002347
Chris Lattner27a6c732007-11-24 07:07:01 +00002348 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2349 // captures the result into a carry flag.
2350 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002351 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002352
Chris Lattner27a6c732007-11-24 07:07:01 +00002353 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002354 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002355
Chris Lattner27a6c732007-11-24 07:07:01 +00002356 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002357 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002358}
2359
Bob Wilson5bafff32009-06-22 23:27:02 +00002360static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2361 SDValue TmpOp0, TmpOp1;
2362 bool Invert = false;
2363 bool Swap = false;
2364 unsigned Opc = 0;
2365
2366 SDValue Op0 = Op.getOperand(0);
2367 SDValue Op1 = Op.getOperand(1);
2368 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002369 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002370 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2371 DebugLoc dl = Op.getDebugLoc();
2372
2373 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2374 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002375 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002376 case ISD::SETUNE:
2377 case ISD::SETNE: Invert = true; // Fallthrough
2378 case ISD::SETOEQ:
2379 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2380 case ISD::SETOLT:
2381 case ISD::SETLT: Swap = true; // Fallthrough
2382 case ISD::SETOGT:
2383 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2384 case ISD::SETOLE:
2385 case ISD::SETLE: Swap = true; // Fallthrough
2386 case ISD::SETOGE:
2387 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2388 case ISD::SETUGE: Swap = true; // Fallthrough
2389 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2390 case ISD::SETUGT: Swap = true; // Fallthrough
2391 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2392 case ISD::SETUEQ: Invert = true; // Fallthrough
2393 case ISD::SETONE:
2394 // Expand this to (OLT | OGT).
2395 TmpOp0 = Op0;
2396 TmpOp1 = Op1;
2397 Opc = ISD::OR;
2398 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2399 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2400 break;
2401 case ISD::SETUO: Invert = true; // Fallthrough
2402 case ISD::SETO:
2403 // Expand this to (OLT | OGE).
2404 TmpOp0 = Op0;
2405 TmpOp1 = Op1;
2406 Opc = ISD::OR;
2407 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2408 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2409 break;
2410 }
2411 } else {
2412 // Integer comparisons.
2413 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002414 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002415 case ISD::SETNE: Invert = true;
2416 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2417 case ISD::SETLT: Swap = true;
2418 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2419 case ISD::SETLE: Swap = true;
2420 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2421 case ISD::SETULT: Swap = true;
2422 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2423 case ISD::SETULE: Swap = true;
2424 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2425 }
2426
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002427 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002428 if (Opc == ARMISD::VCEQ) {
2429
2430 SDValue AndOp;
2431 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2432 AndOp = Op0;
2433 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2434 AndOp = Op1;
2435
2436 // Ignore bitconvert.
2437 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2438 AndOp = AndOp.getOperand(0);
2439
2440 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2441 Opc = ARMISD::VTST;
2442 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2443 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2444 Invert = !Invert;
2445 }
2446 }
2447 }
2448
2449 if (Swap)
2450 std::swap(Op0, Op1);
2451
2452 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2453
2454 if (Invert)
2455 Result = DAG.getNOT(dl, Result, VT);
2456
2457 return Result;
2458}
2459
2460/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2461/// VMOV instruction, and if so, return the constant being splatted.
2462static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2463 unsigned SplatBitSize, SelectionDAG &DAG) {
2464 switch (SplatBitSize) {
2465 case 8:
2466 // Any 1-byte value is OK.
2467 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson825b72b2009-08-11 20:47:22 +00002468 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilson5bafff32009-06-22 23:27:02 +00002469
2470 case 16:
2471 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2472 if ((SplatBits & ~0xff) == 0 ||
2473 (SplatBits & ~0xff00) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002474 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilson5bafff32009-06-22 23:27:02 +00002475 break;
2476
2477 case 32:
2478 // NEON's 32-bit VMOV supports splat values where:
2479 // * only one byte is nonzero, or
2480 // * the least significant byte is 0xff and the second byte is nonzero, or
2481 // * the least significant 2 bytes are 0xff and the third is nonzero.
2482 if ((SplatBits & ~0xff) == 0 ||
2483 (SplatBits & ~0xff00) == 0 ||
2484 (SplatBits & ~0xff0000) == 0 ||
2485 (SplatBits & ~0xff000000) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002486 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002487
2488 if ((SplatBits & ~0xffff) == 0 &&
2489 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002490 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002491
2492 if ((SplatBits & ~0xffffff) == 0 &&
2493 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002494 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002495
2496 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2497 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2498 // VMOV.I32. A (very) minor optimization would be to replicate the value
2499 // and fall through here to test for a valid 64-bit splat. But, then the
2500 // caller would also need to check and handle the change in size.
2501 break;
2502
2503 case 64: {
2504 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2505 uint64_t BitMask = 0xff;
2506 uint64_t Val = 0;
2507 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2508 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2509 Val |= BitMask;
2510 else if ((SplatBits & BitMask) != 0)
2511 return SDValue();
2512 BitMask <<= 8;
2513 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002514 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilson5bafff32009-06-22 23:27:02 +00002515 }
2516
2517 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002518 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilson5bafff32009-06-22 23:27:02 +00002519 break;
2520 }
2521
2522 return SDValue();
2523}
2524
2525/// getVMOVImm - If this is a build_vector of constants which can be
2526/// formed by using a VMOV instruction of the specified element size,
2527/// return the constant being splatted. The ByteSize field indicates the
2528/// number of bytes of each element [1248].
2529SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2530 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2531 APInt SplatBits, SplatUndef;
2532 unsigned SplatBitSize;
2533 bool HasAnyUndefs;
2534 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2535 HasAnyUndefs, ByteSize * 8))
2536 return SDValue();
2537
2538 if (SplatBitSize > ByteSize * 8)
2539 return SDValue();
2540
2541 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2542 SplatBitSize, DAG);
2543}
2544
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002545static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2546 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002547 unsigned NumElts = VT.getVectorNumElements();
2548 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002549 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002550
2551 // If this is a VEXT shuffle, the immediate value is the index of the first
2552 // element. The other shuffle indices must be the successive elements after
2553 // the first one.
2554 unsigned ExpectedElt = Imm;
2555 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002556 // Increment the expected index. If it wraps around, it may still be
2557 // a VEXT but the source vectors must be swapped.
2558 ExpectedElt += 1;
2559 if (ExpectedElt == NumElts * 2) {
2560 ExpectedElt = 0;
2561 ReverseVEXT = true;
2562 }
2563
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002564 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002565 return false;
2566 }
2567
2568 // Adjust the index value if the source operands will be swapped.
2569 if (ReverseVEXT)
2570 Imm -= NumElts;
2571
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002572 return true;
2573}
2574
Bob Wilson8bb9e482009-07-26 00:39:34 +00002575/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2576/// instruction with the specified blocksize. (The order of the elements
2577/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002578static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2579 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002580 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2581 "Only possible block sizes for VREV are: 16, 32, 64");
2582
Bob Wilson8bb9e482009-07-26 00:39:34 +00002583 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00002584 if (EltSz == 64)
2585 return false;
2586
2587 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002588 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002589
2590 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2591 return false;
2592
2593 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002594 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002595 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2596 return false;
2597 }
2598
2599 return true;
2600}
2601
Bob Wilsonc692cb72009-08-21 20:54:19 +00002602static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2603 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002604 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2605 if (EltSz == 64)
2606 return false;
2607
Bob Wilsonc692cb72009-08-21 20:54:19 +00002608 unsigned NumElts = VT.getVectorNumElements();
2609 WhichResult = (M[0] == 0 ? 0 : 1);
2610 for (unsigned i = 0; i < NumElts; i += 2) {
2611 if ((unsigned) M[i] != i + WhichResult ||
2612 (unsigned) M[i+1] != i + NumElts + WhichResult)
2613 return false;
2614 }
2615 return true;
2616}
2617
Bob Wilson324f4f12009-12-03 06:40:55 +00002618/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
2619/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2620/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
2621static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2622 unsigned &WhichResult) {
2623 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2624 if (EltSz == 64)
2625 return false;
2626
2627 unsigned NumElts = VT.getVectorNumElements();
2628 WhichResult = (M[0] == 0 ? 0 : 1);
2629 for (unsigned i = 0; i < NumElts; i += 2) {
2630 if ((unsigned) M[i] != i + WhichResult ||
2631 (unsigned) M[i+1] != i + WhichResult)
2632 return false;
2633 }
2634 return true;
2635}
2636
Bob Wilsonc692cb72009-08-21 20:54:19 +00002637static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2638 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002639 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2640 if (EltSz == 64)
2641 return false;
2642
Bob Wilsonc692cb72009-08-21 20:54:19 +00002643 unsigned NumElts = VT.getVectorNumElements();
2644 WhichResult = (M[0] == 0 ? 0 : 1);
2645 for (unsigned i = 0; i != NumElts; ++i) {
2646 if ((unsigned) M[i] != 2 * i + WhichResult)
2647 return false;
2648 }
2649
2650 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002651 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002652 return false;
2653
2654 return true;
2655}
2656
Bob Wilson324f4f12009-12-03 06:40:55 +00002657/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
2658/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2659/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
2660static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2661 unsigned &WhichResult) {
2662 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2663 if (EltSz == 64)
2664 return false;
2665
2666 unsigned Half = VT.getVectorNumElements() / 2;
2667 WhichResult = (M[0] == 0 ? 0 : 1);
2668 for (unsigned j = 0; j != 2; ++j) {
2669 unsigned Idx = WhichResult;
2670 for (unsigned i = 0; i != Half; ++i) {
2671 if ((unsigned) M[i + j * Half] != Idx)
2672 return false;
2673 Idx += 2;
2674 }
2675 }
2676
2677 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2678 if (VT.is64BitVector() && EltSz == 32)
2679 return false;
2680
2681 return true;
2682}
2683
Bob Wilsonc692cb72009-08-21 20:54:19 +00002684static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2685 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002686 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2687 if (EltSz == 64)
2688 return false;
2689
Bob Wilsonc692cb72009-08-21 20:54:19 +00002690 unsigned NumElts = VT.getVectorNumElements();
2691 WhichResult = (M[0] == 0 ? 0 : 1);
2692 unsigned Idx = WhichResult * NumElts / 2;
2693 for (unsigned i = 0; i != NumElts; i += 2) {
2694 if ((unsigned) M[i] != Idx ||
2695 (unsigned) M[i+1] != Idx + NumElts)
2696 return false;
2697 Idx += 1;
2698 }
2699
2700 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002701 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002702 return false;
2703
2704 return true;
2705}
2706
Bob Wilson324f4f12009-12-03 06:40:55 +00002707/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
2708/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2709/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
2710static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2711 unsigned &WhichResult) {
2712 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2713 if (EltSz == 64)
2714 return false;
2715
2716 unsigned NumElts = VT.getVectorNumElements();
2717 WhichResult = (M[0] == 0 ? 0 : 1);
2718 unsigned Idx = WhichResult * NumElts / 2;
2719 for (unsigned i = 0; i != NumElts; i += 2) {
2720 if ((unsigned) M[i] != Idx ||
2721 (unsigned) M[i+1] != Idx)
2722 return false;
2723 Idx += 1;
2724 }
2725
2726 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2727 if (VT.is64BitVector() && EltSz == 32)
2728 return false;
2729
2730 return true;
2731}
2732
2733
Owen Andersone50ed302009-08-10 22:56:29 +00002734static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002735 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00002736 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002737 if (ConstVal->isNullValue())
2738 return getZeroVector(VT, DAG, dl);
2739 if (ConstVal->isAllOnesValue())
2740 return getOnesVector(VT, DAG, dl);
2741
Owen Andersone50ed302009-08-10 22:56:29 +00002742 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00002743 if (VT.is64BitVector()) {
2744 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002745 case 8: CanonicalVT = MVT::v8i8; break;
2746 case 16: CanonicalVT = MVT::v4i16; break;
2747 case 32: CanonicalVT = MVT::v2i32; break;
2748 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002749 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002750 }
2751 } else {
2752 assert(VT.is128BitVector() && "unknown splat vector size");
2753 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002754 case 8: CanonicalVT = MVT::v16i8; break;
2755 case 16: CanonicalVT = MVT::v8i16; break;
2756 case 32: CanonicalVT = MVT::v4i32; break;
2757 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002758 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002759 }
2760 }
2761
2762 // Build a canonical splat for this value.
2763 SmallVector<SDValue, 8> Ops;
2764 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2765 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2766 Ops.size());
2767 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2768}
2769
2770// If this is a case we can't handle, return null and let the default
2771// expansion code take care of it.
2772static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00002773 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002774 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002775 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002776
2777 APInt SplatBits, SplatUndef;
2778 unsigned SplatBitSize;
2779 bool HasAnyUndefs;
2780 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00002781 if (SplatBitSize <= 64) {
2782 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2783 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2784 if (Val.getNode())
2785 return BuildSplat(Val, VT, DAG, dl);
2786 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00002787 }
2788
2789 // If there are only 2 elements in a 128-bit vector, insert them into an
2790 // undef vector. This handles the common case for 128-bit vector argument
2791 // passing, where the insertions should be translated to subreg accesses
2792 // with no real instructions.
2793 if (VT.is128BitVector() && Op.getNumOperands() == 2) {
2794 SDValue Val = DAG.getUNDEF(VT);
2795 SDValue Op0 = Op.getOperand(0);
2796 SDValue Op1 = Op.getOperand(1);
2797 if (Op0.getOpcode() != ISD::UNDEF)
2798 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op0,
2799 DAG.getIntPtrConstant(0));
2800 if (Op1.getOpcode() != ISD::UNDEF)
2801 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op1,
2802 DAG.getIntPtrConstant(1));
2803 return Val;
Bob Wilson5bafff32009-06-22 23:27:02 +00002804 }
2805
2806 return SDValue();
2807}
2808
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002809/// isShuffleMaskLegal - Targets can use this to indicate that they only
2810/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2811/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2812/// are assumed to be legal.
2813bool
2814ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2815 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002816 if (VT.getVectorNumElements() == 4 &&
2817 (VT.is128BitVector() || VT.is64BitVector())) {
2818 unsigned PFIndexes[4];
2819 for (unsigned i = 0; i != 4; ++i) {
2820 if (M[i] < 0)
2821 PFIndexes[i] = 8;
2822 else
2823 PFIndexes[i] = M[i];
2824 }
2825
2826 // Compute the index in the perfect shuffle table.
2827 unsigned PFTableIndex =
2828 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2829 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2830 unsigned Cost = (PFEntry >> 30);
2831
2832 if (Cost <= 4)
2833 return true;
2834 }
2835
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002836 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00002837 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002838
2839 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2840 isVREVMask(M, VT, 64) ||
2841 isVREVMask(M, VT, 32) ||
2842 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00002843 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2844 isVTRNMask(M, VT, WhichResult) ||
2845 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00002846 isVZIPMask(M, VT, WhichResult) ||
2847 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
2848 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
2849 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002850}
2851
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002852/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2853/// the specified operations to build the shuffle.
2854static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2855 SDValue RHS, SelectionDAG &DAG,
2856 DebugLoc dl) {
2857 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2858 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2859 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2860
2861 enum {
2862 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2863 OP_VREV,
2864 OP_VDUP0,
2865 OP_VDUP1,
2866 OP_VDUP2,
2867 OP_VDUP3,
2868 OP_VEXT1,
2869 OP_VEXT2,
2870 OP_VEXT3,
2871 OP_VUZPL, // VUZP, left result
2872 OP_VUZPR, // VUZP, right result
2873 OP_VZIPL, // VZIP, left result
2874 OP_VZIPR, // VZIP, right result
2875 OP_VTRNL, // VTRN, left result
2876 OP_VTRNR // VTRN, right result
2877 };
2878
2879 if (OpNum == OP_COPY) {
2880 if (LHSID == (1*9+2)*9+3) return LHS;
2881 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2882 return RHS;
2883 }
2884
2885 SDValue OpLHS, OpRHS;
2886 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2887 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2888 EVT VT = OpLHS.getValueType();
2889
2890 switch (OpNum) {
2891 default: llvm_unreachable("Unknown shuffle opcode!");
2892 case OP_VREV:
2893 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2894 case OP_VDUP0:
2895 case OP_VDUP1:
2896 case OP_VDUP2:
2897 case OP_VDUP3:
2898 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002899 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002900 case OP_VEXT1:
2901 case OP_VEXT2:
2902 case OP_VEXT3:
2903 return DAG.getNode(ARMISD::VEXT, dl, VT,
2904 OpLHS, OpRHS,
2905 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2906 case OP_VUZPL:
2907 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002908 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002909 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
2910 case OP_VZIPL:
2911 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002912 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002913 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
2914 case OP_VTRNL:
2915 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002916 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2917 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002918 }
2919}
2920
Bob Wilson5bafff32009-06-22 23:27:02 +00002921static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002922 SDValue V1 = Op.getOperand(0);
2923 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00002924 DebugLoc dl = Op.getDebugLoc();
2925 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002926 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002927 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00002928
Bob Wilson28865062009-08-13 02:13:04 +00002929 // Convert shuffles that are directly supported on NEON to target-specific
2930 // DAG nodes, instead of keeping them as shuffles and matching them again
2931 // during code selection. This is more efficient and avoids the possibility
2932 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00002933 // FIXME: floating-point vectors should be canonicalized to integer vectors
2934 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002935 SVN->getMask(ShuffleMask);
2936
2937 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson0ce37102009-08-14 05:08:32 +00002938 int Lane = SVN->getSplatIndex();
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00002939 // If this is undef splat, generate it via "just" vdup, if possible.
2940 if (Lane == -1) Lane = 0;
2941
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002942 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
2943 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00002944 }
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002945 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002946 DAG.getConstant(Lane, MVT::i32));
Bob Wilson0ce37102009-08-14 05:08:32 +00002947 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002948
2949 bool ReverseVEXT;
2950 unsigned Imm;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002951 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002952 if (ReverseVEXT)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002953 std::swap(V1, V2);
2954 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002955 DAG.getConstant(Imm, MVT::i32));
2956 }
2957
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002958 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002959 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002960 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002961 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002962 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002963 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
2964
Bob Wilsonc692cb72009-08-21 20:54:19 +00002965 // Check for Neon shuffles that modify both input vectors in place.
2966 // If both results are used, i.e., if there are two shuffles with the same
2967 // source operands and with masks corresponding to both results of one of
2968 // these operations, DAG memoization will ensure that a single node is
2969 // used for both shuffles.
2970 unsigned WhichResult;
2971 if (isVTRNMask(ShuffleMask, VT, WhichResult))
2972 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2973 V1, V2).getValue(WhichResult);
2974 if (isVUZPMask(ShuffleMask, VT, WhichResult))
2975 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2976 V1, V2).getValue(WhichResult);
2977 if (isVZIPMask(ShuffleMask, VT, WhichResult))
2978 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2979 V1, V2).getValue(WhichResult);
2980
Bob Wilson324f4f12009-12-03 06:40:55 +00002981 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
2982 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2983 V1, V1).getValue(WhichResult);
2984 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
2985 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2986 V1, V1).getValue(WhichResult);
2987 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
2988 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2989 V1, V1).getValue(WhichResult);
2990
Bob Wilsonc692cb72009-08-21 20:54:19 +00002991 // If the shuffle is not directly supported and it has 4 elements, use
2992 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002993 if (VT.getVectorNumElements() == 4 &&
2994 (VT.is128BitVector() || VT.is64BitVector())) {
2995 unsigned PFIndexes[4];
2996 for (unsigned i = 0; i != 4; ++i) {
2997 if (ShuffleMask[i] < 0)
2998 PFIndexes[i] = 8;
2999 else
3000 PFIndexes[i] = ShuffleMask[i];
3001 }
3002
3003 // Compute the index in the perfect shuffle table.
3004 unsigned PFTableIndex =
3005 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3006
3007 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3008 unsigned Cost = (PFEntry >> 30);
3009
3010 if (Cost <= 4)
3011 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
3012 }
Bob Wilsond8e17572009-08-12 22:31:50 +00003013
Bob Wilson22cac0d2009-08-14 05:16:33 +00003014 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003015}
3016
Bob Wilson5bafff32009-06-22 23:27:02 +00003017static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003018 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003019 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003020 SDValue Vec = Op.getOperand(0);
3021 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00003022 assert(VT == MVT::i32 &&
3023 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
3024 "unexpected type for custom-lowering vector extract");
3025 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00003026}
3027
Bob Wilsona6d65862009-08-03 20:36:38 +00003028static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3029 // The only time a CONCAT_VECTORS operation can have legal types is when
3030 // two 64-bit vectors are concatenated to a 128-bit vector.
3031 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
3032 "unexpected CONCAT_VECTORS");
3033 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00003034 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00003035 SDValue Op0 = Op.getOperand(0);
3036 SDValue Op1 = Op.getOperand(1);
3037 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003038 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3039 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00003040 DAG.getIntPtrConstant(0));
3041 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003042 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3043 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00003044 DAG.getIntPtrConstant(1));
3045 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003046}
3047
Dan Gohmand858e902010-04-17 15:26:15 +00003048SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003049 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003050 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00003051 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00003052 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003053 case ISD::GlobalAddress:
3054 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
3055 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00003056 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00003057 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
3058 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003059 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00003060 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00003061 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00003062 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00003063 case ISD::SINT_TO_FP:
3064 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
3065 case ISD::FP_TO_SINT:
3066 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003067 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00003068 case ISD::RETURNADDR: break;
Jim Grosbach0e0da732009-05-12 23:59:14 +00003069 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003070 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00003071 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
3072 Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00003073 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003074 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00003075 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00003076 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00003077 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003078 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00003079 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00003080 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003081 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
3082 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3083 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003084 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00003085 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003086 }
Dan Gohman475871a2008-07-27 21:46:04 +00003087 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003088}
3089
Duncan Sands1607f052008-12-01 11:39:25 +00003090/// ReplaceNodeResults - Replace the results of node with an illegal result
3091/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00003092void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
3093 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00003094 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00003095 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00003096 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00003097 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003098 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003099 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003100 case ISD::BIT_CONVERT:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003101 Res = ExpandBIT_CONVERT(N, DAG);
3102 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00003103 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003104 case ISD::SRA:
3105 Res = LowerShift(N, DAG, Subtarget);
3106 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003107 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00003108 if (Res.getNode())
3109 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00003110}
Chris Lattner27a6c732007-11-24 07:07:01 +00003111
Evan Chenga8e29892007-01-19 07:51:42 +00003112//===----------------------------------------------------------------------===//
3113// ARM Scheduler Hooks
3114//===----------------------------------------------------------------------===//
3115
3116MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003117ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
3118 MachineBasicBlock *BB,
3119 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00003120 unsigned dest = MI->getOperand(0).getReg();
3121 unsigned ptr = MI->getOperand(1).getReg();
3122 unsigned oldval = MI->getOperand(2).getReg();
3123 unsigned newval = MI->getOperand(3).getReg();
3124 unsigned scratch = BB->getParent()->getRegInfo()
3125 .createVirtualRegister(ARM::GPRRegisterClass);
3126 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3127 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003128 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00003129
3130 unsigned ldrOpc, strOpc;
3131 switch (Size) {
3132 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003133 case 1:
3134 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
3135 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
3136 break;
3137 case 2:
3138 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3139 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3140 break;
3141 case 4:
3142 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3143 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3144 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00003145 }
3146
3147 MachineFunction *MF = BB->getParent();
3148 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3149 MachineFunction::iterator It = BB;
3150 ++It; // insert the new blocks after the current block
3151
3152 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3153 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3154 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3155 MF->insert(It, loop1MBB);
3156 MF->insert(It, loop2MBB);
3157 MF->insert(It, exitMBB);
3158 exitMBB->transferSuccessors(BB);
3159
3160 // thisMBB:
3161 // ...
3162 // fallthrough --> loop1MBB
3163 BB->addSuccessor(loop1MBB);
3164
3165 // loop1MBB:
3166 // ldrex dest, [ptr]
3167 // cmp dest, oldval
3168 // bne exitMBB
3169 BB = loop1MBB;
3170 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003171 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003172 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003173 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3174 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003175 BB->addSuccessor(loop2MBB);
3176 BB->addSuccessor(exitMBB);
3177
3178 // loop2MBB:
3179 // strex scratch, newval, [ptr]
3180 // cmp scratch, #0
3181 // bne loop1MBB
3182 BB = loop2MBB;
3183 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
3184 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003185 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003186 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003187 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3188 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003189 BB->addSuccessor(loop1MBB);
3190 BB->addSuccessor(exitMBB);
3191
3192 // exitMBB:
3193 // ...
3194 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00003195
3196 MF->DeleteMachineInstr(MI); // The instruction is gone now.
3197
Jim Grosbach5278eb82009-12-11 01:42:04 +00003198 return BB;
3199}
3200
3201MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003202ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
3203 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00003204 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
3205 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3206
3207 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003208 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003209 MachineFunction::iterator It = BB;
3210 ++It;
3211
3212 unsigned dest = MI->getOperand(0).getReg();
3213 unsigned ptr = MI->getOperand(1).getReg();
3214 unsigned incr = MI->getOperand(2).getReg();
3215 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00003216
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003217 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003218 unsigned ldrOpc, strOpc;
3219 switch (Size) {
3220 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003221 case 1:
3222 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00003223 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003224 break;
3225 case 2:
3226 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3227 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3228 break;
3229 case 4:
3230 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3231 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3232 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00003233 }
3234
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003235 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3236 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3237 MF->insert(It, loopMBB);
3238 MF->insert(It, exitMBB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003239 exitMBB->transferSuccessors(BB);
3240
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003241 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003242 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3243 unsigned scratch2 = (!BinOpcode) ? incr :
3244 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3245
3246 // thisMBB:
3247 // ...
3248 // fallthrough --> loopMBB
3249 BB->addSuccessor(loopMBB);
3250
3251 // loopMBB:
3252 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003253 // <binop> scratch2, dest, incr
3254 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00003255 // cmp scratch, #0
3256 // bne- loopMBB
3257 // fallthrough --> exitMBB
3258 BB = loopMBB;
3259 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00003260 if (BinOpcode) {
3261 // operand order needs to go the other way for NAND
3262 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
3263 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3264 addReg(incr).addReg(dest)).addReg(0);
3265 else
3266 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3267 addReg(dest).addReg(incr)).addReg(0);
3268 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00003269
3270 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
3271 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003272 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00003273 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003274 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3275 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003276
3277 BB->addSuccessor(loopMBB);
3278 BB->addSuccessor(exitMBB);
3279
3280 // exitMBB:
3281 // ...
3282 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00003283
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003284 MF->DeleteMachineInstr(MI); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00003285
Jim Grosbachc3c23542009-12-14 04:22:04 +00003286 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00003287}
3288
3289MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00003290ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003291 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003292 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00003293 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003294 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00003295 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00003296 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00003297 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00003298 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00003299
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003300 case ARM::ATOMIC_LOAD_ADD_I8:
3301 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3302 case ARM::ATOMIC_LOAD_ADD_I16:
3303 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3304 case ARM::ATOMIC_LOAD_ADD_I32:
3305 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003306
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003307 case ARM::ATOMIC_LOAD_AND_I8:
3308 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3309 case ARM::ATOMIC_LOAD_AND_I16:
3310 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3311 case ARM::ATOMIC_LOAD_AND_I32:
3312 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003313
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003314 case ARM::ATOMIC_LOAD_OR_I8:
3315 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3316 case ARM::ATOMIC_LOAD_OR_I16:
3317 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3318 case ARM::ATOMIC_LOAD_OR_I32:
3319 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003320
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003321 case ARM::ATOMIC_LOAD_XOR_I8:
3322 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3323 case ARM::ATOMIC_LOAD_XOR_I16:
3324 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3325 case ARM::ATOMIC_LOAD_XOR_I32:
3326 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003327
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003328 case ARM::ATOMIC_LOAD_NAND_I8:
3329 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3330 case ARM::ATOMIC_LOAD_NAND_I16:
3331 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3332 case ARM::ATOMIC_LOAD_NAND_I32:
3333 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003334
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003335 case ARM::ATOMIC_LOAD_SUB_I8:
3336 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3337 case ARM::ATOMIC_LOAD_SUB_I16:
3338 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3339 case ARM::ATOMIC_LOAD_SUB_I32:
3340 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003341
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003342 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
3343 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
3344 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00003345
3346 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
3347 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
3348 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003349
Evan Cheng007ea272009-08-12 05:17:19 +00003350 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00003351 // To "insert" a SELECT_CC instruction, we actually have to insert the
3352 // diamond control-flow pattern. The incoming instruction knows the
3353 // destination vreg to set, the condition code register to branch on, the
3354 // true/false values to select between, and a branch opcode to use.
3355 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003356 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00003357 ++It;
3358
3359 // thisMBB:
3360 // ...
3361 // TrueVal = ...
3362 // cmpTY ccX, r1, r2
3363 // bCC copy1MBB
3364 // fallthrough --> copy0MBB
3365 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003366 MachineFunction *F = BB->getParent();
3367 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3368 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00003369 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00003370 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003371 F->insert(It, copy0MBB);
3372 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00003373 // Update machine-CFG edges by first adding all successors of the current
3374 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00003375 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003376 E = BB->succ_end(); I != E; ++I)
Evan Chengce319102009-09-19 09:51:03 +00003377 sinkMBB->addSuccessor(*I);
Evan Chenga8e29892007-01-19 07:51:42 +00003378 // Next, remove all successors of the current block, and add the true
3379 // and fallthrough blocks as its successors.
Evan Chengce319102009-09-19 09:51:03 +00003380 while (!BB->succ_empty())
Evan Chenga8e29892007-01-19 07:51:42 +00003381 BB->removeSuccessor(BB->succ_begin());
3382 BB->addSuccessor(copy0MBB);
3383 BB->addSuccessor(sinkMBB);
3384
3385 // copy0MBB:
3386 // %FalseValue = ...
3387 // # fallthrough to sinkMBB
3388 BB = copy0MBB;
3389
3390 // Update machine-CFG edges
3391 BB->addSuccessor(sinkMBB);
3392
3393 // sinkMBB:
3394 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3395 // ...
3396 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00003397 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00003398 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
3399 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
3400
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003401 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00003402 return BB;
3403 }
Evan Cheng86198642009-08-07 00:34:42 +00003404
3405 case ARM::tANDsp:
3406 case ARM::tADDspr_:
3407 case ARM::tSUBspi_:
3408 case ARM::t2SUBrSPi_:
3409 case ARM::t2SUBrSPi12_:
3410 case ARM::t2SUBrSPs_: {
3411 MachineFunction *MF = BB->getParent();
3412 unsigned DstReg = MI->getOperand(0).getReg();
3413 unsigned SrcReg = MI->getOperand(1).getReg();
3414 bool DstIsDead = MI->getOperand(0).isDead();
3415 bool SrcIsKill = MI->getOperand(1).isKill();
3416
3417 if (SrcReg != ARM::SP) {
3418 // Copy the source to SP from virtual register.
3419 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
3420 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3421 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
3422 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
3423 .addReg(SrcReg, getKillRegState(SrcIsKill));
3424 }
3425
3426 unsigned OpOpc = 0;
3427 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
3428 switch (MI->getOpcode()) {
3429 default:
3430 llvm_unreachable("Unexpected pseudo instruction!");
3431 case ARM::tANDsp:
3432 OpOpc = ARM::tAND;
3433 NeedPred = true;
3434 break;
3435 case ARM::tADDspr_:
3436 OpOpc = ARM::tADDspr;
3437 break;
3438 case ARM::tSUBspi_:
3439 OpOpc = ARM::tSUBspi;
3440 break;
3441 case ARM::t2SUBrSPi_:
3442 OpOpc = ARM::t2SUBrSPi;
3443 NeedPred = true; NeedCC = true;
3444 break;
3445 case ARM::t2SUBrSPi12_:
3446 OpOpc = ARM::t2SUBrSPi12;
3447 NeedPred = true;
3448 break;
3449 case ARM::t2SUBrSPs_:
3450 OpOpc = ARM::t2SUBrSPs;
3451 NeedPred = true; NeedCC = true; NeedOp3 = true;
3452 break;
3453 }
3454 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
3455 if (OpOpc == ARM::tAND)
3456 AddDefaultT1CC(MIB);
3457 MIB.addReg(ARM::SP);
3458 MIB.addOperand(MI->getOperand(2));
3459 if (NeedOp3)
3460 MIB.addOperand(MI->getOperand(3));
3461 if (NeedPred)
3462 AddDefaultPred(MIB);
3463 if (NeedCC)
3464 AddDefaultCC(MIB);
3465
3466 // Copy the result from SP to virtual register.
3467 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
3468 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3469 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
3470 BuildMI(BB, dl, TII->get(CopyOpc))
3471 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
3472 .addReg(ARM::SP);
3473 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3474 return BB;
3475 }
Evan Chenga8e29892007-01-19 07:51:42 +00003476 }
3477}
3478
3479//===----------------------------------------------------------------------===//
3480// ARM Optimization Hooks
3481//===----------------------------------------------------------------------===//
3482
Chris Lattnerd1980a52009-03-12 06:52:53 +00003483static
3484SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3485 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00003486 SelectionDAG &DAG = DCI.DAG;
3487 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003488 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00003489 unsigned Opc = N->getOpcode();
3490 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3491 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3492 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3493 ISD::CondCode CC = ISD::SETCC_INVALID;
3494
3495 if (isSlctCC) {
3496 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
3497 } else {
3498 SDValue CCOp = Slct.getOperand(0);
3499 if (CCOp.getOpcode() == ISD::SETCC)
3500 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
3501 }
3502
3503 bool DoXform = false;
3504 bool InvCC = false;
3505 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
3506 "Bad input!");
3507
3508 if (LHS.getOpcode() == ISD::Constant &&
3509 cast<ConstantSDNode>(LHS)->isNullValue()) {
3510 DoXform = true;
3511 } else if (CC != ISD::SETCC_INVALID &&
3512 RHS.getOpcode() == ISD::Constant &&
3513 cast<ConstantSDNode>(RHS)->isNullValue()) {
3514 std::swap(LHS, RHS);
3515 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003516 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00003517 Op0.getOperand(0).getValueType();
3518 bool isInt = OpVT.isInteger();
3519 CC = ISD::getSetCCInverse(CC, isInt);
3520
3521 if (!TLI.isCondCodeLegal(CC, OpVT))
3522 return SDValue(); // Inverse operator isn't legal.
3523
3524 DoXform = true;
3525 InvCC = true;
3526 }
3527
3528 if (DoXform) {
3529 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3530 if (isSlctCC)
3531 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3532 Slct.getOperand(0), Slct.getOperand(1), CC);
3533 SDValue CCOp = Slct.getOperand(0);
3534 if (InvCC)
3535 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3536 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3537 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3538 CCOp, OtherOp, Result);
3539 }
3540 return SDValue();
3541}
3542
3543/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3544static SDValue PerformADDCombine(SDNode *N,
3545 TargetLowering::DAGCombinerInfo &DCI) {
3546 // added by evan in r37685 with no testcase.
3547 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003548
Chris Lattnerd1980a52009-03-12 06:52:53 +00003549 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3550 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3551 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3552 if (Result.getNode()) return Result;
3553 }
3554 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3555 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3556 if (Result.getNode()) return Result;
3557 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003558
Chris Lattnerd1980a52009-03-12 06:52:53 +00003559 return SDValue();
3560}
3561
3562/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3563static SDValue PerformSUBCombine(SDNode *N,
3564 TargetLowering::DAGCombinerInfo &DCI) {
3565 // added by evan in r37685 with no testcase.
3566 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003567
Chris Lattnerd1980a52009-03-12 06:52:53 +00003568 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3569 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3570 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3571 if (Result.getNode()) return Result;
3572 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003573
Chris Lattnerd1980a52009-03-12 06:52:53 +00003574 return SDValue();
3575}
3576
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +00003577/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
3578/// ARMISD::VMOVRRD.
Jim Grosbache5165492009-11-09 00:11:35 +00003579static SDValue PerformVMOVRRDCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003580 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003581 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003582 SDValue InDouble = N->getOperand(0);
Jim Grosbache5165492009-11-09 00:11:35 +00003583 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003584 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003585 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003586}
3587
Bob Wilson5bafff32009-06-22 23:27:02 +00003588/// getVShiftImm - Check if this is a valid build_vector for the immediate
3589/// operand of a vector shift operation, where all the elements of the
3590/// build_vector must have the same constant integer value.
3591static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3592 // Ignore bit_converts.
3593 while (Op.getOpcode() == ISD::BIT_CONVERT)
3594 Op = Op.getOperand(0);
3595 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3596 APInt SplatBits, SplatUndef;
3597 unsigned SplatBitSize;
3598 bool HasAnyUndefs;
3599 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3600 HasAnyUndefs, ElementBits) ||
3601 SplatBitSize > ElementBits)
3602 return false;
3603 Cnt = SplatBits.getSExtValue();
3604 return true;
3605}
3606
3607/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3608/// operand of a vector shift left operation. That value must be in the range:
3609/// 0 <= Value < ElementBits for a left shift; or
3610/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003611static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003612 assert(VT.isVector() && "vector shift count is not a vector type");
3613 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3614 if (! getVShiftImm(Op, ElementBits, Cnt))
3615 return false;
3616 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3617}
3618
3619/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3620/// operand of a vector shift right operation. For a shift opcode, the value
3621/// is positive, but for an intrinsic the value count must be negative. The
3622/// absolute value must be in the range:
3623/// 1 <= |Value| <= ElementBits for a right shift; or
3624/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003625static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00003626 int64_t &Cnt) {
3627 assert(VT.isVector() && "vector shift count is not a vector type");
3628 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3629 if (! getVShiftImm(Op, ElementBits, Cnt))
3630 return false;
3631 if (isIntrinsic)
3632 Cnt = -Cnt;
3633 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3634}
3635
3636/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3637static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3638 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3639 switch (IntNo) {
3640 default:
3641 // Don't do anything for most intrinsics.
3642 break;
3643
3644 // Vector shifts: check for immediate versions and lower them.
3645 // Note: This is done during DAG combining instead of DAG legalizing because
3646 // the build_vectors for 64-bit vector element shift counts are generally
3647 // not legal, and it is hard to see their values after they get legalized to
3648 // loads from a constant pool.
3649 case Intrinsic::arm_neon_vshifts:
3650 case Intrinsic::arm_neon_vshiftu:
3651 case Intrinsic::arm_neon_vshiftls:
3652 case Intrinsic::arm_neon_vshiftlu:
3653 case Intrinsic::arm_neon_vshiftn:
3654 case Intrinsic::arm_neon_vrshifts:
3655 case Intrinsic::arm_neon_vrshiftu:
3656 case Intrinsic::arm_neon_vrshiftn:
3657 case Intrinsic::arm_neon_vqshifts:
3658 case Intrinsic::arm_neon_vqshiftu:
3659 case Intrinsic::arm_neon_vqshiftsu:
3660 case Intrinsic::arm_neon_vqshiftns:
3661 case Intrinsic::arm_neon_vqshiftnu:
3662 case Intrinsic::arm_neon_vqshiftnsu:
3663 case Intrinsic::arm_neon_vqrshiftns:
3664 case Intrinsic::arm_neon_vqrshiftnu:
3665 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00003666 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003667 int64_t Cnt;
3668 unsigned VShiftOpc = 0;
3669
3670 switch (IntNo) {
3671 case Intrinsic::arm_neon_vshifts:
3672 case Intrinsic::arm_neon_vshiftu:
3673 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3674 VShiftOpc = ARMISD::VSHL;
3675 break;
3676 }
3677 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3678 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3679 ARMISD::VSHRs : ARMISD::VSHRu);
3680 break;
3681 }
3682 return SDValue();
3683
3684 case Intrinsic::arm_neon_vshiftls:
3685 case Intrinsic::arm_neon_vshiftlu:
3686 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3687 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003688 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003689
3690 case Intrinsic::arm_neon_vrshifts:
3691 case Intrinsic::arm_neon_vrshiftu:
3692 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3693 break;
3694 return SDValue();
3695
3696 case Intrinsic::arm_neon_vqshifts:
3697 case Intrinsic::arm_neon_vqshiftu:
3698 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3699 break;
3700 return SDValue();
3701
3702 case Intrinsic::arm_neon_vqshiftsu:
3703 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3704 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003705 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003706
3707 case Intrinsic::arm_neon_vshiftn:
3708 case Intrinsic::arm_neon_vrshiftn:
3709 case Intrinsic::arm_neon_vqshiftns:
3710 case Intrinsic::arm_neon_vqshiftnu:
3711 case Intrinsic::arm_neon_vqshiftnsu:
3712 case Intrinsic::arm_neon_vqrshiftns:
3713 case Intrinsic::arm_neon_vqrshiftnu:
3714 case Intrinsic::arm_neon_vqrshiftnsu:
3715 // Narrowing shifts require an immediate right shift.
3716 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3717 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003718 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003719
3720 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003721 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003722 }
3723
3724 switch (IntNo) {
3725 case Intrinsic::arm_neon_vshifts:
3726 case Intrinsic::arm_neon_vshiftu:
3727 // Opcode already set above.
3728 break;
3729 case Intrinsic::arm_neon_vshiftls:
3730 case Intrinsic::arm_neon_vshiftlu:
3731 if (Cnt == VT.getVectorElementType().getSizeInBits())
3732 VShiftOpc = ARMISD::VSHLLi;
3733 else
3734 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3735 ARMISD::VSHLLs : ARMISD::VSHLLu);
3736 break;
3737 case Intrinsic::arm_neon_vshiftn:
3738 VShiftOpc = ARMISD::VSHRN; break;
3739 case Intrinsic::arm_neon_vrshifts:
3740 VShiftOpc = ARMISD::VRSHRs; break;
3741 case Intrinsic::arm_neon_vrshiftu:
3742 VShiftOpc = ARMISD::VRSHRu; break;
3743 case Intrinsic::arm_neon_vrshiftn:
3744 VShiftOpc = ARMISD::VRSHRN; break;
3745 case Intrinsic::arm_neon_vqshifts:
3746 VShiftOpc = ARMISD::VQSHLs; break;
3747 case Intrinsic::arm_neon_vqshiftu:
3748 VShiftOpc = ARMISD::VQSHLu; break;
3749 case Intrinsic::arm_neon_vqshiftsu:
3750 VShiftOpc = ARMISD::VQSHLsu; break;
3751 case Intrinsic::arm_neon_vqshiftns:
3752 VShiftOpc = ARMISD::VQSHRNs; break;
3753 case Intrinsic::arm_neon_vqshiftnu:
3754 VShiftOpc = ARMISD::VQSHRNu; break;
3755 case Intrinsic::arm_neon_vqshiftnsu:
3756 VShiftOpc = ARMISD::VQSHRNsu; break;
3757 case Intrinsic::arm_neon_vqrshiftns:
3758 VShiftOpc = ARMISD::VQRSHRNs; break;
3759 case Intrinsic::arm_neon_vqrshiftnu:
3760 VShiftOpc = ARMISD::VQRSHRNu; break;
3761 case Intrinsic::arm_neon_vqrshiftnsu:
3762 VShiftOpc = ARMISD::VQRSHRNsu; break;
3763 }
3764
3765 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003766 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003767 }
3768
3769 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00003770 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003771 int64_t Cnt;
3772 unsigned VShiftOpc = 0;
3773
3774 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3775 VShiftOpc = ARMISD::VSLI;
3776 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3777 VShiftOpc = ARMISD::VSRI;
3778 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003779 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003780 }
3781
3782 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3783 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00003784 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003785 }
3786
3787 case Intrinsic::arm_neon_vqrshifts:
3788 case Intrinsic::arm_neon_vqrshiftu:
3789 // No immediate versions of these to check for.
3790 break;
3791 }
3792
3793 return SDValue();
3794}
3795
3796/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3797/// lowers them. As with the vector shift intrinsics, this is done during DAG
3798/// combining instead of DAG legalizing because the build_vectors for 64-bit
3799/// vector element shift counts are generally not legal, and it is hard to see
3800/// their values after they get legalized to loads from a constant pool.
3801static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3802 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003803 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003804
3805 // Nothing to be done for scalar shifts.
3806 if (! VT.isVector())
3807 return SDValue();
3808
3809 assert(ST->hasNEON() && "unexpected vector shift");
3810 int64_t Cnt;
3811
3812 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003813 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003814
3815 case ISD::SHL:
3816 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3817 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003818 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003819 break;
3820
3821 case ISD::SRA:
3822 case ISD::SRL:
3823 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3824 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3825 ARMISD::VSHRs : ARMISD::VSHRu);
3826 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003827 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003828 }
3829 }
3830 return SDValue();
3831}
3832
3833/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3834/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3835static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3836 const ARMSubtarget *ST) {
3837 SDValue N0 = N->getOperand(0);
3838
3839 // Check for sign- and zero-extensions of vector extract operations of 8-
3840 // and 16-bit vector elements. NEON supports these directly. They are
3841 // handled during DAG combining because type legalization will promote them
3842 // to 32-bit types and it is messy to recognize the operations after that.
3843 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3844 SDValue Vec = N0.getOperand(0);
3845 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003846 EVT VT = N->getValueType(0);
3847 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003848 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3849
Owen Anderson825b72b2009-08-11 20:47:22 +00003850 if (VT == MVT::i32 &&
3851 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00003852 TLI.isTypeLegal(Vec.getValueType())) {
3853
3854 unsigned Opc = 0;
3855 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003856 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003857 case ISD::SIGN_EXTEND:
3858 Opc = ARMISD::VGETLANEs;
3859 break;
3860 case ISD::ZERO_EXTEND:
3861 case ISD::ANY_EXTEND:
3862 Opc = ARMISD::VGETLANEu;
3863 break;
3864 }
3865 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
3866 }
3867 }
3868
3869 return SDValue();
3870}
3871
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003872/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
3873/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
3874static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
3875 const ARMSubtarget *ST) {
3876 // If the target supports NEON, try to use vmax/vmin instructions for f32
3877 // selects like "x < y ? x : y". Unless the FiniteOnlyFPMath option is set,
3878 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
3879 // a NaN; only do the transformation when it matches that behavior.
3880
3881 // For now only do this when using NEON for FP operations; if using VFP, it
3882 // is not obvious that the benefit outweighs the cost of switching to the
3883 // NEON pipeline.
3884 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
3885 N->getValueType(0) != MVT::f32)
3886 return SDValue();
3887
3888 SDValue CondLHS = N->getOperand(0);
3889 SDValue CondRHS = N->getOperand(1);
3890 SDValue LHS = N->getOperand(2);
3891 SDValue RHS = N->getOperand(3);
3892 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
3893
3894 unsigned Opcode = 0;
3895 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00003896 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003897 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00003898 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003899 IsReversed = true ; // x CC y ? y : x
3900 } else {
3901 return SDValue();
3902 }
3903
Bob Wilsone742bb52010-02-24 22:15:53 +00003904 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003905 switch (CC) {
3906 default: break;
3907 case ISD::SETOLT:
3908 case ISD::SETOLE:
3909 case ISD::SETLT:
3910 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003911 case ISD::SETULT:
3912 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00003913 // If LHS is NaN, an ordered comparison will be false and the result will
3914 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
3915 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
3916 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
3917 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
3918 break;
3919 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
3920 // will return -0, so vmin can only be used for unsafe math or if one of
3921 // the operands is known to be nonzero.
3922 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
3923 !UnsafeFPMath &&
3924 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
3925 break;
3926 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003927 break;
3928
3929 case ISD::SETOGT:
3930 case ISD::SETOGE:
3931 case ISD::SETGT:
3932 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003933 case ISD::SETUGT:
3934 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00003935 // If LHS is NaN, an ordered comparison will be false and the result will
3936 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
3937 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
3938 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
3939 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
3940 break;
3941 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
3942 // will return +0, so vmax can only be used for unsafe math or if one of
3943 // the operands is known to be nonzero.
3944 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
3945 !UnsafeFPMath &&
3946 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
3947 break;
3948 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003949 break;
3950 }
3951
3952 if (!Opcode)
3953 return SDValue();
3954 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
3955}
3956
Dan Gohman475871a2008-07-27 21:46:04 +00003957SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003958 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003959 switch (N->getOpcode()) {
3960 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003961 case ISD::ADD: return PerformADDCombine(N, DCI);
3962 case ISD::SUB: return PerformSUBCombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00003963 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003964 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003965 case ISD::SHL:
3966 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003967 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003968 case ISD::SIGN_EXTEND:
3969 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003970 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
3971 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003972 }
Dan Gohman475871a2008-07-27 21:46:04 +00003973 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003974}
3975
Bill Wendlingaf566342009-08-15 21:21:19 +00003976bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
3977 if (!Subtarget->hasV6Ops())
3978 // Pre-v6 does not support unaligned mem access.
3979 return false;
Anton Korobeynikov90cfc132010-01-30 14:08:12 +00003980 else {
3981 // v6+ may or may not support unaligned mem access depending on the system
3982 // configuration.
3983 // FIXME: This is pretty conservative. Should we provide cmdline option to
3984 // control the behaviour?
Bill Wendlingaf566342009-08-15 21:21:19 +00003985 if (!Subtarget->isTargetDarwin())
3986 return false;
3987 }
3988
3989 switch (VT.getSimpleVT().SimpleTy) {
3990 default:
3991 return false;
3992 case MVT::i8:
3993 case MVT::i16:
3994 case MVT::i32:
3995 return true;
3996 // FIXME: VLD1 etc with standard alignment is legal.
3997 }
3998}
3999
Evan Chenge6c835f2009-08-14 20:09:37 +00004000static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
4001 if (V < 0)
4002 return false;
4003
4004 unsigned Scale = 1;
4005 switch (VT.getSimpleVT().SimpleTy) {
4006 default: return false;
4007 case MVT::i1:
4008 case MVT::i8:
4009 // Scale == 1;
4010 break;
4011 case MVT::i16:
4012 // Scale == 2;
4013 Scale = 2;
4014 break;
4015 case MVT::i32:
4016 // Scale == 4;
4017 Scale = 4;
4018 break;
4019 }
4020
4021 if ((V & (Scale - 1)) != 0)
4022 return false;
4023 V /= Scale;
4024 return V == (V & ((1LL << 5) - 1));
4025}
4026
4027static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
4028 const ARMSubtarget *Subtarget) {
4029 bool isNeg = false;
4030 if (V < 0) {
4031 isNeg = true;
4032 V = - V;
4033 }
4034
4035 switch (VT.getSimpleVT().SimpleTy) {
4036 default: return false;
4037 case MVT::i1:
4038 case MVT::i8:
4039 case MVT::i16:
4040 case MVT::i32:
4041 // + imm12 or - imm8
4042 if (isNeg)
4043 return V == (V & ((1LL << 8) - 1));
4044 return V == (V & ((1LL << 12) - 1));
4045 case MVT::f32:
4046 case MVT::f64:
4047 // Same as ARM mode. FIXME: NEON?
4048 if (!Subtarget->hasVFP2())
4049 return false;
4050 if ((V & 3) != 0)
4051 return false;
4052 V >>= 2;
4053 return V == (V & ((1LL << 8) - 1));
4054 }
4055}
4056
Evan Chengb01fad62007-03-12 23:30:29 +00004057/// isLegalAddressImmediate - Return true if the integer value can be used
4058/// as the offset of the target addressing mode for load / store of the
4059/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00004060static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004061 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00004062 if (V == 0)
4063 return true;
4064
Evan Cheng65011532009-03-09 19:15:00 +00004065 if (!VT.isSimple())
4066 return false;
4067
Evan Chenge6c835f2009-08-14 20:09:37 +00004068 if (Subtarget->isThumb1Only())
4069 return isLegalT1AddressImmediate(V, VT);
4070 else if (Subtarget->isThumb2())
4071 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00004072
Evan Chenge6c835f2009-08-14 20:09:37 +00004073 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00004074 if (V < 0)
4075 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00004076 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00004077 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004078 case MVT::i1:
4079 case MVT::i8:
4080 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00004081 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004082 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004083 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00004084 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004085 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004086 case MVT::f32:
4087 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00004088 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00004089 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00004090 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00004091 return false;
4092 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004093 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00004094 }
Evan Chenga8e29892007-01-19 07:51:42 +00004095}
4096
Evan Chenge6c835f2009-08-14 20:09:37 +00004097bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
4098 EVT VT) const {
4099 int Scale = AM.Scale;
4100 if (Scale < 0)
4101 return false;
4102
4103 switch (VT.getSimpleVT().SimpleTy) {
4104 default: return false;
4105 case MVT::i1:
4106 case MVT::i8:
4107 case MVT::i16:
4108 case MVT::i32:
4109 if (Scale == 1)
4110 return true;
4111 // r + r << imm
4112 Scale = Scale & ~1;
4113 return Scale == 2 || Scale == 4 || Scale == 8;
4114 case MVT::i64:
4115 // r + r
4116 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
4117 return true;
4118 return false;
4119 case MVT::isVoid:
4120 // Note, we allow "void" uses (basically, uses that aren't loads or
4121 // stores), because arm allows folding a scale into many arithmetic
4122 // operations. This should be made more precise and revisited later.
4123
4124 // Allow r << imm, but the imm has to be a multiple of two.
4125 if (Scale & 1) return false;
4126 return isPowerOf2_32(Scale);
4127 }
4128}
4129
Chris Lattner37caf8c2007-04-09 23:33:39 +00004130/// isLegalAddressingMode - Return true if the addressing mode represented
4131/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004132bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004133 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004134 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00004135 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00004136 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004137
Chris Lattner37caf8c2007-04-09 23:33:39 +00004138 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004139 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004140 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004141
Chris Lattner37caf8c2007-04-09 23:33:39 +00004142 switch (AM.Scale) {
4143 case 0: // no scale reg, must be "r+i" or "r", or "i".
4144 break;
4145 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00004146 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00004147 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004148 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00004149 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004150 // ARM doesn't support any R+R*scale+imm addr modes.
4151 if (AM.BaseOffs)
4152 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004153
Bob Wilson2c7dab12009-04-08 17:55:28 +00004154 if (!VT.isSimple())
4155 return false;
4156
Evan Chenge6c835f2009-08-14 20:09:37 +00004157 if (Subtarget->isThumb2())
4158 return isLegalT2ScaledAddressingMode(AM, VT);
4159
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004160 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00004161 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00004162 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004163 case MVT::i1:
4164 case MVT::i8:
4165 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004166 if (Scale < 0) Scale = -Scale;
4167 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004168 return true;
4169 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00004170 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00004171 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00004172 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004173 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004174 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004175 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00004176 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004177
Owen Anderson825b72b2009-08-11 20:47:22 +00004178 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004179 // Note, we allow "void" uses (basically, uses that aren't loads or
4180 // stores), because arm allows folding a scale into many arithmetic
4181 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004182
Chris Lattner37caf8c2007-04-09 23:33:39 +00004183 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00004184 if (Scale & 1) return false;
4185 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00004186 }
4187 break;
Evan Chengb01fad62007-03-12 23:30:29 +00004188 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00004189 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00004190}
4191
Evan Cheng77e47512009-11-11 19:05:52 +00004192/// isLegalICmpImmediate - Return true if the specified immediate is legal
4193/// icmp immediate, that is the target has icmp instructions which can compare
4194/// a register against the immediate without having to materialize the
4195/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00004196bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00004197 if (!Subtarget->isThumb())
4198 return ARM_AM::getSOImmVal(Imm) != -1;
4199 if (Subtarget->isThumb2())
4200 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00004201 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00004202}
4203
Owen Andersone50ed302009-08-10 22:56:29 +00004204static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004205 bool isSEXTLoad, SDValue &Base,
4206 SDValue &Offset, bool &isInc,
4207 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00004208 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4209 return false;
4210
Owen Anderson825b72b2009-08-11 20:47:22 +00004211 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00004212 // AddressingMode 3
4213 Base = Ptr->getOperand(0);
4214 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004215 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004216 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004217 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004218 isInc = false;
4219 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4220 return true;
4221 }
4222 }
4223 isInc = (Ptr->getOpcode() == ISD::ADD);
4224 Offset = Ptr->getOperand(1);
4225 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004226 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00004227 // AddressingMode 2
4228 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004229 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004230 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004231 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004232 isInc = false;
4233 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4234 Base = Ptr->getOperand(0);
4235 return true;
4236 }
4237 }
4238
4239 if (Ptr->getOpcode() == ISD::ADD) {
4240 isInc = true;
4241 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
4242 if (ShOpcVal != ARM_AM::no_shift) {
4243 Base = Ptr->getOperand(1);
4244 Offset = Ptr->getOperand(0);
4245 } else {
4246 Base = Ptr->getOperand(0);
4247 Offset = Ptr->getOperand(1);
4248 }
4249 return true;
4250 }
4251
4252 isInc = (Ptr->getOpcode() == ISD::ADD);
4253 Base = Ptr->getOperand(0);
4254 Offset = Ptr->getOperand(1);
4255 return true;
4256 }
4257
Jim Grosbache5165492009-11-09 00:11:35 +00004258 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00004259 return false;
4260}
4261
Owen Andersone50ed302009-08-10 22:56:29 +00004262static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004263 bool isSEXTLoad, SDValue &Base,
4264 SDValue &Offset, bool &isInc,
4265 SelectionDAG &DAG) {
4266 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4267 return false;
4268
4269 Base = Ptr->getOperand(0);
4270 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
4271 int RHSC = (int)RHS->getZExtValue();
4272 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
4273 assert(Ptr->getOpcode() == ISD::ADD);
4274 isInc = false;
4275 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4276 return true;
4277 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
4278 isInc = Ptr->getOpcode() == ISD::ADD;
4279 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
4280 return true;
4281 }
4282 }
4283
4284 return false;
4285}
4286
Evan Chenga8e29892007-01-19 07:51:42 +00004287/// getPreIndexedAddressParts - returns true by value, base pointer and
4288/// offset pointer and addressing mode by reference if the node's address
4289/// can be legally represented as pre-indexed load / store address.
4290bool
Dan Gohman475871a2008-07-27 21:46:04 +00004291ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
4292 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004293 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004294 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004295 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004296 return false;
4297
Owen Andersone50ed302009-08-10 22:56:29 +00004298 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004299 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004300 bool isSEXTLoad = false;
4301 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4302 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004303 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004304 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4305 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
4306 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004307 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004308 } else
4309 return false;
4310
4311 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004312 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004313 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004314 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
4315 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004316 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004317 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00004318 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00004319 if (!isLegal)
4320 return false;
4321
4322 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
4323 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004324}
4325
4326/// getPostIndexedAddressParts - returns true by value, base pointer and
4327/// offset pointer and addressing mode by reference if this node can be
4328/// combined with a load / store to form a post-indexed load / store.
4329bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00004330 SDValue &Base,
4331 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004332 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004333 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004334 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004335 return false;
4336
Owen Andersone50ed302009-08-10 22:56:29 +00004337 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004338 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004339 bool isSEXTLoad = false;
4340 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004341 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004342 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4343 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004344 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004345 } else
4346 return false;
4347
4348 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004349 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004350 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004351 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004352 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004353 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004354 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
4355 isInc, DAG);
4356 if (!isLegal)
4357 return false;
4358
4359 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
4360 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004361}
4362
Dan Gohman475871a2008-07-27 21:46:04 +00004363void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00004364 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004365 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004366 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00004367 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00004368 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004369 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004370 switch (Op.getOpcode()) {
4371 default: break;
4372 case ARMISD::CMOV: {
4373 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00004374 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004375 if (KnownZero == 0 && KnownOne == 0) return;
4376
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004377 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00004378 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
4379 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004380 KnownZero &= KnownZeroRHS;
4381 KnownOne &= KnownOneRHS;
4382 return;
4383 }
4384 }
4385}
4386
4387//===----------------------------------------------------------------------===//
4388// ARM Inline Assembly Support
4389//===----------------------------------------------------------------------===//
4390
4391/// getConstraintType - Given a constraint letter, return the type of
4392/// constraint it is for this target.
4393ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00004394ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
4395 if (Constraint.size() == 1) {
4396 switch (Constraint[0]) {
4397 default: break;
4398 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004399 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00004400 }
Evan Chenga8e29892007-01-19 07:51:42 +00004401 }
Chris Lattner4234f572007-03-25 02:14:49 +00004402 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00004403}
4404
Bob Wilson2dc4f542009-03-20 22:42:55 +00004405std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00004406ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004407 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004408 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004409 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00004410 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004411 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004412 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004413 return std::make_pair(0U, ARM::tGPRRegisterClass);
4414 else
4415 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004416 case 'r':
4417 return std::make_pair(0U, ARM::GPRRegisterClass);
4418 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004419 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004420 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00004421 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004422 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00004423 if (VT.getSizeInBits() == 128)
4424 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004425 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004426 }
4427 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00004428 if (StringRef("{cc}").equals_lower(Constraint))
4429 return std::make_pair(0U, ARM::CCRRegisterClass);
4430
Evan Chenga8e29892007-01-19 07:51:42 +00004431 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
4432}
4433
4434std::vector<unsigned> ARMTargetLowering::
4435getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004436 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004437 if (Constraint.size() != 1)
4438 return std::vector<unsigned>();
4439
4440 switch (Constraint[0]) { // GCC ARM Constraint Letters
4441 default: break;
4442 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004443 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4444 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4445 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004446 case 'r':
4447 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4448 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4449 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
4450 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004451 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004452 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004453 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
4454 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
4455 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
4456 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
4457 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
4458 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
4459 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
4460 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00004461 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004462 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
4463 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
4464 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
4465 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00004466 if (VT.getSizeInBits() == 128)
4467 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
4468 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004469 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004470 }
4471
4472 return std::vector<unsigned>();
4473}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004474
4475/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
4476/// vector. If it is invalid, don't add anything to Ops.
4477void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
4478 char Constraint,
4479 bool hasMemory,
4480 std::vector<SDValue>&Ops,
4481 SelectionDAG &DAG) const {
4482 SDValue Result(0, 0);
4483
4484 switch (Constraint) {
4485 default: break;
4486 case 'I': case 'J': case 'K': case 'L':
4487 case 'M': case 'N': case 'O':
4488 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
4489 if (!C)
4490 return;
4491
4492 int64_t CVal64 = C->getSExtValue();
4493 int CVal = (int) CVal64;
4494 // None of these constraints allow values larger than 32 bits. Check
4495 // that the value fits in an int.
4496 if (CVal != CVal64)
4497 return;
4498
4499 switch (Constraint) {
4500 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004501 if (Subtarget->isThumb1Only()) {
4502 // This must be a constant between 0 and 255, for ADD
4503 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004504 if (CVal >= 0 && CVal <= 255)
4505 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004506 } else if (Subtarget->isThumb2()) {
4507 // A constant that can be used as an immediate value in a
4508 // data-processing instruction.
4509 if (ARM_AM::getT2SOImmVal(CVal) != -1)
4510 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004511 } else {
4512 // A constant that can be used as an immediate value in a
4513 // data-processing instruction.
4514 if (ARM_AM::getSOImmVal(CVal) != -1)
4515 break;
4516 }
4517 return;
4518
4519 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004520 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004521 // This must be a constant between -255 and -1, for negated ADD
4522 // immediates. This can be used in GCC with an "n" modifier that
4523 // prints the negated value, for use with SUB instructions. It is
4524 // not useful otherwise but is implemented for compatibility.
4525 if (CVal >= -255 && CVal <= -1)
4526 break;
4527 } else {
4528 // This must be a constant between -4095 and 4095. It is not clear
4529 // what this constraint is intended for. Implemented for
4530 // compatibility with GCC.
4531 if (CVal >= -4095 && CVal <= 4095)
4532 break;
4533 }
4534 return;
4535
4536 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004537 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004538 // A 32-bit value where only one byte has a nonzero value. Exclude
4539 // zero to match GCC. This constraint is used by GCC internally for
4540 // constants that can be loaded with a move/shift combination.
4541 // It is not useful otherwise but is implemented for compatibility.
4542 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
4543 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004544 } else if (Subtarget->isThumb2()) {
4545 // A constant whose bitwise inverse can be used as an immediate
4546 // value in a data-processing instruction. This can be used in GCC
4547 // with a "B" modifier that prints the inverted value, for use with
4548 // BIC and MVN instructions. It is not useful otherwise but is
4549 // implemented for compatibility.
4550 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
4551 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004552 } else {
4553 // A constant whose bitwise inverse can be used as an immediate
4554 // value in a data-processing instruction. This can be used in GCC
4555 // with a "B" modifier that prints the inverted value, for use with
4556 // BIC and MVN instructions. It is not useful otherwise but is
4557 // implemented for compatibility.
4558 if (ARM_AM::getSOImmVal(~CVal) != -1)
4559 break;
4560 }
4561 return;
4562
4563 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004564 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004565 // This must be a constant between -7 and 7,
4566 // for 3-operand ADD/SUB immediate instructions.
4567 if (CVal >= -7 && CVal < 7)
4568 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004569 } else if (Subtarget->isThumb2()) {
4570 // A constant whose negation can be used as an immediate value in a
4571 // data-processing instruction. This can be used in GCC with an "n"
4572 // modifier that prints the negated value, for use with SUB
4573 // instructions. It is not useful otherwise but is implemented for
4574 // compatibility.
4575 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
4576 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004577 } else {
4578 // A constant whose negation can be used as an immediate value in a
4579 // data-processing instruction. This can be used in GCC with an "n"
4580 // modifier that prints the negated value, for use with SUB
4581 // instructions. It is not useful otherwise but is implemented for
4582 // compatibility.
4583 if (ARM_AM::getSOImmVal(-CVal) != -1)
4584 break;
4585 }
4586 return;
4587
4588 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004589 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004590 // This must be a multiple of 4 between 0 and 1020, for
4591 // ADD sp + immediate.
4592 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
4593 break;
4594 } else {
4595 // A power of two or a constant between 0 and 32. This is used in
4596 // GCC for the shift amount on shifted register operands, but it is
4597 // useful in general for any shift amounts.
4598 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
4599 break;
4600 }
4601 return;
4602
4603 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004604 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004605 // This must be a constant between 0 and 31, for shift amounts.
4606 if (CVal >= 0 && CVal <= 31)
4607 break;
4608 }
4609 return;
4610
4611 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004612 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004613 // This must be a multiple of 4 between -508 and 508, for
4614 // ADD/SUB sp = sp + immediate.
4615 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
4616 break;
4617 }
4618 return;
4619 }
4620 Result = DAG.getTargetConstant(CVal, Op.getValueType());
4621 break;
4622 }
4623
4624 if (Result.getNode()) {
4625 Ops.push_back(Result);
4626 return;
4627 }
4628 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
4629 Ops, DAG);
4630}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00004631
4632bool
4633ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4634 // The ARM target isn't yet aware of offsets.
4635 return false;
4636}
Evan Cheng39382422009-10-28 01:44:26 +00004637
4638int ARM::getVFPf32Imm(const APFloat &FPImm) {
4639 APInt Imm = FPImm.bitcastToAPInt();
4640 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
4641 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
4642 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
4643
4644 // We can handle 4 bits of mantissa.
4645 // mantissa = (16+UInt(e:f:g:h))/16.
4646 if (Mantissa & 0x7ffff)
4647 return -1;
4648 Mantissa >>= 19;
4649 if ((Mantissa & 0xf) != Mantissa)
4650 return -1;
4651
4652 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4653 if (Exp < -3 || Exp > 4)
4654 return -1;
4655 Exp = ((Exp+3) & 0x7) ^ 4;
4656
4657 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4658}
4659
4660int ARM::getVFPf64Imm(const APFloat &FPImm) {
4661 APInt Imm = FPImm.bitcastToAPInt();
4662 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
4663 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
4664 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
4665
4666 // We can handle 4 bits of mantissa.
4667 // mantissa = (16+UInt(e:f:g:h))/16.
4668 if (Mantissa & 0xffffffffffffLL)
4669 return -1;
4670 Mantissa >>= 48;
4671 if ((Mantissa & 0xf) != Mantissa)
4672 return -1;
4673
4674 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4675 if (Exp < -3 || Exp > 4)
4676 return -1;
4677 Exp = ((Exp+3) & 0x7) ^ 4;
4678
4679 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4680}
4681
4682/// isFPImmLegal - Returns true if the target can instruction select the
4683/// specified FP immediate natively. If false, the legalizer will
4684/// materialize the FP immediate as a load from a constant pool.
4685bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
4686 if (!Subtarget->hasVFP3())
4687 return false;
4688 if (VT == MVT::f32)
4689 return ARM::getVFPf32Imm(Imm) != -1;
4690 if (VT == MVT::f64)
4691 return ARM::getVFPf64Imm(Imm) != -1;
4692 return false;
4693}