blob: c284a7e04396e6ab23dc3a21407b08ef8e85003a [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "ARM.h"
16#include "ARMAddressingModes.h"
17#include "ARMConstantPoolValue.h"
18#include "ARMISelLowering.h"
19#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000020#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000021#include "ARMRegisterInfo.h"
22#include "ARMSubtarget.h"
23#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000024#include "ARMTargetObjectFile.h"
Evan Chenga8e29892007-01-19 07:51:42 +000025#include "llvm/CallingConv.h"
26#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000027#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000028#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000029#include "llvm/Instruction.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000030#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000031#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000032#include "llvm/CodeGen/CallingConvLower.h"
Evan Chenga8e29892007-01-19 07:51:42 +000033#include "llvm/CodeGen/MachineBasicBlock.h"
34#include "llvm/CodeGen/MachineFrameInfo.h"
35#include "llvm/CodeGen/MachineFunction.h"
36#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000039#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000040#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000041#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000042#include "llvm/ADT/VectorExtras.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000043#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000044#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000045#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000046#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000047#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000048using namespace llvm;
49
Jim Grosbache7b52522010-04-14 22:28:31 +000050static cl::opt<bool>
51EnableARMLongCalls("arm-long-calls", cl::Hidden,
52 cl::desc("Generate calls via indirect call instructions."),
53 cl::init(false));
54
Owen Andersone50ed302009-08-10 22:56:29 +000055static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000056 CCValAssign::LocInfo &LocInfo,
57 ISD::ArgFlagsTy &ArgFlags,
58 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000059static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000060 CCValAssign::LocInfo &LocInfo,
61 ISD::ArgFlagsTy &ArgFlags,
62 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000063static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000064 CCValAssign::LocInfo &LocInfo,
65 ISD::ArgFlagsTy &ArgFlags,
66 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000067static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000068 CCValAssign::LocInfo &LocInfo,
69 ISD::ArgFlagsTy &ArgFlags,
70 CCState &State);
71
Owen Andersone50ed302009-08-10 22:56:29 +000072void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
73 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000074 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000075 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000076 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
77 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000078
Owen Anderson70671842009-08-10 20:18:46 +000079 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000080 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000081 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000082 }
83
Owen Andersone50ed302009-08-10 22:56:29 +000084 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +000085 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +000086 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +000087 if (ElemTy == MVT::i8 || ElemTy == MVT::i16)
Owen Anderson70671842009-08-10 20:18:46 +000088 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +000089 if (ElemTy != MVT::i32) {
90 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
91 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
92 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
93 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
94 }
Owen Anderson70671842009-08-10 20:18:46 +000095 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
96 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Evan Chengde8aa4e2010-05-05 18:28:36 +000097 if (llvm::ModelWithRegSequence())
98 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
99 else
100 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Custom);
Anton Korobeynikov8e6c2b92009-08-21 12:40:35 +0000101 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
Bob Wilsond0910c42010-04-06 22:02:24 +0000102 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
103 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000104 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000105 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
106 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
107 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000108 }
109
110 // Promote all bit-wise operations.
111 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000112 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000113 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
114 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000115 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000116 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000117 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000118 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000119 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000120 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000121 }
Bob Wilson16330762009-09-16 00:17:28 +0000122
123 // Neon does not support vector divide/remainder operations.
124 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
125 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
126 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
127 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
128 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
129 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000130}
131
Owen Andersone50ed302009-08-10 22:56:29 +0000132void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000133 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000135}
136
Owen Andersone50ed302009-08-10 22:56:29 +0000137void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000138 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000140}
141
Chris Lattnerf0144122009-07-28 03:13:23 +0000142static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
143 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000144 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000145
Chris Lattner80ec2792009-08-02 00:34:36 +0000146 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000147}
148
Evan Chenga8e29892007-01-19 07:51:42 +0000149ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000150 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000151 Subtarget = &TM.getSubtarget<ARMSubtarget>();
152
Evan Chengb1df8f22007-04-27 08:15:43 +0000153 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000154 // Uses VFP for Thumb libfuncs if available.
155 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
156 // Single-precision floating-point arithmetic.
157 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
158 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
159 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
160 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000161
Evan Chengb1df8f22007-04-27 08:15:43 +0000162 // Double-precision floating-point arithmetic.
163 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
164 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
165 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
166 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000167
Evan Chengb1df8f22007-04-27 08:15:43 +0000168 // Single-precision comparisons.
169 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
170 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
171 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
172 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
173 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
174 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
175 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
176 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000177
Evan Chengb1df8f22007-04-27 08:15:43 +0000178 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
179 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
180 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
181 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
182 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
183 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
184 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
185 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000186
Evan Chengb1df8f22007-04-27 08:15:43 +0000187 // Double-precision comparisons.
188 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
189 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
190 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
191 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
192 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
193 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
194 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
195 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000196
Evan Chengb1df8f22007-04-27 08:15:43 +0000197 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
198 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
199 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
200 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
201 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
202 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
203 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
204 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000205
Evan Chengb1df8f22007-04-27 08:15:43 +0000206 // Floating-point to integer conversions.
207 // i64 conversions are done via library routines even when generating VFP
208 // instructions, so use the same ones.
209 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
210 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
211 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
212 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000213
Evan Chengb1df8f22007-04-27 08:15:43 +0000214 // Conversions between floating types.
215 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
216 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
217
218 // Integer to floating-point conversions.
219 // i64 conversions are done via library routines even when generating VFP
220 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000221 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
222 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000223 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
224 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
225 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
226 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
227 }
Evan Chenga8e29892007-01-19 07:51:42 +0000228 }
229
Bob Wilson2f954612009-05-22 17:38:41 +0000230 // These libcalls are not available in 32-bit.
231 setLibcallName(RTLIB::SHL_I128, 0);
232 setLibcallName(RTLIB::SRL_I128, 0);
233 setLibcallName(RTLIB::SRA_I128, 0);
234
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000235 // Libcalls should use the AAPCS base standard ABI, even if hard float
236 // is in effect, as per the ARM RTABI specification, section 4.1.2.
237 if (Subtarget->isAAPCS_ABI()) {
238 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
239 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
240 CallingConv::ARM_AAPCS);
241 }
242 }
243
David Goodwinf1daf7d2009-07-08 23:10:31 +0000244 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000245 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000246 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000247 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000248 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
250 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000251
Owen Anderson825b72b2009-08-11 20:47:22 +0000252 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000253 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000254
255 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000256 addDRTypeForNEON(MVT::v2f32);
257 addDRTypeForNEON(MVT::v8i8);
258 addDRTypeForNEON(MVT::v4i16);
259 addDRTypeForNEON(MVT::v2i32);
260 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000261
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 addQRTypeForNEON(MVT::v4f32);
263 addQRTypeForNEON(MVT::v2f64);
264 addQRTypeForNEON(MVT::v16i8);
265 addQRTypeForNEON(MVT::v8i16);
266 addQRTypeForNEON(MVT::v4i32);
267 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000268
Bob Wilson74dc72e2009-09-15 23:55:57 +0000269 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
270 // neither Neon nor VFP support any arithmetic operations on it.
271 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
272 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
273 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
274 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
275 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
276 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
277 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
278 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
279 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
280 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
281 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
282 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
283 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
284 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
285 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
286 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
287 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
288 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
289 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
290 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
291 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
292 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
293 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
294 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
295
Bob Wilson642b3292009-09-16 00:32:15 +0000296 // Neon does not support some operations on v1i64 and v2i64 types.
297 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
298 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
299 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
300 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
301
Bob Wilson5bafff32009-06-22 23:27:02 +0000302 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
303 setTargetDAGCombine(ISD::SHL);
304 setTargetDAGCombine(ISD::SRL);
305 setTargetDAGCombine(ISD::SRA);
306 setTargetDAGCombine(ISD::SIGN_EXTEND);
307 setTargetDAGCombine(ISD::ZERO_EXTEND);
308 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000309 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson5bafff32009-06-22 23:27:02 +0000310 }
311
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000312 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000313
314 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000315 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000316
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000317 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000319
Evan Chenga8e29892007-01-19 07:51:42 +0000320 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000321 if (!Subtarget->isThumb1Only()) {
322 for (unsigned im = (unsigned)ISD::PRE_INC;
323 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setIndexedLoadAction(im, MVT::i1, Legal);
325 setIndexedLoadAction(im, MVT::i8, Legal);
326 setIndexedLoadAction(im, MVT::i16, Legal);
327 setIndexedLoadAction(im, MVT::i32, Legal);
328 setIndexedStoreAction(im, MVT::i1, Legal);
329 setIndexedStoreAction(im, MVT::i8, Legal);
330 setIndexedStoreAction(im, MVT::i16, Legal);
331 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000332 }
Evan Chenga8e29892007-01-19 07:51:42 +0000333 }
334
335 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000336 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 setOperationAction(ISD::MUL, MVT::i64, Expand);
338 setOperationAction(ISD::MULHU, MVT::i32, Expand);
339 setOperationAction(ISD::MULHS, MVT::i32, Expand);
340 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
341 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000342 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 setOperationAction(ISD::MUL, MVT::i64, Expand);
344 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000345 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000346 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000347 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000348 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000349 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000350 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000351 setOperationAction(ISD::SRL, MVT::i64, Custom);
352 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000353
354 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000356 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000357 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000358 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000359 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000360
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000361 // Only ARMv6 has BSWAP.
362 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000363 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000364
Evan Chenga8e29892007-01-19 07:51:42 +0000365 // These are expanded into libcalls.
Jim Grosbach29402132010-05-05 23:44:43 +0000366 if (!Subtarget->hasDivide()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000367 // v7M has a hardware divider
368 setOperationAction(ISD::SDIV, MVT::i32, Expand);
369 setOperationAction(ISD::UDIV, MVT::i32, Expand);
370 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 setOperationAction(ISD::SREM, MVT::i32, Expand);
372 setOperationAction(ISD::UREM, MVT::i32, Expand);
373 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
374 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000375
Owen Anderson825b72b2009-08-11 20:47:22 +0000376 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
377 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
378 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
379 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000380 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000381
Evan Chengfb3611d2010-05-11 07:26:32 +0000382 setOperationAction(ISD::TRAP, MVT::Other, Legal);
383
Evan Chenga8e29892007-01-19 07:51:42 +0000384 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 setOperationAction(ISD::VASTART, MVT::Other, Custom);
386 setOperationAction(ISD::VAARG, MVT::Other, Expand);
387 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
388 setOperationAction(ISD::VAEND, MVT::Other, Expand);
389 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
390 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000391 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
392 // FIXME: Shouldn't need this, since no register is used, but the legalizer
393 // doesn't yet know how to not do that for SjLj.
394 setExceptionSelectorRegister(ARM::R0);
Evan Cheng3a1588a2010-04-15 22:20:34 +0000395 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Jim Grosbach3728e962009-12-10 00:11:09 +0000396 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000397
Jim Grosbach4b77f6a2010-05-07 18:34:55 +0000398 // If the subtarget does not have extract instructions, sign_extend_inreg
399 // needs to be expanded. Extract is available in ARM mode on v6 and up,
400 // and on most Thumb2 implementations.
401 if ((!Subtarget->isThumb() && !Subtarget->hasV6Ops())
402 || (Subtarget->isThumb2() && !Subtarget->hasT2ExtractPack())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000403 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
404 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000405 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000406 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000407
David Goodwinf1daf7d2009-07-08 23:10:31 +0000408 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000409 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
410 // iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000412
413 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000415
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::SETCC, MVT::i32, Expand);
417 setOperationAction(ISD::SETCC, MVT::f32, Expand);
418 setOperationAction(ISD::SETCC, MVT::f64, Expand);
419 setOperationAction(ISD::SELECT, MVT::i32, Expand);
420 setOperationAction(ISD::SELECT, MVT::f32, Expand);
421 setOperationAction(ISD::SELECT, MVT::f64, Expand);
422 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
423 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
424 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000425
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
427 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
428 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
429 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
430 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000431
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000432 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setOperationAction(ISD::FSIN, MVT::f64, Expand);
434 setOperationAction(ISD::FSIN, MVT::f32, Expand);
435 setOperationAction(ISD::FCOS, MVT::f32, Expand);
436 setOperationAction(ISD::FCOS, MVT::f64, Expand);
437 setOperationAction(ISD::FREM, MVT::f64, Expand);
438 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000439 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000440 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
441 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000442 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setOperationAction(ISD::FPOW, MVT::f64, Expand);
444 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000445
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000446 // Various VFP goodness
447 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000448 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
449 if (Subtarget->hasVFP2()) {
450 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
451 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
452 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
453 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
454 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000455 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000456 if (!Subtarget->hasFP16()) {
457 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
458 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000459 }
Evan Cheng110cf482008-04-01 01:50:16 +0000460 }
Evan Chenga8e29892007-01-19 07:51:42 +0000461
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000462 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000463 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000464 setTargetDAGCombine(ISD::ADD);
465 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000466 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000467
Evan Chenga8e29892007-01-19 07:51:42 +0000468 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Chenga8e29892007-01-19 07:51:42 +0000469 setSchedulingPreference(SchedulingForRegPressure);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000470
Evan Chengbc9b7542009-08-15 07:59:10 +0000471 // FIXME: If-converter should use instruction latency to determine
472 // profitability rather than relying on fixed limits.
473 if (Subtarget->getCPUString() == "generic") {
474 // Generic (and overly aggressive) if-conversion limits.
475 setIfCvtBlockSizeLimit(10);
476 setIfCvtDupBlockSizeLimit(2);
Jim Grosbach35075a72010-03-24 16:15:14 +0000477 } else if (Subtarget->hasV7Ops()) {
Jim Grosbachfceabef2010-03-24 00:03:13 +0000478 setIfCvtBlockSizeLimit(3);
479 setIfCvtDupBlockSizeLimit(1);
Evan Chengbc9b7542009-08-15 07:59:10 +0000480 } else if (Subtarget->hasV6Ops()) {
481 setIfCvtBlockSizeLimit(2);
482 setIfCvtDupBlockSizeLimit(1);
483 } else {
484 setIfCvtBlockSizeLimit(3);
485 setIfCvtDupBlockSizeLimit(2);
Evan Cheng8557c2b2009-06-19 01:51:50 +0000486 }
487
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000488 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Bob Wilsone6abdff2009-05-18 20:55:32 +0000489 // Do not enable CodePlacementOpt for now: it currently runs after the
490 // ARMConstantIslandPass and messes up branch relaxation and placement
491 // of constant islands.
492 // benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000493}
494
Evan Chenga8e29892007-01-19 07:51:42 +0000495const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
496 switch (Opcode) {
497 default: return 0;
498 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000499 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
500 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000501 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000502 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
503 case ARMISD::tCALL: return "ARMISD::tCALL";
504 case ARMISD::BRCOND: return "ARMISD::BRCOND";
505 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000506 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000507 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
508 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
509 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000510 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000511 case ARMISD::CMPFP: return "ARMISD::CMPFP";
512 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
513 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
514 case ARMISD::CMOV: return "ARMISD::CMOV";
515 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000516
Jim Grosbach3482c802010-01-18 19:58:49 +0000517 case ARMISD::RBIT: return "ARMISD::RBIT";
518
Bob Wilson76a312b2010-03-19 22:51:32 +0000519 case ARMISD::FTOSI: return "ARMISD::FTOSI";
520 case ARMISD::FTOUI: return "ARMISD::FTOUI";
521 case ARMISD::SITOF: return "ARMISD::SITOF";
522 case ARMISD::UITOF: return "ARMISD::UITOF";
523
Evan Chenga8e29892007-01-19 07:51:42 +0000524 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
525 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
526 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000527
Jim Grosbache5165492009-11-09 00:11:35 +0000528 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
529 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000530
Evan Chengc5942082009-10-28 06:55:03 +0000531 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
532 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
533
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000534 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000535
Evan Cheng86198642009-08-07 00:34:42 +0000536 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
537
Jim Grosbach3728e962009-12-10 00:11:09 +0000538 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
539 case ARMISD::SYNCBARRIER: return "ARMISD::SYNCBARRIER";
540
Bob Wilson5bafff32009-06-22 23:27:02 +0000541 case ARMISD::VCEQ: return "ARMISD::VCEQ";
542 case ARMISD::VCGE: return "ARMISD::VCGE";
543 case ARMISD::VCGEU: return "ARMISD::VCGEU";
544 case ARMISD::VCGT: return "ARMISD::VCGT";
545 case ARMISD::VCGTU: return "ARMISD::VCGTU";
546 case ARMISD::VTST: return "ARMISD::VTST";
547
548 case ARMISD::VSHL: return "ARMISD::VSHL";
549 case ARMISD::VSHRs: return "ARMISD::VSHRs";
550 case ARMISD::VSHRu: return "ARMISD::VSHRu";
551 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
552 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
553 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
554 case ARMISD::VSHRN: return "ARMISD::VSHRN";
555 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
556 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
557 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
558 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
559 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
560 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
561 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
562 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
563 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
564 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
565 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
566 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
567 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
568 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000569 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000570 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000571 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000572 case ARMISD::VREV64: return "ARMISD::VREV64";
573 case ARMISD::VREV32: return "ARMISD::VREV32";
574 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000575 case ARMISD::VZIP: return "ARMISD::VZIP";
576 case ARMISD::VUZP: return "ARMISD::VUZP";
577 case ARMISD::VTRN: return "ARMISD::VTRN";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000578 case ARMISD::FMAX: return "ARMISD::FMAX";
579 case ARMISD::FMIN: return "ARMISD::FMIN";
Evan Chenga8e29892007-01-19 07:51:42 +0000580 }
581}
582
Evan Cheng06b666c2010-05-15 02:18:07 +0000583/// getRegClassFor - Return the register class that should be used for the
584/// specified value type.
585TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
586 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
587 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
588 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000589 if (Subtarget->hasNEON()) {
590 if (VT == MVT::v4i64)
591 return ARM::QQPRRegisterClass;
592 else if (VT == MVT::v8i64)
593 return ARM::QQQQPRRegisterClass;
594 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000595 return TargetLowering::getRegClassFor(VT);
596}
597
Bill Wendlingb4202b82009-07-01 18:50:55 +0000598/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000599unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Evan Cheng048e36f2009-10-02 06:57:25 +0000600 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 0 : 1;
Bill Wendling20c568f2009-06-30 22:38:32 +0000601}
602
Evan Chenga8e29892007-01-19 07:51:42 +0000603//===----------------------------------------------------------------------===//
604// Lowering Code
605//===----------------------------------------------------------------------===//
606
Evan Chenga8e29892007-01-19 07:51:42 +0000607/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
608static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
609 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000610 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000611 case ISD::SETNE: return ARMCC::NE;
612 case ISD::SETEQ: return ARMCC::EQ;
613 case ISD::SETGT: return ARMCC::GT;
614 case ISD::SETGE: return ARMCC::GE;
615 case ISD::SETLT: return ARMCC::LT;
616 case ISD::SETLE: return ARMCC::LE;
617 case ISD::SETUGT: return ARMCC::HI;
618 case ISD::SETUGE: return ARMCC::HS;
619 case ISD::SETULT: return ARMCC::LO;
620 case ISD::SETULE: return ARMCC::LS;
621 }
622}
623
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000624/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
625static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000626 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000627 CondCode2 = ARMCC::AL;
628 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000629 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000630 case ISD::SETEQ:
631 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
632 case ISD::SETGT:
633 case ISD::SETOGT: CondCode = ARMCC::GT; break;
634 case ISD::SETGE:
635 case ISD::SETOGE: CondCode = ARMCC::GE; break;
636 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000637 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000638 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
639 case ISD::SETO: CondCode = ARMCC::VC; break;
640 case ISD::SETUO: CondCode = ARMCC::VS; break;
641 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
642 case ISD::SETUGT: CondCode = ARMCC::HI; break;
643 case ISD::SETUGE: CondCode = ARMCC::PL; break;
644 case ISD::SETLT:
645 case ISD::SETULT: CondCode = ARMCC::LT; break;
646 case ISD::SETLE:
647 case ISD::SETULE: CondCode = ARMCC::LE; break;
648 case ISD::SETNE:
649 case ISD::SETUNE: CondCode = ARMCC::NE; break;
650 }
Evan Chenga8e29892007-01-19 07:51:42 +0000651}
652
Bob Wilson1f595bb2009-04-17 19:07:39 +0000653//===----------------------------------------------------------------------===//
654// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000655//===----------------------------------------------------------------------===//
656
657#include "ARMGenCallingConv.inc"
658
659// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000660static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000661 CCValAssign::LocInfo &LocInfo,
662 CCState &State, bool CanFail) {
663 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
664
665 // Try to get the first register.
666 if (unsigned Reg = State.AllocateReg(RegList, 4))
667 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
668 else {
669 // For the 2nd half of a v2f64, do not fail.
670 if (CanFail)
671 return false;
672
673 // Put the whole thing on the stack.
674 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
675 State.AllocateStack(8, 4),
676 LocVT, LocInfo));
677 return true;
678 }
679
680 // Try to get the second register.
681 if (unsigned Reg = State.AllocateReg(RegList, 4))
682 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
683 else
684 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
685 State.AllocateStack(4, 4),
686 LocVT, LocInfo));
687 return true;
688}
689
Owen Andersone50ed302009-08-10 22:56:29 +0000690static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000691 CCValAssign::LocInfo &LocInfo,
692 ISD::ArgFlagsTy &ArgFlags,
693 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000694 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
695 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000696 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000697 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
698 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000699 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000700}
701
702// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000703static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000704 CCValAssign::LocInfo &LocInfo,
705 CCState &State, bool CanFail) {
706 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
707 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
708
709 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
710 if (Reg == 0) {
711 // For the 2nd half of a v2f64, do not just fail.
712 if (CanFail)
713 return false;
714
715 // Put the whole thing on the stack.
716 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
717 State.AllocateStack(8, 8),
718 LocVT, LocInfo));
719 return true;
720 }
721
722 unsigned i;
723 for (i = 0; i < 2; ++i)
724 if (HiRegList[i] == Reg)
725 break;
726
727 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
728 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
729 LocVT, LocInfo));
730 return true;
731}
732
Owen Andersone50ed302009-08-10 22:56:29 +0000733static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000734 CCValAssign::LocInfo &LocInfo,
735 ISD::ArgFlagsTy &ArgFlags,
736 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000737 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
738 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000739 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000740 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
741 return false;
742 return true; // we handled it
743}
744
Owen Andersone50ed302009-08-10 22:56:29 +0000745static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000746 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000747 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
748 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
749
Bob Wilsone65586b2009-04-17 20:40:45 +0000750 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
751 if (Reg == 0)
752 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000753
Bob Wilsone65586b2009-04-17 20:40:45 +0000754 unsigned i;
755 for (i = 0; i < 2; ++i)
756 if (HiRegList[i] == Reg)
757 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000758
Bob Wilson5bafff32009-06-22 23:27:02 +0000759 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000760 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000761 LocVT, LocInfo));
762 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000763}
764
Owen Andersone50ed302009-08-10 22:56:29 +0000765static bool RetCC_ARM_APCS_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) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000769 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
770 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000771 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000772 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000773 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000774}
775
Owen Andersone50ed302009-08-10 22:56:29 +0000776static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000777 CCValAssign::LocInfo &LocInfo,
778 ISD::ArgFlagsTy &ArgFlags,
779 CCState &State) {
780 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
781 State);
782}
783
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000784/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
785/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000786CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000787 bool Return,
788 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000789 switch (CC) {
790 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000791 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000792 case CallingConv::C:
793 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000794 // Use target triple & subtarget features to do actual dispatch.
795 if (Subtarget->isAAPCS_ABI()) {
796 if (Subtarget->hasVFP2() &&
797 FloatABIType == FloatABI::Hard && !isVarArg)
798 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
799 else
800 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
801 } else
802 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000803 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000804 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000805 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000806 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000807 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000808 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000809 }
810}
811
Dan Gohman98ca4f22009-08-05 01:29:28 +0000812/// LowerCallResult - Lower the result values of a call into the
813/// appropriate copies out of appropriate physical registers.
814SDValue
815ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000816 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000817 const SmallVectorImpl<ISD::InputArg> &Ins,
818 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000819 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000820
Bob Wilson1f595bb2009-04-17 19:07:39 +0000821 // Assign locations to each value returned by this call.
822 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000823 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000824 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000825 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000826 CCAssignFnForNode(CallConv, /* Return*/ true,
827 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000828
829 // Copy all of the result registers out of their specified physreg.
830 for (unsigned i = 0; i != RVLocs.size(); ++i) {
831 CCValAssign VA = RVLocs[i];
832
Bob Wilson80915242009-04-25 00:33:20 +0000833 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000834 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000835 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000836 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000837 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000838 Chain = Lo.getValue(1);
839 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000840 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000841 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000842 InFlag);
843 Chain = Hi.getValue(1);
844 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000845 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000846
Owen Anderson825b72b2009-08-11 20:47:22 +0000847 if (VA.getLocVT() == MVT::v2f64) {
848 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
849 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
850 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000851
852 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000853 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000854 Chain = Lo.getValue(1);
855 InFlag = Lo.getValue(2);
856 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000857 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000858 Chain = Hi.getValue(1);
859 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000860 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +0000861 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
862 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000863 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000864 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000865 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
866 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000867 Chain = Val.getValue(1);
868 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000869 }
Bob Wilson80915242009-04-25 00:33:20 +0000870
871 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000872 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000873 case CCValAssign::Full: break;
874 case CCValAssign::BCvt:
875 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
876 break;
877 }
878
Dan Gohman98ca4f22009-08-05 01:29:28 +0000879 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000880 }
881
Dan Gohman98ca4f22009-08-05 01:29:28 +0000882 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000883}
884
885/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
886/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000887/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000888/// a byval function parameter.
889/// Sometimes what we are copying is the end of a larger object, the part that
890/// does not fit in registers.
891static SDValue
892CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
893 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
894 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000895 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000896 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +0000897 /*isVolatile=*/false, /*AlwaysInline=*/false,
898 NULL, 0, NULL, 0);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000899}
900
Bob Wilsondee46d72009-04-17 20:35:10 +0000901/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000902SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000903ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
904 SDValue StackPtr, SDValue Arg,
905 DebugLoc dl, SelectionDAG &DAG,
906 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +0000907 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000908 unsigned LocMemOffset = VA.getLocMemOffset();
909 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
910 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
911 if (Flags.isByVal()) {
912 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
913 }
914 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene1b58cab2010-02-15 16:55:24 +0000915 PseudoSourceValue::getStack(), LocMemOffset,
916 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +0000917}
918
Dan Gohman98ca4f22009-08-05 01:29:28 +0000919void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000920 SDValue Chain, SDValue &Arg,
921 RegsToPassVector &RegsToPass,
922 CCValAssign &VA, CCValAssign &NextVA,
923 SDValue &StackPtr,
924 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +0000925 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +0000926
Jim Grosbache5165492009-11-09 00:11:35 +0000927 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000928 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000929 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
930
931 if (NextVA.isRegLoc())
932 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
933 else {
934 assert(NextVA.isMemLoc());
935 if (StackPtr.getNode() == 0)
936 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
937
Dan Gohman98ca4f22009-08-05 01:29:28 +0000938 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
939 dl, DAG, NextVA,
940 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000941 }
942}
943
Dan Gohman98ca4f22009-08-05 01:29:28 +0000944/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000945/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
946/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000947SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000948ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000949 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000950 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000951 const SmallVectorImpl<ISD::OutputArg> &Outs,
952 const SmallVectorImpl<ISD::InputArg> &Ins,
953 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000954 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +0000955 // ARM target does not yet support tail call optimization.
956 isTailCall = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000957
Bob Wilson1f595bb2009-04-17 19:07:39 +0000958 // Analyze operands of the call, assigning locations to each operand.
959 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000960 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
961 *DAG.getContext());
962 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000963 CCAssignFnForNode(CallConv, /* Return*/ false,
964 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +0000965
Bob Wilson1f595bb2009-04-17 19:07:39 +0000966 // Get a count of how many bytes are to be pushed on the stack.
967 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000968
969 // Adjust the stack pointer for the new arguments...
970 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000971 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000972
Jim Grosbachf9a4b762010-02-24 01:43:03 +0000973 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000974
Bob Wilson5bafff32009-06-22 23:27:02 +0000975 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000976 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000977
Bob Wilson1f595bb2009-04-17 19:07:39 +0000978 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +0000979 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000980 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
981 i != e;
982 ++i, ++realArgIdx) {
983 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000984 SDValue Arg = Outs[realArgIdx].Val;
985 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +0000986
Bob Wilson1f595bb2009-04-17 19:07:39 +0000987 // Promote the value if needed.
988 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000989 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +0000990 case CCValAssign::Full: break;
991 case CCValAssign::SExt:
992 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
993 break;
994 case CCValAssign::ZExt:
995 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
996 break;
997 case CCValAssign::AExt:
998 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
999 break;
1000 case CCValAssign::BCvt:
1001 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1002 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001003 }
1004
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001005 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001006 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001007 if (VA.getLocVT() == MVT::v2f64) {
1008 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1009 DAG.getConstant(0, MVT::i32));
1010 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1011 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001012
Dan Gohman98ca4f22009-08-05 01:29:28 +00001013 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001014 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1015
1016 VA = ArgLocs[++i]; // skip ahead to next loc
1017 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001018 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001019 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1020 } else {
1021 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001022
Dan Gohman98ca4f22009-08-05 01:29:28 +00001023 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1024 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001025 }
1026 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001027 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001028 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001029 }
1030 } else if (VA.isRegLoc()) {
1031 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1032 } else {
1033 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001034
Dan Gohman98ca4f22009-08-05 01:29:28 +00001035 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1036 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001037 }
Evan Chenga8e29892007-01-19 07:51:42 +00001038 }
1039
1040 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001041 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001042 &MemOpChains[0], MemOpChains.size());
1043
1044 // Build a sequence of copy-to-reg nodes chained together with token chain
1045 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001046 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +00001047 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001048 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001049 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +00001050 InFlag = Chain.getValue(1);
1051 }
1052
Bill Wendling056292f2008-09-16 21:48:12 +00001053 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1054 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1055 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001056 bool isDirect = false;
1057 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001058 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001059 MachineFunction &MF = DAG.getMachineFunction();
1060 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001061
1062 if (EnableARMLongCalls) {
1063 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1064 && "long-calls with non-static relocation model!");
1065 // Handle a global address or an external symbol. If it's not one of
1066 // those, the target's already in a register, so we don't need to do
1067 // anything extra.
1068 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001069 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001070 // Create a constant pool entry for the callee address
1071 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1072 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1073 ARMPCLabelIndex,
1074 ARMCP::CPValue, 0);
1075 // Get the address of the callee into a register
1076 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1077 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1078 Callee = DAG.getLoad(getPointerTy(), dl,
1079 DAG.getEntryNode(), CPAddr,
1080 PseudoSourceValue::getConstantPool(), 0,
1081 false, false, 0);
1082 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1083 const char *Sym = S->getSymbol();
1084
1085 // Create a constant pool entry for the callee address
1086 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1087 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1088 Sym, ARMPCLabelIndex, 0);
1089 // Get the address of the callee into a register
1090 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1091 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1092 Callee = DAG.getLoad(getPointerTy(), dl,
1093 DAG.getEntryNode(), CPAddr,
1094 PseudoSourceValue::getConstantPool(), 0,
1095 false, false, 0);
1096 }
1097 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001098 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001099 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001100 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001101 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001102 getTargetMachine().getRelocationModel() != Reloc::Static;
1103 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001104 // ARM call to a local ARM function is predicable.
1105 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +00001106 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001107 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001108 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001109 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001110 ARMPCLabelIndex,
1111 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001112 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001113 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001114 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001115 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001116 PseudoSourceValue::getConstantPool(), 0,
1117 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001118 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001119 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001120 getPointerTy(), Callee, PICLabel);
Jim Grosbache7b52522010-04-14 22:28:31 +00001121 } else
Evan Chengc60e76d2007-01-30 20:37:08 +00001122 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001123 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001124 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001125 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001126 getTargetMachine().getRelocationModel() != Reloc::Static;
1127 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001128 // tBX takes a register source operand.
1129 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001130 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001131 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001132 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001133 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001134 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001135 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001136 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001137 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001138 PseudoSourceValue::getConstantPool(), 0,
1139 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001140 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001141 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001142 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001143 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001144 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001145 }
1146
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001147 // FIXME: handle tail calls differently.
1148 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001149 if (Subtarget->isThumb()) {
1150 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001151 CallOpc = ARMISD::CALL_NOLINK;
1152 else
1153 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1154 } else {
1155 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001156 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1157 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001158 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001159 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001160 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001161 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001162 InFlag = Chain.getValue(1);
1163 }
1164
Dan Gohman475871a2008-07-27 21:46:04 +00001165 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001166 Ops.push_back(Chain);
1167 Ops.push_back(Callee);
1168
1169 // Add argument registers to the end of the list so that they are known live
1170 // into the call.
1171 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1172 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1173 RegsToPass[i].second.getValueType()));
1174
Gabor Greifba36cb52008-08-28 21:40:38 +00001175 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001176 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +00001177 // Returns a chain and a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00001178 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +00001179 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001180 InFlag = Chain.getValue(1);
1181
Chris Lattnere563bbc2008-10-11 22:08:30 +00001182 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1183 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001184 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001185 InFlag = Chain.getValue(1);
1186
Bob Wilson1f595bb2009-04-17 19:07:39 +00001187 // Handle result values, copying them out of physregs into vregs that we
1188 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001189 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1190 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001191}
1192
Dan Gohman98ca4f22009-08-05 01:29:28 +00001193SDValue
1194ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001195 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001196 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmand858e902010-04-17 15:26:15 +00001197 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001198
Bob Wilsondee46d72009-04-17 20:35:10 +00001199 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001200 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001201
Bob Wilsondee46d72009-04-17 20:35:10 +00001202 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001203 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1204 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001205
Dan Gohman98ca4f22009-08-05 01:29:28 +00001206 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001207 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1208 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001209
1210 // If this is the first return lowered for this function, add
1211 // the regs to the liveout set for the function.
1212 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1213 for (unsigned i = 0; i != RVLocs.size(); ++i)
1214 if (RVLocs[i].isRegLoc())
1215 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001216 }
1217
Bob Wilson1f595bb2009-04-17 19:07:39 +00001218 SDValue Flag;
1219
1220 // Copy the result values into the output registers.
1221 for (unsigned i = 0, realRVLocIdx = 0;
1222 i != RVLocs.size();
1223 ++i, ++realRVLocIdx) {
1224 CCValAssign &VA = RVLocs[i];
1225 assert(VA.isRegLoc() && "Can only return in registers!");
1226
Dan Gohman98ca4f22009-08-05 01:29:28 +00001227 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001228
1229 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001230 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001231 case CCValAssign::Full: break;
1232 case CCValAssign::BCvt:
1233 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1234 break;
1235 }
1236
Bob Wilson1f595bb2009-04-17 19:07:39 +00001237 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001238 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001239 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001240 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1241 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001242 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001243 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001244
1245 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1246 Flag = Chain.getValue(1);
1247 VA = RVLocs[++i]; // skip ahead to next loc
1248 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1249 HalfGPRs.getValue(1), Flag);
1250 Flag = Chain.getValue(1);
1251 VA = RVLocs[++i]; // skip ahead to next loc
1252
1253 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001254 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1255 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001256 }
1257 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1258 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001259 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001260 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001261 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001262 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001263 VA = RVLocs[++i]; // skip ahead to next loc
1264 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1265 Flag);
1266 } else
1267 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1268
Bob Wilsondee46d72009-04-17 20:35:10 +00001269 // Guarantee that all emitted copies are
1270 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001271 Flag = Chain.getValue(1);
1272 }
1273
1274 SDValue result;
1275 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001276 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001277 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001278 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001279
1280 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001281}
1282
Bob Wilsonb62d2572009-11-03 00:02:05 +00001283// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1284// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1285// one of the above mentioned nodes. It has to be wrapped because otherwise
1286// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1287// be used to form addressing mode. These wrapped nodes will be selected
1288// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001289static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001290 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001291 // FIXME there is no actual debug info here
1292 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001293 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001294 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001295 if (CP->isMachineConstantPoolEntry())
1296 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1297 CP->getAlignment());
1298 else
1299 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1300 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001301 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001302}
1303
Dan Gohmand858e902010-04-17 15:26:15 +00001304SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1305 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001306 MachineFunction &MF = DAG.getMachineFunction();
1307 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1308 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001309 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001310 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001311 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001312 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1313 SDValue CPAddr;
1314 if (RelocM == Reloc::Static) {
1315 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1316 } else {
1317 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001318 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001319 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1320 ARMCP::CPBlockAddress,
1321 PCAdj);
1322 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1323 }
1324 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1325 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001326 PseudoSourceValue::getConstantPool(), 0,
1327 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001328 if (RelocM == Reloc::Static)
1329 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001330 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001331 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001332}
1333
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001334// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001335SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001336ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001337 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001338 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001339 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001340 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001341 MachineFunction &MF = DAG.getMachineFunction();
1342 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1343 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001344 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001345 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001346 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001347 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001348 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001349 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
David Greene1b58cab2010-02-15 16:55:24 +00001350 PseudoSourceValue::getConstantPool(), 0,
1351 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001352 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001353
Evan Chenge7e0d622009-11-06 22:24:13 +00001354 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001355 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001356
1357 // call __tls_get_addr.
1358 ArgListTy Args;
1359 ArgListEntry Entry;
1360 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001361 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001362 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001363 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001364 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001365 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1366 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001367 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001368 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001369 return CallResult.first;
1370}
1371
1372// Lower ISD::GlobalTLSAddress using the "initial exec" or
1373// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001374SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001375ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001376 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001377 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001378 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001379 SDValue Offset;
1380 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001381 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001382 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001383 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001384
Chris Lattner4fb63d02009-07-15 04:12:33 +00001385 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001386 MachineFunction &MF = DAG.getMachineFunction();
1387 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1388 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1389 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001390 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1391 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001392 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001393 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001394 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001395 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001396 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001397 PseudoSourceValue::getConstantPool(), 0,
1398 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001399 Chain = Offset.getValue(1);
1400
Evan Chenge7e0d622009-11-06 22:24:13 +00001401 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001402 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001403
Evan Cheng9eda6892009-10-31 03:39:36 +00001404 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001405 PseudoSourceValue::getConstantPool(), 0,
1406 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001407 } else {
1408 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001409 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001410 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001411 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001412 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001413 PseudoSourceValue::getConstantPool(), 0,
1414 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001415 }
1416
1417 // The address of the thread local variable is the add of the thread
1418 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001419 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001420}
1421
Dan Gohman475871a2008-07-27 21:46:04 +00001422SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001423ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001424 // TODO: implement the "local dynamic" model
1425 assert(Subtarget->isTargetELF() &&
1426 "TLS not implemented for non-ELF targets");
1427 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1428 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1429 // otherwise use the "Local Exec" TLS Model
1430 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1431 return LowerToTLSGeneralDynamicModel(GA, DAG);
1432 else
1433 return LowerToTLSExecModels(GA, DAG);
1434}
1435
Dan Gohman475871a2008-07-27 21:46:04 +00001436SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001437 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001438 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001439 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001440 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001441 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1442 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001443 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001444 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001445 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001446 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001447 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001448 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001449 CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001450 PseudoSourceValue::getConstantPool(), 0,
1451 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001452 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001453 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001454 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001455 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001456 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001457 PseudoSourceValue::getGOT(), 0,
1458 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001459 return Result;
1460 } else {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001461 // If we have T2 ops, we can materialize the address directly via movt/movw
1462 // pair. This is always cheaper.
1463 if (Subtarget->useMovt()) {
1464 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
1465 DAG.getTargetGlobalAddress(GV, PtrVT));
1466 } else {
1467 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1468 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1469 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001470 PseudoSourceValue::getConstantPool(), 0,
1471 false, false, 0);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001472 }
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001473 }
1474}
1475
Dan Gohman475871a2008-07-27 21:46:04 +00001476SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001477 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001478 MachineFunction &MF = DAG.getMachineFunction();
1479 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1480 unsigned ARMPCLabelIndex = 0;
Owen Andersone50ed302009-08-10 22:56:29 +00001481 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001482 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001483 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001484 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001485 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001486 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001487 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001488 else {
Evan Chenge7e0d622009-11-06 22:24:13 +00001489 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001490 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1491 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001492 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001493 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001494 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001495 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001496
Evan Cheng9eda6892009-10-31 03:39:36 +00001497 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001498 PseudoSourceValue::getConstantPool(), 0,
1499 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001500 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001501
1502 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001503 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001504 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001505 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001506
Evan Cheng63476a82009-09-03 07:04:02 +00001507 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Evan Cheng9eda6892009-10-31 03:39:36 +00001508 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001509 PseudoSourceValue::getGOT(), 0,
1510 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001511
1512 return Result;
1513}
1514
Dan Gohman475871a2008-07-27 21:46:04 +00001515SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001516 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001517 assert(Subtarget->isTargetELF() &&
1518 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00001519 MachineFunction &MF = DAG.getMachineFunction();
1520 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1521 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Andersone50ed302009-08-10 22:56:29 +00001522 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001523 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001524 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001525 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1526 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001527 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001528 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001529 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001530 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001531 PseudoSourceValue::getConstantPool(), 0,
1532 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001533 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001534 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001535}
1536
Jim Grosbach0e0da732009-05-12 23:59:14 +00001537SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00001538ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001539 const ARMSubtarget *Subtarget)
1540 const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001541 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001542 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001543 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001544 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001545 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001546 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001547 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1548 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001549 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001550 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00001551 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1552 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001553 EVT PtrVT = getPointerTy();
1554 DebugLoc dl = Op.getDebugLoc();
1555 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1556 SDValue CPAddr;
1557 unsigned PCAdj = (RelocM != Reloc::PIC_)
1558 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001559 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001560 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1561 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001562 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001563 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001564 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00001565 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001566 PseudoSourceValue::getConstantPool(), 0,
1567 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001568 SDValue Chain = Result.getValue(1);
1569
1570 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001571 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001572 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1573 }
1574 return Result;
1575 }
Jim Grosbachf9570122009-05-14 00:46:35 +00001576 case Intrinsic::eh_sjlj_setjmp:
Jim Grosbacha87ded22010-02-08 23:22:00 +00001577 SDValue Val = Subtarget->isThumb() ?
1578 DAG.getCopyFromReg(DAG.getEntryNode(), dl, ARM::SP, MVT::i32) :
1579 DAG.getConstant(0, MVT::i32);
1580 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1),
1581 Val);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001582 }
1583}
1584
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001585static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
1586 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00001587 DebugLoc dl = Op.getDebugLoc();
1588 SDValue Op5 = Op.getOperand(5);
1589 SDValue Res;
1590 unsigned isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue();
1591 if (isDeviceBarrier) {
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001592 if (Subtarget->hasV7Ops())
1593 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0));
1594 else
1595 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0),
1596 DAG.getConstant(0, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00001597 } else {
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001598 if (Subtarget->hasV7Ops())
1599 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
1600 else
1601 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
1602 DAG.getConstant(0, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00001603 }
1604 return Res;
1605}
1606
Dan Gohman1e93df62010-04-17 14:41:14 +00001607static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
1608 MachineFunction &MF = DAG.getMachineFunction();
1609 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
1610
Evan Chenga8e29892007-01-19 07:51:42 +00001611 // vastart just stores the address of the VarArgsFrameIndex slot into the
1612 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001613 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001614 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00001615 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001616 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
David Greene1b58cab2010-02-15 16:55:24 +00001617 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
1618 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001619}
1620
Dan Gohman475871a2008-07-27 21:46:04 +00001621SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001622ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1623 SelectionDAG &DAG) const {
Evan Cheng86198642009-08-07 00:34:42 +00001624 SDNode *Node = Op.getNode();
1625 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001626 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001627 SDValue Chain = Op.getOperand(0);
1628 SDValue Size = Op.getOperand(1);
1629 SDValue Align = Op.getOperand(2);
1630
1631 // Chain the dynamic stack allocation so that it doesn't modify the stack
1632 // pointer when other instructions are using the stack.
1633 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1634
1635 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1636 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1637 if (AlignVal > StackAlign)
1638 // Do this now since selection pass cannot introduce new target
1639 // independent node.
1640 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1641
1642 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1643 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1644 // do even more horrible hack later.
1645 MachineFunction &MF = DAG.getMachineFunction();
1646 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1647 if (AFI->isThumb1OnlyFunction()) {
1648 bool Negate = true;
1649 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1650 if (C) {
1651 uint32_t Val = C->getZExtValue();
1652 if (Val <= 508 && ((Val & 3) == 0))
1653 Negate = false;
1654 }
1655 if (Negate)
1656 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1657 }
1658
Owen Anderson825b72b2009-08-11 20:47:22 +00001659 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001660 SDValue Ops1[] = { Chain, Size, Align };
1661 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1662 Chain = Res.getValue(1);
1663 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1664 DAG.getIntPtrConstant(0, true), SDValue());
1665 SDValue Ops2[] = { Res, Chain };
1666 return DAG.getMergeValues(Ops2, 2, dl);
1667}
1668
1669SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001670ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1671 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001672 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001673 MachineFunction &MF = DAG.getMachineFunction();
1674 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1675
1676 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001677 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001678 RC = ARM::tGPRRegisterClass;
1679 else
1680 RC = ARM::GPRRegisterClass;
1681
1682 // Transform the arguments stored in physical registers into virtual ones.
1683 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001684 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001685
1686 SDValue ArgValue2;
1687 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001688 MachineFrameInfo *MFI = MF.getFrameInfo();
Bob Wilson6a234f02010-04-13 22:03:22 +00001689 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true, false);
Bob Wilson5bafff32009-06-22 23:27:02 +00001690
1691 // Create load node to retrieve arguments from the stack.
1692 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001693 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00001694 PseudoSourceValue::getFixedStack(FI), 0,
1695 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001696 } else {
1697 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001698 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001699 }
1700
Jim Grosbache5165492009-11-09 00:11:35 +00001701 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001702}
1703
1704SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001705ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001706 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001707 const SmallVectorImpl<ISD::InputArg>
1708 &Ins,
1709 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001710 SmallVectorImpl<SDValue> &InVals)
1711 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001712
Bob Wilson1f595bb2009-04-17 19:07:39 +00001713 MachineFunction &MF = DAG.getMachineFunction();
1714 MachineFrameInfo *MFI = MF.getFrameInfo();
1715
Bob Wilson1f595bb2009-04-17 19:07:39 +00001716 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1717
1718 // Assign locations to all of the incoming arguments.
1719 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001720 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1721 *DAG.getContext());
1722 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001723 CCAssignFnForNode(CallConv, /* Return*/ false,
1724 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001725
1726 SmallVector<SDValue, 16> ArgValues;
1727
1728 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1729 CCValAssign &VA = ArgLocs[i];
1730
Bob Wilsondee46d72009-04-17 20:35:10 +00001731 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001732 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001733 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00001734
Bob Wilson5bafff32009-06-22 23:27:02 +00001735 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001736 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001737 // f64 and vector types are split up into multiple registers or
1738 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00001739 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001740 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001741 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00001742 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00001743 SDValue ArgValue2;
1744 if (VA.isMemLoc()) {
1745 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(),
1746 true, false);
1747 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1748 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
1749 PseudoSourceValue::getFixedStack(FI), 0,
1750 false, false, 0);
1751 } else {
1752 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
1753 Chain, DAG, dl);
1754 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001755 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1756 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001757 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00001758 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001759 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1760 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001761 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001762
Bob Wilson5bafff32009-06-22 23:27:02 +00001763 } else {
1764 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001765
Owen Anderson825b72b2009-08-11 20:47:22 +00001766 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00001767 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001768 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00001769 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001770 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001771 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001772 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001773 RC = (AFI->isThumb1OnlyFunction() ?
1774 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00001775 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001776 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00001777
1778 // Transform the arguments in physical registers into virtual ones.
1779 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001780 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001781 }
1782
1783 // If this is an 8 or 16-bit value, it is really passed promoted
1784 // to 32 bits. Insert an assert[sz]ext to capture this, then
1785 // truncate to the right size.
1786 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001787 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001788 case CCValAssign::Full: break;
1789 case CCValAssign::BCvt:
1790 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1791 break;
1792 case CCValAssign::SExt:
1793 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1794 DAG.getValueType(VA.getValVT()));
1795 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1796 break;
1797 case CCValAssign::ZExt:
1798 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1799 DAG.getValueType(VA.getValVT()));
1800 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1801 break;
1802 }
1803
Dan Gohman98ca4f22009-08-05 01:29:28 +00001804 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001805
1806 } else { // VA.isRegLoc()
1807
1808 // sanity check
1809 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00001810 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001811
1812 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001813 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
1814 true, false);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001815
Bob Wilsondee46d72009-04-17 20:35:10 +00001816 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001817 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001818 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00001819 PseudoSourceValue::getFixedStack(FI), 0,
1820 false, false, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001821 }
1822 }
1823
1824 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001825 if (isVarArg) {
1826 static const unsigned GPRArgRegs[] = {
1827 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1828 };
1829
Bob Wilsondee46d72009-04-17 20:35:10 +00001830 unsigned NumGPRs = CCInfo.getFirstUnallocated
1831 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001832
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001833 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1834 unsigned VARegSize = (4 - NumGPRs) * 4;
1835 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00001836 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001837 if (VARegSaveSize) {
1838 // If this function is vararg, store any remaining integer argument regs
1839 // to their spots on the stack so that they may be loaded by deferencing
1840 // the result of va_next.
1841 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00001842 AFI->setVarArgsFrameIndex(
1843 MFI->CreateFixedObject(VARegSaveSize,
1844 ArgOffset + VARegSaveSize - VARegSize,
1845 true, false));
1846 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
1847 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001848
Dan Gohman475871a2008-07-27 21:46:04 +00001849 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001850 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001851 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001852 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001853 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001854 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001855 RC = ARM::GPRRegisterClass;
1856
Bob Wilson998e1252009-04-20 18:36:57 +00001857 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001858 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00001859 SDValue Store =
1860 DAG.getStore(Val.getValue(1), dl, Val, FIN,
1861 PseudoSourceValue::getFixedStack(AFI->getVarArgsFrameIndex()), 0,
1862 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001863 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001864 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001865 DAG.getConstant(4, getPointerTy()));
1866 }
1867 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001868 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001869 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001870 } else
1871 // This will point to the next argument passed via stack.
Dan Gohman1e93df62010-04-17 14:41:14 +00001872 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset,
1873 true, false));
Evan Chenga8e29892007-01-19 07:51:42 +00001874 }
1875
Dan Gohman98ca4f22009-08-05 01:29:28 +00001876 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00001877}
1878
1879/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001880static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001881 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001882 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001883 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001884 // Maybe this has already been legalized into the constant pool?
1885 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001886 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001887 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00001888 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001889 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001890 }
1891 }
1892 return false;
1893}
1894
Evan Chenga8e29892007-01-19 07:51:42 +00001895/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1896/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00001897SDValue
1898ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Dan Gohmand858e902010-04-17 15:26:15 +00001899 SDValue &ARMCC, SelectionDAG &DAG,
1900 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00001901 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001902 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00001903 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001904 // Constant does not fit, try adjusting it by one?
1905 switch (CC) {
1906 default: break;
1907 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001908 case ISD::SETGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00001909 if (isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001910 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001911 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001912 }
1913 break;
1914 case ISD::SETULT:
1915 case ISD::SETUGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00001916 if (C > 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001917 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001918 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001919 }
1920 break;
1921 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001922 case ISD::SETGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00001923 if (isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001924 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001925 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001926 }
1927 break;
1928 case ISD::SETULE:
1929 case ISD::SETUGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00001930 if (C < 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001931 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001932 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001933 }
1934 break;
1935 }
1936 }
1937 }
1938
1939 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001940 ARMISD::NodeType CompareType;
1941 switch (CondCode) {
1942 default:
1943 CompareType = ARMISD::CMP;
1944 break;
1945 case ARMCC::EQ:
1946 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00001947 // Uses only Z Flag
1948 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001949 break;
1950 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001951 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1952 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001953}
1954
1955/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001956static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001957 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001958 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001959 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00001960 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001961 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001962 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1963 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001964}
1965
Dan Gohmand858e902010-04-17 15:26:15 +00001966SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001967 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001968 SDValue LHS = Op.getOperand(0);
1969 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001970 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001971 SDValue TrueVal = Op.getOperand(2);
1972 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001973 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001974
Owen Anderson825b72b2009-08-11 20:47:22 +00001975 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001976 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001977 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00001978 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Dale Johannesende064702009-02-06 21:50:26 +00001979 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001980 }
1981
1982 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001983 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00001984
Owen Anderson825b72b2009-08-11 20:47:22 +00001985 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1986 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001987 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1988 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001989 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001990 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001991 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001992 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00001993 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001994 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00001995 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001996 }
1997 return Result;
1998}
1999
Dan Gohmand858e902010-04-17 15:26:15 +00002000SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002001 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002002 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002003 SDValue LHS = Op.getOperand(2);
2004 SDValue RHS = Op.getOperand(3);
2005 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002006 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002007
Owen Anderson825b72b2009-08-11 20:47:22 +00002008 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00002009 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002010 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00002011 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002012 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00002013 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002014 }
2015
Owen Anderson825b72b2009-08-11 20:47:22 +00002016 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00002017 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002018 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002019
Dale Johannesende064702009-02-06 21:50:26 +00002020 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002021 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
2022 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2023 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002024 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002025 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002026 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002027 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00002028 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002029 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002030 }
2031 return Res;
2032}
2033
Dan Gohmand858e902010-04-17 15:26:15 +00002034SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002035 SDValue Chain = Op.getOperand(0);
2036 SDValue Table = Op.getOperand(1);
2037 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002038 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002039
Owen Andersone50ed302009-08-10 22:56:29 +00002040 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002041 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2042 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002043 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002044 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002045 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002046 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2047 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002048 if (Subtarget->isThumb2()) {
2049 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2050 // which does another jump to the destination. This also makes it easier
2051 // to translate it to TBB / TBH later.
2052 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002053 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002054 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002055 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002056 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002057 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002058 PseudoSourceValue::getJumpTable(), 0,
2059 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002060 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002061 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002062 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002063 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002064 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002065 PseudoSourceValue::getJumpTable(), 0, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002066 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002067 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002068 }
Evan Chenga8e29892007-01-19 07:51:42 +00002069}
2070
Bob Wilson76a312b2010-03-19 22:51:32 +00002071static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2072 DebugLoc dl = Op.getDebugLoc();
2073 unsigned Opc;
2074
2075 switch (Op.getOpcode()) {
2076 default:
2077 assert(0 && "Invalid opcode!");
2078 case ISD::FP_TO_SINT:
2079 Opc = ARMISD::FTOSI;
2080 break;
2081 case ISD::FP_TO_UINT:
2082 Opc = ARMISD::FTOUI;
2083 break;
2084 }
2085 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
2086 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
2087}
2088
2089static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2090 EVT VT = Op.getValueType();
2091 DebugLoc dl = Op.getDebugLoc();
2092 unsigned Opc;
2093
2094 switch (Op.getOpcode()) {
2095 default:
2096 assert(0 && "Invalid opcode!");
2097 case ISD::SINT_TO_FP:
2098 Opc = ARMISD::SITOF;
2099 break;
2100 case ISD::UINT_TO_FP:
2101 Opc = ARMISD::UITOF;
2102 break;
2103 }
2104
2105 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
2106 return DAG.getNode(Opc, dl, VT, Op);
2107}
2108
Dan Gohman475871a2008-07-27 21:46:04 +00002109static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002110 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002111 SDValue Tmp0 = Op.getOperand(0);
2112 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002113 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002114 EVT VT = Op.getValueType();
2115 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00002116 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
2117 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002118 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
2119 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002120 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002121}
2122
Dan Gohmand858e902010-04-17 15:26:15 +00002123SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002124 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2125 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00002126 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002127 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2128 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002129 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002130 ? ARM::R7 : ARM::R11;
2131 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2132 while (Depth--)
David Greene1b58cab2010-02-15 16:55:24 +00002133 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
2134 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002135 return FrameAddr;
2136}
2137
Bob Wilson9f3f0612010-04-17 05:30:19 +00002138/// ExpandBIT_CONVERT - If the target supports VFP, this function is called to
2139/// expand a bit convert where either the source or destination type is i64 to
2140/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2141/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2142/// vectors), since the legalizer won't know what to do with that.
Duncan Sands1607f052008-12-01 11:39:25 +00002143static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002144 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2145 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002146 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002147
Bob Wilson9f3f0612010-04-17 05:30:19 +00002148 // This function is only supposed to be called for i64 types, either as the
2149 // source or destination of the bit convert.
2150 EVT SrcVT = Op.getValueType();
2151 EVT DstVT = N->getValueType(0);
2152 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
2153 "ExpandBIT_CONVERT called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002154
Bob Wilson9f3f0612010-04-17 05:30:19 +00002155 // Turn i64->f64 into VMOVDRR.
2156 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002157 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2158 DAG.getConstant(0, MVT::i32));
2159 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2160 DAG.getConstant(1, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00002161 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002162 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002163
Jim Grosbache5165492009-11-09 00:11:35 +00002164 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002165 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2166 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2167 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2168 // Merge the pieces into a single i64 value.
2169 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2170 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002171
Bob Wilson9f3f0612010-04-17 05:30:19 +00002172 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002173}
2174
Bob Wilson5bafff32009-06-22 23:27:02 +00002175/// getZeroVector - Returns a vector of specified type with all zero elements.
2176///
Owen Andersone50ed302009-08-10 22:56:29 +00002177static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002178 assert(VT.isVector() && "Expected a vector type");
2179
2180 // Zero vectors are used to represent vector negation and in those cases
2181 // will be implemented with the NEON VNEG instruction. However, VNEG does
2182 // not support i64 elements, so sometimes the zero vectors will need to be
2183 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002184 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilson5bafff32009-06-22 23:27:02 +00002185 // to their dest type. This ensures they get CSE'd.
2186 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002187 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2188 SmallVector<SDValue, 8> Ops;
2189 MVT TVT;
2190
2191 if (VT.getSizeInBits() == 64) {
2192 Ops.assign(8, Cst); TVT = MVT::v8i8;
2193 } else {
2194 Ops.assign(16, Cst); TVT = MVT::v16i8;
2195 }
2196 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002197
2198 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2199}
2200
2201/// getOnesVector - Returns a vector of specified type with all bits set.
2202///
Owen Andersone50ed302009-08-10 22:56:29 +00002203static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002204 assert(VT.isVector() && "Expected a vector type");
2205
Bob Wilson929ffa22009-10-30 20:13:25 +00002206 // Always build ones vectors as <16 x i8> or <8 x i8> bitcasted to their
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002207 // dest type. This ensures they get CSE'd.
Bob Wilson5bafff32009-06-22 23:27:02 +00002208 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002209 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2210 SmallVector<SDValue, 8> Ops;
2211 MVT TVT;
2212
2213 if (VT.getSizeInBits() == 64) {
2214 Ops.assign(8, Cst); TVT = MVT::v8i8;
2215 } else {
2216 Ops.assign(16, Cst); TVT = MVT::v16i8;
2217 }
2218 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002219
2220 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2221}
2222
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002223/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2224/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002225SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2226 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002227 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2228 EVT VT = Op.getValueType();
2229 unsigned VTBits = VT.getSizeInBits();
2230 DebugLoc dl = Op.getDebugLoc();
2231 SDValue ShOpLo = Op.getOperand(0);
2232 SDValue ShOpHi = Op.getOperand(1);
2233 SDValue ShAmt = Op.getOperand(2);
2234 SDValue ARMCC;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002235 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002236
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002237 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2238
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002239 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2240 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2241 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2242 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2243 DAG.getConstant(VTBits, MVT::i32));
2244 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2245 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002246 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002247
2248 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2249 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002250 ARMCC, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002251 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002252 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC,
2253 CCR, Cmp);
2254
2255 SDValue Ops[2] = { Lo, Hi };
2256 return DAG.getMergeValues(Ops, 2, dl);
2257}
2258
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002259/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2260/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002261SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2262 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002263 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2264 EVT VT = Op.getValueType();
2265 unsigned VTBits = VT.getSizeInBits();
2266 DebugLoc dl = Op.getDebugLoc();
2267 SDValue ShOpLo = Op.getOperand(0);
2268 SDValue ShOpHi = Op.getOperand(1);
2269 SDValue ShAmt = Op.getOperand(2);
2270 SDValue ARMCC;
2271
2272 assert(Op.getOpcode() == ISD::SHL_PARTS);
2273 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2274 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2275 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2276 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2277 DAG.getConstant(VTBits, MVT::i32));
2278 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2279 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2280
2281 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2282 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2283 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002284 ARMCC, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002285 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
2286 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMCC,
2287 CCR, Cmp);
2288
2289 SDValue Ops[2] = { Lo, Hi };
2290 return DAG.getMergeValues(Ops, 2, dl);
2291}
2292
Jim Grosbach3482c802010-01-18 19:58:49 +00002293static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
2294 const ARMSubtarget *ST) {
2295 EVT VT = N->getValueType(0);
2296 DebugLoc dl = N->getDebugLoc();
2297
2298 if (!ST->hasV6T2Ops())
2299 return SDValue();
2300
2301 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
2302 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
2303}
2304
Bob Wilson5bafff32009-06-22 23:27:02 +00002305static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2306 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002307 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002308 DebugLoc dl = N->getDebugLoc();
2309
2310 // Lower vector shifts on NEON to use VSHL.
2311 if (VT.isVector()) {
2312 assert(ST->hasNEON() && "unexpected vector shift");
2313
2314 // Left shifts translate directly to the vshiftu intrinsic.
2315 if (N->getOpcode() == ISD::SHL)
2316 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002317 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002318 N->getOperand(0), N->getOperand(1));
2319
2320 assert((N->getOpcode() == ISD::SRA ||
2321 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2322
2323 // NEON uses the same intrinsics for both left and right shifts. For
2324 // right shifts, the shift amounts are negative, so negate the vector of
2325 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002326 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002327 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2328 getZeroVector(ShiftVT, DAG, dl),
2329 N->getOperand(1));
2330 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2331 Intrinsic::arm_neon_vshifts :
2332 Intrinsic::arm_neon_vshiftu);
2333 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002334 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002335 N->getOperand(0), NegatedCount);
2336 }
2337
Eli Friedmance392eb2009-08-22 03:13:10 +00002338 // We can get here for a node like i32 = ISD::SHL i32, i64
2339 if (VT != MVT::i64)
2340 return SDValue();
2341
2342 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002343 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002344
Chris Lattner27a6c732007-11-24 07:07:01 +00002345 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2346 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002347 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002348 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002349
Chris Lattner27a6c732007-11-24 07:07:01 +00002350 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002351 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002352
Chris Lattner27a6c732007-11-24 07:07:01 +00002353 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002354 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2355 DAG.getConstant(0, MVT::i32));
2356 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2357 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002358
Chris Lattner27a6c732007-11-24 07:07:01 +00002359 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2360 // captures the result into a carry flag.
2361 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002362 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002363
Chris Lattner27a6c732007-11-24 07:07:01 +00002364 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002365 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002366
Chris Lattner27a6c732007-11-24 07:07:01 +00002367 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002368 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002369}
2370
Bob Wilson5bafff32009-06-22 23:27:02 +00002371static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2372 SDValue TmpOp0, TmpOp1;
2373 bool Invert = false;
2374 bool Swap = false;
2375 unsigned Opc = 0;
2376
2377 SDValue Op0 = Op.getOperand(0);
2378 SDValue Op1 = Op.getOperand(1);
2379 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002380 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002381 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2382 DebugLoc dl = Op.getDebugLoc();
2383
2384 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2385 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002386 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002387 case ISD::SETUNE:
2388 case ISD::SETNE: Invert = true; // Fallthrough
2389 case ISD::SETOEQ:
2390 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2391 case ISD::SETOLT:
2392 case ISD::SETLT: Swap = true; // Fallthrough
2393 case ISD::SETOGT:
2394 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2395 case ISD::SETOLE:
2396 case ISD::SETLE: Swap = true; // Fallthrough
2397 case ISD::SETOGE:
2398 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2399 case ISD::SETUGE: Swap = true; // Fallthrough
2400 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2401 case ISD::SETUGT: Swap = true; // Fallthrough
2402 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2403 case ISD::SETUEQ: Invert = true; // Fallthrough
2404 case ISD::SETONE:
2405 // Expand this to (OLT | OGT).
2406 TmpOp0 = Op0;
2407 TmpOp1 = Op1;
2408 Opc = ISD::OR;
2409 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2410 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2411 break;
2412 case ISD::SETUO: Invert = true; // Fallthrough
2413 case ISD::SETO:
2414 // Expand this to (OLT | OGE).
2415 TmpOp0 = Op0;
2416 TmpOp1 = Op1;
2417 Opc = ISD::OR;
2418 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2419 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2420 break;
2421 }
2422 } else {
2423 // Integer comparisons.
2424 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002425 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002426 case ISD::SETNE: Invert = true;
2427 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2428 case ISD::SETLT: Swap = true;
2429 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2430 case ISD::SETLE: Swap = true;
2431 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2432 case ISD::SETULT: Swap = true;
2433 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2434 case ISD::SETULE: Swap = true;
2435 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2436 }
2437
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002438 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002439 if (Opc == ARMISD::VCEQ) {
2440
2441 SDValue AndOp;
2442 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2443 AndOp = Op0;
2444 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2445 AndOp = Op1;
2446
2447 // Ignore bitconvert.
2448 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2449 AndOp = AndOp.getOperand(0);
2450
2451 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2452 Opc = ARMISD::VTST;
2453 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2454 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2455 Invert = !Invert;
2456 }
2457 }
2458 }
2459
2460 if (Swap)
2461 std::swap(Op0, Op1);
2462
2463 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2464
2465 if (Invert)
2466 Result = DAG.getNOT(dl, Result, VT);
2467
2468 return Result;
2469}
2470
2471/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2472/// VMOV instruction, and if so, return the constant being splatted.
2473static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2474 unsigned SplatBitSize, SelectionDAG &DAG) {
2475 switch (SplatBitSize) {
2476 case 8:
2477 // Any 1-byte value is OK.
2478 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson825b72b2009-08-11 20:47:22 +00002479 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilson5bafff32009-06-22 23:27:02 +00002480
2481 case 16:
2482 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2483 if ((SplatBits & ~0xff) == 0 ||
2484 (SplatBits & ~0xff00) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002485 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilson5bafff32009-06-22 23:27:02 +00002486 break;
2487
2488 case 32:
2489 // NEON's 32-bit VMOV supports splat values where:
2490 // * only one byte is nonzero, or
2491 // * the least significant byte is 0xff and the second byte is nonzero, or
2492 // * the least significant 2 bytes are 0xff and the third is nonzero.
2493 if ((SplatBits & ~0xff) == 0 ||
2494 (SplatBits & ~0xff00) == 0 ||
2495 (SplatBits & ~0xff0000) == 0 ||
2496 (SplatBits & ~0xff000000) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002497 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002498
2499 if ((SplatBits & ~0xffff) == 0 &&
2500 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002501 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002502
2503 if ((SplatBits & ~0xffffff) == 0 &&
2504 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002505 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002506
2507 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2508 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2509 // VMOV.I32. A (very) minor optimization would be to replicate the value
2510 // and fall through here to test for a valid 64-bit splat. But, then the
2511 // caller would also need to check and handle the change in size.
2512 break;
2513
2514 case 64: {
2515 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2516 uint64_t BitMask = 0xff;
2517 uint64_t Val = 0;
2518 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2519 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2520 Val |= BitMask;
2521 else if ((SplatBits & BitMask) != 0)
2522 return SDValue();
2523 BitMask <<= 8;
2524 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002525 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilson5bafff32009-06-22 23:27:02 +00002526 }
2527
2528 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002529 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilson5bafff32009-06-22 23:27:02 +00002530 break;
2531 }
2532
2533 return SDValue();
2534}
2535
2536/// getVMOVImm - If this is a build_vector of constants which can be
2537/// formed by using a VMOV instruction of the specified element size,
2538/// return the constant being splatted. The ByteSize field indicates the
2539/// number of bytes of each element [1248].
2540SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2541 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2542 APInt SplatBits, SplatUndef;
2543 unsigned SplatBitSize;
2544 bool HasAnyUndefs;
2545 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2546 HasAnyUndefs, ByteSize * 8))
2547 return SDValue();
2548
2549 if (SplatBitSize > ByteSize * 8)
2550 return SDValue();
2551
2552 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2553 SplatBitSize, DAG);
2554}
2555
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002556static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2557 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002558 unsigned NumElts = VT.getVectorNumElements();
2559 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002560 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002561
2562 // If this is a VEXT shuffle, the immediate value is the index of the first
2563 // element. The other shuffle indices must be the successive elements after
2564 // the first one.
2565 unsigned ExpectedElt = Imm;
2566 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002567 // Increment the expected index. If it wraps around, it may still be
2568 // a VEXT but the source vectors must be swapped.
2569 ExpectedElt += 1;
2570 if (ExpectedElt == NumElts * 2) {
2571 ExpectedElt = 0;
2572 ReverseVEXT = true;
2573 }
2574
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002575 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002576 return false;
2577 }
2578
2579 // Adjust the index value if the source operands will be swapped.
2580 if (ReverseVEXT)
2581 Imm -= NumElts;
2582
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002583 return true;
2584}
2585
Bob Wilson8bb9e482009-07-26 00:39:34 +00002586/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2587/// instruction with the specified blocksize. (The order of the elements
2588/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002589static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2590 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002591 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2592 "Only possible block sizes for VREV are: 16, 32, 64");
2593
Bob Wilson8bb9e482009-07-26 00:39:34 +00002594 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00002595 if (EltSz == 64)
2596 return false;
2597
2598 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002599 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002600
2601 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2602 return false;
2603
2604 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002605 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002606 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2607 return false;
2608 }
2609
2610 return true;
2611}
2612
Bob Wilsonc692cb72009-08-21 20:54:19 +00002613static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2614 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002615 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2616 if (EltSz == 64)
2617 return false;
2618
Bob Wilsonc692cb72009-08-21 20:54:19 +00002619 unsigned NumElts = VT.getVectorNumElements();
2620 WhichResult = (M[0] == 0 ? 0 : 1);
2621 for (unsigned i = 0; i < NumElts; i += 2) {
2622 if ((unsigned) M[i] != i + WhichResult ||
2623 (unsigned) M[i+1] != i + NumElts + WhichResult)
2624 return false;
2625 }
2626 return true;
2627}
2628
Bob Wilson324f4f12009-12-03 06:40:55 +00002629/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
2630/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2631/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
2632static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2633 unsigned &WhichResult) {
2634 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2635 if (EltSz == 64)
2636 return false;
2637
2638 unsigned NumElts = VT.getVectorNumElements();
2639 WhichResult = (M[0] == 0 ? 0 : 1);
2640 for (unsigned i = 0; i < NumElts; i += 2) {
2641 if ((unsigned) M[i] != i + WhichResult ||
2642 (unsigned) M[i+1] != i + WhichResult)
2643 return false;
2644 }
2645 return true;
2646}
2647
Bob Wilsonc692cb72009-08-21 20:54:19 +00002648static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2649 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002650 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2651 if (EltSz == 64)
2652 return false;
2653
Bob Wilsonc692cb72009-08-21 20:54:19 +00002654 unsigned NumElts = VT.getVectorNumElements();
2655 WhichResult = (M[0] == 0 ? 0 : 1);
2656 for (unsigned i = 0; i != NumElts; ++i) {
2657 if ((unsigned) M[i] != 2 * i + WhichResult)
2658 return false;
2659 }
2660
2661 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002662 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002663 return false;
2664
2665 return true;
2666}
2667
Bob Wilson324f4f12009-12-03 06:40:55 +00002668/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
2669/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2670/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
2671static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2672 unsigned &WhichResult) {
2673 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2674 if (EltSz == 64)
2675 return false;
2676
2677 unsigned Half = VT.getVectorNumElements() / 2;
2678 WhichResult = (M[0] == 0 ? 0 : 1);
2679 for (unsigned j = 0; j != 2; ++j) {
2680 unsigned Idx = WhichResult;
2681 for (unsigned i = 0; i != Half; ++i) {
2682 if ((unsigned) M[i + j * Half] != Idx)
2683 return false;
2684 Idx += 2;
2685 }
2686 }
2687
2688 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2689 if (VT.is64BitVector() && EltSz == 32)
2690 return false;
2691
2692 return true;
2693}
2694
Bob Wilsonc692cb72009-08-21 20:54:19 +00002695static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2696 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002697 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2698 if (EltSz == 64)
2699 return false;
2700
Bob Wilsonc692cb72009-08-21 20:54:19 +00002701 unsigned NumElts = VT.getVectorNumElements();
2702 WhichResult = (M[0] == 0 ? 0 : 1);
2703 unsigned Idx = WhichResult * NumElts / 2;
2704 for (unsigned i = 0; i != NumElts; i += 2) {
2705 if ((unsigned) M[i] != Idx ||
2706 (unsigned) M[i+1] != Idx + NumElts)
2707 return false;
2708 Idx += 1;
2709 }
2710
2711 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002712 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002713 return false;
2714
2715 return true;
2716}
2717
Bob Wilson324f4f12009-12-03 06:40:55 +00002718/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
2719/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2720/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
2721static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2722 unsigned &WhichResult) {
2723 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2724 if (EltSz == 64)
2725 return false;
2726
2727 unsigned NumElts = VT.getVectorNumElements();
2728 WhichResult = (M[0] == 0 ? 0 : 1);
2729 unsigned Idx = WhichResult * NumElts / 2;
2730 for (unsigned i = 0; i != NumElts; i += 2) {
2731 if ((unsigned) M[i] != Idx ||
2732 (unsigned) M[i+1] != Idx)
2733 return false;
2734 Idx += 1;
2735 }
2736
2737 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2738 if (VT.is64BitVector() && EltSz == 32)
2739 return false;
2740
2741 return true;
2742}
2743
2744
Owen Andersone50ed302009-08-10 22:56:29 +00002745static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002746 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00002747 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002748 if (ConstVal->isNullValue())
2749 return getZeroVector(VT, DAG, dl);
2750 if (ConstVal->isAllOnesValue())
2751 return getOnesVector(VT, DAG, dl);
2752
Owen Andersone50ed302009-08-10 22:56:29 +00002753 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00002754 if (VT.is64BitVector()) {
2755 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002756 case 8: CanonicalVT = MVT::v8i8; break;
2757 case 16: CanonicalVT = MVT::v4i16; break;
2758 case 32: CanonicalVT = MVT::v2i32; break;
2759 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002760 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002761 }
2762 } else {
2763 assert(VT.is128BitVector() && "unknown splat vector size");
2764 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002765 case 8: CanonicalVT = MVT::v16i8; break;
2766 case 16: CanonicalVT = MVT::v8i16; break;
2767 case 32: CanonicalVT = MVT::v4i32; break;
2768 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002769 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002770 }
2771 }
2772
2773 // Build a canonical splat for this value.
2774 SmallVector<SDValue, 8> Ops;
2775 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2776 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2777 Ops.size());
2778 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2779}
2780
2781// If this is a case we can't handle, return null and let the default
2782// expansion code take care of it.
2783static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00002784 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002785 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002786 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002787
2788 APInt SplatBits, SplatUndef;
2789 unsigned SplatBitSize;
2790 bool HasAnyUndefs;
2791 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00002792 if (SplatBitSize <= 64) {
2793 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2794 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2795 if (Val.getNode())
2796 return BuildSplat(Val, VT, DAG, dl);
2797 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00002798 }
2799
2800 // If there are only 2 elements in a 128-bit vector, insert them into an
2801 // undef vector. This handles the common case for 128-bit vector argument
2802 // passing, where the insertions should be translated to subreg accesses
2803 // with no real instructions.
2804 if (VT.is128BitVector() && Op.getNumOperands() == 2) {
2805 SDValue Val = DAG.getUNDEF(VT);
2806 SDValue Op0 = Op.getOperand(0);
2807 SDValue Op1 = Op.getOperand(1);
2808 if (Op0.getOpcode() != ISD::UNDEF)
2809 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op0,
2810 DAG.getIntPtrConstant(0));
2811 if (Op1.getOpcode() != ISD::UNDEF)
2812 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op1,
2813 DAG.getIntPtrConstant(1));
2814 return Val;
Bob Wilson5bafff32009-06-22 23:27:02 +00002815 }
2816
2817 return SDValue();
2818}
2819
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002820/// isShuffleMaskLegal - Targets can use this to indicate that they only
2821/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2822/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2823/// are assumed to be legal.
2824bool
2825ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2826 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002827 if (VT.getVectorNumElements() == 4 &&
2828 (VT.is128BitVector() || VT.is64BitVector())) {
2829 unsigned PFIndexes[4];
2830 for (unsigned i = 0; i != 4; ++i) {
2831 if (M[i] < 0)
2832 PFIndexes[i] = 8;
2833 else
2834 PFIndexes[i] = M[i];
2835 }
2836
2837 // Compute the index in the perfect shuffle table.
2838 unsigned PFTableIndex =
2839 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2840 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2841 unsigned Cost = (PFEntry >> 30);
2842
2843 if (Cost <= 4)
2844 return true;
2845 }
2846
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002847 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00002848 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002849
2850 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2851 isVREVMask(M, VT, 64) ||
2852 isVREVMask(M, VT, 32) ||
2853 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00002854 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2855 isVTRNMask(M, VT, WhichResult) ||
2856 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00002857 isVZIPMask(M, VT, WhichResult) ||
2858 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
2859 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
2860 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002861}
2862
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002863/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2864/// the specified operations to build the shuffle.
2865static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2866 SDValue RHS, SelectionDAG &DAG,
2867 DebugLoc dl) {
2868 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2869 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2870 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2871
2872 enum {
2873 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2874 OP_VREV,
2875 OP_VDUP0,
2876 OP_VDUP1,
2877 OP_VDUP2,
2878 OP_VDUP3,
2879 OP_VEXT1,
2880 OP_VEXT2,
2881 OP_VEXT3,
2882 OP_VUZPL, // VUZP, left result
2883 OP_VUZPR, // VUZP, right result
2884 OP_VZIPL, // VZIP, left result
2885 OP_VZIPR, // VZIP, right result
2886 OP_VTRNL, // VTRN, left result
2887 OP_VTRNR // VTRN, right result
2888 };
2889
2890 if (OpNum == OP_COPY) {
2891 if (LHSID == (1*9+2)*9+3) return LHS;
2892 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2893 return RHS;
2894 }
2895
2896 SDValue OpLHS, OpRHS;
2897 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2898 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2899 EVT VT = OpLHS.getValueType();
2900
2901 switch (OpNum) {
2902 default: llvm_unreachable("Unknown shuffle opcode!");
2903 case OP_VREV:
2904 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2905 case OP_VDUP0:
2906 case OP_VDUP1:
2907 case OP_VDUP2:
2908 case OP_VDUP3:
2909 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002910 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002911 case OP_VEXT1:
2912 case OP_VEXT2:
2913 case OP_VEXT3:
2914 return DAG.getNode(ARMISD::VEXT, dl, VT,
2915 OpLHS, OpRHS,
2916 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2917 case OP_VUZPL:
2918 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002919 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002920 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
2921 case OP_VZIPL:
2922 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002923 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002924 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
2925 case OP_VTRNL:
2926 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002927 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2928 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002929 }
2930}
2931
Bob Wilson5bafff32009-06-22 23:27:02 +00002932static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002933 SDValue V1 = Op.getOperand(0);
2934 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00002935 DebugLoc dl = Op.getDebugLoc();
2936 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002937 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002938 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00002939
Bob Wilson28865062009-08-13 02:13:04 +00002940 // Convert shuffles that are directly supported on NEON to target-specific
2941 // DAG nodes, instead of keeping them as shuffles and matching them again
2942 // during code selection. This is more efficient and avoids the possibility
2943 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00002944 // FIXME: floating-point vectors should be canonicalized to integer vectors
2945 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002946 SVN->getMask(ShuffleMask);
2947
2948 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson0ce37102009-08-14 05:08:32 +00002949 int Lane = SVN->getSplatIndex();
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00002950 // If this is undef splat, generate it via "just" vdup, if possible.
2951 if (Lane == -1) Lane = 0;
2952
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002953 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
2954 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00002955 }
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002956 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002957 DAG.getConstant(Lane, MVT::i32));
Bob Wilson0ce37102009-08-14 05:08:32 +00002958 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002959
2960 bool ReverseVEXT;
2961 unsigned Imm;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002962 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002963 if (ReverseVEXT)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002964 std::swap(V1, V2);
2965 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002966 DAG.getConstant(Imm, MVT::i32));
2967 }
2968
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002969 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002970 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002971 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002972 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002973 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002974 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
2975
Bob Wilsonc692cb72009-08-21 20:54:19 +00002976 // Check for Neon shuffles that modify both input vectors in place.
2977 // If both results are used, i.e., if there are two shuffles with the same
2978 // source operands and with masks corresponding to both results of one of
2979 // these operations, DAG memoization will ensure that a single node is
2980 // used for both shuffles.
2981 unsigned WhichResult;
2982 if (isVTRNMask(ShuffleMask, VT, WhichResult))
2983 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2984 V1, V2).getValue(WhichResult);
2985 if (isVUZPMask(ShuffleMask, VT, WhichResult))
2986 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2987 V1, V2).getValue(WhichResult);
2988 if (isVZIPMask(ShuffleMask, VT, WhichResult))
2989 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2990 V1, V2).getValue(WhichResult);
2991
Bob Wilson324f4f12009-12-03 06:40:55 +00002992 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
2993 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2994 V1, V1).getValue(WhichResult);
2995 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
2996 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2997 V1, V1).getValue(WhichResult);
2998 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
2999 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3000 V1, V1).getValue(WhichResult);
3001
Bob Wilsonc692cb72009-08-21 20:54:19 +00003002 // If the shuffle is not directly supported and it has 4 elements, use
3003 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003004 if (VT.getVectorNumElements() == 4 &&
3005 (VT.is128BitVector() || VT.is64BitVector())) {
3006 unsigned PFIndexes[4];
3007 for (unsigned i = 0; i != 4; ++i) {
3008 if (ShuffleMask[i] < 0)
3009 PFIndexes[i] = 8;
3010 else
3011 PFIndexes[i] = ShuffleMask[i];
3012 }
3013
3014 // Compute the index in the perfect shuffle table.
3015 unsigned PFTableIndex =
3016 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3017
3018 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3019 unsigned Cost = (PFEntry >> 30);
3020
3021 if (Cost <= 4)
3022 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
3023 }
Bob Wilsond8e17572009-08-12 22:31:50 +00003024
Bob Wilson22cac0d2009-08-14 05:16:33 +00003025 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003026}
3027
Bob Wilson5bafff32009-06-22 23:27:02 +00003028static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003029 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003030 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003031 SDValue Vec = Op.getOperand(0);
3032 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00003033 assert(VT == MVT::i32 &&
3034 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
3035 "unexpected type for custom-lowering vector extract");
3036 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00003037}
3038
Bob Wilsona6d65862009-08-03 20:36:38 +00003039static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3040 // The only time a CONCAT_VECTORS operation can have legal types is when
3041 // two 64-bit vectors are concatenated to a 128-bit vector.
3042 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
3043 "unexpected CONCAT_VECTORS");
3044 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00003045 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00003046 SDValue Op0 = Op.getOperand(0);
3047 SDValue Op1 = Op.getOperand(1);
3048 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003049 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3050 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00003051 DAG.getIntPtrConstant(0));
3052 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003053 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3054 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00003055 DAG.getIntPtrConstant(1));
3056 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003057}
3058
Dan Gohmand858e902010-04-17 15:26:15 +00003059SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003060 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003061 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00003062 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00003063 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003064 case ISD::GlobalAddress:
3065 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
3066 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00003067 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00003068 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
3069 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003070 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00003071 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00003072 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00003073 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00003074 case ISD::SINT_TO_FP:
3075 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
3076 case ISD::FP_TO_SINT:
3077 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003078 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00003079 case ISD::RETURNADDR: break;
Jim Grosbach0e0da732009-05-12 23:59:14 +00003080 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003081 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00003082 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
3083 Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00003084 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003085 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00003086 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00003087 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00003088 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003089 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00003090 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00003091 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003092 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
3093 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3094 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003095 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00003096 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003097 }
Dan Gohman475871a2008-07-27 21:46:04 +00003098 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003099}
3100
Duncan Sands1607f052008-12-01 11:39:25 +00003101/// ReplaceNodeResults - Replace the results of node with an illegal result
3102/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00003103void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
3104 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00003105 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00003106 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00003107 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00003108 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003109 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003110 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003111 case ISD::BIT_CONVERT:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003112 Res = ExpandBIT_CONVERT(N, DAG);
3113 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00003114 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003115 case ISD::SRA:
3116 Res = LowerShift(N, DAG, Subtarget);
3117 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003118 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00003119 if (Res.getNode())
3120 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00003121}
Chris Lattner27a6c732007-11-24 07:07:01 +00003122
Evan Chenga8e29892007-01-19 07:51:42 +00003123//===----------------------------------------------------------------------===//
3124// ARM Scheduler Hooks
3125//===----------------------------------------------------------------------===//
3126
3127MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003128ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
3129 MachineBasicBlock *BB,
3130 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00003131 unsigned dest = MI->getOperand(0).getReg();
3132 unsigned ptr = MI->getOperand(1).getReg();
3133 unsigned oldval = MI->getOperand(2).getReg();
3134 unsigned newval = MI->getOperand(3).getReg();
3135 unsigned scratch = BB->getParent()->getRegInfo()
3136 .createVirtualRegister(ARM::GPRRegisterClass);
3137 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3138 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003139 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00003140
3141 unsigned ldrOpc, strOpc;
3142 switch (Size) {
3143 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003144 case 1:
3145 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
3146 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
3147 break;
3148 case 2:
3149 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3150 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3151 break;
3152 case 4:
3153 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3154 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3155 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00003156 }
3157
3158 MachineFunction *MF = BB->getParent();
3159 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3160 MachineFunction::iterator It = BB;
3161 ++It; // insert the new blocks after the current block
3162
3163 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3164 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3165 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3166 MF->insert(It, loop1MBB);
3167 MF->insert(It, loop2MBB);
3168 MF->insert(It, exitMBB);
3169 exitMBB->transferSuccessors(BB);
3170
3171 // thisMBB:
3172 // ...
3173 // fallthrough --> loop1MBB
3174 BB->addSuccessor(loop1MBB);
3175
3176 // loop1MBB:
3177 // ldrex dest, [ptr]
3178 // cmp dest, oldval
3179 // bne exitMBB
3180 BB = loop1MBB;
3181 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003182 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003183 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003184 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3185 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003186 BB->addSuccessor(loop2MBB);
3187 BB->addSuccessor(exitMBB);
3188
3189 // loop2MBB:
3190 // strex scratch, newval, [ptr]
3191 // cmp scratch, #0
3192 // bne loop1MBB
3193 BB = loop2MBB;
3194 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
3195 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003196 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003197 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003198 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3199 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003200 BB->addSuccessor(loop1MBB);
3201 BB->addSuccessor(exitMBB);
3202
3203 // exitMBB:
3204 // ...
3205 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00003206
3207 MF->DeleteMachineInstr(MI); // The instruction is gone now.
3208
Jim Grosbach5278eb82009-12-11 01:42:04 +00003209 return BB;
3210}
3211
3212MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003213ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
3214 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00003215 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
3216 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3217
3218 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003219 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003220 MachineFunction::iterator It = BB;
3221 ++It;
3222
3223 unsigned dest = MI->getOperand(0).getReg();
3224 unsigned ptr = MI->getOperand(1).getReg();
3225 unsigned incr = MI->getOperand(2).getReg();
3226 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00003227
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003228 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003229 unsigned ldrOpc, strOpc;
3230 switch (Size) {
3231 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003232 case 1:
3233 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00003234 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003235 break;
3236 case 2:
3237 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3238 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3239 break;
3240 case 4:
3241 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3242 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3243 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00003244 }
3245
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003246 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3247 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3248 MF->insert(It, loopMBB);
3249 MF->insert(It, exitMBB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003250 exitMBB->transferSuccessors(BB);
3251
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003252 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003253 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3254 unsigned scratch2 = (!BinOpcode) ? incr :
3255 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3256
3257 // thisMBB:
3258 // ...
3259 // fallthrough --> loopMBB
3260 BB->addSuccessor(loopMBB);
3261
3262 // loopMBB:
3263 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003264 // <binop> scratch2, dest, incr
3265 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00003266 // cmp scratch, #0
3267 // bne- loopMBB
3268 // fallthrough --> exitMBB
3269 BB = loopMBB;
3270 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00003271 if (BinOpcode) {
3272 // operand order needs to go the other way for NAND
3273 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
3274 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3275 addReg(incr).addReg(dest)).addReg(0);
3276 else
3277 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3278 addReg(dest).addReg(incr)).addReg(0);
3279 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00003280
3281 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
3282 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003283 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00003284 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003285 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3286 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003287
3288 BB->addSuccessor(loopMBB);
3289 BB->addSuccessor(exitMBB);
3290
3291 // exitMBB:
3292 // ...
3293 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00003294
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003295 MF->DeleteMachineInstr(MI); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00003296
Jim Grosbachc3c23542009-12-14 04:22:04 +00003297 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00003298}
3299
3300MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00003301ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003302 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003303 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00003304 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003305 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00003306 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00003307 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00003308 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00003309 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00003310
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003311 case ARM::ATOMIC_LOAD_ADD_I8:
3312 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3313 case ARM::ATOMIC_LOAD_ADD_I16:
3314 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3315 case ARM::ATOMIC_LOAD_ADD_I32:
3316 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003317
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003318 case ARM::ATOMIC_LOAD_AND_I8:
3319 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3320 case ARM::ATOMIC_LOAD_AND_I16:
3321 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3322 case ARM::ATOMIC_LOAD_AND_I32:
3323 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003324
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003325 case ARM::ATOMIC_LOAD_OR_I8:
3326 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3327 case ARM::ATOMIC_LOAD_OR_I16:
3328 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3329 case ARM::ATOMIC_LOAD_OR_I32:
3330 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003331
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003332 case ARM::ATOMIC_LOAD_XOR_I8:
3333 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3334 case ARM::ATOMIC_LOAD_XOR_I16:
3335 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3336 case ARM::ATOMIC_LOAD_XOR_I32:
3337 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003338
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003339 case ARM::ATOMIC_LOAD_NAND_I8:
3340 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3341 case ARM::ATOMIC_LOAD_NAND_I16:
3342 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3343 case ARM::ATOMIC_LOAD_NAND_I32:
3344 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003345
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003346 case ARM::ATOMIC_LOAD_SUB_I8:
3347 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3348 case ARM::ATOMIC_LOAD_SUB_I16:
3349 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3350 case ARM::ATOMIC_LOAD_SUB_I32:
3351 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003352
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003353 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
3354 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
3355 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00003356
3357 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
3358 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
3359 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003360
Evan Cheng007ea272009-08-12 05:17:19 +00003361 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00003362 // To "insert" a SELECT_CC instruction, we actually have to insert the
3363 // diamond control-flow pattern. The incoming instruction knows the
3364 // destination vreg to set, the condition code register to branch on, the
3365 // true/false values to select between, and a branch opcode to use.
3366 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003367 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00003368 ++It;
3369
3370 // thisMBB:
3371 // ...
3372 // TrueVal = ...
3373 // cmpTY ccX, r1, r2
3374 // bCC copy1MBB
3375 // fallthrough --> copy0MBB
3376 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003377 MachineFunction *F = BB->getParent();
3378 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3379 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00003380 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00003381 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003382 F->insert(It, copy0MBB);
3383 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00003384 // Update machine-CFG edges by first adding all successors of the current
3385 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00003386 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003387 E = BB->succ_end(); I != E; ++I)
Evan Chengce319102009-09-19 09:51:03 +00003388 sinkMBB->addSuccessor(*I);
Evan Chenga8e29892007-01-19 07:51:42 +00003389 // Next, remove all successors of the current block, and add the true
3390 // and fallthrough blocks as its successors.
Evan Chengce319102009-09-19 09:51:03 +00003391 while (!BB->succ_empty())
Evan Chenga8e29892007-01-19 07:51:42 +00003392 BB->removeSuccessor(BB->succ_begin());
3393 BB->addSuccessor(copy0MBB);
3394 BB->addSuccessor(sinkMBB);
3395
3396 // copy0MBB:
3397 // %FalseValue = ...
3398 // # fallthrough to sinkMBB
3399 BB = copy0MBB;
3400
3401 // Update machine-CFG edges
3402 BB->addSuccessor(sinkMBB);
3403
3404 // sinkMBB:
3405 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3406 // ...
3407 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00003408 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00003409 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
3410 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
3411
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003412 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00003413 return BB;
3414 }
Evan Cheng86198642009-08-07 00:34:42 +00003415
3416 case ARM::tANDsp:
3417 case ARM::tADDspr_:
3418 case ARM::tSUBspi_:
3419 case ARM::t2SUBrSPi_:
3420 case ARM::t2SUBrSPi12_:
3421 case ARM::t2SUBrSPs_: {
3422 MachineFunction *MF = BB->getParent();
3423 unsigned DstReg = MI->getOperand(0).getReg();
3424 unsigned SrcReg = MI->getOperand(1).getReg();
3425 bool DstIsDead = MI->getOperand(0).isDead();
3426 bool SrcIsKill = MI->getOperand(1).isKill();
3427
3428 if (SrcReg != ARM::SP) {
3429 // Copy the source to SP from virtual register.
3430 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
3431 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3432 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
3433 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
3434 .addReg(SrcReg, getKillRegState(SrcIsKill));
3435 }
3436
3437 unsigned OpOpc = 0;
3438 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
3439 switch (MI->getOpcode()) {
3440 default:
3441 llvm_unreachable("Unexpected pseudo instruction!");
3442 case ARM::tANDsp:
3443 OpOpc = ARM::tAND;
3444 NeedPred = true;
3445 break;
3446 case ARM::tADDspr_:
3447 OpOpc = ARM::tADDspr;
3448 break;
3449 case ARM::tSUBspi_:
3450 OpOpc = ARM::tSUBspi;
3451 break;
3452 case ARM::t2SUBrSPi_:
3453 OpOpc = ARM::t2SUBrSPi;
3454 NeedPred = true; NeedCC = true;
3455 break;
3456 case ARM::t2SUBrSPi12_:
3457 OpOpc = ARM::t2SUBrSPi12;
3458 NeedPred = true;
3459 break;
3460 case ARM::t2SUBrSPs_:
3461 OpOpc = ARM::t2SUBrSPs;
3462 NeedPred = true; NeedCC = true; NeedOp3 = true;
3463 break;
3464 }
3465 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
3466 if (OpOpc == ARM::tAND)
3467 AddDefaultT1CC(MIB);
3468 MIB.addReg(ARM::SP);
3469 MIB.addOperand(MI->getOperand(2));
3470 if (NeedOp3)
3471 MIB.addOperand(MI->getOperand(3));
3472 if (NeedPred)
3473 AddDefaultPred(MIB);
3474 if (NeedCC)
3475 AddDefaultCC(MIB);
3476
3477 // Copy the result from SP to virtual register.
3478 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
3479 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3480 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
3481 BuildMI(BB, dl, TII->get(CopyOpc))
3482 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
3483 .addReg(ARM::SP);
3484 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3485 return BB;
3486 }
Evan Chenga8e29892007-01-19 07:51:42 +00003487 }
3488}
3489
3490//===----------------------------------------------------------------------===//
3491// ARM Optimization Hooks
3492//===----------------------------------------------------------------------===//
3493
Chris Lattnerd1980a52009-03-12 06:52:53 +00003494static
3495SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3496 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00003497 SelectionDAG &DAG = DCI.DAG;
3498 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003499 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00003500 unsigned Opc = N->getOpcode();
3501 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3502 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3503 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3504 ISD::CondCode CC = ISD::SETCC_INVALID;
3505
3506 if (isSlctCC) {
3507 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
3508 } else {
3509 SDValue CCOp = Slct.getOperand(0);
3510 if (CCOp.getOpcode() == ISD::SETCC)
3511 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
3512 }
3513
3514 bool DoXform = false;
3515 bool InvCC = false;
3516 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
3517 "Bad input!");
3518
3519 if (LHS.getOpcode() == ISD::Constant &&
3520 cast<ConstantSDNode>(LHS)->isNullValue()) {
3521 DoXform = true;
3522 } else if (CC != ISD::SETCC_INVALID &&
3523 RHS.getOpcode() == ISD::Constant &&
3524 cast<ConstantSDNode>(RHS)->isNullValue()) {
3525 std::swap(LHS, RHS);
3526 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003527 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00003528 Op0.getOperand(0).getValueType();
3529 bool isInt = OpVT.isInteger();
3530 CC = ISD::getSetCCInverse(CC, isInt);
3531
3532 if (!TLI.isCondCodeLegal(CC, OpVT))
3533 return SDValue(); // Inverse operator isn't legal.
3534
3535 DoXform = true;
3536 InvCC = true;
3537 }
3538
3539 if (DoXform) {
3540 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3541 if (isSlctCC)
3542 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3543 Slct.getOperand(0), Slct.getOperand(1), CC);
3544 SDValue CCOp = Slct.getOperand(0);
3545 if (InvCC)
3546 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3547 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3548 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3549 CCOp, OtherOp, Result);
3550 }
3551 return SDValue();
3552}
3553
3554/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3555static SDValue PerformADDCombine(SDNode *N,
3556 TargetLowering::DAGCombinerInfo &DCI) {
3557 // added by evan in r37685 with no testcase.
3558 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003559
Chris Lattnerd1980a52009-03-12 06:52:53 +00003560 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3561 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3562 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3563 if (Result.getNode()) return Result;
3564 }
3565 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3566 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3567 if (Result.getNode()) return Result;
3568 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003569
Chris Lattnerd1980a52009-03-12 06:52:53 +00003570 return SDValue();
3571}
3572
3573/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3574static SDValue PerformSUBCombine(SDNode *N,
3575 TargetLowering::DAGCombinerInfo &DCI) {
3576 // added by evan in r37685 with no testcase.
3577 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003578
Chris Lattnerd1980a52009-03-12 06:52:53 +00003579 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3580 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3581 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3582 if (Result.getNode()) return Result;
3583 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003584
Chris Lattnerd1980a52009-03-12 06:52:53 +00003585 return SDValue();
3586}
3587
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003588static SDValue PerformMULCombine(SDNode *N,
3589 TargetLowering::DAGCombinerInfo &DCI,
3590 const ARMSubtarget *Subtarget) {
3591 SelectionDAG &DAG = DCI.DAG;
3592
3593 if (Subtarget->isThumb1Only())
3594 return SDValue();
3595
3596 if (DAG.getMachineFunction().
3597 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
3598 return SDValue();
3599
3600 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
3601 return SDValue();
3602
3603 EVT VT = N->getValueType(0);
3604 if (VT != MVT::i32)
3605 return SDValue();
3606
3607 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
3608 if (!C)
3609 return SDValue();
3610
3611 uint64_t MulAmt = C->getZExtValue();
3612 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
3613 ShiftAmt = ShiftAmt & (32 - 1);
3614 SDValue V = N->getOperand(0);
3615 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003616
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003617 SDValue Res;
3618 MulAmt >>= ShiftAmt;
3619 if (isPowerOf2_32(MulAmt - 1)) {
3620 // (mul x, 2^N + 1) => (add (shl x, N), x)
3621 Res = DAG.getNode(ISD::ADD, DL, VT,
3622 V, DAG.getNode(ISD::SHL, DL, VT,
3623 V, DAG.getConstant(Log2_32(MulAmt-1),
3624 MVT::i32)));
3625 } else if (isPowerOf2_32(MulAmt + 1)) {
3626 // (mul x, 2^N - 1) => (sub (shl x, N), x)
3627 Res = DAG.getNode(ISD::SUB, DL, VT,
3628 DAG.getNode(ISD::SHL, DL, VT,
3629 V, DAG.getConstant(Log2_32(MulAmt+1),
3630 MVT::i32)),
3631 V);
3632 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003633 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003634
3635 if (ShiftAmt != 0)
3636 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
3637 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003638
3639 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003640 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003641 return SDValue();
3642}
3643
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +00003644/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
3645/// ARMISD::VMOVRRD.
Jim Grosbache5165492009-11-09 00:11:35 +00003646static SDValue PerformVMOVRRDCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003647 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003648 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003649 SDValue InDouble = N->getOperand(0);
Jim Grosbache5165492009-11-09 00:11:35 +00003650 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003651 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003652 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003653}
3654
Bob Wilson5bafff32009-06-22 23:27:02 +00003655/// getVShiftImm - Check if this is a valid build_vector for the immediate
3656/// operand of a vector shift operation, where all the elements of the
3657/// build_vector must have the same constant integer value.
3658static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3659 // Ignore bit_converts.
3660 while (Op.getOpcode() == ISD::BIT_CONVERT)
3661 Op = Op.getOperand(0);
3662 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3663 APInt SplatBits, SplatUndef;
3664 unsigned SplatBitSize;
3665 bool HasAnyUndefs;
3666 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3667 HasAnyUndefs, ElementBits) ||
3668 SplatBitSize > ElementBits)
3669 return false;
3670 Cnt = SplatBits.getSExtValue();
3671 return true;
3672}
3673
3674/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3675/// operand of a vector shift left operation. That value must be in the range:
3676/// 0 <= Value < ElementBits for a left shift; or
3677/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003678static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003679 assert(VT.isVector() && "vector shift count is not a vector type");
3680 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3681 if (! getVShiftImm(Op, ElementBits, Cnt))
3682 return false;
3683 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3684}
3685
3686/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3687/// operand of a vector shift right operation. For a shift opcode, the value
3688/// is positive, but for an intrinsic the value count must be negative. The
3689/// absolute value must be in the range:
3690/// 1 <= |Value| <= ElementBits for a right shift; or
3691/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003692static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00003693 int64_t &Cnt) {
3694 assert(VT.isVector() && "vector shift count is not a vector type");
3695 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3696 if (! getVShiftImm(Op, ElementBits, Cnt))
3697 return false;
3698 if (isIntrinsic)
3699 Cnt = -Cnt;
3700 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3701}
3702
3703/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3704static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3705 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3706 switch (IntNo) {
3707 default:
3708 // Don't do anything for most intrinsics.
3709 break;
3710
3711 // Vector shifts: check for immediate versions and lower them.
3712 // Note: This is done during DAG combining instead of DAG legalizing because
3713 // the build_vectors for 64-bit vector element shift counts are generally
3714 // not legal, and it is hard to see their values after they get legalized to
3715 // loads from a constant pool.
3716 case Intrinsic::arm_neon_vshifts:
3717 case Intrinsic::arm_neon_vshiftu:
3718 case Intrinsic::arm_neon_vshiftls:
3719 case Intrinsic::arm_neon_vshiftlu:
3720 case Intrinsic::arm_neon_vshiftn:
3721 case Intrinsic::arm_neon_vrshifts:
3722 case Intrinsic::arm_neon_vrshiftu:
3723 case Intrinsic::arm_neon_vrshiftn:
3724 case Intrinsic::arm_neon_vqshifts:
3725 case Intrinsic::arm_neon_vqshiftu:
3726 case Intrinsic::arm_neon_vqshiftsu:
3727 case Intrinsic::arm_neon_vqshiftns:
3728 case Intrinsic::arm_neon_vqshiftnu:
3729 case Intrinsic::arm_neon_vqshiftnsu:
3730 case Intrinsic::arm_neon_vqrshiftns:
3731 case Intrinsic::arm_neon_vqrshiftnu:
3732 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00003733 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003734 int64_t Cnt;
3735 unsigned VShiftOpc = 0;
3736
3737 switch (IntNo) {
3738 case Intrinsic::arm_neon_vshifts:
3739 case Intrinsic::arm_neon_vshiftu:
3740 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3741 VShiftOpc = ARMISD::VSHL;
3742 break;
3743 }
3744 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3745 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3746 ARMISD::VSHRs : ARMISD::VSHRu);
3747 break;
3748 }
3749 return SDValue();
3750
3751 case Intrinsic::arm_neon_vshiftls:
3752 case Intrinsic::arm_neon_vshiftlu:
3753 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3754 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003755 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003756
3757 case Intrinsic::arm_neon_vrshifts:
3758 case Intrinsic::arm_neon_vrshiftu:
3759 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3760 break;
3761 return SDValue();
3762
3763 case Intrinsic::arm_neon_vqshifts:
3764 case Intrinsic::arm_neon_vqshiftu:
3765 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3766 break;
3767 return SDValue();
3768
3769 case Intrinsic::arm_neon_vqshiftsu:
3770 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3771 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003772 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003773
3774 case Intrinsic::arm_neon_vshiftn:
3775 case Intrinsic::arm_neon_vrshiftn:
3776 case Intrinsic::arm_neon_vqshiftns:
3777 case Intrinsic::arm_neon_vqshiftnu:
3778 case Intrinsic::arm_neon_vqshiftnsu:
3779 case Intrinsic::arm_neon_vqrshiftns:
3780 case Intrinsic::arm_neon_vqrshiftnu:
3781 case Intrinsic::arm_neon_vqrshiftnsu:
3782 // Narrowing shifts require an immediate right shift.
3783 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3784 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003785 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003786
3787 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003788 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003789 }
3790
3791 switch (IntNo) {
3792 case Intrinsic::arm_neon_vshifts:
3793 case Intrinsic::arm_neon_vshiftu:
3794 // Opcode already set above.
3795 break;
3796 case Intrinsic::arm_neon_vshiftls:
3797 case Intrinsic::arm_neon_vshiftlu:
3798 if (Cnt == VT.getVectorElementType().getSizeInBits())
3799 VShiftOpc = ARMISD::VSHLLi;
3800 else
3801 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3802 ARMISD::VSHLLs : ARMISD::VSHLLu);
3803 break;
3804 case Intrinsic::arm_neon_vshiftn:
3805 VShiftOpc = ARMISD::VSHRN; break;
3806 case Intrinsic::arm_neon_vrshifts:
3807 VShiftOpc = ARMISD::VRSHRs; break;
3808 case Intrinsic::arm_neon_vrshiftu:
3809 VShiftOpc = ARMISD::VRSHRu; break;
3810 case Intrinsic::arm_neon_vrshiftn:
3811 VShiftOpc = ARMISD::VRSHRN; break;
3812 case Intrinsic::arm_neon_vqshifts:
3813 VShiftOpc = ARMISD::VQSHLs; break;
3814 case Intrinsic::arm_neon_vqshiftu:
3815 VShiftOpc = ARMISD::VQSHLu; break;
3816 case Intrinsic::arm_neon_vqshiftsu:
3817 VShiftOpc = ARMISD::VQSHLsu; break;
3818 case Intrinsic::arm_neon_vqshiftns:
3819 VShiftOpc = ARMISD::VQSHRNs; break;
3820 case Intrinsic::arm_neon_vqshiftnu:
3821 VShiftOpc = ARMISD::VQSHRNu; break;
3822 case Intrinsic::arm_neon_vqshiftnsu:
3823 VShiftOpc = ARMISD::VQSHRNsu; break;
3824 case Intrinsic::arm_neon_vqrshiftns:
3825 VShiftOpc = ARMISD::VQRSHRNs; break;
3826 case Intrinsic::arm_neon_vqrshiftnu:
3827 VShiftOpc = ARMISD::VQRSHRNu; break;
3828 case Intrinsic::arm_neon_vqrshiftnsu:
3829 VShiftOpc = ARMISD::VQRSHRNsu; break;
3830 }
3831
3832 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003833 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003834 }
3835
3836 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00003837 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003838 int64_t Cnt;
3839 unsigned VShiftOpc = 0;
3840
3841 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3842 VShiftOpc = ARMISD::VSLI;
3843 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3844 VShiftOpc = ARMISD::VSRI;
3845 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003846 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003847 }
3848
3849 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3850 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00003851 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003852 }
3853
3854 case Intrinsic::arm_neon_vqrshifts:
3855 case Intrinsic::arm_neon_vqrshiftu:
3856 // No immediate versions of these to check for.
3857 break;
3858 }
3859
3860 return SDValue();
3861}
3862
3863/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3864/// lowers them. As with the vector shift intrinsics, this is done during DAG
3865/// combining instead of DAG legalizing because the build_vectors for 64-bit
3866/// vector element shift counts are generally not legal, and it is hard to see
3867/// their values after they get legalized to loads from a constant pool.
3868static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3869 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003870 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003871
3872 // Nothing to be done for scalar shifts.
3873 if (! VT.isVector())
3874 return SDValue();
3875
3876 assert(ST->hasNEON() && "unexpected vector shift");
3877 int64_t Cnt;
3878
3879 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003880 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003881
3882 case ISD::SHL:
3883 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3884 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003885 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003886 break;
3887
3888 case ISD::SRA:
3889 case ISD::SRL:
3890 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3891 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3892 ARMISD::VSHRs : ARMISD::VSHRu);
3893 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003894 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003895 }
3896 }
3897 return SDValue();
3898}
3899
3900/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3901/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3902static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3903 const ARMSubtarget *ST) {
3904 SDValue N0 = N->getOperand(0);
3905
3906 // Check for sign- and zero-extensions of vector extract operations of 8-
3907 // and 16-bit vector elements. NEON supports these directly. They are
3908 // handled during DAG combining because type legalization will promote them
3909 // to 32-bit types and it is messy to recognize the operations after that.
3910 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3911 SDValue Vec = N0.getOperand(0);
3912 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003913 EVT VT = N->getValueType(0);
3914 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003915 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3916
Owen Anderson825b72b2009-08-11 20:47:22 +00003917 if (VT == MVT::i32 &&
3918 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00003919 TLI.isTypeLegal(Vec.getValueType())) {
3920
3921 unsigned Opc = 0;
3922 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003923 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003924 case ISD::SIGN_EXTEND:
3925 Opc = ARMISD::VGETLANEs;
3926 break;
3927 case ISD::ZERO_EXTEND:
3928 case ISD::ANY_EXTEND:
3929 Opc = ARMISD::VGETLANEu;
3930 break;
3931 }
3932 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
3933 }
3934 }
3935
3936 return SDValue();
3937}
3938
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003939/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
3940/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
3941static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
3942 const ARMSubtarget *ST) {
3943 // If the target supports NEON, try to use vmax/vmin instructions for f32
3944 // selects like "x < y ? x : y". Unless the FiniteOnlyFPMath option is set,
3945 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
3946 // a NaN; only do the transformation when it matches that behavior.
3947
3948 // For now only do this when using NEON for FP operations; if using VFP, it
3949 // is not obvious that the benefit outweighs the cost of switching to the
3950 // NEON pipeline.
3951 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
3952 N->getValueType(0) != MVT::f32)
3953 return SDValue();
3954
3955 SDValue CondLHS = N->getOperand(0);
3956 SDValue CondRHS = N->getOperand(1);
3957 SDValue LHS = N->getOperand(2);
3958 SDValue RHS = N->getOperand(3);
3959 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
3960
3961 unsigned Opcode = 0;
3962 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00003963 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003964 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00003965 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003966 IsReversed = true ; // x CC y ? y : x
3967 } else {
3968 return SDValue();
3969 }
3970
Bob Wilsone742bb52010-02-24 22:15:53 +00003971 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003972 switch (CC) {
3973 default: break;
3974 case ISD::SETOLT:
3975 case ISD::SETOLE:
3976 case ISD::SETLT:
3977 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003978 case ISD::SETULT:
3979 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00003980 // If LHS is NaN, an ordered comparison will be false and the result will
3981 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
3982 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
3983 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
3984 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
3985 break;
3986 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
3987 // will return -0, so vmin can only be used for unsafe math or if one of
3988 // the operands is known to be nonzero.
3989 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
3990 !UnsafeFPMath &&
3991 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
3992 break;
3993 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003994 break;
3995
3996 case ISD::SETOGT:
3997 case ISD::SETOGE:
3998 case ISD::SETGT:
3999 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004000 case ISD::SETUGT:
4001 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004002 // If LHS is NaN, an ordered comparison will be false and the result will
4003 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
4004 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4005 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
4006 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4007 break;
4008 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
4009 // will return +0, so vmax can only be used for unsafe math or if one of
4010 // the operands is known to be nonzero.
4011 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
4012 !UnsafeFPMath &&
4013 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4014 break;
4015 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004016 break;
4017 }
4018
4019 if (!Opcode)
4020 return SDValue();
4021 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
4022}
4023
Dan Gohman475871a2008-07-27 21:46:04 +00004024SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004025 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004026 switch (N->getOpcode()) {
4027 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004028 case ISD::ADD: return PerformADDCombine(N, DCI);
4029 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004030 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbache5165492009-11-09 00:11:35 +00004031 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004032 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004033 case ISD::SHL:
4034 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004035 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004036 case ISD::SIGN_EXTEND:
4037 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004038 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
4039 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004040 }
Dan Gohman475871a2008-07-27 21:46:04 +00004041 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004042}
4043
Bill Wendlingaf566342009-08-15 21:21:19 +00004044bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
4045 if (!Subtarget->hasV6Ops())
4046 // Pre-v6 does not support unaligned mem access.
4047 return false;
Anton Korobeynikov90cfc132010-01-30 14:08:12 +00004048 else {
4049 // v6+ may or may not support unaligned mem access depending on the system
4050 // configuration.
4051 // FIXME: This is pretty conservative. Should we provide cmdline option to
4052 // control the behaviour?
Bill Wendlingaf566342009-08-15 21:21:19 +00004053 if (!Subtarget->isTargetDarwin())
4054 return false;
4055 }
4056
4057 switch (VT.getSimpleVT().SimpleTy) {
4058 default:
4059 return false;
4060 case MVT::i8:
4061 case MVT::i16:
4062 case MVT::i32:
4063 return true;
4064 // FIXME: VLD1 etc with standard alignment is legal.
4065 }
4066}
4067
Evan Chenge6c835f2009-08-14 20:09:37 +00004068static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
4069 if (V < 0)
4070 return false;
4071
4072 unsigned Scale = 1;
4073 switch (VT.getSimpleVT().SimpleTy) {
4074 default: return false;
4075 case MVT::i1:
4076 case MVT::i8:
4077 // Scale == 1;
4078 break;
4079 case MVT::i16:
4080 // Scale == 2;
4081 Scale = 2;
4082 break;
4083 case MVT::i32:
4084 // Scale == 4;
4085 Scale = 4;
4086 break;
4087 }
4088
4089 if ((V & (Scale - 1)) != 0)
4090 return false;
4091 V /= Scale;
4092 return V == (V & ((1LL << 5) - 1));
4093}
4094
4095static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
4096 const ARMSubtarget *Subtarget) {
4097 bool isNeg = false;
4098 if (V < 0) {
4099 isNeg = true;
4100 V = - V;
4101 }
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 // + imm12 or - imm8
4110 if (isNeg)
4111 return V == (V & ((1LL << 8) - 1));
4112 return V == (V & ((1LL << 12) - 1));
4113 case MVT::f32:
4114 case MVT::f64:
4115 // Same as ARM mode. FIXME: NEON?
4116 if (!Subtarget->hasVFP2())
4117 return false;
4118 if ((V & 3) != 0)
4119 return false;
4120 V >>= 2;
4121 return V == (V & ((1LL << 8) - 1));
4122 }
4123}
4124
Evan Chengb01fad62007-03-12 23:30:29 +00004125/// isLegalAddressImmediate - Return true if the integer value can be used
4126/// as the offset of the target addressing mode for load / store of the
4127/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00004128static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004129 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00004130 if (V == 0)
4131 return true;
4132
Evan Cheng65011532009-03-09 19:15:00 +00004133 if (!VT.isSimple())
4134 return false;
4135
Evan Chenge6c835f2009-08-14 20:09:37 +00004136 if (Subtarget->isThumb1Only())
4137 return isLegalT1AddressImmediate(V, VT);
4138 else if (Subtarget->isThumb2())
4139 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00004140
Evan Chenge6c835f2009-08-14 20:09:37 +00004141 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00004142 if (V < 0)
4143 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00004144 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00004145 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004146 case MVT::i1:
4147 case MVT::i8:
4148 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00004149 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004150 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004151 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00004152 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004153 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004154 case MVT::f32:
4155 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00004156 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00004157 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00004158 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00004159 return false;
4160 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004161 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00004162 }
Evan Chenga8e29892007-01-19 07:51:42 +00004163}
4164
Evan Chenge6c835f2009-08-14 20:09:37 +00004165bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
4166 EVT VT) const {
4167 int Scale = AM.Scale;
4168 if (Scale < 0)
4169 return false;
4170
4171 switch (VT.getSimpleVT().SimpleTy) {
4172 default: return false;
4173 case MVT::i1:
4174 case MVT::i8:
4175 case MVT::i16:
4176 case MVT::i32:
4177 if (Scale == 1)
4178 return true;
4179 // r + r << imm
4180 Scale = Scale & ~1;
4181 return Scale == 2 || Scale == 4 || Scale == 8;
4182 case MVT::i64:
4183 // r + r
4184 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
4185 return true;
4186 return false;
4187 case MVT::isVoid:
4188 // Note, we allow "void" uses (basically, uses that aren't loads or
4189 // stores), because arm allows folding a scale into many arithmetic
4190 // operations. This should be made more precise and revisited later.
4191
4192 // Allow r << imm, but the imm has to be a multiple of two.
4193 if (Scale & 1) return false;
4194 return isPowerOf2_32(Scale);
4195 }
4196}
4197
Chris Lattner37caf8c2007-04-09 23:33:39 +00004198/// isLegalAddressingMode - Return true if the addressing mode represented
4199/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004200bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004201 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004202 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00004203 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00004204 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004205
Chris Lattner37caf8c2007-04-09 23:33:39 +00004206 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004207 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004208 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004209
Chris Lattner37caf8c2007-04-09 23:33:39 +00004210 switch (AM.Scale) {
4211 case 0: // no scale reg, must be "r+i" or "r", or "i".
4212 break;
4213 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00004214 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00004215 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004216 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00004217 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004218 // ARM doesn't support any R+R*scale+imm addr modes.
4219 if (AM.BaseOffs)
4220 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004221
Bob Wilson2c7dab12009-04-08 17:55:28 +00004222 if (!VT.isSimple())
4223 return false;
4224
Evan Chenge6c835f2009-08-14 20:09:37 +00004225 if (Subtarget->isThumb2())
4226 return isLegalT2ScaledAddressingMode(AM, VT);
4227
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004228 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00004229 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00004230 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004231 case MVT::i1:
4232 case MVT::i8:
4233 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004234 if (Scale < 0) Scale = -Scale;
4235 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004236 return true;
4237 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00004238 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00004239 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00004240 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004241 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004242 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004243 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00004244 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004245
Owen Anderson825b72b2009-08-11 20:47:22 +00004246 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004247 // Note, we allow "void" uses (basically, uses that aren't loads or
4248 // stores), because arm allows folding a scale into many arithmetic
4249 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004250
Chris Lattner37caf8c2007-04-09 23:33:39 +00004251 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00004252 if (Scale & 1) return false;
4253 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00004254 }
4255 break;
Evan Chengb01fad62007-03-12 23:30:29 +00004256 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00004257 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00004258}
4259
Evan Cheng77e47512009-11-11 19:05:52 +00004260/// isLegalICmpImmediate - Return true if the specified immediate is legal
4261/// icmp immediate, that is the target has icmp instructions which can compare
4262/// a register against the immediate without having to materialize the
4263/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00004264bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00004265 if (!Subtarget->isThumb())
4266 return ARM_AM::getSOImmVal(Imm) != -1;
4267 if (Subtarget->isThumb2())
4268 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00004269 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00004270}
4271
Owen Andersone50ed302009-08-10 22:56:29 +00004272static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004273 bool isSEXTLoad, SDValue &Base,
4274 SDValue &Offset, bool &isInc,
4275 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00004276 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4277 return false;
4278
Owen Anderson825b72b2009-08-11 20:47:22 +00004279 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00004280 // AddressingMode 3
4281 Base = Ptr->getOperand(0);
4282 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004283 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004284 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004285 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004286 isInc = false;
4287 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4288 return true;
4289 }
4290 }
4291 isInc = (Ptr->getOpcode() == ISD::ADD);
4292 Offset = Ptr->getOperand(1);
4293 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004294 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00004295 // AddressingMode 2
4296 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004297 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004298 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004299 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004300 isInc = false;
4301 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4302 Base = Ptr->getOperand(0);
4303 return true;
4304 }
4305 }
4306
4307 if (Ptr->getOpcode() == ISD::ADD) {
4308 isInc = true;
4309 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
4310 if (ShOpcVal != ARM_AM::no_shift) {
4311 Base = Ptr->getOperand(1);
4312 Offset = Ptr->getOperand(0);
4313 } else {
4314 Base = Ptr->getOperand(0);
4315 Offset = Ptr->getOperand(1);
4316 }
4317 return true;
4318 }
4319
4320 isInc = (Ptr->getOpcode() == ISD::ADD);
4321 Base = Ptr->getOperand(0);
4322 Offset = Ptr->getOperand(1);
4323 return true;
4324 }
4325
Jim Grosbache5165492009-11-09 00:11:35 +00004326 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00004327 return false;
4328}
4329
Owen Andersone50ed302009-08-10 22:56:29 +00004330static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004331 bool isSEXTLoad, SDValue &Base,
4332 SDValue &Offset, bool &isInc,
4333 SelectionDAG &DAG) {
4334 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4335 return false;
4336
4337 Base = Ptr->getOperand(0);
4338 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
4339 int RHSC = (int)RHS->getZExtValue();
4340 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
4341 assert(Ptr->getOpcode() == ISD::ADD);
4342 isInc = false;
4343 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4344 return true;
4345 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
4346 isInc = Ptr->getOpcode() == ISD::ADD;
4347 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
4348 return true;
4349 }
4350 }
4351
4352 return false;
4353}
4354
Evan Chenga8e29892007-01-19 07:51:42 +00004355/// getPreIndexedAddressParts - returns true by value, base pointer and
4356/// offset pointer and addressing mode by reference if the node's address
4357/// can be legally represented as pre-indexed load / store address.
4358bool
Dan Gohman475871a2008-07-27 21:46:04 +00004359ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
4360 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004361 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004362 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004363 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004364 return false;
4365
Owen Andersone50ed302009-08-10 22:56:29 +00004366 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004367 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004368 bool isSEXTLoad = false;
4369 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4370 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004371 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004372 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4373 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
4374 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004375 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004376 } else
4377 return false;
4378
4379 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004380 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004381 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004382 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
4383 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004384 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004385 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00004386 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00004387 if (!isLegal)
4388 return false;
4389
4390 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
4391 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004392}
4393
4394/// getPostIndexedAddressParts - returns true by value, base pointer and
4395/// offset pointer and addressing mode by reference if this node can be
4396/// combined with a load / store to form a post-indexed load / store.
4397bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00004398 SDValue &Base,
4399 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004400 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004401 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004402 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004403 return false;
4404
Owen Andersone50ed302009-08-10 22:56:29 +00004405 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004406 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004407 bool isSEXTLoad = false;
4408 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004409 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004410 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4411 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004412 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004413 } else
4414 return false;
4415
4416 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004417 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004418 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004419 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004420 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004421 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004422 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
4423 isInc, DAG);
4424 if (!isLegal)
4425 return false;
4426
4427 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
4428 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004429}
4430
Dan Gohman475871a2008-07-27 21:46:04 +00004431void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00004432 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004433 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004434 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00004435 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00004436 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004437 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004438 switch (Op.getOpcode()) {
4439 default: break;
4440 case ARMISD::CMOV: {
4441 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00004442 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004443 if (KnownZero == 0 && KnownOne == 0) return;
4444
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004445 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00004446 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
4447 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004448 KnownZero &= KnownZeroRHS;
4449 KnownOne &= KnownOneRHS;
4450 return;
4451 }
4452 }
4453}
4454
4455//===----------------------------------------------------------------------===//
4456// ARM Inline Assembly Support
4457//===----------------------------------------------------------------------===//
4458
4459/// getConstraintType - Given a constraint letter, return the type of
4460/// constraint it is for this target.
4461ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00004462ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
4463 if (Constraint.size() == 1) {
4464 switch (Constraint[0]) {
4465 default: break;
4466 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004467 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00004468 }
Evan Chenga8e29892007-01-19 07:51:42 +00004469 }
Chris Lattner4234f572007-03-25 02:14:49 +00004470 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00004471}
4472
Bob Wilson2dc4f542009-03-20 22:42:55 +00004473std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00004474ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004475 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004476 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004477 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00004478 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004479 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004480 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004481 return std::make_pair(0U, ARM::tGPRRegisterClass);
4482 else
4483 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004484 case 'r':
4485 return std::make_pair(0U, ARM::GPRRegisterClass);
4486 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004487 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004488 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00004489 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004490 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00004491 if (VT.getSizeInBits() == 128)
4492 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004493 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004494 }
4495 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00004496 if (StringRef("{cc}").equals_lower(Constraint))
4497 return std::make_pair(0U, ARM::CCRRegisterClass);
4498
Evan Chenga8e29892007-01-19 07:51:42 +00004499 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
4500}
4501
4502std::vector<unsigned> ARMTargetLowering::
4503getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004504 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004505 if (Constraint.size() != 1)
4506 return std::vector<unsigned>();
4507
4508 switch (Constraint[0]) { // GCC ARM Constraint Letters
4509 default: break;
4510 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004511 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4512 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4513 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004514 case 'r':
4515 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4516 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4517 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
4518 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004519 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004520 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004521 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
4522 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
4523 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
4524 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
4525 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
4526 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
4527 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
4528 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00004529 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004530 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
4531 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
4532 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
4533 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00004534 if (VT.getSizeInBits() == 128)
4535 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
4536 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004537 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004538 }
4539
4540 return std::vector<unsigned>();
4541}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004542
4543/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
4544/// vector. If it is invalid, don't add anything to Ops.
4545void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
4546 char Constraint,
4547 bool hasMemory,
4548 std::vector<SDValue>&Ops,
4549 SelectionDAG &DAG) const {
4550 SDValue Result(0, 0);
4551
4552 switch (Constraint) {
4553 default: break;
4554 case 'I': case 'J': case 'K': case 'L':
4555 case 'M': case 'N': case 'O':
4556 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
4557 if (!C)
4558 return;
4559
4560 int64_t CVal64 = C->getSExtValue();
4561 int CVal = (int) CVal64;
4562 // None of these constraints allow values larger than 32 bits. Check
4563 // that the value fits in an int.
4564 if (CVal != CVal64)
4565 return;
4566
4567 switch (Constraint) {
4568 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004569 if (Subtarget->isThumb1Only()) {
4570 // This must be a constant between 0 and 255, for ADD
4571 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004572 if (CVal >= 0 && CVal <= 255)
4573 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004574 } else if (Subtarget->isThumb2()) {
4575 // A constant that can be used as an immediate value in a
4576 // data-processing instruction.
4577 if (ARM_AM::getT2SOImmVal(CVal) != -1)
4578 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004579 } else {
4580 // A constant that can be used as an immediate value in a
4581 // data-processing instruction.
4582 if (ARM_AM::getSOImmVal(CVal) != -1)
4583 break;
4584 }
4585 return;
4586
4587 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004588 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004589 // This must be a constant between -255 and -1, for negated ADD
4590 // immediates. This can be used in GCC with an "n" modifier that
4591 // prints the negated value, for use with SUB instructions. It is
4592 // not useful otherwise but is implemented for compatibility.
4593 if (CVal >= -255 && CVal <= -1)
4594 break;
4595 } else {
4596 // This must be a constant between -4095 and 4095. It is not clear
4597 // what this constraint is intended for. Implemented for
4598 // compatibility with GCC.
4599 if (CVal >= -4095 && CVal <= 4095)
4600 break;
4601 }
4602 return;
4603
4604 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004605 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004606 // A 32-bit value where only one byte has a nonzero value. Exclude
4607 // zero to match GCC. This constraint is used by GCC internally for
4608 // constants that can be loaded with a move/shift combination.
4609 // It is not useful otherwise but is implemented for compatibility.
4610 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
4611 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004612 } else if (Subtarget->isThumb2()) {
4613 // A constant whose bitwise inverse can be used as an immediate
4614 // value in a data-processing instruction. This can be used in GCC
4615 // with a "B" modifier that prints the inverted value, for use with
4616 // BIC and MVN instructions. It is not useful otherwise but is
4617 // implemented for compatibility.
4618 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
4619 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004620 } else {
4621 // A constant whose bitwise inverse can be used as an immediate
4622 // value in a data-processing instruction. This can be used in GCC
4623 // with a "B" modifier that prints the inverted value, for use with
4624 // BIC and MVN instructions. It is not useful otherwise but is
4625 // implemented for compatibility.
4626 if (ARM_AM::getSOImmVal(~CVal) != -1)
4627 break;
4628 }
4629 return;
4630
4631 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004632 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004633 // This must be a constant between -7 and 7,
4634 // for 3-operand ADD/SUB immediate instructions.
4635 if (CVal >= -7 && CVal < 7)
4636 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004637 } else if (Subtarget->isThumb2()) {
4638 // A constant whose negation can be used as an immediate value in a
4639 // data-processing instruction. This can be used in GCC with an "n"
4640 // modifier that prints the negated value, for use with SUB
4641 // instructions. It is not useful otherwise but is implemented for
4642 // compatibility.
4643 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
4644 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004645 } else {
4646 // A constant whose negation can be used as an immediate value in a
4647 // data-processing instruction. This can be used in GCC with an "n"
4648 // modifier that prints the negated value, for use with SUB
4649 // instructions. It is not useful otherwise but is implemented for
4650 // compatibility.
4651 if (ARM_AM::getSOImmVal(-CVal) != -1)
4652 break;
4653 }
4654 return;
4655
4656 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004657 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004658 // This must be a multiple of 4 between 0 and 1020, for
4659 // ADD sp + immediate.
4660 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
4661 break;
4662 } else {
4663 // A power of two or a constant between 0 and 32. This is used in
4664 // GCC for the shift amount on shifted register operands, but it is
4665 // useful in general for any shift amounts.
4666 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
4667 break;
4668 }
4669 return;
4670
4671 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004672 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004673 // This must be a constant between 0 and 31, for shift amounts.
4674 if (CVal >= 0 && CVal <= 31)
4675 break;
4676 }
4677 return;
4678
4679 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004680 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004681 // This must be a multiple of 4 between -508 and 508, for
4682 // ADD/SUB sp = sp + immediate.
4683 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
4684 break;
4685 }
4686 return;
4687 }
4688 Result = DAG.getTargetConstant(CVal, Op.getValueType());
4689 break;
4690 }
4691
4692 if (Result.getNode()) {
4693 Ops.push_back(Result);
4694 return;
4695 }
4696 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
4697 Ops, DAG);
4698}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00004699
4700bool
4701ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4702 // The ARM target isn't yet aware of offsets.
4703 return false;
4704}
Evan Cheng39382422009-10-28 01:44:26 +00004705
4706int ARM::getVFPf32Imm(const APFloat &FPImm) {
4707 APInt Imm = FPImm.bitcastToAPInt();
4708 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
4709 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
4710 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
4711
4712 // We can handle 4 bits of mantissa.
4713 // mantissa = (16+UInt(e:f:g:h))/16.
4714 if (Mantissa & 0x7ffff)
4715 return -1;
4716 Mantissa >>= 19;
4717 if ((Mantissa & 0xf) != Mantissa)
4718 return -1;
4719
4720 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4721 if (Exp < -3 || Exp > 4)
4722 return -1;
4723 Exp = ((Exp+3) & 0x7) ^ 4;
4724
4725 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4726}
4727
4728int ARM::getVFPf64Imm(const APFloat &FPImm) {
4729 APInt Imm = FPImm.bitcastToAPInt();
4730 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
4731 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
4732 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
4733
4734 // We can handle 4 bits of mantissa.
4735 // mantissa = (16+UInt(e:f:g:h))/16.
4736 if (Mantissa & 0xffffffffffffLL)
4737 return -1;
4738 Mantissa >>= 48;
4739 if ((Mantissa & 0xf) != Mantissa)
4740 return -1;
4741
4742 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4743 if (Exp < -3 || Exp > 4)
4744 return -1;
4745 Exp = ((Exp+3) & 0x7) ^ 4;
4746
4747 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4748}
4749
4750/// isFPImmLegal - Returns true if the target can instruction select the
4751/// specified FP immediate natively. If false, the legalizer will
4752/// materialize the FP immediate as a load from a constant pool.
4753bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
4754 if (!Subtarget->hasVFP3())
4755 return false;
4756 if (VT == MVT::f32)
4757 return ARM::getVFPf32Imm(Imm) != -1;
4758 if (VT == MVT::f64)
4759 return ARM::getVFPf64Imm(Imm) != -1;
4760 return false;
4761}