blob: 49dd9514b527563a1008f0af9c50ab624e2da856 [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "ARM.h"
16#include "ARMAddressingModes.h"
17#include "ARMConstantPoolValue.h"
18#include "ARMISelLowering.h"
19#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000020#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000021#include "ARMRegisterInfo.h"
22#include "ARMSubtarget.h"
23#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000024#include "ARMTargetObjectFile.h"
Evan Chenga8e29892007-01-19 07:51:42 +000025#include "llvm/CallingConv.h"
26#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000027#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000028#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000029#include "llvm/Instruction.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000030#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000031#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000032#include "llvm/CodeGen/CallingConvLower.h"
Evan Chenga8e29892007-01-19 07:51:42 +000033#include "llvm/CodeGen/MachineBasicBlock.h"
34#include "llvm/CodeGen/MachineFrameInfo.h"
35#include "llvm/CodeGen/MachineFunction.h"
36#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000039#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000040#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000041#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000042#include "llvm/ADT/VectorExtras.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000043#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000044#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000045#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000046#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000047#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000048using namespace llvm;
49
Jim Grosbache7b52522010-04-14 22:28:31 +000050static cl::opt<bool>
51EnableARMLongCalls("arm-long-calls", cl::Hidden,
52 cl::desc("Generate calls via indirect call instructions."),
53 cl::init(false));
54
Owen Andersone50ed302009-08-10 22:56:29 +000055static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000056 CCValAssign::LocInfo &LocInfo,
57 ISD::ArgFlagsTy &ArgFlags,
58 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000059static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000060 CCValAssign::LocInfo &LocInfo,
61 ISD::ArgFlagsTy &ArgFlags,
62 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000063static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000064 CCValAssign::LocInfo &LocInfo,
65 ISD::ArgFlagsTy &ArgFlags,
66 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000067static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000068 CCValAssign::LocInfo &LocInfo,
69 ISD::ArgFlagsTy &ArgFlags,
70 CCState &State);
71
Owen Andersone50ed302009-08-10 22:56:29 +000072void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
73 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000074 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000075 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000076 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
77 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000078
Owen Anderson70671842009-08-10 20:18:46 +000079 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000080 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000081 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000082 }
83
Owen Andersone50ed302009-08-10 22:56:29 +000084 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +000085 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +000086 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +000087 if (ElemTy == MVT::i8 || ElemTy == MVT::i16)
Owen Anderson70671842009-08-10 20:18:46 +000088 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +000089 if (ElemTy != MVT::i32) {
90 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
91 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
92 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
93 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
94 }
Owen Anderson70671842009-08-10 20:18:46 +000095 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
96 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Evan Chengde8aa4e2010-05-05 18:28:36 +000097 if (llvm::ModelWithRegSequence())
98 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
99 else
100 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Custom);
Anton Korobeynikov8e6c2b92009-08-21 12:40:35 +0000101 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
Bob Wilsond0910c42010-04-06 22:02:24 +0000102 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
103 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000104 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000105 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
106 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
107 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000108 }
109
110 // Promote all bit-wise operations.
111 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000112 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000113 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
114 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000115 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000116 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000117 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000118 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000119 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000120 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000121 }
Bob Wilson16330762009-09-16 00:17:28 +0000122
123 // Neon does not support vector divide/remainder operations.
124 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
125 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
126 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
127 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
128 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
129 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000130}
131
Owen Andersone50ed302009-08-10 22:56:29 +0000132void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000133 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000135}
136
Owen Andersone50ed302009-08-10 22:56:29 +0000137void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000138 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000140}
141
Chris Lattnerf0144122009-07-28 03:13:23 +0000142static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
143 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000144 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000145
Chris Lattner80ec2792009-08-02 00:34:36 +0000146 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000147}
148
Evan Chenga8e29892007-01-19 07:51:42 +0000149ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000150 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000151 Subtarget = &TM.getSubtarget<ARMSubtarget>();
152
Evan Chengb1df8f22007-04-27 08:15:43 +0000153 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000154 // Uses VFP for Thumb libfuncs if available.
155 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
156 // Single-precision floating-point arithmetic.
157 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
158 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
159 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
160 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000161
Evan Chengb1df8f22007-04-27 08:15:43 +0000162 // Double-precision floating-point arithmetic.
163 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
164 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
165 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
166 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000167
Evan Chengb1df8f22007-04-27 08:15:43 +0000168 // Single-precision comparisons.
169 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
170 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
171 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
172 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
173 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
174 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
175 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
176 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000177
Evan Chengb1df8f22007-04-27 08:15:43 +0000178 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
179 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
180 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
181 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
182 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
183 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
184 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
185 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000186
Evan Chengb1df8f22007-04-27 08:15:43 +0000187 // Double-precision comparisons.
188 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
189 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
190 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
191 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
192 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
193 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
194 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
195 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000196
Evan Chengb1df8f22007-04-27 08:15:43 +0000197 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
198 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
199 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
200 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
201 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
202 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
203 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
204 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000205
Evan Chengb1df8f22007-04-27 08:15:43 +0000206 // Floating-point to integer conversions.
207 // i64 conversions are done via library routines even when generating VFP
208 // instructions, so use the same ones.
209 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
210 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
211 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
212 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000213
Evan Chengb1df8f22007-04-27 08:15:43 +0000214 // Conversions between floating types.
215 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
216 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
217
218 // Integer to floating-point conversions.
219 // i64 conversions are done via library routines even when generating VFP
220 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000221 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
222 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000223 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
224 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
225 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
226 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
227 }
Evan Chenga8e29892007-01-19 07:51:42 +0000228 }
229
Bob Wilson2f954612009-05-22 17:38:41 +0000230 // These libcalls are not available in 32-bit.
231 setLibcallName(RTLIB::SHL_I128, 0);
232 setLibcallName(RTLIB::SRL_I128, 0);
233 setLibcallName(RTLIB::SRA_I128, 0);
234
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000235 // Libcalls should use the AAPCS base standard ABI, even if hard float
236 // is in effect, as per the ARM RTABI specification, section 4.1.2.
237 if (Subtarget->isAAPCS_ABI()) {
238 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
239 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
240 CallingConv::ARM_AAPCS);
241 }
242 }
243
David Goodwinf1daf7d2009-07-08 23:10:31 +0000244 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000245 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000246 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000247 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000248 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
250 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000251
Owen Anderson825b72b2009-08-11 20:47:22 +0000252 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000253 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000254
255 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000256 addDRTypeForNEON(MVT::v2f32);
257 addDRTypeForNEON(MVT::v8i8);
258 addDRTypeForNEON(MVT::v4i16);
259 addDRTypeForNEON(MVT::v2i32);
260 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000261
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 addQRTypeForNEON(MVT::v4f32);
263 addQRTypeForNEON(MVT::v2f64);
264 addQRTypeForNEON(MVT::v16i8);
265 addQRTypeForNEON(MVT::v8i16);
266 addQRTypeForNEON(MVT::v4i32);
267 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000268
Bob Wilson74dc72e2009-09-15 23:55:57 +0000269 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
270 // neither Neon nor VFP support any arithmetic operations on it.
271 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
272 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
273 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
274 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
275 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
276 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
277 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
278 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
279 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
280 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
281 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
282 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
283 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
284 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
285 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
286 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
287 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
288 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
289 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
290 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
291 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
292 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
293 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
294 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
295
Bob Wilson642b3292009-09-16 00:32:15 +0000296 // Neon does not support some operations on v1i64 and v2i64 types.
297 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
298 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
299 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
300 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
301
Bob Wilson5bafff32009-06-22 23:27:02 +0000302 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
303 setTargetDAGCombine(ISD::SHL);
304 setTargetDAGCombine(ISD::SRL);
305 setTargetDAGCombine(ISD::SRA);
306 setTargetDAGCombine(ISD::SIGN_EXTEND);
307 setTargetDAGCombine(ISD::ZERO_EXTEND);
308 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000309 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson5bafff32009-06-22 23:27:02 +0000310 }
311
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000312 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000313
314 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000315 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000316
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000317 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000319
Evan Chenga8e29892007-01-19 07:51:42 +0000320 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000321 if (!Subtarget->isThumb1Only()) {
322 for (unsigned im = (unsigned)ISD::PRE_INC;
323 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setIndexedLoadAction(im, MVT::i1, Legal);
325 setIndexedLoadAction(im, MVT::i8, Legal);
326 setIndexedLoadAction(im, MVT::i16, Legal);
327 setIndexedLoadAction(im, MVT::i32, Legal);
328 setIndexedStoreAction(im, MVT::i1, Legal);
329 setIndexedStoreAction(im, MVT::i8, Legal);
330 setIndexedStoreAction(im, MVT::i16, Legal);
331 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000332 }
Evan Chenga8e29892007-01-19 07:51:42 +0000333 }
334
335 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000336 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 setOperationAction(ISD::MUL, MVT::i64, Expand);
338 setOperationAction(ISD::MULHU, MVT::i32, Expand);
339 setOperationAction(ISD::MULHS, MVT::i32, Expand);
340 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
341 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000342 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 setOperationAction(ISD::MUL, MVT::i64, Expand);
344 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000345 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000346 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000347 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000348 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000349 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000350 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000351 setOperationAction(ISD::SRL, MVT::i64, Custom);
352 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000353
354 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000356 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000357 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000358 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000359 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000360
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000361 // Only ARMv6 has BSWAP.
362 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000363 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000364
Evan Chenga8e29892007-01-19 07:51:42 +0000365 // These are expanded into libcalls.
Jim Grosbach29402132010-05-05 23:44:43 +0000366 if (!Subtarget->hasDivide()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000367 // v7M has a hardware divider
368 setOperationAction(ISD::SDIV, MVT::i32, Expand);
369 setOperationAction(ISD::UDIV, MVT::i32, Expand);
370 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 setOperationAction(ISD::SREM, MVT::i32, Expand);
372 setOperationAction(ISD::UREM, MVT::i32, Expand);
373 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
374 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000375
Owen Anderson825b72b2009-08-11 20:47:22 +0000376 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
377 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
378 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
379 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000380 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000381
Evan Chengfb3611d2010-05-11 07:26:32 +0000382 setOperationAction(ISD::TRAP, MVT::Other, Legal);
383
Evan Chenga8e29892007-01-19 07:51:42 +0000384 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 setOperationAction(ISD::VASTART, MVT::Other, Custom);
386 setOperationAction(ISD::VAARG, MVT::Other, Expand);
387 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
388 setOperationAction(ISD::VAEND, MVT::Other, Expand);
389 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
390 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000391 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
392 // FIXME: Shouldn't need this, since no register is used, but the legalizer
393 // doesn't yet know how to not do that for SjLj.
394 setExceptionSelectorRegister(ARM::R0);
Evan Cheng3a1588a2010-04-15 22:20:34 +0000395 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Jim Grosbach3728e962009-12-10 00:11:09 +0000396 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000397
Jim Grosbach4b77f6a2010-05-07 18:34:55 +0000398 // If the subtarget does not have extract instructions, sign_extend_inreg
399 // needs to be expanded. Extract is available in ARM mode on v6 and up,
400 // and on most Thumb2 implementations.
401 if ((!Subtarget->isThumb() && !Subtarget->hasV6Ops())
402 || (Subtarget->isThumb2() && !Subtarget->hasT2ExtractPack())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000403 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
404 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000405 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000406 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000407
David Goodwinf1daf7d2009-07-08 23:10:31 +0000408 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000409 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
410 // iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000412
413 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000415
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::SETCC, MVT::i32, Expand);
417 setOperationAction(ISD::SETCC, MVT::f32, Expand);
418 setOperationAction(ISD::SETCC, MVT::f64, Expand);
419 setOperationAction(ISD::SELECT, MVT::i32, Expand);
420 setOperationAction(ISD::SELECT, MVT::f32, Expand);
421 setOperationAction(ISD::SELECT, MVT::f64, Expand);
422 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
423 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
424 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000425
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
427 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
428 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
429 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
430 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000431
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000432 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setOperationAction(ISD::FSIN, MVT::f64, Expand);
434 setOperationAction(ISD::FSIN, MVT::f32, Expand);
435 setOperationAction(ISD::FCOS, MVT::f32, Expand);
436 setOperationAction(ISD::FCOS, MVT::f64, Expand);
437 setOperationAction(ISD::FREM, MVT::f64, Expand);
438 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000439 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000440 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
441 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000442 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setOperationAction(ISD::FPOW, MVT::f64, Expand);
444 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000445
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000446 // Various VFP goodness
447 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000448 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
449 if (Subtarget->hasVFP2()) {
450 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
451 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
452 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
453 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
454 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000455 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000456 if (!Subtarget->hasFP16()) {
457 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
458 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000459 }
Evan Cheng110cf482008-04-01 01:50:16 +0000460 }
Evan Chenga8e29892007-01-19 07:51:42 +0000461
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000462 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000463 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000464 setTargetDAGCombine(ISD::ADD);
465 setTargetDAGCombine(ISD::SUB);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000466
Evan Chenga8e29892007-01-19 07:51:42 +0000467 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Chenga8e29892007-01-19 07:51:42 +0000468 setSchedulingPreference(SchedulingForRegPressure);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000469
Evan Chengbc9b7542009-08-15 07:59:10 +0000470 // FIXME: If-converter should use instruction latency to determine
471 // profitability rather than relying on fixed limits.
472 if (Subtarget->getCPUString() == "generic") {
473 // Generic (and overly aggressive) if-conversion limits.
474 setIfCvtBlockSizeLimit(10);
475 setIfCvtDupBlockSizeLimit(2);
Jim Grosbach35075a72010-03-24 16:15:14 +0000476 } else if (Subtarget->hasV7Ops()) {
Jim Grosbachfceabef2010-03-24 00:03:13 +0000477 setIfCvtBlockSizeLimit(3);
478 setIfCvtDupBlockSizeLimit(1);
Evan Chengbc9b7542009-08-15 07:59:10 +0000479 } else if (Subtarget->hasV6Ops()) {
480 setIfCvtBlockSizeLimit(2);
481 setIfCvtDupBlockSizeLimit(1);
482 } else {
483 setIfCvtBlockSizeLimit(3);
484 setIfCvtDupBlockSizeLimit(2);
Evan Cheng8557c2b2009-06-19 01:51:50 +0000485 }
486
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000487 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Bob Wilsone6abdff2009-05-18 20:55:32 +0000488 // Do not enable CodePlacementOpt for now: it currently runs after the
489 // ARMConstantIslandPass and messes up branch relaxation and placement
490 // of constant islands.
491 // benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000492}
493
Evan Chenga8e29892007-01-19 07:51:42 +0000494const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
495 switch (Opcode) {
496 default: return 0;
497 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000498 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
499 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000500 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000501 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
502 case ARMISD::tCALL: return "ARMISD::tCALL";
503 case ARMISD::BRCOND: return "ARMISD::BRCOND";
504 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000505 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000506 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
507 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
508 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000509 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000510 case ARMISD::CMPFP: return "ARMISD::CMPFP";
511 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
512 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
513 case ARMISD::CMOV: return "ARMISD::CMOV";
514 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000515
Jim Grosbach3482c802010-01-18 19:58:49 +0000516 case ARMISD::RBIT: return "ARMISD::RBIT";
517
Bob Wilson76a312b2010-03-19 22:51:32 +0000518 case ARMISD::FTOSI: return "ARMISD::FTOSI";
519 case ARMISD::FTOUI: return "ARMISD::FTOUI";
520 case ARMISD::SITOF: return "ARMISD::SITOF";
521 case ARMISD::UITOF: return "ARMISD::UITOF";
522
Evan Chenga8e29892007-01-19 07:51:42 +0000523 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
524 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
525 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000526
Jim Grosbache5165492009-11-09 00:11:35 +0000527 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
528 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000529
Evan Chengc5942082009-10-28 06:55:03 +0000530 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
531 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
532
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000533 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000534
Evan Cheng86198642009-08-07 00:34:42 +0000535 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
536
Jim Grosbach3728e962009-12-10 00:11:09 +0000537 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
538 case ARMISD::SYNCBARRIER: return "ARMISD::SYNCBARRIER";
539
Bob Wilson5bafff32009-06-22 23:27:02 +0000540 case ARMISD::VCEQ: return "ARMISD::VCEQ";
541 case ARMISD::VCGE: return "ARMISD::VCGE";
542 case ARMISD::VCGEU: return "ARMISD::VCGEU";
543 case ARMISD::VCGT: return "ARMISD::VCGT";
544 case ARMISD::VCGTU: return "ARMISD::VCGTU";
545 case ARMISD::VTST: return "ARMISD::VTST";
546
547 case ARMISD::VSHL: return "ARMISD::VSHL";
548 case ARMISD::VSHRs: return "ARMISD::VSHRs";
549 case ARMISD::VSHRu: return "ARMISD::VSHRu";
550 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
551 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
552 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
553 case ARMISD::VSHRN: return "ARMISD::VSHRN";
554 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
555 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
556 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
557 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
558 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
559 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
560 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
561 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
562 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
563 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
564 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
565 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
566 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
567 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000568 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000569 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000570 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000571 case ARMISD::VREV64: return "ARMISD::VREV64";
572 case ARMISD::VREV32: return "ARMISD::VREV32";
573 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000574 case ARMISD::VZIP: return "ARMISD::VZIP";
575 case ARMISD::VUZP: return "ARMISD::VUZP";
576 case ARMISD::VTRN: return "ARMISD::VTRN";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000577 case ARMISD::FMAX: return "ARMISD::FMAX";
578 case ARMISD::FMIN: return "ARMISD::FMIN";
Evan Chenga8e29892007-01-19 07:51:42 +0000579 }
580}
581
Evan Cheng06b666c2010-05-15 02:18:07 +0000582/// getRegClassFor - Return the register class that should be used for the
583/// specified value type.
584TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
585 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
586 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
587 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000588 if (Subtarget->hasNEON()) {
589 if (VT == MVT::v4i64)
590 return ARM::QQPRRegisterClass;
591 else if (VT == MVT::v8i64)
592 return ARM::QQQQPRRegisterClass;
593 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000594 return TargetLowering::getRegClassFor(VT);
595}
596
Bill Wendlingb4202b82009-07-01 18:50:55 +0000597/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000598unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Evan Cheng048e36f2009-10-02 06:57:25 +0000599 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 0 : 1;
Bill Wendling20c568f2009-06-30 22:38:32 +0000600}
601
Evan Chenga8e29892007-01-19 07:51:42 +0000602//===----------------------------------------------------------------------===//
603// Lowering Code
604//===----------------------------------------------------------------------===//
605
Evan Chenga8e29892007-01-19 07:51:42 +0000606/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
607static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
608 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000609 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000610 case ISD::SETNE: return ARMCC::NE;
611 case ISD::SETEQ: return ARMCC::EQ;
612 case ISD::SETGT: return ARMCC::GT;
613 case ISD::SETGE: return ARMCC::GE;
614 case ISD::SETLT: return ARMCC::LT;
615 case ISD::SETLE: return ARMCC::LE;
616 case ISD::SETUGT: return ARMCC::HI;
617 case ISD::SETUGE: return ARMCC::HS;
618 case ISD::SETULT: return ARMCC::LO;
619 case ISD::SETULE: return ARMCC::LS;
620 }
621}
622
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000623/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
624static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000625 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000626 CondCode2 = ARMCC::AL;
627 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000628 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000629 case ISD::SETEQ:
630 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
631 case ISD::SETGT:
632 case ISD::SETOGT: CondCode = ARMCC::GT; break;
633 case ISD::SETGE:
634 case ISD::SETOGE: CondCode = ARMCC::GE; break;
635 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000636 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000637 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
638 case ISD::SETO: CondCode = ARMCC::VC; break;
639 case ISD::SETUO: CondCode = ARMCC::VS; break;
640 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
641 case ISD::SETUGT: CondCode = ARMCC::HI; break;
642 case ISD::SETUGE: CondCode = ARMCC::PL; break;
643 case ISD::SETLT:
644 case ISD::SETULT: CondCode = ARMCC::LT; break;
645 case ISD::SETLE:
646 case ISD::SETULE: CondCode = ARMCC::LE; break;
647 case ISD::SETNE:
648 case ISD::SETUNE: CondCode = ARMCC::NE; break;
649 }
Evan Chenga8e29892007-01-19 07:51:42 +0000650}
651
Bob Wilson1f595bb2009-04-17 19:07:39 +0000652//===----------------------------------------------------------------------===//
653// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000654//===----------------------------------------------------------------------===//
655
656#include "ARMGenCallingConv.inc"
657
658// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000659static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000660 CCValAssign::LocInfo &LocInfo,
661 CCState &State, bool CanFail) {
662 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
663
664 // Try to get the first register.
665 if (unsigned Reg = State.AllocateReg(RegList, 4))
666 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
667 else {
668 // For the 2nd half of a v2f64, do not fail.
669 if (CanFail)
670 return false;
671
672 // Put the whole thing on the stack.
673 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
674 State.AllocateStack(8, 4),
675 LocVT, LocInfo));
676 return true;
677 }
678
679 // Try to get the second register.
680 if (unsigned Reg = State.AllocateReg(RegList, 4))
681 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
682 else
683 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
684 State.AllocateStack(4, 4),
685 LocVT, LocInfo));
686 return true;
687}
688
Owen Andersone50ed302009-08-10 22:56:29 +0000689static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000690 CCValAssign::LocInfo &LocInfo,
691 ISD::ArgFlagsTy &ArgFlags,
692 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000693 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
694 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000695 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000696 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
697 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000698 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000699}
700
701// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000702static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000703 CCValAssign::LocInfo &LocInfo,
704 CCState &State, bool CanFail) {
705 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
706 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
707
708 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
709 if (Reg == 0) {
710 // For the 2nd half of a v2f64, do not just fail.
711 if (CanFail)
712 return false;
713
714 // Put the whole thing on the stack.
715 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
716 State.AllocateStack(8, 8),
717 LocVT, LocInfo));
718 return true;
719 }
720
721 unsigned i;
722 for (i = 0; i < 2; ++i)
723 if (HiRegList[i] == Reg)
724 break;
725
726 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
727 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
728 LocVT, LocInfo));
729 return true;
730}
731
Owen Andersone50ed302009-08-10 22:56:29 +0000732static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000733 CCValAssign::LocInfo &LocInfo,
734 ISD::ArgFlagsTy &ArgFlags,
735 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000736 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
737 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000738 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000739 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
740 return false;
741 return true; // we handled it
742}
743
Owen Andersone50ed302009-08-10 22:56:29 +0000744static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000745 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000746 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
747 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
748
Bob Wilsone65586b2009-04-17 20:40:45 +0000749 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
750 if (Reg == 0)
751 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000752
Bob Wilsone65586b2009-04-17 20:40:45 +0000753 unsigned i;
754 for (i = 0; i < 2; ++i)
755 if (HiRegList[i] == Reg)
756 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000757
Bob Wilson5bafff32009-06-22 23:27:02 +0000758 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000759 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000760 LocVT, LocInfo));
761 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000762}
763
Owen Andersone50ed302009-08-10 22:56:29 +0000764static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000765 CCValAssign::LocInfo &LocInfo,
766 ISD::ArgFlagsTy &ArgFlags,
767 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000768 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
769 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000770 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000771 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000772 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000773}
774
Owen Andersone50ed302009-08-10 22:56:29 +0000775static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000776 CCValAssign::LocInfo &LocInfo,
777 ISD::ArgFlagsTy &ArgFlags,
778 CCState &State) {
779 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
780 State);
781}
782
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000783/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
784/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000785CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000786 bool Return,
787 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000788 switch (CC) {
789 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000790 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000791 case CallingConv::C:
792 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000793 // Use target triple & subtarget features to do actual dispatch.
794 if (Subtarget->isAAPCS_ABI()) {
795 if (Subtarget->hasVFP2() &&
796 FloatABIType == FloatABI::Hard && !isVarArg)
797 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
798 else
799 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
800 } else
801 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000802 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000803 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000804 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000805 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000806 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000807 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000808 }
809}
810
Dan Gohman98ca4f22009-08-05 01:29:28 +0000811/// LowerCallResult - Lower the result values of a call into the
812/// appropriate copies out of appropriate physical registers.
813SDValue
814ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000815 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000816 const SmallVectorImpl<ISD::InputArg> &Ins,
817 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000818 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000819
Bob Wilson1f595bb2009-04-17 19:07:39 +0000820 // Assign locations to each value returned by this call.
821 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000822 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000823 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000824 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000825 CCAssignFnForNode(CallConv, /* Return*/ true,
826 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000827
828 // Copy all of the result registers out of their specified physreg.
829 for (unsigned i = 0; i != RVLocs.size(); ++i) {
830 CCValAssign VA = RVLocs[i];
831
Bob Wilson80915242009-04-25 00:33:20 +0000832 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000833 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000834 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000835 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000836 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000837 Chain = Lo.getValue(1);
838 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000839 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000840 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000841 InFlag);
842 Chain = Hi.getValue(1);
843 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000844 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000845
Owen Anderson825b72b2009-08-11 20:47:22 +0000846 if (VA.getLocVT() == MVT::v2f64) {
847 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
848 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
849 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000850
851 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000852 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000853 Chain = Lo.getValue(1);
854 InFlag = Lo.getValue(2);
855 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000856 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000857 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);
Owen Anderson825b72b2009-08-11 20:47:22 +0000860 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
861 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000862 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000863 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000864 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
865 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000866 Chain = Val.getValue(1);
867 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000868 }
Bob Wilson80915242009-04-25 00:33:20 +0000869
870 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000871 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000872 case CCValAssign::Full: break;
873 case CCValAssign::BCvt:
874 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
875 break;
876 }
877
Dan Gohman98ca4f22009-08-05 01:29:28 +0000878 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000879 }
880
Dan Gohman98ca4f22009-08-05 01:29:28 +0000881 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000882}
883
884/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
885/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000886/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000887/// a byval function parameter.
888/// Sometimes what we are copying is the end of a larger object, the part that
889/// does not fit in registers.
890static SDValue
891CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
892 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
893 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000894 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000895 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +0000896 /*isVolatile=*/false, /*AlwaysInline=*/false,
897 NULL, 0, NULL, 0);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000898}
899
Bob Wilsondee46d72009-04-17 20:35:10 +0000900/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000901SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000902ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
903 SDValue StackPtr, SDValue Arg,
904 DebugLoc dl, SelectionDAG &DAG,
905 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +0000906 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000907 unsigned LocMemOffset = VA.getLocMemOffset();
908 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
909 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
910 if (Flags.isByVal()) {
911 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
912 }
913 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene1b58cab2010-02-15 16:55:24 +0000914 PseudoSourceValue::getStack(), LocMemOffset,
915 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +0000916}
917
Dan Gohman98ca4f22009-08-05 01:29:28 +0000918void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000919 SDValue Chain, SDValue &Arg,
920 RegsToPassVector &RegsToPass,
921 CCValAssign &VA, CCValAssign &NextVA,
922 SDValue &StackPtr,
923 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +0000924 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +0000925
Jim Grosbache5165492009-11-09 00:11:35 +0000926 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000927 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000928 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
929
930 if (NextVA.isRegLoc())
931 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
932 else {
933 assert(NextVA.isMemLoc());
934 if (StackPtr.getNode() == 0)
935 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
936
Dan Gohman98ca4f22009-08-05 01:29:28 +0000937 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
938 dl, DAG, NextVA,
939 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000940 }
941}
942
Dan Gohman98ca4f22009-08-05 01:29:28 +0000943/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000944/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
945/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000946SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000947ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000948 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000949 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000950 const SmallVectorImpl<ISD::OutputArg> &Outs,
951 const SmallVectorImpl<ISD::InputArg> &Ins,
952 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000953 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +0000954 // ARM target does not yet support tail call optimization.
955 isTailCall = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000956
Bob Wilson1f595bb2009-04-17 19:07:39 +0000957 // Analyze operands of the call, assigning locations to each operand.
958 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000959 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
960 *DAG.getContext());
961 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000962 CCAssignFnForNode(CallConv, /* Return*/ false,
963 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +0000964
Bob Wilson1f595bb2009-04-17 19:07:39 +0000965 // Get a count of how many bytes are to be pushed on the stack.
966 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000967
968 // Adjust the stack pointer for the new arguments...
969 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000970 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000971
Jim Grosbachf9a4b762010-02-24 01:43:03 +0000972 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000973
Bob Wilson5bafff32009-06-22 23:27:02 +0000974 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000975 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000976
Bob Wilson1f595bb2009-04-17 19:07:39 +0000977 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +0000978 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000979 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
980 i != e;
981 ++i, ++realArgIdx) {
982 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000983 SDValue Arg = Outs[realArgIdx].Val;
984 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +0000985
Bob Wilson1f595bb2009-04-17 19:07:39 +0000986 // Promote the value if needed.
987 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000988 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +0000989 case CCValAssign::Full: break;
990 case CCValAssign::SExt:
991 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
992 break;
993 case CCValAssign::ZExt:
994 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
995 break;
996 case CCValAssign::AExt:
997 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
998 break;
999 case CCValAssign::BCvt:
1000 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1001 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001002 }
1003
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001004 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001005 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001006 if (VA.getLocVT() == MVT::v2f64) {
1007 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1008 DAG.getConstant(0, MVT::i32));
1009 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1010 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001011
Dan Gohman98ca4f22009-08-05 01:29:28 +00001012 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001013 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1014
1015 VA = ArgLocs[++i]; // skip ahead to next loc
1016 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001017 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001018 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1019 } else {
1020 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001021
Dan Gohman98ca4f22009-08-05 01:29:28 +00001022 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1023 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001024 }
1025 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001026 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001027 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001028 }
1029 } else if (VA.isRegLoc()) {
1030 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1031 } else {
1032 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001033
Dan Gohman98ca4f22009-08-05 01:29:28 +00001034 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1035 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001036 }
Evan Chenga8e29892007-01-19 07:51:42 +00001037 }
1038
1039 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001040 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001041 &MemOpChains[0], MemOpChains.size());
1042
1043 // Build a sequence of copy-to-reg nodes chained together with token chain
1044 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001045 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +00001046 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001047 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001048 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +00001049 InFlag = Chain.getValue(1);
1050 }
1051
Bill Wendling056292f2008-09-16 21:48:12 +00001052 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1053 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1054 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001055 bool isDirect = false;
1056 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001057 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001058 MachineFunction &MF = DAG.getMachineFunction();
1059 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001060
1061 if (EnableARMLongCalls) {
1062 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1063 && "long-calls with non-static relocation model!");
1064 // Handle a global address or an external symbol. If it's not one of
1065 // those, the target's already in a register, so we don't need to do
1066 // anything extra.
1067 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001068 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001069 // Create a constant pool entry for the callee address
1070 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1071 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1072 ARMPCLabelIndex,
1073 ARMCP::CPValue, 0);
1074 // Get the address of the callee into a register
1075 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1076 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1077 Callee = DAG.getLoad(getPointerTy(), dl,
1078 DAG.getEntryNode(), CPAddr,
1079 PseudoSourceValue::getConstantPool(), 0,
1080 false, false, 0);
1081 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1082 const char *Sym = S->getSymbol();
1083
1084 // Create a constant pool entry for the callee address
1085 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1086 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1087 Sym, ARMPCLabelIndex, 0);
1088 // Get the address of the callee into a register
1089 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1090 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1091 Callee = DAG.getLoad(getPointerTy(), dl,
1092 DAG.getEntryNode(), CPAddr,
1093 PseudoSourceValue::getConstantPool(), 0,
1094 false, false, 0);
1095 }
1096 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001097 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001098 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001099 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001100 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001101 getTargetMachine().getRelocationModel() != Reloc::Static;
1102 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001103 // ARM call to a local ARM function is predicable.
1104 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +00001105 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001106 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001107 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001108 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001109 ARMPCLabelIndex,
1110 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001111 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001112 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001113 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001114 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001115 PseudoSourceValue::getConstantPool(), 0,
1116 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001117 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001118 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001119 getPointerTy(), Callee, PICLabel);
Jim Grosbache7b52522010-04-14 22:28:31 +00001120 } else
Evan Chengc60e76d2007-01-30 20:37:08 +00001121 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001122 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001123 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001124 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001125 getTargetMachine().getRelocationModel() != Reloc::Static;
1126 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001127 // tBX takes a register source operand.
1128 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001129 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001130 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001131 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001132 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001133 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001134 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001135 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001136 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001137 PseudoSourceValue::getConstantPool(), 0,
1138 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001139 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001140 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001141 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001142 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001143 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001144 }
1145
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001146 // FIXME: handle tail calls differently.
1147 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001148 if (Subtarget->isThumb()) {
1149 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001150 CallOpc = ARMISD::CALL_NOLINK;
1151 else
1152 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1153 } else {
1154 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001155 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1156 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001157 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001158 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001159 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001160 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001161 InFlag = Chain.getValue(1);
1162 }
1163
Dan Gohman475871a2008-07-27 21:46:04 +00001164 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001165 Ops.push_back(Chain);
1166 Ops.push_back(Callee);
1167
1168 // Add argument registers to the end of the list so that they are known live
1169 // into the call.
1170 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1171 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1172 RegsToPass[i].second.getValueType()));
1173
Gabor Greifba36cb52008-08-28 21:40:38 +00001174 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001175 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +00001176 // Returns a chain and a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00001177 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +00001178 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001179 InFlag = Chain.getValue(1);
1180
Chris Lattnere563bbc2008-10-11 22:08:30 +00001181 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1182 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001183 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001184 InFlag = Chain.getValue(1);
1185
Bob Wilson1f595bb2009-04-17 19:07:39 +00001186 // Handle result values, copying them out of physregs into vregs that we
1187 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001188 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1189 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001190}
1191
Dan Gohman98ca4f22009-08-05 01:29:28 +00001192SDValue
1193ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001194 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001195 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmand858e902010-04-17 15:26:15 +00001196 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001197
Bob Wilsondee46d72009-04-17 20:35:10 +00001198 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001199 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001200
Bob Wilsondee46d72009-04-17 20:35:10 +00001201 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001202 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1203 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001204
Dan Gohman98ca4f22009-08-05 01:29:28 +00001205 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001206 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1207 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001208
1209 // If this is the first return lowered for this function, add
1210 // the regs to the liveout set for the function.
1211 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1212 for (unsigned i = 0; i != RVLocs.size(); ++i)
1213 if (RVLocs[i].isRegLoc())
1214 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001215 }
1216
Bob Wilson1f595bb2009-04-17 19:07:39 +00001217 SDValue Flag;
1218
1219 // Copy the result values into the output registers.
1220 for (unsigned i = 0, realRVLocIdx = 0;
1221 i != RVLocs.size();
1222 ++i, ++realRVLocIdx) {
1223 CCValAssign &VA = RVLocs[i];
1224 assert(VA.isRegLoc() && "Can only return in registers!");
1225
Dan Gohman98ca4f22009-08-05 01:29:28 +00001226 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001227
1228 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001229 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001230 case CCValAssign::Full: break;
1231 case CCValAssign::BCvt:
1232 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1233 break;
1234 }
1235
Bob Wilson1f595bb2009-04-17 19:07:39 +00001236 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001237 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001238 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001239 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1240 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001241 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001242 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001243
1244 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1245 Flag = Chain.getValue(1);
1246 VA = RVLocs[++i]; // skip ahead to next loc
1247 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1248 HalfGPRs.getValue(1), Flag);
1249 Flag = Chain.getValue(1);
1250 VA = RVLocs[++i]; // skip ahead to next loc
1251
1252 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001253 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1254 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001255 }
1256 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1257 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001258 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001259 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001260 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001261 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001262 VA = RVLocs[++i]; // skip ahead to next loc
1263 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1264 Flag);
1265 } else
1266 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1267
Bob Wilsondee46d72009-04-17 20:35:10 +00001268 // Guarantee that all emitted copies are
1269 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001270 Flag = Chain.getValue(1);
1271 }
1272
1273 SDValue result;
1274 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001275 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001276 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001277 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001278
1279 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001280}
1281
Bob Wilsonb62d2572009-11-03 00:02:05 +00001282// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1283// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1284// one of the above mentioned nodes. It has to be wrapped because otherwise
1285// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1286// be used to form addressing mode. These wrapped nodes will be selected
1287// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001288static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001289 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001290 // FIXME there is no actual debug info here
1291 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001292 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001293 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001294 if (CP->isMachineConstantPoolEntry())
1295 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1296 CP->getAlignment());
1297 else
1298 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1299 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001300 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001301}
1302
Dan Gohmand858e902010-04-17 15:26:15 +00001303SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1304 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001305 MachineFunction &MF = DAG.getMachineFunction();
1306 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1307 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001308 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001309 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001310 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001311 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1312 SDValue CPAddr;
1313 if (RelocM == Reloc::Static) {
1314 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1315 } else {
1316 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001317 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001318 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1319 ARMCP::CPBlockAddress,
1320 PCAdj);
1321 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1322 }
1323 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1324 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001325 PseudoSourceValue::getConstantPool(), 0,
1326 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001327 if (RelocM == Reloc::Static)
1328 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001329 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001330 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001331}
1332
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001333// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001334SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001335ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001336 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001337 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001338 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001339 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001340 MachineFunction &MF = DAG.getMachineFunction();
1341 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1342 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001343 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001344 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001345 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001346 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001347 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001348 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
David Greene1b58cab2010-02-15 16:55:24 +00001349 PseudoSourceValue::getConstantPool(), 0,
1350 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001351 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001352
Evan Chenge7e0d622009-11-06 22:24:13 +00001353 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001354 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001355
1356 // call __tls_get_addr.
1357 ArgListTy Args;
1358 ArgListEntry Entry;
1359 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001360 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001361 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001362 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001363 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001364 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1365 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001366 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001367 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001368 return CallResult.first;
1369}
1370
1371// Lower ISD::GlobalTLSAddress using the "initial exec" or
1372// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001373SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001374ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001375 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001376 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001377 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001378 SDValue Offset;
1379 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001380 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001381 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001382 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001383
Chris Lattner4fb63d02009-07-15 04:12:33 +00001384 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001385 MachineFunction &MF = DAG.getMachineFunction();
1386 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1387 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1388 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001389 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1390 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001391 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001392 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001393 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001394 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001395 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001396 PseudoSourceValue::getConstantPool(), 0,
1397 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001398 Chain = Offset.getValue(1);
1399
Evan Chenge7e0d622009-11-06 22:24:13 +00001400 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001401 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001402
Evan Cheng9eda6892009-10-31 03:39:36 +00001403 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001404 PseudoSourceValue::getConstantPool(), 0,
1405 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001406 } else {
1407 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001408 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001409 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001410 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001411 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001412 PseudoSourceValue::getConstantPool(), 0,
1413 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001414 }
1415
1416 // The address of the thread local variable is the add of the thread
1417 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001418 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001419}
1420
Dan Gohman475871a2008-07-27 21:46:04 +00001421SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001422ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001423 // TODO: implement the "local dynamic" model
1424 assert(Subtarget->isTargetELF() &&
1425 "TLS not implemented for non-ELF targets");
1426 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1427 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1428 // otherwise use the "Local Exec" TLS Model
1429 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1430 return LowerToTLSGeneralDynamicModel(GA, DAG);
1431 else
1432 return LowerToTLSExecModels(GA, DAG);
1433}
1434
Dan Gohman475871a2008-07-27 21:46:04 +00001435SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001436 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001437 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001438 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001439 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001440 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1441 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001442 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001443 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001444 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001445 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001446 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001447 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001448 CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001449 PseudoSourceValue::getConstantPool(), 0,
1450 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001451 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001452 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001453 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001454 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001455 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001456 PseudoSourceValue::getGOT(), 0,
1457 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001458 return Result;
1459 } else {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001460 // If we have T2 ops, we can materialize the address directly via movt/movw
1461 // pair. This is always cheaper.
1462 if (Subtarget->useMovt()) {
1463 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
1464 DAG.getTargetGlobalAddress(GV, PtrVT));
1465 } else {
1466 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1467 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1468 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001469 PseudoSourceValue::getConstantPool(), 0,
1470 false, false, 0);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001471 }
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001472 }
1473}
1474
Dan Gohman475871a2008-07-27 21:46:04 +00001475SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001476 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001477 MachineFunction &MF = DAG.getMachineFunction();
1478 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1479 unsigned ARMPCLabelIndex = 0;
Owen Andersone50ed302009-08-10 22:56:29 +00001480 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001481 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001482 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001483 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001484 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001485 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001486 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001487 else {
Evan Chenge7e0d622009-11-06 22:24:13 +00001488 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001489 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1490 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001491 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001492 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001493 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001494 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001495
Evan Cheng9eda6892009-10-31 03:39:36 +00001496 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001497 PseudoSourceValue::getConstantPool(), 0,
1498 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001499 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001500
1501 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001502 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001503 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001504 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001505
Evan Cheng63476a82009-09-03 07:04:02 +00001506 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Evan Cheng9eda6892009-10-31 03:39:36 +00001507 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001508 PseudoSourceValue::getGOT(), 0,
1509 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001510
1511 return Result;
1512}
1513
Dan Gohman475871a2008-07-27 21:46:04 +00001514SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001515 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001516 assert(Subtarget->isTargetELF() &&
1517 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00001518 MachineFunction &MF = DAG.getMachineFunction();
1519 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1520 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Andersone50ed302009-08-10 22:56:29 +00001521 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001522 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001523 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001524 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1525 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001526 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001527 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001528 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001529 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001530 PseudoSourceValue::getConstantPool(), 0,
1531 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001532 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001533 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001534}
1535
Jim Grosbach0e0da732009-05-12 23:59:14 +00001536SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00001537ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001538 const ARMSubtarget *Subtarget)
1539 const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001540 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001541 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001542 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001543 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001544 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001545 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001546 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1547 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001548 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001549 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00001550 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1551 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001552 EVT PtrVT = getPointerTy();
1553 DebugLoc dl = Op.getDebugLoc();
1554 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1555 SDValue CPAddr;
1556 unsigned PCAdj = (RelocM != Reloc::PIC_)
1557 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001558 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001559 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1560 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001561 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001562 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001563 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00001564 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001565 PseudoSourceValue::getConstantPool(), 0,
1566 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001567 SDValue Chain = Result.getValue(1);
1568
1569 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001570 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001571 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1572 }
1573 return Result;
1574 }
Jim Grosbachf9570122009-05-14 00:46:35 +00001575 case Intrinsic::eh_sjlj_setjmp:
Jim Grosbacha87ded22010-02-08 23:22:00 +00001576 SDValue Val = Subtarget->isThumb() ?
1577 DAG.getCopyFromReg(DAG.getEntryNode(), dl, ARM::SP, MVT::i32) :
1578 DAG.getConstant(0, MVT::i32);
1579 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1),
1580 Val);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001581 }
1582}
1583
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001584static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
1585 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00001586 DebugLoc dl = Op.getDebugLoc();
1587 SDValue Op5 = Op.getOperand(5);
1588 SDValue Res;
1589 unsigned isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue();
1590 if (isDeviceBarrier) {
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001591 if (Subtarget->hasV7Ops())
1592 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0));
1593 else
1594 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0),
1595 DAG.getConstant(0, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00001596 } else {
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001597 if (Subtarget->hasV7Ops())
1598 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
1599 else
1600 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
1601 DAG.getConstant(0, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00001602 }
1603 return Res;
1604}
1605
Dan Gohman1e93df62010-04-17 14:41:14 +00001606static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
1607 MachineFunction &MF = DAG.getMachineFunction();
1608 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
1609
Evan Chenga8e29892007-01-19 07:51:42 +00001610 // vastart just stores the address of the VarArgsFrameIndex slot into the
1611 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001612 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001613 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00001614 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001615 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
David Greene1b58cab2010-02-15 16:55:24 +00001616 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
1617 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001618}
1619
Dan Gohman475871a2008-07-27 21:46:04 +00001620SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001621ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1622 SelectionDAG &DAG) const {
Evan Cheng86198642009-08-07 00:34:42 +00001623 SDNode *Node = Op.getNode();
1624 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001625 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001626 SDValue Chain = Op.getOperand(0);
1627 SDValue Size = Op.getOperand(1);
1628 SDValue Align = Op.getOperand(2);
1629
1630 // Chain the dynamic stack allocation so that it doesn't modify the stack
1631 // pointer when other instructions are using the stack.
1632 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1633
1634 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1635 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1636 if (AlignVal > StackAlign)
1637 // Do this now since selection pass cannot introduce new target
1638 // independent node.
1639 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1640
1641 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1642 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1643 // do even more horrible hack later.
1644 MachineFunction &MF = DAG.getMachineFunction();
1645 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1646 if (AFI->isThumb1OnlyFunction()) {
1647 bool Negate = true;
1648 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1649 if (C) {
1650 uint32_t Val = C->getZExtValue();
1651 if (Val <= 508 && ((Val & 3) == 0))
1652 Negate = false;
1653 }
1654 if (Negate)
1655 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1656 }
1657
Owen Anderson825b72b2009-08-11 20:47:22 +00001658 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001659 SDValue Ops1[] = { Chain, Size, Align };
1660 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1661 Chain = Res.getValue(1);
1662 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1663 DAG.getIntPtrConstant(0, true), SDValue());
1664 SDValue Ops2[] = { Res, Chain };
1665 return DAG.getMergeValues(Ops2, 2, dl);
1666}
1667
1668SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001669ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1670 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001671 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001672 MachineFunction &MF = DAG.getMachineFunction();
1673 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1674
1675 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001676 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001677 RC = ARM::tGPRRegisterClass;
1678 else
1679 RC = ARM::GPRRegisterClass;
1680
1681 // Transform the arguments stored in physical registers into virtual ones.
1682 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001683 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001684
1685 SDValue ArgValue2;
1686 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001687 MachineFrameInfo *MFI = MF.getFrameInfo();
Bob Wilson6a234f02010-04-13 22:03:22 +00001688 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true, false);
Bob Wilson5bafff32009-06-22 23:27:02 +00001689
1690 // Create load node to retrieve arguments from the stack.
1691 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001692 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00001693 PseudoSourceValue::getFixedStack(FI), 0,
1694 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001695 } else {
1696 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001697 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001698 }
1699
Jim Grosbache5165492009-11-09 00:11:35 +00001700 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001701}
1702
1703SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001704ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001705 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001706 const SmallVectorImpl<ISD::InputArg>
1707 &Ins,
1708 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001709 SmallVectorImpl<SDValue> &InVals)
1710 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001711
Bob Wilson1f595bb2009-04-17 19:07:39 +00001712 MachineFunction &MF = DAG.getMachineFunction();
1713 MachineFrameInfo *MFI = MF.getFrameInfo();
1714
Bob Wilson1f595bb2009-04-17 19:07:39 +00001715 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1716
1717 // Assign locations to all of the incoming arguments.
1718 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001719 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1720 *DAG.getContext());
1721 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001722 CCAssignFnForNode(CallConv, /* Return*/ false,
1723 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001724
1725 SmallVector<SDValue, 16> ArgValues;
1726
1727 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1728 CCValAssign &VA = ArgLocs[i];
1729
Bob Wilsondee46d72009-04-17 20:35:10 +00001730 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001731 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001732 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00001733
Bob Wilson5bafff32009-06-22 23:27:02 +00001734 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001735 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001736 // f64 and vector types are split up into multiple registers or
1737 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00001738 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001739 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001740 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00001741 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00001742 SDValue ArgValue2;
1743 if (VA.isMemLoc()) {
1744 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(),
1745 true, false);
1746 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1747 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
1748 PseudoSourceValue::getFixedStack(FI), 0,
1749 false, false, 0);
1750 } else {
1751 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
1752 Chain, DAG, dl);
1753 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001754 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1755 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001756 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00001757 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001758 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1759 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001760 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001761
Bob Wilson5bafff32009-06-22 23:27:02 +00001762 } else {
1763 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001764
Owen Anderson825b72b2009-08-11 20:47:22 +00001765 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00001766 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001767 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00001768 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001769 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001770 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001771 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001772 RC = (AFI->isThumb1OnlyFunction() ?
1773 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00001774 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001775 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00001776
1777 // Transform the arguments in physical registers into virtual ones.
1778 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001779 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001780 }
1781
1782 // If this is an 8 or 16-bit value, it is really passed promoted
1783 // to 32 bits. Insert an assert[sz]ext to capture this, then
1784 // truncate to the right size.
1785 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001786 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001787 case CCValAssign::Full: break;
1788 case CCValAssign::BCvt:
1789 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1790 break;
1791 case CCValAssign::SExt:
1792 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1793 DAG.getValueType(VA.getValVT()));
1794 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1795 break;
1796 case CCValAssign::ZExt:
1797 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1798 DAG.getValueType(VA.getValVT()));
1799 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1800 break;
1801 }
1802
Dan Gohman98ca4f22009-08-05 01:29:28 +00001803 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001804
1805 } else { // VA.isRegLoc()
1806
1807 // sanity check
1808 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00001809 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001810
1811 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001812 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
1813 true, false);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001814
Bob Wilsondee46d72009-04-17 20:35:10 +00001815 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001816 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001817 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00001818 PseudoSourceValue::getFixedStack(FI), 0,
1819 false, false, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001820 }
1821 }
1822
1823 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001824 if (isVarArg) {
1825 static const unsigned GPRArgRegs[] = {
1826 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1827 };
1828
Bob Wilsondee46d72009-04-17 20:35:10 +00001829 unsigned NumGPRs = CCInfo.getFirstUnallocated
1830 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001831
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001832 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1833 unsigned VARegSize = (4 - NumGPRs) * 4;
1834 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00001835 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001836 if (VARegSaveSize) {
1837 // If this function is vararg, store any remaining integer argument regs
1838 // to their spots on the stack so that they may be loaded by deferencing
1839 // the result of va_next.
1840 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00001841 AFI->setVarArgsFrameIndex(
1842 MFI->CreateFixedObject(VARegSaveSize,
1843 ArgOffset + VARegSaveSize - VARegSize,
1844 true, false));
1845 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
1846 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001847
Dan Gohman475871a2008-07-27 21:46:04 +00001848 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001849 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001850 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001851 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001852 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001853 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001854 RC = ARM::GPRRegisterClass;
1855
Bob Wilson998e1252009-04-20 18:36:57 +00001856 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001857 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00001858 SDValue Store =
1859 DAG.getStore(Val.getValue(1), dl, Val, FIN,
1860 PseudoSourceValue::getFixedStack(AFI->getVarArgsFrameIndex()), 0,
1861 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001862 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001863 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001864 DAG.getConstant(4, getPointerTy()));
1865 }
1866 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001867 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001868 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001869 } else
1870 // This will point to the next argument passed via stack.
Dan Gohman1e93df62010-04-17 14:41:14 +00001871 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset,
1872 true, false));
Evan Chenga8e29892007-01-19 07:51:42 +00001873 }
1874
Dan Gohman98ca4f22009-08-05 01:29:28 +00001875 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00001876}
1877
1878/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001879static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001880 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001881 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001882 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001883 // Maybe this has already been legalized into the constant pool?
1884 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001885 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001886 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00001887 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001888 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001889 }
1890 }
1891 return false;
1892}
1893
Evan Chenga8e29892007-01-19 07:51:42 +00001894/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1895/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00001896SDValue
1897ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Dan Gohmand858e902010-04-17 15:26:15 +00001898 SDValue &ARMCC, SelectionDAG &DAG,
1899 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00001900 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001901 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00001902 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001903 // Constant does not fit, try adjusting it by one?
1904 switch (CC) {
1905 default: break;
1906 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001907 case ISD::SETGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00001908 if (isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001909 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001910 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001911 }
1912 break;
1913 case ISD::SETULT:
1914 case ISD::SETUGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00001915 if (C > 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001916 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001917 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001918 }
1919 break;
1920 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001921 case ISD::SETGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00001922 if (isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001923 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001924 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001925 }
1926 break;
1927 case ISD::SETULE:
1928 case ISD::SETUGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00001929 if (C < 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001930 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001931 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001932 }
1933 break;
1934 }
1935 }
1936 }
1937
1938 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001939 ARMISD::NodeType CompareType;
1940 switch (CondCode) {
1941 default:
1942 CompareType = ARMISD::CMP;
1943 break;
1944 case ARMCC::EQ:
1945 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00001946 // Uses only Z Flag
1947 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001948 break;
1949 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001950 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1951 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001952}
1953
1954/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001955static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001956 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001957 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001958 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00001959 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001960 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001961 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1962 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001963}
1964
Dan Gohmand858e902010-04-17 15:26:15 +00001965SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001966 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001967 SDValue LHS = Op.getOperand(0);
1968 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001969 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001970 SDValue TrueVal = Op.getOperand(2);
1971 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001972 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001973
Owen Anderson825b72b2009-08-11 20:47:22 +00001974 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001975 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001976 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00001977 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Dale Johannesende064702009-02-06 21:50:26 +00001978 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001979 }
1980
1981 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001982 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00001983
Owen Anderson825b72b2009-08-11 20:47:22 +00001984 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1985 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001986 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1987 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001988 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001989 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001990 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001991 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00001992 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001993 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00001994 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001995 }
1996 return Result;
1997}
1998
Dan Gohmand858e902010-04-17 15:26:15 +00001999SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002000 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002001 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002002 SDValue LHS = Op.getOperand(2);
2003 SDValue RHS = Op.getOperand(3);
2004 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002005 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002006
Owen Anderson825b72b2009-08-11 20:47:22 +00002007 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00002008 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002009 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00002010 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002011 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00002012 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002013 }
2014
Owen Anderson825b72b2009-08-11 20:47:22 +00002015 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00002016 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002017 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002018
Dale Johannesende064702009-02-06 21:50:26 +00002019 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002020 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
2021 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2022 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002023 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002024 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002025 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002026 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00002027 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002028 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002029 }
2030 return Res;
2031}
2032
Dan Gohmand858e902010-04-17 15:26:15 +00002033SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002034 SDValue Chain = Op.getOperand(0);
2035 SDValue Table = Op.getOperand(1);
2036 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002037 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002038
Owen Andersone50ed302009-08-10 22:56:29 +00002039 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002040 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2041 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002042 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002043 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002044 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002045 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2046 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002047 if (Subtarget->isThumb2()) {
2048 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2049 // which does another jump to the destination. This also makes it easier
2050 // to translate it to TBB / TBH later.
2051 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002052 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002053 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002054 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002055 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002056 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002057 PseudoSourceValue::getJumpTable(), 0,
2058 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002059 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002060 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002061 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002062 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002063 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002064 PseudoSourceValue::getJumpTable(), 0, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002065 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002066 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002067 }
Evan Chenga8e29892007-01-19 07:51:42 +00002068}
2069
Bob Wilson76a312b2010-03-19 22:51:32 +00002070static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2071 DebugLoc dl = Op.getDebugLoc();
2072 unsigned Opc;
2073
2074 switch (Op.getOpcode()) {
2075 default:
2076 assert(0 && "Invalid opcode!");
2077 case ISD::FP_TO_SINT:
2078 Opc = ARMISD::FTOSI;
2079 break;
2080 case ISD::FP_TO_UINT:
2081 Opc = ARMISD::FTOUI;
2082 break;
2083 }
2084 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
2085 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
2086}
2087
2088static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2089 EVT VT = Op.getValueType();
2090 DebugLoc dl = Op.getDebugLoc();
2091 unsigned Opc;
2092
2093 switch (Op.getOpcode()) {
2094 default:
2095 assert(0 && "Invalid opcode!");
2096 case ISD::SINT_TO_FP:
2097 Opc = ARMISD::SITOF;
2098 break;
2099 case ISD::UINT_TO_FP:
2100 Opc = ARMISD::UITOF;
2101 break;
2102 }
2103
2104 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
2105 return DAG.getNode(Opc, dl, VT, Op);
2106}
2107
Dan Gohman475871a2008-07-27 21:46:04 +00002108static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002109 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002110 SDValue Tmp0 = Op.getOperand(0);
2111 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002112 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002113 EVT VT = Op.getValueType();
2114 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00002115 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
2116 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002117 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
2118 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002119 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002120}
2121
Dan Gohmand858e902010-04-17 15:26:15 +00002122SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002123 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2124 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00002125 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002126 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2127 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002128 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002129 ? ARM::R7 : ARM::R11;
2130 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2131 while (Depth--)
David Greene1b58cab2010-02-15 16:55:24 +00002132 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
2133 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002134 return FrameAddr;
2135}
2136
Bob Wilson9f3f0612010-04-17 05:30:19 +00002137/// ExpandBIT_CONVERT - If the target supports VFP, this function is called to
2138/// expand a bit convert where either the source or destination type is i64 to
2139/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2140/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2141/// vectors), since the legalizer won't know what to do with that.
Duncan Sands1607f052008-12-01 11:39:25 +00002142static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002143 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2144 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002145 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002146
Bob Wilson9f3f0612010-04-17 05:30:19 +00002147 // This function is only supposed to be called for i64 types, either as the
2148 // source or destination of the bit convert.
2149 EVT SrcVT = Op.getValueType();
2150 EVT DstVT = N->getValueType(0);
2151 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
2152 "ExpandBIT_CONVERT called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002153
Bob Wilson9f3f0612010-04-17 05:30:19 +00002154 // Turn i64->f64 into VMOVDRR.
2155 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002156 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2157 DAG.getConstant(0, MVT::i32));
2158 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2159 DAG.getConstant(1, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00002160 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002161 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002162
Jim Grosbache5165492009-11-09 00:11:35 +00002163 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002164 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2165 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2166 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2167 // Merge the pieces into a single i64 value.
2168 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2169 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002170
Bob Wilson9f3f0612010-04-17 05:30:19 +00002171 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002172}
2173
Bob Wilson5bafff32009-06-22 23:27:02 +00002174/// getZeroVector - Returns a vector of specified type with all zero elements.
2175///
Owen Andersone50ed302009-08-10 22:56:29 +00002176static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002177 assert(VT.isVector() && "Expected a vector type");
2178
2179 // Zero vectors are used to represent vector negation and in those cases
2180 // will be implemented with the NEON VNEG instruction. However, VNEG does
2181 // not support i64 elements, so sometimes the zero vectors will need to be
2182 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002183 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilson5bafff32009-06-22 23:27:02 +00002184 // to their dest type. This ensures they get CSE'd.
2185 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002186 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2187 SmallVector<SDValue, 8> Ops;
2188 MVT TVT;
2189
2190 if (VT.getSizeInBits() == 64) {
2191 Ops.assign(8, Cst); TVT = MVT::v8i8;
2192 } else {
2193 Ops.assign(16, Cst); TVT = MVT::v16i8;
2194 }
2195 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002196
2197 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2198}
2199
2200/// getOnesVector - Returns a vector of specified type with all bits set.
2201///
Owen Andersone50ed302009-08-10 22:56:29 +00002202static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002203 assert(VT.isVector() && "Expected a vector type");
2204
Bob Wilson929ffa22009-10-30 20:13:25 +00002205 // Always build ones vectors as <16 x i8> or <8 x i8> bitcasted to their
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002206 // dest type. This ensures they get CSE'd.
Bob Wilson5bafff32009-06-22 23:27:02 +00002207 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002208 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2209 SmallVector<SDValue, 8> Ops;
2210 MVT TVT;
2211
2212 if (VT.getSizeInBits() == 64) {
2213 Ops.assign(8, Cst); TVT = MVT::v8i8;
2214 } else {
2215 Ops.assign(16, Cst); TVT = MVT::v16i8;
2216 }
2217 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002218
2219 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2220}
2221
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002222/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2223/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002224SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2225 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002226 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2227 EVT VT = Op.getValueType();
2228 unsigned VTBits = VT.getSizeInBits();
2229 DebugLoc dl = Op.getDebugLoc();
2230 SDValue ShOpLo = Op.getOperand(0);
2231 SDValue ShOpHi = Op.getOperand(1);
2232 SDValue ShAmt = Op.getOperand(2);
2233 SDValue ARMCC;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002234 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002235
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002236 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2237
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002238 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2239 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2240 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2241 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2242 DAG.getConstant(VTBits, MVT::i32));
2243 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2244 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002245 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002246
2247 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2248 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002249 ARMCC, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002250 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002251 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC,
2252 CCR, Cmp);
2253
2254 SDValue Ops[2] = { Lo, Hi };
2255 return DAG.getMergeValues(Ops, 2, dl);
2256}
2257
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002258/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2259/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002260SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2261 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002262 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2263 EVT VT = Op.getValueType();
2264 unsigned VTBits = VT.getSizeInBits();
2265 DebugLoc dl = Op.getDebugLoc();
2266 SDValue ShOpLo = Op.getOperand(0);
2267 SDValue ShOpHi = Op.getOperand(1);
2268 SDValue ShAmt = Op.getOperand(2);
2269 SDValue ARMCC;
2270
2271 assert(Op.getOpcode() == ISD::SHL_PARTS);
2272 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2273 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2274 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2275 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2276 DAG.getConstant(VTBits, MVT::i32));
2277 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2278 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2279
2280 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2281 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2282 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002283 ARMCC, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002284 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
2285 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMCC,
2286 CCR, Cmp);
2287
2288 SDValue Ops[2] = { Lo, Hi };
2289 return DAG.getMergeValues(Ops, 2, dl);
2290}
2291
Jim Grosbach3482c802010-01-18 19:58:49 +00002292static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
2293 const ARMSubtarget *ST) {
2294 EVT VT = N->getValueType(0);
2295 DebugLoc dl = N->getDebugLoc();
2296
2297 if (!ST->hasV6T2Ops())
2298 return SDValue();
2299
2300 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
2301 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
2302}
2303
Bob Wilson5bafff32009-06-22 23:27:02 +00002304static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2305 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002306 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002307 DebugLoc dl = N->getDebugLoc();
2308
2309 // Lower vector shifts on NEON to use VSHL.
2310 if (VT.isVector()) {
2311 assert(ST->hasNEON() && "unexpected vector shift");
2312
2313 // Left shifts translate directly to the vshiftu intrinsic.
2314 if (N->getOpcode() == ISD::SHL)
2315 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002316 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002317 N->getOperand(0), N->getOperand(1));
2318
2319 assert((N->getOpcode() == ISD::SRA ||
2320 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2321
2322 // NEON uses the same intrinsics for both left and right shifts. For
2323 // right shifts, the shift amounts are negative, so negate the vector of
2324 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002325 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002326 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2327 getZeroVector(ShiftVT, DAG, dl),
2328 N->getOperand(1));
2329 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2330 Intrinsic::arm_neon_vshifts :
2331 Intrinsic::arm_neon_vshiftu);
2332 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002333 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002334 N->getOperand(0), NegatedCount);
2335 }
2336
Eli Friedmance392eb2009-08-22 03:13:10 +00002337 // We can get here for a node like i32 = ISD::SHL i32, i64
2338 if (VT != MVT::i64)
2339 return SDValue();
2340
2341 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002342 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002343
Chris Lattner27a6c732007-11-24 07:07:01 +00002344 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2345 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002346 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002347 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002348
Chris Lattner27a6c732007-11-24 07:07:01 +00002349 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002350 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002351
Chris Lattner27a6c732007-11-24 07:07:01 +00002352 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002353 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2354 DAG.getConstant(0, MVT::i32));
2355 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2356 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002357
Chris Lattner27a6c732007-11-24 07:07:01 +00002358 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2359 // captures the result into a carry flag.
2360 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002361 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002362
Chris Lattner27a6c732007-11-24 07:07:01 +00002363 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002364 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002365
Chris Lattner27a6c732007-11-24 07:07:01 +00002366 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002367 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002368}
2369
Bob Wilson5bafff32009-06-22 23:27:02 +00002370static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2371 SDValue TmpOp0, TmpOp1;
2372 bool Invert = false;
2373 bool Swap = false;
2374 unsigned Opc = 0;
2375
2376 SDValue Op0 = Op.getOperand(0);
2377 SDValue Op1 = Op.getOperand(1);
2378 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002379 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002380 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2381 DebugLoc dl = Op.getDebugLoc();
2382
2383 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2384 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002385 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002386 case ISD::SETUNE:
2387 case ISD::SETNE: Invert = true; // Fallthrough
2388 case ISD::SETOEQ:
2389 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2390 case ISD::SETOLT:
2391 case ISD::SETLT: Swap = true; // Fallthrough
2392 case ISD::SETOGT:
2393 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2394 case ISD::SETOLE:
2395 case ISD::SETLE: Swap = true; // Fallthrough
2396 case ISD::SETOGE:
2397 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2398 case ISD::SETUGE: Swap = true; // Fallthrough
2399 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2400 case ISD::SETUGT: Swap = true; // Fallthrough
2401 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2402 case ISD::SETUEQ: Invert = true; // Fallthrough
2403 case ISD::SETONE:
2404 // Expand this to (OLT | OGT).
2405 TmpOp0 = Op0;
2406 TmpOp1 = Op1;
2407 Opc = ISD::OR;
2408 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2409 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2410 break;
2411 case ISD::SETUO: Invert = true; // Fallthrough
2412 case ISD::SETO:
2413 // Expand this to (OLT | OGE).
2414 TmpOp0 = Op0;
2415 TmpOp1 = Op1;
2416 Opc = ISD::OR;
2417 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2418 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2419 break;
2420 }
2421 } else {
2422 // Integer comparisons.
2423 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002424 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002425 case ISD::SETNE: Invert = true;
2426 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2427 case ISD::SETLT: Swap = true;
2428 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2429 case ISD::SETLE: Swap = true;
2430 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2431 case ISD::SETULT: Swap = true;
2432 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2433 case ISD::SETULE: Swap = true;
2434 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2435 }
2436
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002437 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002438 if (Opc == ARMISD::VCEQ) {
2439
2440 SDValue AndOp;
2441 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2442 AndOp = Op0;
2443 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2444 AndOp = Op1;
2445
2446 // Ignore bitconvert.
2447 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2448 AndOp = AndOp.getOperand(0);
2449
2450 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2451 Opc = ARMISD::VTST;
2452 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2453 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2454 Invert = !Invert;
2455 }
2456 }
2457 }
2458
2459 if (Swap)
2460 std::swap(Op0, Op1);
2461
2462 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2463
2464 if (Invert)
2465 Result = DAG.getNOT(dl, Result, VT);
2466
2467 return Result;
2468}
2469
2470/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2471/// VMOV instruction, and if so, return the constant being splatted.
2472static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2473 unsigned SplatBitSize, SelectionDAG &DAG) {
2474 switch (SplatBitSize) {
2475 case 8:
2476 // Any 1-byte value is OK.
2477 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson825b72b2009-08-11 20:47:22 +00002478 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilson5bafff32009-06-22 23:27:02 +00002479
2480 case 16:
2481 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2482 if ((SplatBits & ~0xff) == 0 ||
2483 (SplatBits & ~0xff00) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002484 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilson5bafff32009-06-22 23:27:02 +00002485 break;
2486
2487 case 32:
2488 // NEON's 32-bit VMOV supports splat values where:
2489 // * only one byte is nonzero, or
2490 // * the least significant byte is 0xff and the second byte is nonzero, or
2491 // * the least significant 2 bytes are 0xff and the third is nonzero.
2492 if ((SplatBits & ~0xff) == 0 ||
2493 (SplatBits & ~0xff00) == 0 ||
2494 (SplatBits & ~0xff0000) == 0 ||
2495 (SplatBits & ~0xff000000) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002496 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002497
2498 if ((SplatBits & ~0xffff) == 0 &&
2499 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002500 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002501
2502 if ((SplatBits & ~0xffffff) == 0 &&
2503 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002504 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002505
2506 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2507 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2508 // VMOV.I32. A (very) minor optimization would be to replicate the value
2509 // and fall through here to test for a valid 64-bit splat. But, then the
2510 // caller would also need to check and handle the change in size.
2511 break;
2512
2513 case 64: {
2514 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2515 uint64_t BitMask = 0xff;
2516 uint64_t Val = 0;
2517 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2518 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2519 Val |= BitMask;
2520 else if ((SplatBits & BitMask) != 0)
2521 return SDValue();
2522 BitMask <<= 8;
2523 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002524 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilson5bafff32009-06-22 23:27:02 +00002525 }
2526
2527 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002528 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilson5bafff32009-06-22 23:27:02 +00002529 break;
2530 }
2531
2532 return SDValue();
2533}
2534
2535/// getVMOVImm - If this is a build_vector of constants which can be
2536/// formed by using a VMOV instruction of the specified element size,
2537/// return the constant being splatted. The ByteSize field indicates the
2538/// number of bytes of each element [1248].
2539SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2540 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2541 APInt SplatBits, SplatUndef;
2542 unsigned SplatBitSize;
2543 bool HasAnyUndefs;
2544 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2545 HasAnyUndefs, ByteSize * 8))
2546 return SDValue();
2547
2548 if (SplatBitSize > ByteSize * 8)
2549 return SDValue();
2550
2551 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2552 SplatBitSize, DAG);
2553}
2554
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002555static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2556 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002557 unsigned NumElts = VT.getVectorNumElements();
2558 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002559 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002560
2561 // If this is a VEXT shuffle, the immediate value is the index of the first
2562 // element. The other shuffle indices must be the successive elements after
2563 // the first one.
2564 unsigned ExpectedElt = Imm;
2565 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002566 // Increment the expected index. If it wraps around, it may still be
2567 // a VEXT but the source vectors must be swapped.
2568 ExpectedElt += 1;
2569 if (ExpectedElt == NumElts * 2) {
2570 ExpectedElt = 0;
2571 ReverseVEXT = true;
2572 }
2573
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002574 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002575 return false;
2576 }
2577
2578 // Adjust the index value if the source operands will be swapped.
2579 if (ReverseVEXT)
2580 Imm -= NumElts;
2581
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002582 return true;
2583}
2584
Bob Wilson8bb9e482009-07-26 00:39:34 +00002585/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2586/// instruction with the specified blocksize. (The order of the elements
2587/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002588static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2589 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002590 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2591 "Only possible block sizes for VREV are: 16, 32, 64");
2592
Bob Wilson8bb9e482009-07-26 00:39:34 +00002593 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00002594 if (EltSz == 64)
2595 return false;
2596
2597 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002598 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002599
2600 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2601 return false;
2602
2603 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002604 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002605 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2606 return false;
2607 }
2608
2609 return true;
2610}
2611
Bob Wilsonc692cb72009-08-21 20:54:19 +00002612static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2613 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002614 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2615 if (EltSz == 64)
2616 return false;
2617
Bob Wilsonc692cb72009-08-21 20:54:19 +00002618 unsigned NumElts = VT.getVectorNumElements();
2619 WhichResult = (M[0] == 0 ? 0 : 1);
2620 for (unsigned i = 0; i < NumElts; i += 2) {
2621 if ((unsigned) M[i] != i + WhichResult ||
2622 (unsigned) M[i+1] != i + NumElts + WhichResult)
2623 return false;
2624 }
2625 return true;
2626}
2627
Bob Wilson324f4f12009-12-03 06:40:55 +00002628/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
2629/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2630/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
2631static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2632 unsigned &WhichResult) {
2633 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2634 if (EltSz == 64)
2635 return false;
2636
2637 unsigned NumElts = VT.getVectorNumElements();
2638 WhichResult = (M[0] == 0 ? 0 : 1);
2639 for (unsigned i = 0; i < NumElts; i += 2) {
2640 if ((unsigned) M[i] != i + WhichResult ||
2641 (unsigned) M[i+1] != i + WhichResult)
2642 return false;
2643 }
2644 return true;
2645}
2646
Bob Wilsonc692cb72009-08-21 20:54:19 +00002647static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2648 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002649 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2650 if (EltSz == 64)
2651 return false;
2652
Bob Wilsonc692cb72009-08-21 20:54:19 +00002653 unsigned NumElts = VT.getVectorNumElements();
2654 WhichResult = (M[0] == 0 ? 0 : 1);
2655 for (unsigned i = 0; i != NumElts; ++i) {
2656 if ((unsigned) M[i] != 2 * i + WhichResult)
2657 return false;
2658 }
2659
2660 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002661 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002662 return false;
2663
2664 return true;
2665}
2666
Bob Wilson324f4f12009-12-03 06:40:55 +00002667/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
2668/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2669/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
2670static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2671 unsigned &WhichResult) {
2672 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2673 if (EltSz == 64)
2674 return false;
2675
2676 unsigned Half = VT.getVectorNumElements() / 2;
2677 WhichResult = (M[0] == 0 ? 0 : 1);
2678 for (unsigned j = 0; j != 2; ++j) {
2679 unsigned Idx = WhichResult;
2680 for (unsigned i = 0; i != Half; ++i) {
2681 if ((unsigned) M[i + j * Half] != Idx)
2682 return false;
2683 Idx += 2;
2684 }
2685 }
2686
2687 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2688 if (VT.is64BitVector() && EltSz == 32)
2689 return false;
2690
2691 return true;
2692}
2693
Bob Wilsonc692cb72009-08-21 20:54:19 +00002694static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2695 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002696 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2697 if (EltSz == 64)
2698 return false;
2699
Bob Wilsonc692cb72009-08-21 20:54:19 +00002700 unsigned NumElts = VT.getVectorNumElements();
2701 WhichResult = (M[0] == 0 ? 0 : 1);
2702 unsigned Idx = WhichResult * NumElts / 2;
2703 for (unsigned i = 0; i != NumElts; i += 2) {
2704 if ((unsigned) M[i] != Idx ||
2705 (unsigned) M[i+1] != Idx + NumElts)
2706 return false;
2707 Idx += 1;
2708 }
2709
2710 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002711 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002712 return false;
2713
2714 return true;
2715}
2716
Bob Wilson324f4f12009-12-03 06:40:55 +00002717/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
2718/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2719/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
2720static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2721 unsigned &WhichResult) {
2722 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2723 if (EltSz == 64)
2724 return false;
2725
2726 unsigned NumElts = VT.getVectorNumElements();
2727 WhichResult = (M[0] == 0 ? 0 : 1);
2728 unsigned Idx = WhichResult * NumElts / 2;
2729 for (unsigned i = 0; i != NumElts; i += 2) {
2730 if ((unsigned) M[i] != Idx ||
2731 (unsigned) M[i+1] != Idx)
2732 return false;
2733 Idx += 1;
2734 }
2735
2736 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2737 if (VT.is64BitVector() && EltSz == 32)
2738 return false;
2739
2740 return true;
2741}
2742
2743
Owen Andersone50ed302009-08-10 22:56:29 +00002744static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002745 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00002746 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002747 if (ConstVal->isNullValue())
2748 return getZeroVector(VT, DAG, dl);
2749 if (ConstVal->isAllOnesValue())
2750 return getOnesVector(VT, DAG, dl);
2751
Owen Andersone50ed302009-08-10 22:56:29 +00002752 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00002753 if (VT.is64BitVector()) {
2754 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002755 case 8: CanonicalVT = MVT::v8i8; break;
2756 case 16: CanonicalVT = MVT::v4i16; break;
2757 case 32: CanonicalVT = MVT::v2i32; break;
2758 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002759 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002760 }
2761 } else {
2762 assert(VT.is128BitVector() && "unknown splat vector size");
2763 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002764 case 8: CanonicalVT = MVT::v16i8; break;
2765 case 16: CanonicalVT = MVT::v8i16; break;
2766 case 32: CanonicalVT = MVT::v4i32; break;
2767 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002768 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002769 }
2770 }
2771
2772 // Build a canonical splat for this value.
2773 SmallVector<SDValue, 8> Ops;
2774 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2775 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2776 Ops.size());
2777 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2778}
2779
2780// If this is a case we can't handle, return null and let the default
2781// expansion code take care of it.
2782static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00002783 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002784 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002785 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002786
2787 APInt SplatBits, SplatUndef;
2788 unsigned SplatBitSize;
2789 bool HasAnyUndefs;
2790 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00002791 if (SplatBitSize <= 64) {
2792 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2793 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2794 if (Val.getNode())
2795 return BuildSplat(Val, VT, DAG, dl);
2796 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00002797 }
2798
2799 // If there are only 2 elements in a 128-bit vector, insert them into an
2800 // undef vector. This handles the common case for 128-bit vector argument
2801 // passing, where the insertions should be translated to subreg accesses
2802 // with no real instructions.
2803 if (VT.is128BitVector() && Op.getNumOperands() == 2) {
2804 SDValue Val = DAG.getUNDEF(VT);
2805 SDValue Op0 = Op.getOperand(0);
2806 SDValue Op1 = Op.getOperand(1);
2807 if (Op0.getOpcode() != ISD::UNDEF)
2808 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op0,
2809 DAG.getIntPtrConstant(0));
2810 if (Op1.getOpcode() != ISD::UNDEF)
2811 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op1,
2812 DAG.getIntPtrConstant(1));
2813 return Val;
Bob Wilson5bafff32009-06-22 23:27:02 +00002814 }
2815
2816 return SDValue();
2817}
2818
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002819/// isShuffleMaskLegal - Targets can use this to indicate that they only
2820/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2821/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2822/// are assumed to be legal.
2823bool
2824ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2825 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002826 if (VT.getVectorNumElements() == 4 &&
2827 (VT.is128BitVector() || VT.is64BitVector())) {
2828 unsigned PFIndexes[4];
2829 for (unsigned i = 0; i != 4; ++i) {
2830 if (M[i] < 0)
2831 PFIndexes[i] = 8;
2832 else
2833 PFIndexes[i] = M[i];
2834 }
2835
2836 // Compute the index in the perfect shuffle table.
2837 unsigned PFTableIndex =
2838 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2839 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2840 unsigned Cost = (PFEntry >> 30);
2841
2842 if (Cost <= 4)
2843 return true;
2844 }
2845
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002846 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00002847 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002848
2849 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2850 isVREVMask(M, VT, 64) ||
2851 isVREVMask(M, VT, 32) ||
2852 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00002853 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2854 isVTRNMask(M, VT, WhichResult) ||
2855 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00002856 isVZIPMask(M, VT, WhichResult) ||
2857 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
2858 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
2859 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002860}
2861
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002862/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2863/// the specified operations to build the shuffle.
2864static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2865 SDValue RHS, SelectionDAG &DAG,
2866 DebugLoc dl) {
2867 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2868 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2869 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2870
2871 enum {
2872 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2873 OP_VREV,
2874 OP_VDUP0,
2875 OP_VDUP1,
2876 OP_VDUP2,
2877 OP_VDUP3,
2878 OP_VEXT1,
2879 OP_VEXT2,
2880 OP_VEXT3,
2881 OP_VUZPL, // VUZP, left result
2882 OP_VUZPR, // VUZP, right result
2883 OP_VZIPL, // VZIP, left result
2884 OP_VZIPR, // VZIP, right result
2885 OP_VTRNL, // VTRN, left result
2886 OP_VTRNR // VTRN, right result
2887 };
2888
2889 if (OpNum == OP_COPY) {
2890 if (LHSID == (1*9+2)*9+3) return LHS;
2891 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2892 return RHS;
2893 }
2894
2895 SDValue OpLHS, OpRHS;
2896 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2897 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2898 EVT VT = OpLHS.getValueType();
2899
2900 switch (OpNum) {
2901 default: llvm_unreachable("Unknown shuffle opcode!");
2902 case OP_VREV:
2903 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2904 case OP_VDUP0:
2905 case OP_VDUP1:
2906 case OP_VDUP2:
2907 case OP_VDUP3:
2908 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002909 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002910 case OP_VEXT1:
2911 case OP_VEXT2:
2912 case OP_VEXT3:
2913 return DAG.getNode(ARMISD::VEXT, dl, VT,
2914 OpLHS, OpRHS,
2915 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2916 case OP_VUZPL:
2917 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002918 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002919 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
2920 case OP_VZIPL:
2921 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002922 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002923 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
2924 case OP_VTRNL:
2925 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002926 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2927 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002928 }
2929}
2930
Bob Wilson5bafff32009-06-22 23:27:02 +00002931static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002932 SDValue V1 = Op.getOperand(0);
2933 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00002934 DebugLoc dl = Op.getDebugLoc();
2935 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002936 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002937 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00002938
Bob Wilson28865062009-08-13 02:13:04 +00002939 // Convert shuffles that are directly supported on NEON to target-specific
2940 // DAG nodes, instead of keeping them as shuffles and matching them again
2941 // during code selection. This is more efficient and avoids the possibility
2942 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00002943 // FIXME: floating-point vectors should be canonicalized to integer vectors
2944 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002945 SVN->getMask(ShuffleMask);
2946
2947 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson0ce37102009-08-14 05:08:32 +00002948 int Lane = SVN->getSplatIndex();
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00002949 // If this is undef splat, generate it via "just" vdup, if possible.
2950 if (Lane == -1) Lane = 0;
2951
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002952 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
2953 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00002954 }
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002955 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002956 DAG.getConstant(Lane, MVT::i32));
Bob Wilson0ce37102009-08-14 05:08:32 +00002957 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002958
2959 bool ReverseVEXT;
2960 unsigned Imm;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002961 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002962 if (ReverseVEXT)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002963 std::swap(V1, V2);
2964 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002965 DAG.getConstant(Imm, MVT::i32));
2966 }
2967
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002968 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002969 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002970 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002971 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002972 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002973 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
2974
Bob Wilsonc692cb72009-08-21 20:54:19 +00002975 // Check for Neon shuffles that modify both input vectors in place.
2976 // If both results are used, i.e., if there are two shuffles with the same
2977 // source operands and with masks corresponding to both results of one of
2978 // these operations, DAG memoization will ensure that a single node is
2979 // used for both shuffles.
2980 unsigned WhichResult;
2981 if (isVTRNMask(ShuffleMask, VT, WhichResult))
2982 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2983 V1, V2).getValue(WhichResult);
2984 if (isVUZPMask(ShuffleMask, VT, WhichResult))
2985 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2986 V1, V2).getValue(WhichResult);
2987 if (isVZIPMask(ShuffleMask, VT, WhichResult))
2988 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2989 V1, V2).getValue(WhichResult);
2990
Bob Wilson324f4f12009-12-03 06:40:55 +00002991 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
2992 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2993 V1, V1).getValue(WhichResult);
2994 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
2995 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2996 V1, V1).getValue(WhichResult);
2997 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
2998 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2999 V1, V1).getValue(WhichResult);
3000
Bob Wilsonc692cb72009-08-21 20:54:19 +00003001 // If the shuffle is not directly supported and it has 4 elements, use
3002 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003003 if (VT.getVectorNumElements() == 4 &&
3004 (VT.is128BitVector() || VT.is64BitVector())) {
3005 unsigned PFIndexes[4];
3006 for (unsigned i = 0; i != 4; ++i) {
3007 if (ShuffleMask[i] < 0)
3008 PFIndexes[i] = 8;
3009 else
3010 PFIndexes[i] = ShuffleMask[i];
3011 }
3012
3013 // Compute the index in the perfect shuffle table.
3014 unsigned PFTableIndex =
3015 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3016
3017 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3018 unsigned Cost = (PFEntry >> 30);
3019
3020 if (Cost <= 4)
3021 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
3022 }
Bob Wilsond8e17572009-08-12 22:31:50 +00003023
Bob Wilson22cac0d2009-08-14 05:16:33 +00003024 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003025}
3026
Bob Wilson5bafff32009-06-22 23:27:02 +00003027static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003028 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003029 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003030 SDValue Vec = Op.getOperand(0);
3031 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00003032 assert(VT == MVT::i32 &&
3033 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
3034 "unexpected type for custom-lowering vector extract");
3035 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00003036}
3037
Bob Wilsona6d65862009-08-03 20:36:38 +00003038static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3039 // The only time a CONCAT_VECTORS operation can have legal types is when
3040 // two 64-bit vectors are concatenated to a 128-bit vector.
3041 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
3042 "unexpected CONCAT_VECTORS");
3043 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00003044 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00003045 SDValue Op0 = Op.getOperand(0);
3046 SDValue Op1 = Op.getOperand(1);
3047 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003048 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3049 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00003050 DAG.getIntPtrConstant(0));
3051 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003052 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3053 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00003054 DAG.getIntPtrConstant(1));
3055 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003056}
3057
Dan Gohmand858e902010-04-17 15:26:15 +00003058SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003059 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003060 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00003061 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00003062 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003063 case ISD::GlobalAddress:
3064 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
3065 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00003066 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00003067 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
3068 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003069 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00003070 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00003071 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00003072 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00003073 case ISD::SINT_TO_FP:
3074 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
3075 case ISD::FP_TO_SINT:
3076 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003077 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00003078 case ISD::RETURNADDR: break;
Jim Grosbach0e0da732009-05-12 23:59:14 +00003079 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003080 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00003081 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
3082 Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00003083 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003084 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00003085 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00003086 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00003087 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003088 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00003089 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00003090 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003091 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
3092 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3093 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003094 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00003095 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003096 }
Dan Gohman475871a2008-07-27 21:46:04 +00003097 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003098}
3099
Duncan Sands1607f052008-12-01 11:39:25 +00003100/// ReplaceNodeResults - Replace the results of node with an illegal result
3101/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00003102void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
3103 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00003104 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00003105 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00003106 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00003107 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003108 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003109 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003110 case ISD::BIT_CONVERT:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003111 Res = ExpandBIT_CONVERT(N, DAG);
3112 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00003113 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003114 case ISD::SRA:
3115 Res = LowerShift(N, DAG, Subtarget);
3116 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003117 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00003118 if (Res.getNode())
3119 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00003120}
Chris Lattner27a6c732007-11-24 07:07:01 +00003121
Evan Chenga8e29892007-01-19 07:51:42 +00003122//===----------------------------------------------------------------------===//
3123// ARM Scheduler Hooks
3124//===----------------------------------------------------------------------===//
3125
3126MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003127ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
3128 MachineBasicBlock *BB,
3129 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00003130 unsigned dest = MI->getOperand(0).getReg();
3131 unsigned ptr = MI->getOperand(1).getReg();
3132 unsigned oldval = MI->getOperand(2).getReg();
3133 unsigned newval = MI->getOperand(3).getReg();
3134 unsigned scratch = BB->getParent()->getRegInfo()
3135 .createVirtualRegister(ARM::GPRRegisterClass);
3136 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3137 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003138 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00003139
3140 unsigned ldrOpc, strOpc;
3141 switch (Size) {
3142 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003143 case 1:
3144 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
3145 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
3146 break;
3147 case 2:
3148 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3149 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3150 break;
3151 case 4:
3152 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3153 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3154 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00003155 }
3156
3157 MachineFunction *MF = BB->getParent();
3158 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3159 MachineFunction::iterator It = BB;
3160 ++It; // insert the new blocks after the current block
3161
3162 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3163 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3164 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3165 MF->insert(It, loop1MBB);
3166 MF->insert(It, loop2MBB);
3167 MF->insert(It, exitMBB);
3168 exitMBB->transferSuccessors(BB);
3169
3170 // thisMBB:
3171 // ...
3172 // fallthrough --> loop1MBB
3173 BB->addSuccessor(loop1MBB);
3174
3175 // loop1MBB:
3176 // ldrex dest, [ptr]
3177 // cmp dest, oldval
3178 // bne exitMBB
3179 BB = loop1MBB;
3180 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003181 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003182 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003183 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3184 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003185 BB->addSuccessor(loop2MBB);
3186 BB->addSuccessor(exitMBB);
3187
3188 // loop2MBB:
3189 // strex scratch, newval, [ptr]
3190 // cmp scratch, #0
3191 // bne loop1MBB
3192 BB = loop2MBB;
3193 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
3194 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003195 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003196 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003197 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3198 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003199 BB->addSuccessor(loop1MBB);
3200 BB->addSuccessor(exitMBB);
3201
3202 // exitMBB:
3203 // ...
3204 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00003205
3206 MF->DeleteMachineInstr(MI); // The instruction is gone now.
3207
Jim Grosbach5278eb82009-12-11 01:42:04 +00003208 return BB;
3209}
3210
3211MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003212ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
3213 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00003214 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
3215 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3216
3217 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003218 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003219 MachineFunction::iterator It = BB;
3220 ++It;
3221
3222 unsigned dest = MI->getOperand(0).getReg();
3223 unsigned ptr = MI->getOperand(1).getReg();
3224 unsigned incr = MI->getOperand(2).getReg();
3225 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00003226
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003227 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003228 unsigned ldrOpc, strOpc;
3229 switch (Size) {
3230 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003231 case 1:
3232 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00003233 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003234 break;
3235 case 2:
3236 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3237 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3238 break;
3239 case 4:
3240 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3241 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3242 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00003243 }
3244
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003245 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3246 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3247 MF->insert(It, loopMBB);
3248 MF->insert(It, exitMBB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003249 exitMBB->transferSuccessors(BB);
3250
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003251 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003252 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3253 unsigned scratch2 = (!BinOpcode) ? incr :
3254 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3255
3256 // thisMBB:
3257 // ...
3258 // fallthrough --> loopMBB
3259 BB->addSuccessor(loopMBB);
3260
3261 // loopMBB:
3262 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003263 // <binop> scratch2, dest, incr
3264 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00003265 // cmp scratch, #0
3266 // bne- loopMBB
3267 // fallthrough --> exitMBB
3268 BB = loopMBB;
3269 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00003270 if (BinOpcode) {
3271 // operand order needs to go the other way for NAND
3272 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
3273 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3274 addReg(incr).addReg(dest)).addReg(0);
3275 else
3276 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3277 addReg(dest).addReg(incr)).addReg(0);
3278 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00003279
3280 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
3281 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003282 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00003283 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003284 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3285 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003286
3287 BB->addSuccessor(loopMBB);
3288 BB->addSuccessor(exitMBB);
3289
3290 // exitMBB:
3291 // ...
3292 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00003293
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003294 MF->DeleteMachineInstr(MI); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00003295
Jim Grosbachc3c23542009-12-14 04:22:04 +00003296 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00003297}
3298
3299MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00003300ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003301 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003302 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00003303 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003304 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00003305 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00003306 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00003307 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00003308 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00003309
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003310 case ARM::ATOMIC_LOAD_ADD_I8:
3311 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3312 case ARM::ATOMIC_LOAD_ADD_I16:
3313 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3314 case ARM::ATOMIC_LOAD_ADD_I32:
3315 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003316
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003317 case ARM::ATOMIC_LOAD_AND_I8:
3318 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3319 case ARM::ATOMIC_LOAD_AND_I16:
3320 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3321 case ARM::ATOMIC_LOAD_AND_I32:
3322 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003323
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003324 case ARM::ATOMIC_LOAD_OR_I8:
3325 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3326 case ARM::ATOMIC_LOAD_OR_I16:
3327 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3328 case ARM::ATOMIC_LOAD_OR_I32:
3329 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003330
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003331 case ARM::ATOMIC_LOAD_XOR_I8:
3332 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3333 case ARM::ATOMIC_LOAD_XOR_I16:
3334 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3335 case ARM::ATOMIC_LOAD_XOR_I32:
3336 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003337
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003338 case ARM::ATOMIC_LOAD_NAND_I8:
3339 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3340 case ARM::ATOMIC_LOAD_NAND_I16:
3341 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3342 case ARM::ATOMIC_LOAD_NAND_I32:
3343 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003344
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003345 case ARM::ATOMIC_LOAD_SUB_I8:
3346 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3347 case ARM::ATOMIC_LOAD_SUB_I16:
3348 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3349 case ARM::ATOMIC_LOAD_SUB_I32:
3350 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003351
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003352 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
3353 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
3354 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00003355
3356 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
3357 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
3358 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003359
Evan Cheng007ea272009-08-12 05:17:19 +00003360 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00003361 // To "insert" a SELECT_CC instruction, we actually have to insert the
3362 // diamond control-flow pattern. The incoming instruction knows the
3363 // destination vreg to set, the condition code register to branch on, the
3364 // true/false values to select between, and a branch opcode to use.
3365 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003366 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00003367 ++It;
3368
3369 // thisMBB:
3370 // ...
3371 // TrueVal = ...
3372 // cmpTY ccX, r1, r2
3373 // bCC copy1MBB
3374 // fallthrough --> copy0MBB
3375 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003376 MachineFunction *F = BB->getParent();
3377 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3378 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00003379 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00003380 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003381 F->insert(It, copy0MBB);
3382 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00003383 // Update machine-CFG edges by first adding all successors of the current
3384 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00003385 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003386 E = BB->succ_end(); I != E; ++I)
Evan Chengce319102009-09-19 09:51:03 +00003387 sinkMBB->addSuccessor(*I);
Evan Chenga8e29892007-01-19 07:51:42 +00003388 // Next, remove all successors of the current block, and add the true
3389 // and fallthrough blocks as its successors.
Evan Chengce319102009-09-19 09:51:03 +00003390 while (!BB->succ_empty())
Evan Chenga8e29892007-01-19 07:51:42 +00003391 BB->removeSuccessor(BB->succ_begin());
3392 BB->addSuccessor(copy0MBB);
3393 BB->addSuccessor(sinkMBB);
3394
3395 // copy0MBB:
3396 // %FalseValue = ...
3397 // # fallthrough to sinkMBB
3398 BB = copy0MBB;
3399
3400 // Update machine-CFG edges
3401 BB->addSuccessor(sinkMBB);
3402
3403 // sinkMBB:
3404 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3405 // ...
3406 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00003407 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00003408 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
3409 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
3410
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003411 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00003412 return BB;
3413 }
Evan Cheng86198642009-08-07 00:34:42 +00003414
3415 case ARM::tANDsp:
3416 case ARM::tADDspr_:
3417 case ARM::tSUBspi_:
3418 case ARM::t2SUBrSPi_:
3419 case ARM::t2SUBrSPi12_:
3420 case ARM::t2SUBrSPs_: {
3421 MachineFunction *MF = BB->getParent();
3422 unsigned DstReg = MI->getOperand(0).getReg();
3423 unsigned SrcReg = MI->getOperand(1).getReg();
3424 bool DstIsDead = MI->getOperand(0).isDead();
3425 bool SrcIsKill = MI->getOperand(1).isKill();
3426
3427 if (SrcReg != ARM::SP) {
3428 // Copy the source to SP from virtual register.
3429 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
3430 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3431 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
3432 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
3433 .addReg(SrcReg, getKillRegState(SrcIsKill));
3434 }
3435
3436 unsigned OpOpc = 0;
3437 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
3438 switch (MI->getOpcode()) {
3439 default:
3440 llvm_unreachable("Unexpected pseudo instruction!");
3441 case ARM::tANDsp:
3442 OpOpc = ARM::tAND;
3443 NeedPred = true;
3444 break;
3445 case ARM::tADDspr_:
3446 OpOpc = ARM::tADDspr;
3447 break;
3448 case ARM::tSUBspi_:
3449 OpOpc = ARM::tSUBspi;
3450 break;
3451 case ARM::t2SUBrSPi_:
3452 OpOpc = ARM::t2SUBrSPi;
3453 NeedPred = true; NeedCC = true;
3454 break;
3455 case ARM::t2SUBrSPi12_:
3456 OpOpc = ARM::t2SUBrSPi12;
3457 NeedPred = true;
3458 break;
3459 case ARM::t2SUBrSPs_:
3460 OpOpc = ARM::t2SUBrSPs;
3461 NeedPred = true; NeedCC = true; NeedOp3 = true;
3462 break;
3463 }
3464 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
3465 if (OpOpc == ARM::tAND)
3466 AddDefaultT1CC(MIB);
3467 MIB.addReg(ARM::SP);
3468 MIB.addOperand(MI->getOperand(2));
3469 if (NeedOp3)
3470 MIB.addOperand(MI->getOperand(3));
3471 if (NeedPred)
3472 AddDefaultPred(MIB);
3473 if (NeedCC)
3474 AddDefaultCC(MIB);
3475
3476 // Copy the result from SP to virtual register.
3477 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
3478 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3479 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
3480 BuildMI(BB, dl, TII->get(CopyOpc))
3481 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
3482 .addReg(ARM::SP);
3483 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3484 return BB;
3485 }
Evan Chenga8e29892007-01-19 07:51:42 +00003486 }
3487}
3488
3489//===----------------------------------------------------------------------===//
3490// ARM Optimization Hooks
3491//===----------------------------------------------------------------------===//
3492
Chris Lattnerd1980a52009-03-12 06:52:53 +00003493static
3494SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3495 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00003496 SelectionDAG &DAG = DCI.DAG;
3497 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003498 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00003499 unsigned Opc = N->getOpcode();
3500 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3501 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3502 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3503 ISD::CondCode CC = ISD::SETCC_INVALID;
3504
3505 if (isSlctCC) {
3506 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
3507 } else {
3508 SDValue CCOp = Slct.getOperand(0);
3509 if (CCOp.getOpcode() == ISD::SETCC)
3510 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
3511 }
3512
3513 bool DoXform = false;
3514 bool InvCC = false;
3515 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
3516 "Bad input!");
3517
3518 if (LHS.getOpcode() == ISD::Constant &&
3519 cast<ConstantSDNode>(LHS)->isNullValue()) {
3520 DoXform = true;
3521 } else if (CC != ISD::SETCC_INVALID &&
3522 RHS.getOpcode() == ISD::Constant &&
3523 cast<ConstantSDNode>(RHS)->isNullValue()) {
3524 std::swap(LHS, RHS);
3525 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003526 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00003527 Op0.getOperand(0).getValueType();
3528 bool isInt = OpVT.isInteger();
3529 CC = ISD::getSetCCInverse(CC, isInt);
3530
3531 if (!TLI.isCondCodeLegal(CC, OpVT))
3532 return SDValue(); // Inverse operator isn't legal.
3533
3534 DoXform = true;
3535 InvCC = true;
3536 }
3537
3538 if (DoXform) {
3539 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3540 if (isSlctCC)
3541 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3542 Slct.getOperand(0), Slct.getOperand(1), CC);
3543 SDValue CCOp = Slct.getOperand(0);
3544 if (InvCC)
3545 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3546 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3547 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3548 CCOp, OtherOp, Result);
3549 }
3550 return SDValue();
3551}
3552
3553/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3554static SDValue PerformADDCombine(SDNode *N,
3555 TargetLowering::DAGCombinerInfo &DCI) {
3556 // added by evan in r37685 with no testcase.
3557 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003558
Chris Lattnerd1980a52009-03-12 06:52:53 +00003559 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3560 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3561 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3562 if (Result.getNode()) return Result;
3563 }
3564 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3565 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3566 if (Result.getNode()) return Result;
3567 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003568
Chris Lattnerd1980a52009-03-12 06:52:53 +00003569 return SDValue();
3570}
3571
3572/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3573static SDValue PerformSUBCombine(SDNode *N,
3574 TargetLowering::DAGCombinerInfo &DCI) {
3575 // added by evan in r37685 with no testcase.
3576 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003577
Chris Lattnerd1980a52009-03-12 06:52:53 +00003578 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3579 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3580 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3581 if (Result.getNode()) return Result;
3582 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003583
Chris Lattnerd1980a52009-03-12 06:52:53 +00003584 return SDValue();
3585}
3586
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +00003587/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
3588/// ARMISD::VMOVRRD.
Jim Grosbache5165492009-11-09 00:11:35 +00003589static SDValue PerformVMOVRRDCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003590 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003591 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003592 SDValue InDouble = N->getOperand(0);
Jim Grosbache5165492009-11-09 00:11:35 +00003593 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003594 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003595 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003596}
3597
Bob Wilson5bafff32009-06-22 23:27:02 +00003598/// getVShiftImm - Check if this is a valid build_vector for the immediate
3599/// operand of a vector shift operation, where all the elements of the
3600/// build_vector must have the same constant integer value.
3601static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3602 // Ignore bit_converts.
3603 while (Op.getOpcode() == ISD::BIT_CONVERT)
3604 Op = Op.getOperand(0);
3605 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3606 APInt SplatBits, SplatUndef;
3607 unsigned SplatBitSize;
3608 bool HasAnyUndefs;
3609 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3610 HasAnyUndefs, ElementBits) ||
3611 SplatBitSize > ElementBits)
3612 return false;
3613 Cnt = SplatBits.getSExtValue();
3614 return true;
3615}
3616
3617/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3618/// operand of a vector shift left operation. That value must be in the range:
3619/// 0 <= Value < ElementBits for a left shift; or
3620/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003621static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003622 assert(VT.isVector() && "vector shift count is not a vector type");
3623 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3624 if (! getVShiftImm(Op, ElementBits, Cnt))
3625 return false;
3626 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3627}
3628
3629/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3630/// operand of a vector shift right operation. For a shift opcode, the value
3631/// is positive, but for an intrinsic the value count must be negative. The
3632/// absolute value must be in the range:
3633/// 1 <= |Value| <= ElementBits for a right shift; or
3634/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003635static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00003636 int64_t &Cnt) {
3637 assert(VT.isVector() && "vector shift count is not a vector type");
3638 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3639 if (! getVShiftImm(Op, ElementBits, Cnt))
3640 return false;
3641 if (isIntrinsic)
3642 Cnt = -Cnt;
3643 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3644}
3645
3646/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3647static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3648 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3649 switch (IntNo) {
3650 default:
3651 // Don't do anything for most intrinsics.
3652 break;
3653
3654 // Vector shifts: check for immediate versions and lower them.
3655 // Note: This is done during DAG combining instead of DAG legalizing because
3656 // the build_vectors for 64-bit vector element shift counts are generally
3657 // not legal, and it is hard to see their values after they get legalized to
3658 // loads from a constant pool.
3659 case Intrinsic::arm_neon_vshifts:
3660 case Intrinsic::arm_neon_vshiftu:
3661 case Intrinsic::arm_neon_vshiftls:
3662 case Intrinsic::arm_neon_vshiftlu:
3663 case Intrinsic::arm_neon_vshiftn:
3664 case Intrinsic::arm_neon_vrshifts:
3665 case Intrinsic::arm_neon_vrshiftu:
3666 case Intrinsic::arm_neon_vrshiftn:
3667 case Intrinsic::arm_neon_vqshifts:
3668 case Intrinsic::arm_neon_vqshiftu:
3669 case Intrinsic::arm_neon_vqshiftsu:
3670 case Intrinsic::arm_neon_vqshiftns:
3671 case Intrinsic::arm_neon_vqshiftnu:
3672 case Intrinsic::arm_neon_vqshiftnsu:
3673 case Intrinsic::arm_neon_vqrshiftns:
3674 case Intrinsic::arm_neon_vqrshiftnu:
3675 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00003676 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003677 int64_t Cnt;
3678 unsigned VShiftOpc = 0;
3679
3680 switch (IntNo) {
3681 case Intrinsic::arm_neon_vshifts:
3682 case Intrinsic::arm_neon_vshiftu:
3683 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3684 VShiftOpc = ARMISD::VSHL;
3685 break;
3686 }
3687 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3688 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3689 ARMISD::VSHRs : ARMISD::VSHRu);
3690 break;
3691 }
3692 return SDValue();
3693
3694 case Intrinsic::arm_neon_vshiftls:
3695 case Intrinsic::arm_neon_vshiftlu:
3696 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3697 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003698 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003699
3700 case Intrinsic::arm_neon_vrshifts:
3701 case Intrinsic::arm_neon_vrshiftu:
3702 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3703 break;
3704 return SDValue();
3705
3706 case Intrinsic::arm_neon_vqshifts:
3707 case Intrinsic::arm_neon_vqshiftu:
3708 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3709 break;
3710 return SDValue();
3711
3712 case Intrinsic::arm_neon_vqshiftsu:
3713 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3714 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003715 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003716
3717 case Intrinsic::arm_neon_vshiftn:
3718 case Intrinsic::arm_neon_vrshiftn:
3719 case Intrinsic::arm_neon_vqshiftns:
3720 case Intrinsic::arm_neon_vqshiftnu:
3721 case Intrinsic::arm_neon_vqshiftnsu:
3722 case Intrinsic::arm_neon_vqrshiftns:
3723 case Intrinsic::arm_neon_vqrshiftnu:
3724 case Intrinsic::arm_neon_vqrshiftnsu:
3725 // Narrowing shifts require an immediate right shift.
3726 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3727 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003728 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003729
3730 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003731 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003732 }
3733
3734 switch (IntNo) {
3735 case Intrinsic::arm_neon_vshifts:
3736 case Intrinsic::arm_neon_vshiftu:
3737 // Opcode already set above.
3738 break;
3739 case Intrinsic::arm_neon_vshiftls:
3740 case Intrinsic::arm_neon_vshiftlu:
3741 if (Cnt == VT.getVectorElementType().getSizeInBits())
3742 VShiftOpc = ARMISD::VSHLLi;
3743 else
3744 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3745 ARMISD::VSHLLs : ARMISD::VSHLLu);
3746 break;
3747 case Intrinsic::arm_neon_vshiftn:
3748 VShiftOpc = ARMISD::VSHRN; break;
3749 case Intrinsic::arm_neon_vrshifts:
3750 VShiftOpc = ARMISD::VRSHRs; break;
3751 case Intrinsic::arm_neon_vrshiftu:
3752 VShiftOpc = ARMISD::VRSHRu; break;
3753 case Intrinsic::arm_neon_vrshiftn:
3754 VShiftOpc = ARMISD::VRSHRN; break;
3755 case Intrinsic::arm_neon_vqshifts:
3756 VShiftOpc = ARMISD::VQSHLs; break;
3757 case Intrinsic::arm_neon_vqshiftu:
3758 VShiftOpc = ARMISD::VQSHLu; break;
3759 case Intrinsic::arm_neon_vqshiftsu:
3760 VShiftOpc = ARMISD::VQSHLsu; break;
3761 case Intrinsic::arm_neon_vqshiftns:
3762 VShiftOpc = ARMISD::VQSHRNs; break;
3763 case Intrinsic::arm_neon_vqshiftnu:
3764 VShiftOpc = ARMISD::VQSHRNu; break;
3765 case Intrinsic::arm_neon_vqshiftnsu:
3766 VShiftOpc = ARMISD::VQSHRNsu; break;
3767 case Intrinsic::arm_neon_vqrshiftns:
3768 VShiftOpc = ARMISD::VQRSHRNs; break;
3769 case Intrinsic::arm_neon_vqrshiftnu:
3770 VShiftOpc = ARMISD::VQRSHRNu; break;
3771 case Intrinsic::arm_neon_vqrshiftnsu:
3772 VShiftOpc = ARMISD::VQRSHRNsu; break;
3773 }
3774
3775 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003776 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003777 }
3778
3779 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00003780 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003781 int64_t Cnt;
3782 unsigned VShiftOpc = 0;
3783
3784 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3785 VShiftOpc = ARMISD::VSLI;
3786 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3787 VShiftOpc = ARMISD::VSRI;
3788 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003789 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003790 }
3791
3792 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3793 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00003794 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003795 }
3796
3797 case Intrinsic::arm_neon_vqrshifts:
3798 case Intrinsic::arm_neon_vqrshiftu:
3799 // No immediate versions of these to check for.
3800 break;
3801 }
3802
3803 return SDValue();
3804}
3805
3806/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3807/// lowers them. As with the vector shift intrinsics, this is done during DAG
3808/// combining instead of DAG legalizing because the build_vectors for 64-bit
3809/// vector element shift counts are generally not legal, and it is hard to see
3810/// their values after they get legalized to loads from a constant pool.
3811static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3812 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003813 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003814
3815 // Nothing to be done for scalar shifts.
3816 if (! VT.isVector())
3817 return SDValue();
3818
3819 assert(ST->hasNEON() && "unexpected vector shift");
3820 int64_t Cnt;
3821
3822 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003823 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003824
3825 case ISD::SHL:
3826 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3827 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003828 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003829 break;
3830
3831 case ISD::SRA:
3832 case ISD::SRL:
3833 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3834 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3835 ARMISD::VSHRs : ARMISD::VSHRu);
3836 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003837 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003838 }
3839 }
3840 return SDValue();
3841}
3842
3843/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3844/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3845static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3846 const ARMSubtarget *ST) {
3847 SDValue N0 = N->getOperand(0);
3848
3849 // Check for sign- and zero-extensions of vector extract operations of 8-
3850 // and 16-bit vector elements. NEON supports these directly. They are
3851 // handled during DAG combining because type legalization will promote them
3852 // to 32-bit types and it is messy to recognize the operations after that.
3853 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3854 SDValue Vec = N0.getOperand(0);
3855 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003856 EVT VT = N->getValueType(0);
3857 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003858 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3859
Owen Anderson825b72b2009-08-11 20:47:22 +00003860 if (VT == MVT::i32 &&
3861 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00003862 TLI.isTypeLegal(Vec.getValueType())) {
3863
3864 unsigned Opc = 0;
3865 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003866 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003867 case ISD::SIGN_EXTEND:
3868 Opc = ARMISD::VGETLANEs;
3869 break;
3870 case ISD::ZERO_EXTEND:
3871 case ISD::ANY_EXTEND:
3872 Opc = ARMISD::VGETLANEu;
3873 break;
3874 }
3875 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
3876 }
3877 }
3878
3879 return SDValue();
3880}
3881
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003882/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
3883/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
3884static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
3885 const ARMSubtarget *ST) {
3886 // If the target supports NEON, try to use vmax/vmin instructions for f32
3887 // selects like "x < y ? x : y". Unless the FiniteOnlyFPMath option is set,
3888 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
3889 // a NaN; only do the transformation when it matches that behavior.
3890
3891 // For now only do this when using NEON for FP operations; if using VFP, it
3892 // is not obvious that the benefit outweighs the cost of switching to the
3893 // NEON pipeline.
3894 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
3895 N->getValueType(0) != MVT::f32)
3896 return SDValue();
3897
3898 SDValue CondLHS = N->getOperand(0);
3899 SDValue CondRHS = N->getOperand(1);
3900 SDValue LHS = N->getOperand(2);
3901 SDValue RHS = N->getOperand(3);
3902 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
3903
3904 unsigned Opcode = 0;
3905 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00003906 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003907 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00003908 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003909 IsReversed = true ; // x CC y ? y : x
3910 } else {
3911 return SDValue();
3912 }
3913
Bob Wilsone742bb52010-02-24 22:15:53 +00003914 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003915 switch (CC) {
3916 default: break;
3917 case ISD::SETOLT:
3918 case ISD::SETOLE:
3919 case ISD::SETLT:
3920 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003921 case ISD::SETULT:
3922 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00003923 // If LHS is NaN, an ordered comparison will be false and the result will
3924 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
3925 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
3926 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
3927 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
3928 break;
3929 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
3930 // will return -0, so vmin can only be used for unsafe math or if one of
3931 // the operands is known to be nonzero.
3932 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
3933 !UnsafeFPMath &&
3934 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
3935 break;
3936 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003937 break;
3938
3939 case ISD::SETOGT:
3940 case ISD::SETOGE:
3941 case ISD::SETGT:
3942 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003943 case ISD::SETUGT:
3944 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00003945 // If LHS is NaN, an ordered comparison will be false and the result will
3946 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
3947 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
3948 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
3949 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
3950 break;
3951 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
3952 // will return +0, so vmax can only be used for unsafe math or if one of
3953 // the operands is known to be nonzero.
3954 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
3955 !UnsafeFPMath &&
3956 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
3957 break;
3958 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003959 break;
3960 }
3961
3962 if (!Opcode)
3963 return SDValue();
3964 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
3965}
3966
Dan Gohman475871a2008-07-27 21:46:04 +00003967SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003968 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003969 switch (N->getOpcode()) {
3970 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003971 case ISD::ADD: return PerformADDCombine(N, DCI);
3972 case ISD::SUB: return PerformSUBCombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00003973 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003974 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003975 case ISD::SHL:
3976 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003977 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003978 case ISD::SIGN_EXTEND:
3979 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003980 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
3981 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003982 }
Dan Gohman475871a2008-07-27 21:46:04 +00003983 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003984}
3985
Bill Wendlingaf566342009-08-15 21:21:19 +00003986bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
3987 if (!Subtarget->hasV6Ops())
3988 // Pre-v6 does not support unaligned mem access.
3989 return false;
Anton Korobeynikov90cfc132010-01-30 14:08:12 +00003990 else {
3991 // v6+ may or may not support unaligned mem access depending on the system
3992 // configuration.
3993 // FIXME: This is pretty conservative. Should we provide cmdline option to
3994 // control the behaviour?
Bill Wendlingaf566342009-08-15 21:21:19 +00003995 if (!Subtarget->isTargetDarwin())
3996 return false;
3997 }
3998
3999 switch (VT.getSimpleVT().SimpleTy) {
4000 default:
4001 return false;
4002 case MVT::i8:
4003 case MVT::i16:
4004 case MVT::i32:
4005 return true;
4006 // FIXME: VLD1 etc with standard alignment is legal.
4007 }
4008}
4009
Evan Chenge6c835f2009-08-14 20:09:37 +00004010static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
4011 if (V < 0)
4012 return false;
4013
4014 unsigned Scale = 1;
4015 switch (VT.getSimpleVT().SimpleTy) {
4016 default: return false;
4017 case MVT::i1:
4018 case MVT::i8:
4019 // Scale == 1;
4020 break;
4021 case MVT::i16:
4022 // Scale == 2;
4023 Scale = 2;
4024 break;
4025 case MVT::i32:
4026 // Scale == 4;
4027 Scale = 4;
4028 break;
4029 }
4030
4031 if ((V & (Scale - 1)) != 0)
4032 return false;
4033 V /= Scale;
4034 return V == (V & ((1LL << 5) - 1));
4035}
4036
4037static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
4038 const ARMSubtarget *Subtarget) {
4039 bool isNeg = false;
4040 if (V < 0) {
4041 isNeg = true;
4042 V = - V;
4043 }
4044
4045 switch (VT.getSimpleVT().SimpleTy) {
4046 default: return false;
4047 case MVT::i1:
4048 case MVT::i8:
4049 case MVT::i16:
4050 case MVT::i32:
4051 // + imm12 or - imm8
4052 if (isNeg)
4053 return V == (V & ((1LL << 8) - 1));
4054 return V == (V & ((1LL << 12) - 1));
4055 case MVT::f32:
4056 case MVT::f64:
4057 // Same as ARM mode. FIXME: NEON?
4058 if (!Subtarget->hasVFP2())
4059 return false;
4060 if ((V & 3) != 0)
4061 return false;
4062 V >>= 2;
4063 return V == (V & ((1LL << 8) - 1));
4064 }
4065}
4066
Evan Chengb01fad62007-03-12 23:30:29 +00004067/// isLegalAddressImmediate - Return true if the integer value can be used
4068/// as the offset of the target addressing mode for load / store of the
4069/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00004070static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004071 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00004072 if (V == 0)
4073 return true;
4074
Evan Cheng65011532009-03-09 19:15:00 +00004075 if (!VT.isSimple())
4076 return false;
4077
Evan Chenge6c835f2009-08-14 20:09:37 +00004078 if (Subtarget->isThumb1Only())
4079 return isLegalT1AddressImmediate(V, VT);
4080 else if (Subtarget->isThumb2())
4081 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00004082
Evan Chenge6c835f2009-08-14 20:09:37 +00004083 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00004084 if (V < 0)
4085 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00004086 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00004087 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004088 case MVT::i1:
4089 case MVT::i8:
4090 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00004091 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004092 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004093 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00004094 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004095 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004096 case MVT::f32:
4097 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00004098 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00004099 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00004100 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00004101 return false;
4102 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004103 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00004104 }
Evan Chenga8e29892007-01-19 07:51:42 +00004105}
4106
Evan Chenge6c835f2009-08-14 20:09:37 +00004107bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
4108 EVT VT) const {
4109 int Scale = AM.Scale;
4110 if (Scale < 0)
4111 return false;
4112
4113 switch (VT.getSimpleVT().SimpleTy) {
4114 default: return false;
4115 case MVT::i1:
4116 case MVT::i8:
4117 case MVT::i16:
4118 case MVT::i32:
4119 if (Scale == 1)
4120 return true;
4121 // r + r << imm
4122 Scale = Scale & ~1;
4123 return Scale == 2 || Scale == 4 || Scale == 8;
4124 case MVT::i64:
4125 // r + r
4126 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
4127 return true;
4128 return false;
4129 case MVT::isVoid:
4130 // Note, we allow "void" uses (basically, uses that aren't loads or
4131 // stores), because arm allows folding a scale into many arithmetic
4132 // operations. This should be made more precise and revisited later.
4133
4134 // Allow r << imm, but the imm has to be a multiple of two.
4135 if (Scale & 1) return false;
4136 return isPowerOf2_32(Scale);
4137 }
4138}
4139
Chris Lattner37caf8c2007-04-09 23:33:39 +00004140/// isLegalAddressingMode - Return true if the addressing mode represented
4141/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004142bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004143 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004144 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00004145 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00004146 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004147
Chris Lattner37caf8c2007-04-09 23:33:39 +00004148 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004149 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004150 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004151
Chris Lattner37caf8c2007-04-09 23:33:39 +00004152 switch (AM.Scale) {
4153 case 0: // no scale reg, must be "r+i" or "r", or "i".
4154 break;
4155 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00004156 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00004157 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004158 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00004159 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004160 // ARM doesn't support any R+R*scale+imm addr modes.
4161 if (AM.BaseOffs)
4162 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004163
Bob Wilson2c7dab12009-04-08 17:55:28 +00004164 if (!VT.isSimple())
4165 return false;
4166
Evan Chenge6c835f2009-08-14 20:09:37 +00004167 if (Subtarget->isThumb2())
4168 return isLegalT2ScaledAddressingMode(AM, VT);
4169
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004170 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00004171 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00004172 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004173 case MVT::i1:
4174 case MVT::i8:
4175 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004176 if (Scale < 0) Scale = -Scale;
4177 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004178 return true;
4179 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00004180 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00004181 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00004182 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004183 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004184 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004185 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00004186 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004187
Owen Anderson825b72b2009-08-11 20:47:22 +00004188 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004189 // Note, we allow "void" uses (basically, uses that aren't loads or
4190 // stores), because arm allows folding a scale into many arithmetic
4191 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004192
Chris Lattner37caf8c2007-04-09 23:33:39 +00004193 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00004194 if (Scale & 1) return false;
4195 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00004196 }
4197 break;
Evan Chengb01fad62007-03-12 23:30:29 +00004198 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00004199 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00004200}
4201
Evan Cheng77e47512009-11-11 19:05:52 +00004202/// isLegalICmpImmediate - Return true if the specified immediate is legal
4203/// icmp immediate, that is the target has icmp instructions which can compare
4204/// a register against the immediate without having to materialize the
4205/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00004206bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00004207 if (!Subtarget->isThumb())
4208 return ARM_AM::getSOImmVal(Imm) != -1;
4209 if (Subtarget->isThumb2())
4210 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00004211 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00004212}
4213
Owen Andersone50ed302009-08-10 22:56:29 +00004214static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004215 bool isSEXTLoad, SDValue &Base,
4216 SDValue &Offset, bool &isInc,
4217 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00004218 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4219 return false;
4220
Owen Anderson825b72b2009-08-11 20:47:22 +00004221 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00004222 // AddressingMode 3
4223 Base = Ptr->getOperand(0);
4224 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004225 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004226 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004227 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004228 isInc = false;
4229 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4230 return true;
4231 }
4232 }
4233 isInc = (Ptr->getOpcode() == ISD::ADD);
4234 Offset = Ptr->getOperand(1);
4235 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004236 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00004237 // AddressingMode 2
4238 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004239 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004240 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004241 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004242 isInc = false;
4243 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4244 Base = Ptr->getOperand(0);
4245 return true;
4246 }
4247 }
4248
4249 if (Ptr->getOpcode() == ISD::ADD) {
4250 isInc = true;
4251 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
4252 if (ShOpcVal != ARM_AM::no_shift) {
4253 Base = Ptr->getOperand(1);
4254 Offset = Ptr->getOperand(0);
4255 } else {
4256 Base = Ptr->getOperand(0);
4257 Offset = Ptr->getOperand(1);
4258 }
4259 return true;
4260 }
4261
4262 isInc = (Ptr->getOpcode() == ISD::ADD);
4263 Base = Ptr->getOperand(0);
4264 Offset = Ptr->getOperand(1);
4265 return true;
4266 }
4267
Jim Grosbache5165492009-11-09 00:11:35 +00004268 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00004269 return false;
4270}
4271
Owen Andersone50ed302009-08-10 22:56:29 +00004272static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004273 bool isSEXTLoad, SDValue &Base,
4274 SDValue &Offset, bool &isInc,
4275 SelectionDAG &DAG) {
4276 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4277 return false;
4278
4279 Base = Ptr->getOperand(0);
4280 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
4281 int RHSC = (int)RHS->getZExtValue();
4282 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
4283 assert(Ptr->getOpcode() == ISD::ADD);
4284 isInc = false;
4285 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4286 return true;
4287 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
4288 isInc = Ptr->getOpcode() == ISD::ADD;
4289 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
4290 return true;
4291 }
4292 }
4293
4294 return false;
4295}
4296
Evan Chenga8e29892007-01-19 07:51:42 +00004297/// getPreIndexedAddressParts - returns true by value, base pointer and
4298/// offset pointer and addressing mode by reference if the node's address
4299/// can be legally represented as pre-indexed load / store address.
4300bool
Dan Gohman475871a2008-07-27 21:46:04 +00004301ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
4302 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004303 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004304 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004305 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004306 return false;
4307
Owen Andersone50ed302009-08-10 22:56:29 +00004308 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004309 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004310 bool isSEXTLoad = false;
4311 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4312 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004313 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004314 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4315 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
4316 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004317 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004318 } else
4319 return false;
4320
4321 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004322 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004323 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004324 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
4325 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004326 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004327 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00004328 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00004329 if (!isLegal)
4330 return false;
4331
4332 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
4333 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004334}
4335
4336/// getPostIndexedAddressParts - returns true by value, base pointer and
4337/// offset pointer and addressing mode by reference if this node can be
4338/// combined with a load / store to form a post-indexed load / store.
4339bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00004340 SDValue &Base,
4341 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004342 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004343 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004344 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004345 return false;
4346
Owen Andersone50ed302009-08-10 22:56:29 +00004347 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004348 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004349 bool isSEXTLoad = false;
4350 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004351 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004352 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4353 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004354 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004355 } else
4356 return false;
4357
4358 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004359 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004360 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004361 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004362 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004363 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004364 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
4365 isInc, DAG);
4366 if (!isLegal)
4367 return false;
4368
4369 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
4370 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004371}
4372
Dan Gohman475871a2008-07-27 21:46:04 +00004373void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00004374 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004375 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004376 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00004377 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00004378 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004379 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004380 switch (Op.getOpcode()) {
4381 default: break;
4382 case ARMISD::CMOV: {
4383 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00004384 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004385 if (KnownZero == 0 && KnownOne == 0) return;
4386
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004387 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00004388 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
4389 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004390 KnownZero &= KnownZeroRHS;
4391 KnownOne &= KnownOneRHS;
4392 return;
4393 }
4394 }
4395}
4396
4397//===----------------------------------------------------------------------===//
4398// ARM Inline Assembly Support
4399//===----------------------------------------------------------------------===//
4400
4401/// getConstraintType - Given a constraint letter, return the type of
4402/// constraint it is for this target.
4403ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00004404ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
4405 if (Constraint.size() == 1) {
4406 switch (Constraint[0]) {
4407 default: break;
4408 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004409 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00004410 }
Evan Chenga8e29892007-01-19 07:51:42 +00004411 }
Chris Lattner4234f572007-03-25 02:14:49 +00004412 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00004413}
4414
Bob Wilson2dc4f542009-03-20 22:42:55 +00004415std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00004416ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004417 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004418 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004419 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00004420 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004421 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004422 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004423 return std::make_pair(0U, ARM::tGPRRegisterClass);
4424 else
4425 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004426 case 'r':
4427 return std::make_pair(0U, ARM::GPRRegisterClass);
4428 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004429 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004430 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00004431 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004432 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00004433 if (VT.getSizeInBits() == 128)
4434 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004435 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004436 }
4437 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00004438 if (StringRef("{cc}").equals_lower(Constraint))
4439 return std::make_pair(0U, ARM::CCRRegisterClass);
4440
Evan Chenga8e29892007-01-19 07:51:42 +00004441 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
4442}
4443
4444std::vector<unsigned> ARMTargetLowering::
4445getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004446 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004447 if (Constraint.size() != 1)
4448 return std::vector<unsigned>();
4449
4450 switch (Constraint[0]) { // GCC ARM Constraint Letters
4451 default: break;
4452 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004453 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4454 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4455 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004456 case 'r':
4457 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4458 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4459 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
4460 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004461 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004462 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004463 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
4464 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
4465 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
4466 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
4467 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
4468 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
4469 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
4470 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00004471 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004472 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
4473 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
4474 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
4475 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00004476 if (VT.getSizeInBits() == 128)
4477 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
4478 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004479 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004480 }
4481
4482 return std::vector<unsigned>();
4483}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004484
4485/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
4486/// vector. If it is invalid, don't add anything to Ops.
4487void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
4488 char Constraint,
4489 bool hasMemory,
4490 std::vector<SDValue>&Ops,
4491 SelectionDAG &DAG) const {
4492 SDValue Result(0, 0);
4493
4494 switch (Constraint) {
4495 default: break;
4496 case 'I': case 'J': case 'K': case 'L':
4497 case 'M': case 'N': case 'O':
4498 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
4499 if (!C)
4500 return;
4501
4502 int64_t CVal64 = C->getSExtValue();
4503 int CVal = (int) CVal64;
4504 // None of these constraints allow values larger than 32 bits. Check
4505 // that the value fits in an int.
4506 if (CVal != CVal64)
4507 return;
4508
4509 switch (Constraint) {
4510 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004511 if (Subtarget->isThumb1Only()) {
4512 // This must be a constant between 0 and 255, for ADD
4513 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004514 if (CVal >= 0 && CVal <= 255)
4515 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004516 } else if (Subtarget->isThumb2()) {
4517 // A constant that can be used as an immediate value in a
4518 // data-processing instruction.
4519 if (ARM_AM::getT2SOImmVal(CVal) != -1)
4520 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004521 } else {
4522 // A constant that can be used as an immediate value in a
4523 // data-processing instruction.
4524 if (ARM_AM::getSOImmVal(CVal) != -1)
4525 break;
4526 }
4527 return;
4528
4529 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004530 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004531 // This must be a constant between -255 and -1, for negated ADD
4532 // immediates. This can be used in GCC with an "n" modifier that
4533 // prints the negated value, for use with SUB instructions. It is
4534 // not useful otherwise but is implemented for compatibility.
4535 if (CVal >= -255 && CVal <= -1)
4536 break;
4537 } else {
4538 // This must be a constant between -4095 and 4095. It is not clear
4539 // what this constraint is intended for. Implemented for
4540 // compatibility with GCC.
4541 if (CVal >= -4095 && CVal <= 4095)
4542 break;
4543 }
4544 return;
4545
4546 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004547 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004548 // A 32-bit value where only one byte has a nonzero value. Exclude
4549 // zero to match GCC. This constraint is used by GCC internally for
4550 // constants that can be loaded with a move/shift combination.
4551 // It is not useful otherwise but is implemented for compatibility.
4552 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
4553 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004554 } else if (Subtarget->isThumb2()) {
4555 // A constant whose bitwise inverse can be used as an immediate
4556 // value in a data-processing instruction. This can be used in GCC
4557 // with a "B" modifier that prints the inverted value, for use with
4558 // BIC and MVN instructions. It is not useful otherwise but is
4559 // implemented for compatibility.
4560 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
4561 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004562 } else {
4563 // A constant whose bitwise inverse can be used as an immediate
4564 // value in a data-processing instruction. This can be used in GCC
4565 // with a "B" modifier that prints the inverted value, for use with
4566 // BIC and MVN instructions. It is not useful otherwise but is
4567 // implemented for compatibility.
4568 if (ARM_AM::getSOImmVal(~CVal) != -1)
4569 break;
4570 }
4571 return;
4572
4573 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004574 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004575 // This must be a constant between -7 and 7,
4576 // for 3-operand ADD/SUB immediate instructions.
4577 if (CVal >= -7 && CVal < 7)
4578 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004579 } else if (Subtarget->isThumb2()) {
4580 // A constant whose negation can be used as an immediate value in a
4581 // data-processing instruction. This can be used in GCC with an "n"
4582 // modifier that prints the negated value, for use with SUB
4583 // instructions. It is not useful otherwise but is implemented for
4584 // compatibility.
4585 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
4586 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004587 } else {
4588 // A constant whose negation can be used as an immediate value in a
4589 // data-processing instruction. This can be used in GCC with an "n"
4590 // modifier that prints the negated value, for use with SUB
4591 // instructions. It is not useful otherwise but is implemented for
4592 // compatibility.
4593 if (ARM_AM::getSOImmVal(-CVal) != -1)
4594 break;
4595 }
4596 return;
4597
4598 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004599 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004600 // This must be a multiple of 4 between 0 and 1020, for
4601 // ADD sp + immediate.
4602 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
4603 break;
4604 } else {
4605 // A power of two or a constant between 0 and 32. This is used in
4606 // GCC for the shift amount on shifted register operands, but it is
4607 // useful in general for any shift amounts.
4608 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
4609 break;
4610 }
4611 return;
4612
4613 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004614 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004615 // This must be a constant between 0 and 31, for shift amounts.
4616 if (CVal >= 0 && CVal <= 31)
4617 break;
4618 }
4619 return;
4620
4621 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004622 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004623 // This must be a multiple of 4 between -508 and 508, for
4624 // ADD/SUB sp = sp + immediate.
4625 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
4626 break;
4627 }
4628 return;
4629 }
4630 Result = DAG.getTargetConstant(CVal, Op.getValueType());
4631 break;
4632 }
4633
4634 if (Result.getNode()) {
4635 Ops.push_back(Result);
4636 return;
4637 }
4638 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
4639 Ops, DAG);
4640}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00004641
4642bool
4643ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4644 // The ARM target isn't yet aware of offsets.
4645 return false;
4646}
Evan Cheng39382422009-10-28 01:44:26 +00004647
4648int ARM::getVFPf32Imm(const APFloat &FPImm) {
4649 APInt Imm = FPImm.bitcastToAPInt();
4650 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
4651 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
4652 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
4653
4654 // We can handle 4 bits of mantissa.
4655 // mantissa = (16+UInt(e:f:g:h))/16.
4656 if (Mantissa & 0x7ffff)
4657 return -1;
4658 Mantissa >>= 19;
4659 if ((Mantissa & 0xf) != Mantissa)
4660 return -1;
4661
4662 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4663 if (Exp < -3 || Exp > 4)
4664 return -1;
4665 Exp = ((Exp+3) & 0x7) ^ 4;
4666
4667 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4668}
4669
4670int ARM::getVFPf64Imm(const APFloat &FPImm) {
4671 APInt Imm = FPImm.bitcastToAPInt();
4672 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
4673 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
4674 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
4675
4676 // We can handle 4 bits of mantissa.
4677 // mantissa = (16+UInt(e:f:g:h))/16.
4678 if (Mantissa & 0xffffffffffffLL)
4679 return -1;
4680 Mantissa >>= 48;
4681 if ((Mantissa & 0xf) != Mantissa)
4682 return -1;
4683
4684 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4685 if (Exp < -3 || Exp > 4)
4686 return -1;
4687 Exp = ((Exp+3) & 0x7) ^ 4;
4688
4689 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4690}
4691
4692/// isFPImmLegal - Returns true if the target can instruction select the
4693/// specified FP immediate natively. If false, the legalizer will
4694/// materialize the FP immediate as a load from a constant pool.
4695bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
4696 if (!Subtarget->hasVFP3())
4697 return false;
4698 if (VT == MVT::f32)
4699 return ARM::getVFPf32Imm(Imm) != -1;
4700 if (VT == MVT::f64)
4701 return ARM::getVFPf64Imm(Imm) != -1;
4702 return false;
4703}