blob: c5a6a8e1191586fd0b5c58fa1f48fdb6135a4315 [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "ARM.h"
16#include "ARMAddressingModes.h"
17#include "ARMConstantPoolValue.h"
18#include "ARMISelLowering.h"
19#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000020#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000021#include "ARMRegisterInfo.h"
22#include "ARMSubtarget.h"
23#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000024#include "ARMTargetObjectFile.h"
Evan Chenga8e29892007-01-19 07:51:42 +000025#include "llvm/CallingConv.h"
26#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000027#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000028#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000029#include "llvm/Instruction.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000030#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000031#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000032#include "llvm/CodeGen/CallingConvLower.h"
Evan Chenga8e29892007-01-19 07:51:42 +000033#include "llvm/CodeGen/MachineBasicBlock.h"
34#include "llvm/CodeGen/MachineFrameInfo.h"
35#include "llvm/CodeGen/MachineFunction.h"
36#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000039#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000040#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000041#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000042#include "llvm/ADT/VectorExtras.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000043#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000044#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000045#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000046#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000047#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000048using namespace llvm;
49
Jim Grosbache7b52522010-04-14 22:28:31 +000050static cl::opt<bool>
51EnableARMLongCalls("arm-long-calls", cl::Hidden,
52 cl::desc("Generate calls via indirect call instructions."),
53 cl::init(false));
54
Owen Andersone50ed302009-08-10 22:56:29 +000055static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000056 CCValAssign::LocInfo &LocInfo,
57 ISD::ArgFlagsTy &ArgFlags,
58 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000059static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000060 CCValAssign::LocInfo &LocInfo,
61 ISD::ArgFlagsTy &ArgFlags,
62 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000063static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000064 CCValAssign::LocInfo &LocInfo,
65 ISD::ArgFlagsTy &ArgFlags,
66 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000067static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000068 CCValAssign::LocInfo &LocInfo,
69 ISD::ArgFlagsTy &ArgFlags,
70 CCState &State);
71
Owen Andersone50ed302009-08-10 22:56:29 +000072void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
73 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000074 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000075 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000076 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
77 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000078
Owen Anderson70671842009-08-10 20:18:46 +000079 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000080 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000081 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000082 }
83
Owen Andersone50ed302009-08-10 22:56:29 +000084 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +000085 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +000086 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +000087 if (ElemTy == MVT::i8 || ElemTy == MVT::i16)
Owen Anderson70671842009-08-10 20:18:46 +000088 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +000089 if (ElemTy != MVT::i32) {
90 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
91 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
92 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
93 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
94 }
Owen Anderson70671842009-08-10 20:18:46 +000095 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
96 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Evan Chengde8aa4e2010-05-05 18:28:36 +000097 if (llvm::ModelWithRegSequence())
98 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
99 else
100 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Custom);
Anton Korobeynikov8e6c2b92009-08-21 12:40:35 +0000101 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
Bob Wilsond0910c42010-04-06 22:02:24 +0000102 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
103 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000104 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000105 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
106 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
107 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000108 }
109
110 // Promote all bit-wise operations.
111 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000112 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000113 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
114 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000115 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000116 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000117 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000118 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000119 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000120 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000121 }
Bob Wilson16330762009-09-16 00:17:28 +0000122
123 // Neon does not support vector divide/remainder operations.
124 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
125 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
126 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
127 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
128 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
129 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000130}
131
Owen Andersone50ed302009-08-10 22:56:29 +0000132void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000133 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000135}
136
Owen Andersone50ed302009-08-10 22:56:29 +0000137void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000138 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000140}
141
Chris Lattnerf0144122009-07-28 03:13:23 +0000142static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
143 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000144 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000145
Chris Lattner80ec2792009-08-02 00:34:36 +0000146 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000147}
148
Evan Chenga8e29892007-01-19 07:51:42 +0000149ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000150 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000151 Subtarget = &TM.getSubtarget<ARMSubtarget>();
152
Evan Chengb1df8f22007-04-27 08:15:43 +0000153 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000154 // Uses VFP for Thumb libfuncs if available.
155 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
156 // Single-precision floating-point arithmetic.
157 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
158 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
159 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
160 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000161
Evan Chengb1df8f22007-04-27 08:15:43 +0000162 // Double-precision floating-point arithmetic.
163 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
164 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
165 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
166 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000167
Evan Chengb1df8f22007-04-27 08:15:43 +0000168 // Single-precision comparisons.
169 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
170 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
171 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
172 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
173 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
174 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
175 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
176 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000177
Evan Chengb1df8f22007-04-27 08:15:43 +0000178 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
179 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
180 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
181 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
182 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
183 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
184 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
185 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000186
Evan Chengb1df8f22007-04-27 08:15:43 +0000187 // Double-precision comparisons.
188 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
189 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
190 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
191 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
192 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
193 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
194 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
195 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000196
Evan Chengb1df8f22007-04-27 08:15:43 +0000197 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
198 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
199 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
200 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
201 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
202 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
203 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
204 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000205
Evan Chengb1df8f22007-04-27 08:15:43 +0000206 // Floating-point to integer conversions.
207 // i64 conversions are done via library routines even when generating VFP
208 // instructions, so use the same ones.
209 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
210 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
211 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
212 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000213
Evan Chengb1df8f22007-04-27 08:15:43 +0000214 // Conversions between floating types.
215 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
216 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
217
218 // Integer to floating-point conversions.
219 // i64 conversions are done via library routines even when generating VFP
220 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000221 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
222 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000223 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
224 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
225 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
226 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
227 }
Evan Chenga8e29892007-01-19 07:51:42 +0000228 }
229
Bob Wilson2f954612009-05-22 17:38:41 +0000230 // These libcalls are not available in 32-bit.
231 setLibcallName(RTLIB::SHL_I128, 0);
232 setLibcallName(RTLIB::SRL_I128, 0);
233 setLibcallName(RTLIB::SRA_I128, 0);
234
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000235 // Libcalls should use the AAPCS base standard ABI, even if hard float
236 // is in effect, as per the ARM RTABI specification, section 4.1.2.
237 if (Subtarget->isAAPCS_ABI()) {
238 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
239 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
240 CallingConv::ARM_AAPCS);
241 }
242 }
243
David Goodwinf1daf7d2009-07-08 23:10:31 +0000244 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000245 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000246 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000247 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000248 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
250 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000251
Owen Anderson825b72b2009-08-11 20:47:22 +0000252 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000253 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000254
255 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000256 addDRTypeForNEON(MVT::v2f32);
257 addDRTypeForNEON(MVT::v8i8);
258 addDRTypeForNEON(MVT::v4i16);
259 addDRTypeForNEON(MVT::v2i32);
260 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000261
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 addQRTypeForNEON(MVT::v4f32);
263 addQRTypeForNEON(MVT::v2f64);
264 addQRTypeForNEON(MVT::v16i8);
265 addQRTypeForNEON(MVT::v8i16);
266 addQRTypeForNEON(MVT::v4i32);
267 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000268
Bob Wilson74dc72e2009-09-15 23:55:57 +0000269 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
270 // neither Neon nor VFP support any arithmetic operations on it.
271 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
272 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
273 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
274 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
275 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
276 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
277 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
278 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
279 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
280 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
281 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
282 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
283 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
284 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
285 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
286 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
287 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
288 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
289 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
290 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
291 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
292 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
293 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
294 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
295
Bob Wilson642b3292009-09-16 00:32:15 +0000296 // Neon does not support some operations on v1i64 and v2i64 types.
297 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
298 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
299 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
300 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
301
Bob Wilson5bafff32009-06-22 23:27:02 +0000302 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
303 setTargetDAGCombine(ISD::SHL);
304 setTargetDAGCombine(ISD::SRL);
305 setTargetDAGCombine(ISD::SRA);
306 setTargetDAGCombine(ISD::SIGN_EXTEND);
307 setTargetDAGCombine(ISD::ZERO_EXTEND);
308 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000309 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson5bafff32009-06-22 23:27:02 +0000310 }
311
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000312 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000313
314 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000315 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000316
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000317 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000319
Evan Chenga8e29892007-01-19 07:51:42 +0000320 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000321 if (!Subtarget->isThumb1Only()) {
322 for (unsigned im = (unsigned)ISD::PRE_INC;
323 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setIndexedLoadAction(im, MVT::i1, Legal);
325 setIndexedLoadAction(im, MVT::i8, Legal);
326 setIndexedLoadAction(im, MVT::i16, Legal);
327 setIndexedLoadAction(im, MVT::i32, Legal);
328 setIndexedStoreAction(im, MVT::i1, Legal);
329 setIndexedStoreAction(im, MVT::i8, Legal);
330 setIndexedStoreAction(im, MVT::i16, Legal);
331 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000332 }
Evan Chenga8e29892007-01-19 07:51:42 +0000333 }
334
335 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000336 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 setOperationAction(ISD::MUL, MVT::i64, Expand);
338 setOperationAction(ISD::MULHU, MVT::i32, Expand);
339 setOperationAction(ISD::MULHS, MVT::i32, Expand);
340 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
341 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000342 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 setOperationAction(ISD::MUL, MVT::i64, Expand);
344 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000345 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000346 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000347 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000348 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000349 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000350 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000351 setOperationAction(ISD::SRL, MVT::i64, Custom);
352 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000353
354 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000356 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000357 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000358 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000359 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000360
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000361 // Only ARMv6 has BSWAP.
362 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000363 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000364
Evan Chenga8e29892007-01-19 07:51:42 +0000365 // These are expanded into libcalls.
Jim Grosbach29402132010-05-05 23:44:43 +0000366 if (!Subtarget->hasDivide()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000367 // v7M has a hardware divider
368 setOperationAction(ISD::SDIV, MVT::i32, Expand);
369 setOperationAction(ISD::UDIV, MVT::i32, Expand);
370 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 setOperationAction(ISD::SREM, MVT::i32, Expand);
372 setOperationAction(ISD::UREM, MVT::i32, Expand);
373 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
374 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000375
Owen Anderson825b72b2009-08-11 20:47:22 +0000376 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
377 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
378 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
379 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000380 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000381
Evan Chengfb3611d2010-05-11 07:26:32 +0000382 setOperationAction(ISD::TRAP, MVT::Other, Legal);
383
Evan Chenga8e29892007-01-19 07:51:42 +0000384 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 setOperationAction(ISD::VASTART, MVT::Other, Custom);
386 setOperationAction(ISD::VAARG, MVT::Other, Expand);
387 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
388 setOperationAction(ISD::VAEND, MVT::Other, Expand);
389 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
390 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000391 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
392 // FIXME: Shouldn't need this, since no register is used, but the legalizer
393 // doesn't yet know how to not do that for SjLj.
394 setExceptionSelectorRegister(ARM::R0);
Evan Cheng3a1588a2010-04-15 22:20:34 +0000395 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Jim Grosbach3728e962009-12-10 00:11:09 +0000396 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000397
Jim Grosbach4b77f6a2010-05-07 18:34:55 +0000398 // If the subtarget does not have extract instructions, sign_extend_inreg
399 // needs to be expanded. Extract is available in ARM mode on v6 and up,
400 // and on most Thumb2 implementations.
401 if ((!Subtarget->isThumb() && !Subtarget->hasV6Ops())
402 || (Subtarget->isThumb2() && !Subtarget->hasT2ExtractPack())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000403 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
404 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000405 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000406 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000407
David Goodwinf1daf7d2009-07-08 23:10:31 +0000408 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000409 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
410 // iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000412
413 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000415
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::SETCC, MVT::i32, Expand);
417 setOperationAction(ISD::SETCC, MVT::f32, Expand);
418 setOperationAction(ISD::SETCC, MVT::f64, Expand);
419 setOperationAction(ISD::SELECT, MVT::i32, Expand);
420 setOperationAction(ISD::SELECT, MVT::f32, Expand);
421 setOperationAction(ISD::SELECT, MVT::f64, Expand);
422 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
423 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
424 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000425
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
427 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
428 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
429 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
430 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000431
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000432 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setOperationAction(ISD::FSIN, MVT::f64, Expand);
434 setOperationAction(ISD::FSIN, MVT::f32, Expand);
435 setOperationAction(ISD::FCOS, MVT::f32, Expand);
436 setOperationAction(ISD::FCOS, MVT::f64, Expand);
437 setOperationAction(ISD::FREM, MVT::f64, Expand);
438 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000439 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000440 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
441 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000442 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setOperationAction(ISD::FPOW, MVT::f64, Expand);
444 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000445
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000446 // Various VFP goodness
447 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000448 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
449 if (Subtarget->hasVFP2()) {
450 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
451 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
452 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
453 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
454 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000455 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000456 if (!Subtarget->hasFP16()) {
457 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
458 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000459 }
Evan Cheng110cf482008-04-01 01:50:16 +0000460 }
Evan Chenga8e29892007-01-19 07:51:42 +0000461
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000462 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000463 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000464 setTargetDAGCombine(ISD::ADD);
465 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000466 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000467
Evan Chenga8e29892007-01-19 07:51:42 +0000468 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000469
Evan Chengf7d87ee2010-05-21 00:43:17 +0000470 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
471 setSchedulingPreference(Sched::RegPressure);
472 else
473 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000474
Evan Chengbc9b7542009-08-15 07:59:10 +0000475 // FIXME: If-converter should use instruction latency to determine
476 // profitability rather than relying on fixed limits.
477 if (Subtarget->getCPUString() == "generic") {
478 // Generic (and overly aggressive) if-conversion limits.
479 setIfCvtBlockSizeLimit(10);
480 setIfCvtDupBlockSizeLimit(2);
Jim Grosbach35075a72010-03-24 16:15:14 +0000481 } else if (Subtarget->hasV7Ops()) {
Jim Grosbachfceabef2010-03-24 00:03:13 +0000482 setIfCvtBlockSizeLimit(3);
483 setIfCvtDupBlockSizeLimit(1);
Evan Chengbc9b7542009-08-15 07:59:10 +0000484 } else if (Subtarget->hasV6Ops()) {
485 setIfCvtBlockSizeLimit(2);
486 setIfCvtDupBlockSizeLimit(1);
487 } else {
488 setIfCvtBlockSizeLimit(3);
489 setIfCvtDupBlockSizeLimit(2);
Evan Cheng8557c2b2009-06-19 01:51:50 +0000490 }
491
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000492 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Bob Wilsone6abdff2009-05-18 20:55:32 +0000493 // Do not enable CodePlacementOpt for now: it currently runs after the
494 // ARMConstantIslandPass and messes up branch relaxation and placement
495 // of constant islands.
496 // benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000497}
498
Evan Chenga8e29892007-01-19 07:51:42 +0000499const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
500 switch (Opcode) {
501 default: return 0;
502 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000503 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
504 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000505 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000506 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
507 case ARMISD::tCALL: return "ARMISD::tCALL";
508 case ARMISD::BRCOND: return "ARMISD::BRCOND";
509 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000510 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000511 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
512 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
513 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000514 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000515 case ARMISD::CMPFP: return "ARMISD::CMPFP";
516 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
517 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
518 case ARMISD::CMOV: return "ARMISD::CMOV";
519 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000520
Jim Grosbach3482c802010-01-18 19:58:49 +0000521 case ARMISD::RBIT: return "ARMISD::RBIT";
522
Bob Wilson76a312b2010-03-19 22:51:32 +0000523 case ARMISD::FTOSI: return "ARMISD::FTOSI";
524 case ARMISD::FTOUI: return "ARMISD::FTOUI";
525 case ARMISD::SITOF: return "ARMISD::SITOF";
526 case ARMISD::UITOF: return "ARMISD::UITOF";
527
Evan Chenga8e29892007-01-19 07:51:42 +0000528 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
529 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
530 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000531
Jim Grosbache5165492009-11-09 00:11:35 +0000532 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
533 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000534
Evan Chengc5942082009-10-28 06:55:03 +0000535 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
536 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
537
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000538 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000539
Evan Cheng86198642009-08-07 00:34:42 +0000540 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
541
Jim Grosbach3728e962009-12-10 00:11:09 +0000542 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
543 case ARMISD::SYNCBARRIER: return "ARMISD::SYNCBARRIER";
544
Bob Wilson5bafff32009-06-22 23:27:02 +0000545 case ARMISD::VCEQ: return "ARMISD::VCEQ";
546 case ARMISD::VCGE: return "ARMISD::VCGE";
547 case ARMISD::VCGEU: return "ARMISD::VCGEU";
548 case ARMISD::VCGT: return "ARMISD::VCGT";
549 case ARMISD::VCGTU: return "ARMISD::VCGTU";
550 case ARMISD::VTST: return "ARMISD::VTST";
551
552 case ARMISD::VSHL: return "ARMISD::VSHL";
553 case ARMISD::VSHRs: return "ARMISD::VSHRs";
554 case ARMISD::VSHRu: return "ARMISD::VSHRu";
555 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
556 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
557 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
558 case ARMISD::VSHRN: return "ARMISD::VSHRN";
559 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
560 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
561 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
562 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
563 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
564 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
565 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
566 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
567 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
568 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
569 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
570 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
571 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
572 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000573 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000574 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000575 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000576 case ARMISD::VREV64: return "ARMISD::VREV64";
577 case ARMISD::VREV32: return "ARMISD::VREV32";
578 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000579 case ARMISD::VZIP: return "ARMISD::VZIP";
580 case ARMISD::VUZP: return "ARMISD::VUZP";
581 case ARMISD::VTRN: return "ARMISD::VTRN";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000582 case ARMISD::FMAX: return "ARMISD::FMAX";
583 case ARMISD::FMIN: return "ARMISD::FMIN";
Evan Chenga8e29892007-01-19 07:51:42 +0000584 }
585}
586
Evan Cheng06b666c2010-05-15 02:18:07 +0000587/// getRegClassFor - Return the register class that should be used for the
588/// specified value type.
589TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
590 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
591 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
592 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000593 if (Subtarget->hasNEON()) {
594 if (VT == MVT::v4i64)
595 return ARM::QQPRRegisterClass;
596 else if (VT == MVT::v8i64)
597 return ARM::QQQQPRRegisterClass;
598 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000599 return TargetLowering::getRegClassFor(VT);
600}
601
Bill Wendlingb4202b82009-07-01 18:50:55 +0000602/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000603unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Evan Cheng048e36f2009-10-02 06:57:25 +0000604 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 0 : 1;
Bill Wendling20c568f2009-06-30 22:38:32 +0000605}
606
Evan Cheng1cc39842010-05-20 23:26:43 +0000607Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
608 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
609 EVT VT = N->getValueType(i);
610 if (VT.isFloatingPoint() || VT.isVector())
611 return Sched::Latency;
612 }
613 return Sched::RegPressure;
614}
615
Evan Chenga8e29892007-01-19 07:51:42 +0000616//===----------------------------------------------------------------------===//
617// Lowering Code
618//===----------------------------------------------------------------------===//
619
Evan Chenga8e29892007-01-19 07:51:42 +0000620/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
621static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
622 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000623 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000624 case ISD::SETNE: return ARMCC::NE;
625 case ISD::SETEQ: return ARMCC::EQ;
626 case ISD::SETGT: return ARMCC::GT;
627 case ISD::SETGE: return ARMCC::GE;
628 case ISD::SETLT: return ARMCC::LT;
629 case ISD::SETLE: return ARMCC::LE;
630 case ISD::SETUGT: return ARMCC::HI;
631 case ISD::SETUGE: return ARMCC::HS;
632 case ISD::SETULT: return ARMCC::LO;
633 case ISD::SETULE: return ARMCC::LS;
634 }
635}
636
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000637/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
638static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000639 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000640 CondCode2 = ARMCC::AL;
641 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000642 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000643 case ISD::SETEQ:
644 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
645 case ISD::SETGT:
646 case ISD::SETOGT: CondCode = ARMCC::GT; break;
647 case ISD::SETGE:
648 case ISD::SETOGE: CondCode = ARMCC::GE; break;
649 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000650 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000651 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
652 case ISD::SETO: CondCode = ARMCC::VC; break;
653 case ISD::SETUO: CondCode = ARMCC::VS; break;
654 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
655 case ISD::SETUGT: CondCode = ARMCC::HI; break;
656 case ISD::SETUGE: CondCode = ARMCC::PL; break;
657 case ISD::SETLT:
658 case ISD::SETULT: CondCode = ARMCC::LT; break;
659 case ISD::SETLE:
660 case ISD::SETULE: CondCode = ARMCC::LE; break;
661 case ISD::SETNE:
662 case ISD::SETUNE: CondCode = ARMCC::NE; break;
663 }
Evan Chenga8e29892007-01-19 07:51:42 +0000664}
665
Bob Wilson1f595bb2009-04-17 19:07:39 +0000666//===----------------------------------------------------------------------===//
667// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000668//===----------------------------------------------------------------------===//
669
670#include "ARMGenCallingConv.inc"
671
672// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000673static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000674 CCValAssign::LocInfo &LocInfo,
675 CCState &State, bool CanFail) {
676 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
677
678 // Try to get the first register.
679 if (unsigned Reg = State.AllocateReg(RegList, 4))
680 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
681 else {
682 // For the 2nd half of a v2f64, do not fail.
683 if (CanFail)
684 return false;
685
686 // Put the whole thing on the stack.
687 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
688 State.AllocateStack(8, 4),
689 LocVT, LocInfo));
690 return true;
691 }
692
693 // Try to get the second register.
694 if (unsigned Reg = State.AllocateReg(RegList, 4))
695 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
696 else
697 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
698 State.AllocateStack(4, 4),
699 LocVT, LocInfo));
700 return true;
701}
702
Owen Andersone50ed302009-08-10 22:56:29 +0000703static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000704 CCValAssign::LocInfo &LocInfo,
705 ISD::ArgFlagsTy &ArgFlags,
706 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000707 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
708 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000709 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000710 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
711 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000712 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000713}
714
715// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000716static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000717 CCValAssign::LocInfo &LocInfo,
718 CCState &State, bool CanFail) {
719 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
720 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
721
722 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
723 if (Reg == 0) {
724 // For the 2nd half of a v2f64, do not just fail.
725 if (CanFail)
726 return false;
727
728 // Put the whole thing on the stack.
729 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
730 State.AllocateStack(8, 8),
731 LocVT, LocInfo));
732 return true;
733 }
734
735 unsigned i;
736 for (i = 0; i < 2; ++i)
737 if (HiRegList[i] == Reg)
738 break;
739
740 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
741 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
742 LocVT, LocInfo));
743 return true;
744}
745
Owen Andersone50ed302009-08-10 22:56:29 +0000746static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000747 CCValAssign::LocInfo &LocInfo,
748 ISD::ArgFlagsTy &ArgFlags,
749 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000750 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
751 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000752 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000753 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
754 return false;
755 return true; // we handled it
756}
757
Owen Andersone50ed302009-08-10 22:56:29 +0000758static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000759 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000760 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
761 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
762
Bob Wilsone65586b2009-04-17 20:40:45 +0000763 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
764 if (Reg == 0)
765 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000766
Bob Wilsone65586b2009-04-17 20:40:45 +0000767 unsigned i;
768 for (i = 0; i < 2; ++i)
769 if (HiRegList[i] == Reg)
770 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000771
Bob Wilson5bafff32009-06-22 23:27:02 +0000772 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000773 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000774 LocVT, LocInfo));
775 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000776}
777
Owen Andersone50ed302009-08-10 22:56:29 +0000778static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000779 CCValAssign::LocInfo &LocInfo,
780 ISD::ArgFlagsTy &ArgFlags,
781 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000782 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
783 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000784 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000785 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000786 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000787}
788
Owen Andersone50ed302009-08-10 22:56:29 +0000789static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000790 CCValAssign::LocInfo &LocInfo,
791 ISD::ArgFlagsTy &ArgFlags,
792 CCState &State) {
793 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
794 State);
795}
796
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000797/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
798/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000799CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000800 bool Return,
801 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000802 switch (CC) {
803 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000804 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000805 case CallingConv::C:
806 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000807 // Use target triple & subtarget features to do actual dispatch.
808 if (Subtarget->isAAPCS_ABI()) {
809 if (Subtarget->hasVFP2() &&
810 FloatABIType == FloatABI::Hard && !isVarArg)
811 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
812 else
813 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
814 } else
815 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000816 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000817 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000818 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000819 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000820 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000821 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000822 }
823}
824
Dan Gohman98ca4f22009-08-05 01:29:28 +0000825/// LowerCallResult - Lower the result values of a call into the
826/// appropriate copies out of appropriate physical registers.
827SDValue
828ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000829 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000830 const SmallVectorImpl<ISD::InputArg> &Ins,
831 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000832 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000833
Bob Wilson1f595bb2009-04-17 19:07:39 +0000834 // Assign locations to each value returned by this call.
835 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000836 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000837 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000838 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000839 CCAssignFnForNode(CallConv, /* Return*/ true,
840 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000841
842 // Copy all of the result registers out of their specified physreg.
843 for (unsigned i = 0; i != RVLocs.size(); ++i) {
844 CCValAssign VA = RVLocs[i];
845
Bob Wilson80915242009-04-25 00:33:20 +0000846 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000847 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000848 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000849 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000850 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000851 Chain = Lo.getValue(1);
852 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000853 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000854 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000855 InFlag);
856 Chain = Hi.getValue(1);
857 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000858 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000859
Owen Anderson825b72b2009-08-11 20:47:22 +0000860 if (VA.getLocVT() == MVT::v2f64) {
861 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
862 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
863 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000864
865 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000866 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000867 Chain = Lo.getValue(1);
868 InFlag = Lo.getValue(2);
869 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000870 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000871 Chain = Hi.getValue(1);
872 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000873 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +0000874 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
875 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000876 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000877 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000878 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
879 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000880 Chain = Val.getValue(1);
881 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000882 }
Bob Wilson80915242009-04-25 00:33:20 +0000883
884 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000885 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000886 case CCValAssign::Full: break;
887 case CCValAssign::BCvt:
888 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
889 break;
890 }
891
Dan Gohman98ca4f22009-08-05 01:29:28 +0000892 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000893 }
894
Dan Gohman98ca4f22009-08-05 01:29:28 +0000895 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000896}
897
898/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
899/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000900/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000901/// a byval function parameter.
902/// Sometimes what we are copying is the end of a larger object, the part that
903/// does not fit in registers.
904static SDValue
905CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
906 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
907 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000908 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000909 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +0000910 /*isVolatile=*/false, /*AlwaysInline=*/false,
911 NULL, 0, NULL, 0);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000912}
913
Bob Wilsondee46d72009-04-17 20:35:10 +0000914/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000915SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000916ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
917 SDValue StackPtr, SDValue Arg,
918 DebugLoc dl, SelectionDAG &DAG,
919 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +0000920 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000921 unsigned LocMemOffset = VA.getLocMemOffset();
922 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
923 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
924 if (Flags.isByVal()) {
925 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
926 }
927 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene1b58cab2010-02-15 16:55:24 +0000928 PseudoSourceValue::getStack(), LocMemOffset,
929 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +0000930}
931
Dan Gohman98ca4f22009-08-05 01:29:28 +0000932void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000933 SDValue Chain, SDValue &Arg,
934 RegsToPassVector &RegsToPass,
935 CCValAssign &VA, CCValAssign &NextVA,
936 SDValue &StackPtr,
937 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +0000938 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +0000939
Jim Grosbache5165492009-11-09 00:11:35 +0000940 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000941 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000942 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
943
944 if (NextVA.isRegLoc())
945 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
946 else {
947 assert(NextVA.isMemLoc());
948 if (StackPtr.getNode() == 0)
949 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
950
Dan Gohman98ca4f22009-08-05 01:29:28 +0000951 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
952 dl, DAG, NextVA,
953 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000954 }
955}
956
Dan Gohman98ca4f22009-08-05 01:29:28 +0000957/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000958/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
959/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000960SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000961ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000962 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000963 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000964 const SmallVectorImpl<ISD::OutputArg> &Outs,
965 const SmallVectorImpl<ISD::InputArg> &Ins,
966 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000967 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +0000968 // ARM target does not yet support tail call optimization.
969 isTailCall = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000970
Bob Wilson1f595bb2009-04-17 19:07:39 +0000971 // Analyze operands of the call, assigning locations to each operand.
972 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000973 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
974 *DAG.getContext());
975 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000976 CCAssignFnForNode(CallConv, /* Return*/ false,
977 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +0000978
Bob Wilson1f595bb2009-04-17 19:07:39 +0000979 // Get a count of how many bytes are to be pushed on the stack.
980 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000981
982 // Adjust the stack pointer for the new arguments...
983 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000984 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000985
Jim Grosbachf9a4b762010-02-24 01:43:03 +0000986 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000987
Bob Wilson5bafff32009-06-22 23:27:02 +0000988 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000989 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000990
Bob Wilson1f595bb2009-04-17 19:07:39 +0000991 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +0000992 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000993 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
994 i != e;
995 ++i, ++realArgIdx) {
996 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000997 SDValue Arg = Outs[realArgIdx].Val;
998 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +0000999
Bob Wilson1f595bb2009-04-17 19:07:39 +00001000 // Promote the value if needed.
1001 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001002 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001003 case CCValAssign::Full: break;
1004 case CCValAssign::SExt:
1005 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1006 break;
1007 case CCValAssign::ZExt:
1008 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1009 break;
1010 case CCValAssign::AExt:
1011 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1012 break;
1013 case CCValAssign::BCvt:
1014 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1015 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001016 }
1017
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001018 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001019 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001020 if (VA.getLocVT() == MVT::v2f64) {
1021 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1022 DAG.getConstant(0, MVT::i32));
1023 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1024 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001025
Dan Gohman98ca4f22009-08-05 01:29:28 +00001026 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001027 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1028
1029 VA = ArgLocs[++i]; // skip ahead to next loc
1030 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001031 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001032 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1033 } else {
1034 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001035
Dan Gohman98ca4f22009-08-05 01:29:28 +00001036 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1037 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001038 }
1039 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001040 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001041 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001042 }
1043 } else if (VA.isRegLoc()) {
1044 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1045 } else {
1046 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001047
Dan Gohman98ca4f22009-08-05 01:29:28 +00001048 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1049 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001050 }
Evan Chenga8e29892007-01-19 07:51:42 +00001051 }
1052
1053 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001054 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001055 &MemOpChains[0], MemOpChains.size());
1056
1057 // Build a sequence of copy-to-reg nodes chained together with token chain
1058 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001059 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +00001060 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001061 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001062 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +00001063 InFlag = Chain.getValue(1);
1064 }
1065
Bill Wendling056292f2008-09-16 21:48:12 +00001066 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1067 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1068 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001069 bool isDirect = false;
1070 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001071 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001072 MachineFunction &MF = DAG.getMachineFunction();
1073 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001074
1075 if (EnableARMLongCalls) {
1076 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1077 && "long-calls with non-static relocation model!");
1078 // Handle a global address or an external symbol. If it's not one of
1079 // those, the target's already in a register, so we don't need to do
1080 // anything extra.
1081 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001082 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001083 // Create a constant pool entry for the callee address
1084 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1085 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1086 ARMPCLabelIndex,
1087 ARMCP::CPValue, 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 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1096 const char *Sym = S->getSymbol();
1097
1098 // Create a constant pool entry for the callee address
1099 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1100 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1101 Sym, ARMPCLabelIndex, 0);
1102 // Get the address of the callee into a register
1103 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1104 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1105 Callee = DAG.getLoad(getPointerTy(), dl,
1106 DAG.getEntryNode(), CPAddr,
1107 PseudoSourceValue::getConstantPool(), 0,
1108 false, false, 0);
1109 }
1110 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001111 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001112 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001113 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001114 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001115 getTargetMachine().getRelocationModel() != Reloc::Static;
1116 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001117 // ARM call to a local ARM function is predicable.
1118 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +00001119 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001120 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001121 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001122 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001123 ARMPCLabelIndex,
1124 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001125 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001126 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001127 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001128 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001129 PseudoSourceValue::getConstantPool(), 0,
1130 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001131 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001132 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001133 getPointerTy(), Callee, PICLabel);
Jim Grosbache7b52522010-04-14 22:28:31 +00001134 } else
Evan Chengc60e76d2007-01-30 20:37:08 +00001135 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001136 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001137 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001138 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001139 getTargetMachine().getRelocationModel() != Reloc::Static;
1140 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001141 // tBX takes a register source operand.
1142 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001143 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001144 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001145 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001146 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001147 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001148 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001149 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001150 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001151 PseudoSourceValue::getConstantPool(), 0,
1152 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001153 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001154 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001155 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001156 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001157 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001158 }
1159
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001160 // FIXME: handle tail calls differently.
1161 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001162 if (Subtarget->isThumb()) {
1163 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001164 CallOpc = ARMISD::CALL_NOLINK;
1165 else
1166 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1167 } else {
1168 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001169 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1170 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001171 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001172 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001173 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001174 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001175 InFlag = Chain.getValue(1);
1176 }
1177
Dan Gohman475871a2008-07-27 21:46:04 +00001178 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001179 Ops.push_back(Chain);
1180 Ops.push_back(Callee);
1181
1182 // Add argument registers to the end of the list so that they are known live
1183 // into the call.
1184 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1185 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1186 RegsToPass[i].second.getValueType()));
1187
Gabor Greifba36cb52008-08-28 21:40:38 +00001188 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001189 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +00001190 // Returns a chain and a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00001191 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +00001192 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001193 InFlag = Chain.getValue(1);
1194
Chris Lattnere563bbc2008-10-11 22:08:30 +00001195 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1196 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001197 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001198 InFlag = Chain.getValue(1);
1199
Bob Wilson1f595bb2009-04-17 19:07:39 +00001200 // Handle result values, copying them out of physregs into vregs that we
1201 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001202 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1203 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001204}
1205
Dan Gohman98ca4f22009-08-05 01:29:28 +00001206SDValue
1207ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001208 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001209 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmand858e902010-04-17 15:26:15 +00001210 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001211
Bob Wilsondee46d72009-04-17 20:35:10 +00001212 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001213 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001214
Bob Wilsondee46d72009-04-17 20:35:10 +00001215 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001216 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1217 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001218
Dan Gohman98ca4f22009-08-05 01:29:28 +00001219 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001220 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1221 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001222
1223 // If this is the first return lowered for this function, add
1224 // the regs to the liveout set for the function.
1225 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1226 for (unsigned i = 0; i != RVLocs.size(); ++i)
1227 if (RVLocs[i].isRegLoc())
1228 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001229 }
1230
Bob Wilson1f595bb2009-04-17 19:07:39 +00001231 SDValue Flag;
1232
1233 // Copy the result values into the output registers.
1234 for (unsigned i = 0, realRVLocIdx = 0;
1235 i != RVLocs.size();
1236 ++i, ++realRVLocIdx) {
1237 CCValAssign &VA = RVLocs[i];
1238 assert(VA.isRegLoc() && "Can only return in registers!");
1239
Dan Gohman98ca4f22009-08-05 01:29:28 +00001240 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001241
1242 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001243 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001244 case CCValAssign::Full: break;
1245 case CCValAssign::BCvt:
1246 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1247 break;
1248 }
1249
Bob Wilson1f595bb2009-04-17 19:07:39 +00001250 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001251 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001252 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001253 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1254 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001255 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001256 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001257
1258 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1259 Flag = Chain.getValue(1);
1260 VA = RVLocs[++i]; // skip ahead to next loc
1261 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1262 HalfGPRs.getValue(1), Flag);
1263 Flag = Chain.getValue(1);
1264 VA = RVLocs[++i]; // skip ahead to next loc
1265
1266 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001267 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1268 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001269 }
1270 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1271 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001272 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001273 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001274 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001275 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001276 VA = RVLocs[++i]; // skip ahead to next loc
1277 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1278 Flag);
1279 } else
1280 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1281
Bob Wilsondee46d72009-04-17 20:35:10 +00001282 // Guarantee that all emitted copies are
1283 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001284 Flag = Chain.getValue(1);
1285 }
1286
1287 SDValue result;
1288 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001289 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001290 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001291 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001292
1293 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001294}
1295
Bob Wilsonb62d2572009-11-03 00:02:05 +00001296// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1297// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1298// one of the above mentioned nodes. It has to be wrapped because otherwise
1299// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1300// be used to form addressing mode. These wrapped nodes will be selected
1301// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001302static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001303 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001304 // FIXME there is no actual debug info here
1305 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001306 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001307 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001308 if (CP->isMachineConstantPoolEntry())
1309 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1310 CP->getAlignment());
1311 else
1312 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1313 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001314 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001315}
1316
Dan Gohmand858e902010-04-17 15:26:15 +00001317SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1318 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001319 MachineFunction &MF = DAG.getMachineFunction();
1320 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1321 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001322 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001323 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001324 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001325 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1326 SDValue CPAddr;
1327 if (RelocM == Reloc::Static) {
1328 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1329 } else {
1330 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001331 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001332 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1333 ARMCP::CPBlockAddress,
1334 PCAdj);
1335 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1336 }
1337 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1338 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001339 PseudoSourceValue::getConstantPool(), 0,
1340 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001341 if (RelocM == Reloc::Static)
1342 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001343 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001344 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001345}
1346
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001347// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001348SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001349ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001350 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001351 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001352 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001353 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001354 MachineFunction &MF = DAG.getMachineFunction();
1355 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1356 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001357 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001358 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001359 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001360 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001361 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001362 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
David Greene1b58cab2010-02-15 16:55:24 +00001363 PseudoSourceValue::getConstantPool(), 0,
1364 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001365 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001366
Evan Chenge7e0d622009-11-06 22:24:13 +00001367 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001368 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001369
1370 // call __tls_get_addr.
1371 ArgListTy Args;
1372 ArgListEntry Entry;
1373 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001374 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001375 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001376 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001377 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001378 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1379 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001380 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001381 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001382 return CallResult.first;
1383}
1384
1385// Lower ISD::GlobalTLSAddress using the "initial exec" or
1386// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001387SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001388ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001389 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001390 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001391 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001392 SDValue Offset;
1393 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001394 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001395 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001396 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001397
Chris Lattner4fb63d02009-07-15 04:12:33 +00001398 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001399 MachineFunction &MF = DAG.getMachineFunction();
1400 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1401 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1402 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001403 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1404 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001405 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001406 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001407 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001408 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001409 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001410 PseudoSourceValue::getConstantPool(), 0,
1411 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001412 Chain = Offset.getValue(1);
1413
Evan Chenge7e0d622009-11-06 22:24:13 +00001414 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001415 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001416
Evan Cheng9eda6892009-10-31 03:39:36 +00001417 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001418 PseudoSourceValue::getConstantPool(), 0,
1419 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001420 } else {
1421 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001422 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001423 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001424 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001425 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001426 PseudoSourceValue::getConstantPool(), 0,
1427 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001428 }
1429
1430 // The address of the thread local variable is the add of the thread
1431 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001432 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001433}
1434
Dan Gohman475871a2008-07-27 21:46:04 +00001435SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001436ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001437 // TODO: implement the "local dynamic" model
1438 assert(Subtarget->isTargetELF() &&
1439 "TLS not implemented for non-ELF targets");
1440 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1441 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1442 // otherwise use the "Local Exec" TLS Model
1443 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1444 return LowerToTLSGeneralDynamicModel(GA, DAG);
1445 else
1446 return LowerToTLSExecModels(GA, DAG);
1447}
1448
Dan Gohman475871a2008-07-27 21:46:04 +00001449SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001450 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001451 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001452 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001453 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001454 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1455 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001456 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001457 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001458 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001459 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001460 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001461 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001462 CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001463 PseudoSourceValue::getConstantPool(), 0,
1464 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001465 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001466 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001467 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001468 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001469 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001470 PseudoSourceValue::getGOT(), 0,
1471 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001472 return Result;
1473 } else {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001474 // If we have T2 ops, we can materialize the address directly via movt/movw
1475 // pair. This is always cheaper.
1476 if (Subtarget->useMovt()) {
1477 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
1478 DAG.getTargetGlobalAddress(GV, PtrVT));
1479 } else {
1480 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1481 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1482 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001483 PseudoSourceValue::getConstantPool(), 0,
1484 false, false, 0);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001485 }
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001486 }
1487}
1488
Dan Gohman475871a2008-07-27 21:46:04 +00001489SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001490 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001491 MachineFunction &MF = DAG.getMachineFunction();
1492 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1493 unsigned ARMPCLabelIndex = 0;
Owen Andersone50ed302009-08-10 22:56:29 +00001494 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001495 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001496 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001497 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001498 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001499 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001500 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001501 else {
Evan Chenge7e0d622009-11-06 22:24:13 +00001502 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001503 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1504 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001505 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001506 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001507 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001508 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001509
Evan Cheng9eda6892009-10-31 03:39:36 +00001510 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001511 PseudoSourceValue::getConstantPool(), 0,
1512 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001513 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001514
1515 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001516 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001517 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001518 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001519
Evan Cheng63476a82009-09-03 07:04:02 +00001520 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Evan Cheng9eda6892009-10-31 03:39:36 +00001521 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001522 PseudoSourceValue::getGOT(), 0,
1523 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001524
1525 return Result;
1526}
1527
Dan Gohman475871a2008-07-27 21:46:04 +00001528SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001529 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001530 assert(Subtarget->isTargetELF() &&
1531 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00001532 MachineFunction &MF = DAG.getMachineFunction();
1533 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1534 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Andersone50ed302009-08-10 22:56:29 +00001535 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001536 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001537 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001538 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1539 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001540 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001541 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001542 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001543 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001544 PseudoSourceValue::getConstantPool(), 0,
1545 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001546 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001547 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001548}
1549
Jim Grosbach0e0da732009-05-12 23:59:14 +00001550SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00001551ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001552 const ARMSubtarget *Subtarget)
1553 const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001554 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001555 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001556 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001557 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001558 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001559 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001560 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1561 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001562 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001563 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00001564 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1565 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001566 EVT PtrVT = getPointerTy();
1567 DebugLoc dl = Op.getDebugLoc();
1568 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1569 SDValue CPAddr;
1570 unsigned PCAdj = (RelocM != Reloc::PIC_)
1571 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001572 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001573 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1574 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001575 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001576 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001577 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00001578 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001579 PseudoSourceValue::getConstantPool(), 0,
1580 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001581 SDValue Chain = Result.getValue(1);
1582
1583 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001584 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001585 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1586 }
1587 return Result;
1588 }
Jim Grosbachf9570122009-05-14 00:46:35 +00001589 case Intrinsic::eh_sjlj_setjmp:
Jim Grosbacha87ded22010-02-08 23:22:00 +00001590 SDValue Val = Subtarget->isThumb() ?
1591 DAG.getCopyFromReg(DAG.getEntryNode(), dl, ARM::SP, MVT::i32) :
1592 DAG.getConstant(0, MVT::i32);
1593 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1),
1594 Val);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001595 }
1596}
1597
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001598static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
1599 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00001600 DebugLoc dl = Op.getDebugLoc();
1601 SDValue Op5 = Op.getOperand(5);
1602 SDValue Res;
1603 unsigned isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue();
1604 if (isDeviceBarrier) {
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001605 if (Subtarget->hasV7Ops())
1606 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0));
1607 else
1608 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0),
1609 DAG.getConstant(0, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00001610 } else {
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001611 if (Subtarget->hasV7Ops())
1612 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
1613 else
1614 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
1615 DAG.getConstant(0, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00001616 }
1617 return Res;
1618}
1619
Dan Gohman1e93df62010-04-17 14:41:14 +00001620static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
1621 MachineFunction &MF = DAG.getMachineFunction();
1622 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
1623
Evan Chenga8e29892007-01-19 07:51:42 +00001624 // vastart just stores the address of the VarArgsFrameIndex slot into the
1625 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001626 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001627 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00001628 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001629 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
David Greene1b58cab2010-02-15 16:55:24 +00001630 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
1631 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001632}
1633
Dan Gohman475871a2008-07-27 21:46:04 +00001634SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001635ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1636 SelectionDAG &DAG) const {
Evan Cheng86198642009-08-07 00:34:42 +00001637 SDNode *Node = Op.getNode();
1638 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001639 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001640 SDValue Chain = Op.getOperand(0);
1641 SDValue Size = Op.getOperand(1);
1642 SDValue Align = Op.getOperand(2);
1643
1644 // Chain the dynamic stack allocation so that it doesn't modify the stack
1645 // pointer when other instructions are using the stack.
1646 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1647
1648 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1649 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1650 if (AlignVal > StackAlign)
1651 // Do this now since selection pass cannot introduce new target
1652 // independent node.
1653 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1654
1655 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1656 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1657 // do even more horrible hack later.
1658 MachineFunction &MF = DAG.getMachineFunction();
1659 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1660 if (AFI->isThumb1OnlyFunction()) {
1661 bool Negate = true;
1662 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1663 if (C) {
1664 uint32_t Val = C->getZExtValue();
1665 if (Val <= 508 && ((Val & 3) == 0))
1666 Negate = false;
1667 }
1668 if (Negate)
1669 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1670 }
1671
Owen Anderson825b72b2009-08-11 20:47:22 +00001672 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001673 SDValue Ops1[] = { Chain, Size, Align };
1674 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1675 Chain = Res.getValue(1);
1676 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1677 DAG.getIntPtrConstant(0, true), SDValue());
1678 SDValue Ops2[] = { Res, Chain };
1679 return DAG.getMergeValues(Ops2, 2, dl);
1680}
1681
1682SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001683ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1684 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001685 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001686 MachineFunction &MF = DAG.getMachineFunction();
1687 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1688
1689 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001690 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001691 RC = ARM::tGPRRegisterClass;
1692 else
1693 RC = ARM::GPRRegisterClass;
1694
1695 // Transform the arguments stored in physical registers into virtual ones.
1696 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001697 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001698
1699 SDValue ArgValue2;
1700 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001701 MachineFrameInfo *MFI = MF.getFrameInfo();
Bob Wilson6a234f02010-04-13 22:03:22 +00001702 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true, false);
Bob Wilson5bafff32009-06-22 23:27:02 +00001703
1704 // Create load node to retrieve arguments from the stack.
1705 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001706 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00001707 PseudoSourceValue::getFixedStack(FI), 0,
1708 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001709 } else {
1710 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001711 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001712 }
1713
Jim Grosbache5165492009-11-09 00:11:35 +00001714 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001715}
1716
1717SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001718ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001719 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001720 const SmallVectorImpl<ISD::InputArg>
1721 &Ins,
1722 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001723 SmallVectorImpl<SDValue> &InVals)
1724 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001725
Bob Wilson1f595bb2009-04-17 19:07:39 +00001726 MachineFunction &MF = DAG.getMachineFunction();
1727 MachineFrameInfo *MFI = MF.getFrameInfo();
1728
Bob Wilson1f595bb2009-04-17 19:07:39 +00001729 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1730
1731 // Assign locations to all of the incoming arguments.
1732 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001733 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1734 *DAG.getContext());
1735 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001736 CCAssignFnForNode(CallConv, /* Return*/ false,
1737 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001738
1739 SmallVector<SDValue, 16> ArgValues;
1740
1741 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1742 CCValAssign &VA = ArgLocs[i];
1743
Bob Wilsondee46d72009-04-17 20:35:10 +00001744 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001745 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001746 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00001747
Bob Wilson5bafff32009-06-22 23:27:02 +00001748 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001749 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001750 // f64 and vector types are split up into multiple registers or
1751 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00001752 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001753 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001754 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00001755 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00001756 SDValue ArgValue2;
1757 if (VA.isMemLoc()) {
1758 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(),
1759 true, false);
1760 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1761 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
1762 PseudoSourceValue::getFixedStack(FI), 0,
1763 false, false, 0);
1764 } else {
1765 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
1766 Chain, DAG, dl);
1767 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001768 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1769 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001770 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00001771 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001772 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1773 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001774 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001775
Bob Wilson5bafff32009-06-22 23:27:02 +00001776 } else {
1777 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001778
Owen Anderson825b72b2009-08-11 20:47:22 +00001779 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00001780 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001781 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00001782 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001783 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001784 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001785 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001786 RC = (AFI->isThumb1OnlyFunction() ?
1787 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00001788 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001789 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00001790
1791 // Transform the arguments in physical registers into virtual ones.
1792 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001793 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001794 }
1795
1796 // If this is an 8 or 16-bit value, it is really passed promoted
1797 // to 32 bits. Insert an assert[sz]ext to capture this, then
1798 // truncate to the right size.
1799 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001800 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001801 case CCValAssign::Full: break;
1802 case CCValAssign::BCvt:
1803 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1804 break;
1805 case CCValAssign::SExt:
1806 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1807 DAG.getValueType(VA.getValVT()));
1808 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1809 break;
1810 case CCValAssign::ZExt:
1811 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1812 DAG.getValueType(VA.getValVT()));
1813 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1814 break;
1815 }
1816
Dan Gohman98ca4f22009-08-05 01:29:28 +00001817 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001818
1819 } else { // VA.isRegLoc()
1820
1821 // sanity check
1822 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00001823 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001824
1825 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001826 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
1827 true, false);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001828
Bob Wilsondee46d72009-04-17 20:35:10 +00001829 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001830 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001831 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00001832 PseudoSourceValue::getFixedStack(FI), 0,
1833 false, false, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001834 }
1835 }
1836
1837 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001838 if (isVarArg) {
1839 static const unsigned GPRArgRegs[] = {
1840 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1841 };
1842
Bob Wilsondee46d72009-04-17 20:35:10 +00001843 unsigned NumGPRs = CCInfo.getFirstUnallocated
1844 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001845
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001846 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1847 unsigned VARegSize = (4 - NumGPRs) * 4;
1848 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00001849 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001850 if (VARegSaveSize) {
1851 // If this function is vararg, store any remaining integer argument regs
1852 // to their spots on the stack so that they may be loaded by deferencing
1853 // the result of va_next.
1854 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00001855 AFI->setVarArgsFrameIndex(
1856 MFI->CreateFixedObject(VARegSaveSize,
1857 ArgOffset + VARegSaveSize - VARegSize,
1858 true, false));
1859 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
1860 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001861
Dan Gohman475871a2008-07-27 21:46:04 +00001862 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001863 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001864 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001865 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001866 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001867 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001868 RC = ARM::GPRRegisterClass;
1869
Bob Wilson998e1252009-04-20 18:36:57 +00001870 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001871 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00001872 SDValue Store =
1873 DAG.getStore(Val.getValue(1), dl, Val, FIN,
1874 PseudoSourceValue::getFixedStack(AFI->getVarArgsFrameIndex()), 0,
1875 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001876 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001877 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001878 DAG.getConstant(4, getPointerTy()));
1879 }
1880 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001881 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001882 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001883 } else
1884 // This will point to the next argument passed via stack.
Dan Gohman1e93df62010-04-17 14:41:14 +00001885 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset,
1886 true, false));
Evan Chenga8e29892007-01-19 07:51:42 +00001887 }
1888
Dan Gohman98ca4f22009-08-05 01:29:28 +00001889 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00001890}
1891
1892/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001893static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001894 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001895 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001896 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001897 // Maybe this has already been legalized into the constant pool?
1898 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001899 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001900 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00001901 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001902 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001903 }
1904 }
1905 return false;
1906}
1907
Evan Chenga8e29892007-01-19 07:51:42 +00001908/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1909/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00001910SDValue
1911ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Dan Gohmand858e902010-04-17 15:26:15 +00001912 SDValue &ARMCC, SelectionDAG &DAG,
1913 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00001914 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001915 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00001916 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001917 // Constant does not fit, try adjusting it by one?
1918 switch (CC) {
1919 default: break;
1920 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001921 case ISD::SETGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00001922 if (isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001923 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
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::SETULT:
1928 case ISD::SETUGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00001929 if (C > 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001930 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
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 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001935 case ISD::SETGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00001936 if (isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001937 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001938 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001939 }
1940 break;
1941 case ISD::SETULE:
1942 case ISD::SETUGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00001943 if (C < 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001944 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001945 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001946 }
1947 break;
1948 }
1949 }
1950 }
1951
1952 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001953 ARMISD::NodeType CompareType;
1954 switch (CondCode) {
1955 default:
1956 CompareType = ARMISD::CMP;
1957 break;
1958 case ARMCC::EQ:
1959 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00001960 // Uses only Z Flag
1961 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001962 break;
1963 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001964 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1965 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001966}
1967
1968/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001969static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001970 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001971 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001972 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00001973 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001974 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001975 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1976 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001977}
1978
Dan Gohmand858e902010-04-17 15:26:15 +00001979SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001980 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001981 SDValue LHS = Op.getOperand(0);
1982 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001983 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001984 SDValue TrueVal = Op.getOperand(2);
1985 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001986 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001987
Owen Anderson825b72b2009-08-11 20:47:22 +00001988 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001989 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001990 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00001991 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Dale Johannesende064702009-02-06 21:50:26 +00001992 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001993 }
1994
1995 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001996 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00001997
Owen Anderson825b72b2009-08-11 20:47:22 +00001998 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1999 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002000 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
2001 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00002002 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002003 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002004 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002005 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00002006 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002007 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00002008 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002009 }
2010 return Result;
2011}
2012
Dan Gohmand858e902010-04-17 15:26:15 +00002013SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002014 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002015 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002016 SDValue LHS = Op.getOperand(2);
2017 SDValue RHS = Op.getOperand(3);
2018 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002019 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002020
Owen Anderson825b72b2009-08-11 20:47:22 +00002021 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00002022 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002023 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00002024 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002025 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00002026 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002027 }
2028
Owen Anderson825b72b2009-08-11 20:47:22 +00002029 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00002030 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002031 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002032
Dale Johannesende064702009-02-06 21:50:26 +00002033 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002034 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
2035 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2036 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002037 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002038 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002039 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002040 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00002041 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002042 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002043 }
2044 return Res;
2045}
2046
Dan Gohmand858e902010-04-17 15:26:15 +00002047SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002048 SDValue Chain = Op.getOperand(0);
2049 SDValue Table = Op.getOperand(1);
2050 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002051 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002052
Owen Andersone50ed302009-08-10 22:56:29 +00002053 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002054 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2055 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002056 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002057 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002058 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002059 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2060 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002061 if (Subtarget->isThumb2()) {
2062 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2063 // which does another jump to the destination. This also makes it easier
2064 // to translate it to TBB / TBH later.
2065 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002066 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002067 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002068 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002069 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002070 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002071 PseudoSourceValue::getJumpTable(), 0,
2072 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002073 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002074 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002075 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002076 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002077 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002078 PseudoSourceValue::getJumpTable(), 0, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002079 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002080 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002081 }
Evan Chenga8e29892007-01-19 07:51:42 +00002082}
2083
Bob Wilson76a312b2010-03-19 22:51:32 +00002084static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2085 DebugLoc dl = Op.getDebugLoc();
2086 unsigned Opc;
2087
2088 switch (Op.getOpcode()) {
2089 default:
2090 assert(0 && "Invalid opcode!");
2091 case ISD::FP_TO_SINT:
2092 Opc = ARMISD::FTOSI;
2093 break;
2094 case ISD::FP_TO_UINT:
2095 Opc = ARMISD::FTOUI;
2096 break;
2097 }
2098 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
2099 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
2100}
2101
2102static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2103 EVT VT = Op.getValueType();
2104 DebugLoc dl = Op.getDebugLoc();
2105 unsigned Opc;
2106
2107 switch (Op.getOpcode()) {
2108 default:
2109 assert(0 && "Invalid opcode!");
2110 case ISD::SINT_TO_FP:
2111 Opc = ARMISD::SITOF;
2112 break;
2113 case ISD::UINT_TO_FP:
2114 Opc = ARMISD::UITOF;
2115 break;
2116 }
2117
2118 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
2119 return DAG.getNode(Opc, dl, VT, Op);
2120}
2121
Dan Gohman475871a2008-07-27 21:46:04 +00002122static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002123 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002124 SDValue Tmp0 = Op.getOperand(0);
2125 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002126 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002127 EVT VT = Op.getValueType();
2128 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00002129 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
2130 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002131 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
2132 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002133 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002134}
2135
Dan Gohmand858e902010-04-17 15:26:15 +00002136SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002137 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2138 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00002139 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002140 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2141 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002142 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002143 ? ARM::R7 : ARM::R11;
2144 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2145 while (Depth--)
David Greene1b58cab2010-02-15 16:55:24 +00002146 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
2147 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002148 return FrameAddr;
2149}
2150
Bob Wilson9f3f0612010-04-17 05:30:19 +00002151/// ExpandBIT_CONVERT - If the target supports VFP, this function is called to
2152/// expand a bit convert where either the source or destination type is i64 to
2153/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2154/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2155/// vectors), since the legalizer won't know what to do with that.
Duncan Sands1607f052008-12-01 11:39:25 +00002156static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002157 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2158 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002159 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002160
Bob Wilson9f3f0612010-04-17 05:30:19 +00002161 // This function is only supposed to be called for i64 types, either as the
2162 // source or destination of the bit convert.
2163 EVT SrcVT = Op.getValueType();
2164 EVT DstVT = N->getValueType(0);
2165 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
2166 "ExpandBIT_CONVERT called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002167
Bob Wilson9f3f0612010-04-17 05:30:19 +00002168 // Turn i64->f64 into VMOVDRR.
2169 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002170 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2171 DAG.getConstant(0, MVT::i32));
2172 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2173 DAG.getConstant(1, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00002174 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002175 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002176
Jim Grosbache5165492009-11-09 00:11:35 +00002177 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002178 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2179 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2180 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2181 // Merge the pieces into a single i64 value.
2182 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2183 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002184
Bob Wilson9f3f0612010-04-17 05:30:19 +00002185 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002186}
2187
Bob Wilson5bafff32009-06-22 23:27:02 +00002188/// getZeroVector - Returns a vector of specified type with all zero elements.
2189///
Owen Andersone50ed302009-08-10 22:56:29 +00002190static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002191 assert(VT.isVector() && "Expected a vector type");
2192
2193 // Zero vectors are used to represent vector negation and in those cases
2194 // will be implemented with the NEON VNEG instruction. However, VNEG does
2195 // not support i64 elements, so sometimes the zero vectors will need to be
2196 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002197 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilson5bafff32009-06-22 23:27:02 +00002198 // to their dest type. This ensures they get CSE'd.
2199 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002200 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2201 SmallVector<SDValue, 8> Ops;
2202 MVT TVT;
2203
2204 if (VT.getSizeInBits() == 64) {
2205 Ops.assign(8, Cst); TVT = MVT::v8i8;
2206 } else {
2207 Ops.assign(16, Cst); TVT = MVT::v16i8;
2208 }
2209 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002210
2211 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2212}
2213
2214/// getOnesVector - Returns a vector of specified type with all bits set.
2215///
Owen Andersone50ed302009-08-10 22:56:29 +00002216static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002217 assert(VT.isVector() && "Expected a vector type");
2218
Bob Wilson929ffa22009-10-30 20:13:25 +00002219 // Always build ones vectors as <16 x i8> or <8 x i8> bitcasted to their
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002220 // dest type. This ensures they get CSE'd.
Bob Wilson5bafff32009-06-22 23:27:02 +00002221 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002222 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2223 SmallVector<SDValue, 8> Ops;
2224 MVT TVT;
2225
2226 if (VT.getSizeInBits() == 64) {
2227 Ops.assign(8, Cst); TVT = MVT::v8i8;
2228 } else {
2229 Ops.assign(16, Cst); TVT = MVT::v16i8;
2230 }
2231 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002232
2233 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2234}
2235
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002236/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2237/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002238SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2239 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002240 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2241 EVT VT = Op.getValueType();
2242 unsigned VTBits = VT.getSizeInBits();
2243 DebugLoc dl = Op.getDebugLoc();
2244 SDValue ShOpLo = Op.getOperand(0);
2245 SDValue ShOpHi = Op.getOperand(1);
2246 SDValue ShAmt = Op.getOperand(2);
2247 SDValue ARMCC;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002248 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002249
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002250 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2251
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002252 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2253 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2254 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2255 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2256 DAG.getConstant(VTBits, MVT::i32));
2257 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2258 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002259 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002260
2261 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2262 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002263 ARMCC, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002264 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002265 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC,
2266 CCR, Cmp);
2267
2268 SDValue Ops[2] = { Lo, Hi };
2269 return DAG.getMergeValues(Ops, 2, dl);
2270}
2271
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002272/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2273/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002274SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2275 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002276 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2277 EVT VT = Op.getValueType();
2278 unsigned VTBits = VT.getSizeInBits();
2279 DebugLoc dl = Op.getDebugLoc();
2280 SDValue ShOpLo = Op.getOperand(0);
2281 SDValue ShOpHi = Op.getOperand(1);
2282 SDValue ShAmt = Op.getOperand(2);
2283 SDValue ARMCC;
2284
2285 assert(Op.getOpcode() == ISD::SHL_PARTS);
2286 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2287 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2288 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2289 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2290 DAG.getConstant(VTBits, MVT::i32));
2291 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2292 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2293
2294 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2295 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2296 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002297 ARMCC, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002298 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
2299 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMCC,
2300 CCR, Cmp);
2301
2302 SDValue Ops[2] = { Lo, Hi };
2303 return DAG.getMergeValues(Ops, 2, dl);
2304}
2305
Jim Grosbach3482c802010-01-18 19:58:49 +00002306static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
2307 const ARMSubtarget *ST) {
2308 EVT VT = N->getValueType(0);
2309 DebugLoc dl = N->getDebugLoc();
2310
2311 if (!ST->hasV6T2Ops())
2312 return SDValue();
2313
2314 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
2315 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
2316}
2317
Bob Wilson5bafff32009-06-22 23:27:02 +00002318static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2319 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002320 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002321 DebugLoc dl = N->getDebugLoc();
2322
2323 // Lower vector shifts on NEON to use VSHL.
2324 if (VT.isVector()) {
2325 assert(ST->hasNEON() && "unexpected vector shift");
2326
2327 // Left shifts translate directly to the vshiftu intrinsic.
2328 if (N->getOpcode() == ISD::SHL)
2329 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002330 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002331 N->getOperand(0), N->getOperand(1));
2332
2333 assert((N->getOpcode() == ISD::SRA ||
2334 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2335
2336 // NEON uses the same intrinsics for both left and right shifts. For
2337 // right shifts, the shift amounts are negative, so negate the vector of
2338 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002339 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002340 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2341 getZeroVector(ShiftVT, DAG, dl),
2342 N->getOperand(1));
2343 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2344 Intrinsic::arm_neon_vshifts :
2345 Intrinsic::arm_neon_vshiftu);
2346 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002347 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002348 N->getOperand(0), NegatedCount);
2349 }
2350
Eli Friedmance392eb2009-08-22 03:13:10 +00002351 // We can get here for a node like i32 = ISD::SHL i32, i64
2352 if (VT != MVT::i64)
2353 return SDValue();
2354
2355 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002356 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002357
Chris Lattner27a6c732007-11-24 07:07:01 +00002358 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2359 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002360 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002361 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002362
Chris Lattner27a6c732007-11-24 07:07:01 +00002363 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002364 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002365
Chris Lattner27a6c732007-11-24 07:07:01 +00002366 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002367 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2368 DAG.getConstant(0, MVT::i32));
2369 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2370 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002371
Chris Lattner27a6c732007-11-24 07:07:01 +00002372 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2373 // captures the result into a carry flag.
2374 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002375 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002376
Chris Lattner27a6c732007-11-24 07:07:01 +00002377 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002378 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002379
Chris Lattner27a6c732007-11-24 07:07:01 +00002380 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002381 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002382}
2383
Bob Wilson5bafff32009-06-22 23:27:02 +00002384static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2385 SDValue TmpOp0, TmpOp1;
2386 bool Invert = false;
2387 bool Swap = false;
2388 unsigned Opc = 0;
2389
2390 SDValue Op0 = Op.getOperand(0);
2391 SDValue Op1 = Op.getOperand(1);
2392 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002393 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002394 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2395 DebugLoc dl = Op.getDebugLoc();
2396
2397 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2398 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002399 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002400 case ISD::SETUNE:
2401 case ISD::SETNE: Invert = true; // Fallthrough
2402 case ISD::SETOEQ:
2403 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2404 case ISD::SETOLT:
2405 case ISD::SETLT: Swap = true; // Fallthrough
2406 case ISD::SETOGT:
2407 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2408 case ISD::SETOLE:
2409 case ISD::SETLE: Swap = true; // Fallthrough
2410 case ISD::SETOGE:
2411 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2412 case ISD::SETUGE: Swap = true; // Fallthrough
2413 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2414 case ISD::SETUGT: Swap = true; // Fallthrough
2415 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2416 case ISD::SETUEQ: Invert = true; // Fallthrough
2417 case ISD::SETONE:
2418 // Expand this to (OLT | OGT).
2419 TmpOp0 = Op0;
2420 TmpOp1 = Op1;
2421 Opc = ISD::OR;
2422 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2423 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2424 break;
2425 case ISD::SETUO: Invert = true; // Fallthrough
2426 case ISD::SETO:
2427 // Expand this to (OLT | OGE).
2428 TmpOp0 = Op0;
2429 TmpOp1 = Op1;
2430 Opc = ISD::OR;
2431 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2432 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2433 break;
2434 }
2435 } else {
2436 // Integer comparisons.
2437 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002438 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002439 case ISD::SETNE: Invert = true;
2440 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2441 case ISD::SETLT: Swap = true;
2442 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2443 case ISD::SETLE: Swap = true;
2444 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2445 case ISD::SETULT: Swap = true;
2446 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2447 case ISD::SETULE: Swap = true;
2448 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2449 }
2450
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002451 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002452 if (Opc == ARMISD::VCEQ) {
2453
2454 SDValue AndOp;
2455 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2456 AndOp = Op0;
2457 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2458 AndOp = Op1;
2459
2460 // Ignore bitconvert.
2461 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2462 AndOp = AndOp.getOperand(0);
2463
2464 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2465 Opc = ARMISD::VTST;
2466 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2467 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2468 Invert = !Invert;
2469 }
2470 }
2471 }
2472
2473 if (Swap)
2474 std::swap(Op0, Op1);
2475
2476 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2477
2478 if (Invert)
2479 Result = DAG.getNOT(dl, Result, VT);
2480
2481 return Result;
2482}
2483
2484/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2485/// VMOV instruction, and if so, return the constant being splatted.
2486static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2487 unsigned SplatBitSize, SelectionDAG &DAG) {
2488 switch (SplatBitSize) {
2489 case 8:
2490 // Any 1-byte value is OK.
2491 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson825b72b2009-08-11 20:47:22 +00002492 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilson5bafff32009-06-22 23:27:02 +00002493
2494 case 16:
2495 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2496 if ((SplatBits & ~0xff) == 0 ||
2497 (SplatBits & ~0xff00) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002498 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilson5bafff32009-06-22 23:27:02 +00002499 break;
2500
2501 case 32:
2502 // NEON's 32-bit VMOV supports splat values where:
2503 // * only one byte is nonzero, or
2504 // * the least significant byte is 0xff and the second byte is nonzero, or
2505 // * the least significant 2 bytes are 0xff and the third is nonzero.
2506 if ((SplatBits & ~0xff) == 0 ||
2507 (SplatBits & ~0xff00) == 0 ||
2508 (SplatBits & ~0xff0000) == 0 ||
2509 (SplatBits & ~0xff000000) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002510 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002511
2512 if ((SplatBits & ~0xffff) == 0 &&
2513 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002514 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002515
2516 if ((SplatBits & ~0xffffff) == 0 &&
2517 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002518 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002519
2520 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2521 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2522 // VMOV.I32. A (very) minor optimization would be to replicate the value
2523 // and fall through here to test for a valid 64-bit splat. But, then the
2524 // caller would also need to check and handle the change in size.
2525 break;
2526
2527 case 64: {
2528 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2529 uint64_t BitMask = 0xff;
2530 uint64_t Val = 0;
2531 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2532 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2533 Val |= BitMask;
2534 else if ((SplatBits & BitMask) != 0)
2535 return SDValue();
2536 BitMask <<= 8;
2537 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002538 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilson5bafff32009-06-22 23:27:02 +00002539 }
2540
2541 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002542 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilson5bafff32009-06-22 23:27:02 +00002543 break;
2544 }
2545
2546 return SDValue();
2547}
2548
2549/// getVMOVImm - If this is a build_vector of constants which can be
2550/// formed by using a VMOV instruction of the specified element size,
2551/// return the constant being splatted. The ByteSize field indicates the
2552/// number of bytes of each element [1248].
2553SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2554 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2555 APInt SplatBits, SplatUndef;
2556 unsigned SplatBitSize;
2557 bool HasAnyUndefs;
2558 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2559 HasAnyUndefs, ByteSize * 8))
2560 return SDValue();
2561
2562 if (SplatBitSize > ByteSize * 8)
2563 return SDValue();
2564
2565 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2566 SplatBitSize, DAG);
2567}
2568
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002569static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2570 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002571 unsigned NumElts = VT.getVectorNumElements();
2572 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002573 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002574
2575 // If this is a VEXT shuffle, the immediate value is the index of the first
2576 // element. The other shuffle indices must be the successive elements after
2577 // the first one.
2578 unsigned ExpectedElt = Imm;
2579 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002580 // Increment the expected index. If it wraps around, it may still be
2581 // a VEXT but the source vectors must be swapped.
2582 ExpectedElt += 1;
2583 if (ExpectedElt == NumElts * 2) {
2584 ExpectedElt = 0;
2585 ReverseVEXT = true;
2586 }
2587
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002588 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002589 return false;
2590 }
2591
2592 // Adjust the index value if the source operands will be swapped.
2593 if (ReverseVEXT)
2594 Imm -= NumElts;
2595
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002596 return true;
2597}
2598
Bob Wilson8bb9e482009-07-26 00:39:34 +00002599/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2600/// instruction with the specified blocksize. (The order of the elements
2601/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002602static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2603 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002604 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2605 "Only possible block sizes for VREV are: 16, 32, 64");
2606
Bob Wilson8bb9e482009-07-26 00:39:34 +00002607 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00002608 if (EltSz == 64)
2609 return false;
2610
2611 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002612 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002613
2614 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2615 return false;
2616
2617 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002618 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002619 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2620 return false;
2621 }
2622
2623 return true;
2624}
2625
Bob Wilsonc692cb72009-08-21 20:54:19 +00002626static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2627 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002628 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2629 if (EltSz == 64)
2630 return false;
2631
Bob Wilsonc692cb72009-08-21 20:54:19 +00002632 unsigned NumElts = VT.getVectorNumElements();
2633 WhichResult = (M[0] == 0 ? 0 : 1);
2634 for (unsigned i = 0; i < NumElts; i += 2) {
2635 if ((unsigned) M[i] != i + WhichResult ||
2636 (unsigned) M[i+1] != i + NumElts + WhichResult)
2637 return false;
2638 }
2639 return true;
2640}
2641
Bob Wilson324f4f12009-12-03 06:40:55 +00002642/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
2643/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2644/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
2645static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2646 unsigned &WhichResult) {
2647 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2648 if (EltSz == 64)
2649 return false;
2650
2651 unsigned NumElts = VT.getVectorNumElements();
2652 WhichResult = (M[0] == 0 ? 0 : 1);
2653 for (unsigned i = 0; i < NumElts; i += 2) {
2654 if ((unsigned) M[i] != i + WhichResult ||
2655 (unsigned) M[i+1] != i + WhichResult)
2656 return false;
2657 }
2658 return true;
2659}
2660
Bob Wilsonc692cb72009-08-21 20:54:19 +00002661static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2662 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002663 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2664 if (EltSz == 64)
2665 return false;
2666
Bob Wilsonc692cb72009-08-21 20:54:19 +00002667 unsigned NumElts = VT.getVectorNumElements();
2668 WhichResult = (M[0] == 0 ? 0 : 1);
2669 for (unsigned i = 0; i != NumElts; ++i) {
2670 if ((unsigned) M[i] != 2 * i + WhichResult)
2671 return false;
2672 }
2673
2674 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002675 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002676 return false;
2677
2678 return true;
2679}
2680
Bob Wilson324f4f12009-12-03 06:40:55 +00002681/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
2682/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2683/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
2684static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2685 unsigned &WhichResult) {
2686 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2687 if (EltSz == 64)
2688 return false;
2689
2690 unsigned Half = VT.getVectorNumElements() / 2;
2691 WhichResult = (M[0] == 0 ? 0 : 1);
2692 for (unsigned j = 0; j != 2; ++j) {
2693 unsigned Idx = WhichResult;
2694 for (unsigned i = 0; i != Half; ++i) {
2695 if ((unsigned) M[i + j * Half] != Idx)
2696 return false;
2697 Idx += 2;
2698 }
2699 }
2700
2701 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2702 if (VT.is64BitVector() && EltSz == 32)
2703 return false;
2704
2705 return true;
2706}
2707
Bob Wilsonc692cb72009-08-21 20:54:19 +00002708static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2709 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002710 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2711 if (EltSz == 64)
2712 return false;
2713
Bob Wilsonc692cb72009-08-21 20:54:19 +00002714 unsigned NumElts = VT.getVectorNumElements();
2715 WhichResult = (M[0] == 0 ? 0 : 1);
2716 unsigned Idx = WhichResult * NumElts / 2;
2717 for (unsigned i = 0; i != NumElts; i += 2) {
2718 if ((unsigned) M[i] != Idx ||
2719 (unsigned) M[i+1] != Idx + NumElts)
2720 return false;
2721 Idx += 1;
2722 }
2723
2724 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002725 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002726 return false;
2727
2728 return true;
2729}
2730
Bob Wilson324f4f12009-12-03 06:40:55 +00002731/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
2732/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2733/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
2734static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2735 unsigned &WhichResult) {
2736 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2737 if (EltSz == 64)
2738 return false;
2739
2740 unsigned NumElts = VT.getVectorNumElements();
2741 WhichResult = (M[0] == 0 ? 0 : 1);
2742 unsigned Idx = WhichResult * NumElts / 2;
2743 for (unsigned i = 0; i != NumElts; i += 2) {
2744 if ((unsigned) M[i] != Idx ||
2745 (unsigned) M[i+1] != Idx)
2746 return false;
2747 Idx += 1;
2748 }
2749
2750 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2751 if (VT.is64BitVector() && EltSz == 32)
2752 return false;
2753
2754 return true;
2755}
2756
2757
Owen Andersone50ed302009-08-10 22:56:29 +00002758static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002759 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00002760 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002761 if (ConstVal->isNullValue())
2762 return getZeroVector(VT, DAG, dl);
2763 if (ConstVal->isAllOnesValue())
2764 return getOnesVector(VT, DAG, dl);
2765
Owen Andersone50ed302009-08-10 22:56:29 +00002766 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00002767 if (VT.is64BitVector()) {
2768 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002769 case 8: CanonicalVT = MVT::v8i8; break;
2770 case 16: CanonicalVT = MVT::v4i16; break;
2771 case 32: CanonicalVT = MVT::v2i32; break;
2772 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002773 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002774 }
2775 } else {
2776 assert(VT.is128BitVector() && "unknown splat vector size");
2777 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002778 case 8: CanonicalVT = MVT::v16i8; break;
2779 case 16: CanonicalVT = MVT::v8i16; break;
2780 case 32: CanonicalVT = MVT::v4i32; break;
2781 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002782 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002783 }
2784 }
2785
2786 // Build a canonical splat for this value.
2787 SmallVector<SDValue, 8> Ops;
2788 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2789 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2790 Ops.size());
2791 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2792}
2793
2794// If this is a case we can't handle, return null and let the default
2795// expansion code take care of it.
2796static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00002797 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002798 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002799 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002800
2801 APInt SplatBits, SplatUndef;
2802 unsigned SplatBitSize;
2803 bool HasAnyUndefs;
2804 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00002805 if (SplatBitSize <= 64) {
2806 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2807 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2808 if (Val.getNode())
2809 return BuildSplat(Val, VT, DAG, dl);
2810 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00002811 }
2812
2813 // If there are only 2 elements in a 128-bit vector, insert them into an
2814 // undef vector. This handles the common case for 128-bit vector argument
2815 // passing, where the insertions should be translated to subreg accesses
2816 // with no real instructions.
2817 if (VT.is128BitVector() && Op.getNumOperands() == 2) {
2818 SDValue Val = DAG.getUNDEF(VT);
2819 SDValue Op0 = Op.getOperand(0);
2820 SDValue Op1 = Op.getOperand(1);
2821 if (Op0.getOpcode() != ISD::UNDEF)
2822 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op0,
2823 DAG.getIntPtrConstant(0));
2824 if (Op1.getOpcode() != ISD::UNDEF)
2825 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op1,
2826 DAG.getIntPtrConstant(1));
2827 return Val;
Bob Wilson5bafff32009-06-22 23:27:02 +00002828 }
2829
2830 return SDValue();
2831}
2832
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002833/// isShuffleMaskLegal - Targets can use this to indicate that they only
2834/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2835/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2836/// are assumed to be legal.
2837bool
2838ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2839 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002840 if (VT.getVectorNumElements() == 4 &&
2841 (VT.is128BitVector() || VT.is64BitVector())) {
2842 unsigned PFIndexes[4];
2843 for (unsigned i = 0; i != 4; ++i) {
2844 if (M[i] < 0)
2845 PFIndexes[i] = 8;
2846 else
2847 PFIndexes[i] = M[i];
2848 }
2849
2850 // Compute the index in the perfect shuffle table.
2851 unsigned PFTableIndex =
2852 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2853 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2854 unsigned Cost = (PFEntry >> 30);
2855
2856 if (Cost <= 4)
2857 return true;
2858 }
2859
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002860 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00002861 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002862
2863 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2864 isVREVMask(M, VT, 64) ||
2865 isVREVMask(M, VT, 32) ||
2866 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00002867 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2868 isVTRNMask(M, VT, WhichResult) ||
2869 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00002870 isVZIPMask(M, VT, WhichResult) ||
2871 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
2872 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
2873 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002874}
2875
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002876/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2877/// the specified operations to build the shuffle.
2878static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2879 SDValue RHS, SelectionDAG &DAG,
2880 DebugLoc dl) {
2881 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2882 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2883 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2884
2885 enum {
2886 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2887 OP_VREV,
2888 OP_VDUP0,
2889 OP_VDUP1,
2890 OP_VDUP2,
2891 OP_VDUP3,
2892 OP_VEXT1,
2893 OP_VEXT2,
2894 OP_VEXT3,
2895 OP_VUZPL, // VUZP, left result
2896 OP_VUZPR, // VUZP, right result
2897 OP_VZIPL, // VZIP, left result
2898 OP_VZIPR, // VZIP, right result
2899 OP_VTRNL, // VTRN, left result
2900 OP_VTRNR // VTRN, right result
2901 };
2902
2903 if (OpNum == OP_COPY) {
2904 if (LHSID == (1*9+2)*9+3) return LHS;
2905 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2906 return RHS;
2907 }
2908
2909 SDValue OpLHS, OpRHS;
2910 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2911 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2912 EVT VT = OpLHS.getValueType();
2913
2914 switch (OpNum) {
2915 default: llvm_unreachable("Unknown shuffle opcode!");
2916 case OP_VREV:
2917 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2918 case OP_VDUP0:
2919 case OP_VDUP1:
2920 case OP_VDUP2:
2921 case OP_VDUP3:
2922 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002923 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002924 case OP_VEXT1:
2925 case OP_VEXT2:
2926 case OP_VEXT3:
2927 return DAG.getNode(ARMISD::VEXT, dl, VT,
2928 OpLHS, OpRHS,
2929 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2930 case OP_VUZPL:
2931 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002932 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002933 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
2934 case OP_VZIPL:
2935 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002936 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002937 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
2938 case OP_VTRNL:
2939 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002940 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2941 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002942 }
2943}
2944
Bob Wilson5bafff32009-06-22 23:27:02 +00002945static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002946 SDValue V1 = Op.getOperand(0);
2947 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00002948 DebugLoc dl = Op.getDebugLoc();
2949 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002950 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002951 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00002952
Bob Wilson28865062009-08-13 02:13:04 +00002953 // Convert shuffles that are directly supported on NEON to target-specific
2954 // DAG nodes, instead of keeping them as shuffles and matching them again
2955 // during code selection. This is more efficient and avoids the possibility
2956 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00002957 // FIXME: floating-point vectors should be canonicalized to integer vectors
2958 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002959 SVN->getMask(ShuffleMask);
2960
2961 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson0ce37102009-08-14 05:08:32 +00002962 int Lane = SVN->getSplatIndex();
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00002963 // If this is undef splat, generate it via "just" vdup, if possible.
2964 if (Lane == -1) Lane = 0;
2965
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002966 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
2967 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00002968 }
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002969 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002970 DAG.getConstant(Lane, MVT::i32));
Bob Wilson0ce37102009-08-14 05:08:32 +00002971 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002972
2973 bool ReverseVEXT;
2974 unsigned Imm;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002975 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002976 if (ReverseVEXT)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002977 std::swap(V1, V2);
2978 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002979 DAG.getConstant(Imm, MVT::i32));
2980 }
2981
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002982 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002983 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002984 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002985 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002986 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002987 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
2988
Bob Wilsonc692cb72009-08-21 20:54:19 +00002989 // Check for Neon shuffles that modify both input vectors in place.
2990 // If both results are used, i.e., if there are two shuffles with the same
2991 // source operands and with masks corresponding to both results of one of
2992 // these operations, DAG memoization will ensure that a single node is
2993 // used for both shuffles.
2994 unsigned WhichResult;
2995 if (isVTRNMask(ShuffleMask, VT, WhichResult))
2996 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2997 V1, V2).getValue(WhichResult);
2998 if (isVUZPMask(ShuffleMask, VT, WhichResult))
2999 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3000 V1, V2).getValue(WhichResult);
3001 if (isVZIPMask(ShuffleMask, VT, WhichResult))
3002 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3003 V1, V2).getValue(WhichResult);
3004
Bob Wilson324f4f12009-12-03 06:40:55 +00003005 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
3006 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3007 V1, V1).getValue(WhichResult);
3008 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3009 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3010 V1, V1).getValue(WhichResult);
3011 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3012 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3013 V1, V1).getValue(WhichResult);
3014
Bob Wilsonc692cb72009-08-21 20:54:19 +00003015 // If the shuffle is not directly supported and it has 4 elements, use
3016 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003017 if (VT.getVectorNumElements() == 4 &&
3018 (VT.is128BitVector() || VT.is64BitVector())) {
3019 unsigned PFIndexes[4];
3020 for (unsigned i = 0; i != 4; ++i) {
3021 if (ShuffleMask[i] < 0)
3022 PFIndexes[i] = 8;
3023 else
3024 PFIndexes[i] = ShuffleMask[i];
3025 }
3026
3027 // Compute the index in the perfect shuffle table.
3028 unsigned PFTableIndex =
3029 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3030
3031 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3032 unsigned Cost = (PFEntry >> 30);
3033
3034 if (Cost <= 4)
3035 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
3036 }
Bob Wilsond8e17572009-08-12 22:31:50 +00003037
Bob Wilson63b88452010-05-20 18:39:53 +00003038 // v2f64 and v2i64 shuffles are just register copies.
3039 if (VT == MVT::v2f64 || VT == MVT::v2i64) {
3040 // Do the expansion as f64 since i64 is not legal.
3041 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, V1);
3042 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, V2);
3043 SDValue Val = DAG.getUNDEF(MVT::v2f64);
3044 for (unsigned i = 0; i < 2; ++i) {
3045 if (ShuffleMask[i] < 0)
3046 continue;
3047 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
3048 ShuffleMask[i] < 2 ? V1 : V2,
3049 DAG.getConstant(ShuffleMask[i] & 1, MVT::i32));
3050 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3051 Elt, DAG.getConstant(i, MVT::i32));
3052 }
3053 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
3054 }
3055
Bob Wilson22cac0d2009-08-14 05:16:33 +00003056 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003057}
3058
Bob Wilson5bafff32009-06-22 23:27:02 +00003059static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003060 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003061 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003062 SDValue Vec = Op.getOperand(0);
3063 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00003064 assert(VT == MVT::i32 &&
3065 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
3066 "unexpected type for custom-lowering vector extract");
3067 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00003068}
3069
Bob Wilsona6d65862009-08-03 20:36:38 +00003070static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3071 // The only time a CONCAT_VECTORS operation can have legal types is when
3072 // two 64-bit vectors are concatenated to a 128-bit vector.
3073 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
3074 "unexpected CONCAT_VECTORS");
3075 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00003076 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00003077 SDValue Op0 = Op.getOperand(0);
3078 SDValue Op1 = Op.getOperand(1);
3079 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003080 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3081 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00003082 DAG.getIntPtrConstant(0));
3083 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003084 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3085 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00003086 DAG.getIntPtrConstant(1));
3087 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003088}
3089
Dan Gohmand858e902010-04-17 15:26:15 +00003090SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003091 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003092 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00003093 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00003094 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003095 case ISD::GlobalAddress:
3096 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
3097 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00003098 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00003099 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
3100 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003101 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00003102 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00003103 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00003104 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00003105 case ISD::SINT_TO_FP:
3106 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
3107 case ISD::FP_TO_SINT:
3108 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003109 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00003110 case ISD::RETURNADDR: break;
Jim Grosbach0e0da732009-05-12 23:59:14 +00003111 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003112 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00003113 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
3114 Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00003115 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003116 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00003117 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00003118 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00003119 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003120 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00003121 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00003122 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003123 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
3124 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3125 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003126 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00003127 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003128 }
Dan Gohman475871a2008-07-27 21:46:04 +00003129 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003130}
3131
Duncan Sands1607f052008-12-01 11:39:25 +00003132/// ReplaceNodeResults - Replace the results of node with an illegal result
3133/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00003134void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
3135 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00003136 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00003137 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00003138 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00003139 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003140 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003141 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003142 case ISD::BIT_CONVERT:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003143 Res = ExpandBIT_CONVERT(N, DAG);
3144 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00003145 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003146 case ISD::SRA:
3147 Res = LowerShift(N, DAG, Subtarget);
3148 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003149 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00003150 if (Res.getNode())
3151 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00003152}
Chris Lattner27a6c732007-11-24 07:07:01 +00003153
Evan Chenga8e29892007-01-19 07:51:42 +00003154//===----------------------------------------------------------------------===//
3155// ARM Scheduler Hooks
3156//===----------------------------------------------------------------------===//
3157
3158MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003159ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
3160 MachineBasicBlock *BB,
3161 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00003162 unsigned dest = MI->getOperand(0).getReg();
3163 unsigned ptr = MI->getOperand(1).getReg();
3164 unsigned oldval = MI->getOperand(2).getReg();
3165 unsigned newval = MI->getOperand(3).getReg();
3166 unsigned scratch = BB->getParent()->getRegInfo()
3167 .createVirtualRegister(ARM::GPRRegisterClass);
3168 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3169 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003170 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00003171
3172 unsigned ldrOpc, strOpc;
3173 switch (Size) {
3174 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003175 case 1:
3176 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
3177 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
3178 break;
3179 case 2:
3180 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3181 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3182 break;
3183 case 4:
3184 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3185 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3186 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00003187 }
3188
3189 MachineFunction *MF = BB->getParent();
3190 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3191 MachineFunction::iterator It = BB;
3192 ++It; // insert the new blocks after the current block
3193
3194 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3195 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3196 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3197 MF->insert(It, loop1MBB);
3198 MF->insert(It, loop2MBB);
3199 MF->insert(It, exitMBB);
3200 exitMBB->transferSuccessors(BB);
3201
3202 // thisMBB:
3203 // ...
3204 // fallthrough --> loop1MBB
3205 BB->addSuccessor(loop1MBB);
3206
3207 // loop1MBB:
3208 // ldrex dest, [ptr]
3209 // cmp dest, oldval
3210 // bne exitMBB
3211 BB = loop1MBB;
3212 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003213 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003214 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003215 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3216 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003217 BB->addSuccessor(loop2MBB);
3218 BB->addSuccessor(exitMBB);
3219
3220 // loop2MBB:
3221 // strex scratch, newval, [ptr]
3222 // cmp scratch, #0
3223 // bne loop1MBB
3224 BB = loop2MBB;
3225 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
3226 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003227 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003228 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003229 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3230 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003231 BB->addSuccessor(loop1MBB);
3232 BB->addSuccessor(exitMBB);
3233
3234 // exitMBB:
3235 // ...
3236 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00003237
3238 MF->DeleteMachineInstr(MI); // The instruction is gone now.
3239
Jim Grosbach5278eb82009-12-11 01:42:04 +00003240 return BB;
3241}
3242
3243MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003244ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
3245 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00003246 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
3247 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3248
3249 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003250 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003251 MachineFunction::iterator It = BB;
3252 ++It;
3253
3254 unsigned dest = MI->getOperand(0).getReg();
3255 unsigned ptr = MI->getOperand(1).getReg();
3256 unsigned incr = MI->getOperand(2).getReg();
3257 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00003258
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003259 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003260 unsigned ldrOpc, strOpc;
3261 switch (Size) {
3262 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003263 case 1:
3264 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00003265 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003266 break;
3267 case 2:
3268 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3269 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3270 break;
3271 case 4:
3272 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3273 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3274 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00003275 }
3276
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003277 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3278 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3279 MF->insert(It, loopMBB);
3280 MF->insert(It, exitMBB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003281 exitMBB->transferSuccessors(BB);
3282
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003283 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003284 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3285 unsigned scratch2 = (!BinOpcode) ? incr :
3286 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3287
3288 // thisMBB:
3289 // ...
3290 // fallthrough --> loopMBB
3291 BB->addSuccessor(loopMBB);
3292
3293 // loopMBB:
3294 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003295 // <binop> scratch2, dest, incr
3296 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00003297 // cmp scratch, #0
3298 // bne- loopMBB
3299 // fallthrough --> exitMBB
3300 BB = loopMBB;
3301 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00003302 if (BinOpcode) {
3303 // operand order needs to go the other way for NAND
3304 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
3305 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3306 addReg(incr).addReg(dest)).addReg(0);
3307 else
3308 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3309 addReg(dest).addReg(incr)).addReg(0);
3310 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00003311
3312 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
3313 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003314 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00003315 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003316 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3317 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003318
3319 BB->addSuccessor(loopMBB);
3320 BB->addSuccessor(exitMBB);
3321
3322 // exitMBB:
3323 // ...
3324 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00003325
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003326 MF->DeleteMachineInstr(MI); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00003327
Jim Grosbachc3c23542009-12-14 04:22:04 +00003328 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00003329}
3330
3331MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00003332ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003333 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003334 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00003335 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003336 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00003337 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00003338 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00003339 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00003340 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00003341
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003342 case ARM::ATOMIC_LOAD_ADD_I8:
3343 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3344 case ARM::ATOMIC_LOAD_ADD_I16:
3345 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3346 case ARM::ATOMIC_LOAD_ADD_I32:
3347 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003348
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003349 case ARM::ATOMIC_LOAD_AND_I8:
3350 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3351 case ARM::ATOMIC_LOAD_AND_I16:
3352 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3353 case ARM::ATOMIC_LOAD_AND_I32:
3354 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003355
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003356 case ARM::ATOMIC_LOAD_OR_I8:
3357 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3358 case ARM::ATOMIC_LOAD_OR_I16:
3359 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3360 case ARM::ATOMIC_LOAD_OR_I32:
3361 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003362
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003363 case ARM::ATOMIC_LOAD_XOR_I8:
3364 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3365 case ARM::ATOMIC_LOAD_XOR_I16:
3366 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3367 case ARM::ATOMIC_LOAD_XOR_I32:
3368 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003369
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003370 case ARM::ATOMIC_LOAD_NAND_I8:
3371 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3372 case ARM::ATOMIC_LOAD_NAND_I16:
3373 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3374 case ARM::ATOMIC_LOAD_NAND_I32:
3375 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003376
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003377 case ARM::ATOMIC_LOAD_SUB_I8:
3378 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3379 case ARM::ATOMIC_LOAD_SUB_I16:
3380 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3381 case ARM::ATOMIC_LOAD_SUB_I32:
3382 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003383
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003384 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
3385 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
3386 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00003387
3388 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
3389 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
3390 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003391
Evan Cheng007ea272009-08-12 05:17:19 +00003392 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00003393 // To "insert" a SELECT_CC instruction, we actually have to insert the
3394 // diamond control-flow pattern. The incoming instruction knows the
3395 // destination vreg to set, the condition code register to branch on, the
3396 // true/false values to select between, and a branch opcode to use.
3397 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003398 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00003399 ++It;
3400
3401 // thisMBB:
3402 // ...
3403 // TrueVal = ...
3404 // cmpTY ccX, r1, r2
3405 // bCC copy1MBB
3406 // fallthrough --> copy0MBB
3407 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003408 MachineFunction *F = BB->getParent();
3409 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3410 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00003411 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00003412 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003413 F->insert(It, copy0MBB);
3414 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00003415 // Update machine-CFG edges by first adding all successors of the current
3416 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00003417 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003418 E = BB->succ_end(); I != E; ++I)
Evan Chengce319102009-09-19 09:51:03 +00003419 sinkMBB->addSuccessor(*I);
Evan Chenga8e29892007-01-19 07:51:42 +00003420 // Next, remove all successors of the current block, and add the true
3421 // and fallthrough blocks as its successors.
Evan Chengce319102009-09-19 09:51:03 +00003422 while (!BB->succ_empty())
Evan Chenga8e29892007-01-19 07:51:42 +00003423 BB->removeSuccessor(BB->succ_begin());
3424 BB->addSuccessor(copy0MBB);
3425 BB->addSuccessor(sinkMBB);
3426
3427 // copy0MBB:
3428 // %FalseValue = ...
3429 // # fallthrough to sinkMBB
3430 BB = copy0MBB;
3431
3432 // Update machine-CFG edges
3433 BB->addSuccessor(sinkMBB);
3434
3435 // sinkMBB:
3436 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3437 // ...
3438 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00003439 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00003440 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
3441 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
3442
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003443 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00003444 return BB;
3445 }
Evan Cheng86198642009-08-07 00:34:42 +00003446
3447 case ARM::tANDsp:
3448 case ARM::tADDspr_:
3449 case ARM::tSUBspi_:
3450 case ARM::t2SUBrSPi_:
3451 case ARM::t2SUBrSPi12_:
3452 case ARM::t2SUBrSPs_: {
3453 MachineFunction *MF = BB->getParent();
3454 unsigned DstReg = MI->getOperand(0).getReg();
3455 unsigned SrcReg = MI->getOperand(1).getReg();
3456 bool DstIsDead = MI->getOperand(0).isDead();
3457 bool SrcIsKill = MI->getOperand(1).isKill();
3458
3459 if (SrcReg != ARM::SP) {
3460 // Copy the source to SP from virtual register.
3461 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
3462 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3463 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
3464 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
3465 .addReg(SrcReg, getKillRegState(SrcIsKill));
3466 }
3467
3468 unsigned OpOpc = 0;
3469 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
3470 switch (MI->getOpcode()) {
3471 default:
3472 llvm_unreachable("Unexpected pseudo instruction!");
3473 case ARM::tANDsp:
3474 OpOpc = ARM::tAND;
3475 NeedPred = true;
3476 break;
3477 case ARM::tADDspr_:
3478 OpOpc = ARM::tADDspr;
3479 break;
3480 case ARM::tSUBspi_:
3481 OpOpc = ARM::tSUBspi;
3482 break;
3483 case ARM::t2SUBrSPi_:
3484 OpOpc = ARM::t2SUBrSPi;
3485 NeedPred = true; NeedCC = true;
3486 break;
3487 case ARM::t2SUBrSPi12_:
3488 OpOpc = ARM::t2SUBrSPi12;
3489 NeedPred = true;
3490 break;
3491 case ARM::t2SUBrSPs_:
3492 OpOpc = ARM::t2SUBrSPs;
3493 NeedPred = true; NeedCC = true; NeedOp3 = true;
3494 break;
3495 }
3496 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
3497 if (OpOpc == ARM::tAND)
3498 AddDefaultT1CC(MIB);
3499 MIB.addReg(ARM::SP);
3500 MIB.addOperand(MI->getOperand(2));
3501 if (NeedOp3)
3502 MIB.addOperand(MI->getOperand(3));
3503 if (NeedPred)
3504 AddDefaultPred(MIB);
3505 if (NeedCC)
3506 AddDefaultCC(MIB);
3507
3508 // Copy the result from SP to virtual register.
3509 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
3510 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3511 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
3512 BuildMI(BB, dl, TII->get(CopyOpc))
3513 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
3514 .addReg(ARM::SP);
3515 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3516 return BB;
3517 }
Evan Chenga8e29892007-01-19 07:51:42 +00003518 }
3519}
3520
3521//===----------------------------------------------------------------------===//
3522// ARM Optimization Hooks
3523//===----------------------------------------------------------------------===//
3524
Chris Lattnerd1980a52009-03-12 06:52:53 +00003525static
3526SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3527 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00003528 SelectionDAG &DAG = DCI.DAG;
3529 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003530 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00003531 unsigned Opc = N->getOpcode();
3532 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3533 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3534 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3535 ISD::CondCode CC = ISD::SETCC_INVALID;
3536
3537 if (isSlctCC) {
3538 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
3539 } else {
3540 SDValue CCOp = Slct.getOperand(0);
3541 if (CCOp.getOpcode() == ISD::SETCC)
3542 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
3543 }
3544
3545 bool DoXform = false;
3546 bool InvCC = false;
3547 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
3548 "Bad input!");
3549
3550 if (LHS.getOpcode() == ISD::Constant &&
3551 cast<ConstantSDNode>(LHS)->isNullValue()) {
3552 DoXform = true;
3553 } else if (CC != ISD::SETCC_INVALID &&
3554 RHS.getOpcode() == ISD::Constant &&
3555 cast<ConstantSDNode>(RHS)->isNullValue()) {
3556 std::swap(LHS, RHS);
3557 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003558 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00003559 Op0.getOperand(0).getValueType();
3560 bool isInt = OpVT.isInteger();
3561 CC = ISD::getSetCCInverse(CC, isInt);
3562
3563 if (!TLI.isCondCodeLegal(CC, OpVT))
3564 return SDValue(); // Inverse operator isn't legal.
3565
3566 DoXform = true;
3567 InvCC = true;
3568 }
3569
3570 if (DoXform) {
3571 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3572 if (isSlctCC)
3573 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3574 Slct.getOperand(0), Slct.getOperand(1), CC);
3575 SDValue CCOp = Slct.getOperand(0);
3576 if (InvCC)
3577 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3578 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3579 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3580 CCOp, OtherOp, Result);
3581 }
3582 return SDValue();
3583}
3584
3585/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3586static SDValue PerformADDCombine(SDNode *N,
3587 TargetLowering::DAGCombinerInfo &DCI) {
3588 // added by evan in r37685 with no testcase.
3589 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003590
Chris Lattnerd1980a52009-03-12 06:52:53 +00003591 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3592 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3593 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3594 if (Result.getNode()) return Result;
3595 }
3596 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3597 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3598 if (Result.getNode()) return Result;
3599 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003600
Chris Lattnerd1980a52009-03-12 06:52:53 +00003601 return SDValue();
3602}
3603
3604/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3605static SDValue PerformSUBCombine(SDNode *N,
3606 TargetLowering::DAGCombinerInfo &DCI) {
3607 // added by evan in r37685 with no testcase.
3608 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003609
Chris Lattnerd1980a52009-03-12 06:52:53 +00003610 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3611 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3612 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3613 if (Result.getNode()) return Result;
3614 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003615
Chris Lattnerd1980a52009-03-12 06:52:53 +00003616 return SDValue();
3617}
3618
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003619static SDValue PerformMULCombine(SDNode *N,
3620 TargetLowering::DAGCombinerInfo &DCI,
3621 const ARMSubtarget *Subtarget) {
3622 SelectionDAG &DAG = DCI.DAG;
3623
3624 if (Subtarget->isThumb1Only())
3625 return SDValue();
3626
3627 if (DAG.getMachineFunction().
3628 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
3629 return SDValue();
3630
3631 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
3632 return SDValue();
3633
3634 EVT VT = N->getValueType(0);
3635 if (VT != MVT::i32)
3636 return SDValue();
3637
3638 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
3639 if (!C)
3640 return SDValue();
3641
3642 uint64_t MulAmt = C->getZExtValue();
3643 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
3644 ShiftAmt = ShiftAmt & (32 - 1);
3645 SDValue V = N->getOperand(0);
3646 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003647
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003648 SDValue Res;
3649 MulAmt >>= ShiftAmt;
3650 if (isPowerOf2_32(MulAmt - 1)) {
3651 // (mul x, 2^N + 1) => (add (shl x, N), x)
3652 Res = DAG.getNode(ISD::ADD, DL, VT,
3653 V, DAG.getNode(ISD::SHL, DL, VT,
3654 V, DAG.getConstant(Log2_32(MulAmt-1),
3655 MVT::i32)));
3656 } else if (isPowerOf2_32(MulAmt + 1)) {
3657 // (mul x, 2^N - 1) => (sub (shl x, N), x)
3658 Res = DAG.getNode(ISD::SUB, DL, VT,
3659 DAG.getNode(ISD::SHL, DL, VT,
3660 V, DAG.getConstant(Log2_32(MulAmt+1),
3661 MVT::i32)),
3662 V);
3663 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003664 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003665
3666 if (ShiftAmt != 0)
3667 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
3668 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003669
3670 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003671 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003672 return SDValue();
3673}
3674
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +00003675/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
3676/// ARMISD::VMOVRRD.
Jim Grosbache5165492009-11-09 00:11:35 +00003677static SDValue PerformVMOVRRDCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003678 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003679 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003680 SDValue InDouble = N->getOperand(0);
Jim Grosbache5165492009-11-09 00:11:35 +00003681 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003682 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003683 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003684}
3685
Bob Wilson5bafff32009-06-22 23:27:02 +00003686/// getVShiftImm - Check if this is a valid build_vector for the immediate
3687/// operand of a vector shift operation, where all the elements of the
3688/// build_vector must have the same constant integer value.
3689static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3690 // Ignore bit_converts.
3691 while (Op.getOpcode() == ISD::BIT_CONVERT)
3692 Op = Op.getOperand(0);
3693 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3694 APInt SplatBits, SplatUndef;
3695 unsigned SplatBitSize;
3696 bool HasAnyUndefs;
3697 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3698 HasAnyUndefs, ElementBits) ||
3699 SplatBitSize > ElementBits)
3700 return false;
3701 Cnt = SplatBits.getSExtValue();
3702 return true;
3703}
3704
3705/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3706/// operand of a vector shift left operation. That value must be in the range:
3707/// 0 <= Value < ElementBits for a left shift; or
3708/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003709static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003710 assert(VT.isVector() && "vector shift count is not a vector type");
3711 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3712 if (! getVShiftImm(Op, ElementBits, Cnt))
3713 return false;
3714 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3715}
3716
3717/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3718/// operand of a vector shift right operation. For a shift opcode, the value
3719/// is positive, but for an intrinsic the value count must be negative. The
3720/// absolute value must be in the range:
3721/// 1 <= |Value| <= ElementBits for a right shift; or
3722/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003723static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00003724 int64_t &Cnt) {
3725 assert(VT.isVector() && "vector shift count is not a vector type");
3726 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3727 if (! getVShiftImm(Op, ElementBits, Cnt))
3728 return false;
3729 if (isIntrinsic)
3730 Cnt = -Cnt;
3731 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3732}
3733
3734/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3735static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3736 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3737 switch (IntNo) {
3738 default:
3739 // Don't do anything for most intrinsics.
3740 break;
3741
3742 // Vector shifts: check for immediate versions and lower them.
3743 // Note: This is done during DAG combining instead of DAG legalizing because
3744 // the build_vectors for 64-bit vector element shift counts are generally
3745 // not legal, and it is hard to see their values after they get legalized to
3746 // loads from a constant pool.
3747 case Intrinsic::arm_neon_vshifts:
3748 case Intrinsic::arm_neon_vshiftu:
3749 case Intrinsic::arm_neon_vshiftls:
3750 case Intrinsic::arm_neon_vshiftlu:
3751 case Intrinsic::arm_neon_vshiftn:
3752 case Intrinsic::arm_neon_vrshifts:
3753 case Intrinsic::arm_neon_vrshiftu:
3754 case Intrinsic::arm_neon_vrshiftn:
3755 case Intrinsic::arm_neon_vqshifts:
3756 case Intrinsic::arm_neon_vqshiftu:
3757 case Intrinsic::arm_neon_vqshiftsu:
3758 case Intrinsic::arm_neon_vqshiftns:
3759 case Intrinsic::arm_neon_vqshiftnu:
3760 case Intrinsic::arm_neon_vqshiftnsu:
3761 case Intrinsic::arm_neon_vqrshiftns:
3762 case Intrinsic::arm_neon_vqrshiftnu:
3763 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00003764 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003765 int64_t Cnt;
3766 unsigned VShiftOpc = 0;
3767
3768 switch (IntNo) {
3769 case Intrinsic::arm_neon_vshifts:
3770 case Intrinsic::arm_neon_vshiftu:
3771 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3772 VShiftOpc = ARMISD::VSHL;
3773 break;
3774 }
3775 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3776 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3777 ARMISD::VSHRs : ARMISD::VSHRu);
3778 break;
3779 }
3780 return SDValue();
3781
3782 case Intrinsic::arm_neon_vshiftls:
3783 case Intrinsic::arm_neon_vshiftlu:
3784 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3785 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003786 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003787
3788 case Intrinsic::arm_neon_vrshifts:
3789 case Intrinsic::arm_neon_vrshiftu:
3790 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3791 break;
3792 return SDValue();
3793
3794 case Intrinsic::arm_neon_vqshifts:
3795 case Intrinsic::arm_neon_vqshiftu:
3796 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3797 break;
3798 return SDValue();
3799
3800 case Intrinsic::arm_neon_vqshiftsu:
3801 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3802 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003803 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003804
3805 case Intrinsic::arm_neon_vshiftn:
3806 case Intrinsic::arm_neon_vrshiftn:
3807 case Intrinsic::arm_neon_vqshiftns:
3808 case Intrinsic::arm_neon_vqshiftnu:
3809 case Intrinsic::arm_neon_vqshiftnsu:
3810 case Intrinsic::arm_neon_vqrshiftns:
3811 case Intrinsic::arm_neon_vqrshiftnu:
3812 case Intrinsic::arm_neon_vqrshiftnsu:
3813 // Narrowing shifts require an immediate right shift.
3814 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3815 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003816 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003817
3818 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003819 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003820 }
3821
3822 switch (IntNo) {
3823 case Intrinsic::arm_neon_vshifts:
3824 case Intrinsic::arm_neon_vshiftu:
3825 // Opcode already set above.
3826 break;
3827 case Intrinsic::arm_neon_vshiftls:
3828 case Intrinsic::arm_neon_vshiftlu:
3829 if (Cnt == VT.getVectorElementType().getSizeInBits())
3830 VShiftOpc = ARMISD::VSHLLi;
3831 else
3832 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3833 ARMISD::VSHLLs : ARMISD::VSHLLu);
3834 break;
3835 case Intrinsic::arm_neon_vshiftn:
3836 VShiftOpc = ARMISD::VSHRN; break;
3837 case Intrinsic::arm_neon_vrshifts:
3838 VShiftOpc = ARMISD::VRSHRs; break;
3839 case Intrinsic::arm_neon_vrshiftu:
3840 VShiftOpc = ARMISD::VRSHRu; break;
3841 case Intrinsic::arm_neon_vrshiftn:
3842 VShiftOpc = ARMISD::VRSHRN; break;
3843 case Intrinsic::arm_neon_vqshifts:
3844 VShiftOpc = ARMISD::VQSHLs; break;
3845 case Intrinsic::arm_neon_vqshiftu:
3846 VShiftOpc = ARMISD::VQSHLu; break;
3847 case Intrinsic::arm_neon_vqshiftsu:
3848 VShiftOpc = ARMISD::VQSHLsu; break;
3849 case Intrinsic::arm_neon_vqshiftns:
3850 VShiftOpc = ARMISD::VQSHRNs; break;
3851 case Intrinsic::arm_neon_vqshiftnu:
3852 VShiftOpc = ARMISD::VQSHRNu; break;
3853 case Intrinsic::arm_neon_vqshiftnsu:
3854 VShiftOpc = ARMISD::VQSHRNsu; break;
3855 case Intrinsic::arm_neon_vqrshiftns:
3856 VShiftOpc = ARMISD::VQRSHRNs; break;
3857 case Intrinsic::arm_neon_vqrshiftnu:
3858 VShiftOpc = ARMISD::VQRSHRNu; break;
3859 case Intrinsic::arm_neon_vqrshiftnsu:
3860 VShiftOpc = ARMISD::VQRSHRNsu; break;
3861 }
3862
3863 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003864 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003865 }
3866
3867 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00003868 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003869 int64_t Cnt;
3870 unsigned VShiftOpc = 0;
3871
3872 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3873 VShiftOpc = ARMISD::VSLI;
3874 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3875 VShiftOpc = ARMISD::VSRI;
3876 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003877 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003878 }
3879
3880 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3881 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00003882 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003883 }
3884
3885 case Intrinsic::arm_neon_vqrshifts:
3886 case Intrinsic::arm_neon_vqrshiftu:
3887 // No immediate versions of these to check for.
3888 break;
3889 }
3890
3891 return SDValue();
3892}
3893
3894/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3895/// lowers them. As with the vector shift intrinsics, this is done during DAG
3896/// combining instead of DAG legalizing because the build_vectors for 64-bit
3897/// vector element shift counts are generally not legal, and it is hard to see
3898/// their values after they get legalized to loads from a constant pool.
3899static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3900 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003901 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003902
3903 // Nothing to be done for scalar shifts.
3904 if (! VT.isVector())
3905 return SDValue();
3906
3907 assert(ST->hasNEON() && "unexpected vector shift");
3908 int64_t Cnt;
3909
3910 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003911 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003912
3913 case ISD::SHL:
3914 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3915 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003916 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003917 break;
3918
3919 case ISD::SRA:
3920 case ISD::SRL:
3921 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3922 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3923 ARMISD::VSHRs : ARMISD::VSHRu);
3924 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003925 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003926 }
3927 }
3928 return SDValue();
3929}
3930
3931/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3932/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3933static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3934 const ARMSubtarget *ST) {
3935 SDValue N0 = N->getOperand(0);
3936
3937 // Check for sign- and zero-extensions of vector extract operations of 8-
3938 // and 16-bit vector elements. NEON supports these directly. They are
3939 // handled during DAG combining because type legalization will promote them
3940 // to 32-bit types and it is messy to recognize the operations after that.
3941 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3942 SDValue Vec = N0.getOperand(0);
3943 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003944 EVT VT = N->getValueType(0);
3945 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003946 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3947
Owen Anderson825b72b2009-08-11 20:47:22 +00003948 if (VT == MVT::i32 &&
3949 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00003950 TLI.isTypeLegal(Vec.getValueType())) {
3951
3952 unsigned Opc = 0;
3953 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003954 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003955 case ISD::SIGN_EXTEND:
3956 Opc = ARMISD::VGETLANEs;
3957 break;
3958 case ISD::ZERO_EXTEND:
3959 case ISD::ANY_EXTEND:
3960 Opc = ARMISD::VGETLANEu;
3961 break;
3962 }
3963 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
3964 }
3965 }
3966
3967 return SDValue();
3968}
3969
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003970/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
3971/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
3972static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
3973 const ARMSubtarget *ST) {
3974 // If the target supports NEON, try to use vmax/vmin instructions for f32
3975 // selects like "x < y ? x : y". Unless the FiniteOnlyFPMath option is set,
3976 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
3977 // a NaN; only do the transformation when it matches that behavior.
3978
3979 // For now only do this when using NEON for FP operations; if using VFP, it
3980 // is not obvious that the benefit outweighs the cost of switching to the
3981 // NEON pipeline.
3982 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
3983 N->getValueType(0) != MVT::f32)
3984 return SDValue();
3985
3986 SDValue CondLHS = N->getOperand(0);
3987 SDValue CondRHS = N->getOperand(1);
3988 SDValue LHS = N->getOperand(2);
3989 SDValue RHS = N->getOperand(3);
3990 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
3991
3992 unsigned Opcode = 0;
3993 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00003994 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003995 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00003996 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003997 IsReversed = true ; // x CC y ? y : x
3998 } else {
3999 return SDValue();
4000 }
4001
Bob Wilsone742bb52010-02-24 22:15:53 +00004002 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004003 switch (CC) {
4004 default: break;
4005 case ISD::SETOLT:
4006 case ISD::SETOLE:
4007 case ISD::SETLT:
4008 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004009 case ISD::SETULT:
4010 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004011 // If LHS is NaN, an ordered comparison will be false and the result will
4012 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
4013 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4014 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
4015 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4016 break;
4017 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
4018 // will return -0, so vmin can only be used for unsafe math or if one of
4019 // the operands is known to be nonzero.
4020 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
4021 !UnsafeFPMath &&
4022 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4023 break;
4024 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004025 break;
4026
4027 case ISD::SETOGT:
4028 case ISD::SETOGE:
4029 case ISD::SETGT:
4030 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004031 case ISD::SETUGT:
4032 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004033 // If LHS is NaN, an ordered comparison will be false and the result will
4034 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
4035 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4036 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
4037 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4038 break;
4039 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
4040 // will return +0, so vmax can only be used for unsafe math or if one of
4041 // the operands is known to be nonzero.
4042 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
4043 !UnsafeFPMath &&
4044 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4045 break;
4046 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004047 break;
4048 }
4049
4050 if (!Opcode)
4051 return SDValue();
4052 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
4053}
4054
Dan Gohman475871a2008-07-27 21:46:04 +00004055SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004056 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004057 switch (N->getOpcode()) {
4058 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004059 case ISD::ADD: return PerformADDCombine(N, DCI);
4060 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004061 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbache5165492009-11-09 00:11:35 +00004062 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004063 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004064 case ISD::SHL:
4065 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004066 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004067 case ISD::SIGN_EXTEND:
4068 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004069 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
4070 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004071 }
Dan Gohman475871a2008-07-27 21:46:04 +00004072 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004073}
4074
Bill Wendlingaf566342009-08-15 21:21:19 +00004075bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
4076 if (!Subtarget->hasV6Ops())
4077 // Pre-v6 does not support unaligned mem access.
4078 return false;
Anton Korobeynikov90cfc132010-01-30 14:08:12 +00004079 else {
4080 // v6+ may or may not support unaligned mem access depending on the system
4081 // configuration.
4082 // FIXME: This is pretty conservative. Should we provide cmdline option to
4083 // control the behaviour?
Bill Wendlingaf566342009-08-15 21:21:19 +00004084 if (!Subtarget->isTargetDarwin())
4085 return false;
4086 }
4087
4088 switch (VT.getSimpleVT().SimpleTy) {
4089 default:
4090 return false;
4091 case MVT::i8:
4092 case MVT::i16:
4093 case MVT::i32:
4094 return true;
4095 // FIXME: VLD1 etc with standard alignment is legal.
4096 }
4097}
4098
Evan Chenge6c835f2009-08-14 20:09:37 +00004099static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
4100 if (V < 0)
4101 return false;
4102
4103 unsigned Scale = 1;
4104 switch (VT.getSimpleVT().SimpleTy) {
4105 default: return false;
4106 case MVT::i1:
4107 case MVT::i8:
4108 // Scale == 1;
4109 break;
4110 case MVT::i16:
4111 // Scale == 2;
4112 Scale = 2;
4113 break;
4114 case MVT::i32:
4115 // Scale == 4;
4116 Scale = 4;
4117 break;
4118 }
4119
4120 if ((V & (Scale - 1)) != 0)
4121 return false;
4122 V /= Scale;
4123 return V == (V & ((1LL << 5) - 1));
4124}
4125
4126static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
4127 const ARMSubtarget *Subtarget) {
4128 bool isNeg = false;
4129 if (V < 0) {
4130 isNeg = true;
4131 V = - V;
4132 }
4133
4134 switch (VT.getSimpleVT().SimpleTy) {
4135 default: return false;
4136 case MVT::i1:
4137 case MVT::i8:
4138 case MVT::i16:
4139 case MVT::i32:
4140 // + imm12 or - imm8
4141 if (isNeg)
4142 return V == (V & ((1LL << 8) - 1));
4143 return V == (V & ((1LL << 12) - 1));
4144 case MVT::f32:
4145 case MVT::f64:
4146 // Same as ARM mode. FIXME: NEON?
4147 if (!Subtarget->hasVFP2())
4148 return false;
4149 if ((V & 3) != 0)
4150 return false;
4151 V >>= 2;
4152 return V == (V & ((1LL << 8) - 1));
4153 }
4154}
4155
Evan Chengb01fad62007-03-12 23:30:29 +00004156/// isLegalAddressImmediate - Return true if the integer value can be used
4157/// as the offset of the target addressing mode for load / store of the
4158/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00004159static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004160 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00004161 if (V == 0)
4162 return true;
4163
Evan Cheng65011532009-03-09 19:15:00 +00004164 if (!VT.isSimple())
4165 return false;
4166
Evan Chenge6c835f2009-08-14 20:09:37 +00004167 if (Subtarget->isThumb1Only())
4168 return isLegalT1AddressImmediate(V, VT);
4169 else if (Subtarget->isThumb2())
4170 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00004171
Evan Chenge6c835f2009-08-14 20:09:37 +00004172 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00004173 if (V < 0)
4174 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00004175 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00004176 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004177 case MVT::i1:
4178 case MVT::i8:
4179 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00004180 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004181 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004182 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00004183 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004184 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004185 case MVT::f32:
4186 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00004187 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00004188 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00004189 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00004190 return false;
4191 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004192 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00004193 }
Evan Chenga8e29892007-01-19 07:51:42 +00004194}
4195
Evan Chenge6c835f2009-08-14 20:09:37 +00004196bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
4197 EVT VT) const {
4198 int Scale = AM.Scale;
4199 if (Scale < 0)
4200 return false;
4201
4202 switch (VT.getSimpleVT().SimpleTy) {
4203 default: return false;
4204 case MVT::i1:
4205 case MVT::i8:
4206 case MVT::i16:
4207 case MVT::i32:
4208 if (Scale == 1)
4209 return true;
4210 // r + r << imm
4211 Scale = Scale & ~1;
4212 return Scale == 2 || Scale == 4 || Scale == 8;
4213 case MVT::i64:
4214 // r + r
4215 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
4216 return true;
4217 return false;
4218 case MVT::isVoid:
4219 // Note, we allow "void" uses (basically, uses that aren't loads or
4220 // stores), because arm allows folding a scale into many arithmetic
4221 // operations. This should be made more precise and revisited later.
4222
4223 // Allow r << imm, but the imm has to be a multiple of two.
4224 if (Scale & 1) return false;
4225 return isPowerOf2_32(Scale);
4226 }
4227}
4228
Chris Lattner37caf8c2007-04-09 23:33:39 +00004229/// isLegalAddressingMode - Return true if the addressing mode represented
4230/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004231bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004232 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004233 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00004234 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00004235 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004236
Chris Lattner37caf8c2007-04-09 23:33:39 +00004237 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004238 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004239 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004240
Chris Lattner37caf8c2007-04-09 23:33:39 +00004241 switch (AM.Scale) {
4242 case 0: // no scale reg, must be "r+i" or "r", or "i".
4243 break;
4244 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00004245 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00004246 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004247 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00004248 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004249 // ARM doesn't support any R+R*scale+imm addr modes.
4250 if (AM.BaseOffs)
4251 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004252
Bob Wilson2c7dab12009-04-08 17:55:28 +00004253 if (!VT.isSimple())
4254 return false;
4255
Evan Chenge6c835f2009-08-14 20:09:37 +00004256 if (Subtarget->isThumb2())
4257 return isLegalT2ScaledAddressingMode(AM, VT);
4258
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004259 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00004260 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00004261 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004262 case MVT::i1:
4263 case MVT::i8:
4264 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004265 if (Scale < 0) Scale = -Scale;
4266 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004267 return true;
4268 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00004269 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00004270 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00004271 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004272 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004273 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004274 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00004275 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004276
Owen Anderson825b72b2009-08-11 20:47:22 +00004277 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004278 // Note, we allow "void" uses (basically, uses that aren't loads or
4279 // stores), because arm allows folding a scale into many arithmetic
4280 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004281
Chris Lattner37caf8c2007-04-09 23:33:39 +00004282 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00004283 if (Scale & 1) return false;
4284 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00004285 }
4286 break;
Evan Chengb01fad62007-03-12 23:30:29 +00004287 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00004288 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00004289}
4290
Evan Cheng77e47512009-11-11 19:05:52 +00004291/// isLegalICmpImmediate - Return true if the specified immediate is legal
4292/// icmp immediate, that is the target has icmp instructions which can compare
4293/// a register against the immediate without having to materialize the
4294/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00004295bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00004296 if (!Subtarget->isThumb())
4297 return ARM_AM::getSOImmVal(Imm) != -1;
4298 if (Subtarget->isThumb2())
4299 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00004300 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00004301}
4302
Owen Andersone50ed302009-08-10 22:56:29 +00004303static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004304 bool isSEXTLoad, SDValue &Base,
4305 SDValue &Offset, bool &isInc,
4306 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00004307 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4308 return false;
4309
Owen Anderson825b72b2009-08-11 20:47:22 +00004310 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00004311 // AddressingMode 3
4312 Base = Ptr->getOperand(0);
4313 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004314 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004315 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004316 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004317 isInc = false;
4318 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4319 return true;
4320 }
4321 }
4322 isInc = (Ptr->getOpcode() == ISD::ADD);
4323 Offset = Ptr->getOperand(1);
4324 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004325 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00004326 // AddressingMode 2
4327 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004328 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004329 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004330 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004331 isInc = false;
4332 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4333 Base = Ptr->getOperand(0);
4334 return true;
4335 }
4336 }
4337
4338 if (Ptr->getOpcode() == ISD::ADD) {
4339 isInc = true;
4340 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
4341 if (ShOpcVal != ARM_AM::no_shift) {
4342 Base = Ptr->getOperand(1);
4343 Offset = Ptr->getOperand(0);
4344 } else {
4345 Base = Ptr->getOperand(0);
4346 Offset = Ptr->getOperand(1);
4347 }
4348 return true;
4349 }
4350
4351 isInc = (Ptr->getOpcode() == ISD::ADD);
4352 Base = Ptr->getOperand(0);
4353 Offset = Ptr->getOperand(1);
4354 return true;
4355 }
4356
Jim Grosbache5165492009-11-09 00:11:35 +00004357 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00004358 return false;
4359}
4360
Owen Andersone50ed302009-08-10 22:56:29 +00004361static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004362 bool isSEXTLoad, SDValue &Base,
4363 SDValue &Offset, bool &isInc,
4364 SelectionDAG &DAG) {
4365 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4366 return false;
4367
4368 Base = Ptr->getOperand(0);
4369 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
4370 int RHSC = (int)RHS->getZExtValue();
4371 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
4372 assert(Ptr->getOpcode() == ISD::ADD);
4373 isInc = false;
4374 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4375 return true;
4376 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
4377 isInc = Ptr->getOpcode() == ISD::ADD;
4378 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
4379 return true;
4380 }
4381 }
4382
4383 return false;
4384}
4385
Evan Chenga8e29892007-01-19 07:51:42 +00004386/// getPreIndexedAddressParts - returns true by value, base pointer and
4387/// offset pointer and addressing mode by reference if the node's address
4388/// can be legally represented as pre-indexed load / store address.
4389bool
Dan Gohman475871a2008-07-27 21:46:04 +00004390ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
4391 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004392 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004393 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004394 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004395 return false;
4396
Owen Andersone50ed302009-08-10 22:56:29 +00004397 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004398 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004399 bool isSEXTLoad = false;
4400 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4401 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004402 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004403 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4404 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
4405 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004406 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004407 } else
4408 return false;
4409
4410 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004411 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004412 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004413 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
4414 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004415 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004416 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00004417 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00004418 if (!isLegal)
4419 return false;
4420
4421 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
4422 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004423}
4424
4425/// getPostIndexedAddressParts - returns true by value, base pointer and
4426/// offset pointer and addressing mode by reference if this node can be
4427/// combined with a load / store to form a post-indexed load / store.
4428bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00004429 SDValue &Base,
4430 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004431 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004432 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004433 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004434 return false;
4435
Owen Andersone50ed302009-08-10 22:56:29 +00004436 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004437 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004438 bool isSEXTLoad = false;
4439 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004440 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00004441 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00004442 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4443 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004444 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00004445 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00004446 } else
4447 return false;
4448
4449 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004450 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004451 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004452 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00004453 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004454 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004455 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
4456 isInc, DAG);
4457 if (!isLegal)
4458 return false;
4459
Evan Cheng28dad2a2010-05-18 21:31:17 +00004460 if (Ptr != Base) {
4461 // Swap base ptr and offset to catch more post-index load / store when
4462 // it's legal. In Thumb2 mode, offset must be an immediate.
4463 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
4464 !Subtarget->isThumb2())
4465 std::swap(Base, Offset);
4466
4467 // Post-indexed load / store update the base pointer.
4468 if (Ptr != Base)
4469 return false;
4470 }
4471
Evan Chenge88d5ce2009-07-02 07:28:31 +00004472 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
4473 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004474}
4475
Dan Gohman475871a2008-07-27 21:46:04 +00004476void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00004477 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004478 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004479 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00004480 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00004481 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004482 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004483 switch (Op.getOpcode()) {
4484 default: break;
4485 case ARMISD::CMOV: {
4486 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00004487 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004488 if (KnownZero == 0 && KnownOne == 0) return;
4489
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004490 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00004491 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
4492 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004493 KnownZero &= KnownZeroRHS;
4494 KnownOne &= KnownOneRHS;
4495 return;
4496 }
4497 }
4498}
4499
4500//===----------------------------------------------------------------------===//
4501// ARM Inline Assembly Support
4502//===----------------------------------------------------------------------===//
4503
4504/// getConstraintType - Given a constraint letter, return the type of
4505/// constraint it is for this target.
4506ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00004507ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
4508 if (Constraint.size() == 1) {
4509 switch (Constraint[0]) {
4510 default: break;
4511 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004512 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00004513 }
Evan Chenga8e29892007-01-19 07:51:42 +00004514 }
Chris Lattner4234f572007-03-25 02:14:49 +00004515 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00004516}
4517
Bob Wilson2dc4f542009-03-20 22:42:55 +00004518std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00004519ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004520 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004521 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004522 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00004523 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004524 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004525 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004526 return std::make_pair(0U, ARM::tGPRRegisterClass);
4527 else
4528 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004529 case 'r':
4530 return std::make_pair(0U, ARM::GPRRegisterClass);
4531 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004532 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004533 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00004534 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004535 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00004536 if (VT.getSizeInBits() == 128)
4537 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004538 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004539 }
4540 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00004541 if (StringRef("{cc}").equals_lower(Constraint))
4542 return std::make_pair(0U, ARM::CCRRegisterClass);
4543
Evan Chenga8e29892007-01-19 07:51:42 +00004544 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
4545}
4546
4547std::vector<unsigned> ARMTargetLowering::
4548getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004549 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004550 if (Constraint.size() != 1)
4551 return std::vector<unsigned>();
4552
4553 switch (Constraint[0]) { // GCC ARM Constraint Letters
4554 default: break;
4555 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004556 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4557 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4558 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004559 case 'r':
4560 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4561 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4562 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
4563 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004564 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004565 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004566 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
4567 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
4568 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
4569 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
4570 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
4571 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
4572 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
4573 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00004574 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004575 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
4576 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
4577 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
4578 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00004579 if (VT.getSizeInBits() == 128)
4580 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
4581 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004582 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004583 }
4584
4585 return std::vector<unsigned>();
4586}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004587
4588/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
4589/// vector. If it is invalid, don't add anything to Ops.
4590void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
4591 char Constraint,
4592 bool hasMemory,
4593 std::vector<SDValue>&Ops,
4594 SelectionDAG &DAG) const {
4595 SDValue Result(0, 0);
4596
4597 switch (Constraint) {
4598 default: break;
4599 case 'I': case 'J': case 'K': case 'L':
4600 case 'M': case 'N': case 'O':
4601 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
4602 if (!C)
4603 return;
4604
4605 int64_t CVal64 = C->getSExtValue();
4606 int CVal = (int) CVal64;
4607 // None of these constraints allow values larger than 32 bits. Check
4608 // that the value fits in an int.
4609 if (CVal != CVal64)
4610 return;
4611
4612 switch (Constraint) {
4613 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004614 if (Subtarget->isThumb1Only()) {
4615 // This must be a constant between 0 and 255, for ADD
4616 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004617 if (CVal >= 0 && CVal <= 255)
4618 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004619 } else if (Subtarget->isThumb2()) {
4620 // A constant that can be used as an immediate value in a
4621 // data-processing instruction.
4622 if (ARM_AM::getT2SOImmVal(CVal) != -1)
4623 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004624 } else {
4625 // A constant that can be used as an immediate value in a
4626 // data-processing instruction.
4627 if (ARM_AM::getSOImmVal(CVal) != -1)
4628 break;
4629 }
4630 return;
4631
4632 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004633 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004634 // This must be a constant between -255 and -1, for negated ADD
4635 // immediates. This can be used in GCC with an "n" modifier that
4636 // prints the negated value, for use with SUB instructions. It is
4637 // not useful otherwise but is implemented for compatibility.
4638 if (CVal >= -255 && CVal <= -1)
4639 break;
4640 } else {
4641 // This must be a constant between -4095 and 4095. It is not clear
4642 // what this constraint is intended for. Implemented for
4643 // compatibility with GCC.
4644 if (CVal >= -4095 && CVal <= 4095)
4645 break;
4646 }
4647 return;
4648
4649 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004650 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004651 // A 32-bit value where only one byte has a nonzero value. Exclude
4652 // zero to match GCC. This constraint is used by GCC internally for
4653 // constants that can be loaded with a move/shift combination.
4654 // It is not useful otherwise but is implemented for compatibility.
4655 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
4656 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004657 } else if (Subtarget->isThumb2()) {
4658 // A constant whose bitwise inverse can be used as an immediate
4659 // value in a data-processing instruction. This can be used in GCC
4660 // with a "B" modifier that prints the inverted value, for use with
4661 // BIC and MVN instructions. It is not useful otherwise but is
4662 // implemented for compatibility.
4663 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
4664 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004665 } else {
4666 // A constant whose bitwise inverse can be used as an immediate
4667 // value in a data-processing instruction. This can be used in GCC
4668 // with a "B" modifier that prints the inverted value, for use with
4669 // BIC and MVN instructions. It is not useful otherwise but is
4670 // implemented for compatibility.
4671 if (ARM_AM::getSOImmVal(~CVal) != -1)
4672 break;
4673 }
4674 return;
4675
4676 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004677 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004678 // This must be a constant between -7 and 7,
4679 // for 3-operand ADD/SUB immediate instructions.
4680 if (CVal >= -7 && CVal < 7)
4681 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004682 } else if (Subtarget->isThumb2()) {
4683 // A constant whose negation can be used as an immediate value in a
4684 // data-processing instruction. This can be used in GCC with an "n"
4685 // modifier that prints the negated value, for use with SUB
4686 // instructions. It is not useful otherwise but is implemented for
4687 // compatibility.
4688 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
4689 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004690 } else {
4691 // A constant whose negation can be used as an immediate value in a
4692 // data-processing instruction. This can be used in GCC with an "n"
4693 // modifier that prints the negated value, for use with SUB
4694 // instructions. It is not useful otherwise but is implemented for
4695 // compatibility.
4696 if (ARM_AM::getSOImmVal(-CVal) != -1)
4697 break;
4698 }
4699 return;
4700
4701 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004702 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004703 // This must be a multiple of 4 between 0 and 1020, for
4704 // ADD sp + immediate.
4705 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
4706 break;
4707 } else {
4708 // A power of two or a constant between 0 and 32. This is used in
4709 // GCC for the shift amount on shifted register operands, but it is
4710 // useful in general for any shift amounts.
4711 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
4712 break;
4713 }
4714 return;
4715
4716 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004717 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004718 // This must be a constant between 0 and 31, for shift amounts.
4719 if (CVal >= 0 && CVal <= 31)
4720 break;
4721 }
4722 return;
4723
4724 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004725 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004726 // This must be a multiple of 4 between -508 and 508, for
4727 // ADD/SUB sp = sp + immediate.
4728 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
4729 break;
4730 }
4731 return;
4732 }
4733 Result = DAG.getTargetConstant(CVal, Op.getValueType());
4734 break;
4735 }
4736
4737 if (Result.getNode()) {
4738 Ops.push_back(Result);
4739 return;
4740 }
4741 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
4742 Ops, DAG);
4743}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00004744
4745bool
4746ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4747 // The ARM target isn't yet aware of offsets.
4748 return false;
4749}
Evan Cheng39382422009-10-28 01:44:26 +00004750
4751int ARM::getVFPf32Imm(const APFloat &FPImm) {
4752 APInt Imm = FPImm.bitcastToAPInt();
4753 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
4754 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
4755 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
4756
4757 // We can handle 4 bits of mantissa.
4758 // mantissa = (16+UInt(e:f:g:h))/16.
4759 if (Mantissa & 0x7ffff)
4760 return -1;
4761 Mantissa >>= 19;
4762 if ((Mantissa & 0xf) != Mantissa)
4763 return -1;
4764
4765 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4766 if (Exp < -3 || Exp > 4)
4767 return -1;
4768 Exp = ((Exp+3) & 0x7) ^ 4;
4769
4770 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4771}
4772
4773int ARM::getVFPf64Imm(const APFloat &FPImm) {
4774 APInt Imm = FPImm.bitcastToAPInt();
4775 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
4776 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
4777 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
4778
4779 // We can handle 4 bits of mantissa.
4780 // mantissa = (16+UInt(e:f:g:h))/16.
4781 if (Mantissa & 0xffffffffffffLL)
4782 return -1;
4783 Mantissa >>= 48;
4784 if ((Mantissa & 0xf) != Mantissa)
4785 return -1;
4786
4787 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4788 if (Exp < -3 || Exp > 4)
4789 return -1;
4790 Exp = ((Exp+3) & 0x7) ^ 4;
4791
4792 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4793}
4794
4795/// isFPImmLegal - Returns true if the target can instruction select the
4796/// specified FP immediate natively. If false, the legalizer will
4797/// materialize the FP immediate as a load from a constant pool.
4798bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
4799 if (!Subtarget->hasVFP3())
4800 return false;
4801 if (VT == MVT::f32)
4802 return ARM::getVFPf32Imm(Imm) != -1;
4803 if (VT == MVT::f64)
4804 return ARM::getVFPf64Imm(Imm) != -1;
4805 return false;
4806}