blob: 48f3bbfac90507d0be568ab41c6b7539eb8443bc [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 Cheng211ffa12010-05-19 20:19:50 +0000469 setSchedulingPreference(Sched::RegPressure);
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 Wilson63b88452010-05-20 18:39:53 +00003025 // v2f64 and v2i64 shuffles are just register copies.
3026 if (VT == MVT::v2f64 || VT == MVT::v2i64) {
3027 // Do the expansion as f64 since i64 is not legal.
3028 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, V1);
3029 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, V2);
3030 SDValue Val = DAG.getUNDEF(MVT::v2f64);
3031 for (unsigned i = 0; i < 2; ++i) {
3032 if (ShuffleMask[i] < 0)
3033 continue;
3034 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
3035 ShuffleMask[i] < 2 ? V1 : V2,
3036 DAG.getConstant(ShuffleMask[i] & 1, MVT::i32));
3037 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3038 Elt, DAG.getConstant(i, MVT::i32));
3039 }
3040 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
3041 }
3042
Bob Wilson22cac0d2009-08-14 05:16:33 +00003043 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003044}
3045
Bob Wilson5bafff32009-06-22 23:27:02 +00003046static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003047 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003048 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003049 SDValue Vec = Op.getOperand(0);
3050 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00003051 assert(VT == MVT::i32 &&
3052 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
3053 "unexpected type for custom-lowering vector extract");
3054 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00003055}
3056
Bob Wilsona6d65862009-08-03 20:36:38 +00003057static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3058 // The only time a CONCAT_VECTORS operation can have legal types is when
3059 // two 64-bit vectors are concatenated to a 128-bit vector.
3060 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
3061 "unexpected CONCAT_VECTORS");
3062 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00003063 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00003064 SDValue Op0 = Op.getOperand(0);
3065 SDValue Op1 = Op.getOperand(1);
3066 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003067 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3068 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00003069 DAG.getIntPtrConstant(0));
3070 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003071 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3072 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00003073 DAG.getIntPtrConstant(1));
3074 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003075}
3076
Dan Gohmand858e902010-04-17 15:26:15 +00003077SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003078 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003079 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00003080 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00003081 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003082 case ISD::GlobalAddress:
3083 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
3084 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00003085 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00003086 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
3087 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003088 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00003089 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00003090 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00003091 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00003092 case ISD::SINT_TO_FP:
3093 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
3094 case ISD::FP_TO_SINT:
3095 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003096 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00003097 case ISD::RETURNADDR: break;
Jim Grosbach0e0da732009-05-12 23:59:14 +00003098 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003099 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00003100 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
3101 Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00003102 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003103 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00003104 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00003105 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00003106 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003107 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00003108 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00003109 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003110 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
3111 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3112 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003113 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00003114 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003115 }
Dan Gohman475871a2008-07-27 21:46:04 +00003116 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003117}
3118
Duncan Sands1607f052008-12-01 11:39:25 +00003119/// ReplaceNodeResults - Replace the results of node with an illegal result
3120/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00003121void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
3122 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00003123 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00003124 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00003125 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00003126 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003127 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003128 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003129 case ISD::BIT_CONVERT:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003130 Res = ExpandBIT_CONVERT(N, DAG);
3131 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00003132 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003133 case ISD::SRA:
3134 Res = LowerShift(N, DAG, Subtarget);
3135 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003136 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00003137 if (Res.getNode())
3138 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00003139}
Chris Lattner27a6c732007-11-24 07:07:01 +00003140
Evan Chenga8e29892007-01-19 07:51:42 +00003141//===----------------------------------------------------------------------===//
3142// ARM Scheduler Hooks
3143//===----------------------------------------------------------------------===//
3144
3145MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003146ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
3147 MachineBasicBlock *BB,
3148 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00003149 unsigned dest = MI->getOperand(0).getReg();
3150 unsigned ptr = MI->getOperand(1).getReg();
3151 unsigned oldval = MI->getOperand(2).getReg();
3152 unsigned newval = MI->getOperand(3).getReg();
3153 unsigned scratch = BB->getParent()->getRegInfo()
3154 .createVirtualRegister(ARM::GPRRegisterClass);
3155 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3156 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003157 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00003158
3159 unsigned ldrOpc, strOpc;
3160 switch (Size) {
3161 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003162 case 1:
3163 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
3164 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
3165 break;
3166 case 2:
3167 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3168 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3169 break;
3170 case 4:
3171 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3172 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3173 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00003174 }
3175
3176 MachineFunction *MF = BB->getParent();
3177 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3178 MachineFunction::iterator It = BB;
3179 ++It; // insert the new blocks after the current block
3180
3181 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3182 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3183 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3184 MF->insert(It, loop1MBB);
3185 MF->insert(It, loop2MBB);
3186 MF->insert(It, exitMBB);
3187 exitMBB->transferSuccessors(BB);
3188
3189 // thisMBB:
3190 // ...
3191 // fallthrough --> loop1MBB
3192 BB->addSuccessor(loop1MBB);
3193
3194 // loop1MBB:
3195 // ldrex dest, [ptr]
3196 // cmp dest, oldval
3197 // bne exitMBB
3198 BB = loop1MBB;
3199 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003200 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003201 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003202 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3203 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003204 BB->addSuccessor(loop2MBB);
3205 BB->addSuccessor(exitMBB);
3206
3207 // loop2MBB:
3208 // strex scratch, newval, [ptr]
3209 // cmp scratch, #0
3210 // bne loop1MBB
3211 BB = loop2MBB;
3212 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
3213 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003214 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003215 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003216 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3217 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003218 BB->addSuccessor(loop1MBB);
3219 BB->addSuccessor(exitMBB);
3220
3221 // exitMBB:
3222 // ...
3223 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00003224
3225 MF->DeleteMachineInstr(MI); // The instruction is gone now.
3226
Jim Grosbach5278eb82009-12-11 01:42:04 +00003227 return BB;
3228}
3229
3230MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003231ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
3232 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00003233 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
3234 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3235
3236 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003237 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003238 MachineFunction::iterator It = BB;
3239 ++It;
3240
3241 unsigned dest = MI->getOperand(0).getReg();
3242 unsigned ptr = MI->getOperand(1).getReg();
3243 unsigned incr = MI->getOperand(2).getReg();
3244 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00003245
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003246 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003247 unsigned ldrOpc, strOpc;
3248 switch (Size) {
3249 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003250 case 1:
3251 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00003252 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003253 break;
3254 case 2:
3255 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3256 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3257 break;
3258 case 4:
3259 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3260 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3261 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00003262 }
3263
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003264 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3265 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3266 MF->insert(It, loopMBB);
3267 MF->insert(It, exitMBB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003268 exitMBB->transferSuccessors(BB);
3269
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003270 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003271 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3272 unsigned scratch2 = (!BinOpcode) ? incr :
3273 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3274
3275 // thisMBB:
3276 // ...
3277 // fallthrough --> loopMBB
3278 BB->addSuccessor(loopMBB);
3279
3280 // loopMBB:
3281 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003282 // <binop> scratch2, dest, incr
3283 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00003284 // cmp scratch, #0
3285 // bne- loopMBB
3286 // fallthrough --> exitMBB
3287 BB = loopMBB;
3288 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00003289 if (BinOpcode) {
3290 // operand order needs to go the other way for NAND
3291 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
3292 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3293 addReg(incr).addReg(dest)).addReg(0);
3294 else
3295 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3296 addReg(dest).addReg(incr)).addReg(0);
3297 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00003298
3299 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
3300 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003301 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00003302 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003303 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3304 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003305
3306 BB->addSuccessor(loopMBB);
3307 BB->addSuccessor(exitMBB);
3308
3309 // exitMBB:
3310 // ...
3311 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00003312
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003313 MF->DeleteMachineInstr(MI); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00003314
Jim Grosbachc3c23542009-12-14 04:22:04 +00003315 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00003316}
3317
3318MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00003319ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003320 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003321 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00003322 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003323 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00003324 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00003325 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00003326 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00003327 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00003328
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003329 case ARM::ATOMIC_LOAD_ADD_I8:
3330 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3331 case ARM::ATOMIC_LOAD_ADD_I16:
3332 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3333 case ARM::ATOMIC_LOAD_ADD_I32:
3334 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003335
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003336 case ARM::ATOMIC_LOAD_AND_I8:
3337 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3338 case ARM::ATOMIC_LOAD_AND_I16:
3339 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3340 case ARM::ATOMIC_LOAD_AND_I32:
3341 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003342
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003343 case ARM::ATOMIC_LOAD_OR_I8:
3344 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3345 case ARM::ATOMIC_LOAD_OR_I16:
3346 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3347 case ARM::ATOMIC_LOAD_OR_I32:
3348 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003349
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003350 case ARM::ATOMIC_LOAD_XOR_I8:
3351 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3352 case ARM::ATOMIC_LOAD_XOR_I16:
3353 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3354 case ARM::ATOMIC_LOAD_XOR_I32:
3355 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003356
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003357 case ARM::ATOMIC_LOAD_NAND_I8:
3358 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3359 case ARM::ATOMIC_LOAD_NAND_I16:
3360 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3361 case ARM::ATOMIC_LOAD_NAND_I32:
3362 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003363
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003364 case ARM::ATOMIC_LOAD_SUB_I8:
3365 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3366 case ARM::ATOMIC_LOAD_SUB_I16:
3367 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3368 case ARM::ATOMIC_LOAD_SUB_I32:
3369 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003370
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003371 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
3372 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
3373 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00003374
3375 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
3376 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
3377 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003378
Evan Cheng007ea272009-08-12 05:17:19 +00003379 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00003380 // To "insert" a SELECT_CC instruction, we actually have to insert the
3381 // diamond control-flow pattern. The incoming instruction knows the
3382 // destination vreg to set, the condition code register to branch on, the
3383 // true/false values to select between, and a branch opcode to use.
3384 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003385 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00003386 ++It;
3387
3388 // thisMBB:
3389 // ...
3390 // TrueVal = ...
3391 // cmpTY ccX, r1, r2
3392 // bCC copy1MBB
3393 // fallthrough --> copy0MBB
3394 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003395 MachineFunction *F = BB->getParent();
3396 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3397 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00003398 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00003399 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003400 F->insert(It, copy0MBB);
3401 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00003402 // Update machine-CFG edges by first adding all successors of the current
3403 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00003404 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003405 E = BB->succ_end(); I != E; ++I)
Evan Chengce319102009-09-19 09:51:03 +00003406 sinkMBB->addSuccessor(*I);
Evan Chenga8e29892007-01-19 07:51:42 +00003407 // Next, remove all successors of the current block, and add the true
3408 // and fallthrough blocks as its successors.
Evan Chengce319102009-09-19 09:51:03 +00003409 while (!BB->succ_empty())
Evan Chenga8e29892007-01-19 07:51:42 +00003410 BB->removeSuccessor(BB->succ_begin());
3411 BB->addSuccessor(copy0MBB);
3412 BB->addSuccessor(sinkMBB);
3413
3414 // copy0MBB:
3415 // %FalseValue = ...
3416 // # fallthrough to sinkMBB
3417 BB = copy0MBB;
3418
3419 // Update machine-CFG edges
3420 BB->addSuccessor(sinkMBB);
3421
3422 // sinkMBB:
3423 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3424 // ...
3425 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00003426 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00003427 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
3428 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
3429
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003430 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00003431 return BB;
3432 }
Evan Cheng86198642009-08-07 00:34:42 +00003433
3434 case ARM::tANDsp:
3435 case ARM::tADDspr_:
3436 case ARM::tSUBspi_:
3437 case ARM::t2SUBrSPi_:
3438 case ARM::t2SUBrSPi12_:
3439 case ARM::t2SUBrSPs_: {
3440 MachineFunction *MF = BB->getParent();
3441 unsigned DstReg = MI->getOperand(0).getReg();
3442 unsigned SrcReg = MI->getOperand(1).getReg();
3443 bool DstIsDead = MI->getOperand(0).isDead();
3444 bool SrcIsKill = MI->getOperand(1).isKill();
3445
3446 if (SrcReg != ARM::SP) {
3447 // Copy the source to SP from virtual register.
3448 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
3449 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3450 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
3451 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
3452 .addReg(SrcReg, getKillRegState(SrcIsKill));
3453 }
3454
3455 unsigned OpOpc = 0;
3456 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
3457 switch (MI->getOpcode()) {
3458 default:
3459 llvm_unreachable("Unexpected pseudo instruction!");
3460 case ARM::tANDsp:
3461 OpOpc = ARM::tAND;
3462 NeedPred = true;
3463 break;
3464 case ARM::tADDspr_:
3465 OpOpc = ARM::tADDspr;
3466 break;
3467 case ARM::tSUBspi_:
3468 OpOpc = ARM::tSUBspi;
3469 break;
3470 case ARM::t2SUBrSPi_:
3471 OpOpc = ARM::t2SUBrSPi;
3472 NeedPred = true; NeedCC = true;
3473 break;
3474 case ARM::t2SUBrSPi12_:
3475 OpOpc = ARM::t2SUBrSPi12;
3476 NeedPred = true;
3477 break;
3478 case ARM::t2SUBrSPs_:
3479 OpOpc = ARM::t2SUBrSPs;
3480 NeedPred = true; NeedCC = true; NeedOp3 = true;
3481 break;
3482 }
3483 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
3484 if (OpOpc == ARM::tAND)
3485 AddDefaultT1CC(MIB);
3486 MIB.addReg(ARM::SP);
3487 MIB.addOperand(MI->getOperand(2));
3488 if (NeedOp3)
3489 MIB.addOperand(MI->getOperand(3));
3490 if (NeedPred)
3491 AddDefaultPred(MIB);
3492 if (NeedCC)
3493 AddDefaultCC(MIB);
3494
3495 // Copy the result from SP to virtual register.
3496 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
3497 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3498 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
3499 BuildMI(BB, dl, TII->get(CopyOpc))
3500 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
3501 .addReg(ARM::SP);
3502 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3503 return BB;
3504 }
Evan Chenga8e29892007-01-19 07:51:42 +00003505 }
3506}
3507
3508//===----------------------------------------------------------------------===//
3509// ARM Optimization Hooks
3510//===----------------------------------------------------------------------===//
3511
Chris Lattnerd1980a52009-03-12 06:52:53 +00003512static
3513SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3514 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00003515 SelectionDAG &DAG = DCI.DAG;
3516 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003517 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00003518 unsigned Opc = N->getOpcode();
3519 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3520 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3521 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3522 ISD::CondCode CC = ISD::SETCC_INVALID;
3523
3524 if (isSlctCC) {
3525 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
3526 } else {
3527 SDValue CCOp = Slct.getOperand(0);
3528 if (CCOp.getOpcode() == ISD::SETCC)
3529 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
3530 }
3531
3532 bool DoXform = false;
3533 bool InvCC = false;
3534 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
3535 "Bad input!");
3536
3537 if (LHS.getOpcode() == ISD::Constant &&
3538 cast<ConstantSDNode>(LHS)->isNullValue()) {
3539 DoXform = true;
3540 } else if (CC != ISD::SETCC_INVALID &&
3541 RHS.getOpcode() == ISD::Constant &&
3542 cast<ConstantSDNode>(RHS)->isNullValue()) {
3543 std::swap(LHS, RHS);
3544 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003545 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00003546 Op0.getOperand(0).getValueType();
3547 bool isInt = OpVT.isInteger();
3548 CC = ISD::getSetCCInverse(CC, isInt);
3549
3550 if (!TLI.isCondCodeLegal(CC, OpVT))
3551 return SDValue(); // Inverse operator isn't legal.
3552
3553 DoXform = true;
3554 InvCC = true;
3555 }
3556
3557 if (DoXform) {
3558 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3559 if (isSlctCC)
3560 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3561 Slct.getOperand(0), Slct.getOperand(1), CC);
3562 SDValue CCOp = Slct.getOperand(0);
3563 if (InvCC)
3564 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3565 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3566 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3567 CCOp, OtherOp, Result);
3568 }
3569 return SDValue();
3570}
3571
3572/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3573static SDValue PerformADDCombine(SDNode *N,
3574 TargetLowering::DAGCombinerInfo &DCI) {
3575 // added by evan in r37685 with no testcase.
3576 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003577
Chris Lattnerd1980a52009-03-12 06:52:53 +00003578 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3579 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3580 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3581 if (Result.getNode()) return Result;
3582 }
3583 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3584 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3585 if (Result.getNode()) return Result;
3586 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003587
Chris Lattnerd1980a52009-03-12 06:52:53 +00003588 return SDValue();
3589}
3590
3591/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3592static SDValue PerformSUBCombine(SDNode *N,
3593 TargetLowering::DAGCombinerInfo &DCI) {
3594 // added by evan in r37685 with no testcase.
3595 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003596
Chris Lattnerd1980a52009-03-12 06:52:53 +00003597 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3598 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3599 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3600 if (Result.getNode()) return Result;
3601 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003602
Chris Lattnerd1980a52009-03-12 06:52:53 +00003603 return SDValue();
3604}
3605
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003606static SDValue PerformMULCombine(SDNode *N,
3607 TargetLowering::DAGCombinerInfo &DCI,
3608 const ARMSubtarget *Subtarget) {
3609 SelectionDAG &DAG = DCI.DAG;
3610
3611 if (Subtarget->isThumb1Only())
3612 return SDValue();
3613
3614 if (DAG.getMachineFunction().
3615 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
3616 return SDValue();
3617
3618 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
3619 return SDValue();
3620
3621 EVT VT = N->getValueType(0);
3622 if (VT != MVT::i32)
3623 return SDValue();
3624
3625 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
3626 if (!C)
3627 return SDValue();
3628
3629 uint64_t MulAmt = C->getZExtValue();
3630 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
3631 ShiftAmt = ShiftAmt & (32 - 1);
3632 SDValue V = N->getOperand(0);
3633 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003634
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003635 SDValue Res;
3636 MulAmt >>= ShiftAmt;
3637 if (isPowerOf2_32(MulAmt - 1)) {
3638 // (mul x, 2^N + 1) => (add (shl x, N), x)
3639 Res = DAG.getNode(ISD::ADD, DL, VT,
3640 V, DAG.getNode(ISD::SHL, DL, VT,
3641 V, DAG.getConstant(Log2_32(MulAmt-1),
3642 MVT::i32)));
3643 } else if (isPowerOf2_32(MulAmt + 1)) {
3644 // (mul x, 2^N - 1) => (sub (shl x, N), x)
3645 Res = DAG.getNode(ISD::SUB, DL, VT,
3646 DAG.getNode(ISD::SHL, DL, VT,
3647 V, DAG.getConstant(Log2_32(MulAmt+1),
3648 MVT::i32)),
3649 V);
3650 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003651 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003652
3653 if (ShiftAmt != 0)
3654 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
3655 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003656
3657 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003658 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003659 return SDValue();
3660}
3661
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +00003662/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
3663/// ARMISD::VMOVRRD.
Jim Grosbache5165492009-11-09 00:11:35 +00003664static SDValue PerformVMOVRRDCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003665 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003666 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003667 SDValue InDouble = N->getOperand(0);
Jim Grosbache5165492009-11-09 00:11:35 +00003668 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003669 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003670 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003671}
3672
Bob Wilson5bafff32009-06-22 23:27:02 +00003673/// getVShiftImm - Check if this is a valid build_vector for the immediate
3674/// operand of a vector shift operation, where all the elements of the
3675/// build_vector must have the same constant integer value.
3676static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3677 // Ignore bit_converts.
3678 while (Op.getOpcode() == ISD::BIT_CONVERT)
3679 Op = Op.getOperand(0);
3680 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3681 APInt SplatBits, SplatUndef;
3682 unsigned SplatBitSize;
3683 bool HasAnyUndefs;
3684 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3685 HasAnyUndefs, ElementBits) ||
3686 SplatBitSize > ElementBits)
3687 return false;
3688 Cnt = SplatBits.getSExtValue();
3689 return true;
3690}
3691
3692/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3693/// operand of a vector shift left operation. That value must be in the range:
3694/// 0 <= Value < ElementBits for a left shift; or
3695/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003696static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003697 assert(VT.isVector() && "vector shift count is not a vector type");
3698 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3699 if (! getVShiftImm(Op, ElementBits, Cnt))
3700 return false;
3701 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3702}
3703
3704/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3705/// operand of a vector shift right operation. For a shift opcode, the value
3706/// is positive, but for an intrinsic the value count must be negative. The
3707/// absolute value must be in the range:
3708/// 1 <= |Value| <= ElementBits for a right shift; or
3709/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003710static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00003711 int64_t &Cnt) {
3712 assert(VT.isVector() && "vector shift count is not a vector type");
3713 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3714 if (! getVShiftImm(Op, ElementBits, Cnt))
3715 return false;
3716 if (isIntrinsic)
3717 Cnt = -Cnt;
3718 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3719}
3720
3721/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3722static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3723 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3724 switch (IntNo) {
3725 default:
3726 // Don't do anything for most intrinsics.
3727 break;
3728
3729 // Vector shifts: check for immediate versions and lower them.
3730 // Note: This is done during DAG combining instead of DAG legalizing because
3731 // the build_vectors for 64-bit vector element shift counts are generally
3732 // not legal, and it is hard to see their values after they get legalized to
3733 // loads from a constant pool.
3734 case Intrinsic::arm_neon_vshifts:
3735 case Intrinsic::arm_neon_vshiftu:
3736 case Intrinsic::arm_neon_vshiftls:
3737 case Intrinsic::arm_neon_vshiftlu:
3738 case Intrinsic::arm_neon_vshiftn:
3739 case Intrinsic::arm_neon_vrshifts:
3740 case Intrinsic::arm_neon_vrshiftu:
3741 case Intrinsic::arm_neon_vrshiftn:
3742 case Intrinsic::arm_neon_vqshifts:
3743 case Intrinsic::arm_neon_vqshiftu:
3744 case Intrinsic::arm_neon_vqshiftsu:
3745 case Intrinsic::arm_neon_vqshiftns:
3746 case Intrinsic::arm_neon_vqshiftnu:
3747 case Intrinsic::arm_neon_vqshiftnsu:
3748 case Intrinsic::arm_neon_vqrshiftns:
3749 case Intrinsic::arm_neon_vqrshiftnu:
3750 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00003751 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003752 int64_t Cnt;
3753 unsigned VShiftOpc = 0;
3754
3755 switch (IntNo) {
3756 case Intrinsic::arm_neon_vshifts:
3757 case Intrinsic::arm_neon_vshiftu:
3758 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3759 VShiftOpc = ARMISD::VSHL;
3760 break;
3761 }
3762 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3763 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3764 ARMISD::VSHRs : ARMISD::VSHRu);
3765 break;
3766 }
3767 return SDValue();
3768
3769 case Intrinsic::arm_neon_vshiftls:
3770 case Intrinsic::arm_neon_vshiftlu:
3771 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3772 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003773 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003774
3775 case Intrinsic::arm_neon_vrshifts:
3776 case Intrinsic::arm_neon_vrshiftu:
3777 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3778 break;
3779 return SDValue();
3780
3781 case Intrinsic::arm_neon_vqshifts:
3782 case Intrinsic::arm_neon_vqshiftu:
3783 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3784 break;
3785 return SDValue();
3786
3787 case Intrinsic::arm_neon_vqshiftsu:
3788 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3789 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003790 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003791
3792 case Intrinsic::arm_neon_vshiftn:
3793 case Intrinsic::arm_neon_vrshiftn:
3794 case Intrinsic::arm_neon_vqshiftns:
3795 case Intrinsic::arm_neon_vqshiftnu:
3796 case Intrinsic::arm_neon_vqshiftnsu:
3797 case Intrinsic::arm_neon_vqrshiftns:
3798 case Intrinsic::arm_neon_vqrshiftnu:
3799 case Intrinsic::arm_neon_vqrshiftnsu:
3800 // Narrowing shifts require an immediate right shift.
3801 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3802 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003803 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003804
3805 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003806 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003807 }
3808
3809 switch (IntNo) {
3810 case Intrinsic::arm_neon_vshifts:
3811 case Intrinsic::arm_neon_vshiftu:
3812 // Opcode already set above.
3813 break;
3814 case Intrinsic::arm_neon_vshiftls:
3815 case Intrinsic::arm_neon_vshiftlu:
3816 if (Cnt == VT.getVectorElementType().getSizeInBits())
3817 VShiftOpc = ARMISD::VSHLLi;
3818 else
3819 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3820 ARMISD::VSHLLs : ARMISD::VSHLLu);
3821 break;
3822 case Intrinsic::arm_neon_vshiftn:
3823 VShiftOpc = ARMISD::VSHRN; break;
3824 case Intrinsic::arm_neon_vrshifts:
3825 VShiftOpc = ARMISD::VRSHRs; break;
3826 case Intrinsic::arm_neon_vrshiftu:
3827 VShiftOpc = ARMISD::VRSHRu; break;
3828 case Intrinsic::arm_neon_vrshiftn:
3829 VShiftOpc = ARMISD::VRSHRN; break;
3830 case Intrinsic::arm_neon_vqshifts:
3831 VShiftOpc = ARMISD::VQSHLs; break;
3832 case Intrinsic::arm_neon_vqshiftu:
3833 VShiftOpc = ARMISD::VQSHLu; break;
3834 case Intrinsic::arm_neon_vqshiftsu:
3835 VShiftOpc = ARMISD::VQSHLsu; break;
3836 case Intrinsic::arm_neon_vqshiftns:
3837 VShiftOpc = ARMISD::VQSHRNs; break;
3838 case Intrinsic::arm_neon_vqshiftnu:
3839 VShiftOpc = ARMISD::VQSHRNu; break;
3840 case Intrinsic::arm_neon_vqshiftnsu:
3841 VShiftOpc = ARMISD::VQSHRNsu; break;
3842 case Intrinsic::arm_neon_vqrshiftns:
3843 VShiftOpc = ARMISD::VQRSHRNs; break;
3844 case Intrinsic::arm_neon_vqrshiftnu:
3845 VShiftOpc = ARMISD::VQRSHRNu; break;
3846 case Intrinsic::arm_neon_vqrshiftnsu:
3847 VShiftOpc = ARMISD::VQRSHRNsu; break;
3848 }
3849
3850 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003851 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003852 }
3853
3854 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00003855 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003856 int64_t Cnt;
3857 unsigned VShiftOpc = 0;
3858
3859 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3860 VShiftOpc = ARMISD::VSLI;
3861 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3862 VShiftOpc = ARMISD::VSRI;
3863 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003864 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003865 }
3866
3867 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3868 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00003869 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003870 }
3871
3872 case Intrinsic::arm_neon_vqrshifts:
3873 case Intrinsic::arm_neon_vqrshiftu:
3874 // No immediate versions of these to check for.
3875 break;
3876 }
3877
3878 return SDValue();
3879}
3880
3881/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3882/// lowers them. As with the vector shift intrinsics, this is done during DAG
3883/// combining instead of DAG legalizing because the build_vectors for 64-bit
3884/// vector element shift counts are generally not legal, and it is hard to see
3885/// their values after they get legalized to loads from a constant pool.
3886static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3887 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003888 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003889
3890 // Nothing to be done for scalar shifts.
3891 if (! VT.isVector())
3892 return SDValue();
3893
3894 assert(ST->hasNEON() && "unexpected vector shift");
3895 int64_t Cnt;
3896
3897 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003898 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003899
3900 case ISD::SHL:
3901 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3902 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003903 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003904 break;
3905
3906 case ISD::SRA:
3907 case ISD::SRL:
3908 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3909 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3910 ARMISD::VSHRs : ARMISD::VSHRu);
3911 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003912 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003913 }
3914 }
3915 return SDValue();
3916}
3917
3918/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3919/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3920static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3921 const ARMSubtarget *ST) {
3922 SDValue N0 = N->getOperand(0);
3923
3924 // Check for sign- and zero-extensions of vector extract operations of 8-
3925 // and 16-bit vector elements. NEON supports these directly. They are
3926 // handled during DAG combining because type legalization will promote them
3927 // to 32-bit types and it is messy to recognize the operations after that.
3928 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3929 SDValue Vec = N0.getOperand(0);
3930 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003931 EVT VT = N->getValueType(0);
3932 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003933 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3934
Owen Anderson825b72b2009-08-11 20:47:22 +00003935 if (VT == MVT::i32 &&
3936 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00003937 TLI.isTypeLegal(Vec.getValueType())) {
3938
3939 unsigned Opc = 0;
3940 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003941 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003942 case ISD::SIGN_EXTEND:
3943 Opc = ARMISD::VGETLANEs;
3944 break;
3945 case ISD::ZERO_EXTEND:
3946 case ISD::ANY_EXTEND:
3947 Opc = ARMISD::VGETLANEu;
3948 break;
3949 }
3950 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
3951 }
3952 }
3953
3954 return SDValue();
3955}
3956
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003957/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
3958/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
3959static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
3960 const ARMSubtarget *ST) {
3961 // If the target supports NEON, try to use vmax/vmin instructions for f32
3962 // selects like "x < y ? x : y". Unless the FiniteOnlyFPMath option is set,
3963 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
3964 // a NaN; only do the transformation when it matches that behavior.
3965
3966 // For now only do this when using NEON for FP operations; if using VFP, it
3967 // is not obvious that the benefit outweighs the cost of switching to the
3968 // NEON pipeline.
3969 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
3970 N->getValueType(0) != MVT::f32)
3971 return SDValue();
3972
3973 SDValue CondLHS = N->getOperand(0);
3974 SDValue CondRHS = N->getOperand(1);
3975 SDValue LHS = N->getOperand(2);
3976 SDValue RHS = N->getOperand(3);
3977 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
3978
3979 unsigned Opcode = 0;
3980 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00003981 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003982 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00003983 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003984 IsReversed = true ; // x CC y ? y : x
3985 } else {
3986 return SDValue();
3987 }
3988
Bob Wilsone742bb52010-02-24 22:15:53 +00003989 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003990 switch (CC) {
3991 default: break;
3992 case ISD::SETOLT:
3993 case ISD::SETOLE:
3994 case ISD::SETLT:
3995 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003996 case ISD::SETULT:
3997 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00003998 // If LHS is NaN, an ordered comparison will be false and the result will
3999 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
4000 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4001 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
4002 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4003 break;
4004 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
4005 // will return -0, so vmin can only be used for unsafe math or if one of
4006 // the operands is known to be nonzero.
4007 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
4008 !UnsafeFPMath &&
4009 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4010 break;
4011 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004012 break;
4013
4014 case ISD::SETOGT:
4015 case ISD::SETOGE:
4016 case ISD::SETGT:
4017 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004018 case ISD::SETUGT:
4019 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004020 // If LHS is NaN, an ordered comparison will be false and the result will
4021 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
4022 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4023 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
4024 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4025 break;
4026 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
4027 // will return +0, so vmax can only be used for unsafe math or if one of
4028 // the operands is known to be nonzero.
4029 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
4030 !UnsafeFPMath &&
4031 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4032 break;
4033 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004034 break;
4035 }
4036
4037 if (!Opcode)
4038 return SDValue();
4039 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
4040}
4041
Dan Gohman475871a2008-07-27 21:46:04 +00004042SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004043 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004044 switch (N->getOpcode()) {
4045 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004046 case ISD::ADD: return PerformADDCombine(N, DCI);
4047 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004048 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbache5165492009-11-09 00:11:35 +00004049 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004050 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004051 case ISD::SHL:
4052 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004053 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004054 case ISD::SIGN_EXTEND:
4055 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004056 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
4057 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004058 }
Dan Gohman475871a2008-07-27 21:46:04 +00004059 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004060}
4061
Bill Wendlingaf566342009-08-15 21:21:19 +00004062bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
4063 if (!Subtarget->hasV6Ops())
4064 // Pre-v6 does not support unaligned mem access.
4065 return false;
Anton Korobeynikov90cfc132010-01-30 14:08:12 +00004066 else {
4067 // v6+ may or may not support unaligned mem access depending on the system
4068 // configuration.
4069 // FIXME: This is pretty conservative. Should we provide cmdline option to
4070 // control the behaviour?
Bill Wendlingaf566342009-08-15 21:21:19 +00004071 if (!Subtarget->isTargetDarwin())
4072 return false;
4073 }
4074
4075 switch (VT.getSimpleVT().SimpleTy) {
4076 default:
4077 return false;
4078 case MVT::i8:
4079 case MVT::i16:
4080 case MVT::i32:
4081 return true;
4082 // FIXME: VLD1 etc with standard alignment is legal.
4083 }
4084}
4085
Evan Chenge6c835f2009-08-14 20:09:37 +00004086static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
4087 if (V < 0)
4088 return false;
4089
4090 unsigned Scale = 1;
4091 switch (VT.getSimpleVT().SimpleTy) {
4092 default: return false;
4093 case MVT::i1:
4094 case MVT::i8:
4095 // Scale == 1;
4096 break;
4097 case MVT::i16:
4098 // Scale == 2;
4099 Scale = 2;
4100 break;
4101 case MVT::i32:
4102 // Scale == 4;
4103 Scale = 4;
4104 break;
4105 }
4106
4107 if ((V & (Scale - 1)) != 0)
4108 return false;
4109 V /= Scale;
4110 return V == (V & ((1LL << 5) - 1));
4111}
4112
4113static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
4114 const ARMSubtarget *Subtarget) {
4115 bool isNeg = false;
4116 if (V < 0) {
4117 isNeg = true;
4118 V = - V;
4119 }
4120
4121 switch (VT.getSimpleVT().SimpleTy) {
4122 default: return false;
4123 case MVT::i1:
4124 case MVT::i8:
4125 case MVT::i16:
4126 case MVT::i32:
4127 // + imm12 or - imm8
4128 if (isNeg)
4129 return V == (V & ((1LL << 8) - 1));
4130 return V == (V & ((1LL << 12) - 1));
4131 case MVT::f32:
4132 case MVT::f64:
4133 // Same as ARM mode. FIXME: NEON?
4134 if (!Subtarget->hasVFP2())
4135 return false;
4136 if ((V & 3) != 0)
4137 return false;
4138 V >>= 2;
4139 return V == (V & ((1LL << 8) - 1));
4140 }
4141}
4142
Evan Chengb01fad62007-03-12 23:30:29 +00004143/// isLegalAddressImmediate - Return true if the integer value can be used
4144/// as the offset of the target addressing mode for load / store of the
4145/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00004146static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004147 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00004148 if (V == 0)
4149 return true;
4150
Evan Cheng65011532009-03-09 19:15:00 +00004151 if (!VT.isSimple())
4152 return false;
4153
Evan Chenge6c835f2009-08-14 20:09:37 +00004154 if (Subtarget->isThumb1Only())
4155 return isLegalT1AddressImmediate(V, VT);
4156 else if (Subtarget->isThumb2())
4157 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00004158
Evan Chenge6c835f2009-08-14 20:09:37 +00004159 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00004160 if (V < 0)
4161 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00004162 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00004163 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004164 case MVT::i1:
4165 case MVT::i8:
4166 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00004167 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004168 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004169 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00004170 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004171 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004172 case MVT::f32:
4173 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00004174 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00004175 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00004176 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00004177 return false;
4178 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004179 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00004180 }
Evan Chenga8e29892007-01-19 07:51:42 +00004181}
4182
Evan Chenge6c835f2009-08-14 20:09:37 +00004183bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
4184 EVT VT) const {
4185 int Scale = AM.Scale;
4186 if (Scale < 0)
4187 return false;
4188
4189 switch (VT.getSimpleVT().SimpleTy) {
4190 default: return false;
4191 case MVT::i1:
4192 case MVT::i8:
4193 case MVT::i16:
4194 case MVT::i32:
4195 if (Scale == 1)
4196 return true;
4197 // r + r << imm
4198 Scale = Scale & ~1;
4199 return Scale == 2 || Scale == 4 || Scale == 8;
4200 case MVT::i64:
4201 // r + r
4202 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
4203 return true;
4204 return false;
4205 case MVT::isVoid:
4206 // Note, we allow "void" uses (basically, uses that aren't loads or
4207 // stores), because arm allows folding a scale into many arithmetic
4208 // operations. This should be made more precise and revisited later.
4209
4210 // Allow r << imm, but the imm has to be a multiple of two.
4211 if (Scale & 1) return false;
4212 return isPowerOf2_32(Scale);
4213 }
4214}
4215
Chris Lattner37caf8c2007-04-09 23:33:39 +00004216/// isLegalAddressingMode - Return true if the addressing mode represented
4217/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004218bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004219 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004220 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00004221 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00004222 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004223
Chris Lattner37caf8c2007-04-09 23:33:39 +00004224 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004225 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004226 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004227
Chris Lattner37caf8c2007-04-09 23:33:39 +00004228 switch (AM.Scale) {
4229 case 0: // no scale reg, must be "r+i" or "r", or "i".
4230 break;
4231 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00004232 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00004233 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004234 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00004235 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004236 // ARM doesn't support any R+R*scale+imm addr modes.
4237 if (AM.BaseOffs)
4238 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004239
Bob Wilson2c7dab12009-04-08 17:55:28 +00004240 if (!VT.isSimple())
4241 return false;
4242
Evan Chenge6c835f2009-08-14 20:09:37 +00004243 if (Subtarget->isThumb2())
4244 return isLegalT2ScaledAddressingMode(AM, VT);
4245
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004246 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00004247 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00004248 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004249 case MVT::i1:
4250 case MVT::i8:
4251 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004252 if (Scale < 0) Scale = -Scale;
4253 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004254 return true;
4255 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00004256 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00004257 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00004258 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004259 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004260 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004261 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00004262 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004263
Owen Anderson825b72b2009-08-11 20:47:22 +00004264 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004265 // Note, we allow "void" uses (basically, uses that aren't loads or
4266 // stores), because arm allows folding a scale into many arithmetic
4267 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004268
Chris Lattner37caf8c2007-04-09 23:33:39 +00004269 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00004270 if (Scale & 1) return false;
4271 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00004272 }
4273 break;
Evan Chengb01fad62007-03-12 23:30:29 +00004274 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00004275 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00004276}
4277
Evan Cheng77e47512009-11-11 19:05:52 +00004278/// isLegalICmpImmediate - Return true if the specified immediate is legal
4279/// icmp immediate, that is the target has icmp instructions which can compare
4280/// a register against the immediate without having to materialize the
4281/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00004282bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00004283 if (!Subtarget->isThumb())
4284 return ARM_AM::getSOImmVal(Imm) != -1;
4285 if (Subtarget->isThumb2())
4286 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00004287 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00004288}
4289
Owen Andersone50ed302009-08-10 22:56:29 +00004290static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004291 bool isSEXTLoad, SDValue &Base,
4292 SDValue &Offset, bool &isInc,
4293 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00004294 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4295 return false;
4296
Owen Anderson825b72b2009-08-11 20:47:22 +00004297 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00004298 // AddressingMode 3
4299 Base = Ptr->getOperand(0);
4300 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004301 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004302 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004303 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004304 isInc = false;
4305 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4306 return true;
4307 }
4308 }
4309 isInc = (Ptr->getOpcode() == ISD::ADD);
4310 Offset = Ptr->getOperand(1);
4311 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004312 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00004313 // AddressingMode 2
4314 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004315 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004316 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004317 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004318 isInc = false;
4319 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4320 Base = Ptr->getOperand(0);
4321 return true;
4322 }
4323 }
4324
4325 if (Ptr->getOpcode() == ISD::ADD) {
4326 isInc = true;
4327 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
4328 if (ShOpcVal != ARM_AM::no_shift) {
4329 Base = Ptr->getOperand(1);
4330 Offset = Ptr->getOperand(0);
4331 } else {
4332 Base = Ptr->getOperand(0);
4333 Offset = Ptr->getOperand(1);
4334 }
4335 return true;
4336 }
4337
4338 isInc = (Ptr->getOpcode() == ISD::ADD);
4339 Base = Ptr->getOperand(0);
4340 Offset = Ptr->getOperand(1);
4341 return true;
4342 }
4343
Jim Grosbache5165492009-11-09 00:11:35 +00004344 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00004345 return false;
4346}
4347
Owen Andersone50ed302009-08-10 22:56:29 +00004348static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004349 bool isSEXTLoad, SDValue &Base,
4350 SDValue &Offset, bool &isInc,
4351 SelectionDAG &DAG) {
4352 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4353 return false;
4354
4355 Base = Ptr->getOperand(0);
4356 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
4357 int RHSC = (int)RHS->getZExtValue();
4358 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
4359 assert(Ptr->getOpcode() == ISD::ADD);
4360 isInc = false;
4361 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4362 return true;
4363 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
4364 isInc = Ptr->getOpcode() == ISD::ADD;
4365 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
4366 return true;
4367 }
4368 }
4369
4370 return false;
4371}
4372
Evan Chenga8e29892007-01-19 07:51:42 +00004373/// getPreIndexedAddressParts - returns true by value, base pointer and
4374/// offset pointer and addressing mode by reference if the node's address
4375/// can be legally represented as pre-indexed load / store address.
4376bool
Dan Gohman475871a2008-07-27 21:46:04 +00004377ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
4378 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004379 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004380 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004381 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004382 return false;
4383
Owen Andersone50ed302009-08-10 22:56:29 +00004384 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004385 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004386 bool isSEXTLoad = false;
4387 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4388 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004389 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004390 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4391 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
4392 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004393 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004394 } else
4395 return false;
4396
4397 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004398 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004399 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004400 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
4401 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004402 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004403 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00004404 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00004405 if (!isLegal)
4406 return false;
4407
4408 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
4409 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004410}
4411
4412/// getPostIndexedAddressParts - returns true by value, base pointer and
4413/// offset pointer and addressing mode by reference if this node can be
4414/// combined with a load / store to form a post-indexed load / store.
4415bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00004416 SDValue &Base,
4417 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004418 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004419 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004420 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004421 return false;
4422
Owen Andersone50ed302009-08-10 22:56:29 +00004423 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004424 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004425 bool isSEXTLoad = false;
4426 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004427 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00004428 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00004429 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4430 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004431 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00004432 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00004433 } else
4434 return false;
4435
4436 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004437 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004438 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004439 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00004440 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004441 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004442 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
4443 isInc, DAG);
4444 if (!isLegal)
4445 return false;
4446
Evan Cheng28dad2a2010-05-18 21:31:17 +00004447 if (Ptr != Base) {
4448 // Swap base ptr and offset to catch more post-index load / store when
4449 // it's legal. In Thumb2 mode, offset must be an immediate.
4450 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
4451 !Subtarget->isThumb2())
4452 std::swap(Base, Offset);
4453
4454 // Post-indexed load / store update the base pointer.
4455 if (Ptr != Base)
4456 return false;
4457 }
4458
Evan Chenge88d5ce2009-07-02 07:28:31 +00004459 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
4460 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004461}
4462
Dan Gohman475871a2008-07-27 21:46:04 +00004463void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00004464 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004465 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004466 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00004467 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00004468 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004469 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004470 switch (Op.getOpcode()) {
4471 default: break;
4472 case ARMISD::CMOV: {
4473 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00004474 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004475 if (KnownZero == 0 && KnownOne == 0) return;
4476
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004477 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00004478 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
4479 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004480 KnownZero &= KnownZeroRHS;
4481 KnownOne &= KnownOneRHS;
4482 return;
4483 }
4484 }
4485}
4486
4487//===----------------------------------------------------------------------===//
4488// ARM Inline Assembly Support
4489//===----------------------------------------------------------------------===//
4490
4491/// getConstraintType - Given a constraint letter, return the type of
4492/// constraint it is for this target.
4493ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00004494ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
4495 if (Constraint.size() == 1) {
4496 switch (Constraint[0]) {
4497 default: break;
4498 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004499 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00004500 }
Evan Chenga8e29892007-01-19 07:51:42 +00004501 }
Chris Lattner4234f572007-03-25 02:14:49 +00004502 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00004503}
4504
Bob Wilson2dc4f542009-03-20 22:42:55 +00004505std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00004506ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004507 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004508 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004509 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00004510 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004511 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004512 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004513 return std::make_pair(0U, ARM::tGPRRegisterClass);
4514 else
4515 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004516 case 'r':
4517 return std::make_pair(0U, ARM::GPRRegisterClass);
4518 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004519 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004520 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00004521 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004522 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00004523 if (VT.getSizeInBits() == 128)
4524 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004525 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004526 }
4527 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00004528 if (StringRef("{cc}").equals_lower(Constraint))
4529 return std::make_pair(0U, ARM::CCRRegisterClass);
4530
Evan Chenga8e29892007-01-19 07:51:42 +00004531 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
4532}
4533
4534std::vector<unsigned> ARMTargetLowering::
4535getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004536 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004537 if (Constraint.size() != 1)
4538 return std::vector<unsigned>();
4539
4540 switch (Constraint[0]) { // GCC ARM Constraint Letters
4541 default: break;
4542 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004543 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4544 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4545 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004546 case 'r':
4547 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4548 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4549 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
4550 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004551 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004552 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004553 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
4554 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
4555 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
4556 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
4557 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
4558 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
4559 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
4560 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00004561 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004562 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
4563 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
4564 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
4565 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00004566 if (VT.getSizeInBits() == 128)
4567 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
4568 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004569 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004570 }
4571
4572 return std::vector<unsigned>();
4573}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004574
4575/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
4576/// vector. If it is invalid, don't add anything to Ops.
4577void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
4578 char Constraint,
4579 bool hasMemory,
4580 std::vector<SDValue>&Ops,
4581 SelectionDAG &DAG) const {
4582 SDValue Result(0, 0);
4583
4584 switch (Constraint) {
4585 default: break;
4586 case 'I': case 'J': case 'K': case 'L':
4587 case 'M': case 'N': case 'O':
4588 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
4589 if (!C)
4590 return;
4591
4592 int64_t CVal64 = C->getSExtValue();
4593 int CVal = (int) CVal64;
4594 // None of these constraints allow values larger than 32 bits. Check
4595 // that the value fits in an int.
4596 if (CVal != CVal64)
4597 return;
4598
4599 switch (Constraint) {
4600 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004601 if (Subtarget->isThumb1Only()) {
4602 // This must be a constant between 0 and 255, for ADD
4603 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004604 if (CVal >= 0 && CVal <= 255)
4605 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004606 } else if (Subtarget->isThumb2()) {
4607 // A constant that can be used as an immediate value in a
4608 // data-processing instruction.
4609 if (ARM_AM::getT2SOImmVal(CVal) != -1)
4610 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004611 } else {
4612 // A constant that can be used as an immediate value in a
4613 // data-processing instruction.
4614 if (ARM_AM::getSOImmVal(CVal) != -1)
4615 break;
4616 }
4617 return;
4618
4619 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004620 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004621 // This must be a constant between -255 and -1, for negated ADD
4622 // immediates. This can be used in GCC with an "n" modifier that
4623 // prints the negated value, for use with SUB instructions. It is
4624 // not useful otherwise but is implemented for compatibility.
4625 if (CVal >= -255 && CVal <= -1)
4626 break;
4627 } else {
4628 // This must be a constant between -4095 and 4095. It is not clear
4629 // what this constraint is intended for. Implemented for
4630 // compatibility with GCC.
4631 if (CVal >= -4095 && CVal <= 4095)
4632 break;
4633 }
4634 return;
4635
4636 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004637 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004638 // A 32-bit value where only one byte has a nonzero value. Exclude
4639 // zero to match GCC. This constraint is used by GCC internally for
4640 // constants that can be loaded with a move/shift combination.
4641 // It is not useful otherwise but is implemented for compatibility.
4642 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
4643 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004644 } else if (Subtarget->isThumb2()) {
4645 // A constant whose bitwise inverse can be used as an immediate
4646 // value in a data-processing instruction. This can be used in GCC
4647 // with a "B" modifier that prints the inverted value, for use with
4648 // BIC and MVN instructions. It is not useful otherwise but is
4649 // implemented for compatibility.
4650 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
4651 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004652 } else {
4653 // A constant whose bitwise inverse can be used as an immediate
4654 // value in a data-processing instruction. This can be used in GCC
4655 // with a "B" modifier that prints the inverted value, for use with
4656 // BIC and MVN instructions. It is not useful otherwise but is
4657 // implemented for compatibility.
4658 if (ARM_AM::getSOImmVal(~CVal) != -1)
4659 break;
4660 }
4661 return;
4662
4663 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004664 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004665 // This must be a constant between -7 and 7,
4666 // for 3-operand ADD/SUB immediate instructions.
4667 if (CVal >= -7 && CVal < 7)
4668 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004669 } else if (Subtarget->isThumb2()) {
4670 // A constant whose negation can be used as an immediate value in a
4671 // data-processing instruction. This can be used in GCC with an "n"
4672 // modifier that prints the negated value, for use with SUB
4673 // instructions. It is not useful otherwise but is implemented for
4674 // compatibility.
4675 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
4676 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004677 } else {
4678 // A constant whose negation can be used as an immediate value in a
4679 // data-processing instruction. This can be used in GCC with an "n"
4680 // modifier that prints the negated value, for use with SUB
4681 // instructions. It is not useful otherwise but is implemented for
4682 // compatibility.
4683 if (ARM_AM::getSOImmVal(-CVal) != -1)
4684 break;
4685 }
4686 return;
4687
4688 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004689 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004690 // This must be a multiple of 4 between 0 and 1020, for
4691 // ADD sp + immediate.
4692 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
4693 break;
4694 } else {
4695 // A power of two or a constant between 0 and 32. This is used in
4696 // GCC for the shift amount on shifted register operands, but it is
4697 // useful in general for any shift amounts.
4698 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
4699 break;
4700 }
4701 return;
4702
4703 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004704 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004705 // This must be a constant between 0 and 31, for shift amounts.
4706 if (CVal >= 0 && CVal <= 31)
4707 break;
4708 }
4709 return;
4710
4711 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004712 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004713 // This must be a multiple of 4 between -508 and 508, for
4714 // ADD/SUB sp = sp + immediate.
4715 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
4716 break;
4717 }
4718 return;
4719 }
4720 Result = DAG.getTargetConstant(CVal, Op.getValueType());
4721 break;
4722 }
4723
4724 if (Result.getNode()) {
4725 Ops.push_back(Result);
4726 return;
4727 }
4728 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
4729 Ops, DAG);
4730}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00004731
4732bool
4733ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4734 // The ARM target isn't yet aware of offsets.
4735 return false;
4736}
Evan Cheng39382422009-10-28 01:44:26 +00004737
4738int ARM::getVFPf32Imm(const APFloat &FPImm) {
4739 APInt Imm = FPImm.bitcastToAPInt();
4740 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
4741 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
4742 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
4743
4744 // We can handle 4 bits of mantissa.
4745 // mantissa = (16+UInt(e:f:g:h))/16.
4746 if (Mantissa & 0x7ffff)
4747 return -1;
4748 Mantissa >>= 19;
4749 if ((Mantissa & 0xf) != Mantissa)
4750 return -1;
4751
4752 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4753 if (Exp < -3 || Exp > 4)
4754 return -1;
4755 Exp = ((Exp+3) & 0x7) ^ 4;
4756
4757 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4758}
4759
4760int ARM::getVFPf64Imm(const APFloat &FPImm) {
4761 APInt Imm = FPImm.bitcastToAPInt();
4762 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
4763 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
4764 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
4765
4766 // We can handle 4 bits of mantissa.
4767 // mantissa = (16+UInt(e:f:g:h))/16.
4768 if (Mantissa & 0xffffffffffffLL)
4769 return -1;
4770 Mantissa >>= 48;
4771 if ((Mantissa & 0xf) != Mantissa)
4772 return -1;
4773
4774 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4775 if (Exp < -3 || Exp > 4)
4776 return -1;
4777 Exp = ((Exp+3) & 0x7) ^ 4;
4778
4779 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4780}
4781
4782/// isFPImmLegal - Returns true if the target can instruction select the
4783/// specified FP immediate natively. If false, the legalizer will
4784/// materialize the FP immediate as a load from a constant pool.
4785bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
4786 if (!Subtarget->hasVFP3())
4787 return false;
4788 if (VT == MVT::f32)
4789 return ARM::getVFPf32Imm(Imm) != -1;
4790 if (VT == MVT::f64)
4791 return ARM::getVFPf64Imm(Imm) != -1;
4792 return false;
4793}