blob: 0b86e3808ca47e0860fc288e2ed1ea8b53ede12d [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);
Jim Grosbach5eb19512010-05-22 01:06:18 +0000415 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000416
Owen Anderson825b72b2009-08-11 20:47:22 +0000417 setOperationAction(ISD::SETCC, MVT::i32, Expand);
418 setOperationAction(ISD::SETCC, MVT::f32, Expand);
419 setOperationAction(ISD::SETCC, MVT::f64, Expand);
420 setOperationAction(ISD::SELECT, MVT::i32, Expand);
421 setOperationAction(ISD::SELECT, MVT::f32, Expand);
422 setOperationAction(ISD::SELECT, MVT::f64, Expand);
423 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
424 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
425 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000426
Owen Anderson825b72b2009-08-11 20:47:22 +0000427 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
428 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
429 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
430 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
431 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000432
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000433 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000434 setOperationAction(ISD::FSIN, MVT::f64, Expand);
435 setOperationAction(ISD::FSIN, MVT::f32, Expand);
436 setOperationAction(ISD::FCOS, MVT::f32, Expand);
437 setOperationAction(ISD::FCOS, MVT::f64, Expand);
438 setOperationAction(ISD::FREM, MVT::f64, Expand);
439 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000440 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
442 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000443 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000444 setOperationAction(ISD::FPOW, MVT::f64, Expand);
445 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000446
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000447 // Various VFP goodness
448 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000449 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
450 if (Subtarget->hasVFP2()) {
451 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
452 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
453 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
454 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
455 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000456 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000457 if (!Subtarget->hasFP16()) {
458 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
459 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000460 }
Evan Cheng110cf482008-04-01 01:50:16 +0000461 }
Evan Chenga8e29892007-01-19 07:51:42 +0000462
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000463 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000464 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000465 setTargetDAGCombine(ISD::ADD);
466 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000467 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000468
Evan Chenga8e29892007-01-19 07:51:42 +0000469 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000470
Evan Chengf7d87ee2010-05-21 00:43:17 +0000471 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
472 setSchedulingPreference(Sched::RegPressure);
473 else
474 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000475
Evan Chengbc9b7542009-08-15 07:59:10 +0000476 // FIXME: If-converter should use instruction latency to determine
477 // profitability rather than relying on fixed limits.
478 if (Subtarget->getCPUString() == "generic") {
479 // Generic (and overly aggressive) if-conversion limits.
480 setIfCvtBlockSizeLimit(10);
481 setIfCvtDupBlockSizeLimit(2);
Jim Grosbach35075a72010-03-24 16:15:14 +0000482 } else if (Subtarget->hasV7Ops()) {
Jim Grosbachfceabef2010-03-24 00:03:13 +0000483 setIfCvtBlockSizeLimit(3);
484 setIfCvtDupBlockSizeLimit(1);
Evan Chengbc9b7542009-08-15 07:59:10 +0000485 } else if (Subtarget->hasV6Ops()) {
486 setIfCvtBlockSizeLimit(2);
487 setIfCvtDupBlockSizeLimit(1);
488 } else {
489 setIfCvtBlockSizeLimit(3);
490 setIfCvtDupBlockSizeLimit(2);
Evan Cheng8557c2b2009-06-19 01:51:50 +0000491 }
492
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000493 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Bob Wilsone6abdff2009-05-18 20:55:32 +0000494 // Do not enable CodePlacementOpt for now: it currently runs after the
495 // ARMConstantIslandPass and messes up branch relaxation and placement
496 // of constant islands.
497 // benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000498}
499
Evan Chenga8e29892007-01-19 07:51:42 +0000500const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
501 switch (Opcode) {
502 default: return 0;
503 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000504 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
505 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000506 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000507 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
508 case ARMISD::tCALL: return "ARMISD::tCALL";
509 case ARMISD::BRCOND: return "ARMISD::BRCOND";
510 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000511 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000512 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
513 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
514 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000515 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000516 case ARMISD::CMPFP: return "ARMISD::CMPFP";
517 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
518 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
519 case ARMISD::CMOV: return "ARMISD::CMOV";
520 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000521
Jim Grosbach3482c802010-01-18 19:58:49 +0000522 case ARMISD::RBIT: return "ARMISD::RBIT";
523
Bob Wilson76a312b2010-03-19 22:51:32 +0000524 case ARMISD::FTOSI: return "ARMISD::FTOSI";
525 case ARMISD::FTOUI: return "ARMISD::FTOUI";
526 case ARMISD::SITOF: return "ARMISD::SITOF";
527 case ARMISD::UITOF: return "ARMISD::UITOF";
528
Evan Chenga8e29892007-01-19 07:51:42 +0000529 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
530 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
531 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000532
Jim Grosbache5165492009-11-09 00:11:35 +0000533 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
534 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000535
Evan Chengc5942082009-10-28 06:55:03 +0000536 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
537 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
538
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000539 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000540
Evan Cheng86198642009-08-07 00:34:42 +0000541 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
542
Jim Grosbach3728e962009-12-10 00:11:09 +0000543 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
544 case ARMISD::SYNCBARRIER: return "ARMISD::SYNCBARRIER";
545
Bob Wilson5bafff32009-06-22 23:27:02 +0000546 case ARMISD::VCEQ: return "ARMISD::VCEQ";
547 case ARMISD::VCGE: return "ARMISD::VCGE";
548 case ARMISD::VCGEU: return "ARMISD::VCGEU";
549 case ARMISD::VCGT: return "ARMISD::VCGT";
550 case ARMISD::VCGTU: return "ARMISD::VCGTU";
551 case ARMISD::VTST: return "ARMISD::VTST";
552
553 case ARMISD::VSHL: return "ARMISD::VSHL";
554 case ARMISD::VSHRs: return "ARMISD::VSHRs";
555 case ARMISD::VSHRu: return "ARMISD::VSHRu";
556 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
557 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
558 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
559 case ARMISD::VSHRN: return "ARMISD::VSHRN";
560 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
561 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
562 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
563 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
564 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
565 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
566 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
567 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
568 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
569 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
570 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
571 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
572 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
573 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000574 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000575 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000576 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000577 case ARMISD::VREV64: return "ARMISD::VREV64";
578 case ARMISD::VREV32: return "ARMISD::VREV32";
579 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000580 case ARMISD::VZIP: return "ARMISD::VZIP";
581 case ARMISD::VUZP: return "ARMISD::VUZP";
582 case ARMISD::VTRN: return "ARMISD::VTRN";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000583 case ARMISD::FMAX: return "ARMISD::FMAX";
584 case ARMISD::FMIN: return "ARMISD::FMIN";
Evan Chenga8e29892007-01-19 07:51:42 +0000585 }
586}
587
Evan Cheng06b666c2010-05-15 02:18:07 +0000588/// getRegClassFor - Return the register class that should be used for the
589/// specified value type.
590TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
591 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
592 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
593 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000594 if (Subtarget->hasNEON()) {
595 if (VT == MVT::v4i64)
596 return ARM::QQPRRegisterClass;
597 else if (VT == MVT::v8i64)
598 return ARM::QQQQPRRegisterClass;
599 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000600 return TargetLowering::getRegClassFor(VT);
601}
602
Bill Wendlingb4202b82009-07-01 18:50:55 +0000603/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000604unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Evan Cheng048e36f2009-10-02 06:57:25 +0000605 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 0 : 1;
Bill Wendling20c568f2009-06-30 22:38:32 +0000606}
607
Evan Cheng1cc39842010-05-20 23:26:43 +0000608Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
609 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
610 EVT VT = N->getValueType(i);
611 if (VT.isFloatingPoint() || VT.isVector())
612 return Sched::Latency;
613 }
614 return Sched::RegPressure;
615}
616
Evan Chenga8e29892007-01-19 07:51:42 +0000617//===----------------------------------------------------------------------===//
618// Lowering Code
619//===----------------------------------------------------------------------===//
620
Evan Chenga8e29892007-01-19 07:51:42 +0000621/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
622static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
623 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000624 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000625 case ISD::SETNE: return ARMCC::NE;
626 case ISD::SETEQ: return ARMCC::EQ;
627 case ISD::SETGT: return ARMCC::GT;
628 case ISD::SETGE: return ARMCC::GE;
629 case ISD::SETLT: return ARMCC::LT;
630 case ISD::SETLE: return ARMCC::LE;
631 case ISD::SETUGT: return ARMCC::HI;
632 case ISD::SETUGE: return ARMCC::HS;
633 case ISD::SETULT: return ARMCC::LO;
634 case ISD::SETULE: return ARMCC::LS;
635 }
636}
637
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000638/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
639static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000640 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000641 CondCode2 = ARMCC::AL;
642 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000643 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000644 case ISD::SETEQ:
645 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
646 case ISD::SETGT:
647 case ISD::SETOGT: CondCode = ARMCC::GT; break;
648 case ISD::SETGE:
649 case ISD::SETOGE: CondCode = ARMCC::GE; break;
650 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000651 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000652 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
653 case ISD::SETO: CondCode = ARMCC::VC; break;
654 case ISD::SETUO: CondCode = ARMCC::VS; break;
655 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
656 case ISD::SETUGT: CondCode = ARMCC::HI; break;
657 case ISD::SETUGE: CondCode = ARMCC::PL; break;
658 case ISD::SETLT:
659 case ISD::SETULT: CondCode = ARMCC::LT; break;
660 case ISD::SETLE:
661 case ISD::SETULE: CondCode = ARMCC::LE; break;
662 case ISD::SETNE:
663 case ISD::SETUNE: CondCode = ARMCC::NE; break;
664 }
Evan Chenga8e29892007-01-19 07:51:42 +0000665}
666
Bob Wilson1f595bb2009-04-17 19:07:39 +0000667//===----------------------------------------------------------------------===//
668// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000669//===----------------------------------------------------------------------===//
670
671#include "ARMGenCallingConv.inc"
672
673// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000674static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000675 CCValAssign::LocInfo &LocInfo,
676 CCState &State, bool CanFail) {
677 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
678
679 // Try to get the first register.
680 if (unsigned Reg = State.AllocateReg(RegList, 4))
681 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
682 else {
683 // For the 2nd half of a v2f64, do not fail.
684 if (CanFail)
685 return false;
686
687 // Put the whole thing on the stack.
688 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
689 State.AllocateStack(8, 4),
690 LocVT, LocInfo));
691 return true;
692 }
693
694 // Try to get the second register.
695 if (unsigned Reg = State.AllocateReg(RegList, 4))
696 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
697 else
698 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
699 State.AllocateStack(4, 4),
700 LocVT, LocInfo));
701 return true;
702}
703
Owen Andersone50ed302009-08-10 22:56:29 +0000704static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000705 CCValAssign::LocInfo &LocInfo,
706 ISD::ArgFlagsTy &ArgFlags,
707 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000708 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
709 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000710 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000711 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
712 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000713 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000714}
715
716// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000717static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000718 CCValAssign::LocInfo &LocInfo,
719 CCState &State, bool CanFail) {
720 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
721 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
722
723 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
724 if (Reg == 0) {
725 // For the 2nd half of a v2f64, do not just fail.
726 if (CanFail)
727 return false;
728
729 // Put the whole thing on the stack.
730 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
731 State.AllocateStack(8, 8),
732 LocVT, LocInfo));
733 return true;
734 }
735
736 unsigned i;
737 for (i = 0; i < 2; ++i)
738 if (HiRegList[i] == Reg)
739 break;
740
741 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
742 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
743 LocVT, LocInfo));
744 return true;
745}
746
Owen Andersone50ed302009-08-10 22:56:29 +0000747static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000748 CCValAssign::LocInfo &LocInfo,
749 ISD::ArgFlagsTy &ArgFlags,
750 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000751 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
752 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000753 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000754 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
755 return false;
756 return true; // we handled it
757}
758
Owen Andersone50ed302009-08-10 22:56:29 +0000759static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000760 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000761 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
762 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
763
Bob Wilsone65586b2009-04-17 20:40:45 +0000764 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
765 if (Reg == 0)
766 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000767
Bob Wilsone65586b2009-04-17 20:40:45 +0000768 unsigned i;
769 for (i = 0; i < 2; ++i)
770 if (HiRegList[i] == Reg)
771 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000772
Bob Wilson5bafff32009-06-22 23:27:02 +0000773 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000774 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000775 LocVT, LocInfo));
776 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000777}
778
Owen Andersone50ed302009-08-10 22:56:29 +0000779static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000780 CCValAssign::LocInfo &LocInfo,
781 ISD::ArgFlagsTy &ArgFlags,
782 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000783 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
784 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000785 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000786 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000787 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000788}
789
Owen Andersone50ed302009-08-10 22:56:29 +0000790static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000791 CCValAssign::LocInfo &LocInfo,
792 ISD::ArgFlagsTy &ArgFlags,
793 CCState &State) {
794 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
795 State);
796}
797
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000798/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
799/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000800CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000801 bool Return,
802 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000803 switch (CC) {
804 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000805 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000806 case CallingConv::C:
807 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000808 // Use target triple & subtarget features to do actual dispatch.
809 if (Subtarget->isAAPCS_ABI()) {
810 if (Subtarget->hasVFP2() &&
811 FloatABIType == FloatABI::Hard && !isVarArg)
812 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
813 else
814 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
815 } else
816 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000817 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000818 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000819 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000820 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000821 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000822 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000823 }
824}
825
Dan Gohman98ca4f22009-08-05 01:29:28 +0000826/// LowerCallResult - Lower the result values of a call into the
827/// appropriate copies out of appropriate physical registers.
828SDValue
829ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000830 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000831 const SmallVectorImpl<ISD::InputArg> &Ins,
832 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000833 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000834
Bob Wilson1f595bb2009-04-17 19:07:39 +0000835 // Assign locations to each value returned by this call.
836 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000837 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000838 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000839 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000840 CCAssignFnForNode(CallConv, /* Return*/ true,
841 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000842
843 // Copy all of the result registers out of their specified physreg.
844 for (unsigned i = 0; i != RVLocs.size(); ++i) {
845 CCValAssign VA = RVLocs[i];
846
Bob Wilson80915242009-04-25 00:33:20 +0000847 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000848 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000849 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000850 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000851 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000852 Chain = Lo.getValue(1);
853 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000854 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000855 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000856 InFlag);
857 Chain = Hi.getValue(1);
858 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000859 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000860
Owen Anderson825b72b2009-08-11 20:47:22 +0000861 if (VA.getLocVT() == MVT::v2f64) {
862 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
863 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
864 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000865
866 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000867 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000868 Chain = Lo.getValue(1);
869 InFlag = Lo.getValue(2);
870 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000871 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000872 Chain = Hi.getValue(1);
873 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000874 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +0000875 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
876 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000877 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000878 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000879 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
880 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000881 Chain = Val.getValue(1);
882 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000883 }
Bob Wilson80915242009-04-25 00:33:20 +0000884
885 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000886 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000887 case CCValAssign::Full: break;
888 case CCValAssign::BCvt:
889 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
890 break;
891 }
892
Dan Gohman98ca4f22009-08-05 01:29:28 +0000893 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000894 }
895
Dan Gohman98ca4f22009-08-05 01:29:28 +0000896 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000897}
898
899/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
900/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000901/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000902/// a byval function parameter.
903/// Sometimes what we are copying is the end of a larger object, the part that
904/// does not fit in registers.
905static SDValue
906CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
907 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
908 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000909 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000910 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +0000911 /*isVolatile=*/false, /*AlwaysInline=*/false,
912 NULL, 0, NULL, 0);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000913}
914
Bob Wilsondee46d72009-04-17 20:35:10 +0000915/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000916SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000917ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
918 SDValue StackPtr, SDValue Arg,
919 DebugLoc dl, SelectionDAG &DAG,
920 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +0000921 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000922 unsigned LocMemOffset = VA.getLocMemOffset();
923 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
924 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
925 if (Flags.isByVal()) {
926 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
927 }
928 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene1b58cab2010-02-15 16:55:24 +0000929 PseudoSourceValue::getStack(), LocMemOffset,
930 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +0000931}
932
Dan Gohman98ca4f22009-08-05 01:29:28 +0000933void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000934 SDValue Chain, SDValue &Arg,
935 RegsToPassVector &RegsToPass,
936 CCValAssign &VA, CCValAssign &NextVA,
937 SDValue &StackPtr,
938 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +0000939 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +0000940
Jim Grosbache5165492009-11-09 00:11:35 +0000941 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000942 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000943 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
944
945 if (NextVA.isRegLoc())
946 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
947 else {
948 assert(NextVA.isMemLoc());
949 if (StackPtr.getNode() == 0)
950 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
951
Dan Gohman98ca4f22009-08-05 01:29:28 +0000952 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
953 dl, DAG, NextVA,
954 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000955 }
956}
957
Dan Gohman98ca4f22009-08-05 01:29:28 +0000958/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000959/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
960/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000961SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000962ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000963 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000964 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000965 const SmallVectorImpl<ISD::OutputArg> &Outs,
966 const SmallVectorImpl<ISD::InputArg> &Ins,
967 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000968 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +0000969 // ARM target does not yet support tail call optimization.
970 isTailCall = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000971
Bob Wilson1f595bb2009-04-17 19:07:39 +0000972 // Analyze operands of the call, assigning locations to each operand.
973 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000974 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
975 *DAG.getContext());
976 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000977 CCAssignFnForNode(CallConv, /* Return*/ false,
978 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +0000979
Bob Wilson1f595bb2009-04-17 19:07:39 +0000980 // Get a count of how many bytes are to be pushed on the stack.
981 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000982
983 // Adjust the stack pointer for the new arguments...
984 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000985 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000986
Jim Grosbachf9a4b762010-02-24 01:43:03 +0000987 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000988
Bob Wilson5bafff32009-06-22 23:27:02 +0000989 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000990 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000991
Bob Wilson1f595bb2009-04-17 19:07:39 +0000992 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +0000993 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000994 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
995 i != e;
996 ++i, ++realArgIdx) {
997 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000998 SDValue Arg = Outs[realArgIdx].Val;
999 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +00001000
Bob Wilson1f595bb2009-04-17 19:07:39 +00001001 // Promote the value if needed.
1002 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001003 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001004 case CCValAssign::Full: break;
1005 case CCValAssign::SExt:
1006 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1007 break;
1008 case CCValAssign::ZExt:
1009 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1010 break;
1011 case CCValAssign::AExt:
1012 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1013 break;
1014 case CCValAssign::BCvt:
1015 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1016 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001017 }
1018
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001019 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001020 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001021 if (VA.getLocVT() == MVT::v2f64) {
1022 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1023 DAG.getConstant(0, MVT::i32));
1024 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1025 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001026
Dan Gohman98ca4f22009-08-05 01:29:28 +00001027 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001028 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1029
1030 VA = ArgLocs[++i]; // skip ahead to next loc
1031 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001032 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001033 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1034 } else {
1035 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001036
Dan Gohman98ca4f22009-08-05 01:29:28 +00001037 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1038 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001039 }
1040 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001041 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001042 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001043 }
1044 } else if (VA.isRegLoc()) {
1045 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1046 } else {
1047 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001048
Dan Gohman98ca4f22009-08-05 01:29:28 +00001049 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1050 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001051 }
Evan Chenga8e29892007-01-19 07:51:42 +00001052 }
1053
1054 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001055 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001056 &MemOpChains[0], MemOpChains.size());
1057
1058 // Build a sequence of copy-to-reg nodes chained together with token chain
1059 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001060 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +00001061 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001062 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001063 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +00001064 InFlag = Chain.getValue(1);
1065 }
1066
Bill Wendling056292f2008-09-16 21:48:12 +00001067 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1068 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1069 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001070 bool isDirect = false;
1071 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001072 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001073 MachineFunction &MF = DAG.getMachineFunction();
1074 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001075
1076 if (EnableARMLongCalls) {
1077 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1078 && "long-calls with non-static relocation model!");
1079 // Handle a global address or an external symbol. If it's not one of
1080 // those, the target's already in a register, so we don't need to do
1081 // anything extra.
1082 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001083 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001084 // Create a constant pool entry for the callee address
1085 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1086 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1087 ARMPCLabelIndex,
1088 ARMCP::CPValue, 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 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1097 const char *Sym = S->getSymbol();
1098
1099 // Create a constant pool entry for the callee address
1100 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1101 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1102 Sym, ARMPCLabelIndex, 0);
1103 // Get the address of the callee into a register
1104 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1105 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1106 Callee = DAG.getLoad(getPointerTy(), dl,
1107 DAG.getEntryNode(), CPAddr,
1108 PseudoSourceValue::getConstantPool(), 0,
1109 false, false, 0);
1110 }
1111 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001112 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001113 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001114 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001115 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001116 getTargetMachine().getRelocationModel() != Reloc::Static;
1117 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001118 // ARM call to a local ARM function is predicable.
1119 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +00001120 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001121 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001122 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001123 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001124 ARMPCLabelIndex,
1125 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001126 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001127 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001128 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001129 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001130 PseudoSourceValue::getConstantPool(), 0,
1131 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001132 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001133 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001134 getPointerTy(), Callee, PICLabel);
Jim Grosbache7b52522010-04-14 22:28:31 +00001135 } else
Evan Chengc60e76d2007-01-30 20:37:08 +00001136 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001137 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001138 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001139 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001140 getTargetMachine().getRelocationModel() != Reloc::Static;
1141 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001142 // tBX takes a register source operand.
1143 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001144 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001145 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001146 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001147 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001148 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001149 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001150 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001151 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001152 PseudoSourceValue::getConstantPool(), 0,
1153 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001154 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001155 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001156 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001157 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001158 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001159 }
1160
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001161 // FIXME: handle tail calls differently.
1162 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001163 if (Subtarget->isThumb()) {
1164 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001165 CallOpc = ARMISD::CALL_NOLINK;
1166 else
1167 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1168 } else {
1169 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001170 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1171 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001172 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001173 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001174 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001175 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001176 InFlag = Chain.getValue(1);
1177 }
1178
Dan Gohman475871a2008-07-27 21:46:04 +00001179 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001180 Ops.push_back(Chain);
1181 Ops.push_back(Callee);
1182
1183 // Add argument registers to the end of the list so that they are known live
1184 // into the call.
1185 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1186 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1187 RegsToPass[i].second.getValueType()));
1188
Gabor Greifba36cb52008-08-28 21:40:38 +00001189 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001190 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +00001191 // Returns a chain and a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00001192 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +00001193 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001194 InFlag = Chain.getValue(1);
1195
Chris Lattnere563bbc2008-10-11 22:08:30 +00001196 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1197 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001198 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001199 InFlag = Chain.getValue(1);
1200
Bob Wilson1f595bb2009-04-17 19:07:39 +00001201 // Handle result values, copying them out of physregs into vregs that we
1202 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001203 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1204 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001205}
1206
Dan Gohman98ca4f22009-08-05 01:29:28 +00001207SDValue
1208ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001209 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001210 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmand858e902010-04-17 15:26:15 +00001211 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001212
Bob Wilsondee46d72009-04-17 20:35:10 +00001213 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001214 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001215
Bob Wilsondee46d72009-04-17 20:35:10 +00001216 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001217 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1218 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001219
Dan Gohman98ca4f22009-08-05 01:29:28 +00001220 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001221 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1222 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001223
1224 // If this is the first return lowered for this function, add
1225 // the regs to the liveout set for the function.
1226 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1227 for (unsigned i = 0; i != RVLocs.size(); ++i)
1228 if (RVLocs[i].isRegLoc())
1229 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001230 }
1231
Bob Wilson1f595bb2009-04-17 19:07:39 +00001232 SDValue Flag;
1233
1234 // Copy the result values into the output registers.
1235 for (unsigned i = 0, realRVLocIdx = 0;
1236 i != RVLocs.size();
1237 ++i, ++realRVLocIdx) {
1238 CCValAssign &VA = RVLocs[i];
1239 assert(VA.isRegLoc() && "Can only return in registers!");
1240
Dan Gohman98ca4f22009-08-05 01:29:28 +00001241 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001242
1243 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001244 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001245 case CCValAssign::Full: break;
1246 case CCValAssign::BCvt:
1247 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1248 break;
1249 }
1250
Bob Wilson1f595bb2009-04-17 19:07:39 +00001251 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001252 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001253 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001254 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1255 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001256 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001257 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001258
1259 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1260 Flag = Chain.getValue(1);
1261 VA = RVLocs[++i]; // skip ahead to next loc
1262 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1263 HalfGPRs.getValue(1), Flag);
1264 Flag = Chain.getValue(1);
1265 VA = RVLocs[++i]; // skip ahead to next loc
1266
1267 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001268 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1269 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001270 }
1271 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1272 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001273 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001274 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001275 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001276 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001277 VA = RVLocs[++i]; // skip ahead to next loc
1278 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1279 Flag);
1280 } else
1281 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1282
Bob Wilsondee46d72009-04-17 20:35:10 +00001283 // Guarantee that all emitted copies are
1284 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001285 Flag = Chain.getValue(1);
1286 }
1287
1288 SDValue result;
1289 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001290 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001291 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001292 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001293
1294 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001295}
1296
Bob Wilsonb62d2572009-11-03 00:02:05 +00001297// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1298// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1299// one of the above mentioned nodes. It has to be wrapped because otherwise
1300// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1301// be used to form addressing mode. These wrapped nodes will be selected
1302// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001303static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001304 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001305 // FIXME there is no actual debug info here
1306 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001307 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001308 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001309 if (CP->isMachineConstantPoolEntry())
1310 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1311 CP->getAlignment());
1312 else
1313 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1314 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001315 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001316}
1317
Dan Gohmand858e902010-04-17 15:26:15 +00001318SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1319 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001320 MachineFunction &MF = DAG.getMachineFunction();
1321 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1322 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001323 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001324 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001325 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001326 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1327 SDValue CPAddr;
1328 if (RelocM == Reloc::Static) {
1329 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1330 } else {
1331 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001332 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001333 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1334 ARMCP::CPBlockAddress,
1335 PCAdj);
1336 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1337 }
1338 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1339 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001340 PseudoSourceValue::getConstantPool(), 0,
1341 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001342 if (RelocM == Reloc::Static)
1343 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001344 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001345 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001346}
1347
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001348// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001349SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001350ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001351 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001352 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001353 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001354 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001355 MachineFunction &MF = DAG.getMachineFunction();
1356 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1357 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001358 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001359 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001360 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001361 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001362 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001363 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
David Greene1b58cab2010-02-15 16:55:24 +00001364 PseudoSourceValue::getConstantPool(), 0,
1365 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001366 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001367
Evan Chenge7e0d622009-11-06 22:24:13 +00001368 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001369 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001370
1371 // call __tls_get_addr.
1372 ArgListTy Args;
1373 ArgListEntry Entry;
1374 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001375 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001376 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001377 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001378 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001379 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1380 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001381 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001382 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001383 return CallResult.first;
1384}
1385
1386// Lower ISD::GlobalTLSAddress using the "initial exec" or
1387// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001388SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001389ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001390 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001391 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001392 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001393 SDValue Offset;
1394 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001395 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001396 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001397 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001398
Chris Lattner4fb63d02009-07-15 04:12:33 +00001399 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001400 MachineFunction &MF = DAG.getMachineFunction();
1401 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1402 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1403 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001404 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1405 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001406 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001407 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001408 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001409 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001410 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001411 PseudoSourceValue::getConstantPool(), 0,
1412 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001413 Chain = Offset.getValue(1);
1414
Evan Chenge7e0d622009-11-06 22:24:13 +00001415 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001416 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001417
Evan Cheng9eda6892009-10-31 03:39:36 +00001418 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001419 PseudoSourceValue::getConstantPool(), 0,
1420 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001421 } else {
1422 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001423 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001424 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001425 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001426 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001427 PseudoSourceValue::getConstantPool(), 0,
1428 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001429 }
1430
1431 // The address of the thread local variable is the add of the thread
1432 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001433 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001434}
1435
Dan Gohman475871a2008-07-27 21:46:04 +00001436SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001437ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001438 // TODO: implement the "local dynamic" model
1439 assert(Subtarget->isTargetELF() &&
1440 "TLS not implemented for non-ELF targets");
1441 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1442 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1443 // otherwise use the "Local Exec" TLS Model
1444 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1445 return LowerToTLSGeneralDynamicModel(GA, DAG);
1446 else
1447 return LowerToTLSExecModels(GA, DAG);
1448}
1449
Dan Gohman475871a2008-07-27 21:46:04 +00001450SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001451 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001452 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001453 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001454 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001455 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1456 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001457 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001458 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001459 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001460 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001461 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001462 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001463 CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001464 PseudoSourceValue::getConstantPool(), 0,
1465 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001466 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001467 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001468 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001469 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001470 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001471 PseudoSourceValue::getGOT(), 0,
1472 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001473 return Result;
1474 } else {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001475 // If we have T2 ops, we can materialize the address directly via movt/movw
1476 // pair. This is always cheaper.
1477 if (Subtarget->useMovt()) {
1478 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
1479 DAG.getTargetGlobalAddress(GV, PtrVT));
1480 } else {
1481 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1482 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1483 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001484 PseudoSourceValue::getConstantPool(), 0,
1485 false, false, 0);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001486 }
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001487 }
1488}
1489
Dan Gohman475871a2008-07-27 21:46:04 +00001490SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001491 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001492 MachineFunction &MF = DAG.getMachineFunction();
1493 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1494 unsigned ARMPCLabelIndex = 0;
Owen Andersone50ed302009-08-10 22:56:29 +00001495 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001496 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001497 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001498 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001499 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001500 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001501 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001502 else {
Evan Chenge7e0d622009-11-06 22:24:13 +00001503 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001504 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1505 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001506 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001507 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001508 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001509 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001510
Evan Cheng9eda6892009-10-31 03:39:36 +00001511 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001512 PseudoSourceValue::getConstantPool(), 0,
1513 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001514 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001515
1516 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001517 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001518 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001519 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001520
Evan Cheng63476a82009-09-03 07:04:02 +00001521 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Evan Cheng9eda6892009-10-31 03:39:36 +00001522 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001523 PseudoSourceValue::getGOT(), 0,
1524 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001525
1526 return Result;
1527}
1528
Dan Gohman475871a2008-07-27 21:46:04 +00001529SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001530 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001531 assert(Subtarget->isTargetELF() &&
1532 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00001533 MachineFunction &MF = DAG.getMachineFunction();
1534 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1535 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Andersone50ed302009-08-10 22:56:29 +00001536 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001537 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001538 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001539 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1540 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001541 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001542 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001543 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001544 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001545 PseudoSourceValue::getConstantPool(), 0,
1546 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001547 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001548 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001549}
1550
Jim Grosbach0e0da732009-05-12 23:59:14 +00001551SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00001552ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
1553 DebugLoc dl = Op.getDebugLoc();
1554 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
1555 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
1556}
1557
1558SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00001559ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001560 const ARMSubtarget *Subtarget)
1561 const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001562 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001563 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001564 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001565 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001566 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001567 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001568 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1569 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001570 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001571 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00001572 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1573 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001574 EVT PtrVT = getPointerTy();
1575 DebugLoc dl = Op.getDebugLoc();
1576 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1577 SDValue CPAddr;
1578 unsigned PCAdj = (RelocM != Reloc::PIC_)
1579 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001580 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001581 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1582 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001583 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001584 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001585 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00001586 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001587 PseudoSourceValue::getConstantPool(), 0,
1588 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001589 SDValue Chain = Result.getValue(1);
1590
1591 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001592 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001593 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1594 }
1595 return Result;
1596 }
Jim Grosbachf9570122009-05-14 00:46:35 +00001597 case Intrinsic::eh_sjlj_setjmp:
Jim Grosbacha87ded22010-02-08 23:22:00 +00001598 SDValue Val = Subtarget->isThumb() ?
1599 DAG.getCopyFromReg(DAG.getEntryNode(), dl, ARM::SP, MVT::i32) :
1600 DAG.getConstant(0, MVT::i32);
1601 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1),
1602 Val);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001603 }
1604}
1605
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001606static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
1607 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00001608 DebugLoc dl = Op.getDebugLoc();
1609 SDValue Op5 = Op.getOperand(5);
1610 SDValue Res;
1611 unsigned isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue();
1612 if (isDeviceBarrier) {
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001613 if (Subtarget->hasV7Ops())
1614 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0));
1615 else
1616 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0),
1617 DAG.getConstant(0, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00001618 } else {
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001619 if (Subtarget->hasV7Ops())
1620 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
1621 else
1622 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
1623 DAG.getConstant(0, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00001624 }
1625 return Res;
1626}
1627
Dan Gohman1e93df62010-04-17 14:41:14 +00001628static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
1629 MachineFunction &MF = DAG.getMachineFunction();
1630 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
1631
Evan Chenga8e29892007-01-19 07:51:42 +00001632 // vastart just stores the address of the VarArgsFrameIndex slot into the
1633 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001634 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001635 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00001636 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001637 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
David Greene1b58cab2010-02-15 16:55:24 +00001638 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
1639 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001640}
1641
Dan Gohman475871a2008-07-27 21:46:04 +00001642SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001643ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1644 SelectionDAG &DAG) const {
Evan Cheng86198642009-08-07 00:34:42 +00001645 SDNode *Node = Op.getNode();
1646 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001647 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001648 SDValue Chain = Op.getOperand(0);
1649 SDValue Size = Op.getOperand(1);
1650 SDValue Align = Op.getOperand(2);
1651
1652 // Chain the dynamic stack allocation so that it doesn't modify the stack
1653 // pointer when other instructions are using the stack.
1654 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1655
1656 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1657 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1658 if (AlignVal > StackAlign)
1659 // Do this now since selection pass cannot introduce new target
1660 // independent node.
1661 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1662
1663 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1664 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1665 // do even more horrible hack later.
1666 MachineFunction &MF = DAG.getMachineFunction();
1667 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1668 if (AFI->isThumb1OnlyFunction()) {
1669 bool Negate = true;
1670 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1671 if (C) {
1672 uint32_t Val = C->getZExtValue();
1673 if (Val <= 508 && ((Val & 3) == 0))
1674 Negate = false;
1675 }
1676 if (Negate)
1677 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1678 }
1679
Owen Anderson825b72b2009-08-11 20:47:22 +00001680 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001681 SDValue Ops1[] = { Chain, Size, Align };
1682 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1683 Chain = Res.getValue(1);
1684 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1685 DAG.getIntPtrConstant(0, true), SDValue());
1686 SDValue Ops2[] = { Res, Chain };
1687 return DAG.getMergeValues(Ops2, 2, dl);
1688}
1689
1690SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001691ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1692 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001693 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001694 MachineFunction &MF = DAG.getMachineFunction();
1695 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1696
1697 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001698 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001699 RC = ARM::tGPRRegisterClass;
1700 else
1701 RC = ARM::GPRRegisterClass;
1702
1703 // Transform the arguments stored in physical registers into virtual ones.
Evan Cheng2457f2c2010-05-22 01:47:14 +00001704 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001705 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001706
1707 SDValue ArgValue2;
1708 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001709 MachineFrameInfo *MFI = MF.getFrameInfo();
Bob Wilson6a234f02010-04-13 22:03:22 +00001710 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true, false);
Bob Wilson5bafff32009-06-22 23:27:02 +00001711
1712 // Create load node to retrieve arguments from the stack.
1713 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001714 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00001715 PseudoSourceValue::getFixedStack(FI), 0,
1716 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001717 } else {
1718 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001719 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001720 }
1721
Jim Grosbache5165492009-11-09 00:11:35 +00001722 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001723}
1724
1725SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001726ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001727 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001728 const SmallVectorImpl<ISD::InputArg>
1729 &Ins,
1730 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001731 SmallVectorImpl<SDValue> &InVals)
1732 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001733
Bob Wilson1f595bb2009-04-17 19:07:39 +00001734 MachineFunction &MF = DAG.getMachineFunction();
1735 MachineFrameInfo *MFI = MF.getFrameInfo();
1736
Bob Wilson1f595bb2009-04-17 19:07:39 +00001737 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1738
1739 // Assign locations to all of the incoming arguments.
1740 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001741 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1742 *DAG.getContext());
1743 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001744 CCAssignFnForNode(CallConv, /* Return*/ false,
1745 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001746
1747 SmallVector<SDValue, 16> ArgValues;
1748
1749 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1750 CCValAssign &VA = ArgLocs[i];
1751
Bob Wilsondee46d72009-04-17 20:35:10 +00001752 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001753 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001754 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00001755
Bob Wilson5bafff32009-06-22 23:27:02 +00001756 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001757 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001758 // f64 and vector types are split up into multiple registers or
1759 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00001760 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001761 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001762 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00001763 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00001764 SDValue ArgValue2;
1765 if (VA.isMemLoc()) {
1766 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(),
1767 true, false);
1768 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1769 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
1770 PseudoSourceValue::getFixedStack(FI), 0,
1771 false, false, 0);
1772 } else {
1773 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
1774 Chain, DAG, dl);
1775 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001776 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1777 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001778 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00001779 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001780 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1781 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001782 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001783
Bob Wilson5bafff32009-06-22 23:27:02 +00001784 } else {
1785 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001786
Owen Anderson825b72b2009-08-11 20:47:22 +00001787 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00001788 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001789 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00001790 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001791 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001792 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001793 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001794 RC = (AFI->isThumb1OnlyFunction() ?
1795 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00001796 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001797 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00001798
1799 // Transform the arguments in physical registers into virtual ones.
1800 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001801 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001802 }
1803
1804 // If this is an 8 or 16-bit value, it is really passed promoted
1805 // to 32 bits. Insert an assert[sz]ext to capture this, then
1806 // truncate to the right size.
1807 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001808 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001809 case CCValAssign::Full: break;
1810 case CCValAssign::BCvt:
1811 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1812 break;
1813 case CCValAssign::SExt:
1814 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1815 DAG.getValueType(VA.getValVT()));
1816 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1817 break;
1818 case CCValAssign::ZExt:
1819 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1820 DAG.getValueType(VA.getValVT()));
1821 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1822 break;
1823 }
1824
Dan Gohman98ca4f22009-08-05 01:29:28 +00001825 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001826
1827 } else { // VA.isRegLoc()
1828
1829 // sanity check
1830 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00001831 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001832
1833 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001834 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
1835 true, false);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001836
Bob Wilsondee46d72009-04-17 20:35:10 +00001837 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001838 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001839 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00001840 PseudoSourceValue::getFixedStack(FI), 0,
1841 false, false, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001842 }
1843 }
1844
1845 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001846 if (isVarArg) {
1847 static const unsigned GPRArgRegs[] = {
1848 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1849 };
1850
Bob Wilsondee46d72009-04-17 20:35:10 +00001851 unsigned NumGPRs = CCInfo.getFirstUnallocated
1852 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001853
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001854 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1855 unsigned VARegSize = (4 - NumGPRs) * 4;
1856 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00001857 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001858 if (VARegSaveSize) {
1859 // If this function is vararg, store any remaining integer argument regs
1860 // to their spots on the stack so that they may be loaded by deferencing
1861 // the result of va_next.
1862 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00001863 AFI->setVarArgsFrameIndex(
1864 MFI->CreateFixedObject(VARegSaveSize,
1865 ArgOffset + VARegSaveSize - VARegSize,
1866 true, false));
1867 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
1868 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001869
Dan Gohman475871a2008-07-27 21:46:04 +00001870 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001871 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001872 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001873 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001874 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001875 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001876 RC = ARM::GPRRegisterClass;
1877
Bob Wilson998e1252009-04-20 18:36:57 +00001878 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001879 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00001880 SDValue Store =
1881 DAG.getStore(Val.getValue(1), dl, Val, FIN,
1882 PseudoSourceValue::getFixedStack(AFI->getVarArgsFrameIndex()), 0,
1883 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001884 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001885 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001886 DAG.getConstant(4, getPointerTy()));
1887 }
1888 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001889 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001890 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001891 } else
1892 // This will point to the next argument passed via stack.
Dan Gohman1e93df62010-04-17 14:41:14 +00001893 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset,
1894 true, false));
Evan Chenga8e29892007-01-19 07:51:42 +00001895 }
1896
Dan Gohman98ca4f22009-08-05 01:29:28 +00001897 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00001898}
1899
1900/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001901static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001902 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001903 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001904 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001905 // Maybe this has already been legalized into the constant pool?
1906 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001907 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001908 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00001909 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001910 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001911 }
1912 }
1913 return false;
1914}
1915
Evan Chenga8e29892007-01-19 07:51:42 +00001916/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1917/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00001918SDValue
1919ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Dan Gohmand858e902010-04-17 15:26:15 +00001920 SDValue &ARMCC, SelectionDAG &DAG,
1921 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00001922 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001923 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00001924 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001925 // Constant does not fit, try adjusting it by one?
1926 switch (CC) {
1927 default: break;
1928 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001929 case ISD::SETGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00001930 if (isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001931 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001932 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001933 }
1934 break;
1935 case ISD::SETULT:
1936 case ISD::SETUGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00001937 if (C > 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001938 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001939 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001940 }
1941 break;
1942 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001943 case ISD::SETGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00001944 if (isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001945 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001946 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001947 }
1948 break;
1949 case ISD::SETULE:
1950 case ISD::SETUGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00001951 if (C < 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001952 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001953 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001954 }
1955 break;
1956 }
1957 }
1958 }
1959
1960 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001961 ARMISD::NodeType CompareType;
1962 switch (CondCode) {
1963 default:
1964 CompareType = ARMISD::CMP;
1965 break;
1966 case ARMCC::EQ:
1967 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00001968 // Uses only Z Flag
1969 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001970 break;
1971 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001972 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1973 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001974}
1975
1976/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001977static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001978 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001979 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001980 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00001981 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001982 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001983 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1984 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001985}
1986
Dan Gohmand858e902010-04-17 15:26:15 +00001987SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001988 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001989 SDValue LHS = Op.getOperand(0);
1990 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001991 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001992 SDValue TrueVal = Op.getOperand(2);
1993 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001994 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001995
Owen Anderson825b72b2009-08-11 20:47:22 +00001996 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001997 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001998 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00001999 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Dale Johannesende064702009-02-06 21:50:26 +00002000 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002001 }
2002
2003 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002004 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002005
Owen Anderson825b72b2009-08-11 20:47:22 +00002006 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
2007 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002008 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
2009 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00002010 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002011 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002012 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002013 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00002014 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002015 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00002016 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002017 }
2018 return Result;
2019}
2020
Dan Gohmand858e902010-04-17 15:26:15 +00002021SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002022 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002023 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002024 SDValue LHS = Op.getOperand(2);
2025 SDValue RHS = Op.getOperand(3);
2026 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002027 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002028
Owen Anderson825b72b2009-08-11 20:47:22 +00002029 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00002030 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002031 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00002032 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002033 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00002034 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002035 }
2036
Owen Anderson825b72b2009-08-11 20:47:22 +00002037 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00002038 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002039 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002040
Dale Johannesende064702009-02-06 21:50:26 +00002041 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002042 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
2043 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2044 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002045 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002046 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002047 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002048 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00002049 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002050 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002051 }
2052 return Res;
2053}
2054
Dan Gohmand858e902010-04-17 15:26:15 +00002055SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002056 SDValue Chain = Op.getOperand(0);
2057 SDValue Table = Op.getOperand(1);
2058 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002059 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002060
Owen Andersone50ed302009-08-10 22:56:29 +00002061 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002062 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2063 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002064 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002065 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002066 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002067 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2068 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002069 if (Subtarget->isThumb2()) {
2070 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2071 // which does another jump to the destination. This also makes it easier
2072 // to translate it to TBB / TBH later.
2073 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002074 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002075 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002076 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002077 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002078 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002079 PseudoSourceValue::getJumpTable(), 0,
2080 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002081 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002082 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002083 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002084 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002085 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002086 PseudoSourceValue::getJumpTable(), 0, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002087 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002088 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002089 }
Evan Chenga8e29892007-01-19 07:51:42 +00002090}
2091
Bob Wilson76a312b2010-03-19 22:51:32 +00002092static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2093 DebugLoc dl = Op.getDebugLoc();
2094 unsigned Opc;
2095
2096 switch (Op.getOpcode()) {
2097 default:
2098 assert(0 && "Invalid opcode!");
2099 case ISD::FP_TO_SINT:
2100 Opc = ARMISD::FTOSI;
2101 break;
2102 case ISD::FP_TO_UINT:
2103 Opc = ARMISD::FTOUI;
2104 break;
2105 }
2106 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
2107 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
2108}
2109
2110static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2111 EVT VT = Op.getValueType();
2112 DebugLoc dl = Op.getDebugLoc();
2113 unsigned Opc;
2114
2115 switch (Op.getOpcode()) {
2116 default:
2117 assert(0 && "Invalid opcode!");
2118 case ISD::SINT_TO_FP:
2119 Opc = ARMISD::SITOF;
2120 break;
2121 case ISD::UINT_TO_FP:
2122 Opc = ARMISD::UITOF;
2123 break;
2124 }
2125
2126 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
2127 return DAG.getNode(Opc, dl, VT, Op);
2128}
2129
Dan Gohman475871a2008-07-27 21:46:04 +00002130static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002131 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002132 SDValue Tmp0 = Op.getOperand(0);
2133 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002134 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002135 EVT VT = Op.getValueType();
2136 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00002137 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
2138 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002139 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
2140 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002141 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002142}
2143
Evan Cheng2457f2c2010-05-22 01:47:14 +00002144SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
2145 MachineFunction &MF = DAG.getMachineFunction();
2146 MachineFrameInfo *MFI = MF.getFrameInfo();
2147 MFI->setReturnAddressIsTaken(true);
2148
2149 EVT VT = Op.getValueType();
2150 DebugLoc dl = Op.getDebugLoc();
2151 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2152 if (Depth) {
2153 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
2154 SDValue Offset = DAG.getConstant(4, MVT::i32);
2155 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
2156 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
2157 NULL, 0, false, false, 0);
2158 }
2159
2160 // Return LR, which contains the return address. Mark it an implicit live-in.
Evan Chengc7cf10c2010-05-24 18:00:18 +00002161 unsigned Reg = MF.addLiveIn(ARM::LR, ARM::GPRRegisterClass);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002162 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
2163}
2164
Dan Gohmand858e902010-04-17 15:26:15 +00002165SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002166 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2167 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002168
Owen Andersone50ed302009-08-10 22:56:29 +00002169 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002170 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2171 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002172 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002173 ? ARM::R7 : ARM::R11;
2174 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2175 while (Depth--)
David Greene1b58cab2010-02-15 16:55:24 +00002176 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
2177 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002178 return FrameAddr;
2179}
2180
Bob Wilson9f3f0612010-04-17 05:30:19 +00002181/// ExpandBIT_CONVERT - If the target supports VFP, this function is called to
2182/// expand a bit convert where either the source or destination type is i64 to
2183/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2184/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2185/// vectors), since the legalizer won't know what to do with that.
Duncan Sands1607f052008-12-01 11:39:25 +00002186static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002187 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2188 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002189 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002190
Bob Wilson9f3f0612010-04-17 05:30:19 +00002191 // This function is only supposed to be called for i64 types, either as the
2192 // source or destination of the bit convert.
2193 EVT SrcVT = Op.getValueType();
2194 EVT DstVT = N->getValueType(0);
2195 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
2196 "ExpandBIT_CONVERT called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002197
Bob Wilson9f3f0612010-04-17 05:30:19 +00002198 // Turn i64->f64 into VMOVDRR.
2199 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002200 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2201 DAG.getConstant(0, MVT::i32));
2202 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2203 DAG.getConstant(1, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00002204 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002205 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002206
Jim Grosbache5165492009-11-09 00:11:35 +00002207 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002208 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2209 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2210 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2211 // Merge the pieces into a single i64 value.
2212 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2213 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002214
Bob Wilson9f3f0612010-04-17 05:30:19 +00002215 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002216}
2217
Bob Wilson5bafff32009-06-22 23:27:02 +00002218/// getZeroVector - Returns a vector of specified type with all zero elements.
2219///
Owen Andersone50ed302009-08-10 22:56:29 +00002220static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002221 assert(VT.isVector() && "Expected a vector type");
2222
2223 // Zero vectors are used to represent vector negation and in those cases
2224 // will be implemented with the NEON VNEG instruction. However, VNEG does
2225 // not support i64 elements, so sometimes the zero vectors will need to be
2226 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002227 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilson5bafff32009-06-22 23:27:02 +00002228 // to their dest type. This ensures they get CSE'd.
2229 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002230 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2231 SmallVector<SDValue, 8> Ops;
2232 MVT TVT;
2233
2234 if (VT.getSizeInBits() == 64) {
2235 Ops.assign(8, Cst); TVT = MVT::v8i8;
2236 } else {
2237 Ops.assign(16, Cst); TVT = MVT::v16i8;
2238 }
2239 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002240
2241 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2242}
2243
2244/// getOnesVector - Returns a vector of specified type with all bits set.
2245///
Owen Andersone50ed302009-08-10 22:56:29 +00002246static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002247 assert(VT.isVector() && "Expected a vector type");
2248
Bob Wilson929ffa22009-10-30 20:13:25 +00002249 // Always build ones vectors as <16 x i8> or <8 x i8> bitcasted to their
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002250 // dest type. This ensures they get CSE'd.
Bob Wilson5bafff32009-06-22 23:27:02 +00002251 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002252 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2253 SmallVector<SDValue, 8> Ops;
2254 MVT TVT;
2255
2256 if (VT.getSizeInBits() == 64) {
2257 Ops.assign(8, Cst); TVT = MVT::v8i8;
2258 } else {
2259 Ops.assign(16, Cst); TVT = MVT::v16i8;
2260 }
2261 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002262
2263 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2264}
2265
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002266/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2267/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002268SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2269 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002270 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2271 EVT VT = Op.getValueType();
2272 unsigned VTBits = VT.getSizeInBits();
2273 DebugLoc dl = Op.getDebugLoc();
2274 SDValue ShOpLo = Op.getOperand(0);
2275 SDValue ShOpHi = Op.getOperand(1);
2276 SDValue ShAmt = Op.getOperand(2);
2277 SDValue ARMCC;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002278 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002279
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002280 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2281
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002282 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2283 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2284 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2285 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2286 DAG.getConstant(VTBits, MVT::i32));
2287 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2288 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002289 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002290
2291 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2292 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002293 ARMCC, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002294 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002295 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC,
2296 CCR, Cmp);
2297
2298 SDValue Ops[2] = { Lo, Hi };
2299 return DAG.getMergeValues(Ops, 2, dl);
2300}
2301
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002302/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2303/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002304SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2305 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002306 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2307 EVT VT = Op.getValueType();
2308 unsigned VTBits = VT.getSizeInBits();
2309 DebugLoc dl = Op.getDebugLoc();
2310 SDValue ShOpLo = Op.getOperand(0);
2311 SDValue ShOpHi = Op.getOperand(1);
2312 SDValue ShAmt = Op.getOperand(2);
2313 SDValue ARMCC;
2314
2315 assert(Op.getOpcode() == ISD::SHL_PARTS);
2316 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2317 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2318 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2319 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2320 DAG.getConstant(VTBits, MVT::i32));
2321 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2322 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2323
2324 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2325 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2326 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002327 ARMCC, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002328 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
2329 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMCC,
2330 CCR, Cmp);
2331
2332 SDValue Ops[2] = { Lo, Hi };
2333 return DAG.getMergeValues(Ops, 2, dl);
2334}
2335
Jim Grosbach3482c802010-01-18 19:58:49 +00002336static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
2337 const ARMSubtarget *ST) {
2338 EVT VT = N->getValueType(0);
2339 DebugLoc dl = N->getDebugLoc();
2340
2341 if (!ST->hasV6T2Ops())
2342 return SDValue();
2343
2344 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
2345 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
2346}
2347
Bob Wilson5bafff32009-06-22 23:27:02 +00002348static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2349 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002350 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002351 DebugLoc dl = N->getDebugLoc();
2352
2353 // Lower vector shifts on NEON to use VSHL.
2354 if (VT.isVector()) {
2355 assert(ST->hasNEON() && "unexpected vector shift");
2356
2357 // Left shifts translate directly to the vshiftu intrinsic.
2358 if (N->getOpcode() == ISD::SHL)
2359 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002360 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002361 N->getOperand(0), N->getOperand(1));
2362
2363 assert((N->getOpcode() == ISD::SRA ||
2364 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2365
2366 // NEON uses the same intrinsics for both left and right shifts. For
2367 // right shifts, the shift amounts are negative, so negate the vector of
2368 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002369 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002370 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2371 getZeroVector(ShiftVT, DAG, dl),
2372 N->getOperand(1));
2373 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2374 Intrinsic::arm_neon_vshifts :
2375 Intrinsic::arm_neon_vshiftu);
2376 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002377 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002378 N->getOperand(0), NegatedCount);
2379 }
2380
Eli Friedmance392eb2009-08-22 03:13:10 +00002381 // We can get here for a node like i32 = ISD::SHL i32, i64
2382 if (VT != MVT::i64)
2383 return SDValue();
2384
2385 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002386 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002387
Chris Lattner27a6c732007-11-24 07:07:01 +00002388 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2389 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002390 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002391 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002392
Chris Lattner27a6c732007-11-24 07:07:01 +00002393 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002394 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002395
Chris Lattner27a6c732007-11-24 07:07:01 +00002396 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002397 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00002398 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00002399 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00002400 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002401
Chris Lattner27a6c732007-11-24 07:07:01 +00002402 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2403 // captures the result into a carry flag.
2404 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002405 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002406
Chris Lattner27a6c732007-11-24 07:07:01 +00002407 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002408 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002409
Chris Lattner27a6c732007-11-24 07:07:01 +00002410 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002411 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002412}
2413
Bob Wilson5bafff32009-06-22 23:27:02 +00002414static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2415 SDValue TmpOp0, TmpOp1;
2416 bool Invert = false;
2417 bool Swap = false;
2418 unsigned Opc = 0;
2419
2420 SDValue Op0 = Op.getOperand(0);
2421 SDValue Op1 = Op.getOperand(1);
2422 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002423 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002424 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2425 DebugLoc dl = Op.getDebugLoc();
2426
2427 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2428 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002429 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002430 case ISD::SETUNE:
2431 case ISD::SETNE: Invert = true; // Fallthrough
2432 case ISD::SETOEQ:
2433 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2434 case ISD::SETOLT:
2435 case ISD::SETLT: Swap = true; // Fallthrough
2436 case ISD::SETOGT:
2437 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2438 case ISD::SETOLE:
2439 case ISD::SETLE: Swap = true; // Fallthrough
2440 case ISD::SETOGE:
2441 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2442 case ISD::SETUGE: Swap = true; // Fallthrough
2443 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2444 case ISD::SETUGT: Swap = true; // Fallthrough
2445 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2446 case ISD::SETUEQ: Invert = true; // Fallthrough
2447 case ISD::SETONE:
2448 // Expand this to (OLT | OGT).
2449 TmpOp0 = Op0;
2450 TmpOp1 = Op1;
2451 Opc = ISD::OR;
2452 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2453 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2454 break;
2455 case ISD::SETUO: Invert = true; // Fallthrough
2456 case ISD::SETO:
2457 // Expand this to (OLT | OGE).
2458 TmpOp0 = Op0;
2459 TmpOp1 = Op1;
2460 Opc = ISD::OR;
2461 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2462 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2463 break;
2464 }
2465 } else {
2466 // Integer comparisons.
2467 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002468 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002469 case ISD::SETNE: Invert = true;
2470 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2471 case ISD::SETLT: Swap = true;
2472 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2473 case ISD::SETLE: Swap = true;
2474 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2475 case ISD::SETULT: Swap = true;
2476 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2477 case ISD::SETULE: Swap = true;
2478 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2479 }
2480
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002481 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002482 if (Opc == ARMISD::VCEQ) {
2483
2484 SDValue AndOp;
2485 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2486 AndOp = Op0;
2487 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2488 AndOp = Op1;
2489
2490 // Ignore bitconvert.
2491 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2492 AndOp = AndOp.getOperand(0);
2493
2494 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2495 Opc = ARMISD::VTST;
2496 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2497 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2498 Invert = !Invert;
2499 }
2500 }
2501 }
2502
2503 if (Swap)
2504 std::swap(Op0, Op1);
2505
2506 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2507
2508 if (Invert)
2509 Result = DAG.getNOT(dl, Result, VT);
2510
2511 return Result;
2512}
2513
2514/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2515/// VMOV instruction, and if so, return the constant being splatted.
2516static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2517 unsigned SplatBitSize, SelectionDAG &DAG) {
2518 switch (SplatBitSize) {
2519 case 8:
2520 // Any 1-byte value is OK.
2521 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson825b72b2009-08-11 20:47:22 +00002522 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilson5bafff32009-06-22 23:27:02 +00002523
2524 case 16:
2525 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2526 if ((SplatBits & ~0xff) == 0 ||
2527 (SplatBits & ~0xff00) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002528 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilson5bafff32009-06-22 23:27:02 +00002529 break;
2530
2531 case 32:
2532 // NEON's 32-bit VMOV supports splat values where:
2533 // * only one byte is nonzero, or
2534 // * the least significant byte is 0xff and the second byte is nonzero, or
2535 // * the least significant 2 bytes are 0xff and the third is nonzero.
2536 if ((SplatBits & ~0xff) == 0 ||
2537 (SplatBits & ~0xff00) == 0 ||
2538 (SplatBits & ~0xff0000) == 0 ||
2539 (SplatBits & ~0xff000000) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002540 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002541
2542 if ((SplatBits & ~0xffff) == 0 &&
2543 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002544 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002545
2546 if ((SplatBits & ~0xffffff) == 0 &&
2547 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002548 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002549
2550 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2551 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2552 // VMOV.I32. A (very) minor optimization would be to replicate the value
2553 // and fall through here to test for a valid 64-bit splat. But, then the
2554 // caller would also need to check and handle the change in size.
2555 break;
2556
2557 case 64: {
2558 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2559 uint64_t BitMask = 0xff;
2560 uint64_t Val = 0;
2561 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2562 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2563 Val |= BitMask;
2564 else if ((SplatBits & BitMask) != 0)
2565 return SDValue();
2566 BitMask <<= 8;
2567 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002568 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilson5bafff32009-06-22 23:27:02 +00002569 }
2570
2571 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002572 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilson5bafff32009-06-22 23:27:02 +00002573 break;
2574 }
2575
2576 return SDValue();
2577}
2578
2579/// getVMOVImm - If this is a build_vector of constants which can be
2580/// formed by using a VMOV instruction of the specified element size,
2581/// return the constant being splatted. The ByteSize field indicates the
2582/// number of bytes of each element [1248].
2583SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2584 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2585 APInt SplatBits, SplatUndef;
2586 unsigned SplatBitSize;
2587 bool HasAnyUndefs;
2588 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2589 HasAnyUndefs, ByteSize * 8))
2590 return SDValue();
2591
2592 if (SplatBitSize > ByteSize * 8)
2593 return SDValue();
2594
2595 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2596 SplatBitSize, DAG);
2597}
2598
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002599static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2600 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002601 unsigned NumElts = VT.getVectorNumElements();
2602 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002603 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002604
2605 // If this is a VEXT shuffle, the immediate value is the index of the first
2606 // element. The other shuffle indices must be the successive elements after
2607 // the first one.
2608 unsigned ExpectedElt = Imm;
2609 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002610 // Increment the expected index. If it wraps around, it may still be
2611 // a VEXT but the source vectors must be swapped.
2612 ExpectedElt += 1;
2613 if (ExpectedElt == NumElts * 2) {
2614 ExpectedElt = 0;
2615 ReverseVEXT = true;
2616 }
2617
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002618 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002619 return false;
2620 }
2621
2622 // Adjust the index value if the source operands will be swapped.
2623 if (ReverseVEXT)
2624 Imm -= NumElts;
2625
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002626 return true;
2627}
2628
Bob Wilson8bb9e482009-07-26 00:39:34 +00002629/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2630/// instruction with the specified blocksize. (The order of the elements
2631/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002632static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2633 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002634 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2635 "Only possible block sizes for VREV are: 16, 32, 64");
2636
Bob Wilson8bb9e482009-07-26 00:39:34 +00002637 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00002638 if (EltSz == 64)
2639 return false;
2640
2641 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002642 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002643
2644 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2645 return false;
2646
2647 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002648 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002649 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2650 return false;
2651 }
2652
2653 return true;
2654}
2655
Bob Wilsonc692cb72009-08-21 20:54:19 +00002656static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2657 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002658 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2659 if (EltSz == 64)
2660 return false;
2661
Bob Wilsonc692cb72009-08-21 20:54:19 +00002662 unsigned NumElts = VT.getVectorNumElements();
2663 WhichResult = (M[0] == 0 ? 0 : 1);
2664 for (unsigned i = 0; i < NumElts; i += 2) {
2665 if ((unsigned) M[i] != i + WhichResult ||
2666 (unsigned) M[i+1] != i + NumElts + WhichResult)
2667 return false;
2668 }
2669 return true;
2670}
2671
Bob Wilson324f4f12009-12-03 06:40:55 +00002672/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
2673/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2674/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
2675static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2676 unsigned &WhichResult) {
2677 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2678 if (EltSz == 64)
2679 return false;
2680
2681 unsigned NumElts = VT.getVectorNumElements();
2682 WhichResult = (M[0] == 0 ? 0 : 1);
2683 for (unsigned i = 0; i < NumElts; i += 2) {
2684 if ((unsigned) M[i] != i + WhichResult ||
2685 (unsigned) M[i+1] != i + WhichResult)
2686 return false;
2687 }
2688 return true;
2689}
2690
Bob Wilsonc692cb72009-08-21 20:54:19 +00002691static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2692 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002693 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2694 if (EltSz == 64)
2695 return false;
2696
Bob Wilsonc692cb72009-08-21 20:54:19 +00002697 unsigned NumElts = VT.getVectorNumElements();
2698 WhichResult = (M[0] == 0 ? 0 : 1);
2699 for (unsigned i = 0; i != NumElts; ++i) {
2700 if ((unsigned) M[i] != 2 * i + WhichResult)
2701 return false;
2702 }
2703
2704 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002705 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002706 return false;
2707
2708 return true;
2709}
2710
Bob Wilson324f4f12009-12-03 06:40:55 +00002711/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
2712/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2713/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
2714static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2715 unsigned &WhichResult) {
2716 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2717 if (EltSz == 64)
2718 return false;
2719
2720 unsigned Half = VT.getVectorNumElements() / 2;
2721 WhichResult = (M[0] == 0 ? 0 : 1);
2722 for (unsigned j = 0; j != 2; ++j) {
2723 unsigned Idx = WhichResult;
2724 for (unsigned i = 0; i != Half; ++i) {
2725 if ((unsigned) M[i + j * Half] != Idx)
2726 return false;
2727 Idx += 2;
2728 }
2729 }
2730
2731 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2732 if (VT.is64BitVector() && EltSz == 32)
2733 return false;
2734
2735 return true;
2736}
2737
Bob Wilsonc692cb72009-08-21 20:54:19 +00002738static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2739 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002740 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2741 if (EltSz == 64)
2742 return false;
2743
Bob Wilsonc692cb72009-08-21 20:54:19 +00002744 unsigned NumElts = VT.getVectorNumElements();
2745 WhichResult = (M[0] == 0 ? 0 : 1);
2746 unsigned Idx = WhichResult * NumElts / 2;
2747 for (unsigned i = 0; i != NumElts; i += 2) {
2748 if ((unsigned) M[i] != Idx ||
2749 (unsigned) M[i+1] != Idx + NumElts)
2750 return false;
2751 Idx += 1;
2752 }
2753
2754 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002755 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002756 return false;
2757
2758 return true;
2759}
2760
Bob Wilson324f4f12009-12-03 06:40:55 +00002761/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
2762/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2763/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
2764static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2765 unsigned &WhichResult) {
2766 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2767 if (EltSz == 64)
2768 return false;
2769
2770 unsigned NumElts = VT.getVectorNumElements();
2771 WhichResult = (M[0] == 0 ? 0 : 1);
2772 unsigned Idx = WhichResult * NumElts / 2;
2773 for (unsigned i = 0; i != NumElts; i += 2) {
2774 if ((unsigned) M[i] != Idx ||
2775 (unsigned) M[i+1] != Idx)
2776 return false;
2777 Idx += 1;
2778 }
2779
2780 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2781 if (VT.is64BitVector() && EltSz == 32)
2782 return false;
2783
2784 return true;
2785}
2786
2787
Owen Andersone50ed302009-08-10 22:56:29 +00002788static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002789 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00002790 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002791 if (ConstVal->isNullValue())
2792 return getZeroVector(VT, DAG, dl);
2793 if (ConstVal->isAllOnesValue())
2794 return getOnesVector(VT, DAG, dl);
2795
Owen Andersone50ed302009-08-10 22:56:29 +00002796 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00002797 if (VT.is64BitVector()) {
2798 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002799 case 8: CanonicalVT = MVT::v8i8; break;
2800 case 16: CanonicalVT = MVT::v4i16; break;
2801 case 32: CanonicalVT = MVT::v2i32; break;
2802 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002803 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002804 }
2805 } else {
2806 assert(VT.is128BitVector() && "unknown splat vector size");
2807 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002808 case 8: CanonicalVT = MVT::v16i8; break;
2809 case 16: CanonicalVT = MVT::v8i16; break;
2810 case 32: CanonicalVT = MVT::v4i32; break;
2811 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002812 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002813 }
2814 }
2815
2816 // Build a canonical splat for this value.
2817 SmallVector<SDValue, 8> Ops;
2818 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2819 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2820 Ops.size());
2821 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2822}
2823
2824// If this is a case we can't handle, return null and let the default
2825// expansion code take care of it.
2826static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00002827 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002828 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002829 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002830
2831 APInt SplatBits, SplatUndef;
2832 unsigned SplatBitSize;
2833 bool HasAnyUndefs;
2834 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00002835 if (SplatBitSize <= 64) {
2836 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2837 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2838 if (Val.getNode())
2839 return BuildSplat(Val, VT, DAG, dl);
2840 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00002841 }
2842
Bob Wilsonbe751cf2010-05-22 00:23:12 +00002843 // Scan through the operands to see if only one value is used.
2844 unsigned NumElts = VT.getVectorNumElements();
2845 bool isOnlyLowElement = true;
2846 bool usesOnlyOneValue = true;
2847 bool isConstant = true;
2848 SDValue Value;
2849 for (unsigned i = 0; i < NumElts; ++i) {
2850 SDValue V = Op.getOperand(i);
2851 if (V.getOpcode() == ISD::UNDEF)
2852 continue;
2853 if (i > 0)
2854 isOnlyLowElement = false;
2855 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
2856 isConstant = false;
2857
2858 if (!Value.getNode())
2859 Value = V;
2860 else if (V != Value)
2861 usesOnlyOneValue = false;
2862 }
2863
2864 if (!Value.getNode())
2865 return DAG.getUNDEF(VT);
2866
2867 if (isOnlyLowElement)
2868 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
2869
2870 // If all elements are constants, fall back to the default expansion, which
2871 // will generate a load from the constant pool.
2872 if (isConstant)
2873 return SDValue();
2874
2875 // Use VDUP for non-constant splats.
Bob Wilson069e4342010-05-23 05:42:31 +00002876 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
2877 if (usesOnlyOneValue && EltSize <= 32)
Bob Wilsonbe751cf2010-05-22 00:23:12 +00002878 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
2879
2880 // Vectors with 32- or 64-bit elements can be built by directly assigning
2881 // the subregisters.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00002882 if (EltSize >= 32) {
2883 // Do the expansion with floating-point types, since that is what the VFP
2884 // registers are defined to use, and since i64 is not legal.
2885 EVT EltVT = EVT::getFloatingPointVT(EltSize);
2886 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
2887 SDValue Val = DAG.getUNDEF(VecVT);
2888 for (unsigned i = 0; i < NumElts; ++i) {
2889 SDValue Elt = Op.getOperand(i);
2890 if (Elt.getOpcode() == ISD::UNDEF)
2891 continue;
2892 Elt = DAG.getNode(ISD::BIT_CONVERT, dl, EltVT, Elt);
2893 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Val, Elt,
2894 DAG.getConstant(i, MVT::i32));
2895 }
2896 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00002897 }
2898
2899 return SDValue();
2900}
2901
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002902/// isShuffleMaskLegal - Targets can use this to indicate that they only
2903/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2904/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2905/// are assumed to be legal.
2906bool
2907ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2908 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002909 if (VT.getVectorNumElements() == 4 &&
2910 (VT.is128BitVector() || VT.is64BitVector())) {
2911 unsigned PFIndexes[4];
2912 for (unsigned i = 0; i != 4; ++i) {
2913 if (M[i] < 0)
2914 PFIndexes[i] = 8;
2915 else
2916 PFIndexes[i] = M[i];
2917 }
2918
2919 // Compute the index in the perfect shuffle table.
2920 unsigned PFTableIndex =
2921 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2922 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2923 unsigned Cost = (PFEntry >> 30);
2924
2925 if (Cost <= 4)
2926 return true;
2927 }
2928
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002929 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00002930 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002931
2932 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2933 isVREVMask(M, VT, 64) ||
2934 isVREVMask(M, VT, 32) ||
2935 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00002936 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2937 isVTRNMask(M, VT, WhichResult) ||
2938 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00002939 isVZIPMask(M, VT, WhichResult) ||
2940 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
2941 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
2942 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002943}
2944
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002945/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2946/// the specified operations to build the shuffle.
2947static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2948 SDValue RHS, SelectionDAG &DAG,
2949 DebugLoc dl) {
2950 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2951 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2952 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2953
2954 enum {
2955 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2956 OP_VREV,
2957 OP_VDUP0,
2958 OP_VDUP1,
2959 OP_VDUP2,
2960 OP_VDUP3,
2961 OP_VEXT1,
2962 OP_VEXT2,
2963 OP_VEXT3,
2964 OP_VUZPL, // VUZP, left result
2965 OP_VUZPR, // VUZP, right result
2966 OP_VZIPL, // VZIP, left result
2967 OP_VZIPR, // VZIP, right result
2968 OP_VTRNL, // VTRN, left result
2969 OP_VTRNR // VTRN, right result
2970 };
2971
2972 if (OpNum == OP_COPY) {
2973 if (LHSID == (1*9+2)*9+3) return LHS;
2974 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2975 return RHS;
2976 }
2977
2978 SDValue OpLHS, OpRHS;
2979 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2980 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2981 EVT VT = OpLHS.getValueType();
2982
2983 switch (OpNum) {
2984 default: llvm_unreachable("Unknown shuffle opcode!");
2985 case OP_VREV:
2986 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2987 case OP_VDUP0:
2988 case OP_VDUP1:
2989 case OP_VDUP2:
2990 case OP_VDUP3:
2991 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002992 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002993 case OP_VEXT1:
2994 case OP_VEXT2:
2995 case OP_VEXT3:
2996 return DAG.getNode(ARMISD::VEXT, dl, VT,
2997 OpLHS, OpRHS,
2998 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2999 case OP_VUZPL:
3000 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003001 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003002 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
3003 case OP_VZIPL:
3004 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003005 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003006 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
3007 case OP_VTRNL:
3008 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003009 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3010 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003011 }
3012}
3013
Bob Wilson5bafff32009-06-22 23:27:02 +00003014static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003015 SDValue V1 = Op.getOperand(0);
3016 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00003017 DebugLoc dl = Op.getDebugLoc();
3018 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003019 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003020 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00003021
Bob Wilson28865062009-08-13 02:13:04 +00003022 // Convert shuffles that are directly supported on NEON to target-specific
3023 // DAG nodes, instead of keeping them as shuffles and matching them again
3024 // during code selection. This is more efficient and avoids the possibility
3025 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00003026 // FIXME: floating-point vectors should be canonicalized to integer vectors
3027 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003028 SVN->getMask(ShuffleMask);
3029
3030 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson0ce37102009-08-14 05:08:32 +00003031 int Lane = SVN->getSplatIndex();
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00003032 // If this is undef splat, generate it via "just" vdup, if possible.
3033 if (Lane == -1) Lane = 0;
3034
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003035 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3036 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00003037 }
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003038 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003039 DAG.getConstant(Lane, MVT::i32));
Bob Wilson0ce37102009-08-14 05:08:32 +00003040 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003041
3042 bool ReverseVEXT;
3043 unsigned Imm;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003044 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003045 if (ReverseVEXT)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003046 std::swap(V1, V2);
3047 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003048 DAG.getConstant(Imm, MVT::i32));
3049 }
3050
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003051 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003052 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003053 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003054 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003055 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003056 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
3057
Bob Wilsonc692cb72009-08-21 20:54:19 +00003058 // Check for Neon shuffles that modify both input vectors in place.
3059 // If both results are used, i.e., if there are two shuffles with the same
3060 // source operands and with masks corresponding to both results of one of
3061 // these operations, DAG memoization will ensure that a single node is
3062 // used for both shuffles.
3063 unsigned WhichResult;
3064 if (isVTRNMask(ShuffleMask, VT, WhichResult))
3065 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3066 V1, V2).getValue(WhichResult);
3067 if (isVUZPMask(ShuffleMask, VT, WhichResult))
3068 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3069 V1, V2).getValue(WhichResult);
3070 if (isVZIPMask(ShuffleMask, VT, WhichResult))
3071 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3072 V1, V2).getValue(WhichResult);
3073
Bob Wilson324f4f12009-12-03 06:40:55 +00003074 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
3075 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3076 V1, V1).getValue(WhichResult);
3077 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3078 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3079 V1, V1).getValue(WhichResult);
3080 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3081 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3082 V1, V1).getValue(WhichResult);
3083
Bob Wilsonc692cb72009-08-21 20:54:19 +00003084 // If the shuffle is not directly supported and it has 4 elements, use
3085 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003086 unsigned NumElts = VT.getVectorNumElements();
3087 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003088 unsigned PFIndexes[4];
3089 for (unsigned i = 0; i != 4; ++i) {
3090 if (ShuffleMask[i] < 0)
3091 PFIndexes[i] = 8;
3092 else
3093 PFIndexes[i] = ShuffleMask[i];
3094 }
3095
3096 // Compute the index in the perfect shuffle table.
3097 unsigned PFTableIndex =
3098 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003099 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3100 unsigned Cost = (PFEntry >> 30);
3101
3102 if (Cost <= 4)
3103 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
3104 }
Bob Wilsond8e17572009-08-12 22:31:50 +00003105
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003106 // Implement shuffles with 32- or 64-bit elements as subreg copies.
3107 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3108 if (EltSize >= 32) {
3109 // Do the expansion with floating-point types, since that is what the VFP
3110 // registers are defined to use, and since i64 is not legal.
3111 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3112 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
3113 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V1);
3114 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V2);
3115 SDValue Val = DAG.getUNDEF(VecVT);
3116 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00003117 if (ShuffleMask[i] < 0)
3118 continue;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003119 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
3120 ShuffleMask[i] < (int)NumElts ? V1 : V2,
3121 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
3122 MVT::i32));
3123 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Val,
Bob Wilson63b88452010-05-20 18:39:53 +00003124 Elt, DAG.getConstant(i, MVT::i32));
3125 }
3126 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
3127 }
3128
Bob Wilson22cac0d2009-08-14 05:16:33 +00003129 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003130}
3131
Bob Wilson5bafff32009-06-22 23:27:02 +00003132static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003133 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003134 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003135 SDValue Vec = Op.getOperand(0);
3136 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00003137 assert(VT == MVT::i32 &&
3138 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
3139 "unexpected type for custom-lowering vector extract");
3140 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00003141}
3142
Bob Wilsona6d65862009-08-03 20:36:38 +00003143static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3144 // The only time a CONCAT_VECTORS operation can have legal types is when
3145 // two 64-bit vectors are concatenated to a 128-bit vector.
3146 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
3147 "unexpected CONCAT_VECTORS");
3148 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00003149 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00003150 SDValue Op0 = Op.getOperand(0);
3151 SDValue Op1 = Op.getOperand(1);
3152 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003153 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3154 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00003155 DAG.getIntPtrConstant(0));
3156 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003157 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3158 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00003159 DAG.getIntPtrConstant(1));
3160 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003161}
3162
Dan Gohmand858e902010-04-17 15:26:15 +00003163SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003164 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003165 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00003166 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00003167 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003168 case ISD::GlobalAddress:
3169 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
3170 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00003171 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00003172 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
3173 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003174 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00003175 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00003176 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00003177 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00003178 case ISD::SINT_TO_FP:
3179 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
3180 case ISD::FP_TO_SINT:
3181 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003182 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003183 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003184 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003185 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00003186 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00003187 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
3188 Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00003189 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003190 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00003191 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00003192 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00003193 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003194 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00003195 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00003196 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003197 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
3198 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3199 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003200 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00003201 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003202 }
Dan Gohman475871a2008-07-27 21:46:04 +00003203 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003204}
3205
Duncan Sands1607f052008-12-01 11:39:25 +00003206/// ReplaceNodeResults - Replace the results of node with an illegal result
3207/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00003208void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
3209 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00003210 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00003211 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00003212 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00003213 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003214 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003215 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003216 case ISD::BIT_CONVERT:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003217 Res = ExpandBIT_CONVERT(N, DAG);
3218 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00003219 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003220 case ISD::SRA:
3221 Res = LowerShift(N, DAG, Subtarget);
3222 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003223 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00003224 if (Res.getNode())
3225 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00003226}
Chris Lattner27a6c732007-11-24 07:07:01 +00003227
Evan Chenga8e29892007-01-19 07:51:42 +00003228//===----------------------------------------------------------------------===//
3229// ARM Scheduler Hooks
3230//===----------------------------------------------------------------------===//
3231
3232MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003233ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
3234 MachineBasicBlock *BB,
3235 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00003236 unsigned dest = MI->getOperand(0).getReg();
3237 unsigned ptr = MI->getOperand(1).getReg();
3238 unsigned oldval = MI->getOperand(2).getReg();
3239 unsigned newval = MI->getOperand(3).getReg();
3240 unsigned scratch = BB->getParent()->getRegInfo()
3241 .createVirtualRegister(ARM::GPRRegisterClass);
3242 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3243 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003244 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00003245
3246 unsigned ldrOpc, strOpc;
3247 switch (Size) {
3248 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003249 case 1:
3250 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
3251 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
3252 break;
3253 case 2:
3254 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3255 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3256 break;
3257 case 4:
3258 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3259 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3260 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00003261 }
3262
3263 MachineFunction *MF = BB->getParent();
3264 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3265 MachineFunction::iterator It = BB;
3266 ++It; // insert the new blocks after the current block
3267
3268 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3269 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3270 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3271 MF->insert(It, loop1MBB);
3272 MF->insert(It, loop2MBB);
3273 MF->insert(It, exitMBB);
3274 exitMBB->transferSuccessors(BB);
3275
3276 // thisMBB:
3277 // ...
3278 // fallthrough --> loop1MBB
3279 BB->addSuccessor(loop1MBB);
3280
3281 // loop1MBB:
3282 // ldrex dest, [ptr]
3283 // cmp dest, oldval
3284 // bne exitMBB
3285 BB = loop1MBB;
3286 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003287 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003288 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003289 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3290 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003291 BB->addSuccessor(loop2MBB);
3292 BB->addSuccessor(exitMBB);
3293
3294 // loop2MBB:
3295 // strex scratch, newval, [ptr]
3296 // cmp scratch, #0
3297 // bne loop1MBB
3298 BB = loop2MBB;
3299 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
3300 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003301 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42: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(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003305 BB->addSuccessor(loop1MBB);
3306 BB->addSuccessor(exitMBB);
3307
3308 // exitMBB:
3309 // ...
3310 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00003311
3312 MF->DeleteMachineInstr(MI); // The instruction is gone now.
3313
Jim Grosbach5278eb82009-12-11 01:42:04 +00003314 return BB;
3315}
3316
3317MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003318ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
3319 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00003320 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
3321 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3322
3323 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003324 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003325 MachineFunction::iterator It = BB;
3326 ++It;
3327
3328 unsigned dest = MI->getOperand(0).getReg();
3329 unsigned ptr = MI->getOperand(1).getReg();
3330 unsigned incr = MI->getOperand(2).getReg();
3331 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00003332
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003333 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003334 unsigned ldrOpc, strOpc;
3335 switch (Size) {
3336 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003337 case 1:
3338 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00003339 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003340 break;
3341 case 2:
3342 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3343 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3344 break;
3345 case 4:
3346 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3347 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3348 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00003349 }
3350
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003351 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3352 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3353 MF->insert(It, loopMBB);
3354 MF->insert(It, exitMBB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003355 exitMBB->transferSuccessors(BB);
3356
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003357 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003358 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3359 unsigned scratch2 = (!BinOpcode) ? incr :
3360 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3361
3362 // thisMBB:
3363 // ...
3364 // fallthrough --> loopMBB
3365 BB->addSuccessor(loopMBB);
3366
3367 // loopMBB:
3368 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003369 // <binop> scratch2, dest, incr
3370 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00003371 // cmp scratch, #0
3372 // bne- loopMBB
3373 // fallthrough --> exitMBB
3374 BB = loopMBB;
3375 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00003376 if (BinOpcode) {
3377 // operand order needs to go the other way for NAND
3378 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
3379 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3380 addReg(incr).addReg(dest)).addReg(0);
3381 else
3382 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3383 addReg(dest).addReg(incr)).addReg(0);
3384 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00003385
3386 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
3387 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003388 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00003389 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003390 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3391 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003392
3393 BB->addSuccessor(loopMBB);
3394 BB->addSuccessor(exitMBB);
3395
3396 // exitMBB:
3397 // ...
3398 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00003399
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003400 MF->DeleteMachineInstr(MI); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00003401
Jim Grosbachc3c23542009-12-14 04:22:04 +00003402 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00003403}
3404
3405MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00003406ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003407 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003408 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00003409 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003410 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00003411 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00003412 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00003413 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00003414 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00003415
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003416 case ARM::ATOMIC_LOAD_ADD_I8:
3417 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3418 case ARM::ATOMIC_LOAD_ADD_I16:
3419 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3420 case ARM::ATOMIC_LOAD_ADD_I32:
3421 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003422
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003423 case ARM::ATOMIC_LOAD_AND_I8:
3424 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3425 case ARM::ATOMIC_LOAD_AND_I16:
3426 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3427 case ARM::ATOMIC_LOAD_AND_I32:
3428 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003429
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003430 case ARM::ATOMIC_LOAD_OR_I8:
3431 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3432 case ARM::ATOMIC_LOAD_OR_I16:
3433 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3434 case ARM::ATOMIC_LOAD_OR_I32:
3435 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003436
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003437 case ARM::ATOMIC_LOAD_XOR_I8:
3438 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3439 case ARM::ATOMIC_LOAD_XOR_I16:
3440 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3441 case ARM::ATOMIC_LOAD_XOR_I32:
3442 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003443
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003444 case ARM::ATOMIC_LOAD_NAND_I8:
3445 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3446 case ARM::ATOMIC_LOAD_NAND_I16:
3447 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3448 case ARM::ATOMIC_LOAD_NAND_I32:
3449 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003450
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003451 case ARM::ATOMIC_LOAD_SUB_I8:
3452 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3453 case ARM::ATOMIC_LOAD_SUB_I16:
3454 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3455 case ARM::ATOMIC_LOAD_SUB_I32:
3456 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003457
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003458 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
3459 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
3460 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00003461
3462 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
3463 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
3464 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003465
Evan Cheng007ea272009-08-12 05:17:19 +00003466 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00003467 // To "insert" a SELECT_CC instruction, we actually have to insert the
3468 // diamond control-flow pattern. The incoming instruction knows the
3469 // destination vreg to set, the condition code register to branch on, the
3470 // true/false values to select between, and a branch opcode to use.
3471 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003472 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00003473 ++It;
3474
3475 // thisMBB:
3476 // ...
3477 // TrueVal = ...
3478 // cmpTY ccX, r1, r2
3479 // bCC copy1MBB
3480 // fallthrough --> copy0MBB
3481 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003482 MachineFunction *F = BB->getParent();
3483 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3484 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00003485 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00003486 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003487 F->insert(It, copy0MBB);
3488 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00003489 // Update machine-CFG edges by first adding all successors of the current
3490 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00003491 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003492 E = BB->succ_end(); I != E; ++I)
Evan Chengce319102009-09-19 09:51:03 +00003493 sinkMBB->addSuccessor(*I);
Evan Chenga8e29892007-01-19 07:51:42 +00003494 // Next, remove all successors of the current block, and add the true
3495 // and fallthrough blocks as its successors.
Evan Chengce319102009-09-19 09:51:03 +00003496 while (!BB->succ_empty())
Evan Chenga8e29892007-01-19 07:51:42 +00003497 BB->removeSuccessor(BB->succ_begin());
3498 BB->addSuccessor(copy0MBB);
3499 BB->addSuccessor(sinkMBB);
3500
3501 // copy0MBB:
3502 // %FalseValue = ...
3503 // # fallthrough to sinkMBB
3504 BB = copy0MBB;
3505
3506 // Update machine-CFG edges
3507 BB->addSuccessor(sinkMBB);
3508
3509 // sinkMBB:
3510 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3511 // ...
3512 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00003513 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00003514 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
3515 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
3516
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003517 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00003518 return BB;
3519 }
Evan Cheng86198642009-08-07 00:34:42 +00003520
3521 case ARM::tANDsp:
3522 case ARM::tADDspr_:
3523 case ARM::tSUBspi_:
3524 case ARM::t2SUBrSPi_:
3525 case ARM::t2SUBrSPi12_:
3526 case ARM::t2SUBrSPs_: {
3527 MachineFunction *MF = BB->getParent();
3528 unsigned DstReg = MI->getOperand(0).getReg();
3529 unsigned SrcReg = MI->getOperand(1).getReg();
3530 bool DstIsDead = MI->getOperand(0).isDead();
3531 bool SrcIsKill = MI->getOperand(1).isKill();
3532
3533 if (SrcReg != ARM::SP) {
3534 // Copy the source to SP from virtual register.
3535 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
3536 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3537 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
3538 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
3539 .addReg(SrcReg, getKillRegState(SrcIsKill));
3540 }
3541
3542 unsigned OpOpc = 0;
3543 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
3544 switch (MI->getOpcode()) {
3545 default:
3546 llvm_unreachable("Unexpected pseudo instruction!");
3547 case ARM::tANDsp:
3548 OpOpc = ARM::tAND;
3549 NeedPred = true;
3550 break;
3551 case ARM::tADDspr_:
3552 OpOpc = ARM::tADDspr;
3553 break;
3554 case ARM::tSUBspi_:
3555 OpOpc = ARM::tSUBspi;
3556 break;
3557 case ARM::t2SUBrSPi_:
3558 OpOpc = ARM::t2SUBrSPi;
3559 NeedPred = true; NeedCC = true;
3560 break;
3561 case ARM::t2SUBrSPi12_:
3562 OpOpc = ARM::t2SUBrSPi12;
3563 NeedPred = true;
3564 break;
3565 case ARM::t2SUBrSPs_:
3566 OpOpc = ARM::t2SUBrSPs;
3567 NeedPred = true; NeedCC = true; NeedOp3 = true;
3568 break;
3569 }
3570 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
3571 if (OpOpc == ARM::tAND)
3572 AddDefaultT1CC(MIB);
3573 MIB.addReg(ARM::SP);
3574 MIB.addOperand(MI->getOperand(2));
3575 if (NeedOp3)
3576 MIB.addOperand(MI->getOperand(3));
3577 if (NeedPred)
3578 AddDefaultPred(MIB);
3579 if (NeedCC)
3580 AddDefaultCC(MIB);
3581
3582 // Copy the result from SP to virtual register.
3583 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
3584 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3585 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
3586 BuildMI(BB, dl, TII->get(CopyOpc))
3587 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
3588 .addReg(ARM::SP);
3589 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3590 return BB;
3591 }
Evan Chenga8e29892007-01-19 07:51:42 +00003592 }
3593}
3594
3595//===----------------------------------------------------------------------===//
3596// ARM Optimization Hooks
3597//===----------------------------------------------------------------------===//
3598
Chris Lattnerd1980a52009-03-12 06:52:53 +00003599static
3600SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3601 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00003602 SelectionDAG &DAG = DCI.DAG;
3603 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003604 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00003605 unsigned Opc = N->getOpcode();
3606 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3607 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3608 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3609 ISD::CondCode CC = ISD::SETCC_INVALID;
3610
3611 if (isSlctCC) {
3612 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
3613 } else {
3614 SDValue CCOp = Slct.getOperand(0);
3615 if (CCOp.getOpcode() == ISD::SETCC)
3616 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
3617 }
3618
3619 bool DoXform = false;
3620 bool InvCC = false;
3621 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
3622 "Bad input!");
3623
3624 if (LHS.getOpcode() == ISD::Constant &&
3625 cast<ConstantSDNode>(LHS)->isNullValue()) {
3626 DoXform = true;
3627 } else if (CC != ISD::SETCC_INVALID &&
3628 RHS.getOpcode() == ISD::Constant &&
3629 cast<ConstantSDNode>(RHS)->isNullValue()) {
3630 std::swap(LHS, RHS);
3631 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003632 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00003633 Op0.getOperand(0).getValueType();
3634 bool isInt = OpVT.isInteger();
3635 CC = ISD::getSetCCInverse(CC, isInt);
3636
3637 if (!TLI.isCondCodeLegal(CC, OpVT))
3638 return SDValue(); // Inverse operator isn't legal.
3639
3640 DoXform = true;
3641 InvCC = true;
3642 }
3643
3644 if (DoXform) {
3645 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3646 if (isSlctCC)
3647 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3648 Slct.getOperand(0), Slct.getOperand(1), CC);
3649 SDValue CCOp = Slct.getOperand(0);
3650 if (InvCC)
3651 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3652 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3653 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3654 CCOp, OtherOp, Result);
3655 }
3656 return SDValue();
3657}
3658
3659/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3660static SDValue PerformADDCombine(SDNode *N,
3661 TargetLowering::DAGCombinerInfo &DCI) {
3662 // added by evan in r37685 with no testcase.
3663 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003664
Chris Lattnerd1980a52009-03-12 06:52:53 +00003665 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3666 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3667 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3668 if (Result.getNode()) return Result;
3669 }
3670 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3671 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3672 if (Result.getNode()) return Result;
3673 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003674
Chris Lattnerd1980a52009-03-12 06:52:53 +00003675 return SDValue();
3676}
3677
3678/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3679static SDValue PerformSUBCombine(SDNode *N,
3680 TargetLowering::DAGCombinerInfo &DCI) {
3681 // added by evan in r37685 with no testcase.
3682 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003683
Chris Lattnerd1980a52009-03-12 06:52:53 +00003684 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3685 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3686 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3687 if (Result.getNode()) return Result;
3688 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003689
Chris Lattnerd1980a52009-03-12 06:52:53 +00003690 return SDValue();
3691}
3692
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003693static SDValue PerformMULCombine(SDNode *N,
3694 TargetLowering::DAGCombinerInfo &DCI,
3695 const ARMSubtarget *Subtarget) {
3696 SelectionDAG &DAG = DCI.DAG;
3697
3698 if (Subtarget->isThumb1Only())
3699 return SDValue();
3700
3701 if (DAG.getMachineFunction().
3702 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
3703 return SDValue();
3704
3705 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
3706 return SDValue();
3707
3708 EVT VT = N->getValueType(0);
3709 if (VT != MVT::i32)
3710 return SDValue();
3711
3712 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
3713 if (!C)
3714 return SDValue();
3715
3716 uint64_t MulAmt = C->getZExtValue();
3717 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
3718 ShiftAmt = ShiftAmt & (32 - 1);
3719 SDValue V = N->getOperand(0);
3720 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003721
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003722 SDValue Res;
3723 MulAmt >>= ShiftAmt;
3724 if (isPowerOf2_32(MulAmt - 1)) {
3725 // (mul x, 2^N + 1) => (add (shl x, N), x)
3726 Res = DAG.getNode(ISD::ADD, DL, VT,
3727 V, DAG.getNode(ISD::SHL, DL, VT,
3728 V, DAG.getConstant(Log2_32(MulAmt-1),
3729 MVT::i32)));
3730 } else if (isPowerOf2_32(MulAmt + 1)) {
3731 // (mul x, 2^N - 1) => (sub (shl x, N), x)
3732 Res = DAG.getNode(ISD::SUB, DL, VT,
3733 DAG.getNode(ISD::SHL, DL, VT,
3734 V, DAG.getConstant(Log2_32(MulAmt+1),
3735 MVT::i32)),
3736 V);
3737 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003738 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003739
3740 if (ShiftAmt != 0)
3741 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
3742 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003743
3744 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003745 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003746 return SDValue();
3747}
3748
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +00003749/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
3750/// ARMISD::VMOVRRD.
Jim Grosbache5165492009-11-09 00:11:35 +00003751static SDValue PerformVMOVRRDCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003752 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003753 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003754 SDValue InDouble = N->getOperand(0);
Jim Grosbache5165492009-11-09 00:11:35 +00003755 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003756 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003757 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003758}
3759
Bob Wilson5bafff32009-06-22 23:27:02 +00003760/// getVShiftImm - Check if this is a valid build_vector for the immediate
3761/// operand of a vector shift operation, where all the elements of the
3762/// build_vector must have the same constant integer value.
3763static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3764 // Ignore bit_converts.
3765 while (Op.getOpcode() == ISD::BIT_CONVERT)
3766 Op = Op.getOperand(0);
3767 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3768 APInt SplatBits, SplatUndef;
3769 unsigned SplatBitSize;
3770 bool HasAnyUndefs;
3771 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3772 HasAnyUndefs, ElementBits) ||
3773 SplatBitSize > ElementBits)
3774 return false;
3775 Cnt = SplatBits.getSExtValue();
3776 return true;
3777}
3778
3779/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3780/// operand of a vector shift left operation. That value must be in the range:
3781/// 0 <= Value < ElementBits for a left shift; or
3782/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003783static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003784 assert(VT.isVector() && "vector shift count is not a vector type");
3785 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3786 if (! getVShiftImm(Op, ElementBits, Cnt))
3787 return false;
3788 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3789}
3790
3791/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3792/// operand of a vector shift right operation. For a shift opcode, the value
3793/// is positive, but for an intrinsic the value count must be negative. The
3794/// absolute value must be in the range:
3795/// 1 <= |Value| <= ElementBits for a right shift; or
3796/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003797static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00003798 int64_t &Cnt) {
3799 assert(VT.isVector() && "vector shift count is not a vector type");
3800 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3801 if (! getVShiftImm(Op, ElementBits, Cnt))
3802 return false;
3803 if (isIntrinsic)
3804 Cnt = -Cnt;
3805 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3806}
3807
3808/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3809static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3810 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3811 switch (IntNo) {
3812 default:
3813 // Don't do anything for most intrinsics.
3814 break;
3815
3816 // Vector shifts: check for immediate versions and lower them.
3817 // Note: This is done during DAG combining instead of DAG legalizing because
3818 // the build_vectors for 64-bit vector element shift counts are generally
3819 // not legal, and it is hard to see their values after they get legalized to
3820 // loads from a constant pool.
3821 case Intrinsic::arm_neon_vshifts:
3822 case Intrinsic::arm_neon_vshiftu:
3823 case Intrinsic::arm_neon_vshiftls:
3824 case Intrinsic::arm_neon_vshiftlu:
3825 case Intrinsic::arm_neon_vshiftn:
3826 case Intrinsic::arm_neon_vrshifts:
3827 case Intrinsic::arm_neon_vrshiftu:
3828 case Intrinsic::arm_neon_vrshiftn:
3829 case Intrinsic::arm_neon_vqshifts:
3830 case Intrinsic::arm_neon_vqshiftu:
3831 case Intrinsic::arm_neon_vqshiftsu:
3832 case Intrinsic::arm_neon_vqshiftns:
3833 case Intrinsic::arm_neon_vqshiftnu:
3834 case Intrinsic::arm_neon_vqshiftnsu:
3835 case Intrinsic::arm_neon_vqrshiftns:
3836 case Intrinsic::arm_neon_vqrshiftnu:
3837 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00003838 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003839 int64_t Cnt;
3840 unsigned VShiftOpc = 0;
3841
3842 switch (IntNo) {
3843 case Intrinsic::arm_neon_vshifts:
3844 case Intrinsic::arm_neon_vshiftu:
3845 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3846 VShiftOpc = ARMISD::VSHL;
3847 break;
3848 }
3849 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3850 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3851 ARMISD::VSHRs : ARMISD::VSHRu);
3852 break;
3853 }
3854 return SDValue();
3855
3856 case Intrinsic::arm_neon_vshiftls:
3857 case Intrinsic::arm_neon_vshiftlu:
3858 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3859 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003860 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003861
3862 case Intrinsic::arm_neon_vrshifts:
3863 case Intrinsic::arm_neon_vrshiftu:
3864 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3865 break;
3866 return SDValue();
3867
3868 case Intrinsic::arm_neon_vqshifts:
3869 case Intrinsic::arm_neon_vqshiftu:
3870 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3871 break;
3872 return SDValue();
3873
3874 case Intrinsic::arm_neon_vqshiftsu:
3875 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3876 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003877 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003878
3879 case Intrinsic::arm_neon_vshiftn:
3880 case Intrinsic::arm_neon_vrshiftn:
3881 case Intrinsic::arm_neon_vqshiftns:
3882 case Intrinsic::arm_neon_vqshiftnu:
3883 case Intrinsic::arm_neon_vqshiftnsu:
3884 case Intrinsic::arm_neon_vqrshiftns:
3885 case Intrinsic::arm_neon_vqrshiftnu:
3886 case Intrinsic::arm_neon_vqrshiftnsu:
3887 // Narrowing shifts require an immediate right shift.
3888 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3889 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003890 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003891
3892 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003893 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003894 }
3895
3896 switch (IntNo) {
3897 case Intrinsic::arm_neon_vshifts:
3898 case Intrinsic::arm_neon_vshiftu:
3899 // Opcode already set above.
3900 break;
3901 case Intrinsic::arm_neon_vshiftls:
3902 case Intrinsic::arm_neon_vshiftlu:
3903 if (Cnt == VT.getVectorElementType().getSizeInBits())
3904 VShiftOpc = ARMISD::VSHLLi;
3905 else
3906 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3907 ARMISD::VSHLLs : ARMISD::VSHLLu);
3908 break;
3909 case Intrinsic::arm_neon_vshiftn:
3910 VShiftOpc = ARMISD::VSHRN; break;
3911 case Intrinsic::arm_neon_vrshifts:
3912 VShiftOpc = ARMISD::VRSHRs; break;
3913 case Intrinsic::arm_neon_vrshiftu:
3914 VShiftOpc = ARMISD::VRSHRu; break;
3915 case Intrinsic::arm_neon_vrshiftn:
3916 VShiftOpc = ARMISD::VRSHRN; break;
3917 case Intrinsic::arm_neon_vqshifts:
3918 VShiftOpc = ARMISD::VQSHLs; break;
3919 case Intrinsic::arm_neon_vqshiftu:
3920 VShiftOpc = ARMISD::VQSHLu; break;
3921 case Intrinsic::arm_neon_vqshiftsu:
3922 VShiftOpc = ARMISD::VQSHLsu; break;
3923 case Intrinsic::arm_neon_vqshiftns:
3924 VShiftOpc = ARMISD::VQSHRNs; break;
3925 case Intrinsic::arm_neon_vqshiftnu:
3926 VShiftOpc = ARMISD::VQSHRNu; break;
3927 case Intrinsic::arm_neon_vqshiftnsu:
3928 VShiftOpc = ARMISD::VQSHRNsu; break;
3929 case Intrinsic::arm_neon_vqrshiftns:
3930 VShiftOpc = ARMISD::VQRSHRNs; break;
3931 case Intrinsic::arm_neon_vqrshiftnu:
3932 VShiftOpc = ARMISD::VQRSHRNu; break;
3933 case Intrinsic::arm_neon_vqrshiftnsu:
3934 VShiftOpc = ARMISD::VQRSHRNsu; break;
3935 }
3936
3937 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003938 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003939 }
3940
3941 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00003942 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003943 int64_t Cnt;
3944 unsigned VShiftOpc = 0;
3945
3946 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3947 VShiftOpc = ARMISD::VSLI;
3948 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3949 VShiftOpc = ARMISD::VSRI;
3950 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003951 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003952 }
3953
3954 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3955 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00003956 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003957 }
3958
3959 case Intrinsic::arm_neon_vqrshifts:
3960 case Intrinsic::arm_neon_vqrshiftu:
3961 // No immediate versions of these to check for.
3962 break;
3963 }
3964
3965 return SDValue();
3966}
3967
3968/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3969/// lowers them. As with the vector shift intrinsics, this is done during DAG
3970/// combining instead of DAG legalizing because the build_vectors for 64-bit
3971/// vector element shift counts are generally not legal, and it is hard to see
3972/// their values after they get legalized to loads from a constant pool.
3973static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3974 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003975 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003976
3977 // Nothing to be done for scalar shifts.
3978 if (! VT.isVector())
3979 return SDValue();
3980
3981 assert(ST->hasNEON() && "unexpected vector shift");
3982 int64_t Cnt;
3983
3984 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003985 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003986
3987 case ISD::SHL:
3988 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3989 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003990 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003991 break;
3992
3993 case ISD::SRA:
3994 case ISD::SRL:
3995 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3996 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3997 ARMISD::VSHRs : ARMISD::VSHRu);
3998 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003999 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004000 }
4001 }
4002 return SDValue();
4003}
4004
4005/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
4006/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
4007static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
4008 const ARMSubtarget *ST) {
4009 SDValue N0 = N->getOperand(0);
4010
4011 // Check for sign- and zero-extensions of vector extract operations of 8-
4012 // and 16-bit vector elements. NEON supports these directly. They are
4013 // handled during DAG combining because type legalization will promote them
4014 // to 32-bit types and it is messy to recognize the operations after that.
4015 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
4016 SDValue Vec = N0.getOperand(0);
4017 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004018 EVT VT = N->getValueType(0);
4019 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004020 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4021
Owen Anderson825b72b2009-08-11 20:47:22 +00004022 if (VT == MVT::i32 &&
4023 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00004024 TLI.isTypeLegal(Vec.getValueType())) {
4025
4026 unsigned Opc = 0;
4027 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004028 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00004029 case ISD::SIGN_EXTEND:
4030 Opc = ARMISD::VGETLANEs;
4031 break;
4032 case ISD::ZERO_EXTEND:
4033 case ISD::ANY_EXTEND:
4034 Opc = ARMISD::VGETLANEu;
4035 break;
4036 }
4037 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
4038 }
4039 }
4040
4041 return SDValue();
4042}
4043
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004044/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
4045/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
4046static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
4047 const ARMSubtarget *ST) {
4048 // If the target supports NEON, try to use vmax/vmin instructions for f32
4049 // selects like "x < y ? x : y". Unless the FiniteOnlyFPMath option is set,
4050 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
4051 // a NaN; only do the transformation when it matches that behavior.
4052
4053 // For now only do this when using NEON for FP operations; if using VFP, it
4054 // is not obvious that the benefit outweighs the cost of switching to the
4055 // NEON pipeline.
4056 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
4057 N->getValueType(0) != MVT::f32)
4058 return SDValue();
4059
4060 SDValue CondLHS = N->getOperand(0);
4061 SDValue CondRHS = N->getOperand(1);
4062 SDValue LHS = N->getOperand(2);
4063 SDValue RHS = N->getOperand(3);
4064 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
4065
4066 unsigned Opcode = 0;
4067 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00004068 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004069 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00004070 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004071 IsReversed = true ; // x CC y ? y : x
4072 } else {
4073 return SDValue();
4074 }
4075
Bob Wilsone742bb52010-02-24 22:15:53 +00004076 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004077 switch (CC) {
4078 default: break;
4079 case ISD::SETOLT:
4080 case ISD::SETOLE:
4081 case ISD::SETLT:
4082 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004083 case ISD::SETULT:
4084 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004085 // If LHS is NaN, an ordered comparison will be false and the result will
4086 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
4087 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4088 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
4089 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4090 break;
4091 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
4092 // will return -0, so vmin can only be used for unsafe math or if one of
4093 // the operands is known to be nonzero.
4094 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
4095 !UnsafeFPMath &&
4096 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4097 break;
4098 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004099 break;
4100
4101 case ISD::SETOGT:
4102 case ISD::SETOGE:
4103 case ISD::SETGT:
4104 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004105 case ISD::SETUGT:
4106 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004107 // If LHS is NaN, an ordered comparison will be false and the result will
4108 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
4109 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4110 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
4111 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4112 break;
4113 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
4114 // will return +0, so vmax can only be used for unsafe math or if one of
4115 // the operands is known to be nonzero.
4116 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
4117 !UnsafeFPMath &&
4118 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4119 break;
4120 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004121 break;
4122 }
4123
4124 if (!Opcode)
4125 return SDValue();
4126 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
4127}
4128
Dan Gohman475871a2008-07-27 21:46:04 +00004129SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004130 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004131 switch (N->getOpcode()) {
4132 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004133 case ISD::ADD: return PerformADDCombine(N, DCI);
4134 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004135 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbache5165492009-11-09 00:11:35 +00004136 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004137 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004138 case ISD::SHL:
4139 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004140 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004141 case ISD::SIGN_EXTEND:
4142 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004143 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
4144 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004145 }
Dan Gohman475871a2008-07-27 21:46:04 +00004146 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004147}
4148
Bill Wendlingaf566342009-08-15 21:21:19 +00004149bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
4150 if (!Subtarget->hasV6Ops())
4151 // Pre-v6 does not support unaligned mem access.
4152 return false;
Anton Korobeynikov90cfc132010-01-30 14:08:12 +00004153 else {
4154 // v6+ may or may not support unaligned mem access depending on the system
4155 // configuration.
4156 // FIXME: This is pretty conservative. Should we provide cmdline option to
4157 // control the behaviour?
Bill Wendlingaf566342009-08-15 21:21:19 +00004158 if (!Subtarget->isTargetDarwin())
4159 return false;
4160 }
4161
4162 switch (VT.getSimpleVT().SimpleTy) {
4163 default:
4164 return false;
4165 case MVT::i8:
4166 case MVT::i16:
4167 case MVT::i32:
4168 return true;
4169 // FIXME: VLD1 etc with standard alignment is legal.
4170 }
4171}
4172
Evan Chenge6c835f2009-08-14 20:09:37 +00004173static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
4174 if (V < 0)
4175 return false;
4176
4177 unsigned Scale = 1;
4178 switch (VT.getSimpleVT().SimpleTy) {
4179 default: return false;
4180 case MVT::i1:
4181 case MVT::i8:
4182 // Scale == 1;
4183 break;
4184 case MVT::i16:
4185 // Scale == 2;
4186 Scale = 2;
4187 break;
4188 case MVT::i32:
4189 // Scale == 4;
4190 Scale = 4;
4191 break;
4192 }
4193
4194 if ((V & (Scale - 1)) != 0)
4195 return false;
4196 V /= Scale;
4197 return V == (V & ((1LL << 5) - 1));
4198}
4199
4200static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
4201 const ARMSubtarget *Subtarget) {
4202 bool isNeg = false;
4203 if (V < 0) {
4204 isNeg = true;
4205 V = - V;
4206 }
4207
4208 switch (VT.getSimpleVT().SimpleTy) {
4209 default: return false;
4210 case MVT::i1:
4211 case MVT::i8:
4212 case MVT::i16:
4213 case MVT::i32:
4214 // + imm12 or - imm8
4215 if (isNeg)
4216 return V == (V & ((1LL << 8) - 1));
4217 return V == (V & ((1LL << 12) - 1));
4218 case MVT::f32:
4219 case MVT::f64:
4220 // Same as ARM mode. FIXME: NEON?
4221 if (!Subtarget->hasVFP2())
4222 return false;
4223 if ((V & 3) != 0)
4224 return false;
4225 V >>= 2;
4226 return V == (V & ((1LL << 8) - 1));
4227 }
4228}
4229
Evan Chengb01fad62007-03-12 23:30:29 +00004230/// isLegalAddressImmediate - Return true if the integer value can be used
4231/// as the offset of the target addressing mode for load / store of the
4232/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00004233static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004234 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00004235 if (V == 0)
4236 return true;
4237
Evan Cheng65011532009-03-09 19:15:00 +00004238 if (!VT.isSimple())
4239 return false;
4240
Evan Chenge6c835f2009-08-14 20:09:37 +00004241 if (Subtarget->isThumb1Only())
4242 return isLegalT1AddressImmediate(V, VT);
4243 else if (Subtarget->isThumb2())
4244 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00004245
Evan Chenge6c835f2009-08-14 20:09:37 +00004246 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00004247 if (V < 0)
4248 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00004249 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00004250 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004251 case MVT::i1:
4252 case MVT::i8:
4253 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00004254 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004255 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004256 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00004257 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004258 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004259 case MVT::f32:
4260 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00004261 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00004262 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00004263 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00004264 return false;
4265 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004266 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00004267 }
Evan Chenga8e29892007-01-19 07:51:42 +00004268}
4269
Evan Chenge6c835f2009-08-14 20:09:37 +00004270bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
4271 EVT VT) const {
4272 int Scale = AM.Scale;
4273 if (Scale < 0)
4274 return false;
4275
4276 switch (VT.getSimpleVT().SimpleTy) {
4277 default: return false;
4278 case MVT::i1:
4279 case MVT::i8:
4280 case MVT::i16:
4281 case MVT::i32:
4282 if (Scale == 1)
4283 return true;
4284 // r + r << imm
4285 Scale = Scale & ~1;
4286 return Scale == 2 || Scale == 4 || Scale == 8;
4287 case MVT::i64:
4288 // r + r
4289 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
4290 return true;
4291 return false;
4292 case MVT::isVoid:
4293 // Note, we allow "void" uses (basically, uses that aren't loads or
4294 // stores), because arm allows folding a scale into many arithmetic
4295 // operations. This should be made more precise and revisited later.
4296
4297 // Allow r << imm, but the imm has to be a multiple of two.
4298 if (Scale & 1) return false;
4299 return isPowerOf2_32(Scale);
4300 }
4301}
4302
Chris Lattner37caf8c2007-04-09 23:33:39 +00004303/// isLegalAddressingMode - Return true if the addressing mode represented
4304/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004305bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004306 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004307 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00004308 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00004309 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004310
Chris Lattner37caf8c2007-04-09 23:33:39 +00004311 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004312 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004313 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004314
Chris Lattner37caf8c2007-04-09 23:33:39 +00004315 switch (AM.Scale) {
4316 case 0: // no scale reg, must be "r+i" or "r", or "i".
4317 break;
4318 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00004319 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00004320 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004321 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00004322 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004323 // ARM doesn't support any R+R*scale+imm addr modes.
4324 if (AM.BaseOffs)
4325 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004326
Bob Wilson2c7dab12009-04-08 17:55:28 +00004327 if (!VT.isSimple())
4328 return false;
4329
Evan Chenge6c835f2009-08-14 20:09:37 +00004330 if (Subtarget->isThumb2())
4331 return isLegalT2ScaledAddressingMode(AM, VT);
4332
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004333 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00004334 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00004335 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004336 case MVT::i1:
4337 case MVT::i8:
4338 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004339 if (Scale < 0) Scale = -Scale;
4340 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004341 return true;
4342 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00004343 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00004344 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00004345 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004346 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004347 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004348 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00004349 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004350
Owen Anderson825b72b2009-08-11 20:47:22 +00004351 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004352 // Note, we allow "void" uses (basically, uses that aren't loads or
4353 // stores), because arm allows folding a scale into many arithmetic
4354 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004355
Chris Lattner37caf8c2007-04-09 23:33:39 +00004356 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00004357 if (Scale & 1) return false;
4358 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00004359 }
4360 break;
Evan Chengb01fad62007-03-12 23:30:29 +00004361 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00004362 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00004363}
4364
Evan Cheng77e47512009-11-11 19:05:52 +00004365/// isLegalICmpImmediate - Return true if the specified immediate is legal
4366/// icmp immediate, that is the target has icmp instructions which can compare
4367/// a register against the immediate without having to materialize the
4368/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00004369bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00004370 if (!Subtarget->isThumb())
4371 return ARM_AM::getSOImmVal(Imm) != -1;
4372 if (Subtarget->isThumb2())
4373 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00004374 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00004375}
4376
Owen Andersone50ed302009-08-10 22:56:29 +00004377static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004378 bool isSEXTLoad, SDValue &Base,
4379 SDValue &Offset, bool &isInc,
4380 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00004381 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4382 return false;
4383
Owen Anderson825b72b2009-08-11 20:47:22 +00004384 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00004385 // AddressingMode 3
4386 Base = Ptr->getOperand(0);
4387 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004388 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004389 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004390 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004391 isInc = false;
4392 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4393 return true;
4394 }
4395 }
4396 isInc = (Ptr->getOpcode() == ISD::ADD);
4397 Offset = Ptr->getOperand(1);
4398 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004399 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00004400 // AddressingMode 2
4401 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004402 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004403 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004404 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004405 isInc = false;
4406 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4407 Base = Ptr->getOperand(0);
4408 return true;
4409 }
4410 }
4411
4412 if (Ptr->getOpcode() == ISD::ADD) {
4413 isInc = true;
4414 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
4415 if (ShOpcVal != ARM_AM::no_shift) {
4416 Base = Ptr->getOperand(1);
4417 Offset = Ptr->getOperand(0);
4418 } else {
4419 Base = Ptr->getOperand(0);
4420 Offset = Ptr->getOperand(1);
4421 }
4422 return true;
4423 }
4424
4425 isInc = (Ptr->getOpcode() == ISD::ADD);
4426 Base = Ptr->getOperand(0);
4427 Offset = Ptr->getOperand(1);
4428 return true;
4429 }
4430
Jim Grosbache5165492009-11-09 00:11:35 +00004431 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00004432 return false;
4433}
4434
Owen Andersone50ed302009-08-10 22:56:29 +00004435static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004436 bool isSEXTLoad, SDValue &Base,
4437 SDValue &Offset, bool &isInc,
4438 SelectionDAG &DAG) {
4439 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4440 return false;
4441
4442 Base = Ptr->getOperand(0);
4443 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
4444 int RHSC = (int)RHS->getZExtValue();
4445 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
4446 assert(Ptr->getOpcode() == ISD::ADD);
4447 isInc = false;
4448 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4449 return true;
4450 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
4451 isInc = Ptr->getOpcode() == ISD::ADD;
4452 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
4453 return true;
4454 }
4455 }
4456
4457 return false;
4458}
4459
Evan Chenga8e29892007-01-19 07:51:42 +00004460/// getPreIndexedAddressParts - returns true by value, base pointer and
4461/// offset pointer and addressing mode by reference if the node's address
4462/// can be legally represented as pre-indexed load / store address.
4463bool
Dan Gohman475871a2008-07-27 21:46:04 +00004464ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
4465 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004466 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004467 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004468 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004469 return false;
4470
Owen Andersone50ed302009-08-10 22:56:29 +00004471 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004472 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004473 bool isSEXTLoad = false;
4474 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4475 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004476 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004477 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4478 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
4479 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004480 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004481 } else
4482 return false;
4483
4484 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004485 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004486 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004487 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
4488 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004489 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004490 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00004491 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00004492 if (!isLegal)
4493 return false;
4494
4495 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
4496 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004497}
4498
4499/// getPostIndexedAddressParts - returns true by value, base pointer and
4500/// offset pointer and addressing mode by reference if this node can be
4501/// combined with a load / store to form a post-indexed load / store.
4502bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00004503 SDValue &Base,
4504 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004505 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004506 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004507 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004508 return false;
4509
Owen Andersone50ed302009-08-10 22:56:29 +00004510 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004511 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004512 bool isSEXTLoad = false;
4513 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004514 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00004515 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00004516 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4517 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004518 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00004519 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00004520 } else
4521 return false;
4522
4523 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004524 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004525 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004526 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00004527 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004528 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004529 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
4530 isInc, DAG);
4531 if (!isLegal)
4532 return false;
4533
Evan Cheng28dad2a2010-05-18 21:31:17 +00004534 if (Ptr != Base) {
4535 // Swap base ptr and offset to catch more post-index load / store when
4536 // it's legal. In Thumb2 mode, offset must be an immediate.
4537 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
4538 !Subtarget->isThumb2())
4539 std::swap(Base, Offset);
4540
4541 // Post-indexed load / store update the base pointer.
4542 if (Ptr != Base)
4543 return false;
4544 }
4545
Evan Chenge88d5ce2009-07-02 07:28:31 +00004546 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
4547 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004548}
4549
Dan Gohman475871a2008-07-27 21:46:04 +00004550void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00004551 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004552 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004553 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00004554 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00004555 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004556 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004557 switch (Op.getOpcode()) {
4558 default: break;
4559 case ARMISD::CMOV: {
4560 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00004561 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004562 if (KnownZero == 0 && KnownOne == 0) return;
4563
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004564 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00004565 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
4566 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004567 KnownZero &= KnownZeroRHS;
4568 KnownOne &= KnownOneRHS;
4569 return;
4570 }
4571 }
4572}
4573
4574//===----------------------------------------------------------------------===//
4575// ARM Inline Assembly Support
4576//===----------------------------------------------------------------------===//
4577
4578/// getConstraintType - Given a constraint letter, return the type of
4579/// constraint it is for this target.
4580ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00004581ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
4582 if (Constraint.size() == 1) {
4583 switch (Constraint[0]) {
4584 default: break;
4585 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004586 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00004587 }
Evan Chenga8e29892007-01-19 07:51:42 +00004588 }
Chris Lattner4234f572007-03-25 02:14:49 +00004589 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00004590}
4591
Bob Wilson2dc4f542009-03-20 22:42:55 +00004592std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00004593ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004594 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004595 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004596 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00004597 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004598 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004599 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004600 return std::make_pair(0U, ARM::tGPRRegisterClass);
4601 else
4602 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004603 case 'r':
4604 return std::make_pair(0U, ARM::GPRRegisterClass);
4605 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004606 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004607 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00004608 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004609 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00004610 if (VT.getSizeInBits() == 128)
4611 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004612 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004613 }
4614 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00004615 if (StringRef("{cc}").equals_lower(Constraint))
4616 return std::make_pair(0U, ARM::CCRRegisterClass);
4617
Evan Chenga8e29892007-01-19 07:51:42 +00004618 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
4619}
4620
4621std::vector<unsigned> ARMTargetLowering::
4622getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004623 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004624 if (Constraint.size() != 1)
4625 return std::vector<unsigned>();
4626
4627 switch (Constraint[0]) { // GCC ARM Constraint Letters
4628 default: break;
4629 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004630 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4631 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4632 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004633 case 'r':
4634 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4635 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4636 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
4637 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004638 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004639 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004640 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
4641 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
4642 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
4643 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
4644 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
4645 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
4646 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
4647 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00004648 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004649 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
4650 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
4651 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
4652 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00004653 if (VT.getSizeInBits() == 128)
4654 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
4655 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004656 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004657 }
4658
4659 return std::vector<unsigned>();
4660}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004661
4662/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
4663/// vector. If it is invalid, don't add anything to Ops.
4664void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
4665 char Constraint,
4666 bool hasMemory,
4667 std::vector<SDValue>&Ops,
4668 SelectionDAG &DAG) const {
4669 SDValue Result(0, 0);
4670
4671 switch (Constraint) {
4672 default: break;
4673 case 'I': case 'J': case 'K': case 'L':
4674 case 'M': case 'N': case 'O':
4675 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
4676 if (!C)
4677 return;
4678
4679 int64_t CVal64 = C->getSExtValue();
4680 int CVal = (int) CVal64;
4681 // None of these constraints allow values larger than 32 bits. Check
4682 // that the value fits in an int.
4683 if (CVal != CVal64)
4684 return;
4685
4686 switch (Constraint) {
4687 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004688 if (Subtarget->isThumb1Only()) {
4689 // This must be a constant between 0 and 255, for ADD
4690 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004691 if (CVal >= 0 && CVal <= 255)
4692 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004693 } else if (Subtarget->isThumb2()) {
4694 // A constant that can be used as an immediate value in a
4695 // data-processing instruction.
4696 if (ARM_AM::getT2SOImmVal(CVal) != -1)
4697 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004698 } else {
4699 // A constant that can be used as an immediate value in a
4700 // data-processing instruction.
4701 if (ARM_AM::getSOImmVal(CVal) != -1)
4702 break;
4703 }
4704 return;
4705
4706 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004707 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004708 // This must be a constant between -255 and -1, for negated ADD
4709 // immediates. This can be used in GCC with an "n" modifier that
4710 // prints the negated value, for use with SUB instructions. It is
4711 // not useful otherwise but is implemented for compatibility.
4712 if (CVal >= -255 && CVal <= -1)
4713 break;
4714 } else {
4715 // This must be a constant between -4095 and 4095. It is not clear
4716 // what this constraint is intended for. Implemented for
4717 // compatibility with GCC.
4718 if (CVal >= -4095 && CVal <= 4095)
4719 break;
4720 }
4721 return;
4722
4723 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004724 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004725 // A 32-bit value where only one byte has a nonzero value. Exclude
4726 // zero to match GCC. This constraint is used by GCC internally for
4727 // constants that can be loaded with a move/shift combination.
4728 // It is not useful otherwise but is implemented for compatibility.
4729 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
4730 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004731 } else if (Subtarget->isThumb2()) {
4732 // A constant whose bitwise inverse can be used as an immediate
4733 // value in a data-processing instruction. This can be used in GCC
4734 // with a "B" modifier that prints the inverted value, for use with
4735 // BIC and MVN instructions. It is not useful otherwise but is
4736 // implemented for compatibility.
4737 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
4738 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004739 } else {
4740 // A constant whose bitwise inverse can be used as an immediate
4741 // value in a data-processing instruction. This can be used in GCC
4742 // with a "B" modifier that prints the inverted value, for use with
4743 // BIC and MVN instructions. It is not useful otherwise but is
4744 // implemented for compatibility.
4745 if (ARM_AM::getSOImmVal(~CVal) != -1)
4746 break;
4747 }
4748 return;
4749
4750 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004751 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004752 // This must be a constant between -7 and 7,
4753 // for 3-operand ADD/SUB immediate instructions.
4754 if (CVal >= -7 && CVal < 7)
4755 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004756 } else if (Subtarget->isThumb2()) {
4757 // A constant whose negation can be used as an immediate value in a
4758 // data-processing instruction. This can be used in GCC with an "n"
4759 // modifier that prints the negated value, for use with SUB
4760 // instructions. It is not useful otherwise but is implemented for
4761 // compatibility.
4762 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
4763 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004764 } else {
4765 // A constant whose negation can be used as an immediate value in a
4766 // data-processing instruction. This can be used in GCC with an "n"
4767 // modifier that prints the negated value, for use with SUB
4768 // instructions. It is not useful otherwise but is implemented for
4769 // compatibility.
4770 if (ARM_AM::getSOImmVal(-CVal) != -1)
4771 break;
4772 }
4773 return;
4774
4775 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004776 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004777 // This must be a multiple of 4 between 0 and 1020, for
4778 // ADD sp + immediate.
4779 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
4780 break;
4781 } else {
4782 // A power of two or a constant between 0 and 32. This is used in
4783 // GCC for the shift amount on shifted register operands, but it is
4784 // useful in general for any shift amounts.
4785 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
4786 break;
4787 }
4788 return;
4789
4790 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004791 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004792 // This must be a constant between 0 and 31, for shift amounts.
4793 if (CVal >= 0 && CVal <= 31)
4794 break;
4795 }
4796 return;
4797
4798 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004799 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004800 // This must be a multiple of 4 between -508 and 508, for
4801 // ADD/SUB sp = sp + immediate.
4802 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
4803 break;
4804 }
4805 return;
4806 }
4807 Result = DAG.getTargetConstant(CVal, Op.getValueType());
4808 break;
4809 }
4810
4811 if (Result.getNode()) {
4812 Ops.push_back(Result);
4813 return;
4814 }
4815 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
4816 Ops, DAG);
4817}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00004818
4819bool
4820ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4821 // The ARM target isn't yet aware of offsets.
4822 return false;
4823}
Evan Cheng39382422009-10-28 01:44:26 +00004824
4825int ARM::getVFPf32Imm(const APFloat &FPImm) {
4826 APInt Imm = FPImm.bitcastToAPInt();
4827 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
4828 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
4829 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
4830
4831 // We can handle 4 bits of mantissa.
4832 // mantissa = (16+UInt(e:f:g:h))/16.
4833 if (Mantissa & 0x7ffff)
4834 return -1;
4835 Mantissa >>= 19;
4836 if ((Mantissa & 0xf) != Mantissa)
4837 return -1;
4838
4839 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4840 if (Exp < -3 || Exp > 4)
4841 return -1;
4842 Exp = ((Exp+3) & 0x7) ^ 4;
4843
4844 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4845}
4846
4847int ARM::getVFPf64Imm(const APFloat &FPImm) {
4848 APInt Imm = FPImm.bitcastToAPInt();
4849 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
4850 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
4851 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
4852
4853 // We can handle 4 bits of mantissa.
4854 // mantissa = (16+UInt(e:f:g:h))/16.
4855 if (Mantissa & 0xffffffffffffLL)
4856 return -1;
4857 Mantissa >>= 48;
4858 if ((Mantissa & 0xf) != Mantissa)
4859 return -1;
4860
4861 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4862 if (Exp < -3 || Exp > 4)
4863 return -1;
4864 Exp = ((Exp+3) & 0x7) ^ 4;
4865
4866 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4867}
4868
4869/// isFPImmLegal - Returns true if the target can instruction select the
4870/// specified FP immediate natively. If false, the legalizer will
4871/// materialize the FP immediate as a load from a constant pool.
4872bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
4873 if (!Subtarget->hasVFP3())
4874 return false;
4875 if (VT == MVT::f32)
4876 return ARM::getVFPf32Imm(Imm) != -1;
4877 if (VT == MVT::f64)
4878 return ARM::getVFPf64Imm(Imm) != -1;
4879 return false;
4880}