blob: f9a34e0e570fc85dc9cb5919bf0fed58d0d214c5 [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
Dale Johannesen51e28e62010-06-03 21:09:53 +000015#define DEBUG_TYPE "arm-isel"
Evan Chenga8e29892007-01-19 07:51:42 +000016#include "ARM.h"
17#include "ARMAddressingModes.h"
18#include "ARMConstantPoolValue.h"
19#include "ARMISelLowering.h"
20#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000021#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000022#include "ARMRegisterInfo.h"
23#include "ARMSubtarget.h"
24#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000025#include "ARMTargetObjectFile.h"
Evan Chenga8e29892007-01-19 07:51:42 +000026#include "llvm/CallingConv.h"
27#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000028#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000029#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000030#include "llvm/Instruction.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000031#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000032#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000033#include "llvm/CodeGen/CallingConvLower.h"
Evan Chenga8e29892007-01-19 07:51:42 +000034#include "llvm/CodeGen/MachineBasicBlock.h"
35#include "llvm/CodeGen/MachineFrameInfo.h"
36#include "llvm/CodeGen/MachineFunction.h"
37#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000038#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000039#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000040#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000041#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000042#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000043#include "llvm/ADT/VectorExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000044#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000045#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000046#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000047#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000048#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000049#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000050using namespace llvm;
51
Dale Johannesen51e28e62010-06-03 21:09:53 +000052STATISTIC(NumTailCalls, "Number of tail calls");
53
54// This option should go away when tail calls fully work.
55static cl::opt<bool>
56EnableARMTailCalls("arm-tail-calls", cl::Hidden,
57 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
58 cl::init(false));
59
Jim Grosbache7b52522010-04-14 22:28:31 +000060static cl::opt<bool>
61EnableARMLongCalls("arm-long-calls", cl::Hidden,
62 cl::desc("Generate calls via indirect call instructions."),
63 cl::init(false));
64
Owen Andersone50ed302009-08-10 22:56:29 +000065static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000066 CCValAssign::LocInfo &LocInfo,
67 ISD::ArgFlagsTy &ArgFlags,
68 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000069static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000070 CCValAssign::LocInfo &LocInfo,
71 ISD::ArgFlagsTy &ArgFlags,
72 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000073static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000074 CCValAssign::LocInfo &LocInfo,
75 ISD::ArgFlagsTy &ArgFlags,
76 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000077static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000078 CCValAssign::LocInfo &LocInfo,
79 ISD::ArgFlagsTy &ArgFlags,
80 CCState &State);
81
Owen Andersone50ed302009-08-10 22:56:29 +000082void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
83 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000084 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000085 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000086 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
87 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000088
Owen Anderson70671842009-08-10 20:18:46 +000089 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000090 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000091 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000092 }
93
Owen Andersone50ed302009-08-10 22:56:29 +000094 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +000095 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +000096 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +000097 if (ElemTy == MVT::i8 || ElemTy == MVT::i16)
Owen Anderson70671842009-08-10 20:18:46 +000098 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +000099 if (ElemTy != MVT::i32) {
100 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
101 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
102 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
103 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
104 }
Owen Anderson70671842009-08-10 20:18:46 +0000105 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
106 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Evan Chengde8aa4e2010-05-05 18:28:36 +0000107 if (llvm::ModelWithRegSequence())
108 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
109 else
110 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Custom);
Anton Korobeynikov8e6c2b92009-08-21 12:40:35 +0000111 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
Bob Wilsond0910c42010-04-06 22:02:24 +0000112 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
113 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000114 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000115 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
116 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
117 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000118 }
119
120 // Promote all bit-wise operations.
121 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000122 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000123 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
124 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000125 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000126 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000127 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000128 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000129 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000130 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000131 }
Bob Wilson16330762009-09-16 00:17:28 +0000132
133 // Neon does not support vector divide/remainder operations.
134 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
135 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
136 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
137 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
138 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
139 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000140}
141
Owen Andersone50ed302009-08-10 22:56:29 +0000142void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000143 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000144 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000145}
146
Owen Andersone50ed302009-08-10 22:56:29 +0000147void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000148 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000149 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000150}
151
Chris Lattnerf0144122009-07-28 03:13:23 +0000152static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
153 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000154 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000155
Chris Lattner80ec2792009-08-02 00:34:36 +0000156 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000157}
158
Evan Chenga8e29892007-01-19 07:51:42 +0000159ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000160 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000161 Subtarget = &TM.getSubtarget<ARMSubtarget>();
162
Evan Chengb1df8f22007-04-27 08:15:43 +0000163 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000164 // Uses VFP for Thumb libfuncs if available.
165 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
166 // Single-precision floating-point arithmetic.
167 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
168 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
169 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
170 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000171
Evan Chengb1df8f22007-04-27 08:15:43 +0000172 // Double-precision floating-point arithmetic.
173 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
174 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
175 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
176 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000177
Evan Chengb1df8f22007-04-27 08:15:43 +0000178 // Single-precision comparisons.
179 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
180 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
181 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
182 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
183 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
184 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
185 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
186 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000187
Evan Chengb1df8f22007-04-27 08:15:43 +0000188 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
189 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
190 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
191 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
192 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
193 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
194 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
195 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000196
Evan Chengb1df8f22007-04-27 08:15:43 +0000197 // Double-precision comparisons.
198 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
199 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
200 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
201 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
202 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
203 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
204 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
205 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000206
Evan Chengb1df8f22007-04-27 08:15:43 +0000207 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
214 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000215
Evan Chengb1df8f22007-04-27 08:15:43 +0000216 // Floating-point to integer conversions.
217 // i64 conversions are done via library routines even when generating VFP
218 // instructions, so use the same ones.
219 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
220 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
221 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
222 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000223
Evan Chengb1df8f22007-04-27 08:15:43 +0000224 // Conversions between floating types.
225 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
226 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
227
228 // Integer to floating-point conversions.
229 // i64 conversions are done via library routines even when generating VFP
230 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000231 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
232 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000233 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
234 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
235 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
236 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
237 }
Evan Chenga8e29892007-01-19 07:51:42 +0000238 }
239
Bob Wilson2f954612009-05-22 17:38:41 +0000240 // These libcalls are not available in 32-bit.
241 setLibcallName(RTLIB::SHL_I128, 0);
242 setLibcallName(RTLIB::SRL_I128, 0);
243 setLibcallName(RTLIB::SRA_I128, 0);
244
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000245 // Libcalls should use the AAPCS base standard ABI, even if hard float
246 // is in effect, as per the ARM RTABI specification, section 4.1.2.
247 if (Subtarget->isAAPCS_ABI()) {
248 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
249 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
250 CallingConv::ARM_AAPCS);
251 }
252 }
253
David Goodwinf1daf7d2009-07-08 23:10:31 +0000254 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000255 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000256 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000258 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000259 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
260 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000261
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000263 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000264
265 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000266 addDRTypeForNEON(MVT::v2f32);
267 addDRTypeForNEON(MVT::v8i8);
268 addDRTypeForNEON(MVT::v4i16);
269 addDRTypeForNEON(MVT::v2i32);
270 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000271
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 addQRTypeForNEON(MVT::v4f32);
273 addQRTypeForNEON(MVT::v2f64);
274 addQRTypeForNEON(MVT::v16i8);
275 addQRTypeForNEON(MVT::v8i16);
276 addQRTypeForNEON(MVT::v4i32);
277 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000278
Bob Wilson74dc72e2009-09-15 23:55:57 +0000279 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
280 // neither Neon nor VFP support any arithmetic operations on it.
281 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
282 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
283 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
284 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
285 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
286 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
287 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
288 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
289 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
290 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
291 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
292 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
293 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
294 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
295 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
296 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
297 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
298 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
299 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
300 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
301 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
302 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
303 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
304 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
305
Bob Wilson642b3292009-09-16 00:32:15 +0000306 // Neon does not support some operations on v1i64 and v2i64 types.
307 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
308 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
309 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
310 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
311
Bob Wilson5bafff32009-06-22 23:27:02 +0000312 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
313 setTargetDAGCombine(ISD::SHL);
314 setTargetDAGCombine(ISD::SRL);
315 setTargetDAGCombine(ISD::SRA);
316 setTargetDAGCombine(ISD::SIGN_EXTEND);
317 setTargetDAGCombine(ISD::ZERO_EXTEND);
318 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000319 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson5bafff32009-06-22 23:27:02 +0000320 }
321
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000322 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000323
324 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000325 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000326
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000327 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000328 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000329
Evan Chenga8e29892007-01-19 07:51:42 +0000330 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000331 if (!Subtarget->isThumb1Only()) {
332 for (unsigned im = (unsigned)ISD::PRE_INC;
333 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000334 setIndexedLoadAction(im, MVT::i1, Legal);
335 setIndexedLoadAction(im, MVT::i8, Legal);
336 setIndexedLoadAction(im, MVT::i16, Legal);
337 setIndexedLoadAction(im, MVT::i32, Legal);
338 setIndexedStoreAction(im, MVT::i1, Legal);
339 setIndexedStoreAction(im, MVT::i8, Legal);
340 setIndexedStoreAction(im, MVT::i16, Legal);
341 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000342 }
Evan Chenga8e29892007-01-19 07:51:42 +0000343 }
344
345 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000346 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000347 setOperationAction(ISD::MUL, MVT::i64, Expand);
348 setOperationAction(ISD::MULHU, MVT::i32, Expand);
349 setOperationAction(ISD::MULHS, MVT::i32, Expand);
350 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
351 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000352 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000353 setOperationAction(ISD::MUL, MVT::i64, Expand);
354 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000355 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000356 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000357 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000358 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000359 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000360 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000361 setOperationAction(ISD::SRL, MVT::i64, Custom);
362 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000363
364 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000365 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000366 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000368 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000369 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000370
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000371 // Only ARMv6 has BSWAP.
372 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000373 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000374
Evan Chenga8e29892007-01-19 07:51:42 +0000375 // These are expanded into libcalls.
Jim Grosbach29402132010-05-05 23:44:43 +0000376 if (!Subtarget->hasDivide()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000377 // v7M has a hardware divider
378 setOperationAction(ISD::SDIV, MVT::i32, Expand);
379 setOperationAction(ISD::UDIV, MVT::i32, Expand);
380 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000381 setOperationAction(ISD::SREM, MVT::i32, Expand);
382 setOperationAction(ISD::UREM, MVT::i32, Expand);
383 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
384 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000385
Owen Anderson825b72b2009-08-11 20:47:22 +0000386 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
387 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
388 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
389 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000390 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000391
Evan Chengfb3611d2010-05-11 07:26:32 +0000392 setOperationAction(ISD::TRAP, MVT::Other, Legal);
393
Evan Chenga8e29892007-01-19 07:51:42 +0000394 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000395 setOperationAction(ISD::VASTART, MVT::Other, Custom);
396 setOperationAction(ISD::VAARG, MVT::Other, Expand);
397 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
398 setOperationAction(ISD::VAEND, MVT::Other, Expand);
399 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
400 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000401 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
402 // FIXME: Shouldn't need this, since no register is used, but the legalizer
403 // doesn't yet know how to not do that for SjLj.
404 setExceptionSelectorRegister(ARM::R0);
Evan Cheng3a1588a2010-04-15 22:20:34 +0000405 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Jim Grosbach3728e962009-12-10 00:11:09 +0000406 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000407
Jim Grosbach4b77f6a2010-05-07 18:34:55 +0000408 // If the subtarget does not have extract instructions, sign_extend_inreg
409 // needs to be expanded. Extract is available in ARM mode on v6 and up,
410 // and on most Thumb2 implementations.
411 if ((!Subtarget->isThumb() && !Subtarget->hasV6Ops())
412 || (Subtarget->isThumb2() && !Subtarget->hasT2ExtractPack())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000413 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
414 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000415 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000417
David Goodwinf1daf7d2009-07-08 23:10:31 +0000418 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000419 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
420 // iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000421 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000422
423 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000424 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000425
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 setOperationAction(ISD::SETCC, MVT::i32, Expand);
427 setOperationAction(ISD::SETCC, MVT::f32, Expand);
428 setOperationAction(ISD::SETCC, MVT::f64, Expand);
429 setOperationAction(ISD::SELECT, MVT::i32, Expand);
430 setOperationAction(ISD::SELECT, MVT::f32, Expand);
431 setOperationAction(ISD::SELECT, MVT::f64, Expand);
432 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
433 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
434 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000435
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
437 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
438 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
439 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
440 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000441
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000442 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setOperationAction(ISD::FSIN, MVT::f64, Expand);
444 setOperationAction(ISD::FSIN, MVT::f32, Expand);
445 setOperationAction(ISD::FCOS, MVT::f32, Expand);
446 setOperationAction(ISD::FCOS, MVT::f64, Expand);
447 setOperationAction(ISD::FREM, MVT::f64, Expand);
448 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000449 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000450 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
451 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000452 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000453 setOperationAction(ISD::FPOW, MVT::f64, Expand);
454 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000455
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000456 // Various VFP goodness
457 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000458 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
459 if (Subtarget->hasVFP2()) {
460 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
461 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
462 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
463 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
464 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000465 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000466 if (!Subtarget->hasFP16()) {
467 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
468 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000469 }
Evan Cheng110cf482008-04-01 01:50:16 +0000470 }
Evan Chenga8e29892007-01-19 07:51:42 +0000471
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000472 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000473 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000474 setTargetDAGCombine(ISD::ADD);
475 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000476 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000477
Evan Chenga8e29892007-01-19 07:51:42 +0000478 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000479
Evan Chengf7d87ee2010-05-21 00:43:17 +0000480 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
481 setSchedulingPreference(Sched::RegPressure);
482 else
483 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000484
Evan Chengbc9b7542009-08-15 07:59:10 +0000485 // FIXME: If-converter should use instruction latency to determine
486 // profitability rather than relying on fixed limits.
487 if (Subtarget->getCPUString() == "generic") {
488 // Generic (and overly aggressive) if-conversion limits.
489 setIfCvtBlockSizeLimit(10);
490 setIfCvtDupBlockSizeLimit(2);
Jim Grosbach35075a72010-03-24 16:15:14 +0000491 } else if (Subtarget->hasV7Ops()) {
Jim Grosbachfceabef2010-03-24 00:03:13 +0000492 setIfCvtBlockSizeLimit(3);
493 setIfCvtDupBlockSizeLimit(1);
Evan Chengbc9b7542009-08-15 07:59:10 +0000494 } else if (Subtarget->hasV6Ops()) {
495 setIfCvtBlockSizeLimit(2);
496 setIfCvtDupBlockSizeLimit(1);
497 } else {
498 setIfCvtBlockSizeLimit(3);
499 setIfCvtDupBlockSizeLimit(2);
Evan Cheng8557c2b2009-06-19 01:51:50 +0000500 }
501
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000502 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Bob Wilsone6abdff2009-05-18 20:55:32 +0000503 // Do not enable CodePlacementOpt for now: it currently runs after the
504 // ARMConstantIslandPass and messes up branch relaxation and placement
505 // of constant islands.
506 // benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000507}
508
Evan Chenga8e29892007-01-19 07:51:42 +0000509const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
510 switch (Opcode) {
511 default: return 0;
512 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000513 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
514 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000515 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000516 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
517 case ARMISD::tCALL: return "ARMISD::tCALL";
518 case ARMISD::BRCOND: return "ARMISD::BRCOND";
519 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000520 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000521 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
522 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
523 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000524 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000525 case ARMISD::CMPFP: return "ARMISD::CMPFP";
526 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
527 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
528 case ARMISD::CMOV: return "ARMISD::CMOV";
529 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000530
Jim Grosbach3482c802010-01-18 19:58:49 +0000531 case ARMISD::RBIT: return "ARMISD::RBIT";
532
Bob Wilson76a312b2010-03-19 22:51:32 +0000533 case ARMISD::FTOSI: return "ARMISD::FTOSI";
534 case ARMISD::FTOUI: return "ARMISD::FTOUI";
535 case ARMISD::SITOF: return "ARMISD::SITOF";
536 case ARMISD::UITOF: return "ARMISD::UITOF";
537
Evan Chenga8e29892007-01-19 07:51:42 +0000538 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
539 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
540 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000541
Jim Grosbache5165492009-11-09 00:11:35 +0000542 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
543 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000544
Evan Chengc5942082009-10-28 06:55:03 +0000545 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
546 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
547
Dale Johannesen51e28e62010-06-03 21:09:53 +0000548 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
549
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000550 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000551
Evan Cheng86198642009-08-07 00:34:42 +0000552 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
553
Jim Grosbach3728e962009-12-10 00:11:09 +0000554 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
555 case ARMISD::SYNCBARRIER: return "ARMISD::SYNCBARRIER";
556
Bob Wilson5bafff32009-06-22 23:27:02 +0000557 case ARMISD::VCEQ: return "ARMISD::VCEQ";
558 case ARMISD::VCGE: return "ARMISD::VCGE";
559 case ARMISD::VCGEU: return "ARMISD::VCGEU";
560 case ARMISD::VCGT: return "ARMISD::VCGT";
561 case ARMISD::VCGTU: return "ARMISD::VCGTU";
562 case ARMISD::VTST: return "ARMISD::VTST";
563
564 case ARMISD::VSHL: return "ARMISD::VSHL";
565 case ARMISD::VSHRs: return "ARMISD::VSHRs";
566 case ARMISD::VSHRu: return "ARMISD::VSHRu";
567 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
568 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
569 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
570 case ARMISD::VSHRN: return "ARMISD::VSHRN";
571 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
572 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
573 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
574 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
575 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
576 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
577 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
578 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
579 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
580 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
581 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
582 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
583 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
584 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000585 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000586 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000587 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000588 case ARMISD::VREV64: return "ARMISD::VREV64";
589 case ARMISD::VREV32: return "ARMISD::VREV32";
590 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000591 case ARMISD::VZIP: return "ARMISD::VZIP";
592 case ARMISD::VUZP: return "ARMISD::VUZP";
593 case ARMISD::VTRN: return "ARMISD::VTRN";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000594 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000595 case ARMISD::FMAX: return "ARMISD::FMAX";
596 case ARMISD::FMIN: return "ARMISD::FMIN";
Evan Chenga8e29892007-01-19 07:51:42 +0000597 }
598}
599
Evan Cheng06b666c2010-05-15 02:18:07 +0000600/// getRegClassFor - Return the register class that should be used for the
601/// specified value type.
602TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
603 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
604 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
605 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000606 if (Subtarget->hasNEON()) {
607 if (VT == MVT::v4i64)
608 return ARM::QQPRRegisterClass;
609 else if (VT == MVT::v8i64)
610 return ARM::QQQQPRRegisterClass;
611 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000612 return TargetLowering::getRegClassFor(VT);
613}
614
Bill Wendlingb4202b82009-07-01 18:50:55 +0000615/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000616unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Evan Cheng048e36f2009-10-02 06:57:25 +0000617 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 0 : 1;
Bill Wendling20c568f2009-06-30 22:38:32 +0000618}
619
Evan Cheng1cc39842010-05-20 23:26:43 +0000620Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000621 unsigned NumVals = N->getNumValues();
622 if (!NumVals)
623 return Sched::RegPressure;
624
625 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000626 EVT VT = N->getValueType(i);
627 if (VT.isFloatingPoint() || VT.isVector())
628 return Sched::Latency;
629 }
Evan Chengc10f5432010-05-28 23:25:23 +0000630
631 if (!N->isMachineOpcode())
632 return Sched::RegPressure;
633
634 // Load are scheduled for latency even if there instruction itinerary
635 // is not available.
636 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
637 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
638 if (TID.mayLoad())
639 return Sched::Latency;
640
641 const InstrItineraryData &Itins = getTargetMachine().getInstrItineraryData();
642 if (!Itins.isEmpty() && Itins.getStageLatency(TID.getSchedClass()) > 2)
643 return Sched::Latency;
Evan Cheng1cc39842010-05-20 23:26:43 +0000644 return Sched::RegPressure;
645}
646
Evan Chenga8e29892007-01-19 07:51:42 +0000647//===----------------------------------------------------------------------===//
648// Lowering Code
649//===----------------------------------------------------------------------===//
650
Evan Chenga8e29892007-01-19 07:51:42 +0000651/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
652static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
653 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000654 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000655 case ISD::SETNE: return ARMCC::NE;
656 case ISD::SETEQ: return ARMCC::EQ;
657 case ISD::SETGT: return ARMCC::GT;
658 case ISD::SETGE: return ARMCC::GE;
659 case ISD::SETLT: return ARMCC::LT;
660 case ISD::SETLE: return ARMCC::LE;
661 case ISD::SETUGT: return ARMCC::HI;
662 case ISD::SETUGE: return ARMCC::HS;
663 case ISD::SETULT: return ARMCC::LO;
664 case ISD::SETULE: return ARMCC::LS;
665 }
666}
667
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000668/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
669static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000670 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000671 CondCode2 = ARMCC::AL;
672 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000673 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000674 case ISD::SETEQ:
675 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
676 case ISD::SETGT:
677 case ISD::SETOGT: CondCode = ARMCC::GT; break;
678 case ISD::SETGE:
679 case ISD::SETOGE: CondCode = ARMCC::GE; break;
680 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000681 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000682 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
683 case ISD::SETO: CondCode = ARMCC::VC; break;
684 case ISD::SETUO: CondCode = ARMCC::VS; break;
685 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
686 case ISD::SETUGT: CondCode = ARMCC::HI; break;
687 case ISD::SETUGE: CondCode = ARMCC::PL; break;
688 case ISD::SETLT:
689 case ISD::SETULT: CondCode = ARMCC::LT; break;
690 case ISD::SETLE:
691 case ISD::SETULE: CondCode = ARMCC::LE; break;
692 case ISD::SETNE:
693 case ISD::SETUNE: CondCode = ARMCC::NE; break;
694 }
Evan Chenga8e29892007-01-19 07:51:42 +0000695}
696
Bob Wilson1f595bb2009-04-17 19:07:39 +0000697//===----------------------------------------------------------------------===//
698// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000699//===----------------------------------------------------------------------===//
700
701#include "ARMGenCallingConv.inc"
702
703// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000704static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000705 CCValAssign::LocInfo &LocInfo,
706 CCState &State, bool CanFail) {
707 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
708
709 // Try to get the first register.
710 if (unsigned Reg = State.AllocateReg(RegList, 4))
711 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
712 else {
713 // For the 2nd half of a v2f64, do not fail.
714 if (CanFail)
715 return false;
716
717 // Put the whole thing on the stack.
718 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
719 State.AllocateStack(8, 4),
720 LocVT, LocInfo));
721 return true;
722 }
723
724 // Try to get the second register.
725 if (unsigned Reg = State.AllocateReg(RegList, 4))
726 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
727 else
728 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
729 State.AllocateStack(4, 4),
730 LocVT, LocInfo));
731 return true;
732}
733
Owen Andersone50ed302009-08-10 22:56:29 +0000734static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000735 CCValAssign::LocInfo &LocInfo,
736 ISD::ArgFlagsTy &ArgFlags,
737 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000738 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
739 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000740 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000741 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
742 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000743 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000744}
745
746// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000747static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000748 CCValAssign::LocInfo &LocInfo,
749 CCState &State, bool CanFail) {
750 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
751 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
752
753 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
754 if (Reg == 0) {
755 // For the 2nd half of a v2f64, do not just fail.
756 if (CanFail)
757 return false;
758
759 // Put the whole thing on the stack.
760 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
761 State.AllocateStack(8, 8),
762 LocVT, LocInfo));
763 return true;
764 }
765
766 unsigned i;
767 for (i = 0; i < 2; ++i)
768 if (HiRegList[i] == Reg)
769 break;
770
771 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
772 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
773 LocVT, LocInfo));
774 return true;
775}
776
Owen Andersone50ed302009-08-10 22:56:29 +0000777static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000778 CCValAssign::LocInfo &LocInfo,
779 ISD::ArgFlagsTy &ArgFlags,
780 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000781 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
782 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000783 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000784 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
785 return false;
786 return true; // we handled it
787}
788
Owen Andersone50ed302009-08-10 22:56:29 +0000789static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000790 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000791 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
792 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
793
Bob Wilsone65586b2009-04-17 20:40:45 +0000794 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
795 if (Reg == 0)
796 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000797
Bob Wilsone65586b2009-04-17 20:40:45 +0000798 unsigned i;
799 for (i = 0; i < 2; ++i)
800 if (HiRegList[i] == Reg)
801 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000802
Bob Wilson5bafff32009-06-22 23:27:02 +0000803 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000804 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000805 LocVT, LocInfo));
806 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000807}
808
Owen Andersone50ed302009-08-10 22:56:29 +0000809static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000810 CCValAssign::LocInfo &LocInfo,
811 ISD::ArgFlagsTy &ArgFlags,
812 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000813 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
814 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000815 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000816 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000817 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000818}
819
Owen Andersone50ed302009-08-10 22:56:29 +0000820static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000821 CCValAssign::LocInfo &LocInfo,
822 ISD::ArgFlagsTy &ArgFlags,
823 CCState &State) {
824 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
825 State);
826}
827
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000828/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
829/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000830CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000831 bool Return,
832 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000833 switch (CC) {
834 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000835 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000836 case CallingConv::C:
837 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000838 // Use target triple & subtarget features to do actual dispatch.
839 if (Subtarget->isAAPCS_ABI()) {
840 if (Subtarget->hasVFP2() &&
841 FloatABIType == FloatABI::Hard && !isVarArg)
842 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
843 else
844 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
845 } else
846 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000847 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000848 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000849 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000850 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000851 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000852 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000853 }
854}
855
Dan Gohman98ca4f22009-08-05 01:29:28 +0000856/// LowerCallResult - Lower the result values of a call into the
857/// appropriate copies out of appropriate physical registers.
858SDValue
859ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000860 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000861 const SmallVectorImpl<ISD::InputArg> &Ins,
862 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000863 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000864
Bob Wilson1f595bb2009-04-17 19:07:39 +0000865 // Assign locations to each value returned by this call.
866 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000867 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000868 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000869 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000870 CCAssignFnForNode(CallConv, /* Return*/ true,
871 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000872
873 // Copy all of the result registers out of their specified physreg.
874 for (unsigned i = 0; i != RVLocs.size(); ++i) {
875 CCValAssign VA = RVLocs[i];
876
Bob Wilson80915242009-04-25 00:33:20 +0000877 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000878 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000879 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000881 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000882 Chain = Lo.getValue(1);
883 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000884 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000885 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000886 InFlag);
887 Chain = Hi.getValue(1);
888 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000889 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000890
Owen Anderson825b72b2009-08-11 20:47:22 +0000891 if (VA.getLocVT() == MVT::v2f64) {
892 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
893 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
894 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000895
896 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000898 Chain = Lo.getValue(1);
899 InFlag = Lo.getValue(2);
900 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000901 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000902 Chain = Hi.getValue(1);
903 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000904 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +0000905 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
906 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000907 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000908 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000909 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
910 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000911 Chain = Val.getValue(1);
912 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000913 }
Bob Wilson80915242009-04-25 00:33:20 +0000914
915 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000916 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000917 case CCValAssign::Full: break;
918 case CCValAssign::BCvt:
919 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
920 break;
921 }
922
Dan Gohman98ca4f22009-08-05 01:29:28 +0000923 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000924 }
925
Dan Gohman98ca4f22009-08-05 01:29:28 +0000926 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000927}
928
929/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
930/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000931/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000932/// a byval function parameter.
933/// Sometimes what we are copying is the end of a larger object, the part that
934/// does not fit in registers.
935static SDValue
936CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
937 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
938 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000939 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000940 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +0000941 /*isVolatile=*/false, /*AlwaysInline=*/false,
942 NULL, 0, NULL, 0);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000943}
944
Bob Wilsondee46d72009-04-17 20:35:10 +0000945/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000946SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000947ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
948 SDValue StackPtr, SDValue Arg,
949 DebugLoc dl, SelectionDAG &DAG,
950 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +0000951 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000952 unsigned LocMemOffset = VA.getLocMemOffset();
953 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
954 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
955 if (Flags.isByVal()) {
956 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
957 }
958 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene1b58cab2010-02-15 16:55:24 +0000959 PseudoSourceValue::getStack(), LocMemOffset,
960 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +0000961}
962
Dan Gohman98ca4f22009-08-05 01:29:28 +0000963void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000964 SDValue Chain, SDValue &Arg,
965 RegsToPassVector &RegsToPass,
966 CCValAssign &VA, CCValAssign &NextVA,
967 SDValue &StackPtr,
968 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +0000969 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +0000970
Jim Grosbache5165492009-11-09 00:11:35 +0000971 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000972 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000973 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
974
975 if (NextVA.isRegLoc())
976 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
977 else {
978 assert(NextVA.isMemLoc());
979 if (StackPtr.getNode() == 0)
980 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
981
Dan Gohman98ca4f22009-08-05 01:29:28 +0000982 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
983 dl, DAG, NextVA,
984 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000985 }
986}
987
Dan Gohman98ca4f22009-08-05 01:29:28 +0000988/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000989/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
990/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000991SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000992ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000993 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000994 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000995 const SmallVectorImpl<ISD::OutputArg> &Outs,
996 const SmallVectorImpl<ISD::InputArg> &Ins,
997 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000998 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +0000999 MachineFunction &MF = DAG.getMachineFunction();
1000 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1001 bool IsSibCall = false;
Dale Johannesen8fa8e7f2010-06-04 18:04:24 +00001002 // Temporarily disable tail calls so things don't break.
1003 if (!EnableARMTailCalls)
1004 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001005 if (isTailCall) {
1006 // Check if it's really possible to do a tail call.
1007 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1008 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
1009 Outs, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001010 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1011 // detected sibcalls.
1012 if (isTailCall) {
1013 ++NumTailCalls;
1014 IsSibCall = true;
1015 }
1016 }
Evan Chenga8e29892007-01-19 07:51:42 +00001017
Bob Wilson1f595bb2009-04-17 19:07:39 +00001018 // Analyze operands of the call, assigning locations to each operand.
1019 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001020 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1021 *DAG.getContext());
1022 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001023 CCAssignFnForNode(CallConv, /* Return*/ false,
1024 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001025
Bob Wilson1f595bb2009-04-17 19:07:39 +00001026 // Get a count of how many bytes are to be pushed on the stack.
1027 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001028
Dale Johannesen51e28e62010-06-03 21:09:53 +00001029 // For tail calls, memory operands are available in our caller's stack.
1030 if (IsSibCall)
1031 NumBytes = 0;
1032
Evan Chenga8e29892007-01-19 07:51:42 +00001033 // Adjust the stack pointer for the new arguments...
1034 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001035 if (!IsSibCall)
1036 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001037
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001038 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001039
Bob Wilson5bafff32009-06-22 23:27:02 +00001040 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001041 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001042
Bob Wilson1f595bb2009-04-17 19:07:39 +00001043 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001044 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001045 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1046 i != e;
1047 ++i, ++realArgIdx) {
1048 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001049 SDValue Arg = Outs[realArgIdx].Val;
1050 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +00001051
Bob Wilson1f595bb2009-04-17 19:07:39 +00001052 // Promote the value if needed.
1053 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001054 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001055 case CCValAssign::Full: break;
1056 case CCValAssign::SExt:
1057 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1058 break;
1059 case CCValAssign::ZExt:
1060 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1061 break;
1062 case CCValAssign::AExt:
1063 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1064 break;
1065 case CCValAssign::BCvt:
1066 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1067 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001068 }
1069
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001070 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001071 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001072 if (VA.getLocVT() == MVT::v2f64) {
1073 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1074 DAG.getConstant(0, MVT::i32));
1075 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1076 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001077
Dan Gohman98ca4f22009-08-05 01:29:28 +00001078 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001079 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1080
1081 VA = ArgLocs[++i]; // skip ahead to next loc
1082 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001083 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001084 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1085 } else {
1086 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001087
Dan Gohman98ca4f22009-08-05 01:29:28 +00001088 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1089 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001090 }
1091 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001092 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001093 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001094 }
1095 } else if (VA.isRegLoc()) {
1096 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1097 } else {
1098 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001099
Dan Gohman98ca4f22009-08-05 01:29:28 +00001100 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1101 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001102 }
Evan Chenga8e29892007-01-19 07:51:42 +00001103 }
1104
1105 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001106 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001107 &MemOpChains[0], MemOpChains.size());
1108
1109 // Build a sequence of copy-to-reg nodes chained together with token chain
1110 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001111 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +00001112 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001113 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001114 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +00001115 InFlag = Chain.getValue(1);
1116 }
1117
Dale Johannesen51e28e62010-06-03 21:09:53 +00001118 // For tail calls lower the arguments to the 'real' stack slot.
1119 if (isTailCall) {
1120 // Force all the incoming stack arguments to be loaded from the stack
1121 // before any new outgoing arguments are stored to the stack, because the
1122 // outgoing stack slots may alias the incoming argument stack slots, and
1123 // the alias isn't otherwise explicit. This is slightly more conservative
1124 // than necessary, because it means that each store effectively depends
1125 // on every argument instead of just those arguments it would clobber.
1126
1127 // Do not flag preceeding copytoreg stuff together with the following stuff.
1128 InFlag = SDValue();
1129 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1130 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1131 RegsToPass[i].second, InFlag);
1132 InFlag = Chain.getValue(1);
1133 }
1134 InFlag =SDValue();
1135 }
1136
Bill Wendling056292f2008-09-16 21:48:12 +00001137 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1138 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1139 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001140 bool isDirect = false;
1141 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001142 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001143 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001144
1145 if (EnableARMLongCalls) {
1146 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1147 && "long-calls with non-static relocation model!");
1148 // Handle a global address or an external symbol. If it's not one of
1149 // those, the target's already in a register, so we don't need to do
1150 // anything extra.
1151 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001152 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001153 // Create a constant pool entry for the callee address
1154 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1155 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1156 ARMPCLabelIndex,
1157 ARMCP::CPValue, 0);
1158 // Get the address of the callee into a register
1159 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1160 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1161 Callee = DAG.getLoad(getPointerTy(), dl,
1162 DAG.getEntryNode(), CPAddr,
1163 PseudoSourceValue::getConstantPool(), 0,
1164 false, false, 0);
1165 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1166 const char *Sym = S->getSymbol();
1167
1168 // Create a constant pool entry for the callee address
1169 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1170 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1171 Sym, ARMPCLabelIndex, 0);
1172 // Get the address of the callee into a register
1173 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1174 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1175 Callee = DAG.getLoad(getPointerTy(), dl,
1176 DAG.getEntryNode(), CPAddr,
1177 PseudoSourceValue::getConstantPool(), 0,
1178 false, false, 0);
1179 }
1180 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001181 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001182 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001183 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001184 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001185 getTargetMachine().getRelocationModel() != Reloc::Static;
1186 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001187 // ARM call to a local ARM function is predicable.
1188 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +00001189 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001190 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001191 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001192 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001193 ARMPCLabelIndex,
1194 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001195 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001196 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001197 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001198 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001199 PseudoSourceValue::getConstantPool(), 0,
1200 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001201 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001202 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001203 getPointerTy(), Callee, PICLabel);
Jim Grosbache7b52522010-04-14 22:28:31 +00001204 } else
Evan Chengc60e76d2007-01-30 20:37:08 +00001205 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001206 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001207 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001208 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001209 getTargetMachine().getRelocationModel() != Reloc::Static;
1210 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001211 // tBX takes a register source operand.
1212 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001213 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001214 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001215 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001216 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001217 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001218 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001219 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001220 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001221 PseudoSourceValue::getConstantPool(), 0,
1222 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001223 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001224 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001225 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001226 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001227 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001228 }
1229
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001230 // FIXME: handle tail calls differently.
1231 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001232 if (Subtarget->isThumb()) {
1233 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001234 CallOpc = ARMISD::CALL_NOLINK;
1235 else
1236 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1237 } else {
1238 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001239 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1240 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001241 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001242 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001243 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001244 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001245 InFlag = Chain.getValue(1);
1246 }
1247
Dan Gohman475871a2008-07-27 21:46:04 +00001248 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001249 Ops.push_back(Chain);
1250 Ops.push_back(Callee);
1251
1252 // Add argument registers to the end of the list so that they are known live
1253 // into the call.
1254 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1255 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1256 RegsToPass[i].second.getValueType()));
1257
Gabor Greifba36cb52008-08-28 21:40:38 +00001258 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001259 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001260
1261 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001262 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001263 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001264
Duncan Sands4bdcb612008-07-02 17:40:58 +00001265 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001266 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001267 InFlag = Chain.getValue(1);
1268
Chris Lattnere563bbc2008-10-11 22:08:30 +00001269 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1270 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001271 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001272 InFlag = Chain.getValue(1);
1273
Bob Wilson1f595bb2009-04-17 19:07:39 +00001274 // Handle result values, copying them out of physregs into vregs that we
1275 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001276 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1277 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001278}
1279
Dale Johannesen51e28e62010-06-03 21:09:53 +00001280/// MatchingStackOffset - Return true if the given stack call argument is
1281/// already available in the same position (relatively) of the caller's
1282/// incoming argument stack.
1283static
1284bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1285 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1286 const ARMInstrInfo *TII) {
1287 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1288 int FI = INT_MAX;
1289 if (Arg.getOpcode() == ISD::CopyFromReg) {
1290 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
1291 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
1292 return false;
1293 MachineInstr *Def = MRI->getVRegDef(VR);
1294 if (!Def)
1295 return false;
1296 if (!Flags.isByVal()) {
1297 if (!TII->isLoadFromStackSlot(Def, FI))
1298 return false;
1299 } else {
1300// unsigned Opcode = Def->getOpcode();
1301// if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
1302// Def->getOperand(1).isFI()) {
1303// FI = Def->getOperand(1).getIndex();
1304// Bytes = Flags.getByValSize();
1305// } else
1306 return false;
1307 }
1308 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1309 if (Flags.isByVal())
1310 // ByVal argument is passed in as a pointer but it's now being
1311 // dereferenced. e.g.
1312 // define @foo(%struct.X* %A) {
1313 // tail call @bar(%struct.X* byval %A)
1314 // }
1315 return false;
1316 SDValue Ptr = Ld->getBasePtr();
1317 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1318 if (!FINode)
1319 return false;
1320 FI = FINode->getIndex();
1321 } else
1322 return false;
1323
1324 assert(FI != INT_MAX);
1325 if (!MFI->isFixedObjectIndex(FI))
1326 return false;
1327 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1328}
1329
1330/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1331/// for tail call optimization. Targets which want to do tail call
1332/// optimization should implement this function.
1333bool
1334ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1335 CallingConv::ID CalleeCC,
1336 bool isVarArg,
1337 bool isCalleeStructRet,
1338 bool isCallerStructRet,
1339 const SmallVectorImpl<ISD::OutputArg> &Outs,
1340 const SmallVectorImpl<ISD::InputArg> &Ins,
1341 SelectionDAG& DAG) const {
1342
Dale Johannesen51e28e62010-06-03 21:09:53 +00001343 const Function *CallerF = DAG.getMachineFunction().getFunction();
1344 CallingConv::ID CallerCC = CallerF->getCallingConv();
1345 bool CCMatch = CallerCC == CalleeCC;
1346
1347 // Look for obvious safe cases to perform tail call optimization that do not
1348 // require ABI changes. This is what gcc calls sibcall.
1349
1350 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
1351 // emit a special epilogue.
1352 // Not sure yet if this is true on ARM.
1353//?? if (RegInfo->needsStackRealignment(MF))
1354//?? return false;
1355
1356 // Do not sibcall optimize vararg calls unless the call site is not passing any
1357 // arguments.
1358 if (isVarArg && !Outs.empty())
1359 return false;
1360
1361 // Also avoid sibcall optimization if either caller or callee uses struct
1362 // return semantics.
1363 if (isCalleeStructRet || isCallerStructRet)
1364 return false;
1365
1366 // If the calling conventions do not match, then we'd better make sure the
1367 // results are returned in the same way as what the caller expects.
1368 if (!CCMatch) {
1369 SmallVector<CCValAssign, 16> RVLocs1;
1370 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
1371 RVLocs1, *DAG.getContext());
1372 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1373
1374 SmallVector<CCValAssign, 16> RVLocs2;
1375 CCState CCInfo2(CallerCC, false, getTargetMachine(),
1376 RVLocs2, *DAG.getContext());
1377 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1378
1379 if (RVLocs1.size() != RVLocs2.size())
1380 return false;
1381 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1382 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1383 return false;
1384 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1385 return false;
1386 if (RVLocs1[i].isRegLoc()) {
1387 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1388 return false;
1389 } else {
1390 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1391 return false;
1392 }
1393 }
1394 }
1395
1396 // If the callee takes no arguments then go on to check the results of the
1397 // call.
1398 if (!Outs.empty()) {
1399 // Check if stack adjustment is needed. For now, do not do this if any
1400 // argument is passed on the stack.
1401 SmallVector<CCValAssign, 16> ArgLocs;
1402 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
1403 ArgLocs, *DAG.getContext());
1404 CCInfo.AnalyzeCallOperands(Outs,
1405 CCAssignFnForNode(CalleeCC, false, isVarArg));
1406 if (CCInfo.getNextStackOffset()) {
1407 MachineFunction &MF = DAG.getMachineFunction();
1408
1409 // Check if the arguments are already laid out in the right way as
1410 // the caller's fixed stack objects.
1411 MachineFrameInfo *MFI = MF.getFrameInfo();
1412 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1413 const ARMInstrInfo *TII =
1414 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001415 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1416 i != e;
1417 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001418 CCValAssign &VA = ArgLocs[i];
1419 EVT RegVT = VA.getLocVT();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001420 SDValue Arg = Outs[realArgIdx].Val;
1421 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001422 if (VA.getLocInfo() == CCValAssign::Indirect)
1423 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001424 if (VA.needsCustom()) {
1425 // f64 and vector types are split into multiple registers or
1426 // register/stack-slot combinations. The types will not match
1427 // the registers; give up on memory f64 refs until we figure
1428 // out what to do about this.
1429 if (!VA.isRegLoc())
1430 return false;
1431 if (!ArgLocs[++i].isRegLoc())
1432 return false;
1433 if (RegVT == MVT::v2f64) {
1434 if (!ArgLocs[++i].isRegLoc())
1435 return false;
1436 if (!ArgLocs[++i].isRegLoc())
1437 return false;
1438 }
1439 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001440 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1441 MFI, MRI, TII))
1442 return false;
1443 }
1444 }
1445 }
1446 }
1447
1448 return true;
1449}
1450
Dan Gohman98ca4f22009-08-05 01:29:28 +00001451SDValue
1452ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001453 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001454 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmand858e902010-04-17 15:26:15 +00001455 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001456
Bob Wilsondee46d72009-04-17 20:35:10 +00001457 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001458 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001459
Bob Wilsondee46d72009-04-17 20:35:10 +00001460 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001461 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1462 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001463
Dan Gohman98ca4f22009-08-05 01:29:28 +00001464 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001465 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1466 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001467
1468 // If this is the first return lowered for this function, add
1469 // the regs to the liveout set for the function.
1470 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1471 for (unsigned i = 0; i != RVLocs.size(); ++i)
1472 if (RVLocs[i].isRegLoc())
1473 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001474 }
1475
Bob Wilson1f595bb2009-04-17 19:07:39 +00001476 SDValue Flag;
1477
1478 // Copy the result values into the output registers.
1479 for (unsigned i = 0, realRVLocIdx = 0;
1480 i != RVLocs.size();
1481 ++i, ++realRVLocIdx) {
1482 CCValAssign &VA = RVLocs[i];
1483 assert(VA.isRegLoc() && "Can only return in registers!");
1484
Dan Gohman98ca4f22009-08-05 01:29:28 +00001485 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001486
1487 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001488 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001489 case CCValAssign::Full: break;
1490 case CCValAssign::BCvt:
1491 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1492 break;
1493 }
1494
Bob Wilson1f595bb2009-04-17 19:07:39 +00001495 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001496 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001497 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001498 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1499 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001500 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001501 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001502
1503 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1504 Flag = Chain.getValue(1);
1505 VA = RVLocs[++i]; // skip ahead to next loc
1506 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1507 HalfGPRs.getValue(1), Flag);
1508 Flag = Chain.getValue(1);
1509 VA = RVLocs[++i]; // skip ahead to next loc
1510
1511 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001512 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1513 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001514 }
1515 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1516 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001517 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001518 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001519 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001520 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001521 VA = RVLocs[++i]; // skip ahead to next loc
1522 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1523 Flag);
1524 } else
1525 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1526
Bob Wilsondee46d72009-04-17 20:35:10 +00001527 // Guarantee that all emitted copies are
1528 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001529 Flag = Chain.getValue(1);
1530 }
1531
1532 SDValue result;
1533 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001534 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001535 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001536 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001537
1538 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001539}
1540
Bob Wilsonb62d2572009-11-03 00:02:05 +00001541// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1542// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1543// one of the above mentioned nodes. It has to be wrapped because otherwise
1544// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1545// be used to form addressing mode. These wrapped nodes will be selected
1546// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001547static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001548 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001549 // FIXME there is no actual debug info here
1550 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001551 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001552 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001553 if (CP->isMachineConstantPoolEntry())
1554 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1555 CP->getAlignment());
1556 else
1557 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1558 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001559 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001560}
1561
Dan Gohmand858e902010-04-17 15:26:15 +00001562SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1563 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001564 MachineFunction &MF = DAG.getMachineFunction();
1565 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1566 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001567 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001568 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001569 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001570 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1571 SDValue CPAddr;
1572 if (RelocM == Reloc::Static) {
1573 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1574 } else {
1575 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001576 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001577 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1578 ARMCP::CPBlockAddress,
1579 PCAdj);
1580 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1581 }
1582 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1583 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001584 PseudoSourceValue::getConstantPool(), 0,
1585 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001586 if (RelocM == Reloc::Static)
1587 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001588 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001589 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001590}
1591
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001592// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001593SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001594ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001595 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001596 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001597 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001598 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001599 MachineFunction &MF = DAG.getMachineFunction();
1600 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1601 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001602 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001603 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001604 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001605 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001606 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001607 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
David Greene1b58cab2010-02-15 16:55:24 +00001608 PseudoSourceValue::getConstantPool(), 0,
1609 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001610 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001611
Evan Chenge7e0d622009-11-06 22:24:13 +00001612 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001613 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001614
1615 // call __tls_get_addr.
1616 ArgListTy Args;
1617 ArgListEntry Entry;
1618 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001619 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001620 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001621 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001622 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001623 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1624 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001625 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001626 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001627 return CallResult.first;
1628}
1629
1630// Lower ISD::GlobalTLSAddress using the "initial exec" or
1631// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001632SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001633ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001634 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001635 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001636 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001637 SDValue Offset;
1638 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001639 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001640 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001641 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001642
Chris Lattner4fb63d02009-07-15 04:12:33 +00001643 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001644 MachineFunction &MF = DAG.getMachineFunction();
1645 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1646 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1647 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001648 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1649 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001650 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001651 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001652 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001653 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001654 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001655 PseudoSourceValue::getConstantPool(), 0,
1656 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001657 Chain = Offset.getValue(1);
1658
Evan Chenge7e0d622009-11-06 22:24:13 +00001659 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001660 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001661
Evan Cheng9eda6892009-10-31 03:39:36 +00001662 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001663 PseudoSourceValue::getConstantPool(), 0,
1664 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001665 } else {
1666 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001667 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001668 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001669 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001670 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001671 PseudoSourceValue::getConstantPool(), 0,
1672 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001673 }
1674
1675 // The address of the thread local variable is the add of the thread
1676 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001677 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001678}
1679
Dan Gohman475871a2008-07-27 21:46:04 +00001680SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001681ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001682 // TODO: implement the "local dynamic" model
1683 assert(Subtarget->isTargetELF() &&
1684 "TLS not implemented for non-ELF targets");
1685 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1686 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1687 // otherwise use the "Local Exec" TLS Model
1688 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1689 return LowerToTLSGeneralDynamicModel(GA, DAG);
1690 else
1691 return LowerToTLSExecModels(GA, DAG);
1692}
1693
Dan Gohman475871a2008-07-27 21:46:04 +00001694SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001695 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001696 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001697 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001698 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001699 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1700 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001701 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001702 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001703 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001704 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001705 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001706 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001707 CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001708 PseudoSourceValue::getConstantPool(), 0,
1709 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001710 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001711 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001712 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001713 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001714 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001715 PseudoSourceValue::getGOT(), 0,
1716 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001717 return Result;
1718 } else {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001719 // If we have T2 ops, we can materialize the address directly via movt/movw
1720 // pair. This is always cheaper.
1721 if (Subtarget->useMovt()) {
1722 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
1723 DAG.getTargetGlobalAddress(GV, PtrVT));
1724 } else {
1725 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1726 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1727 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001728 PseudoSourceValue::getConstantPool(), 0,
1729 false, false, 0);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001730 }
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001731 }
1732}
1733
Dan Gohman475871a2008-07-27 21:46:04 +00001734SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001735 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001736 MachineFunction &MF = DAG.getMachineFunction();
1737 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1738 unsigned ARMPCLabelIndex = 0;
Owen Andersone50ed302009-08-10 22:56:29 +00001739 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001740 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001741 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001742 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001743 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001744 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001745 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001746 else {
Evan Chenge7e0d622009-11-06 22:24:13 +00001747 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001748 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1749 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001750 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001751 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001752 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001753 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001754
Evan Cheng9eda6892009-10-31 03:39:36 +00001755 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001756 PseudoSourceValue::getConstantPool(), 0,
1757 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001758 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001759
1760 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001761 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001762 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001763 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001764
Evan Cheng63476a82009-09-03 07:04:02 +00001765 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Evan Cheng9eda6892009-10-31 03:39:36 +00001766 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001767 PseudoSourceValue::getGOT(), 0,
1768 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001769
1770 return Result;
1771}
1772
Dan Gohman475871a2008-07-27 21:46:04 +00001773SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001774 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001775 assert(Subtarget->isTargetELF() &&
1776 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00001777 MachineFunction &MF = DAG.getMachineFunction();
1778 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1779 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Andersone50ed302009-08-10 22:56:29 +00001780 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001781 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001782 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001783 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1784 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001785 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001786 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001787 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001788 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001789 PseudoSourceValue::getConstantPool(), 0,
1790 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001791 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001792 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001793}
1794
Jim Grosbach0e0da732009-05-12 23:59:14 +00001795SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00001796ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
1797 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00001798 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00001799 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
1800 Op.getOperand(1), Val);
1801}
1802
1803SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00001804ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
1805 DebugLoc dl = Op.getDebugLoc();
1806 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
1807 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
1808}
1809
1810SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00001811ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001812 const ARMSubtarget *Subtarget)
1813 const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001814 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001815 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001816 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001817 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001818 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001819 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001820 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1821 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001822 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001823 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00001824 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1825 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001826 EVT PtrVT = getPointerTy();
1827 DebugLoc dl = Op.getDebugLoc();
1828 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1829 SDValue CPAddr;
1830 unsigned PCAdj = (RelocM != Reloc::PIC_)
1831 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001832 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001833 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1834 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001835 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001836 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001837 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00001838 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001839 PseudoSourceValue::getConstantPool(), 0,
1840 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001841 SDValue Chain = Result.getValue(1);
1842
1843 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001844 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001845 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1846 }
1847 return Result;
1848 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001849 }
1850}
1851
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001852static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
1853 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00001854 DebugLoc dl = Op.getDebugLoc();
1855 SDValue Op5 = Op.getOperand(5);
1856 SDValue Res;
1857 unsigned isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue();
1858 if (isDeviceBarrier) {
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001859 if (Subtarget->hasV7Ops())
1860 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0));
1861 else
1862 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0),
1863 DAG.getConstant(0, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00001864 } else {
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001865 if (Subtarget->hasV7Ops())
1866 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
1867 else
1868 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
1869 DAG.getConstant(0, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00001870 }
1871 return Res;
1872}
1873
Dan Gohman1e93df62010-04-17 14:41:14 +00001874static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
1875 MachineFunction &MF = DAG.getMachineFunction();
1876 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
1877
Evan Chenga8e29892007-01-19 07:51:42 +00001878 // vastart just stores the address of the VarArgsFrameIndex slot into the
1879 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001880 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001881 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00001882 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001883 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
David Greene1b58cab2010-02-15 16:55:24 +00001884 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
1885 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001886}
1887
Dan Gohman475871a2008-07-27 21:46:04 +00001888SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001889ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1890 SelectionDAG &DAG) const {
Evan Cheng86198642009-08-07 00:34:42 +00001891 SDNode *Node = Op.getNode();
1892 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001893 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001894 SDValue Chain = Op.getOperand(0);
1895 SDValue Size = Op.getOperand(1);
1896 SDValue Align = Op.getOperand(2);
1897
1898 // Chain the dynamic stack allocation so that it doesn't modify the stack
1899 // pointer when other instructions are using the stack.
1900 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1901
1902 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1903 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1904 if (AlignVal > StackAlign)
1905 // Do this now since selection pass cannot introduce new target
1906 // independent node.
1907 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1908
1909 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1910 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1911 // do even more horrible hack later.
1912 MachineFunction &MF = DAG.getMachineFunction();
1913 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1914 if (AFI->isThumb1OnlyFunction()) {
1915 bool Negate = true;
1916 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1917 if (C) {
1918 uint32_t Val = C->getZExtValue();
1919 if (Val <= 508 && ((Val & 3) == 0))
1920 Negate = false;
1921 }
1922 if (Negate)
1923 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1924 }
1925
Owen Anderson825b72b2009-08-11 20:47:22 +00001926 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001927 SDValue Ops1[] = { Chain, Size, Align };
1928 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1929 Chain = Res.getValue(1);
1930 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1931 DAG.getIntPtrConstant(0, true), SDValue());
1932 SDValue Ops2[] = { Res, Chain };
1933 return DAG.getMergeValues(Ops2, 2, dl);
1934}
1935
1936SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001937ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1938 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001939 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001940 MachineFunction &MF = DAG.getMachineFunction();
1941 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1942
1943 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001944 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001945 RC = ARM::tGPRRegisterClass;
1946 else
1947 RC = ARM::GPRRegisterClass;
1948
1949 // Transform the arguments stored in physical registers into virtual ones.
Evan Cheng2457f2c2010-05-22 01:47:14 +00001950 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001951 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001952
1953 SDValue ArgValue2;
1954 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001955 MachineFrameInfo *MFI = MF.getFrameInfo();
Bob Wilson6a234f02010-04-13 22:03:22 +00001956 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true, false);
Bob Wilson5bafff32009-06-22 23:27:02 +00001957
1958 // Create load node to retrieve arguments from the stack.
1959 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001960 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00001961 PseudoSourceValue::getFixedStack(FI), 0,
1962 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001963 } else {
1964 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001965 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001966 }
1967
Jim Grosbache5165492009-11-09 00:11:35 +00001968 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001969}
1970
1971SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001972ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001973 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001974 const SmallVectorImpl<ISD::InputArg>
1975 &Ins,
1976 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001977 SmallVectorImpl<SDValue> &InVals)
1978 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001979
Bob Wilson1f595bb2009-04-17 19:07:39 +00001980 MachineFunction &MF = DAG.getMachineFunction();
1981 MachineFrameInfo *MFI = MF.getFrameInfo();
1982
Bob Wilson1f595bb2009-04-17 19:07:39 +00001983 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1984
1985 // Assign locations to all of the incoming arguments.
1986 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001987 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1988 *DAG.getContext());
1989 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001990 CCAssignFnForNode(CallConv, /* Return*/ false,
1991 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001992
1993 SmallVector<SDValue, 16> ArgValues;
1994
1995 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1996 CCValAssign &VA = ArgLocs[i];
1997
Bob Wilsondee46d72009-04-17 20:35:10 +00001998 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001999 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002000 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002001
Bob Wilson5bafff32009-06-22 23:27:02 +00002002 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002003 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002004 // f64 and vector types are split up into multiple registers or
2005 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002006 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002007 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002008 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002009 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002010 SDValue ArgValue2;
2011 if (VA.isMemLoc()) {
2012 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(),
2013 true, false);
2014 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2015 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
2016 PseudoSourceValue::getFixedStack(FI), 0,
2017 false, false, 0);
2018 } else {
2019 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2020 Chain, DAG, dl);
2021 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002022 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2023 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002024 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002025 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002026 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2027 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002028 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002029
Bob Wilson5bafff32009-06-22 23:27:02 +00002030 } else {
2031 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002032
Owen Anderson825b72b2009-08-11 20:47:22 +00002033 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002034 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002035 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002036 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002037 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002038 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002039 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002040 RC = (AFI->isThumb1OnlyFunction() ?
2041 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002042 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002043 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002044
2045 // Transform the arguments in physical registers into virtual ones.
2046 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002047 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002048 }
2049
2050 // If this is an 8 or 16-bit value, it is really passed promoted
2051 // to 32 bits. Insert an assert[sz]ext to capture this, then
2052 // truncate to the right size.
2053 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002054 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002055 case CCValAssign::Full: break;
2056 case CCValAssign::BCvt:
2057 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
2058 break;
2059 case CCValAssign::SExt:
2060 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2061 DAG.getValueType(VA.getValVT()));
2062 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2063 break;
2064 case CCValAssign::ZExt:
2065 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2066 DAG.getValueType(VA.getValVT()));
2067 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2068 break;
2069 }
2070
Dan Gohman98ca4f22009-08-05 01:29:28 +00002071 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002072
2073 } else { // VA.isRegLoc()
2074
2075 // sanity check
2076 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002077 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002078
2079 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00002080 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
2081 true, false);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002082
Bob Wilsondee46d72009-04-17 20:35:10 +00002083 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002084 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002085 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00002086 PseudoSourceValue::getFixedStack(FI), 0,
2087 false, false, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002088 }
2089 }
2090
2091 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00002092 if (isVarArg) {
2093 static const unsigned GPRArgRegs[] = {
2094 ARM::R0, ARM::R1, ARM::R2, ARM::R3
2095 };
2096
Bob Wilsondee46d72009-04-17 20:35:10 +00002097 unsigned NumGPRs = CCInfo.getFirstUnallocated
2098 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002099
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00002100 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
2101 unsigned VARegSize = (4 - NumGPRs) * 4;
2102 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00002103 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00002104 if (VARegSaveSize) {
2105 // If this function is vararg, store any remaining integer argument regs
2106 // to their spots on the stack so that they may be loaded by deferencing
2107 // the result of va_next.
2108 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00002109 AFI->setVarArgsFrameIndex(
2110 MFI->CreateFixedObject(VARegSaveSize,
2111 ArgOffset + VARegSaveSize - VARegSize,
2112 true, false));
2113 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2114 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00002115
Dan Gohman475871a2008-07-27 21:46:04 +00002116 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00002117 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002118 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002119 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00002120 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002121 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00002122 RC = ARM::GPRRegisterClass;
2123
Bob Wilson998e1252009-04-20 18:36:57 +00002124 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002125 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00002126 SDValue Store =
2127 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Jim Grosbach18f30e62010-06-02 21:53:11 +00002128 PseudoSourceValue::getFixedStack(AFI->getVarArgsFrameIndex()),
2129 0, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002130 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002131 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00002132 DAG.getConstant(4, getPointerTy()));
2133 }
2134 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002135 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002136 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00002137 } else
2138 // This will point to the next argument passed via stack.
Dan Gohman1e93df62010-04-17 14:41:14 +00002139 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset,
2140 true, false));
Evan Chenga8e29892007-01-19 07:51:42 +00002141 }
2142
Dan Gohman98ca4f22009-08-05 01:29:28 +00002143 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002144}
2145
2146/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002147static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002148 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002149 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002150 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002151 // Maybe this has already been legalized into the constant pool?
2152 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002153 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002154 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002155 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002156 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002157 }
2158 }
2159 return false;
2160}
2161
Evan Chenga8e29892007-01-19 07:51:42 +00002162/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2163/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002164SDValue
2165ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Dan Gohmand858e902010-04-17 15:26:15 +00002166 SDValue &ARMCC, SelectionDAG &DAG,
2167 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002168 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002169 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002170 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002171 // Constant does not fit, try adjusting it by one?
2172 switch (CC) {
2173 default: break;
2174 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002175 case ISD::SETGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00002176 if (isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002177 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002178 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002179 }
2180 break;
2181 case ISD::SETULT:
2182 case ISD::SETUGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00002183 if (C > 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002184 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002185 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002186 }
2187 break;
2188 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002189 case ISD::SETGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00002190 if (isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002191 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002192 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002193 }
2194 break;
2195 case ISD::SETULE:
2196 case ISD::SETUGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00002197 if (C < 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002198 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002199 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002200 }
2201 break;
2202 }
2203 }
2204 }
2205
2206 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002207 ARMISD::NodeType CompareType;
2208 switch (CondCode) {
2209 default:
2210 CompareType = ARMISD::CMP;
2211 break;
2212 case ARMCC::EQ:
2213 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002214 // Uses only Z Flag
2215 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002216 break;
2217 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002218 ARMCC = DAG.getConstant(CondCode, MVT::i32);
2219 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002220}
2221
2222/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00002223static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00002224 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00002225 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002226 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00002227 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002228 else
Owen Anderson825b72b2009-08-11 20:47:22 +00002229 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
2230 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002231}
2232
Dan Gohmand858e902010-04-17 15:26:15 +00002233SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002234 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002235 SDValue LHS = Op.getOperand(0);
2236 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002237 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002238 SDValue TrueVal = Op.getOperand(2);
2239 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002240 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002241
Owen Anderson825b72b2009-08-11 20:47:22 +00002242 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00002243 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002244 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00002245 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Dale Johannesende064702009-02-06 21:50:26 +00002246 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002247 }
2248
2249 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002250 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002251
Owen Anderson825b72b2009-08-11 20:47:22 +00002252 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
2253 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002254 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
2255 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00002256 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002257 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002258 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002259 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00002260 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002261 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00002262 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002263 }
2264 return Result;
2265}
2266
Dan Gohmand858e902010-04-17 15:26:15 +00002267SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002268 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002269 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002270 SDValue LHS = Op.getOperand(2);
2271 SDValue RHS = Op.getOperand(3);
2272 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002273 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002274
Owen Anderson825b72b2009-08-11 20:47:22 +00002275 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00002276 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002277 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00002278 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002279 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00002280 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002281 }
2282
Owen Anderson825b72b2009-08-11 20:47:22 +00002283 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00002284 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002285 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002286
Dale Johannesende064702009-02-06 21:50:26 +00002287 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002288 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
2289 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2290 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002291 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002292 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002293 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002294 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00002295 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002296 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002297 }
2298 return Res;
2299}
2300
Dan Gohmand858e902010-04-17 15:26:15 +00002301SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002302 SDValue Chain = Op.getOperand(0);
2303 SDValue Table = Op.getOperand(1);
2304 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002305 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002306
Owen Andersone50ed302009-08-10 22:56:29 +00002307 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002308 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2309 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002310 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002311 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002312 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002313 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2314 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002315 if (Subtarget->isThumb2()) {
2316 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2317 // which does another jump to the destination. This also makes it easier
2318 // to translate it to TBB / TBH later.
2319 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002320 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002321 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002322 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002323 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002324 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002325 PseudoSourceValue::getJumpTable(), 0,
2326 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002327 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002328 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002329 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002330 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002331 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002332 PseudoSourceValue::getJumpTable(), 0, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002333 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002334 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002335 }
Evan Chenga8e29892007-01-19 07:51:42 +00002336}
2337
Bob Wilson76a312b2010-03-19 22:51:32 +00002338static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2339 DebugLoc dl = Op.getDebugLoc();
2340 unsigned Opc;
2341
2342 switch (Op.getOpcode()) {
2343 default:
2344 assert(0 && "Invalid opcode!");
2345 case ISD::FP_TO_SINT:
2346 Opc = ARMISD::FTOSI;
2347 break;
2348 case ISD::FP_TO_UINT:
2349 Opc = ARMISD::FTOUI;
2350 break;
2351 }
2352 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
2353 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
2354}
2355
2356static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2357 EVT VT = Op.getValueType();
2358 DebugLoc dl = Op.getDebugLoc();
2359 unsigned Opc;
2360
2361 switch (Op.getOpcode()) {
2362 default:
2363 assert(0 && "Invalid opcode!");
2364 case ISD::SINT_TO_FP:
2365 Opc = ARMISD::SITOF;
2366 break;
2367 case ISD::UINT_TO_FP:
2368 Opc = ARMISD::UITOF;
2369 break;
2370 }
2371
2372 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
2373 return DAG.getNode(Opc, dl, VT, Op);
2374}
2375
Dan Gohman475871a2008-07-27 21:46:04 +00002376static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002377 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002378 SDValue Tmp0 = Op.getOperand(0);
2379 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002380 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002381 EVT VT = Op.getValueType();
2382 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00002383 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
2384 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002385 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
2386 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002387 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002388}
2389
Evan Cheng2457f2c2010-05-22 01:47:14 +00002390SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
2391 MachineFunction &MF = DAG.getMachineFunction();
2392 MachineFrameInfo *MFI = MF.getFrameInfo();
2393 MFI->setReturnAddressIsTaken(true);
2394
2395 EVT VT = Op.getValueType();
2396 DebugLoc dl = Op.getDebugLoc();
2397 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2398 if (Depth) {
2399 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
2400 SDValue Offset = DAG.getConstant(4, MVT::i32);
2401 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
2402 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
2403 NULL, 0, false, false, 0);
2404 }
2405
2406 // Return LR, which contains the return address. Mark it an implicit live-in.
Evan Chengc7cf10c2010-05-24 18:00:18 +00002407 unsigned Reg = MF.addLiveIn(ARM::LR, ARM::GPRRegisterClass);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002408 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
2409}
2410
Dan Gohmand858e902010-04-17 15:26:15 +00002411SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002412 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2413 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002414
Owen Andersone50ed302009-08-10 22:56:29 +00002415 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002416 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2417 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002418 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002419 ? ARM::R7 : ARM::R11;
2420 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2421 while (Depth--)
David Greene1b58cab2010-02-15 16:55:24 +00002422 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
2423 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002424 return FrameAddr;
2425}
2426
Bob Wilson9f3f0612010-04-17 05:30:19 +00002427/// ExpandBIT_CONVERT - If the target supports VFP, this function is called to
2428/// expand a bit convert where either the source or destination type is i64 to
2429/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2430/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2431/// vectors), since the legalizer won't know what to do with that.
Duncan Sands1607f052008-12-01 11:39:25 +00002432static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002433 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2434 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002435 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002436
Bob Wilson9f3f0612010-04-17 05:30:19 +00002437 // This function is only supposed to be called for i64 types, either as the
2438 // source or destination of the bit convert.
2439 EVT SrcVT = Op.getValueType();
2440 EVT DstVT = N->getValueType(0);
2441 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
2442 "ExpandBIT_CONVERT called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002443
Bob Wilson9f3f0612010-04-17 05:30:19 +00002444 // Turn i64->f64 into VMOVDRR.
2445 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002446 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2447 DAG.getConstant(0, MVT::i32));
2448 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2449 DAG.getConstant(1, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00002450 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002451 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002452
Jim Grosbache5165492009-11-09 00:11:35 +00002453 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002454 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2455 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2456 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2457 // Merge the pieces into a single i64 value.
2458 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2459 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002460
Bob Wilson9f3f0612010-04-17 05:30:19 +00002461 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002462}
2463
Bob Wilson5bafff32009-06-22 23:27:02 +00002464/// getZeroVector - Returns a vector of specified type with all zero elements.
2465///
Owen Andersone50ed302009-08-10 22:56:29 +00002466static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002467 assert(VT.isVector() && "Expected a vector type");
2468
2469 // Zero vectors are used to represent vector negation and in those cases
2470 // will be implemented with the NEON VNEG instruction. However, VNEG does
2471 // not support i64 elements, so sometimes the zero vectors will need to be
2472 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002473 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilson5bafff32009-06-22 23:27:02 +00002474 // to their dest type. This ensures they get CSE'd.
2475 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002476 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2477 SmallVector<SDValue, 8> Ops;
2478 MVT TVT;
2479
2480 if (VT.getSizeInBits() == 64) {
2481 Ops.assign(8, Cst); TVT = MVT::v8i8;
2482 } else {
2483 Ops.assign(16, Cst); TVT = MVT::v16i8;
2484 }
2485 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002486
2487 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2488}
2489
2490/// getOnesVector - Returns a vector of specified type with all bits set.
2491///
Owen Andersone50ed302009-08-10 22:56:29 +00002492static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002493 assert(VT.isVector() && "Expected a vector type");
2494
Bob Wilson929ffa22009-10-30 20:13:25 +00002495 // Always build ones vectors as <16 x i8> or <8 x i8> bitcasted to their
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002496 // dest type. This ensures they get CSE'd.
Bob Wilson5bafff32009-06-22 23:27:02 +00002497 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002498 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2499 SmallVector<SDValue, 8> Ops;
2500 MVT TVT;
2501
2502 if (VT.getSizeInBits() == 64) {
2503 Ops.assign(8, Cst); TVT = MVT::v8i8;
2504 } else {
2505 Ops.assign(16, Cst); TVT = MVT::v16i8;
2506 }
2507 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002508
2509 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2510}
2511
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002512/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2513/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002514SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2515 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002516 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2517 EVT VT = Op.getValueType();
2518 unsigned VTBits = VT.getSizeInBits();
2519 DebugLoc dl = Op.getDebugLoc();
2520 SDValue ShOpLo = Op.getOperand(0);
2521 SDValue ShOpHi = Op.getOperand(1);
2522 SDValue ShAmt = Op.getOperand(2);
2523 SDValue ARMCC;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002524 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002525
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002526 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2527
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002528 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2529 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2530 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2531 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2532 DAG.getConstant(VTBits, MVT::i32));
2533 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2534 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002535 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002536
2537 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2538 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002539 ARMCC, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002540 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002541 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC,
2542 CCR, Cmp);
2543
2544 SDValue Ops[2] = { Lo, Hi };
2545 return DAG.getMergeValues(Ops, 2, dl);
2546}
2547
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002548/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2549/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002550SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2551 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002552 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2553 EVT VT = Op.getValueType();
2554 unsigned VTBits = VT.getSizeInBits();
2555 DebugLoc dl = Op.getDebugLoc();
2556 SDValue ShOpLo = Op.getOperand(0);
2557 SDValue ShOpHi = Op.getOperand(1);
2558 SDValue ShAmt = Op.getOperand(2);
2559 SDValue ARMCC;
2560
2561 assert(Op.getOpcode() == ISD::SHL_PARTS);
2562 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2563 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2564 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2565 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2566 DAG.getConstant(VTBits, MVT::i32));
2567 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2568 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2569
2570 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2571 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2572 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002573 ARMCC, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002574 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
2575 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMCC,
2576 CCR, Cmp);
2577
2578 SDValue Ops[2] = { Lo, Hi };
2579 return DAG.getMergeValues(Ops, 2, dl);
2580}
2581
Jim Grosbach3482c802010-01-18 19:58:49 +00002582static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
2583 const ARMSubtarget *ST) {
2584 EVT VT = N->getValueType(0);
2585 DebugLoc dl = N->getDebugLoc();
2586
2587 if (!ST->hasV6T2Ops())
2588 return SDValue();
2589
2590 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
2591 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
2592}
2593
Bob Wilson5bafff32009-06-22 23:27:02 +00002594static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2595 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002596 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002597 DebugLoc dl = N->getDebugLoc();
2598
2599 // Lower vector shifts on NEON to use VSHL.
2600 if (VT.isVector()) {
2601 assert(ST->hasNEON() && "unexpected vector shift");
2602
2603 // Left shifts translate directly to the vshiftu intrinsic.
2604 if (N->getOpcode() == ISD::SHL)
2605 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002606 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002607 N->getOperand(0), N->getOperand(1));
2608
2609 assert((N->getOpcode() == ISD::SRA ||
2610 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2611
2612 // NEON uses the same intrinsics for both left and right shifts. For
2613 // right shifts, the shift amounts are negative, so negate the vector of
2614 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002615 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002616 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2617 getZeroVector(ShiftVT, DAG, dl),
2618 N->getOperand(1));
2619 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2620 Intrinsic::arm_neon_vshifts :
2621 Intrinsic::arm_neon_vshiftu);
2622 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002623 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002624 N->getOperand(0), NegatedCount);
2625 }
2626
Eli Friedmance392eb2009-08-22 03:13:10 +00002627 // We can get here for a node like i32 = ISD::SHL i32, i64
2628 if (VT != MVT::i64)
2629 return SDValue();
2630
2631 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002632 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002633
Chris Lattner27a6c732007-11-24 07:07:01 +00002634 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2635 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002636 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002637 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002638
Chris Lattner27a6c732007-11-24 07:07:01 +00002639 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002640 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002641
Chris Lattner27a6c732007-11-24 07:07:01 +00002642 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002643 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00002644 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00002645 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00002646 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002647
Chris Lattner27a6c732007-11-24 07:07:01 +00002648 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2649 // captures the result into a carry flag.
2650 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002651 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002652
Chris Lattner27a6c732007-11-24 07:07:01 +00002653 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002654 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002655
Chris Lattner27a6c732007-11-24 07:07:01 +00002656 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002657 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002658}
2659
Bob Wilson5bafff32009-06-22 23:27:02 +00002660static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2661 SDValue TmpOp0, TmpOp1;
2662 bool Invert = false;
2663 bool Swap = false;
2664 unsigned Opc = 0;
2665
2666 SDValue Op0 = Op.getOperand(0);
2667 SDValue Op1 = Op.getOperand(1);
2668 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002669 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002670 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2671 DebugLoc dl = Op.getDebugLoc();
2672
2673 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2674 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002675 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002676 case ISD::SETUNE:
2677 case ISD::SETNE: Invert = true; // Fallthrough
2678 case ISD::SETOEQ:
2679 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2680 case ISD::SETOLT:
2681 case ISD::SETLT: Swap = true; // Fallthrough
2682 case ISD::SETOGT:
2683 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2684 case ISD::SETOLE:
2685 case ISD::SETLE: Swap = true; // Fallthrough
2686 case ISD::SETOGE:
2687 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2688 case ISD::SETUGE: Swap = true; // Fallthrough
2689 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2690 case ISD::SETUGT: Swap = true; // Fallthrough
2691 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2692 case ISD::SETUEQ: Invert = true; // Fallthrough
2693 case ISD::SETONE:
2694 // Expand this to (OLT | OGT).
2695 TmpOp0 = Op0;
2696 TmpOp1 = Op1;
2697 Opc = ISD::OR;
2698 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2699 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2700 break;
2701 case ISD::SETUO: Invert = true; // Fallthrough
2702 case ISD::SETO:
2703 // Expand this to (OLT | OGE).
2704 TmpOp0 = Op0;
2705 TmpOp1 = Op1;
2706 Opc = ISD::OR;
2707 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2708 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2709 break;
2710 }
2711 } else {
2712 // Integer comparisons.
2713 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002714 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002715 case ISD::SETNE: Invert = true;
2716 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2717 case ISD::SETLT: Swap = true;
2718 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2719 case ISD::SETLE: Swap = true;
2720 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2721 case ISD::SETULT: Swap = true;
2722 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2723 case ISD::SETULE: Swap = true;
2724 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2725 }
2726
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002727 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002728 if (Opc == ARMISD::VCEQ) {
2729
2730 SDValue AndOp;
2731 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2732 AndOp = Op0;
2733 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2734 AndOp = Op1;
2735
2736 // Ignore bitconvert.
2737 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2738 AndOp = AndOp.getOperand(0);
2739
2740 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2741 Opc = ARMISD::VTST;
2742 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2743 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2744 Invert = !Invert;
2745 }
2746 }
2747 }
2748
2749 if (Swap)
2750 std::swap(Op0, Op1);
2751
2752 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2753
2754 if (Invert)
2755 Result = DAG.getNOT(dl, Result, VT);
2756
2757 return Result;
2758}
2759
2760/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
Bob Wilson1a913ed2010-06-11 21:34:50 +00002761/// VMOV instruction. If so, return either the constant being splatted or the
2762/// encoded value, depending on the DoEncode parameter. The format of the
2763/// encoded value is: bit12=Op, bits11-8=Cmode, bits7-0=Immediate.
Bob Wilson5bafff32009-06-22 23:27:02 +00002764static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
Bob Wilson1a913ed2010-06-11 21:34:50 +00002765 unsigned SplatBitSize, SelectionDAG &DAG,
2766 bool DoEncode) {
2767 unsigned Op, Cmode, Imm;
2768 EVT VT;
2769
2770 Op = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00002771 switch (SplatBitSize) {
2772 case 8:
Bob Wilson1a913ed2010-06-11 21:34:50 +00002773 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00002774 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson1a913ed2010-06-11 21:34:50 +00002775 Cmode = 0xe;
2776 Imm = SplatBits;
2777 VT = MVT::i8;
2778 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002779
2780 case 16:
2781 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilson1a913ed2010-06-11 21:34:50 +00002782 VT = MVT::i16;
2783 if ((SplatBits & ~0xff) == 0) {
2784 // Value = 0x00nn: Op=x, Cmode=100x.
2785 Cmode = 0x8;
2786 Imm = SplatBits;
2787 break;
2788 }
2789 if ((SplatBits & ~0xff00) == 0) {
2790 // Value = 0xnn00: Op=x, Cmode=101x.
2791 Cmode = 0xa;
2792 Imm = SplatBits >> 8;
2793 break;
2794 }
2795 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002796
2797 case 32:
2798 // NEON's 32-bit VMOV supports splat values where:
2799 // * only one byte is nonzero, or
2800 // * the least significant byte is 0xff and the second byte is nonzero, or
2801 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilson1a913ed2010-06-11 21:34:50 +00002802 VT = MVT::i32;
2803 if ((SplatBits & ~0xff) == 0) {
2804 // Value = 0x000000nn: Op=x, Cmode=000x.
2805 Cmode = 0;
2806 Imm = SplatBits;
2807 break;
2808 }
2809 if ((SplatBits & ~0xff00) == 0) {
2810 // Value = 0x0000nn00: Op=x, Cmode=001x.
2811 Cmode = 0x2;
2812 Imm = SplatBits >> 8;
2813 break;
2814 }
2815 if ((SplatBits & ~0xff0000) == 0) {
2816 // Value = 0x00nn0000: Op=x, Cmode=010x.
2817 Cmode = 0x4;
2818 Imm = SplatBits >> 16;
2819 break;
2820 }
2821 if ((SplatBits & ~0xff000000) == 0) {
2822 // Value = 0xnn000000: Op=x, Cmode=011x.
2823 Cmode = 0x6;
2824 Imm = SplatBits >> 24;
2825 break;
2826 }
Bob Wilson5bafff32009-06-22 23:27:02 +00002827
2828 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00002829 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
2830 // Value = 0x0000nnff: Op=x, Cmode=1100.
2831 Cmode = 0xc;
2832 Imm = SplatBits >> 8;
2833 SplatBits |= 0xff;
2834 break;
2835 }
Bob Wilson5bafff32009-06-22 23:27:02 +00002836
2837 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00002838 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
2839 // Value = 0x00nnffff: Op=x, Cmode=1101.
2840 Cmode = 0xd;
2841 Imm = SplatBits >> 16;
2842 SplatBits |= 0xffff;
2843 break;
2844 }
Bob Wilson5bafff32009-06-22 23:27:02 +00002845
2846 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2847 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2848 // VMOV.I32. A (very) minor optimization would be to replicate the value
2849 // and fall through here to test for a valid 64-bit splat. But, then the
2850 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00002851 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002852
2853 case 64: {
2854 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2855 uint64_t BitMask = 0xff;
2856 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002857 unsigned ImmMask = 1;
2858 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00002859 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00002860 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002861 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002862 Imm |= ImmMask;
2863 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002864 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00002865 }
Bob Wilson5bafff32009-06-22 23:27:02 +00002866 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002867 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00002868 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00002869 // Op=1, Cmode=1110.
2870 Op = 1;
2871 Cmode = 0xe;
2872 SplatBits = Val;
2873 VT = MVT::i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00002874 break;
2875 }
2876
Bob Wilson1a913ed2010-06-11 21:34:50 +00002877 default:
2878 llvm_unreachable("unexpected size for EncodeNEONModImm");
2879 return SDValue();
2880 }
2881
2882 if (DoEncode)
2883 return DAG.getTargetConstant((Op << 12) | (Cmode << 8) | Imm, MVT::i32);
2884 return DAG.getTargetConstant(SplatBits, VT);
Bob Wilson5bafff32009-06-22 23:27:02 +00002885}
2886
2887/// getVMOVImm - If this is a build_vector of constants which can be
2888/// formed by using a VMOV instruction of the specified element size,
2889/// return the constant being splatted. The ByteSize field indicates the
2890/// number of bytes of each element [1248].
2891SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2892 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2893 APInt SplatBits, SplatUndef;
2894 unsigned SplatBitSize;
2895 bool HasAnyUndefs;
2896 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2897 HasAnyUndefs, ByteSize * 8))
2898 return SDValue();
2899
2900 if (SplatBitSize > ByteSize * 8)
2901 return SDValue();
2902
2903 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
Bob Wilson1a913ed2010-06-11 21:34:50 +00002904 SplatBitSize, DAG, true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002905}
2906
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002907static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2908 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002909 unsigned NumElts = VT.getVectorNumElements();
2910 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002911 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002912
2913 // If this is a VEXT shuffle, the immediate value is the index of the first
2914 // element. The other shuffle indices must be the successive elements after
2915 // the first one.
2916 unsigned ExpectedElt = Imm;
2917 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002918 // Increment the expected index. If it wraps around, it may still be
2919 // a VEXT but the source vectors must be swapped.
2920 ExpectedElt += 1;
2921 if (ExpectedElt == NumElts * 2) {
2922 ExpectedElt = 0;
2923 ReverseVEXT = true;
2924 }
2925
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002926 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002927 return false;
2928 }
2929
2930 // Adjust the index value if the source operands will be swapped.
2931 if (ReverseVEXT)
2932 Imm -= NumElts;
2933
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002934 return true;
2935}
2936
Bob Wilson8bb9e482009-07-26 00:39:34 +00002937/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2938/// instruction with the specified blocksize. (The order of the elements
2939/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002940static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2941 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002942 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2943 "Only possible block sizes for VREV are: 16, 32, 64");
2944
Bob Wilson8bb9e482009-07-26 00:39:34 +00002945 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00002946 if (EltSz == 64)
2947 return false;
2948
2949 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002950 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002951
2952 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2953 return false;
2954
2955 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002956 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002957 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2958 return false;
2959 }
2960
2961 return true;
2962}
2963
Bob Wilsonc692cb72009-08-21 20:54:19 +00002964static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2965 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002966 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2967 if (EltSz == 64)
2968 return false;
2969
Bob Wilsonc692cb72009-08-21 20:54:19 +00002970 unsigned NumElts = VT.getVectorNumElements();
2971 WhichResult = (M[0] == 0 ? 0 : 1);
2972 for (unsigned i = 0; i < NumElts; i += 2) {
2973 if ((unsigned) M[i] != i + WhichResult ||
2974 (unsigned) M[i+1] != i + NumElts + WhichResult)
2975 return false;
2976 }
2977 return true;
2978}
2979
Bob Wilson324f4f12009-12-03 06:40:55 +00002980/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
2981/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2982/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
2983static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2984 unsigned &WhichResult) {
2985 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2986 if (EltSz == 64)
2987 return false;
2988
2989 unsigned NumElts = VT.getVectorNumElements();
2990 WhichResult = (M[0] == 0 ? 0 : 1);
2991 for (unsigned i = 0; i < NumElts; i += 2) {
2992 if ((unsigned) M[i] != i + WhichResult ||
2993 (unsigned) M[i+1] != i + WhichResult)
2994 return false;
2995 }
2996 return true;
2997}
2998
Bob Wilsonc692cb72009-08-21 20:54:19 +00002999static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3000 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003001 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3002 if (EltSz == 64)
3003 return false;
3004
Bob Wilsonc692cb72009-08-21 20:54:19 +00003005 unsigned NumElts = VT.getVectorNumElements();
3006 WhichResult = (M[0] == 0 ? 0 : 1);
3007 for (unsigned i = 0; i != NumElts; ++i) {
3008 if ((unsigned) M[i] != 2 * i + WhichResult)
3009 return false;
3010 }
3011
3012 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003013 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003014 return false;
3015
3016 return true;
3017}
3018
Bob Wilson324f4f12009-12-03 06:40:55 +00003019/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3020/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3021/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3022static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3023 unsigned &WhichResult) {
3024 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3025 if (EltSz == 64)
3026 return false;
3027
3028 unsigned Half = VT.getVectorNumElements() / 2;
3029 WhichResult = (M[0] == 0 ? 0 : 1);
3030 for (unsigned j = 0; j != 2; ++j) {
3031 unsigned Idx = WhichResult;
3032 for (unsigned i = 0; i != Half; ++i) {
3033 if ((unsigned) M[i + j * Half] != Idx)
3034 return false;
3035 Idx += 2;
3036 }
3037 }
3038
3039 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3040 if (VT.is64BitVector() && EltSz == 32)
3041 return false;
3042
3043 return true;
3044}
3045
Bob Wilsonc692cb72009-08-21 20:54:19 +00003046static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3047 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003048 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3049 if (EltSz == 64)
3050 return false;
3051
Bob Wilsonc692cb72009-08-21 20:54:19 +00003052 unsigned NumElts = VT.getVectorNumElements();
3053 WhichResult = (M[0] == 0 ? 0 : 1);
3054 unsigned Idx = WhichResult * NumElts / 2;
3055 for (unsigned i = 0; i != NumElts; i += 2) {
3056 if ((unsigned) M[i] != Idx ||
3057 (unsigned) M[i+1] != Idx + NumElts)
3058 return false;
3059 Idx += 1;
3060 }
3061
3062 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003063 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003064 return false;
3065
3066 return true;
3067}
3068
Bob Wilson324f4f12009-12-03 06:40:55 +00003069/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3070/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3071/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3072static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3073 unsigned &WhichResult) {
3074 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3075 if (EltSz == 64)
3076 return false;
3077
3078 unsigned NumElts = VT.getVectorNumElements();
3079 WhichResult = (M[0] == 0 ? 0 : 1);
3080 unsigned Idx = WhichResult * NumElts / 2;
3081 for (unsigned i = 0; i != NumElts; i += 2) {
3082 if ((unsigned) M[i] != Idx ||
3083 (unsigned) M[i+1] != Idx)
3084 return false;
3085 Idx += 1;
3086 }
3087
3088 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3089 if (VT.is64BitVector() && EltSz == 32)
3090 return false;
3091
3092 return true;
3093}
3094
3095
Owen Andersone50ed302009-08-10 22:56:29 +00003096static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003097 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00003098 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003099 if (ConstVal->isNullValue())
3100 return getZeroVector(VT, DAG, dl);
3101 if (ConstVal->isAllOnesValue())
3102 return getOnesVector(VT, DAG, dl);
3103
Owen Andersone50ed302009-08-10 22:56:29 +00003104 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00003105 if (VT.is64BitVector()) {
3106 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003107 case 8: CanonicalVT = MVT::v8i8; break;
3108 case 16: CanonicalVT = MVT::v4i16; break;
3109 case 32: CanonicalVT = MVT::v2i32; break;
3110 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003111 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003112 }
3113 } else {
3114 assert(VT.is128BitVector() && "unknown splat vector size");
3115 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003116 case 8: CanonicalVT = MVT::v16i8; break;
3117 case 16: CanonicalVT = MVT::v8i16; break;
3118 case 32: CanonicalVT = MVT::v4i32; break;
3119 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003120 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003121 }
3122 }
3123
3124 // Build a canonical splat for this value.
3125 SmallVector<SDValue, 8> Ops;
3126 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
3127 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
3128 Ops.size());
3129 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
3130}
3131
3132// If this is a case we can't handle, return null and let the default
3133// expansion code take care of it.
3134static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00003135 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003136 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003137 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003138
3139 APInt SplatBits, SplatUndef;
3140 unsigned SplatBitSize;
3141 bool HasAnyUndefs;
3142 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003143 if (SplatBitSize <= 64) {
3144 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
Bob Wilson1a913ed2010-06-11 21:34:50 +00003145 SplatUndef.getZExtValue(), SplatBitSize, DAG,
3146 false);
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003147 if (Val.getNode())
3148 return BuildSplat(Val, VT, DAG, dl);
3149 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003150 }
3151
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003152 // Scan through the operands to see if only one value is used.
3153 unsigned NumElts = VT.getVectorNumElements();
3154 bool isOnlyLowElement = true;
3155 bool usesOnlyOneValue = true;
3156 bool isConstant = true;
3157 SDValue Value;
3158 for (unsigned i = 0; i < NumElts; ++i) {
3159 SDValue V = Op.getOperand(i);
3160 if (V.getOpcode() == ISD::UNDEF)
3161 continue;
3162 if (i > 0)
3163 isOnlyLowElement = false;
3164 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3165 isConstant = false;
3166
3167 if (!Value.getNode())
3168 Value = V;
3169 else if (V != Value)
3170 usesOnlyOneValue = false;
3171 }
3172
3173 if (!Value.getNode())
3174 return DAG.getUNDEF(VT);
3175
3176 if (isOnlyLowElement)
3177 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3178
3179 // If all elements are constants, fall back to the default expansion, which
3180 // will generate a load from the constant pool.
3181 if (isConstant)
3182 return SDValue();
3183
3184 // Use VDUP for non-constant splats.
Bob Wilson069e4342010-05-23 05:42:31 +00003185 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3186 if (usesOnlyOneValue && EltSize <= 32)
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003187 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3188
3189 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003190 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3191 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003192 if (EltSize >= 32) {
3193 // Do the expansion with floating-point types, since that is what the VFP
3194 // registers are defined to use, and since i64 is not legal.
3195 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3196 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003197 SmallVector<SDValue, 8> Ops;
3198 for (unsigned i = 0; i < NumElts; ++i)
3199 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, dl, EltVT, Op.getOperand(i)));
3200 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003201 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003202 }
3203
3204 return SDValue();
3205}
3206
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003207/// isShuffleMaskLegal - Targets can use this to indicate that they only
3208/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3209/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3210/// are assumed to be legal.
3211bool
3212ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
3213 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003214 if (VT.getVectorNumElements() == 4 &&
3215 (VT.is128BitVector() || VT.is64BitVector())) {
3216 unsigned PFIndexes[4];
3217 for (unsigned i = 0; i != 4; ++i) {
3218 if (M[i] < 0)
3219 PFIndexes[i] = 8;
3220 else
3221 PFIndexes[i] = M[i];
3222 }
3223
3224 // Compute the index in the perfect shuffle table.
3225 unsigned PFTableIndex =
3226 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3227 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3228 unsigned Cost = (PFEntry >> 30);
3229
3230 if (Cost <= 4)
3231 return true;
3232 }
3233
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003234 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00003235 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003236
Bob Wilson53dd2452010-06-07 23:53:38 +00003237 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3238 return (EltSize >= 32 ||
3239 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003240 isVREVMask(M, VT, 64) ||
3241 isVREVMask(M, VT, 32) ||
3242 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00003243 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
3244 isVTRNMask(M, VT, WhichResult) ||
3245 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00003246 isVZIPMask(M, VT, WhichResult) ||
3247 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
3248 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
3249 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003250}
3251
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003252/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
3253/// the specified operations to build the shuffle.
3254static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
3255 SDValue RHS, SelectionDAG &DAG,
3256 DebugLoc dl) {
3257 unsigned OpNum = (PFEntry >> 26) & 0x0F;
3258 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
3259 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
3260
3261 enum {
3262 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
3263 OP_VREV,
3264 OP_VDUP0,
3265 OP_VDUP1,
3266 OP_VDUP2,
3267 OP_VDUP3,
3268 OP_VEXT1,
3269 OP_VEXT2,
3270 OP_VEXT3,
3271 OP_VUZPL, // VUZP, left result
3272 OP_VUZPR, // VUZP, right result
3273 OP_VZIPL, // VZIP, left result
3274 OP_VZIPR, // VZIP, right result
3275 OP_VTRNL, // VTRN, left result
3276 OP_VTRNR // VTRN, right result
3277 };
3278
3279 if (OpNum == OP_COPY) {
3280 if (LHSID == (1*9+2)*9+3) return LHS;
3281 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
3282 return RHS;
3283 }
3284
3285 SDValue OpLHS, OpRHS;
3286 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
3287 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
3288 EVT VT = OpLHS.getValueType();
3289
3290 switch (OpNum) {
3291 default: llvm_unreachable("Unknown shuffle opcode!");
3292 case OP_VREV:
3293 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
3294 case OP_VDUP0:
3295 case OP_VDUP1:
3296 case OP_VDUP2:
3297 case OP_VDUP3:
3298 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003299 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003300 case OP_VEXT1:
3301 case OP_VEXT2:
3302 case OP_VEXT3:
3303 return DAG.getNode(ARMISD::VEXT, dl, VT,
3304 OpLHS, OpRHS,
3305 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
3306 case OP_VUZPL:
3307 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003308 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003309 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
3310 case OP_VZIPL:
3311 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003312 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003313 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
3314 case OP_VTRNL:
3315 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003316 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3317 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003318 }
3319}
3320
Bob Wilson5bafff32009-06-22 23:27:02 +00003321static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003322 SDValue V1 = Op.getOperand(0);
3323 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00003324 DebugLoc dl = Op.getDebugLoc();
3325 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003326 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003327 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00003328
Bob Wilson28865062009-08-13 02:13:04 +00003329 // Convert shuffles that are directly supported on NEON to target-specific
3330 // DAG nodes, instead of keeping them as shuffles and matching them again
3331 // during code selection. This is more efficient and avoids the possibility
3332 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00003333 // FIXME: floating-point vectors should be canonicalized to integer vectors
3334 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003335 SVN->getMask(ShuffleMask);
3336
Bob Wilson53dd2452010-06-07 23:53:38 +00003337 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3338 if (EltSize <= 32) {
3339 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
3340 int Lane = SVN->getSplatIndex();
3341 // If this is undef splat, generate it via "just" vdup, if possible.
3342 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00003343
Bob Wilson53dd2452010-06-07 23:53:38 +00003344 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3345 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
3346 }
3347 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
3348 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00003349 }
Bob Wilson53dd2452010-06-07 23:53:38 +00003350
3351 bool ReverseVEXT;
3352 unsigned Imm;
3353 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
3354 if (ReverseVEXT)
3355 std::swap(V1, V2);
3356 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
3357 DAG.getConstant(Imm, MVT::i32));
3358 }
3359
3360 if (isVREVMask(ShuffleMask, VT, 64))
3361 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
3362 if (isVREVMask(ShuffleMask, VT, 32))
3363 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
3364 if (isVREVMask(ShuffleMask, VT, 16))
3365 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
3366
3367 // Check for Neon shuffles that modify both input vectors in place.
3368 // If both results are used, i.e., if there are two shuffles with the same
3369 // source operands and with masks corresponding to both results of one of
3370 // these operations, DAG memoization will ensure that a single node is
3371 // used for both shuffles.
3372 unsigned WhichResult;
3373 if (isVTRNMask(ShuffleMask, VT, WhichResult))
3374 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3375 V1, V2).getValue(WhichResult);
3376 if (isVUZPMask(ShuffleMask, VT, WhichResult))
3377 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3378 V1, V2).getValue(WhichResult);
3379 if (isVZIPMask(ShuffleMask, VT, WhichResult))
3380 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3381 V1, V2).getValue(WhichResult);
3382
3383 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
3384 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3385 V1, V1).getValue(WhichResult);
3386 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3387 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3388 V1, V1).getValue(WhichResult);
3389 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3390 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3391 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00003392 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003393
Bob Wilsonc692cb72009-08-21 20:54:19 +00003394 // If the shuffle is not directly supported and it has 4 elements, use
3395 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003396 unsigned NumElts = VT.getVectorNumElements();
3397 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003398 unsigned PFIndexes[4];
3399 for (unsigned i = 0; i != 4; ++i) {
3400 if (ShuffleMask[i] < 0)
3401 PFIndexes[i] = 8;
3402 else
3403 PFIndexes[i] = ShuffleMask[i];
3404 }
3405
3406 // Compute the index in the perfect shuffle table.
3407 unsigned PFTableIndex =
3408 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003409 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3410 unsigned Cost = (PFEntry >> 30);
3411
3412 if (Cost <= 4)
3413 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
3414 }
Bob Wilsond8e17572009-08-12 22:31:50 +00003415
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003416 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003417 if (EltSize >= 32) {
3418 // Do the expansion with floating-point types, since that is what the VFP
3419 // registers are defined to use, and since i64 is not legal.
3420 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3421 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
3422 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V1);
3423 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003424 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003425 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00003426 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003427 Ops.push_back(DAG.getUNDEF(EltVT));
3428 else
3429 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
3430 ShuffleMask[i] < (int)NumElts ? V1 : V2,
3431 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
3432 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00003433 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003434 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Bob Wilson63b88452010-05-20 18:39:53 +00003435 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
3436 }
3437
Bob Wilson22cac0d2009-08-14 05:16:33 +00003438 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003439}
3440
Bob Wilson5bafff32009-06-22 23:27:02 +00003441static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003442 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003443 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003444 SDValue Vec = Op.getOperand(0);
3445 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00003446 assert(VT == MVT::i32 &&
3447 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
3448 "unexpected type for custom-lowering vector extract");
3449 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00003450}
3451
Bob Wilsona6d65862009-08-03 20:36:38 +00003452static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3453 // The only time a CONCAT_VECTORS operation can have legal types is when
3454 // two 64-bit vectors are concatenated to a 128-bit vector.
3455 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
3456 "unexpected CONCAT_VECTORS");
3457 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00003458 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00003459 SDValue Op0 = Op.getOperand(0);
3460 SDValue Op1 = Op.getOperand(1);
3461 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003462 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3463 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00003464 DAG.getIntPtrConstant(0));
3465 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003466 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3467 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00003468 DAG.getIntPtrConstant(1));
3469 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003470}
3471
Dan Gohmand858e902010-04-17 15:26:15 +00003472SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003473 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003474 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00003475 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00003476 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003477 case ISD::GlobalAddress:
3478 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
3479 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00003480 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00003481 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
3482 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003483 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00003484 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00003485 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00003486 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00003487 case ISD::SINT_TO_FP:
3488 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
3489 case ISD::FP_TO_SINT:
3490 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003491 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003492 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003493 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003494 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00003495 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00003496 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00003497 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
3498 Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00003499 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003500 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00003501 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00003502 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00003503 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003504 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00003505 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00003506 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003507 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
3508 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3509 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003510 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00003511 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003512 }
Dan Gohman475871a2008-07-27 21:46:04 +00003513 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003514}
3515
Duncan Sands1607f052008-12-01 11:39:25 +00003516/// ReplaceNodeResults - Replace the results of node with an illegal result
3517/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00003518void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
3519 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00003520 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00003521 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00003522 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00003523 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003524 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003525 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003526 case ISD::BIT_CONVERT:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003527 Res = ExpandBIT_CONVERT(N, DAG);
3528 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00003529 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003530 case ISD::SRA:
3531 Res = LowerShift(N, DAG, Subtarget);
3532 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003533 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00003534 if (Res.getNode())
3535 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00003536}
Chris Lattner27a6c732007-11-24 07:07:01 +00003537
Evan Chenga8e29892007-01-19 07:51:42 +00003538//===----------------------------------------------------------------------===//
3539// ARM Scheduler Hooks
3540//===----------------------------------------------------------------------===//
3541
3542MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003543ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
3544 MachineBasicBlock *BB,
3545 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00003546 unsigned dest = MI->getOperand(0).getReg();
3547 unsigned ptr = MI->getOperand(1).getReg();
3548 unsigned oldval = MI->getOperand(2).getReg();
3549 unsigned newval = MI->getOperand(3).getReg();
3550 unsigned scratch = BB->getParent()->getRegInfo()
3551 .createVirtualRegister(ARM::GPRRegisterClass);
3552 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3553 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003554 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00003555
3556 unsigned ldrOpc, strOpc;
3557 switch (Size) {
3558 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003559 case 1:
3560 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
3561 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
3562 break;
3563 case 2:
3564 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3565 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3566 break;
3567 case 4:
3568 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3569 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3570 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00003571 }
3572
3573 MachineFunction *MF = BB->getParent();
3574 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3575 MachineFunction::iterator It = BB;
3576 ++It; // insert the new blocks after the current block
3577
3578 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3579 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3580 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3581 MF->insert(It, loop1MBB);
3582 MF->insert(It, loop2MBB);
3583 MF->insert(It, exitMBB);
3584 exitMBB->transferSuccessors(BB);
3585
3586 // thisMBB:
3587 // ...
3588 // fallthrough --> loop1MBB
3589 BB->addSuccessor(loop1MBB);
3590
3591 // loop1MBB:
3592 // ldrex dest, [ptr]
3593 // cmp dest, oldval
3594 // bne exitMBB
3595 BB = loop1MBB;
3596 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003597 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003598 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003599 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3600 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003601 BB->addSuccessor(loop2MBB);
3602 BB->addSuccessor(exitMBB);
3603
3604 // loop2MBB:
3605 // strex scratch, newval, [ptr]
3606 // cmp scratch, #0
3607 // bne loop1MBB
3608 BB = loop2MBB;
3609 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
3610 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003611 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003612 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003613 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3614 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003615 BB->addSuccessor(loop1MBB);
3616 BB->addSuccessor(exitMBB);
3617
3618 // exitMBB:
3619 // ...
3620 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00003621
3622 MF->DeleteMachineInstr(MI); // The instruction is gone now.
3623
Jim Grosbach5278eb82009-12-11 01:42:04 +00003624 return BB;
3625}
3626
3627MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003628ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
3629 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00003630 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
3631 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3632
3633 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003634 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003635 MachineFunction::iterator It = BB;
3636 ++It;
3637
3638 unsigned dest = MI->getOperand(0).getReg();
3639 unsigned ptr = MI->getOperand(1).getReg();
3640 unsigned incr = MI->getOperand(2).getReg();
3641 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00003642
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003643 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003644 unsigned ldrOpc, strOpc;
3645 switch (Size) {
3646 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003647 case 1:
3648 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00003649 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003650 break;
3651 case 2:
3652 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3653 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3654 break;
3655 case 4:
3656 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3657 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3658 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00003659 }
3660
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003661 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3662 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3663 MF->insert(It, loopMBB);
3664 MF->insert(It, exitMBB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003665 exitMBB->transferSuccessors(BB);
3666
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003667 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003668 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3669 unsigned scratch2 = (!BinOpcode) ? incr :
3670 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3671
3672 // thisMBB:
3673 // ...
3674 // fallthrough --> loopMBB
3675 BB->addSuccessor(loopMBB);
3676
3677 // loopMBB:
3678 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003679 // <binop> scratch2, dest, incr
3680 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00003681 // cmp scratch, #0
3682 // bne- loopMBB
3683 // fallthrough --> exitMBB
3684 BB = loopMBB;
3685 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00003686 if (BinOpcode) {
3687 // operand order needs to go the other way for NAND
3688 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
3689 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3690 addReg(incr).addReg(dest)).addReg(0);
3691 else
3692 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3693 addReg(dest).addReg(incr)).addReg(0);
3694 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00003695
3696 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
3697 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003698 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00003699 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003700 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3701 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003702
3703 BB->addSuccessor(loopMBB);
3704 BB->addSuccessor(exitMBB);
3705
3706 // exitMBB:
3707 // ...
3708 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00003709
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003710 MF->DeleteMachineInstr(MI); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00003711
Jim Grosbachc3c23542009-12-14 04:22:04 +00003712 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00003713}
3714
3715MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00003716ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003717 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003718 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00003719 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003720 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00003721 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00003722 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00003723 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00003724 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00003725
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003726 case ARM::ATOMIC_LOAD_ADD_I8:
3727 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3728 case ARM::ATOMIC_LOAD_ADD_I16:
3729 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3730 case ARM::ATOMIC_LOAD_ADD_I32:
3731 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003732
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003733 case ARM::ATOMIC_LOAD_AND_I8:
3734 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3735 case ARM::ATOMIC_LOAD_AND_I16:
3736 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3737 case ARM::ATOMIC_LOAD_AND_I32:
3738 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003739
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003740 case ARM::ATOMIC_LOAD_OR_I8:
3741 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3742 case ARM::ATOMIC_LOAD_OR_I16:
3743 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3744 case ARM::ATOMIC_LOAD_OR_I32:
3745 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003746
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003747 case ARM::ATOMIC_LOAD_XOR_I8:
3748 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3749 case ARM::ATOMIC_LOAD_XOR_I16:
3750 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3751 case ARM::ATOMIC_LOAD_XOR_I32:
3752 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003753
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003754 case ARM::ATOMIC_LOAD_NAND_I8:
3755 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3756 case ARM::ATOMIC_LOAD_NAND_I16:
3757 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3758 case ARM::ATOMIC_LOAD_NAND_I32:
3759 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003760
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003761 case ARM::ATOMIC_LOAD_SUB_I8:
3762 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3763 case ARM::ATOMIC_LOAD_SUB_I16:
3764 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3765 case ARM::ATOMIC_LOAD_SUB_I32:
3766 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003767
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003768 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
3769 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
3770 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00003771
3772 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
3773 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
3774 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003775
Evan Cheng007ea272009-08-12 05:17:19 +00003776 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00003777 // To "insert" a SELECT_CC instruction, we actually have to insert the
3778 // diamond control-flow pattern. The incoming instruction knows the
3779 // destination vreg to set, the condition code register to branch on, the
3780 // true/false values to select between, and a branch opcode to use.
3781 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003782 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00003783 ++It;
3784
3785 // thisMBB:
3786 // ...
3787 // TrueVal = ...
3788 // cmpTY ccX, r1, r2
3789 // bCC copy1MBB
3790 // fallthrough --> copy0MBB
3791 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003792 MachineFunction *F = BB->getParent();
3793 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3794 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00003795 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00003796 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003797 F->insert(It, copy0MBB);
3798 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00003799 // Update machine-CFG edges by first adding all successors of the current
3800 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00003801 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003802 E = BB->succ_end(); I != E; ++I)
Evan Chengce319102009-09-19 09:51:03 +00003803 sinkMBB->addSuccessor(*I);
Evan Chenga8e29892007-01-19 07:51:42 +00003804 // Next, remove all successors of the current block, and add the true
3805 // and fallthrough blocks as its successors.
Evan Chengce319102009-09-19 09:51:03 +00003806 while (!BB->succ_empty())
Evan Chenga8e29892007-01-19 07:51:42 +00003807 BB->removeSuccessor(BB->succ_begin());
3808 BB->addSuccessor(copy0MBB);
3809 BB->addSuccessor(sinkMBB);
3810
3811 // copy0MBB:
3812 // %FalseValue = ...
3813 // # fallthrough to sinkMBB
3814 BB = copy0MBB;
3815
3816 // Update machine-CFG edges
3817 BB->addSuccessor(sinkMBB);
3818
3819 // sinkMBB:
3820 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3821 // ...
3822 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00003823 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00003824 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
3825 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
3826
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003827 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00003828 return BB;
3829 }
Evan Cheng86198642009-08-07 00:34:42 +00003830
3831 case ARM::tANDsp:
3832 case ARM::tADDspr_:
3833 case ARM::tSUBspi_:
3834 case ARM::t2SUBrSPi_:
3835 case ARM::t2SUBrSPi12_:
3836 case ARM::t2SUBrSPs_: {
3837 MachineFunction *MF = BB->getParent();
3838 unsigned DstReg = MI->getOperand(0).getReg();
3839 unsigned SrcReg = MI->getOperand(1).getReg();
3840 bool DstIsDead = MI->getOperand(0).isDead();
3841 bool SrcIsKill = MI->getOperand(1).isKill();
3842
3843 if (SrcReg != ARM::SP) {
3844 // Copy the source to SP from virtual register.
3845 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
3846 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3847 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
3848 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
3849 .addReg(SrcReg, getKillRegState(SrcIsKill));
3850 }
3851
3852 unsigned OpOpc = 0;
3853 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
3854 switch (MI->getOpcode()) {
3855 default:
3856 llvm_unreachable("Unexpected pseudo instruction!");
3857 case ARM::tANDsp:
3858 OpOpc = ARM::tAND;
3859 NeedPred = true;
3860 break;
3861 case ARM::tADDspr_:
3862 OpOpc = ARM::tADDspr;
3863 break;
3864 case ARM::tSUBspi_:
3865 OpOpc = ARM::tSUBspi;
3866 break;
3867 case ARM::t2SUBrSPi_:
3868 OpOpc = ARM::t2SUBrSPi;
3869 NeedPred = true; NeedCC = true;
3870 break;
3871 case ARM::t2SUBrSPi12_:
3872 OpOpc = ARM::t2SUBrSPi12;
3873 NeedPred = true;
3874 break;
3875 case ARM::t2SUBrSPs_:
3876 OpOpc = ARM::t2SUBrSPs;
3877 NeedPred = true; NeedCC = true; NeedOp3 = true;
3878 break;
3879 }
3880 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
3881 if (OpOpc == ARM::tAND)
3882 AddDefaultT1CC(MIB);
3883 MIB.addReg(ARM::SP);
3884 MIB.addOperand(MI->getOperand(2));
3885 if (NeedOp3)
3886 MIB.addOperand(MI->getOperand(3));
3887 if (NeedPred)
3888 AddDefaultPred(MIB);
3889 if (NeedCC)
3890 AddDefaultCC(MIB);
3891
3892 // Copy the result from SP to virtual register.
3893 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
3894 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3895 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
3896 BuildMI(BB, dl, TII->get(CopyOpc))
3897 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
3898 .addReg(ARM::SP);
3899 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3900 return BB;
3901 }
Evan Chenga8e29892007-01-19 07:51:42 +00003902 }
3903}
3904
3905//===----------------------------------------------------------------------===//
3906// ARM Optimization Hooks
3907//===----------------------------------------------------------------------===//
3908
Chris Lattnerd1980a52009-03-12 06:52:53 +00003909static
3910SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3911 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00003912 SelectionDAG &DAG = DCI.DAG;
3913 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003914 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00003915 unsigned Opc = N->getOpcode();
3916 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3917 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3918 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3919 ISD::CondCode CC = ISD::SETCC_INVALID;
3920
3921 if (isSlctCC) {
3922 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
3923 } else {
3924 SDValue CCOp = Slct.getOperand(0);
3925 if (CCOp.getOpcode() == ISD::SETCC)
3926 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
3927 }
3928
3929 bool DoXform = false;
3930 bool InvCC = false;
3931 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
3932 "Bad input!");
3933
3934 if (LHS.getOpcode() == ISD::Constant &&
3935 cast<ConstantSDNode>(LHS)->isNullValue()) {
3936 DoXform = true;
3937 } else if (CC != ISD::SETCC_INVALID &&
3938 RHS.getOpcode() == ISD::Constant &&
3939 cast<ConstantSDNode>(RHS)->isNullValue()) {
3940 std::swap(LHS, RHS);
3941 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003942 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00003943 Op0.getOperand(0).getValueType();
3944 bool isInt = OpVT.isInteger();
3945 CC = ISD::getSetCCInverse(CC, isInt);
3946
3947 if (!TLI.isCondCodeLegal(CC, OpVT))
3948 return SDValue(); // Inverse operator isn't legal.
3949
3950 DoXform = true;
3951 InvCC = true;
3952 }
3953
3954 if (DoXform) {
3955 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3956 if (isSlctCC)
3957 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3958 Slct.getOperand(0), Slct.getOperand(1), CC);
3959 SDValue CCOp = Slct.getOperand(0);
3960 if (InvCC)
3961 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3962 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3963 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3964 CCOp, OtherOp, Result);
3965 }
3966 return SDValue();
3967}
3968
3969/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3970static SDValue PerformADDCombine(SDNode *N,
3971 TargetLowering::DAGCombinerInfo &DCI) {
3972 // added by evan in r37685 with no testcase.
3973 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003974
Chris Lattnerd1980a52009-03-12 06:52:53 +00003975 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3976 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3977 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3978 if (Result.getNode()) return Result;
3979 }
3980 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3981 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3982 if (Result.getNode()) return Result;
3983 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003984
Chris Lattnerd1980a52009-03-12 06:52:53 +00003985 return SDValue();
3986}
3987
3988/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3989static SDValue PerformSUBCombine(SDNode *N,
3990 TargetLowering::DAGCombinerInfo &DCI) {
3991 // added by evan in r37685 with no testcase.
3992 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003993
Chris Lattnerd1980a52009-03-12 06:52:53 +00003994 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3995 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3996 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3997 if (Result.getNode()) return Result;
3998 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003999
Chris Lattnerd1980a52009-03-12 06:52:53 +00004000 return SDValue();
4001}
4002
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004003static SDValue PerformMULCombine(SDNode *N,
4004 TargetLowering::DAGCombinerInfo &DCI,
4005 const ARMSubtarget *Subtarget) {
4006 SelectionDAG &DAG = DCI.DAG;
4007
4008 if (Subtarget->isThumb1Only())
4009 return SDValue();
4010
4011 if (DAG.getMachineFunction().
4012 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
4013 return SDValue();
4014
4015 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
4016 return SDValue();
4017
4018 EVT VT = N->getValueType(0);
4019 if (VT != MVT::i32)
4020 return SDValue();
4021
4022 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
4023 if (!C)
4024 return SDValue();
4025
4026 uint64_t MulAmt = C->getZExtValue();
4027 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
4028 ShiftAmt = ShiftAmt & (32 - 1);
4029 SDValue V = N->getOperand(0);
4030 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004031
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004032 SDValue Res;
4033 MulAmt >>= ShiftAmt;
4034 if (isPowerOf2_32(MulAmt - 1)) {
4035 // (mul x, 2^N + 1) => (add (shl x, N), x)
4036 Res = DAG.getNode(ISD::ADD, DL, VT,
4037 V, DAG.getNode(ISD::SHL, DL, VT,
4038 V, DAG.getConstant(Log2_32(MulAmt-1),
4039 MVT::i32)));
4040 } else if (isPowerOf2_32(MulAmt + 1)) {
4041 // (mul x, 2^N - 1) => (sub (shl x, N), x)
4042 Res = DAG.getNode(ISD::SUB, DL, VT,
4043 DAG.getNode(ISD::SHL, DL, VT,
4044 V, DAG.getConstant(Log2_32(MulAmt+1),
4045 MVT::i32)),
4046 V);
4047 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004048 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004049
4050 if (ShiftAmt != 0)
4051 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
4052 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004053
4054 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004055 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004056 return SDValue();
4057}
4058
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +00004059/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
4060/// ARMISD::VMOVRRD.
Jim Grosbache5165492009-11-09 00:11:35 +00004061static SDValue PerformVMOVRRDCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004062 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004063 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00004064 SDValue InDouble = N->getOperand(0);
Jim Grosbache5165492009-11-09 00:11:35 +00004065 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004066 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004067 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004068}
4069
Bob Wilson5bafff32009-06-22 23:27:02 +00004070/// getVShiftImm - Check if this is a valid build_vector for the immediate
4071/// operand of a vector shift operation, where all the elements of the
4072/// build_vector must have the same constant integer value.
4073static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
4074 // Ignore bit_converts.
4075 while (Op.getOpcode() == ISD::BIT_CONVERT)
4076 Op = Op.getOperand(0);
4077 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
4078 APInt SplatBits, SplatUndef;
4079 unsigned SplatBitSize;
4080 bool HasAnyUndefs;
4081 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
4082 HasAnyUndefs, ElementBits) ||
4083 SplatBitSize > ElementBits)
4084 return false;
4085 Cnt = SplatBits.getSExtValue();
4086 return true;
4087}
4088
4089/// isVShiftLImm - Check if this is a valid build_vector for the immediate
4090/// operand of a vector shift left operation. That value must be in the range:
4091/// 0 <= Value < ElementBits for a left shift; or
4092/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00004093static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00004094 assert(VT.isVector() && "vector shift count is not a vector type");
4095 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
4096 if (! getVShiftImm(Op, ElementBits, Cnt))
4097 return false;
4098 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
4099}
4100
4101/// isVShiftRImm - Check if this is a valid build_vector for the immediate
4102/// operand of a vector shift right operation. For a shift opcode, the value
4103/// is positive, but for an intrinsic the value count must be negative. The
4104/// absolute value must be in the range:
4105/// 1 <= |Value| <= ElementBits for a right shift; or
4106/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00004107static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00004108 int64_t &Cnt) {
4109 assert(VT.isVector() && "vector shift count is not a vector type");
4110 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
4111 if (! getVShiftImm(Op, ElementBits, Cnt))
4112 return false;
4113 if (isIntrinsic)
4114 Cnt = -Cnt;
4115 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
4116}
4117
4118/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
4119static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
4120 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
4121 switch (IntNo) {
4122 default:
4123 // Don't do anything for most intrinsics.
4124 break;
4125
4126 // Vector shifts: check for immediate versions and lower them.
4127 // Note: This is done during DAG combining instead of DAG legalizing because
4128 // the build_vectors for 64-bit vector element shift counts are generally
4129 // not legal, and it is hard to see their values after they get legalized to
4130 // loads from a constant pool.
4131 case Intrinsic::arm_neon_vshifts:
4132 case Intrinsic::arm_neon_vshiftu:
4133 case Intrinsic::arm_neon_vshiftls:
4134 case Intrinsic::arm_neon_vshiftlu:
4135 case Intrinsic::arm_neon_vshiftn:
4136 case Intrinsic::arm_neon_vrshifts:
4137 case Intrinsic::arm_neon_vrshiftu:
4138 case Intrinsic::arm_neon_vrshiftn:
4139 case Intrinsic::arm_neon_vqshifts:
4140 case Intrinsic::arm_neon_vqshiftu:
4141 case Intrinsic::arm_neon_vqshiftsu:
4142 case Intrinsic::arm_neon_vqshiftns:
4143 case Intrinsic::arm_neon_vqshiftnu:
4144 case Intrinsic::arm_neon_vqshiftnsu:
4145 case Intrinsic::arm_neon_vqrshiftns:
4146 case Intrinsic::arm_neon_vqrshiftnu:
4147 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00004148 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004149 int64_t Cnt;
4150 unsigned VShiftOpc = 0;
4151
4152 switch (IntNo) {
4153 case Intrinsic::arm_neon_vshifts:
4154 case Intrinsic::arm_neon_vshiftu:
4155 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
4156 VShiftOpc = ARMISD::VSHL;
4157 break;
4158 }
4159 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
4160 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
4161 ARMISD::VSHRs : ARMISD::VSHRu);
4162 break;
4163 }
4164 return SDValue();
4165
4166 case Intrinsic::arm_neon_vshiftls:
4167 case Intrinsic::arm_neon_vshiftlu:
4168 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
4169 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00004170 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004171
4172 case Intrinsic::arm_neon_vrshifts:
4173 case Intrinsic::arm_neon_vrshiftu:
4174 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
4175 break;
4176 return SDValue();
4177
4178 case Intrinsic::arm_neon_vqshifts:
4179 case Intrinsic::arm_neon_vqshiftu:
4180 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
4181 break;
4182 return SDValue();
4183
4184 case Intrinsic::arm_neon_vqshiftsu:
4185 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
4186 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00004187 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004188
4189 case Intrinsic::arm_neon_vshiftn:
4190 case Intrinsic::arm_neon_vrshiftn:
4191 case Intrinsic::arm_neon_vqshiftns:
4192 case Intrinsic::arm_neon_vqshiftnu:
4193 case Intrinsic::arm_neon_vqshiftnsu:
4194 case Intrinsic::arm_neon_vqrshiftns:
4195 case Intrinsic::arm_neon_vqrshiftnu:
4196 case Intrinsic::arm_neon_vqrshiftnsu:
4197 // Narrowing shifts require an immediate right shift.
4198 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
4199 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00004200 llvm_unreachable("invalid shift count for narrowing vector shift "
4201 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004202
4203 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004204 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00004205 }
4206
4207 switch (IntNo) {
4208 case Intrinsic::arm_neon_vshifts:
4209 case Intrinsic::arm_neon_vshiftu:
4210 // Opcode already set above.
4211 break;
4212 case Intrinsic::arm_neon_vshiftls:
4213 case Intrinsic::arm_neon_vshiftlu:
4214 if (Cnt == VT.getVectorElementType().getSizeInBits())
4215 VShiftOpc = ARMISD::VSHLLi;
4216 else
4217 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
4218 ARMISD::VSHLLs : ARMISD::VSHLLu);
4219 break;
4220 case Intrinsic::arm_neon_vshiftn:
4221 VShiftOpc = ARMISD::VSHRN; break;
4222 case Intrinsic::arm_neon_vrshifts:
4223 VShiftOpc = ARMISD::VRSHRs; break;
4224 case Intrinsic::arm_neon_vrshiftu:
4225 VShiftOpc = ARMISD::VRSHRu; break;
4226 case Intrinsic::arm_neon_vrshiftn:
4227 VShiftOpc = ARMISD::VRSHRN; break;
4228 case Intrinsic::arm_neon_vqshifts:
4229 VShiftOpc = ARMISD::VQSHLs; break;
4230 case Intrinsic::arm_neon_vqshiftu:
4231 VShiftOpc = ARMISD::VQSHLu; break;
4232 case Intrinsic::arm_neon_vqshiftsu:
4233 VShiftOpc = ARMISD::VQSHLsu; break;
4234 case Intrinsic::arm_neon_vqshiftns:
4235 VShiftOpc = ARMISD::VQSHRNs; break;
4236 case Intrinsic::arm_neon_vqshiftnu:
4237 VShiftOpc = ARMISD::VQSHRNu; break;
4238 case Intrinsic::arm_neon_vqshiftnsu:
4239 VShiftOpc = ARMISD::VQSHRNsu; break;
4240 case Intrinsic::arm_neon_vqrshiftns:
4241 VShiftOpc = ARMISD::VQRSHRNs; break;
4242 case Intrinsic::arm_neon_vqrshiftnu:
4243 VShiftOpc = ARMISD::VQRSHRNu; break;
4244 case Intrinsic::arm_neon_vqrshiftnsu:
4245 VShiftOpc = ARMISD::VQRSHRNsu; break;
4246 }
4247
4248 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004249 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004250 }
4251
4252 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00004253 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004254 int64_t Cnt;
4255 unsigned VShiftOpc = 0;
4256
4257 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
4258 VShiftOpc = ARMISD::VSLI;
4259 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
4260 VShiftOpc = ARMISD::VSRI;
4261 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00004262 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004263 }
4264
4265 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
4266 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00004267 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004268 }
4269
4270 case Intrinsic::arm_neon_vqrshifts:
4271 case Intrinsic::arm_neon_vqrshiftu:
4272 // No immediate versions of these to check for.
4273 break;
4274 }
4275
4276 return SDValue();
4277}
4278
4279/// PerformShiftCombine - Checks for immediate versions of vector shifts and
4280/// lowers them. As with the vector shift intrinsics, this is done during DAG
4281/// combining instead of DAG legalizing because the build_vectors for 64-bit
4282/// vector element shift counts are generally not legal, and it is hard to see
4283/// their values after they get legalized to loads from a constant pool.
4284static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
4285 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00004286 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00004287
4288 // Nothing to be done for scalar shifts.
4289 if (! VT.isVector())
4290 return SDValue();
4291
4292 assert(ST->hasNEON() && "unexpected vector shift");
4293 int64_t Cnt;
4294
4295 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004296 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00004297
4298 case ISD::SHL:
4299 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
4300 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004301 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004302 break;
4303
4304 case ISD::SRA:
4305 case ISD::SRL:
4306 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
4307 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
4308 ARMISD::VSHRs : ARMISD::VSHRu);
4309 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004310 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004311 }
4312 }
4313 return SDValue();
4314}
4315
4316/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
4317/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
4318static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
4319 const ARMSubtarget *ST) {
4320 SDValue N0 = N->getOperand(0);
4321
4322 // Check for sign- and zero-extensions of vector extract operations of 8-
4323 // and 16-bit vector elements. NEON supports these directly. They are
4324 // handled during DAG combining because type legalization will promote them
4325 // to 32-bit types and it is messy to recognize the operations after that.
4326 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
4327 SDValue Vec = N0.getOperand(0);
4328 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004329 EVT VT = N->getValueType(0);
4330 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004331 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4332
Owen Anderson825b72b2009-08-11 20:47:22 +00004333 if (VT == MVT::i32 &&
4334 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00004335 TLI.isTypeLegal(Vec.getValueType())) {
4336
4337 unsigned Opc = 0;
4338 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004339 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00004340 case ISD::SIGN_EXTEND:
4341 Opc = ARMISD::VGETLANEs;
4342 break;
4343 case ISD::ZERO_EXTEND:
4344 case ISD::ANY_EXTEND:
4345 Opc = ARMISD::VGETLANEu;
4346 break;
4347 }
4348 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
4349 }
4350 }
4351
4352 return SDValue();
4353}
4354
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004355/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
4356/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
4357static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
4358 const ARMSubtarget *ST) {
4359 // If the target supports NEON, try to use vmax/vmin instructions for f32
4360 // selects like "x < y ? x : y". Unless the FiniteOnlyFPMath option is set,
4361 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
4362 // a NaN; only do the transformation when it matches that behavior.
4363
4364 // For now only do this when using NEON for FP operations; if using VFP, it
4365 // is not obvious that the benefit outweighs the cost of switching to the
4366 // NEON pipeline.
4367 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
4368 N->getValueType(0) != MVT::f32)
4369 return SDValue();
4370
4371 SDValue CondLHS = N->getOperand(0);
4372 SDValue CondRHS = N->getOperand(1);
4373 SDValue LHS = N->getOperand(2);
4374 SDValue RHS = N->getOperand(3);
4375 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
4376
4377 unsigned Opcode = 0;
4378 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00004379 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004380 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00004381 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004382 IsReversed = true ; // x CC y ? y : x
4383 } else {
4384 return SDValue();
4385 }
4386
Bob Wilsone742bb52010-02-24 22:15:53 +00004387 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004388 switch (CC) {
4389 default: break;
4390 case ISD::SETOLT:
4391 case ISD::SETOLE:
4392 case ISD::SETLT:
4393 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004394 case ISD::SETULT:
4395 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004396 // If LHS is NaN, an ordered comparison will be false and the result will
4397 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
4398 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4399 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
4400 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4401 break;
4402 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
4403 // will return -0, so vmin can only be used for unsafe math or if one of
4404 // the operands is known to be nonzero.
4405 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
4406 !UnsafeFPMath &&
4407 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4408 break;
4409 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004410 break;
4411
4412 case ISD::SETOGT:
4413 case ISD::SETOGE:
4414 case ISD::SETGT:
4415 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004416 case ISD::SETUGT:
4417 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004418 // If LHS is NaN, an ordered comparison will be false and the result will
4419 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
4420 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4421 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
4422 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4423 break;
4424 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
4425 // will return +0, so vmax can only be used for unsafe math or if one of
4426 // the operands is known to be nonzero.
4427 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
4428 !UnsafeFPMath &&
4429 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4430 break;
4431 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004432 break;
4433 }
4434
4435 if (!Opcode)
4436 return SDValue();
4437 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
4438}
4439
Dan Gohman475871a2008-07-27 21:46:04 +00004440SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004441 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004442 switch (N->getOpcode()) {
4443 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004444 case ISD::ADD: return PerformADDCombine(N, DCI);
4445 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004446 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbache5165492009-11-09 00:11:35 +00004447 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004448 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004449 case ISD::SHL:
4450 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004451 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004452 case ISD::SIGN_EXTEND:
4453 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004454 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
4455 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004456 }
Dan Gohman475871a2008-07-27 21:46:04 +00004457 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004458}
4459
Bill Wendlingaf566342009-08-15 21:21:19 +00004460bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
4461 if (!Subtarget->hasV6Ops())
4462 // Pre-v6 does not support unaligned mem access.
4463 return false;
Anton Korobeynikov90cfc132010-01-30 14:08:12 +00004464 else {
4465 // v6+ may or may not support unaligned mem access depending on the system
4466 // configuration.
4467 // FIXME: This is pretty conservative. Should we provide cmdline option to
4468 // control the behaviour?
Bill Wendlingaf566342009-08-15 21:21:19 +00004469 if (!Subtarget->isTargetDarwin())
4470 return false;
4471 }
4472
4473 switch (VT.getSimpleVT().SimpleTy) {
4474 default:
4475 return false;
4476 case MVT::i8:
4477 case MVT::i16:
4478 case MVT::i32:
4479 return true;
4480 // FIXME: VLD1 etc with standard alignment is legal.
4481 }
4482}
4483
Evan Chenge6c835f2009-08-14 20:09:37 +00004484static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
4485 if (V < 0)
4486 return false;
4487
4488 unsigned Scale = 1;
4489 switch (VT.getSimpleVT().SimpleTy) {
4490 default: return false;
4491 case MVT::i1:
4492 case MVT::i8:
4493 // Scale == 1;
4494 break;
4495 case MVT::i16:
4496 // Scale == 2;
4497 Scale = 2;
4498 break;
4499 case MVT::i32:
4500 // Scale == 4;
4501 Scale = 4;
4502 break;
4503 }
4504
4505 if ((V & (Scale - 1)) != 0)
4506 return false;
4507 V /= Scale;
4508 return V == (V & ((1LL << 5) - 1));
4509}
4510
4511static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
4512 const ARMSubtarget *Subtarget) {
4513 bool isNeg = false;
4514 if (V < 0) {
4515 isNeg = true;
4516 V = - V;
4517 }
4518
4519 switch (VT.getSimpleVT().SimpleTy) {
4520 default: return false;
4521 case MVT::i1:
4522 case MVT::i8:
4523 case MVT::i16:
4524 case MVT::i32:
4525 // + imm12 or - imm8
4526 if (isNeg)
4527 return V == (V & ((1LL << 8) - 1));
4528 return V == (V & ((1LL << 12) - 1));
4529 case MVT::f32:
4530 case MVT::f64:
4531 // Same as ARM mode. FIXME: NEON?
4532 if (!Subtarget->hasVFP2())
4533 return false;
4534 if ((V & 3) != 0)
4535 return false;
4536 V >>= 2;
4537 return V == (V & ((1LL << 8) - 1));
4538 }
4539}
4540
Evan Chengb01fad62007-03-12 23:30:29 +00004541/// isLegalAddressImmediate - Return true if the integer value can be used
4542/// as the offset of the target addressing mode for load / store of the
4543/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00004544static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004545 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00004546 if (V == 0)
4547 return true;
4548
Evan Cheng65011532009-03-09 19:15:00 +00004549 if (!VT.isSimple())
4550 return false;
4551
Evan Chenge6c835f2009-08-14 20:09:37 +00004552 if (Subtarget->isThumb1Only())
4553 return isLegalT1AddressImmediate(V, VT);
4554 else if (Subtarget->isThumb2())
4555 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00004556
Evan Chenge6c835f2009-08-14 20:09:37 +00004557 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00004558 if (V < 0)
4559 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00004560 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00004561 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004562 case MVT::i1:
4563 case MVT::i8:
4564 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00004565 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004566 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004567 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00004568 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004569 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004570 case MVT::f32:
4571 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00004572 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00004573 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00004574 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00004575 return false;
4576 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004577 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00004578 }
Evan Chenga8e29892007-01-19 07:51:42 +00004579}
4580
Evan Chenge6c835f2009-08-14 20:09:37 +00004581bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
4582 EVT VT) const {
4583 int Scale = AM.Scale;
4584 if (Scale < 0)
4585 return false;
4586
4587 switch (VT.getSimpleVT().SimpleTy) {
4588 default: return false;
4589 case MVT::i1:
4590 case MVT::i8:
4591 case MVT::i16:
4592 case MVT::i32:
4593 if (Scale == 1)
4594 return true;
4595 // r + r << imm
4596 Scale = Scale & ~1;
4597 return Scale == 2 || Scale == 4 || Scale == 8;
4598 case MVT::i64:
4599 // r + r
4600 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
4601 return true;
4602 return false;
4603 case MVT::isVoid:
4604 // Note, we allow "void" uses (basically, uses that aren't loads or
4605 // stores), because arm allows folding a scale into many arithmetic
4606 // operations. This should be made more precise and revisited later.
4607
4608 // Allow r << imm, but the imm has to be a multiple of two.
4609 if (Scale & 1) return false;
4610 return isPowerOf2_32(Scale);
4611 }
4612}
4613
Chris Lattner37caf8c2007-04-09 23:33:39 +00004614/// isLegalAddressingMode - Return true if the addressing mode represented
4615/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004616bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004617 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004618 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00004619 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00004620 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004621
Chris Lattner37caf8c2007-04-09 23:33:39 +00004622 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004623 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004624 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004625
Chris Lattner37caf8c2007-04-09 23:33:39 +00004626 switch (AM.Scale) {
4627 case 0: // no scale reg, must be "r+i" or "r", or "i".
4628 break;
4629 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00004630 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00004631 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004632 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00004633 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004634 // ARM doesn't support any R+R*scale+imm addr modes.
4635 if (AM.BaseOffs)
4636 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004637
Bob Wilson2c7dab12009-04-08 17:55:28 +00004638 if (!VT.isSimple())
4639 return false;
4640
Evan Chenge6c835f2009-08-14 20:09:37 +00004641 if (Subtarget->isThumb2())
4642 return isLegalT2ScaledAddressingMode(AM, VT);
4643
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004644 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00004645 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00004646 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004647 case MVT::i1:
4648 case MVT::i8:
4649 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004650 if (Scale < 0) Scale = -Scale;
4651 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004652 return true;
4653 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00004654 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00004655 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00004656 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004657 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004658 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004659 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00004660 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004661
Owen Anderson825b72b2009-08-11 20:47:22 +00004662 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004663 // Note, we allow "void" uses (basically, uses that aren't loads or
4664 // stores), because arm allows folding a scale into many arithmetic
4665 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004666
Chris Lattner37caf8c2007-04-09 23:33:39 +00004667 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00004668 if (Scale & 1) return false;
4669 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00004670 }
4671 break;
Evan Chengb01fad62007-03-12 23:30:29 +00004672 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00004673 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00004674}
4675
Evan Cheng77e47512009-11-11 19:05:52 +00004676/// isLegalICmpImmediate - Return true if the specified immediate is legal
4677/// icmp immediate, that is the target has icmp instructions which can compare
4678/// a register against the immediate without having to materialize the
4679/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00004680bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00004681 if (!Subtarget->isThumb())
4682 return ARM_AM::getSOImmVal(Imm) != -1;
4683 if (Subtarget->isThumb2())
4684 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00004685 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00004686}
4687
Owen Andersone50ed302009-08-10 22:56:29 +00004688static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004689 bool isSEXTLoad, SDValue &Base,
4690 SDValue &Offset, bool &isInc,
4691 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00004692 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4693 return false;
4694
Owen Anderson825b72b2009-08-11 20:47:22 +00004695 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00004696 // AddressingMode 3
4697 Base = Ptr->getOperand(0);
4698 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004699 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004700 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004701 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004702 isInc = false;
4703 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4704 return true;
4705 }
4706 }
4707 isInc = (Ptr->getOpcode() == ISD::ADD);
4708 Offset = Ptr->getOperand(1);
4709 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004710 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00004711 // AddressingMode 2
4712 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004713 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004714 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004715 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004716 isInc = false;
4717 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4718 Base = Ptr->getOperand(0);
4719 return true;
4720 }
4721 }
4722
4723 if (Ptr->getOpcode() == ISD::ADD) {
4724 isInc = true;
4725 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
4726 if (ShOpcVal != ARM_AM::no_shift) {
4727 Base = Ptr->getOperand(1);
4728 Offset = Ptr->getOperand(0);
4729 } else {
4730 Base = Ptr->getOperand(0);
4731 Offset = Ptr->getOperand(1);
4732 }
4733 return true;
4734 }
4735
4736 isInc = (Ptr->getOpcode() == ISD::ADD);
4737 Base = Ptr->getOperand(0);
4738 Offset = Ptr->getOperand(1);
4739 return true;
4740 }
4741
Jim Grosbache5165492009-11-09 00:11:35 +00004742 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00004743 return false;
4744}
4745
Owen Andersone50ed302009-08-10 22:56:29 +00004746static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004747 bool isSEXTLoad, SDValue &Base,
4748 SDValue &Offset, bool &isInc,
4749 SelectionDAG &DAG) {
4750 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4751 return false;
4752
4753 Base = Ptr->getOperand(0);
4754 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
4755 int RHSC = (int)RHS->getZExtValue();
4756 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
4757 assert(Ptr->getOpcode() == ISD::ADD);
4758 isInc = false;
4759 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4760 return true;
4761 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
4762 isInc = Ptr->getOpcode() == ISD::ADD;
4763 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
4764 return true;
4765 }
4766 }
4767
4768 return false;
4769}
4770
Evan Chenga8e29892007-01-19 07:51:42 +00004771/// getPreIndexedAddressParts - returns true by value, base pointer and
4772/// offset pointer and addressing mode by reference if the node's address
4773/// can be legally represented as pre-indexed load / store address.
4774bool
Dan Gohman475871a2008-07-27 21:46:04 +00004775ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
4776 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004777 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004778 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004779 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004780 return false;
4781
Owen Andersone50ed302009-08-10 22:56:29 +00004782 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004783 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004784 bool isSEXTLoad = false;
4785 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4786 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004787 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004788 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4789 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
4790 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004791 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004792 } else
4793 return false;
4794
4795 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004796 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004797 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004798 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
4799 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004800 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004801 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00004802 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00004803 if (!isLegal)
4804 return false;
4805
4806 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
4807 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004808}
4809
4810/// getPostIndexedAddressParts - returns true by value, base pointer and
4811/// offset pointer and addressing mode by reference if this node can be
4812/// combined with a load / store to form a post-indexed load / store.
4813bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00004814 SDValue &Base,
4815 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004816 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004817 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004818 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004819 return false;
4820
Owen Andersone50ed302009-08-10 22:56:29 +00004821 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004822 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004823 bool isSEXTLoad = false;
4824 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004825 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00004826 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00004827 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4828 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004829 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00004830 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00004831 } else
4832 return false;
4833
4834 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004835 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004836 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004837 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00004838 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004839 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004840 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
4841 isInc, DAG);
4842 if (!isLegal)
4843 return false;
4844
Evan Cheng28dad2a2010-05-18 21:31:17 +00004845 if (Ptr != Base) {
4846 // Swap base ptr and offset to catch more post-index load / store when
4847 // it's legal. In Thumb2 mode, offset must be an immediate.
4848 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
4849 !Subtarget->isThumb2())
4850 std::swap(Base, Offset);
4851
4852 // Post-indexed load / store update the base pointer.
4853 if (Ptr != Base)
4854 return false;
4855 }
4856
Evan Chenge88d5ce2009-07-02 07:28:31 +00004857 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
4858 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004859}
4860
Dan Gohman475871a2008-07-27 21:46:04 +00004861void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00004862 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004863 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004864 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00004865 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00004866 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004867 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004868 switch (Op.getOpcode()) {
4869 default: break;
4870 case ARMISD::CMOV: {
4871 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00004872 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004873 if (KnownZero == 0 && KnownOne == 0) return;
4874
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004875 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00004876 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
4877 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004878 KnownZero &= KnownZeroRHS;
4879 KnownOne &= KnownOneRHS;
4880 return;
4881 }
4882 }
4883}
4884
4885//===----------------------------------------------------------------------===//
4886// ARM Inline Assembly Support
4887//===----------------------------------------------------------------------===//
4888
4889/// getConstraintType - Given a constraint letter, return the type of
4890/// constraint it is for this target.
4891ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00004892ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
4893 if (Constraint.size() == 1) {
4894 switch (Constraint[0]) {
4895 default: break;
4896 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004897 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00004898 }
Evan Chenga8e29892007-01-19 07:51:42 +00004899 }
Chris Lattner4234f572007-03-25 02:14:49 +00004900 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00004901}
4902
Bob Wilson2dc4f542009-03-20 22:42:55 +00004903std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00004904ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004905 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004906 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004907 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00004908 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004909 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004910 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004911 return std::make_pair(0U, ARM::tGPRRegisterClass);
4912 else
4913 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004914 case 'r':
4915 return std::make_pair(0U, ARM::GPRRegisterClass);
4916 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004917 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004918 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00004919 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004920 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00004921 if (VT.getSizeInBits() == 128)
4922 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004923 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004924 }
4925 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00004926 if (StringRef("{cc}").equals_lower(Constraint))
4927 return std::make_pair(0U, ARM::CCRRegisterClass);
4928
Evan Chenga8e29892007-01-19 07:51:42 +00004929 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
4930}
4931
4932std::vector<unsigned> ARMTargetLowering::
4933getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004934 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004935 if (Constraint.size() != 1)
4936 return std::vector<unsigned>();
4937
4938 switch (Constraint[0]) { // GCC ARM Constraint Letters
4939 default: break;
4940 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004941 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4942 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4943 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004944 case 'r':
4945 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4946 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4947 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
4948 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004949 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004950 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004951 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
4952 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
4953 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
4954 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
4955 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
4956 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
4957 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
4958 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00004959 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004960 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
4961 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
4962 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
4963 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00004964 if (VT.getSizeInBits() == 128)
4965 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
4966 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004967 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004968 }
4969
4970 return std::vector<unsigned>();
4971}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004972
4973/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
4974/// vector. If it is invalid, don't add anything to Ops.
4975void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
4976 char Constraint,
4977 bool hasMemory,
4978 std::vector<SDValue>&Ops,
4979 SelectionDAG &DAG) const {
4980 SDValue Result(0, 0);
4981
4982 switch (Constraint) {
4983 default: break;
4984 case 'I': case 'J': case 'K': case 'L':
4985 case 'M': case 'N': case 'O':
4986 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
4987 if (!C)
4988 return;
4989
4990 int64_t CVal64 = C->getSExtValue();
4991 int CVal = (int) CVal64;
4992 // None of these constraints allow values larger than 32 bits. Check
4993 // that the value fits in an int.
4994 if (CVal != CVal64)
4995 return;
4996
4997 switch (Constraint) {
4998 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004999 if (Subtarget->isThumb1Only()) {
5000 // This must be a constant between 0 and 255, for ADD
5001 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005002 if (CVal >= 0 && CVal <= 255)
5003 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005004 } else if (Subtarget->isThumb2()) {
5005 // A constant that can be used as an immediate value in a
5006 // data-processing instruction.
5007 if (ARM_AM::getT2SOImmVal(CVal) != -1)
5008 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005009 } else {
5010 // A constant that can be used as an immediate value in a
5011 // data-processing instruction.
5012 if (ARM_AM::getSOImmVal(CVal) != -1)
5013 break;
5014 }
5015 return;
5016
5017 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005018 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005019 // This must be a constant between -255 and -1, for negated ADD
5020 // immediates. This can be used in GCC with an "n" modifier that
5021 // prints the negated value, for use with SUB instructions. It is
5022 // not useful otherwise but is implemented for compatibility.
5023 if (CVal >= -255 && CVal <= -1)
5024 break;
5025 } else {
5026 // This must be a constant between -4095 and 4095. It is not clear
5027 // what this constraint is intended for. Implemented for
5028 // compatibility with GCC.
5029 if (CVal >= -4095 && CVal <= 4095)
5030 break;
5031 }
5032 return;
5033
5034 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005035 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005036 // A 32-bit value where only one byte has a nonzero value. Exclude
5037 // zero to match GCC. This constraint is used by GCC internally for
5038 // constants that can be loaded with a move/shift combination.
5039 // It is not useful otherwise but is implemented for compatibility.
5040 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
5041 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005042 } else if (Subtarget->isThumb2()) {
5043 // A constant whose bitwise inverse can be used as an immediate
5044 // value in a data-processing instruction. This can be used in GCC
5045 // with a "B" modifier that prints the inverted value, for use with
5046 // BIC and MVN instructions. It is not useful otherwise but is
5047 // implemented for compatibility.
5048 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
5049 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005050 } else {
5051 // A constant whose bitwise inverse can be used as an immediate
5052 // value in a data-processing instruction. This can be used in GCC
5053 // with a "B" modifier that prints the inverted value, for use with
5054 // BIC and MVN instructions. It is not useful otherwise but is
5055 // implemented for compatibility.
5056 if (ARM_AM::getSOImmVal(~CVal) != -1)
5057 break;
5058 }
5059 return;
5060
5061 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005062 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005063 // This must be a constant between -7 and 7,
5064 // for 3-operand ADD/SUB immediate instructions.
5065 if (CVal >= -7 && CVal < 7)
5066 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005067 } else if (Subtarget->isThumb2()) {
5068 // A constant whose negation can be used as an immediate value in a
5069 // data-processing instruction. This can be used in GCC with an "n"
5070 // modifier that prints the negated value, for use with SUB
5071 // instructions. It is not useful otherwise but is implemented for
5072 // compatibility.
5073 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
5074 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005075 } else {
5076 // A constant whose negation can be used as an immediate value in a
5077 // data-processing instruction. This can be used in GCC with an "n"
5078 // modifier that prints the negated value, for use with SUB
5079 // instructions. It is not useful otherwise but is implemented for
5080 // compatibility.
5081 if (ARM_AM::getSOImmVal(-CVal) != -1)
5082 break;
5083 }
5084 return;
5085
5086 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005087 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005088 // This must be a multiple of 4 between 0 and 1020, for
5089 // ADD sp + immediate.
5090 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
5091 break;
5092 } else {
5093 // A power of two or a constant between 0 and 32. This is used in
5094 // GCC for the shift amount on shifted register operands, but it is
5095 // useful in general for any shift amounts.
5096 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
5097 break;
5098 }
5099 return;
5100
5101 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005102 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005103 // This must be a constant between 0 and 31, for shift amounts.
5104 if (CVal >= 0 && CVal <= 31)
5105 break;
5106 }
5107 return;
5108
5109 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005110 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005111 // This must be a multiple of 4 between -508 and 508, for
5112 // ADD/SUB sp = sp + immediate.
5113 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
5114 break;
5115 }
5116 return;
5117 }
5118 Result = DAG.getTargetConstant(CVal, Op.getValueType());
5119 break;
5120 }
5121
5122 if (Result.getNode()) {
5123 Ops.push_back(Result);
5124 return;
5125 }
5126 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
5127 Ops, DAG);
5128}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00005129
5130bool
5131ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
5132 // The ARM target isn't yet aware of offsets.
5133 return false;
5134}
Evan Cheng39382422009-10-28 01:44:26 +00005135
5136int ARM::getVFPf32Imm(const APFloat &FPImm) {
5137 APInt Imm = FPImm.bitcastToAPInt();
5138 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
5139 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
5140 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
5141
5142 // We can handle 4 bits of mantissa.
5143 // mantissa = (16+UInt(e:f:g:h))/16.
5144 if (Mantissa & 0x7ffff)
5145 return -1;
5146 Mantissa >>= 19;
5147 if ((Mantissa & 0xf) != Mantissa)
5148 return -1;
5149
5150 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
5151 if (Exp < -3 || Exp > 4)
5152 return -1;
5153 Exp = ((Exp+3) & 0x7) ^ 4;
5154
5155 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
5156}
5157
5158int ARM::getVFPf64Imm(const APFloat &FPImm) {
5159 APInt Imm = FPImm.bitcastToAPInt();
5160 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
5161 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
5162 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
5163
5164 // We can handle 4 bits of mantissa.
5165 // mantissa = (16+UInt(e:f:g:h))/16.
5166 if (Mantissa & 0xffffffffffffLL)
5167 return -1;
5168 Mantissa >>= 48;
5169 if ((Mantissa & 0xf) != Mantissa)
5170 return -1;
5171
5172 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
5173 if (Exp < -3 || Exp > 4)
5174 return -1;
5175 Exp = ((Exp+3) & 0x7) ^ 4;
5176
5177 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
5178}
5179
5180/// isFPImmLegal - Returns true if the target can instruction select the
5181/// specified FP immediate natively. If false, the legalizer will
5182/// materialize the FP immediate as a load from a constant pool.
5183bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
5184 if (!Subtarget->hasVFP3())
5185 return false;
5186 if (VT == MVT::f32)
5187 return ARM::getVFPf32Imm(Imm) != -1;
5188 if (VT == MVT::f64)
5189 return ARM::getVFPf64Imm(Imm) != -1;
5190 return false;
5191}