blob: 0e33758508fc8d7fecce955d6625ff210384397c [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)."),
Dale Johannesenc66cdf72010-06-18 19:00:18 +000058 cl::init(true));
Dale Johannesen51e28e62010-06-03 21:09:53 +000059
Jim Grosbache7b52522010-04-14 22:28:31 +000060static cl::opt<bool>
61EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000062 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbache7b52522010-04-14 22:28:31 +000063 cl::init(false));
64
Evan Cheng46df4eb2010-06-16 07:35:02 +000065static cl::opt<bool>
66ARMInterworking("arm-interworking", cl::Hidden,
67 cl::desc("Enable / disable ARM interworking (for debugging only)"),
68 cl::init(true));
69
Evan Chengf6799392010-06-26 01:52:05 +000070static cl::opt<bool>
71EnableARMCodePlacement("arm-code-placement", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000072 cl::desc("Enable code placement pass for ARM"),
Evan Chengf6799392010-06-26 01:52:05 +000073 cl::init(false));
74
Owen Andersone50ed302009-08-10 22:56:29 +000075static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000076 CCValAssign::LocInfo &LocInfo,
77 ISD::ArgFlagsTy &ArgFlags,
78 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000079static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000080 CCValAssign::LocInfo &LocInfo,
81 ISD::ArgFlagsTy &ArgFlags,
82 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000083static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000084 CCValAssign::LocInfo &LocInfo,
85 ISD::ArgFlagsTy &ArgFlags,
86 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000087static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000088 CCValAssign::LocInfo &LocInfo,
89 ISD::ArgFlagsTy &ArgFlags,
90 CCState &State);
91
Owen Andersone50ed302009-08-10 22:56:29 +000092void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
93 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000094 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000095 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000096 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
97 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000098
Owen Anderson70671842009-08-10 20:18:46 +000099 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000100 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000101 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000102 }
103
Owen Andersone50ed302009-08-10 22:56:29 +0000104 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000105 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +0000106 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000107 if (ElemTy == MVT::i8 || ElemTy == MVT::i16)
Owen Anderson70671842009-08-10 20:18:46 +0000108 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +0000109 if (ElemTy != MVT::i32) {
110 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
111 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
112 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
113 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
114 }
Owen Anderson70671842009-08-10 20:18:46 +0000115 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
116 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +0000117 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Anton Korobeynikov8e6c2b92009-08-21 12:40:35 +0000118 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
Bob Wilsond0910c42010-04-06 22:02:24 +0000119 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
120 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000121 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000122 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
123 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
124 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000125 }
126
127 // Promote all bit-wise operations.
128 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000129 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000130 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
131 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000132 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000133 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000134 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000135 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000136 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000137 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000138 }
Bob Wilson16330762009-09-16 00:17:28 +0000139
140 // Neon does not support vector divide/remainder operations.
141 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
142 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
143 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
144 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
145 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
146 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000147}
148
Owen Andersone50ed302009-08-10 22:56:29 +0000149void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000150 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000151 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000152}
153
Owen Andersone50ed302009-08-10 22:56:29 +0000154void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000155 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000156 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000157}
158
Chris Lattnerf0144122009-07-28 03:13:23 +0000159static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
160 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000161 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000162
Chris Lattner80ec2792009-08-02 00:34:36 +0000163 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000164}
165
Evan Chenga8e29892007-01-19 07:51:42 +0000166ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000167 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000168 Subtarget = &TM.getSubtarget<ARMSubtarget>();
169
Evan Chengb1df8f22007-04-27 08:15:43 +0000170 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000171 // Uses VFP for Thumb libfuncs if available.
172 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
173 // Single-precision floating-point arithmetic.
174 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
175 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
176 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
177 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000178
Evan Chengb1df8f22007-04-27 08:15:43 +0000179 // Double-precision floating-point arithmetic.
180 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
181 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
182 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
183 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000184
Evan Chengb1df8f22007-04-27 08:15:43 +0000185 // Single-precision comparisons.
186 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
187 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
188 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
189 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
190 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
191 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
192 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
193 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000194
Evan Chengb1df8f22007-04-27 08:15:43 +0000195 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
196 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
197 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
198 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
199 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
200 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
201 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
202 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000203
Evan Chengb1df8f22007-04-27 08:15:43 +0000204 // Double-precision comparisons.
205 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
206 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
207 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
208 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
209 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
210 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
211 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
212 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000213
Evan Chengb1df8f22007-04-27 08:15:43 +0000214 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
215 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
216 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
217 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
218 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
219 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
220 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
221 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000222
Evan Chengb1df8f22007-04-27 08:15:43 +0000223 // Floating-point to integer conversions.
224 // i64 conversions are done via library routines even when generating VFP
225 // instructions, so use the same ones.
226 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
227 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
228 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
229 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000230
Evan Chengb1df8f22007-04-27 08:15:43 +0000231 // Conversions between floating types.
232 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
233 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
234
235 // Integer to floating-point conversions.
236 // i64 conversions are done via library routines even when generating VFP
237 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000238 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
239 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000240 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
241 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
242 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
243 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
244 }
Evan Chenga8e29892007-01-19 07:51:42 +0000245 }
246
Bob Wilson2f954612009-05-22 17:38:41 +0000247 // These libcalls are not available in 32-bit.
248 setLibcallName(RTLIB::SHL_I128, 0);
249 setLibcallName(RTLIB::SRL_I128, 0);
250 setLibcallName(RTLIB::SRA_I128, 0);
251
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000252 // Libcalls should use the AAPCS base standard ABI, even if hard float
253 // is in effect, as per the ARM RTABI specification, section 4.1.2.
254 if (Subtarget->isAAPCS_ABI()) {
255 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
256 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
257 CallingConv::ARM_AAPCS);
258 }
259 }
260
David Goodwinf1daf7d2009-07-08 23:10:31 +0000261 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000263 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000264 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000265 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000266 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
267 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000268
Owen Anderson825b72b2009-08-11 20:47:22 +0000269 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000270 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000271
272 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 addDRTypeForNEON(MVT::v2f32);
274 addDRTypeForNEON(MVT::v8i8);
275 addDRTypeForNEON(MVT::v4i16);
276 addDRTypeForNEON(MVT::v2i32);
277 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000278
Owen Anderson825b72b2009-08-11 20:47:22 +0000279 addQRTypeForNEON(MVT::v4f32);
280 addQRTypeForNEON(MVT::v2f64);
281 addQRTypeForNEON(MVT::v16i8);
282 addQRTypeForNEON(MVT::v8i16);
283 addQRTypeForNEON(MVT::v4i32);
284 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000285
Bob Wilson74dc72e2009-09-15 23:55:57 +0000286 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
287 // neither Neon nor VFP support any arithmetic operations on it.
288 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
289 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
290 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
291 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
292 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
293 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
294 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
295 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
296 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
297 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
298 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
299 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
300 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
301 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
302 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
303 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
304 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
305 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
306 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
307 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
308 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
309 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
310 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
311 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
312
Bob Wilson642b3292009-09-16 00:32:15 +0000313 // Neon does not support some operations on v1i64 and v2i64 types.
314 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
315 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
316 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
317 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
318
Bob Wilson5bafff32009-06-22 23:27:02 +0000319 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
320 setTargetDAGCombine(ISD::SHL);
321 setTargetDAGCombine(ISD::SRL);
322 setTargetDAGCombine(ISD::SRA);
323 setTargetDAGCombine(ISD::SIGN_EXTEND);
324 setTargetDAGCombine(ISD::ZERO_EXTEND);
325 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000326 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson5bafff32009-06-22 23:27:02 +0000327 }
328
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000329 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000330
331 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000332 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000333
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000334 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000335 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000336
Evan Chenga8e29892007-01-19 07:51:42 +0000337 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000338 if (!Subtarget->isThumb1Only()) {
339 for (unsigned im = (unsigned)ISD::PRE_INC;
340 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000341 setIndexedLoadAction(im, MVT::i1, Legal);
342 setIndexedLoadAction(im, MVT::i8, Legal);
343 setIndexedLoadAction(im, MVT::i16, Legal);
344 setIndexedLoadAction(im, MVT::i32, Legal);
345 setIndexedStoreAction(im, MVT::i1, Legal);
346 setIndexedStoreAction(im, MVT::i8, Legal);
347 setIndexedStoreAction(im, MVT::i16, Legal);
348 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000349 }
Evan Chenga8e29892007-01-19 07:51:42 +0000350 }
351
352 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000353 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 setOperationAction(ISD::MUL, MVT::i64, Expand);
355 setOperationAction(ISD::MULHU, MVT::i32, Expand);
356 setOperationAction(ISD::MULHS, MVT::i32, Expand);
357 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
358 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000359 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000360 setOperationAction(ISD::MUL, MVT::i64, Expand);
361 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000362 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000363 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000364 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000365 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000366 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000367 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000368 setOperationAction(ISD::SRL, MVT::i64, Custom);
369 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000370
371 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000373 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000374 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000375 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000376 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000377
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000378 // Only ARMv6 has BSWAP.
379 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000380 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000381
Evan Chenga8e29892007-01-19 07:51:42 +0000382 // These are expanded into libcalls.
Jim Grosbach29402132010-05-05 23:44:43 +0000383 if (!Subtarget->hasDivide()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000384 // v7M has a hardware divider
385 setOperationAction(ISD::SDIV, MVT::i32, Expand);
386 setOperationAction(ISD::UDIV, MVT::i32, Expand);
387 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000388 setOperationAction(ISD::SREM, MVT::i32, Expand);
389 setOperationAction(ISD::UREM, MVT::i32, Expand);
390 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
391 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000392
Owen Anderson825b72b2009-08-11 20:47:22 +0000393 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
394 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
395 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
396 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000397 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000398
Evan Chengfb3611d2010-05-11 07:26:32 +0000399 setOperationAction(ISD::TRAP, MVT::Other, Legal);
400
Evan Chenga8e29892007-01-19 07:51:42 +0000401 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000402 setOperationAction(ISD::VASTART, MVT::Other, Custom);
403 setOperationAction(ISD::VAARG, MVT::Other, Expand);
404 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
405 setOperationAction(ISD::VAEND, MVT::Other, Expand);
406 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
407 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000408 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
409 // FIXME: Shouldn't need this, since no register is used, but the legalizer
410 // doesn't yet know how to not do that for SjLj.
411 setExceptionSelectorRegister(ARM::R0);
Evan Cheng3a1588a2010-04-15 22:20:34 +0000412 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Jim Grosbach7072cf62010-06-17 02:02:03 +0000413 // Handle atomics directly for ARMv[67] (except for Thumb1), otherwise
414 // use the default expansion.
Jim Grosbach68741be2010-06-18 22:35:32 +0000415 bool canHandleAtomics =
Jim Grosbach7072cf62010-06-17 02:02:03 +0000416 (Subtarget->hasV7Ops() ||
Jim Grosbach68741be2010-06-18 22:35:32 +0000417 (Subtarget->hasV6Ops() && !Subtarget->isThumb1Only()));
418 if (canHandleAtomics) {
419 // membarrier needs custom lowering; the rest are legal and handled
420 // normally.
421 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
422 } else {
423 // Set them all for expansion, which will force libcalls.
424 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
425 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Expand);
426 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Expand);
427 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000428 setOperationAction(ISD::ATOMIC_SWAP, MVT::i8, Expand);
429 setOperationAction(ISD::ATOMIC_SWAP, MVT::i16, Expand);
430 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000431 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i8, Expand);
432 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i16, Expand);
433 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
434 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Expand);
435 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Expand);
436 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
437 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i8, Expand);
438 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i16, Expand);
439 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
440 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i8, Expand);
441 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i16, Expand);
442 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
443 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i8, Expand);
444 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i16, Expand);
445 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
446 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i8, Expand);
447 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i16, Expand);
448 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000449 // Since the libcalls include locking, fold in the fences
450 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000451 }
452 // 64-bit versions are always libcalls (for now)
453 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000454 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000455 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Expand);
456 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Expand);
457 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Expand);
458 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Expand);
459 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Expand);
460 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000461
Eli Friedmana2c6f452010-06-26 04:36:50 +0000462 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
463 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000464 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
465 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000466 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000468
David Goodwinf1daf7d2009-07-08 23:10:31 +0000469 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000470 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
471 // iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000472 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000473
474 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000475 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000476 if (Subtarget->isTargetDarwin()) {
477 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
478 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
479 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000480
Owen Anderson825b72b2009-08-11 20:47:22 +0000481 setOperationAction(ISD::SETCC, MVT::i32, Expand);
482 setOperationAction(ISD::SETCC, MVT::f32, Expand);
483 setOperationAction(ISD::SETCC, MVT::f64, Expand);
484 setOperationAction(ISD::SELECT, MVT::i32, Expand);
485 setOperationAction(ISD::SELECT, MVT::f32, Expand);
486 setOperationAction(ISD::SELECT, MVT::f64, Expand);
487 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
488 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
489 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000490
Owen Anderson825b72b2009-08-11 20:47:22 +0000491 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
492 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
493 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
494 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
495 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000496
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000497 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000498 setOperationAction(ISD::FSIN, MVT::f64, Expand);
499 setOperationAction(ISD::FSIN, MVT::f32, Expand);
500 setOperationAction(ISD::FCOS, MVT::f32, Expand);
501 setOperationAction(ISD::FCOS, MVT::f64, Expand);
502 setOperationAction(ISD::FREM, MVT::f64, Expand);
503 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000504 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000505 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
506 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000507 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000508 setOperationAction(ISD::FPOW, MVT::f64, Expand);
509 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000510
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000511 // Various VFP goodness
512 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000513 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
514 if (Subtarget->hasVFP2()) {
515 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
516 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
517 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
518 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
519 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000520 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000521 if (!Subtarget->hasFP16()) {
522 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
523 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000524 }
Evan Cheng110cf482008-04-01 01:50:16 +0000525 }
Evan Chenga8e29892007-01-19 07:51:42 +0000526
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000527 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000528 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000529 setTargetDAGCombine(ISD::ADD);
530 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000531 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000532
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000533 if (Subtarget->hasV6T2Ops())
534 setTargetDAGCombine(ISD::OR);
535
Evan Chenga8e29892007-01-19 07:51:42 +0000536 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000537
Evan Chengf7d87ee2010-05-21 00:43:17 +0000538 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
539 setSchedulingPreference(Sched::RegPressure);
540 else
541 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000542
543 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Evan Chengf6799392010-06-26 01:52:05 +0000544
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000545 // On ARM arguments smaller than 4 bytes are extended, so all arguments
546 // are at least 4 bytes aligned.
547 setMinStackArgumentAlignment(4);
548
Evan Chengf6799392010-06-26 01:52:05 +0000549 if (EnableARMCodePlacement)
550 benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000551}
552
Evan Cheng4f6b4672010-07-21 06:09:07 +0000553std::pair<const TargetRegisterClass*, uint8_t>
554ARMTargetLowering::findRepresentativeClass(EVT VT) const{
555 const TargetRegisterClass *RRC = 0;
556 uint8_t Cost = 1;
557 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000558 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000559 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000560 // Use DPR as representative register class for all floating point
561 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
562 // the cost is 1 for both f32 and f64.
563 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000564 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000565 RRC = ARM::DPRRegisterClass;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000566 break;
567 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
568 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000569 RRC = ARM::DPRRegisterClass;
570 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000571 break;
572 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000573 RRC = ARM::DPRRegisterClass;
574 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000575 break;
576 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000577 RRC = ARM::DPRRegisterClass;
578 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000579 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000580 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000581 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000582}
583
Evan Chenga8e29892007-01-19 07:51:42 +0000584const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
585 switch (Opcode) {
586 default: return 0;
587 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000588 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
589 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000590 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000591 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
592 case ARMISD::tCALL: return "ARMISD::tCALL";
593 case ARMISD::BRCOND: return "ARMISD::BRCOND";
594 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000595 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000596 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
597 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
598 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000599 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000600 case ARMISD::CMPFP: return "ARMISD::CMPFP";
601 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000602 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000603 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
604 case ARMISD::CMOV: return "ARMISD::CMOV";
605 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000606
Jim Grosbach3482c802010-01-18 19:58:49 +0000607 case ARMISD::RBIT: return "ARMISD::RBIT";
608
Bob Wilson76a312b2010-03-19 22:51:32 +0000609 case ARMISD::FTOSI: return "ARMISD::FTOSI";
610 case ARMISD::FTOUI: return "ARMISD::FTOUI";
611 case ARMISD::SITOF: return "ARMISD::SITOF";
612 case ARMISD::UITOF: return "ARMISD::UITOF";
613
Evan Chenga8e29892007-01-19 07:51:42 +0000614 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
615 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
616 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000617
Jim Grosbache5165492009-11-09 00:11:35 +0000618 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
619 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000620
Evan Chengc5942082009-10-28 06:55:03 +0000621 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
622 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
623
Dale Johannesen51e28e62010-06-03 21:09:53 +0000624 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
625
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000626 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000627
Evan Cheng86198642009-08-07 00:34:42 +0000628 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
629
Jim Grosbach3728e962009-12-10 00:11:09 +0000630 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
631 case ARMISD::SYNCBARRIER: return "ARMISD::SYNCBARRIER";
632
Bob Wilson5bafff32009-06-22 23:27:02 +0000633 case ARMISD::VCEQ: return "ARMISD::VCEQ";
634 case ARMISD::VCGE: return "ARMISD::VCGE";
635 case ARMISD::VCGEU: return "ARMISD::VCGEU";
636 case ARMISD::VCGT: return "ARMISD::VCGT";
637 case ARMISD::VCGTU: return "ARMISD::VCGTU";
638 case ARMISD::VTST: return "ARMISD::VTST";
639
640 case ARMISD::VSHL: return "ARMISD::VSHL";
641 case ARMISD::VSHRs: return "ARMISD::VSHRs";
642 case ARMISD::VSHRu: return "ARMISD::VSHRu";
643 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
644 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
645 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
646 case ARMISD::VSHRN: return "ARMISD::VSHRN";
647 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
648 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
649 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
650 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
651 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
652 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
653 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
654 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
655 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
656 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
657 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
658 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
659 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
660 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000661 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000662 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000663 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000664 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000665 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000666 case ARMISD::VREV64: return "ARMISD::VREV64";
667 case ARMISD::VREV32: return "ARMISD::VREV32";
668 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000669 case ARMISD::VZIP: return "ARMISD::VZIP";
670 case ARMISD::VUZP: return "ARMISD::VUZP";
671 case ARMISD::VTRN: return "ARMISD::VTRN";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000672 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000673 case ARMISD::FMAX: return "ARMISD::FMAX";
674 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000675 case ARMISD::BFI: return "ARMISD::BFI";
Evan Chenga8e29892007-01-19 07:51:42 +0000676 }
677}
678
Evan Cheng06b666c2010-05-15 02:18:07 +0000679/// getRegClassFor - Return the register class that should be used for the
680/// specified value type.
681TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
682 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
683 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
684 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000685 if (Subtarget->hasNEON()) {
686 if (VT == MVT::v4i64)
687 return ARM::QQPRRegisterClass;
688 else if (VT == MVT::v8i64)
689 return ARM::QQQQPRRegisterClass;
690 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000691 return TargetLowering::getRegClassFor(VT);
692}
693
Eric Christopherab695882010-07-21 22:26:11 +0000694// Create a fast isel object.
695FastISel *
696ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
697 return ARM::createFastISel(funcInfo);
698}
699
Bill Wendlingb4202b82009-07-01 18:50:55 +0000700/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000701unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Bob Wilsonb5b50572010-07-01 22:26:26 +0000702 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 1 : 2;
Bill Wendling20c568f2009-06-30 22:38:32 +0000703}
704
Evan Cheng1cc39842010-05-20 23:26:43 +0000705Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000706 unsigned NumVals = N->getNumValues();
707 if (!NumVals)
708 return Sched::RegPressure;
709
710 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000711 EVT VT = N->getValueType(i);
712 if (VT.isFloatingPoint() || VT.isVector())
713 return Sched::Latency;
714 }
Evan Chengc10f5432010-05-28 23:25:23 +0000715
716 if (!N->isMachineOpcode())
717 return Sched::RegPressure;
718
719 // Load are scheduled for latency even if there instruction itinerary
720 // is not available.
721 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
722 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
723 if (TID.mayLoad())
724 return Sched::Latency;
725
726 const InstrItineraryData &Itins = getTargetMachine().getInstrItineraryData();
727 if (!Itins.isEmpty() && Itins.getStageLatency(TID.getSchedClass()) > 2)
728 return Sched::Latency;
Evan Cheng1cc39842010-05-20 23:26:43 +0000729 return Sched::RegPressure;
730}
731
Evan Chenga8e29892007-01-19 07:51:42 +0000732//===----------------------------------------------------------------------===//
733// Lowering Code
734//===----------------------------------------------------------------------===//
735
Evan Chenga8e29892007-01-19 07:51:42 +0000736/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
737static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
738 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000739 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000740 case ISD::SETNE: return ARMCC::NE;
741 case ISD::SETEQ: return ARMCC::EQ;
742 case ISD::SETGT: return ARMCC::GT;
743 case ISD::SETGE: return ARMCC::GE;
744 case ISD::SETLT: return ARMCC::LT;
745 case ISD::SETLE: return ARMCC::LE;
746 case ISD::SETUGT: return ARMCC::HI;
747 case ISD::SETUGE: return ARMCC::HS;
748 case ISD::SETULT: return ARMCC::LO;
749 case ISD::SETULE: return ARMCC::LS;
750 }
751}
752
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000753/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
754static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000755 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000756 CondCode2 = ARMCC::AL;
757 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000758 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000759 case ISD::SETEQ:
760 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
761 case ISD::SETGT:
762 case ISD::SETOGT: CondCode = ARMCC::GT; break;
763 case ISD::SETGE:
764 case ISD::SETOGE: CondCode = ARMCC::GE; break;
765 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000766 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000767 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
768 case ISD::SETO: CondCode = ARMCC::VC; break;
769 case ISD::SETUO: CondCode = ARMCC::VS; break;
770 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
771 case ISD::SETUGT: CondCode = ARMCC::HI; break;
772 case ISD::SETUGE: CondCode = ARMCC::PL; break;
773 case ISD::SETLT:
774 case ISD::SETULT: CondCode = ARMCC::LT; break;
775 case ISD::SETLE:
776 case ISD::SETULE: CondCode = ARMCC::LE; break;
777 case ISD::SETNE:
778 case ISD::SETUNE: CondCode = ARMCC::NE; break;
779 }
Evan Chenga8e29892007-01-19 07:51:42 +0000780}
781
Bob Wilson1f595bb2009-04-17 19:07:39 +0000782//===----------------------------------------------------------------------===//
783// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000784//===----------------------------------------------------------------------===//
785
786#include "ARMGenCallingConv.inc"
787
788// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000789static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000790 CCValAssign::LocInfo &LocInfo,
791 CCState &State, bool CanFail) {
792 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
793
794 // Try to get the first register.
795 if (unsigned Reg = State.AllocateReg(RegList, 4))
796 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
797 else {
798 // For the 2nd half of a v2f64, do not fail.
799 if (CanFail)
800 return false;
801
802 // Put the whole thing on the stack.
803 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
804 State.AllocateStack(8, 4),
805 LocVT, LocInfo));
806 return true;
807 }
808
809 // Try to get the second register.
810 if (unsigned Reg = State.AllocateReg(RegList, 4))
811 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
812 else
813 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
814 State.AllocateStack(4, 4),
815 LocVT, LocInfo));
816 return true;
817}
818
Owen Andersone50ed302009-08-10 22:56:29 +0000819static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000820 CCValAssign::LocInfo &LocInfo,
821 ISD::ArgFlagsTy &ArgFlags,
822 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000823 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
824 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000825 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000826 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
827 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000828 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000829}
830
831// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000832static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000833 CCValAssign::LocInfo &LocInfo,
834 CCState &State, bool CanFail) {
835 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
836 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
Rafael Espindolabc565012010-07-21 11:38:30 +0000837 static const unsigned ShadowRegList[] = { ARM::R0, ARM::R1 };
Bob Wilson5bafff32009-06-22 23:27:02 +0000838
Rafael Espindolabc565012010-07-21 11:38:30 +0000839 unsigned Reg = State.AllocateReg(HiRegList, ShadowRegList, 2);
Bob Wilson5bafff32009-06-22 23:27:02 +0000840 if (Reg == 0) {
841 // For the 2nd half of a v2f64, do not just fail.
842 if (CanFail)
843 return false;
844
845 // Put the whole thing on the stack.
846 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
847 State.AllocateStack(8, 8),
848 LocVT, LocInfo));
849 return true;
850 }
851
852 unsigned i;
853 for (i = 0; i < 2; ++i)
854 if (HiRegList[i] == Reg)
855 break;
856
Rafael Espindolabc565012010-07-21 11:38:30 +0000857 unsigned T = State.AllocateReg(LoRegList[i]);
Chandler Carruth30d35b82010-07-22 08:02:25 +0000858 (void)T;
Rafael Espindolabc565012010-07-21 11:38:30 +0000859 assert(T == LoRegList[i] && "Could not allocate register");
860
Bob Wilson5bafff32009-06-22 23:27:02 +0000861 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
862 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
863 LocVT, LocInfo));
864 return true;
865}
866
Owen Andersone50ed302009-08-10 22:56:29 +0000867static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000868 CCValAssign::LocInfo &LocInfo,
869 ISD::ArgFlagsTy &ArgFlags,
870 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000871 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
872 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000873 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000874 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
875 return false;
876 return true; // we handled it
877}
878
Owen Andersone50ed302009-08-10 22:56:29 +0000879static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000880 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000881 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
882 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
883
Bob Wilsone65586b2009-04-17 20:40:45 +0000884 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
885 if (Reg == 0)
886 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000887
Bob Wilsone65586b2009-04-17 20:40:45 +0000888 unsigned i;
889 for (i = 0; i < 2; ++i)
890 if (HiRegList[i] == Reg)
891 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000892
Bob Wilson5bafff32009-06-22 23:27:02 +0000893 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000894 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000895 LocVT, LocInfo));
896 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000897}
898
Owen Andersone50ed302009-08-10 22:56:29 +0000899static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000900 CCValAssign::LocInfo &LocInfo,
901 ISD::ArgFlagsTy &ArgFlags,
902 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000903 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
904 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000905 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000906 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000907 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000908}
909
Owen Andersone50ed302009-08-10 22:56:29 +0000910static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000911 CCValAssign::LocInfo &LocInfo,
912 ISD::ArgFlagsTy &ArgFlags,
913 CCState &State) {
914 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
915 State);
916}
917
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000918/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
919/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000920CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000921 bool Return,
922 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000923 switch (CC) {
924 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000925 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000926 case CallingConv::C:
927 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000928 // Use target triple & subtarget features to do actual dispatch.
929 if (Subtarget->isAAPCS_ABI()) {
930 if (Subtarget->hasVFP2() &&
931 FloatABIType == FloatABI::Hard && !isVarArg)
932 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
933 else
934 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
935 } else
936 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000937 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000938 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000939 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000940 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000941 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000942 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000943 }
944}
945
Dan Gohman98ca4f22009-08-05 01:29:28 +0000946/// LowerCallResult - Lower the result values of a call into the
947/// appropriate copies out of appropriate physical registers.
948SDValue
949ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000950 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000951 const SmallVectorImpl<ISD::InputArg> &Ins,
952 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000953 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000954
Bob Wilson1f595bb2009-04-17 19:07:39 +0000955 // Assign locations to each value returned by this call.
956 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000957 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000958 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000959 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000960 CCAssignFnForNode(CallConv, /* Return*/ true,
961 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000962
963 // Copy all of the result registers out of their specified physreg.
964 for (unsigned i = 0; i != RVLocs.size(); ++i) {
965 CCValAssign VA = RVLocs[i];
966
Bob Wilson80915242009-04-25 00:33:20 +0000967 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000968 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000969 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000970 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000971 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000972 Chain = Lo.getValue(1);
973 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000974 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000975 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000976 InFlag);
977 Chain = Hi.getValue(1);
978 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000979 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000980
Owen Anderson825b72b2009-08-11 20:47:22 +0000981 if (VA.getLocVT() == MVT::v2f64) {
982 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
983 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
984 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000985
986 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000987 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000988 Chain = Lo.getValue(1);
989 InFlag = Lo.getValue(2);
990 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000991 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000992 Chain = Hi.getValue(1);
993 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000994 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +0000995 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
996 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000997 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000998 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000999 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1000 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001001 Chain = Val.getValue(1);
1002 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001003 }
Bob Wilson80915242009-04-25 00:33:20 +00001004
1005 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001006 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001007 case CCValAssign::Full: break;
1008 case CCValAssign::BCvt:
1009 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
1010 break;
1011 }
1012
Dan Gohman98ca4f22009-08-05 01:29:28 +00001013 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001014 }
1015
Dan Gohman98ca4f22009-08-05 01:29:28 +00001016 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001017}
1018
1019/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1020/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +00001021/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +00001022/// a byval function parameter.
1023/// Sometimes what we are copying is the end of a larger object, the part that
1024/// does not fit in registers.
1025static SDValue
1026CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
1027 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1028 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001029 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001030 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001031 /*isVolatile=*/false, /*AlwaysInline=*/false,
1032 NULL, 0, NULL, 0);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001033}
1034
Bob Wilsondee46d72009-04-17 20:35:10 +00001035/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001036SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001037ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1038 SDValue StackPtr, SDValue Arg,
1039 DebugLoc dl, SelectionDAG &DAG,
1040 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001041 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001042 unsigned LocMemOffset = VA.getLocMemOffset();
1043 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1044 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
1045 if (Flags.isByVal()) {
1046 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
1047 }
1048 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene1b58cab2010-02-15 16:55:24 +00001049 PseudoSourceValue::getStack(), LocMemOffset,
1050 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001051}
1052
Dan Gohman98ca4f22009-08-05 01:29:28 +00001053void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001054 SDValue Chain, SDValue &Arg,
1055 RegsToPassVector &RegsToPass,
1056 CCValAssign &VA, CCValAssign &NextVA,
1057 SDValue &StackPtr,
1058 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001059 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001060
Jim Grosbache5165492009-11-09 00:11:35 +00001061 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001062 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001063 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1064
1065 if (NextVA.isRegLoc())
1066 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1067 else {
1068 assert(NextVA.isMemLoc());
1069 if (StackPtr.getNode() == 0)
1070 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1071
Dan Gohman98ca4f22009-08-05 01:29:28 +00001072 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1073 dl, DAG, NextVA,
1074 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001075 }
1076}
1077
Dan Gohman98ca4f22009-08-05 01:29:28 +00001078/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001079/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1080/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001081SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001082ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001083 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001084 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001085 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001086 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001087 const SmallVectorImpl<ISD::InputArg> &Ins,
1088 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001089 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001090 MachineFunction &MF = DAG.getMachineFunction();
1091 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1092 bool IsSibCall = false;
Dale Johannesen8fa8e7f2010-06-04 18:04:24 +00001093 // Temporarily disable tail calls so things don't break.
1094 if (!EnableARMTailCalls)
1095 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001096 if (isTailCall) {
1097 // Check if it's really possible to do a tail call.
1098 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1099 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001100 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001101 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1102 // detected sibcalls.
1103 if (isTailCall) {
1104 ++NumTailCalls;
1105 IsSibCall = true;
1106 }
1107 }
Evan Chenga8e29892007-01-19 07:51:42 +00001108
Bob Wilson1f595bb2009-04-17 19:07:39 +00001109 // Analyze operands of the call, assigning locations to each operand.
1110 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001111 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1112 *DAG.getContext());
1113 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001114 CCAssignFnForNode(CallConv, /* Return*/ false,
1115 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001116
Bob Wilson1f595bb2009-04-17 19:07:39 +00001117 // Get a count of how many bytes are to be pushed on the stack.
1118 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001119
Dale Johannesen51e28e62010-06-03 21:09:53 +00001120 // For tail calls, memory operands are available in our caller's stack.
1121 if (IsSibCall)
1122 NumBytes = 0;
1123
Evan Chenga8e29892007-01-19 07:51:42 +00001124 // Adjust the stack pointer for the new arguments...
1125 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001126 if (!IsSibCall)
1127 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001128
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001129 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001130
Bob Wilson5bafff32009-06-22 23:27:02 +00001131 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001132 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001133
Bob Wilson1f595bb2009-04-17 19:07:39 +00001134 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001135 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001136 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1137 i != e;
1138 ++i, ++realArgIdx) {
1139 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001140 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001141 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +00001142
Bob Wilson1f595bb2009-04-17 19:07:39 +00001143 // Promote the value if needed.
1144 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001145 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001146 case CCValAssign::Full: break;
1147 case CCValAssign::SExt:
1148 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1149 break;
1150 case CCValAssign::ZExt:
1151 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1152 break;
1153 case CCValAssign::AExt:
1154 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1155 break;
1156 case CCValAssign::BCvt:
1157 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1158 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001159 }
1160
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001161 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001162 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001163 if (VA.getLocVT() == MVT::v2f64) {
1164 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1165 DAG.getConstant(0, MVT::i32));
1166 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1167 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001168
Dan Gohman98ca4f22009-08-05 01:29:28 +00001169 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001170 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1171
1172 VA = ArgLocs[++i]; // skip ahead to next loc
1173 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001174 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001175 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1176 } else {
1177 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001178
Dan Gohman98ca4f22009-08-05 01:29:28 +00001179 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1180 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001181 }
1182 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001183 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001184 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001185 }
1186 } else if (VA.isRegLoc()) {
1187 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001188 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001189 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001190
Dan Gohman98ca4f22009-08-05 01:29:28 +00001191 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1192 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001193 }
Evan Chenga8e29892007-01-19 07:51:42 +00001194 }
1195
1196 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001197 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001198 &MemOpChains[0], MemOpChains.size());
1199
1200 // Build a sequence of copy-to-reg nodes chained together with token chain
1201 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001202 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001203 // Tail call byval lowering might overwrite argument registers so in case of
1204 // tail call optimization the copies to registers are lowered later.
1205 if (!isTailCall)
1206 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1207 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1208 RegsToPass[i].second, InFlag);
1209 InFlag = Chain.getValue(1);
1210 }
Evan Chenga8e29892007-01-19 07:51:42 +00001211
Dale Johannesen51e28e62010-06-03 21:09:53 +00001212 // For tail calls lower the arguments to the 'real' stack slot.
1213 if (isTailCall) {
1214 // Force all the incoming stack arguments to be loaded from the stack
1215 // before any new outgoing arguments are stored to the stack, because the
1216 // outgoing stack slots may alias the incoming argument stack slots, and
1217 // the alias isn't otherwise explicit. This is slightly more conservative
1218 // than necessary, because it means that each store effectively depends
1219 // on every argument instead of just those arguments it would clobber.
1220
1221 // Do not flag preceeding copytoreg stuff together with the following stuff.
1222 InFlag = SDValue();
1223 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1224 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1225 RegsToPass[i].second, InFlag);
1226 InFlag = Chain.getValue(1);
1227 }
1228 InFlag =SDValue();
1229 }
1230
Bill Wendling056292f2008-09-16 21:48:12 +00001231 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1232 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1233 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001234 bool isDirect = false;
1235 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001236 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001237 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001238
1239 if (EnableARMLongCalls) {
1240 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1241 && "long-calls with non-static relocation model!");
1242 // Handle a global address or an external symbol. If it's not one of
1243 // those, the target's already in a register, so we don't need to do
1244 // anything extra.
1245 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001246 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001247 // Create a constant pool entry for the callee address
1248 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1249 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1250 ARMPCLabelIndex,
1251 ARMCP::CPValue, 0);
1252 // Get the address of the callee into a register
1253 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1254 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1255 Callee = DAG.getLoad(getPointerTy(), dl,
1256 DAG.getEntryNode(), CPAddr,
1257 PseudoSourceValue::getConstantPool(), 0,
1258 false, false, 0);
1259 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1260 const char *Sym = S->getSymbol();
1261
1262 // Create a constant pool entry for the callee address
1263 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1264 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1265 Sym, ARMPCLabelIndex, 0);
1266 // Get the address of the callee into a register
1267 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1268 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1269 Callee = DAG.getLoad(getPointerTy(), dl,
1270 DAG.getEntryNode(), CPAddr,
1271 PseudoSourceValue::getConstantPool(), 0,
1272 false, false, 0);
1273 }
1274 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001275 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001276 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001277 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001278 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001279 getTargetMachine().getRelocationModel() != Reloc::Static;
1280 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001281 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001282 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001283 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001284 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001285 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001286 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001287 ARMPCLabelIndex,
1288 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001289 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001290 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001291 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001292 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001293 PseudoSourceValue::getConstantPool(), 0,
1294 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001295 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001296 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001297 getPointerTy(), Callee, PICLabel);
Jim Grosbache7b52522010-04-14 22:28:31 +00001298 } else
Devang Patel0d881da2010-07-06 22:08:15 +00001299 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001300 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001301 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001302 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001303 getTargetMachine().getRelocationModel() != Reloc::Static;
1304 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001305 // tBX takes a register source operand.
1306 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001307 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001308 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001309 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001310 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001311 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001312 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001313 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001314 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001315 PseudoSourceValue::getConstantPool(), 0,
1316 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001317 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001318 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001319 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001320 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001321 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001322 }
1323
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001324 // FIXME: handle tail calls differently.
1325 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001326 if (Subtarget->isThumb()) {
1327 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001328 CallOpc = ARMISD::CALL_NOLINK;
1329 else
1330 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1331 } else {
1332 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001333 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1334 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001335 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001336
Dan Gohman475871a2008-07-27 21:46:04 +00001337 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001338 Ops.push_back(Chain);
1339 Ops.push_back(Callee);
1340
1341 // Add argument registers to the end of the list so that they are known live
1342 // into the call.
1343 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1344 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1345 RegsToPass[i].second.getValueType()));
1346
Gabor Greifba36cb52008-08-28 21:40:38 +00001347 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001348 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001349
1350 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001351 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001352 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001353
Duncan Sands4bdcb612008-07-02 17:40:58 +00001354 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001355 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001356 InFlag = Chain.getValue(1);
1357
Chris Lattnere563bbc2008-10-11 22:08:30 +00001358 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1359 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001360 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001361 InFlag = Chain.getValue(1);
1362
Bob Wilson1f595bb2009-04-17 19:07:39 +00001363 // Handle result values, copying them out of physregs into vregs that we
1364 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001365 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1366 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001367}
1368
Dale Johannesen51e28e62010-06-03 21:09:53 +00001369/// MatchingStackOffset - Return true if the given stack call argument is
1370/// already available in the same position (relatively) of the caller's
1371/// incoming argument stack.
1372static
1373bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1374 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1375 const ARMInstrInfo *TII) {
1376 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1377 int FI = INT_MAX;
1378 if (Arg.getOpcode() == ISD::CopyFromReg) {
1379 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
1380 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
1381 return false;
1382 MachineInstr *Def = MRI->getVRegDef(VR);
1383 if (!Def)
1384 return false;
1385 if (!Flags.isByVal()) {
1386 if (!TII->isLoadFromStackSlot(Def, FI))
1387 return false;
1388 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001389 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001390 }
1391 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1392 if (Flags.isByVal())
1393 // ByVal argument is passed in as a pointer but it's now being
1394 // dereferenced. e.g.
1395 // define @foo(%struct.X* %A) {
1396 // tail call @bar(%struct.X* byval %A)
1397 // }
1398 return false;
1399 SDValue Ptr = Ld->getBasePtr();
1400 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1401 if (!FINode)
1402 return false;
1403 FI = FINode->getIndex();
1404 } else
1405 return false;
1406
1407 assert(FI != INT_MAX);
1408 if (!MFI->isFixedObjectIndex(FI))
1409 return false;
1410 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1411}
1412
1413/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1414/// for tail call optimization. Targets which want to do tail call
1415/// optimization should implement this function.
1416bool
1417ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1418 CallingConv::ID CalleeCC,
1419 bool isVarArg,
1420 bool isCalleeStructRet,
1421 bool isCallerStructRet,
1422 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001423 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001424 const SmallVectorImpl<ISD::InputArg> &Ins,
1425 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001426 const Function *CallerF = DAG.getMachineFunction().getFunction();
1427 CallingConv::ID CallerCC = CallerF->getCallingConv();
1428 bool CCMatch = CallerCC == CalleeCC;
1429
1430 // Look for obvious safe cases to perform tail call optimization that do not
1431 // require ABI changes. This is what gcc calls sibcall.
1432
Jim Grosbach7616b642010-06-16 23:45:49 +00001433 // Do not sibcall optimize vararg calls unless the call site is not passing
1434 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001435 if (isVarArg && !Outs.empty())
1436 return false;
1437
1438 // Also avoid sibcall optimization if either caller or callee uses struct
1439 // return semantics.
1440 if (isCalleeStructRet || isCallerStructRet)
1441 return false;
1442
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001443 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Evan Cheng0110ac62010-06-19 01:01:32 +00001444 // emitEpilogue is not ready for them.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001445 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1446 // LR. This means if we need to reload LR, it takes an extra instructions,
1447 // which outweighs the value of the tail call; but here we don't know yet
1448 // whether LR is going to be used. Probably the right approach is to
1449 // generate the tail call here and turn it back into CALL/RET in
1450 // emitEpilogue if LR is used.
Evan Cheng0110ac62010-06-19 01:01:32 +00001451 if (Subtarget->isThumb1Only())
1452 return false;
1453
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001454 // For the moment, we can only do this to functions defined in this
1455 // compilation, or to indirect calls. A Thumb B to an ARM function,
1456 // or vice versa, is not easily fixed up in the linker unlike BL.
1457 // (We could do this by loading the address of the callee into a register;
1458 // that is an extra instruction over the direct call and burns a register
1459 // as well, so is not likely to be a win.)
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001460
1461 // It might be safe to remove this restriction on non-Darwin.
1462
1463 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1464 // but we need to make sure there are enough registers; the only valid
1465 // registers are the 4 used for parameters. We don't currently do this
1466 // case.
Evan Cheng0110ac62010-06-19 01:01:32 +00001467 if (isa<ExternalSymbolSDNode>(Callee))
1468 return false;
1469
1470 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001471 const GlobalValue *GV = G->getGlobal();
1472 if (GV->isDeclaration() || GV->isWeakForLinker())
Evan Cheng0110ac62010-06-19 01:01:32 +00001473 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001474 }
1475
Dale Johannesen51e28e62010-06-03 21:09:53 +00001476 // If the calling conventions do not match, then we'd better make sure the
1477 // results are returned in the same way as what the caller expects.
1478 if (!CCMatch) {
1479 SmallVector<CCValAssign, 16> RVLocs1;
1480 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
1481 RVLocs1, *DAG.getContext());
1482 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1483
1484 SmallVector<CCValAssign, 16> RVLocs2;
1485 CCState CCInfo2(CallerCC, false, getTargetMachine(),
1486 RVLocs2, *DAG.getContext());
1487 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1488
1489 if (RVLocs1.size() != RVLocs2.size())
1490 return false;
1491 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1492 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1493 return false;
1494 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1495 return false;
1496 if (RVLocs1[i].isRegLoc()) {
1497 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1498 return false;
1499 } else {
1500 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1501 return false;
1502 }
1503 }
1504 }
1505
1506 // If the callee takes no arguments then go on to check the results of the
1507 // call.
1508 if (!Outs.empty()) {
1509 // Check if stack adjustment is needed. For now, do not do this if any
1510 // argument is passed on the stack.
1511 SmallVector<CCValAssign, 16> ArgLocs;
1512 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
1513 ArgLocs, *DAG.getContext());
1514 CCInfo.AnalyzeCallOperands(Outs,
1515 CCAssignFnForNode(CalleeCC, false, isVarArg));
1516 if (CCInfo.getNextStackOffset()) {
1517 MachineFunction &MF = DAG.getMachineFunction();
1518
1519 // Check if the arguments are already laid out in the right way as
1520 // the caller's fixed stack objects.
1521 MachineFrameInfo *MFI = MF.getFrameInfo();
1522 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1523 const ARMInstrInfo *TII =
1524 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001525 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1526 i != e;
1527 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001528 CCValAssign &VA = ArgLocs[i];
1529 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001530 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001531 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001532 if (VA.getLocInfo() == CCValAssign::Indirect)
1533 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001534 if (VA.needsCustom()) {
1535 // f64 and vector types are split into multiple registers or
1536 // register/stack-slot combinations. The types will not match
1537 // the registers; give up on memory f64 refs until we figure
1538 // out what to do about this.
1539 if (!VA.isRegLoc())
1540 return false;
1541 if (!ArgLocs[++i].isRegLoc())
1542 return false;
1543 if (RegVT == MVT::v2f64) {
1544 if (!ArgLocs[++i].isRegLoc())
1545 return false;
1546 if (!ArgLocs[++i].isRegLoc())
1547 return false;
1548 }
1549 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001550 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1551 MFI, MRI, TII))
1552 return false;
1553 }
1554 }
1555 }
1556 }
1557
1558 return true;
1559}
1560
Dan Gohman98ca4f22009-08-05 01:29:28 +00001561SDValue
1562ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001563 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001564 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001565 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001566 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001567
Bob Wilsondee46d72009-04-17 20:35:10 +00001568 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001569 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001570
Bob Wilsondee46d72009-04-17 20:35:10 +00001571 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001572 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1573 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001574
Dan Gohman98ca4f22009-08-05 01:29:28 +00001575 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001576 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1577 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001578
1579 // If this is the first return lowered for this function, add
1580 // the regs to the liveout set for the function.
1581 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1582 for (unsigned i = 0; i != RVLocs.size(); ++i)
1583 if (RVLocs[i].isRegLoc())
1584 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001585 }
1586
Bob Wilson1f595bb2009-04-17 19:07:39 +00001587 SDValue Flag;
1588
1589 // Copy the result values into the output registers.
1590 for (unsigned i = 0, realRVLocIdx = 0;
1591 i != RVLocs.size();
1592 ++i, ++realRVLocIdx) {
1593 CCValAssign &VA = RVLocs[i];
1594 assert(VA.isRegLoc() && "Can only return in registers!");
1595
Dan Gohmanc9403652010-07-07 15:54:55 +00001596 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001597
1598 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001599 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001600 case CCValAssign::Full: break;
1601 case CCValAssign::BCvt:
1602 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1603 break;
1604 }
1605
Bob Wilson1f595bb2009-04-17 19:07:39 +00001606 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001607 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001608 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001609 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1610 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001611 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001612 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001613
1614 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1615 Flag = Chain.getValue(1);
1616 VA = RVLocs[++i]; // skip ahead to next loc
1617 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1618 HalfGPRs.getValue(1), Flag);
1619 Flag = Chain.getValue(1);
1620 VA = RVLocs[++i]; // skip ahead to next loc
1621
1622 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001623 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1624 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001625 }
1626 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1627 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001628 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001629 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001630 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001631 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001632 VA = RVLocs[++i]; // skip ahead to next loc
1633 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1634 Flag);
1635 } else
1636 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1637
Bob Wilsondee46d72009-04-17 20:35:10 +00001638 // Guarantee that all emitted copies are
1639 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001640 Flag = Chain.getValue(1);
1641 }
1642
1643 SDValue result;
1644 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001645 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001646 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001647 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001648
1649 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001650}
1651
Bob Wilsonb62d2572009-11-03 00:02:05 +00001652// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1653// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1654// one of the above mentioned nodes. It has to be wrapped because otherwise
1655// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1656// be used to form addressing mode. These wrapped nodes will be selected
1657// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001658static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001659 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001660 // FIXME there is no actual debug info here
1661 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001662 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001663 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001664 if (CP->isMachineConstantPoolEntry())
1665 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1666 CP->getAlignment());
1667 else
1668 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1669 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001670 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001671}
1672
Jim Grosbache1102ca2010-07-19 17:20:38 +00001673unsigned ARMTargetLowering::getJumpTableEncoding() const {
1674 return MachineJumpTableInfo::EK_Inline;
1675}
1676
Dan Gohmand858e902010-04-17 15:26:15 +00001677SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1678 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001679 MachineFunction &MF = DAG.getMachineFunction();
1680 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1681 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001682 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001683 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001684 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001685 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1686 SDValue CPAddr;
1687 if (RelocM == Reloc::Static) {
1688 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1689 } else {
1690 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001691 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001692 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1693 ARMCP::CPBlockAddress,
1694 PCAdj);
1695 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1696 }
1697 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1698 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001699 PseudoSourceValue::getConstantPool(), 0,
1700 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001701 if (RelocM == Reloc::Static)
1702 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001703 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001704 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001705}
1706
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001707// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001708SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001709ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001710 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001711 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001712 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001713 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001714 MachineFunction &MF = DAG.getMachineFunction();
1715 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1716 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001717 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001718 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001719 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001720 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001721 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001722 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
David Greene1b58cab2010-02-15 16:55:24 +00001723 PseudoSourceValue::getConstantPool(), 0,
1724 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001725 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001726
Evan Chenge7e0d622009-11-06 22:24:13 +00001727 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001728 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001729
1730 // call __tls_get_addr.
1731 ArgListTy Args;
1732 ArgListEntry Entry;
1733 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001734 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001735 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001736 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001737 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001738 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1739 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001740 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001741 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001742 return CallResult.first;
1743}
1744
1745// Lower ISD::GlobalTLSAddress using the "initial exec" or
1746// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001747SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001748ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001749 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001750 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001751 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001752 SDValue Offset;
1753 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001754 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001755 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001756 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001757
Chris Lattner4fb63d02009-07-15 04:12:33 +00001758 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001759 MachineFunction &MF = DAG.getMachineFunction();
1760 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1761 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1762 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001763 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1764 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001765 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001766 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001767 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001768 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001769 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001770 PseudoSourceValue::getConstantPool(), 0,
1771 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001772 Chain = Offset.getValue(1);
1773
Evan Chenge7e0d622009-11-06 22:24:13 +00001774 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001775 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001776
Evan Cheng9eda6892009-10-31 03:39:36 +00001777 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001778 PseudoSourceValue::getConstantPool(), 0,
1779 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001780 } else {
1781 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001782 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001783 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001784 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001785 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001786 PseudoSourceValue::getConstantPool(), 0,
1787 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001788 }
1789
1790 // The address of the thread local variable is the add of the thread
1791 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001792 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001793}
1794
Dan Gohman475871a2008-07-27 21:46:04 +00001795SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001796ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001797 // TODO: implement the "local dynamic" model
1798 assert(Subtarget->isTargetELF() &&
1799 "TLS not implemented for non-ELF targets");
1800 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1801 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1802 // otherwise use the "Local Exec" TLS Model
1803 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1804 return LowerToTLSGeneralDynamicModel(GA, DAG);
1805 else
1806 return LowerToTLSExecModels(GA, DAG);
1807}
1808
Dan Gohman475871a2008-07-27 21:46:04 +00001809SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001810 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001811 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001812 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001813 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001814 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1815 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001816 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001817 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001818 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001819 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001820 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001821 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001822 CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001823 PseudoSourceValue::getConstantPool(), 0,
1824 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001825 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001826 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001827 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001828 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001829 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001830 PseudoSourceValue::getGOT(), 0,
1831 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001832 return Result;
1833 } else {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001834 // If we have T2 ops, we can materialize the address directly via movt/movw
1835 // pair. This is always cheaper.
1836 if (Subtarget->useMovt()) {
1837 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
Devang Patel0d881da2010-07-06 22:08:15 +00001838 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001839 } else {
1840 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1841 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1842 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001843 PseudoSourceValue::getConstantPool(), 0,
1844 false, false, 0);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001845 }
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001846 }
1847}
1848
Dan Gohman475871a2008-07-27 21:46:04 +00001849SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001850 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001851 MachineFunction &MF = DAG.getMachineFunction();
1852 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1853 unsigned ARMPCLabelIndex = 0;
Owen Andersone50ed302009-08-10 22:56:29 +00001854 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001855 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001856 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001857 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001858 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001859 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001860 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001861 else {
Evan Chenge7e0d622009-11-06 22:24:13 +00001862 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001863 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1864 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001865 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001866 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001867 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001868 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001869
Evan Cheng9eda6892009-10-31 03:39:36 +00001870 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001871 PseudoSourceValue::getConstantPool(), 0,
1872 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001873 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001874
1875 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001876 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001877 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001878 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001879
Evan Cheng63476a82009-09-03 07:04:02 +00001880 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Evan Cheng9eda6892009-10-31 03:39:36 +00001881 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001882 PseudoSourceValue::getGOT(), 0,
1883 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001884
1885 return Result;
1886}
1887
Dan Gohman475871a2008-07-27 21:46:04 +00001888SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001889 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001890 assert(Subtarget->isTargetELF() &&
1891 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00001892 MachineFunction &MF = DAG.getMachineFunction();
1893 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1894 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Andersone50ed302009-08-10 22:56:29 +00001895 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001896 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001897 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001898 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1899 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001900 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001901 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001902 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001903 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001904 PseudoSourceValue::getConstantPool(), 0,
1905 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001906 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001907 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001908}
1909
Jim Grosbach0e0da732009-05-12 23:59:14 +00001910SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00001911ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
1912 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00001913 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00001914 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
1915 Op.getOperand(1), Val);
1916}
1917
1918SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00001919ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
1920 DebugLoc dl = Op.getDebugLoc();
1921 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
1922 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
1923}
1924
1925SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00001926ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00001927 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001928 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001929 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001930 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001931 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001932 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001933 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001934 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1935 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001936 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001937 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00001938 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1939 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001940 EVT PtrVT = getPointerTy();
1941 DebugLoc dl = Op.getDebugLoc();
1942 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1943 SDValue CPAddr;
1944 unsigned PCAdj = (RelocM != Reloc::PIC_)
1945 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001946 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001947 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1948 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001949 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001950 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001951 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00001952 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001953 PseudoSourceValue::getConstantPool(), 0,
1954 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001955
1956 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001957 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001958 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1959 }
1960 return Result;
1961 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001962 }
1963}
1964
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001965static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00001966 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00001967 DebugLoc dl = Op.getDebugLoc();
1968 SDValue Op5 = Op.getOperand(5);
Jim Grosbach3728e962009-12-10 00:11:09 +00001969 unsigned isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue();
Jim Grosbachc73993b2010-06-17 01:37:00 +00001970 // v6 and v7 can both handle barriers directly, but need handled a bit
1971 // differently. Thumb1 and pre-v6 ARM mode use a libcall instead and should
1972 // never get here.
1973 unsigned Opc = isDeviceBarrier ? ARMISD::SYNCBARRIER : ARMISD::MEMBARRIER;
1974 if (Subtarget->hasV7Ops())
1975 return DAG.getNode(Opc, dl, MVT::Other, Op.getOperand(0));
1976 else if (Subtarget->hasV6Ops() && !Subtarget->isThumb1Only())
1977 return DAG.getNode(Opc, dl, MVT::Other, Op.getOperand(0),
1978 DAG.getConstant(0, MVT::i32));
1979 assert(0 && "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
1980 return SDValue();
Jim Grosbach3728e962009-12-10 00:11:09 +00001981}
1982
Dan Gohman1e93df62010-04-17 14:41:14 +00001983static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
1984 MachineFunction &MF = DAG.getMachineFunction();
1985 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
1986
Evan Chenga8e29892007-01-19 07:51:42 +00001987 // vastart just stores the address of the VarArgsFrameIndex slot into the
1988 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001989 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001990 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00001991 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001992 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
David Greene1b58cab2010-02-15 16:55:24 +00001993 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
1994 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001995}
1996
Dan Gohman475871a2008-07-27 21:46:04 +00001997SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001998ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1999 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002000 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002001 MachineFunction &MF = DAG.getMachineFunction();
2002 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2003
2004 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002005 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002006 RC = ARM::tGPRRegisterClass;
2007 else
2008 RC = ARM::GPRRegisterClass;
2009
2010 // Transform the arguments stored in physical registers into virtual ones.
Evan Cheng2457f2c2010-05-22 01:47:14 +00002011 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002012 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002013
2014 SDValue ArgValue2;
2015 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002016 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002017 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002018
2019 // Create load node to retrieve arguments from the stack.
2020 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002021 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00002022 PseudoSourceValue::getFixedStack(FI), 0,
2023 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002024 } else {
2025 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002026 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002027 }
2028
Jim Grosbache5165492009-11-09 00:11:35 +00002029 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002030}
2031
2032SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002033ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002034 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002035 const SmallVectorImpl<ISD::InputArg>
2036 &Ins,
2037 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002038 SmallVectorImpl<SDValue> &InVals)
2039 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002040
Bob Wilson1f595bb2009-04-17 19:07:39 +00002041 MachineFunction &MF = DAG.getMachineFunction();
2042 MachineFrameInfo *MFI = MF.getFrameInfo();
2043
Bob Wilson1f595bb2009-04-17 19:07:39 +00002044 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2045
2046 // Assign locations to all of the incoming arguments.
2047 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002048 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
2049 *DAG.getContext());
2050 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002051 CCAssignFnForNode(CallConv, /* Return*/ false,
2052 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002053
2054 SmallVector<SDValue, 16> ArgValues;
2055
2056 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2057 CCValAssign &VA = ArgLocs[i];
2058
Bob Wilsondee46d72009-04-17 20:35:10 +00002059 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002060 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002061 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002062
Bob Wilson5bafff32009-06-22 23:27:02 +00002063 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002064 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002065 // f64 and vector types are split up into multiple registers or
2066 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002067 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002068 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002069 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002070 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002071 SDValue ArgValue2;
2072 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002073 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002074 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2075 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
2076 PseudoSourceValue::getFixedStack(FI), 0,
2077 false, false, 0);
2078 } else {
2079 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2080 Chain, DAG, dl);
2081 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002082 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2083 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002084 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002085 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002086 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2087 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002088 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002089
Bob Wilson5bafff32009-06-22 23:27:02 +00002090 } else {
2091 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002092
Owen Anderson825b72b2009-08-11 20:47:22 +00002093 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002094 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002095 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002096 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002097 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002098 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002099 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002100 RC = (AFI->isThumb1OnlyFunction() ?
2101 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002102 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002103 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002104
2105 // Transform the arguments in physical registers into virtual ones.
2106 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002107 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002108 }
2109
2110 // If this is an 8 or 16-bit value, it is really passed promoted
2111 // to 32 bits. Insert an assert[sz]ext to capture this, then
2112 // truncate to the right size.
2113 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002114 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002115 case CCValAssign::Full: break;
2116 case CCValAssign::BCvt:
2117 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
2118 break;
2119 case CCValAssign::SExt:
2120 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2121 DAG.getValueType(VA.getValVT()));
2122 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2123 break;
2124 case CCValAssign::ZExt:
2125 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2126 DAG.getValueType(VA.getValVT()));
2127 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2128 break;
2129 }
2130
Dan Gohman98ca4f22009-08-05 01:29:28 +00002131 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002132
2133 } else { // VA.isRegLoc()
2134
2135 // sanity check
2136 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002137 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002138
2139 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002140 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002141
Bob Wilsondee46d72009-04-17 20:35:10 +00002142 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002143 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002144 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00002145 PseudoSourceValue::getFixedStack(FI), 0,
2146 false, false, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002147 }
2148 }
2149
2150 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00002151 if (isVarArg) {
2152 static const unsigned GPRArgRegs[] = {
2153 ARM::R0, ARM::R1, ARM::R2, ARM::R3
2154 };
2155
Bob Wilsondee46d72009-04-17 20:35:10 +00002156 unsigned NumGPRs = CCInfo.getFirstUnallocated
2157 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002158
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00002159 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
2160 unsigned VARegSize = (4 - NumGPRs) * 4;
2161 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00002162 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00002163 if (VARegSaveSize) {
2164 // If this function is vararg, store any remaining integer argument regs
2165 // to their spots on the stack so that they may be loaded by deferencing
2166 // the result of va_next.
2167 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00002168 AFI->setVarArgsFrameIndex(
2169 MFI->CreateFixedObject(VARegSaveSize,
2170 ArgOffset + VARegSaveSize - VARegSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002171 true));
Dan Gohman1e93df62010-04-17 14:41:14 +00002172 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2173 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00002174
Dan Gohman475871a2008-07-27 21:46:04 +00002175 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00002176 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002177 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002178 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00002179 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002180 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00002181 RC = ARM::GPRRegisterClass;
2182
Bob Wilson998e1252009-04-20 18:36:57 +00002183 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002184 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00002185 SDValue Store =
2186 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Jim Grosbach18f30e62010-06-02 21:53:11 +00002187 PseudoSourceValue::getFixedStack(AFI->getVarArgsFrameIndex()),
2188 0, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002189 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002190 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00002191 DAG.getConstant(4, getPointerTy()));
2192 }
2193 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002194 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002195 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00002196 } else
2197 // This will point to the next argument passed via stack.
Evan Chenged2ae132010-07-03 00:40:23 +00002198 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
Evan Chenga8e29892007-01-19 07:51:42 +00002199 }
2200
Dan Gohman98ca4f22009-08-05 01:29:28 +00002201 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002202}
2203
2204/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002205static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002206 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002207 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002208 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002209 // Maybe this has already been legalized into the constant pool?
2210 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002211 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002212 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002213 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002214 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002215 }
2216 }
2217 return false;
2218}
2219
Evan Chenga8e29892007-01-19 07:51:42 +00002220/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2221/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002222SDValue
2223ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002224 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002225 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002226 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002227 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002228 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002229 // Constant does not fit, try adjusting it by one?
2230 switch (CC) {
2231 default: break;
2232 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002233 case ISD::SETGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00002234 if (isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002235 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002236 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002237 }
2238 break;
2239 case ISD::SETULT:
2240 case ISD::SETUGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00002241 if (C > 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002242 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002243 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002244 }
2245 break;
2246 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002247 case ISD::SETGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00002248 if (isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002249 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002250 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002251 }
2252 break;
2253 case ISD::SETULE:
2254 case ISD::SETUGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00002255 if (C < 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002256 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002257 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002258 }
2259 break;
2260 }
2261 }
2262 }
2263
2264 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002265 ARMISD::NodeType CompareType;
2266 switch (CondCode) {
2267 default:
2268 CompareType = ARMISD::CMP;
2269 break;
2270 case ARMCC::EQ:
2271 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002272 // Uses only Z Flag
2273 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002274 break;
2275 }
Evan Cheng218977b2010-07-13 19:27:42 +00002276 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002277 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002278}
2279
2280/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002281SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002282ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002283 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002284 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002285 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00002286 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002287 else
Owen Anderson825b72b2009-08-11 20:47:22 +00002288 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
2289 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002290}
2291
Dan Gohmand858e902010-04-17 15:26:15 +00002292SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002293 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002294 SDValue LHS = Op.getOperand(0);
2295 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002296 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002297 SDValue TrueVal = Op.getOperand(2);
2298 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002299 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002300
Owen Anderson825b72b2009-08-11 20:47:22 +00002301 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002302 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002303 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002304 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2305 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002306 }
2307
2308 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002309 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002310
Evan Cheng218977b2010-07-13 19:27:42 +00002311 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2312 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002313 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002314 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002315 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002316 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002317 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002318 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002319 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002320 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002321 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002322 }
2323 return Result;
2324}
2325
Evan Cheng218977b2010-07-13 19:27:42 +00002326/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2327/// to morph to an integer compare sequence.
2328static bool canChangeToInt(SDValue Op, bool &SeenZero,
2329 const ARMSubtarget *Subtarget) {
2330 SDNode *N = Op.getNode();
2331 if (!N->hasOneUse())
2332 // Otherwise it requires moving the value from fp to integer registers.
2333 return false;
2334 if (!N->getNumValues())
2335 return false;
2336 EVT VT = Op.getValueType();
2337 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2338 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2339 // vmrs are very slow, e.g. cortex-a8.
2340 return false;
2341
2342 if (isFloatingPointZero(Op)) {
2343 SeenZero = true;
2344 return true;
2345 }
2346 return ISD::isNormalLoad(N);
2347}
2348
2349static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2350 if (isFloatingPointZero(Op))
2351 return DAG.getConstant(0, MVT::i32);
2352
2353 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2354 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2355 Ld->getChain(), Ld->getBasePtr(),
2356 Ld->getSrcValue(), Ld->getSrcValueOffset(),
2357 Ld->isVolatile(), Ld->isNonTemporal(),
2358 Ld->getAlignment());
2359
2360 llvm_unreachable("Unknown VFP cmp argument!");
2361}
2362
2363static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2364 SDValue &RetVal1, SDValue &RetVal2) {
2365 if (isFloatingPointZero(Op)) {
2366 RetVal1 = DAG.getConstant(0, MVT::i32);
2367 RetVal2 = DAG.getConstant(0, MVT::i32);
2368 return;
2369 }
2370
2371 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2372 SDValue Ptr = Ld->getBasePtr();
2373 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2374 Ld->getChain(), Ptr,
2375 Ld->getSrcValue(), Ld->getSrcValueOffset(),
2376 Ld->isVolatile(), Ld->isNonTemporal(),
2377 Ld->getAlignment());
2378
2379 EVT PtrType = Ptr.getValueType();
2380 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2381 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2382 PtrType, Ptr, DAG.getConstant(4, PtrType));
2383 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2384 Ld->getChain(), NewPtr,
2385 Ld->getSrcValue(), Ld->getSrcValueOffset() + 4,
2386 Ld->isVolatile(), Ld->isNonTemporal(),
2387 NewAlign);
2388 return;
2389 }
2390
2391 llvm_unreachable("Unknown VFP cmp argument!");
2392}
2393
2394/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2395/// f32 and even f64 comparisons to integer ones.
2396SDValue
2397ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2398 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002399 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002400 SDValue LHS = Op.getOperand(2);
2401 SDValue RHS = Op.getOperand(3);
2402 SDValue Dest = Op.getOperand(4);
2403 DebugLoc dl = Op.getDebugLoc();
2404
2405 bool SeenZero = false;
2406 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2407 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002408 // If one of the operand is zero, it's safe to ignore the NaN case since
2409 // we only care about equality comparisons.
2410 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Evan Cheng218977b2010-07-13 19:27:42 +00002411 // If unsafe fp math optimization is enabled and there are no othter uses of
2412 // the CMP operands, and the condition code is EQ oe NE, we can optimize it
2413 // to an integer comparison.
2414 if (CC == ISD::SETOEQ)
2415 CC = ISD::SETEQ;
2416 else if (CC == ISD::SETUNE)
2417 CC = ISD::SETNE;
2418
2419 SDValue ARMcc;
2420 if (LHS.getValueType() == MVT::f32) {
2421 LHS = bitcastf32Toi32(LHS, DAG);
2422 RHS = bitcastf32Toi32(RHS, DAG);
2423 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2424 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2425 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2426 Chain, Dest, ARMcc, CCR, Cmp);
2427 }
2428
2429 SDValue LHS1, LHS2;
2430 SDValue RHS1, RHS2;
2431 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2432 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2433 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2434 ARMcc = DAG.getConstant(CondCode, MVT::i32);
2435 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
2436 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2437 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2438 }
2439
2440 return SDValue();
2441}
2442
2443SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2444 SDValue Chain = Op.getOperand(0);
2445 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2446 SDValue LHS = Op.getOperand(2);
2447 SDValue RHS = Op.getOperand(3);
2448 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002449 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002450
Owen Anderson825b72b2009-08-11 20:47:22 +00002451 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002452 SDValue ARMcc;
2453 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002454 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002455 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002456 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002457 }
2458
Owen Anderson825b72b2009-08-11 20:47:22 +00002459 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002460
2461 if (UnsafeFPMath &&
2462 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2463 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2464 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2465 if (Result.getNode())
2466 return Result;
2467 }
2468
Evan Chenga8e29892007-01-19 07:51:42 +00002469 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002470 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002471
Evan Cheng218977b2010-07-13 19:27:42 +00002472 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2473 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002474 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2475 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng218977b2010-07-13 19:27:42 +00002476 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002477 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002478 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002479 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2480 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002481 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002482 }
2483 return Res;
2484}
2485
Dan Gohmand858e902010-04-17 15:26:15 +00002486SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002487 SDValue Chain = Op.getOperand(0);
2488 SDValue Table = Op.getOperand(1);
2489 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002490 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002491
Owen Andersone50ed302009-08-10 22:56:29 +00002492 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002493 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2494 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002495 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002496 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002497 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002498 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2499 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002500 if (Subtarget->isThumb2()) {
2501 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2502 // which does another jump to the destination. This also makes it easier
2503 // to translate it to TBB / TBH later.
2504 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002505 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002506 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002507 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002508 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002509 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002510 PseudoSourceValue::getJumpTable(), 0,
2511 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002512 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002513 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002514 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002515 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002516 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002517 PseudoSourceValue::getJumpTable(), 0, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002518 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002519 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002520 }
Evan Chenga8e29892007-01-19 07:51:42 +00002521}
2522
Bob Wilson76a312b2010-03-19 22:51:32 +00002523static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2524 DebugLoc dl = Op.getDebugLoc();
2525 unsigned Opc;
2526
2527 switch (Op.getOpcode()) {
2528 default:
2529 assert(0 && "Invalid opcode!");
2530 case ISD::FP_TO_SINT:
2531 Opc = ARMISD::FTOSI;
2532 break;
2533 case ISD::FP_TO_UINT:
2534 Opc = ARMISD::FTOUI;
2535 break;
2536 }
2537 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
2538 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
2539}
2540
2541static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2542 EVT VT = Op.getValueType();
2543 DebugLoc dl = Op.getDebugLoc();
2544 unsigned Opc;
2545
2546 switch (Op.getOpcode()) {
2547 default:
2548 assert(0 && "Invalid opcode!");
2549 case ISD::SINT_TO_FP:
2550 Opc = ARMISD::SITOF;
2551 break;
2552 case ISD::UINT_TO_FP:
2553 Opc = ARMISD::UITOF;
2554 break;
2555 }
2556
2557 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
2558 return DAG.getNode(Opc, dl, VT, Op);
2559}
2560
Evan Cheng515fe3a2010-07-08 02:08:50 +00002561SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002562 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002563 SDValue Tmp0 = Op.getOperand(0);
2564 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002565 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002566 EVT VT = Op.getValueType();
2567 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00002568 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
Evan Cheng218977b2010-07-13 19:27:42 +00002569 SDValue ARMcc = DAG.getConstant(ARMCC::LT, MVT::i32);
Evan Cheng515fe3a2010-07-08 02:08:50 +00002570 SDValue FP0 = DAG.getConstantFP(0.0, SrcVT);
Evan Cheng218977b2010-07-13 19:27:42 +00002571 SDValue Cmp = getVFPCmp(Tmp1, FP0, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002572 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002573 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002574}
2575
Evan Cheng2457f2c2010-05-22 01:47:14 +00002576SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
2577 MachineFunction &MF = DAG.getMachineFunction();
2578 MachineFrameInfo *MFI = MF.getFrameInfo();
2579 MFI->setReturnAddressIsTaken(true);
2580
2581 EVT VT = Op.getValueType();
2582 DebugLoc dl = Op.getDebugLoc();
2583 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2584 if (Depth) {
2585 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
2586 SDValue Offset = DAG.getConstant(4, MVT::i32);
2587 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
2588 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
2589 NULL, 0, false, false, 0);
2590 }
2591
2592 // Return LR, which contains the return address. Mark it an implicit live-in.
Evan Chengc7cf10c2010-05-24 18:00:18 +00002593 unsigned Reg = MF.addLiveIn(ARM::LR, ARM::GPRRegisterClass);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002594 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
2595}
2596
Dan Gohmand858e902010-04-17 15:26:15 +00002597SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002598 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2599 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002600
Owen Andersone50ed302009-08-10 22:56:29 +00002601 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002602 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2603 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002604 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002605 ? ARM::R7 : ARM::R11;
2606 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2607 while (Depth--)
David Greene1b58cab2010-02-15 16:55:24 +00002608 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
2609 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002610 return FrameAddr;
2611}
2612
Bob Wilson9f3f0612010-04-17 05:30:19 +00002613/// ExpandBIT_CONVERT - If the target supports VFP, this function is called to
2614/// expand a bit convert where either the source or destination type is i64 to
2615/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2616/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2617/// vectors), since the legalizer won't know what to do with that.
Duncan Sands1607f052008-12-01 11:39:25 +00002618static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002619 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2620 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002621 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002622
Bob Wilson9f3f0612010-04-17 05:30:19 +00002623 // This function is only supposed to be called for i64 types, either as the
2624 // source or destination of the bit convert.
2625 EVT SrcVT = Op.getValueType();
2626 EVT DstVT = N->getValueType(0);
2627 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
2628 "ExpandBIT_CONVERT called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002629
Bob Wilson9f3f0612010-04-17 05:30:19 +00002630 // Turn i64->f64 into VMOVDRR.
2631 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002632 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2633 DAG.getConstant(0, MVT::i32));
2634 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2635 DAG.getConstant(1, MVT::i32));
Bob Wilson1114f562010-06-11 22:45:25 +00002636 return DAG.getNode(ISD::BIT_CONVERT, dl, DstVT,
2637 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00002638 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002639
Jim Grosbache5165492009-11-09 00:11:35 +00002640 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002641 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2642 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2643 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2644 // Merge the pieces into a single i64 value.
2645 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2646 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002647
Bob Wilson9f3f0612010-04-17 05:30:19 +00002648 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002649}
2650
Bob Wilson5bafff32009-06-22 23:27:02 +00002651/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00002652/// Zero vectors are used to represent vector negation and in those cases
2653/// will be implemented with the NEON VNEG instruction. However, VNEG does
2654/// not support i64 elements, so sometimes the zero vectors will need to be
2655/// explicitly constructed. Regardless, use a canonical VMOV to create the
2656/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002657static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002658 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00002659 // The canonical modified immediate encoding of a zero vector is....0!
2660 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
2661 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
2662 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
2663 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00002664}
2665
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002666/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2667/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002668SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2669 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002670 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2671 EVT VT = Op.getValueType();
2672 unsigned VTBits = VT.getSizeInBits();
2673 DebugLoc dl = Op.getDebugLoc();
2674 SDValue ShOpLo = Op.getOperand(0);
2675 SDValue ShOpHi = Op.getOperand(1);
2676 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00002677 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002678 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002679
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002680 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2681
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002682 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2683 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2684 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2685 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2686 DAG.getConstant(VTBits, MVT::i32));
2687 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2688 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002689 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002690
2691 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2692 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00002693 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002694 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00002695 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002696 CCR, Cmp);
2697
2698 SDValue Ops[2] = { Lo, Hi };
2699 return DAG.getMergeValues(Ops, 2, dl);
2700}
2701
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002702/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2703/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002704SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2705 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002706 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2707 EVT VT = Op.getValueType();
2708 unsigned VTBits = VT.getSizeInBits();
2709 DebugLoc dl = Op.getDebugLoc();
2710 SDValue ShOpLo = Op.getOperand(0);
2711 SDValue ShOpHi = Op.getOperand(1);
2712 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00002713 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002714
2715 assert(Op.getOpcode() == ISD::SHL_PARTS);
2716 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2717 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2718 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2719 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2720 DAG.getConstant(VTBits, MVT::i32));
2721 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2722 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2723
2724 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2725 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2726 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00002727 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002728 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00002729 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002730 CCR, Cmp);
2731
2732 SDValue Ops[2] = { Lo, Hi };
2733 return DAG.getMergeValues(Ops, 2, dl);
2734}
2735
Jim Grosbach3482c802010-01-18 19:58:49 +00002736static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
2737 const ARMSubtarget *ST) {
2738 EVT VT = N->getValueType(0);
2739 DebugLoc dl = N->getDebugLoc();
2740
2741 if (!ST->hasV6T2Ops())
2742 return SDValue();
2743
2744 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
2745 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
2746}
2747
Bob Wilson5bafff32009-06-22 23:27:02 +00002748static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2749 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002750 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002751 DebugLoc dl = N->getDebugLoc();
2752
2753 // Lower vector shifts on NEON to use VSHL.
2754 if (VT.isVector()) {
2755 assert(ST->hasNEON() && "unexpected vector shift");
2756
2757 // Left shifts translate directly to the vshiftu intrinsic.
2758 if (N->getOpcode() == ISD::SHL)
2759 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002760 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002761 N->getOperand(0), N->getOperand(1));
2762
2763 assert((N->getOpcode() == ISD::SRA ||
2764 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2765
2766 // NEON uses the same intrinsics for both left and right shifts. For
2767 // right shifts, the shift amounts are negative, so negate the vector of
2768 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002769 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002770 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2771 getZeroVector(ShiftVT, DAG, dl),
2772 N->getOperand(1));
2773 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2774 Intrinsic::arm_neon_vshifts :
2775 Intrinsic::arm_neon_vshiftu);
2776 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002777 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002778 N->getOperand(0), NegatedCount);
2779 }
2780
Eli Friedmance392eb2009-08-22 03:13:10 +00002781 // We can get here for a node like i32 = ISD::SHL i32, i64
2782 if (VT != MVT::i64)
2783 return SDValue();
2784
2785 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002786 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002787
Chris Lattner27a6c732007-11-24 07:07:01 +00002788 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2789 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002790 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002791 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002792
Chris Lattner27a6c732007-11-24 07:07:01 +00002793 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002794 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002795
Chris Lattner27a6c732007-11-24 07:07:01 +00002796 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002797 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00002798 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00002799 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00002800 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002801
Chris Lattner27a6c732007-11-24 07:07:01 +00002802 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2803 // captures the result into a carry flag.
2804 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002805 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002806
Chris Lattner27a6c732007-11-24 07:07:01 +00002807 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002808 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002809
Chris Lattner27a6c732007-11-24 07:07:01 +00002810 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002811 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002812}
2813
Bob Wilson5bafff32009-06-22 23:27:02 +00002814static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2815 SDValue TmpOp0, TmpOp1;
2816 bool Invert = false;
2817 bool Swap = false;
2818 unsigned Opc = 0;
2819
2820 SDValue Op0 = Op.getOperand(0);
2821 SDValue Op1 = Op.getOperand(1);
2822 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002823 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002824 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2825 DebugLoc dl = Op.getDebugLoc();
2826
2827 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2828 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002829 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002830 case ISD::SETUNE:
2831 case ISD::SETNE: Invert = true; // Fallthrough
2832 case ISD::SETOEQ:
2833 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2834 case ISD::SETOLT:
2835 case ISD::SETLT: Swap = true; // Fallthrough
2836 case ISD::SETOGT:
2837 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2838 case ISD::SETOLE:
2839 case ISD::SETLE: Swap = true; // Fallthrough
2840 case ISD::SETOGE:
2841 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2842 case ISD::SETUGE: Swap = true; // Fallthrough
2843 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2844 case ISD::SETUGT: Swap = true; // Fallthrough
2845 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2846 case ISD::SETUEQ: Invert = true; // Fallthrough
2847 case ISD::SETONE:
2848 // Expand this to (OLT | OGT).
2849 TmpOp0 = Op0;
2850 TmpOp1 = Op1;
2851 Opc = ISD::OR;
2852 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2853 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2854 break;
2855 case ISD::SETUO: Invert = true; // Fallthrough
2856 case ISD::SETO:
2857 // Expand this to (OLT | OGE).
2858 TmpOp0 = Op0;
2859 TmpOp1 = Op1;
2860 Opc = ISD::OR;
2861 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2862 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2863 break;
2864 }
2865 } else {
2866 // Integer comparisons.
2867 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002868 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002869 case ISD::SETNE: Invert = true;
2870 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2871 case ISD::SETLT: Swap = true;
2872 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2873 case ISD::SETLE: Swap = true;
2874 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2875 case ISD::SETULT: Swap = true;
2876 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2877 case ISD::SETULE: Swap = true;
2878 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2879 }
2880
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002881 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002882 if (Opc == ARMISD::VCEQ) {
2883
2884 SDValue AndOp;
2885 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2886 AndOp = Op0;
2887 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2888 AndOp = Op1;
2889
2890 // Ignore bitconvert.
2891 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2892 AndOp = AndOp.getOperand(0);
2893
2894 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2895 Opc = ARMISD::VTST;
2896 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2897 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2898 Invert = !Invert;
2899 }
2900 }
2901 }
2902
2903 if (Swap)
2904 std::swap(Op0, Op1);
2905
2906 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2907
2908 if (Invert)
2909 Result = DAG.getNOT(dl, Result, VT);
2910
2911 return Result;
2912}
2913
Bob Wilsond3c42842010-06-14 22:19:57 +00002914/// isNEONModifiedImm - Check if the specified splat value corresponds to a
2915/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00002916/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00002917static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
2918 unsigned SplatBitSize, SelectionDAG &DAG,
Bob Wilsoncba270d2010-07-13 21:16:48 +00002919 EVT &VT, bool is128Bits, bool isVMOV) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00002920 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002921
Bob Wilson827b2102010-06-15 19:05:35 +00002922 // SplatBitSize is set to the smallest size that splats the vector, so a
2923 // zero vector will always have SplatBitSize == 8. However, NEON modified
2924 // immediate instructions others than VMOV do not support the 8-bit encoding
2925 // of a zero vector, and the default encoding of zero is supposed to be the
2926 // 32-bit version.
2927 if (SplatBits == 0)
2928 SplatBitSize = 32;
2929
Bob Wilson5bafff32009-06-22 23:27:02 +00002930 switch (SplatBitSize) {
2931 case 8:
Bob Wilson7e3f0d22010-07-14 06:31:50 +00002932 if (!isVMOV)
2933 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00002934 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00002935 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00002936 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002937 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00002938 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002939 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002940
2941 case 16:
2942 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00002943 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002944 if ((SplatBits & ~0xff) == 0) {
2945 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00002946 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002947 Imm = SplatBits;
2948 break;
2949 }
2950 if ((SplatBits & ~0xff00) == 0) {
2951 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00002952 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002953 Imm = SplatBits >> 8;
2954 break;
2955 }
2956 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002957
2958 case 32:
2959 // NEON's 32-bit VMOV supports splat values where:
2960 // * only one byte is nonzero, or
2961 // * the least significant byte is 0xff and the second byte is nonzero, or
2962 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00002963 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002964 if ((SplatBits & ~0xff) == 0) {
2965 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00002966 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002967 Imm = SplatBits;
2968 break;
2969 }
2970 if ((SplatBits & ~0xff00) == 0) {
2971 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00002972 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002973 Imm = SplatBits >> 8;
2974 break;
2975 }
2976 if ((SplatBits & ~0xff0000) == 0) {
2977 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00002978 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002979 Imm = SplatBits >> 16;
2980 break;
2981 }
2982 if ((SplatBits & ~0xff000000) == 0) {
2983 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00002984 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002985 Imm = SplatBits >> 24;
2986 break;
2987 }
Bob Wilson5bafff32009-06-22 23:27:02 +00002988
2989 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00002990 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
2991 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00002992 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002993 Imm = SplatBits >> 8;
2994 SplatBits |= 0xff;
2995 break;
2996 }
Bob Wilson5bafff32009-06-22 23:27:02 +00002997
2998 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00002999 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3000 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003001 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003002 Imm = SplatBits >> 16;
3003 SplatBits |= 0xffff;
3004 break;
3005 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003006
3007 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3008 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3009 // VMOV.I32. A (very) minor optimization would be to replicate the value
3010 // and fall through here to test for a valid 64-bit splat. But, then the
3011 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003012 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003013
3014 case 64: {
Bob Wilson827b2102010-06-15 19:05:35 +00003015 if (!isVMOV)
3016 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003017 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003018 uint64_t BitMask = 0xff;
3019 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003020 unsigned ImmMask = 1;
3021 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003022 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003023 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003024 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003025 Imm |= ImmMask;
3026 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003027 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003028 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003029 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003030 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003031 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003032 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003033 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003034 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003035 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003036 break;
3037 }
3038
Bob Wilson1a913ed2010-06-11 21:34:50 +00003039 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003040 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003041 return SDValue();
3042 }
3043
Bob Wilsoncba270d2010-07-13 21:16:48 +00003044 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3045 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003046}
3047
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003048static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3049 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003050 unsigned NumElts = VT.getVectorNumElements();
3051 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003052 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003053
3054 // If this is a VEXT shuffle, the immediate value is the index of the first
3055 // element. The other shuffle indices must be the successive elements after
3056 // the first one.
3057 unsigned ExpectedElt = Imm;
3058 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003059 // Increment the expected index. If it wraps around, it may still be
3060 // a VEXT but the source vectors must be swapped.
3061 ExpectedElt += 1;
3062 if (ExpectedElt == NumElts * 2) {
3063 ExpectedElt = 0;
3064 ReverseVEXT = true;
3065 }
3066
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003067 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003068 return false;
3069 }
3070
3071 // Adjust the index value if the source operands will be swapped.
3072 if (ReverseVEXT)
3073 Imm -= NumElts;
3074
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003075 return true;
3076}
3077
Bob Wilson8bb9e482009-07-26 00:39:34 +00003078/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3079/// instruction with the specified blocksize. (The order of the elements
3080/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003081static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3082 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003083 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3084 "Only possible block sizes for VREV are: 16, 32, 64");
3085
Bob Wilson8bb9e482009-07-26 00:39:34 +00003086 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003087 if (EltSz == 64)
3088 return false;
3089
3090 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003091 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003092
3093 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3094 return false;
3095
3096 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003097 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00003098 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
3099 return false;
3100 }
3101
3102 return true;
3103}
3104
Bob Wilsonc692cb72009-08-21 20:54:19 +00003105static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3106 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003107 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3108 if (EltSz == 64)
3109 return false;
3110
Bob Wilsonc692cb72009-08-21 20:54:19 +00003111 unsigned NumElts = VT.getVectorNumElements();
3112 WhichResult = (M[0] == 0 ? 0 : 1);
3113 for (unsigned i = 0; i < NumElts; i += 2) {
3114 if ((unsigned) M[i] != i + WhichResult ||
3115 (unsigned) M[i+1] != i + NumElts + WhichResult)
3116 return false;
3117 }
3118 return true;
3119}
3120
Bob Wilson324f4f12009-12-03 06:40:55 +00003121/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3122/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3123/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3124static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3125 unsigned &WhichResult) {
3126 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3127 if (EltSz == 64)
3128 return false;
3129
3130 unsigned NumElts = VT.getVectorNumElements();
3131 WhichResult = (M[0] == 0 ? 0 : 1);
3132 for (unsigned i = 0; i < NumElts; i += 2) {
3133 if ((unsigned) M[i] != i + WhichResult ||
3134 (unsigned) M[i+1] != i + WhichResult)
3135 return false;
3136 }
3137 return true;
3138}
3139
Bob Wilsonc692cb72009-08-21 20:54:19 +00003140static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3141 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003142 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3143 if (EltSz == 64)
3144 return false;
3145
Bob Wilsonc692cb72009-08-21 20:54:19 +00003146 unsigned NumElts = VT.getVectorNumElements();
3147 WhichResult = (M[0] == 0 ? 0 : 1);
3148 for (unsigned i = 0; i != NumElts; ++i) {
3149 if ((unsigned) M[i] != 2 * i + WhichResult)
3150 return false;
3151 }
3152
3153 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003154 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003155 return false;
3156
3157 return true;
3158}
3159
Bob Wilson324f4f12009-12-03 06:40:55 +00003160/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3161/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3162/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3163static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3164 unsigned &WhichResult) {
3165 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3166 if (EltSz == 64)
3167 return false;
3168
3169 unsigned Half = VT.getVectorNumElements() / 2;
3170 WhichResult = (M[0] == 0 ? 0 : 1);
3171 for (unsigned j = 0; j != 2; ++j) {
3172 unsigned Idx = WhichResult;
3173 for (unsigned i = 0; i != Half; ++i) {
3174 if ((unsigned) M[i + j * Half] != Idx)
3175 return false;
3176 Idx += 2;
3177 }
3178 }
3179
3180 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3181 if (VT.is64BitVector() && EltSz == 32)
3182 return false;
3183
3184 return true;
3185}
3186
Bob Wilsonc692cb72009-08-21 20:54:19 +00003187static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3188 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003189 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3190 if (EltSz == 64)
3191 return false;
3192
Bob Wilsonc692cb72009-08-21 20:54:19 +00003193 unsigned NumElts = VT.getVectorNumElements();
3194 WhichResult = (M[0] == 0 ? 0 : 1);
3195 unsigned Idx = WhichResult * NumElts / 2;
3196 for (unsigned i = 0; i != NumElts; i += 2) {
3197 if ((unsigned) M[i] != Idx ||
3198 (unsigned) M[i+1] != Idx + NumElts)
3199 return false;
3200 Idx += 1;
3201 }
3202
3203 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003204 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003205 return false;
3206
3207 return true;
3208}
3209
Bob Wilson324f4f12009-12-03 06:40:55 +00003210/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3211/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3212/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3213static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3214 unsigned &WhichResult) {
3215 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3216 if (EltSz == 64)
3217 return false;
3218
3219 unsigned NumElts = VT.getVectorNumElements();
3220 WhichResult = (M[0] == 0 ? 0 : 1);
3221 unsigned Idx = WhichResult * NumElts / 2;
3222 for (unsigned i = 0; i != NumElts; i += 2) {
3223 if ((unsigned) M[i] != Idx ||
3224 (unsigned) M[i+1] != Idx)
3225 return false;
3226 Idx += 1;
3227 }
3228
3229 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3230 if (VT.is64BitVector() && EltSz == 32)
3231 return false;
3232
3233 return true;
3234}
3235
Bob Wilson5bafff32009-06-22 23:27:02 +00003236// If this is a case we can't handle, return null and let the default
3237// expansion code take care of it.
3238static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00003239 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003240 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003241 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003242
3243 APInt SplatBits, SplatUndef;
3244 unsigned SplatBitSize;
3245 bool HasAnyUndefs;
3246 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003247 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003248 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003249 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003250 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003251 SplatUndef.getZExtValue(), SplatBitSize,
3252 DAG, VmovVT, VT.is128BitVector(), true);
3253 if (Val.getNode()) {
3254 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
3255 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vmov);
3256 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003257
3258 // Try an immediate VMVN.
3259 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3260 ((1LL << SplatBitSize) - 1));
3261 Val = isNEONModifiedImm(NegatedImm,
3262 SplatUndef.getZExtValue(), SplatBitSize,
3263 DAG, VmovVT, VT.is128BitVector(), false);
3264 if (Val.getNode()) {
3265 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
3266 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vmov);
3267 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003268 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003269 }
3270
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003271 // Scan through the operands to see if only one value is used.
3272 unsigned NumElts = VT.getVectorNumElements();
3273 bool isOnlyLowElement = true;
3274 bool usesOnlyOneValue = true;
3275 bool isConstant = true;
3276 SDValue Value;
3277 for (unsigned i = 0; i < NumElts; ++i) {
3278 SDValue V = Op.getOperand(i);
3279 if (V.getOpcode() == ISD::UNDEF)
3280 continue;
3281 if (i > 0)
3282 isOnlyLowElement = false;
3283 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3284 isConstant = false;
3285
3286 if (!Value.getNode())
3287 Value = V;
3288 else if (V != Value)
3289 usesOnlyOneValue = false;
3290 }
3291
3292 if (!Value.getNode())
3293 return DAG.getUNDEF(VT);
3294
3295 if (isOnlyLowElement)
3296 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3297
3298 // If all elements are constants, fall back to the default expansion, which
3299 // will generate a load from the constant pool.
3300 if (isConstant)
3301 return SDValue();
3302
3303 // Use VDUP for non-constant splats.
Bob Wilson069e4342010-05-23 05:42:31 +00003304 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3305 if (usesOnlyOneValue && EltSize <= 32)
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003306 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3307
3308 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003309 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3310 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003311 if (EltSize >= 32) {
3312 // Do the expansion with floating-point types, since that is what the VFP
3313 // registers are defined to use, and since i64 is not legal.
3314 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3315 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003316 SmallVector<SDValue, 8> Ops;
3317 for (unsigned i = 0; i < NumElts; ++i)
3318 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, dl, EltVT, Op.getOperand(i)));
3319 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003320 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003321 }
3322
3323 return SDValue();
3324}
3325
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003326/// isShuffleMaskLegal - Targets can use this to indicate that they only
3327/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3328/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3329/// are assumed to be legal.
3330bool
3331ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
3332 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003333 if (VT.getVectorNumElements() == 4 &&
3334 (VT.is128BitVector() || VT.is64BitVector())) {
3335 unsigned PFIndexes[4];
3336 for (unsigned i = 0; i != 4; ++i) {
3337 if (M[i] < 0)
3338 PFIndexes[i] = 8;
3339 else
3340 PFIndexes[i] = M[i];
3341 }
3342
3343 // Compute the index in the perfect shuffle table.
3344 unsigned PFTableIndex =
3345 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3346 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3347 unsigned Cost = (PFEntry >> 30);
3348
3349 if (Cost <= 4)
3350 return true;
3351 }
3352
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003353 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00003354 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003355
Bob Wilson53dd2452010-06-07 23:53:38 +00003356 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3357 return (EltSize >= 32 ||
3358 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003359 isVREVMask(M, VT, 64) ||
3360 isVREVMask(M, VT, 32) ||
3361 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00003362 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
3363 isVTRNMask(M, VT, WhichResult) ||
3364 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00003365 isVZIPMask(M, VT, WhichResult) ||
3366 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
3367 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
3368 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003369}
3370
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003371/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
3372/// the specified operations to build the shuffle.
3373static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
3374 SDValue RHS, SelectionDAG &DAG,
3375 DebugLoc dl) {
3376 unsigned OpNum = (PFEntry >> 26) & 0x0F;
3377 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
3378 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
3379
3380 enum {
3381 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
3382 OP_VREV,
3383 OP_VDUP0,
3384 OP_VDUP1,
3385 OP_VDUP2,
3386 OP_VDUP3,
3387 OP_VEXT1,
3388 OP_VEXT2,
3389 OP_VEXT3,
3390 OP_VUZPL, // VUZP, left result
3391 OP_VUZPR, // VUZP, right result
3392 OP_VZIPL, // VZIP, left result
3393 OP_VZIPR, // VZIP, right result
3394 OP_VTRNL, // VTRN, left result
3395 OP_VTRNR // VTRN, right result
3396 };
3397
3398 if (OpNum == OP_COPY) {
3399 if (LHSID == (1*9+2)*9+3) return LHS;
3400 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
3401 return RHS;
3402 }
3403
3404 SDValue OpLHS, OpRHS;
3405 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
3406 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
3407 EVT VT = OpLHS.getValueType();
3408
3409 switch (OpNum) {
3410 default: llvm_unreachable("Unknown shuffle opcode!");
3411 case OP_VREV:
3412 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
3413 case OP_VDUP0:
3414 case OP_VDUP1:
3415 case OP_VDUP2:
3416 case OP_VDUP3:
3417 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003418 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003419 case OP_VEXT1:
3420 case OP_VEXT2:
3421 case OP_VEXT3:
3422 return DAG.getNode(ARMISD::VEXT, dl, VT,
3423 OpLHS, OpRHS,
3424 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
3425 case OP_VUZPL:
3426 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003427 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003428 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
3429 case OP_VZIPL:
3430 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003431 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003432 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
3433 case OP_VTRNL:
3434 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003435 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3436 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003437 }
3438}
3439
Bob Wilson5bafff32009-06-22 23:27:02 +00003440static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003441 SDValue V1 = Op.getOperand(0);
3442 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00003443 DebugLoc dl = Op.getDebugLoc();
3444 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003445 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003446 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00003447
Bob Wilson28865062009-08-13 02:13:04 +00003448 // Convert shuffles that are directly supported on NEON to target-specific
3449 // DAG nodes, instead of keeping them as shuffles and matching them again
3450 // during code selection. This is more efficient and avoids the possibility
3451 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00003452 // FIXME: floating-point vectors should be canonicalized to integer vectors
3453 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003454 SVN->getMask(ShuffleMask);
3455
Bob Wilson53dd2452010-06-07 23:53:38 +00003456 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3457 if (EltSize <= 32) {
3458 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
3459 int Lane = SVN->getSplatIndex();
3460 // If this is undef splat, generate it via "just" vdup, if possible.
3461 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00003462
Bob Wilson53dd2452010-06-07 23:53:38 +00003463 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3464 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
3465 }
3466 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
3467 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00003468 }
Bob Wilson53dd2452010-06-07 23:53:38 +00003469
3470 bool ReverseVEXT;
3471 unsigned Imm;
3472 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
3473 if (ReverseVEXT)
3474 std::swap(V1, V2);
3475 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
3476 DAG.getConstant(Imm, MVT::i32));
3477 }
3478
3479 if (isVREVMask(ShuffleMask, VT, 64))
3480 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
3481 if (isVREVMask(ShuffleMask, VT, 32))
3482 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
3483 if (isVREVMask(ShuffleMask, VT, 16))
3484 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
3485
3486 // Check for Neon shuffles that modify both input vectors in place.
3487 // If both results are used, i.e., if there are two shuffles with the same
3488 // source operands and with masks corresponding to both results of one of
3489 // these operations, DAG memoization will ensure that a single node is
3490 // used for both shuffles.
3491 unsigned WhichResult;
3492 if (isVTRNMask(ShuffleMask, VT, WhichResult))
3493 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3494 V1, V2).getValue(WhichResult);
3495 if (isVUZPMask(ShuffleMask, VT, WhichResult))
3496 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3497 V1, V2).getValue(WhichResult);
3498 if (isVZIPMask(ShuffleMask, VT, WhichResult))
3499 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3500 V1, V2).getValue(WhichResult);
3501
3502 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
3503 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3504 V1, V1).getValue(WhichResult);
3505 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3506 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3507 V1, V1).getValue(WhichResult);
3508 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3509 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3510 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00003511 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003512
Bob Wilsonc692cb72009-08-21 20:54:19 +00003513 // If the shuffle is not directly supported and it has 4 elements, use
3514 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003515 unsigned NumElts = VT.getVectorNumElements();
3516 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003517 unsigned PFIndexes[4];
3518 for (unsigned i = 0; i != 4; ++i) {
3519 if (ShuffleMask[i] < 0)
3520 PFIndexes[i] = 8;
3521 else
3522 PFIndexes[i] = ShuffleMask[i];
3523 }
3524
3525 // Compute the index in the perfect shuffle table.
3526 unsigned PFTableIndex =
3527 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003528 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3529 unsigned Cost = (PFEntry >> 30);
3530
3531 if (Cost <= 4)
3532 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
3533 }
Bob Wilsond8e17572009-08-12 22:31:50 +00003534
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003535 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003536 if (EltSize >= 32) {
3537 // Do the expansion with floating-point types, since that is what the VFP
3538 // registers are defined to use, and since i64 is not legal.
3539 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3540 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
3541 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V1);
3542 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003543 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003544 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00003545 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003546 Ops.push_back(DAG.getUNDEF(EltVT));
3547 else
3548 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
3549 ShuffleMask[i] < (int)NumElts ? V1 : V2,
3550 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
3551 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00003552 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003553 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Bob Wilson63b88452010-05-20 18:39:53 +00003554 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
3555 }
3556
Bob Wilson22cac0d2009-08-14 05:16:33 +00003557 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003558}
3559
Bob Wilson5bafff32009-06-22 23:27:02 +00003560static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003561 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003562 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003563 SDValue Vec = Op.getOperand(0);
3564 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00003565 assert(VT == MVT::i32 &&
3566 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
3567 "unexpected type for custom-lowering vector extract");
3568 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00003569}
3570
Bob Wilsona6d65862009-08-03 20:36:38 +00003571static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3572 // The only time a CONCAT_VECTORS operation can have legal types is when
3573 // two 64-bit vectors are concatenated to a 128-bit vector.
3574 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
3575 "unexpected CONCAT_VECTORS");
3576 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00003577 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00003578 SDValue Op0 = Op.getOperand(0);
3579 SDValue Op1 = Op.getOperand(1);
3580 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003581 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3582 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00003583 DAG.getIntPtrConstant(0));
3584 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003585 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3586 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00003587 DAG.getIntPtrConstant(1));
3588 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003589}
3590
Dan Gohmand858e902010-04-17 15:26:15 +00003591SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003592 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003593 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00003594 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00003595 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003596 case ISD::GlobalAddress:
3597 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
3598 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00003599 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00003600 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
3601 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003602 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00003603 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00003604 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00003605 case ISD::SINT_TO_FP:
3606 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
3607 case ISD::FP_TO_SINT:
3608 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003609 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003610 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003611 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003612 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00003613 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00003614 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00003615 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
3616 Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00003617 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003618 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00003619 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00003620 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00003621 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003622 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00003623 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00003624 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003625 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
3626 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3627 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003628 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00003629 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003630 }
Dan Gohman475871a2008-07-27 21:46:04 +00003631 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003632}
3633
Duncan Sands1607f052008-12-01 11:39:25 +00003634/// ReplaceNodeResults - Replace the results of node with an illegal result
3635/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00003636void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
3637 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00003638 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00003639 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00003640 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00003641 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003642 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003643 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003644 case ISD::BIT_CONVERT:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003645 Res = ExpandBIT_CONVERT(N, DAG);
3646 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00003647 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003648 case ISD::SRA:
3649 Res = LowerShift(N, DAG, Subtarget);
3650 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003651 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00003652 if (Res.getNode())
3653 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00003654}
Chris Lattner27a6c732007-11-24 07:07:01 +00003655
Evan Chenga8e29892007-01-19 07:51:42 +00003656//===----------------------------------------------------------------------===//
3657// ARM Scheduler Hooks
3658//===----------------------------------------------------------------------===//
3659
3660MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003661ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
3662 MachineBasicBlock *BB,
3663 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00003664 unsigned dest = MI->getOperand(0).getReg();
3665 unsigned ptr = MI->getOperand(1).getReg();
3666 unsigned oldval = MI->getOperand(2).getReg();
3667 unsigned newval = MI->getOperand(3).getReg();
3668 unsigned scratch = BB->getParent()->getRegInfo()
3669 .createVirtualRegister(ARM::GPRRegisterClass);
3670 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3671 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003672 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00003673
3674 unsigned ldrOpc, strOpc;
3675 switch (Size) {
3676 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003677 case 1:
3678 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
3679 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
3680 break;
3681 case 2:
3682 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3683 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3684 break;
3685 case 4:
3686 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3687 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3688 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00003689 }
3690
3691 MachineFunction *MF = BB->getParent();
3692 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3693 MachineFunction::iterator It = BB;
3694 ++It; // insert the new blocks after the current block
3695
3696 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3697 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3698 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3699 MF->insert(It, loop1MBB);
3700 MF->insert(It, loop2MBB);
3701 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00003702
3703 // Transfer the remainder of BB and its successor edges to exitMBB.
3704 exitMBB->splice(exitMBB->begin(), BB,
3705 llvm::next(MachineBasicBlock::iterator(MI)),
3706 BB->end());
3707 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003708
3709 // thisMBB:
3710 // ...
3711 // fallthrough --> loop1MBB
3712 BB->addSuccessor(loop1MBB);
3713
3714 // loop1MBB:
3715 // ldrex dest, [ptr]
3716 // cmp dest, oldval
3717 // bne exitMBB
3718 BB = loop1MBB;
3719 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003720 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003721 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003722 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3723 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003724 BB->addSuccessor(loop2MBB);
3725 BB->addSuccessor(exitMBB);
3726
3727 // loop2MBB:
3728 // strex scratch, newval, [ptr]
3729 // cmp scratch, #0
3730 // bne loop1MBB
3731 BB = loop2MBB;
3732 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
3733 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003734 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003735 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003736 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3737 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003738 BB->addSuccessor(loop1MBB);
3739 BB->addSuccessor(exitMBB);
3740
3741 // exitMBB:
3742 // ...
3743 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00003744
Dan Gohman14152b42010-07-06 20:24:04 +00003745 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00003746
Jim Grosbach5278eb82009-12-11 01:42:04 +00003747 return BB;
3748}
3749
3750MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003751ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
3752 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00003753 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
3754 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3755
3756 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003757 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003758 MachineFunction::iterator It = BB;
3759 ++It;
3760
3761 unsigned dest = MI->getOperand(0).getReg();
3762 unsigned ptr = MI->getOperand(1).getReg();
3763 unsigned incr = MI->getOperand(2).getReg();
3764 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00003765
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003766 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003767 unsigned ldrOpc, strOpc;
3768 switch (Size) {
3769 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003770 case 1:
3771 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00003772 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003773 break;
3774 case 2:
3775 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3776 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3777 break;
3778 case 4:
3779 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3780 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3781 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00003782 }
3783
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003784 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3785 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3786 MF->insert(It, loopMBB);
3787 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00003788
3789 // Transfer the remainder of BB and its successor edges to exitMBB.
3790 exitMBB->splice(exitMBB->begin(), BB,
3791 llvm::next(MachineBasicBlock::iterator(MI)),
3792 BB->end());
3793 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003794
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003795 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003796 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3797 unsigned scratch2 = (!BinOpcode) ? incr :
3798 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3799
3800 // thisMBB:
3801 // ...
3802 // fallthrough --> loopMBB
3803 BB->addSuccessor(loopMBB);
3804
3805 // loopMBB:
3806 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003807 // <binop> scratch2, dest, incr
3808 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00003809 // cmp scratch, #0
3810 // bne- loopMBB
3811 // fallthrough --> exitMBB
3812 BB = loopMBB;
3813 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00003814 if (BinOpcode) {
3815 // operand order needs to go the other way for NAND
3816 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
3817 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3818 addReg(incr).addReg(dest)).addReg(0);
3819 else
3820 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3821 addReg(dest).addReg(incr)).addReg(0);
3822 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00003823
3824 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
3825 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003826 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00003827 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003828 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3829 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003830
3831 BB->addSuccessor(loopMBB);
3832 BB->addSuccessor(exitMBB);
3833
3834 // exitMBB:
3835 // ...
3836 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00003837
Dan Gohman14152b42010-07-06 20:24:04 +00003838 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00003839
Jim Grosbachc3c23542009-12-14 04:22:04 +00003840 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00003841}
3842
Evan Cheng218977b2010-07-13 19:27:42 +00003843static
3844MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
3845 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
3846 E = MBB->succ_end(); I != E; ++I)
3847 if (*I != Succ)
3848 return *I;
3849 llvm_unreachable("Expecting a BB with two successors!");
3850}
3851
Jim Grosbache801dc42009-12-12 01:40:06 +00003852MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00003853ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003854 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003855 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00003856 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003857 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00003858 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00003859 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00003860 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00003861 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00003862
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003863 case ARM::ATOMIC_LOAD_ADD_I8:
3864 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3865 case ARM::ATOMIC_LOAD_ADD_I16:
3866 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3867 case ARM::ATOMIC_LOAD_ADD_I32:
3868 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003869
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003870 case ARM::ATOMIC_LOAD_AND_I8:
3871 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3872 case ARM::ATOMIC_LOAD_AND_I16:
3873 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3874 case ARM::ATOMIC_LOAD_AND_I32:
3875 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003876
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003877 case ARM::ATOMIC_LOAD_OR_I8:
3878 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3879 case ARM::ATOMIC_LOAD_OR_I16:
3880 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3881 case ARM::ATOMIC_LOAD_OR_I32:
3882 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003883
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003884 case ARM::ATOMIC_LOAD_XOR_I8:
3885 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3886 case ARM::ATOMIC_LOAD_XOR_I16:
3887 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3888 case ARM::ATOMIC_LOAD_XOR_I32:
3889 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003890
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003891 case ARM::ATOMIC_LOAD_NAND_I8:
3892 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3893 case ARM::ATOMIC_LOAD_NAND_I16:
3894 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3895 case ARM::ATOMIC_LOAD_NAND_I32:
3896 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003897
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003898 case ARM::ATOMIC_LOAD_SUB_I8:
3899 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3900 case ARM::ATOMIC_LOAD_SUB_I16:
3901 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3902 case ARM::ATOMIC_LOAD_SUB_I32:
3903 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003904
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003905 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
3906 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
3907 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00003908
3909 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
3910 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
3911 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003912
Evan Cheng007ea272009-08-12 05:17:19 +00003913 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00003914 // To "insert" a SELECT_CC instruction, we actually have to insert the
3915 // diamond control-flow pattern. The incoming instruction knows the
3916 // destination vreg to set, the condition code register to branch on, the
3917 // true/false values to select between, and a branch opcode to use.
3918 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003919 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00003920 ++It;
3921
3922 // thisMBB:
3923 // ...
3924 // TrueVal = ...
3925 // cmpTY ccX, r1, r2
3926 // bCC copy1MBB
3927 // fallthrough --> copy0MBB
3928 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003929 MachineFunction *F = BB->getParent();
3930 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3931 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00003932 F->insert(It, copy0MBB);
3933 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00003934
3935 // Transfer the remainder of BB and its successor edges to sinkMBB.
3936 sinkMBB->splice(sinkMBB->begin(), BB,
3937 llvm::next(MachineBasicBlock::iterator(MI)),
3938 BB->end());
3939 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
3940
Dan Gohman258c58c2010-07-06 15:49:48 +00003941 BB->addSuccessor(copy0MBB);
3942 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00003943
Dan Gohman14152b42010-07-06 20:24:04 +00003944 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
3945 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
3946
Evan Chenga8e29892007-01-19 07:51:42 +00003947 // copy0MBB:
3948 // %FalseValue = ...
3949 // # fallthrough to sinkMBB
3950 BB = copy0MBB;
3951
3952 // Update machine-CFG edges
3953 BB->addSuccessor(sinkMBB);
3954
3955 // sinkMBB:
3956 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3957 // ...
3958 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00003959 BuildMI(*BB, BB->begin(), dl,
3960 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00003961 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
3962 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
3963
Dan Gohman14152b42010-07-06 20:24:04 +00003964 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00003965 return BB;
3966 }
Evan Cheng86198642009-08-07 00:34:42 +00003967
Evan Cheng218977b2010-07-13 19:27:42 +00003968 case ARM::BCCi64:
3969 case ARM::BCCZi64: {
3970 // Compare both parts that make up the double comparison separately for
3971 // equality.
3972 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
3973
3974 unsigned LHS1 = MI->getOperand(1).getReg();
3975 unsigned LHS2 = MI->getOperand(2).getReg();
3976 if (RHSisZero) {
3977 AddDefaultPred(BuildMI(BB, dl,
3978 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
3979 .addReg(LHS1).addImm(0));
3980 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
3981 .addReg(LHS2).addImm(0)
3982 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
3983 } else {
3984 unsigned RHS1 = MI->getOperand(3).getReg();
3985 unsigned RHS2 = MI->getOperand(4).getReg();
3986 AddDefaultPred(BuildMI(BB, dl,
3987 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
3988 .addReg(LHS1).addReg(RHS1));
3989 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
3990 .addReg(LHS2).addReg(RHS2)
3991 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
3992 }
3993
3994 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
3995 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
3996 if (MI->getOperand(0).getImm() == ARMCC::NE)
3997 std::swap(destMBB, exitMBB);
3998
3999 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4000 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
4001 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
4002 .addMBB(exitMBB);
4003
4004 MI->eraseFromParent(); // The pseudo instruction is gone now.
4005 return BB;
4006 }
4007
Evan Cheng86198642009-08-07 00:34:42 +00004008 case ARM::tANDsp:
4009 case ARM::tADDspr_:
4010 case ARM::tSUBspi_:
4011 case ARM::t2SUBrSPi_:
4012 case ARM::t2SUBrSPi12_:
4013 case ARM::t2SUBrSPs_: {
4014 MachineFunction *MF = BB->getParent();
4015 unsigned DstReg = MI->getOperand(0).getReg();
4016 unsigned SrcReg = MI->getOperand(1).getReg();
4017 bool DstIsDead = MI->getOperand(0).isDead();
4018 bool SrcIsKill = MI->getOperand(1).isKill();
4019
4020 if (SrcReg != ARM::SP) {
4021 // Copy the source to SP from virtual register.
4022 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
4023 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
4024 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
Dan Gohman14152b42010-07-06 20:24:04 +00004025 BuildMI(*BB, MI, dl, TII->get(CopyOpc), ARM::SP)
Evan Cheng86198642009-08-07 00:34:42 +00004026 .addReg(SrcReg, getKillRegState(SrcIsKill));
4027 }
4028
4029 unsigned OpOpc = 0;
4030 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
4031 switch (MI->getOpcode()) {
4032 default:
4033 llvm_unreachable("Unexpected pseudo instruction!");
4034 case ARM::tANDsp:
4035 OpOpc = ARM::tAND;
4036 NeedPred = true;
4037 break;
4038 case ARM::tADDspr_:
4039 OpOpc = ARM::tADDspr;
4040 break;
4041 case ARM::tSUBspi_:
4042 OpOpc = ARM::tSUBspi;
4043 break;
4044 case ARM::t2SUBrSPi_:
4045 OpOpc = ARM::t2SUBrSPi;
4046 NeedPred = true; NeedCC = true;
4047 break;
4048 case ARM::t2SUBrSPi12_:
4049 OpOpc = ARM::t2SUBrSPi12;
4050 NeedPred = true;
4051 break;
4052 case ARM::t2SUBrSPs_:
4053 OpOpc = ARM::t2SUBrSPs;
4054 NeedPred = true; NeedCC = true; NeedOp3 = true;
4055 break;
4056 }
Dan Gohman14152b42010-07-06 20:24:04 +00004057 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(OpOpc), ARM::SP);
Evan Cheng86198642009-08-07 00:34:42 +00004058 if (OpOpc == ARM::tAND)
4059 AddDefaultT1CC(MIB);
4060 MIB.addReg(ARM::SP);
4061 MIB.addOperand(MI->getOperand(2));
4062 if (NeedOp3)
4063 MIB.addOperand(MI->getOperand(3));
4064 if (NeedPred)
4065 AddDefaultPred(MIB);
4066 if (NeedCC)
4067 AddDefaultCC(MIB);
4068
4069 // Copy the result from SP to virtual register.
4070 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
4071 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
4072 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
Dan Gohman14152b42010-07-06 20:24:04 +00004073 BuildMI(*BB, MI, dl, TII->get(CopyOpc))
Evan Cheng86198642009-08-07 00:34:42 +00004074 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
4075 .addReg(ARM::SP);
Dan Gohman14152b42010-07-06 20:24:04 +00004076 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng86198642009-08-07 00:34:42 +00004077 return BB;
4078 }
Evan Chenga8e29892007-01-19 07:51:42 +00004079 }
4080}
4081
4082//===----------------------------------------------------------------------===//
4083// ARM Optimization Hooks
4084//===----------------------------------------------------------------------===//
4085
Chris Lattnerd1980a52009-03-12 06:52:53 +00004086static
4087SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
4088 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00004089 SelectionDAG &DAG = DCI.DAG;
4090 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004091 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00004092 unsigned Opc = N->getOpcode();
4093 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
4094 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
4095 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
4096 ISD::CondCode CC = ISD::SETCC_INVALID;
4097
4098 if (isSlctCC) {
4099 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
4100 } else {
4101 SDValue CCOp = Slct.getOperand(0);
4102 if (CCOp.getOpcode() == ISD::SETCC)
4103 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
4104 }
4105
4106 bool DoXform = false;
4107 bool InvCC = false;
4108 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
4109 "Bad input!");
4110
4111 if (LHS.getOpcode() == ISD::Constant &&
4112 cast<ConstantSDNode>(LHS)->isNullValue()) {
4113 DoXform = true;
4114 } else if (CC != ISD::SETCC_INVALID &&
4115 RHS.getOpcode() == ISD::Constant &&
4116 cast<ConstantSDNode>(RHS)->isNullValue()) {
4117 std::swap(LHS, RHS);
4118 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004119 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00004120 Op0.getOperand(0).getValueType();
4121 bool isInt = OpVT.isInteger();
4122 CC = ISD::getSetCCInverse(CC, isInt);
4123
4124 if (!TLI.isCondCodeLegal(CC, OpVT))
4125 return SDValue(); // Inverse operator isn't legal.
4126
4127 DoXform = true;
4128 InvCC = true;
4129 }
4130
4131 if (DoXform) {
4132 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
4133 if (isSlctCC)
4134 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
4135 Slct.getOperand(0), Slct.getOperand(1), CC);
4136 SDValue CCOp = Slct.getOperand(0);
4137 if (InvCC)
4138 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
4139 CCOp.getOperand(0), CCOp.getOperand(1), CC);
4140 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
4141 CCOp, OtherOp, Result);
4142 }
4143 return SDValue();
4144}
4145
4146/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
4147static SDValue PerformADDCombine(SDNode *N,
4148 TargetLowering::DAGCombinerInfo &DCI) {
4149 // added by evan in r37685 with no testcase.
4150 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00004151
Chris Lattnerd1980a52009-03-12 06:52:53 +00004152 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
4153 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
4154 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
4155 if (Result.getNode()) return Result;
4156 }
4157 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
4158 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
4159 if (Result.getNode()) return Result;
4160 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00004161
Chris Lattnerd1980a52009-03-12 06:52:53 +00004162 return SDValue();
4163}
4164
4165/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
4166static SDValue PerformSUBCombine(SDNode *N,
4167 TargetLowering::DAGCombinerInfo &DCI) {
4168 // added by evan in r37685 with no testcase.
4169 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00004170
Chris Lattnerd1980a52009-03-12 06:52:53 +00004171 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
4172 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
4173 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
4174 if (Result.getNode()) return Result;
4175 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00004176
Chris Lattnerd1980a52009-03-12 06:52:53 +00004177 return SDValue();
4178}
4179
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004180static SDValue PerformMULCombine(SDNode *N,
4181 TargetLowering::DAGCombinerInfo &DCI,
4182 const ARMSubtarget *Subtarget) {
4183 SelectionDAG &DAG = DCI.DAG;
4184
4185 if (Subtarget->isThumb1Only())
4186 return SDValue();
4187
4188 if (DAG.getMachineFunction().
4189 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
4190 return SDValue();
4191
4192 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
4193 return SDValue();
4194
4195 EVT VT = N->getValueType(0);
4196 if (VT != MVT::i32)
4197 return SDValue();
4198
4199 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
4200 if (!C)
4201 return SDValue();
4202
4203 uint64_t MulAmt = C->getZExtValue();
4204 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
4205 ShiftAmt = ShiftAmt & (32 - 1);
4206 SDValue V = N->getOperand(0);
4207 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004208
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004209 SDValue Res;
4210 MulAmt >>= ShiftAmt;
4211 if (isPowerOf2_32(MulAmt - 1)) {
4212 // (mul x, 2^N + 1) => (add (shl x, N), x)
4213 Res = DAG.getNode(ISD::ADD, DL, VT,
4214 V, DAG.getNode(ISD::SHL, DL, VT,
4215 V, DAG.getConstant(Log2_32(MulAmt-1),
4216 MVT::i32)));
4217 } else if (isPowerOf2_32(MulAmt + 1)) {
4218 // (mul x, 2^N - 1) => (sub (shl x, N), x)
4219 Res = DAG.getNode(ISD::SUB, DL, VT,
4220 DAG.getNode(ISD::SHL, DL, VT,
4221 V, DAG.getConstant(Log2_32(MulAmt+1),
4222 MVT::i32)),
4223 V);
4224 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004225 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004226
4227 if (ShiftAmt != 0)
4228 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
4229 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004230
4231 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004232 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004233 return SDValue();
4234}
4235
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004236/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
4237static SDValue PerformORCombine(SDNode *N,
4238 TargetLowering::DAGCombinerInfo &DCI,
4239 const ARMSubtarget *Subtarget) {
Jim Grosbach54238562010-07-17 03:30:54 +00004240 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
4241 // reasonable.
4242
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004243 // BFI is only available on V6T2+
4244 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
4245 return SDValue();
4246
4247 SelectionDAG &DAG = DCI.DAG;
4248 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Jim Grosbach54238562010-07-17 03:30:54 +00004249 DebugLoc DL = N->getDebugLoc();
4250 // 1) or (and A, mask), val => ARMbfi A, val, mask
4251 // iff (val & mask) == val
4252 //
4253 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
4254 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
4255 // && CountPopulation_32(mask) == CountPopulation_32(~mask2)
4256 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
4257 // && CountPopulation_32(mask) == CountPopulation_32(~mask2)
4258 // (i.e., copy a bitfield value into another bitfield of the same width)
4259 if (N0.getOpcode() != ISD::AND)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004260 return SDValue();
4261
4262 EVT VT = N->getValueType(0);
4263 if (VT != MVT::i32)
4264 return SDValue();
4265
Jim Grosbach54238562010-07-17 03:30:54 +00004266
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004267 // The value and the mask need to be constants so we can verify this is
4268 // actually a bitfield set. If the mask is 0xffff, we can do better
4269 // via a movt instruction, so don't use BFI in that case.
4270 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
4271 if (!C)
4272 return SDValue();
4273 unsigned Mask = C->getZExtValue();
4274 if (Mask == 0xffff)
4275 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004276 SDValue Res;
4277 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
4278 if ((C = dyn_cast<ConstantSDNode>(N1))) {
4279 unsigned Val = C->getZExtValue();
4280 if (!ARM::isBitFieldInvertedMask(Mask) || (Val & ~Mask) != Val)
4281 return SDValue();
4282 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004283
Jim Grosbach54238562010-07-17 03:30:54 +00004284 Res = DAG.getNode(ARMISD::BFI, DL, VT, N0.getOperand(0),
4285 DAG.getConstant(Val, MVT::i32),
4286 DAG.getConstant(Mask, MVT::i32));
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004287
Jim Grosbach54238562010-07-17 03:30:54 +00004288 // Do not add new nodes to DAG combiner worklist.
4289 DCI.CombineTo(N, Res, false);
4290 } else if (N1.getOpcode() == ISD::AND) {
4291 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
4292 C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
4293 if (!C)
4294 return SDValue();
4295 unsigned Mask2 = C->getZExtValue();
4296
4297 if (ARM::isBitFieldInvertedMask(Mask) &&
4298 ARM::isBitFieldInvertedMask(~Mask2) &&
4299 (CountPopulation_32(Mask) == CountPopulation_32(~Mask2))) {
4300 // The pack halfword instruction works better for masks that fit it,
4301 // so use that when it's available.
4302 if (Subtarget->hasT2ExtractPack() &&
4303 (Mask == 0xffff || Mask == 0xffff0000))
4304 return SDValue();
4305 // 2a
4306 unsigned lsb = CountTrailingZeros_32(Mask2);
4307 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
4308 DAG.getConstant(lsb, MVT::i32));
4309 Res = DAG.getNode(ARMISD::BFI, DL, VT, N0.getOperand(0), Res,
4310 DAG.getConstant(Mask, MVT::i32));
4311 // Do not add new nodes to DAG combiner worklist.
4312 DCI.CombineTo(N, Res, false);
4313 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
4314 ARM::isBitFieldInvertedMask(Mask2) &&
4315 (CountPopulation_32(~Mask) == CountPopulation_32(Mask2))) {
4316 // The pack halfword instruction works better for masks that fit it,
4317 // so use that when it's available.
4318 if (Subtarget->hasT2ExtractPack() &&
4319 (Mask2 == 0xffff || Mask2 == 0xffff0000))
4320 return SDValue();
4321 // 2b
4322 unsigned lsb = CountTrailingZeros_32(Mask);
4323 Res = DAG.getNode(ISD::SRL, DL, VT, N0.getOperand(0),
4324 DAG.getConstant(lsb, MVT::i32));
4325 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
4326 DAG.getConstant(Mask2, MVT::i32));
4327 // Do not add new nodes to DAG combiner worklist.
4328 DCI.CombineTo(N, Res, false);
4329 }
4330 }
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004331
4332 return SDValue();
4333}
4334
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +00004335/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
4336/// ARMISD::VMOVRRD.
Jim Grosbache5165492009-11-09 00:11:35 +00004337static SDValue PerformVMOVRRDCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004338 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004339 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00004340 SDValue InDouble = N->getOperand(0);
Jim Grosbache5165492009-11-09 00:11:35 +00004341 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004342 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004343 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004344}
4345
Bob Wilson9e82bf12010-07-14 01:22:12 +00004346/// PerformVDUPLANECombine - Target-specific dag combine xforms for
4347/// ARMISD::VDUPLANE.
4348static SDValue PerformVDUPLANECombine(SDNode *N,
4349 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004350 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
4351 // redundant.
Bob Wilson9e82bf12010-07-14 01:22:12 +00004352 SDValue Op = N->getOperand(0);
4353 EVT VT = N->getValueType(0);
4354
4355 // Ignore bit_converts.
4356 while (Op.getOpcode() == ISD::BIT_CONVERT)
4357 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004358 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00004359 return SDValue();
4360
4361 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
4362 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
4363 // The canonical VMOV for a zero vector uses a 32-bit element size.
4364 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
4365 unsigned EltBits;
4366 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
4367 EltSize = 8;
4368 if (EltSize > VT.getVectorElementType().getSizeInBits())
4369 return SDValue();
4370
4371 SDValue Res = DCI.DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
4372 return DCI.CombineTo(N, Res, false);
4373}
4374
Bob Wilson5bafff32009-06-22 23:27:02 +00004375/// getVShiftImm - Check if this is a valid build_vector for the immediate
4376/// operand of a vector shift operation, where all the elements of the
4377/// build_vector must have the same constant integer value.
4378static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
4379 // Ignore bit_converts.
4380 while (Op.getOpcode() == ISD::BIT_CONVERT)
4381 Op = Op.getOperand(0);
4382 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
4383 APInt SplatBits, SplatUndef;
4384 unsigned SplatBitSize;
4385 bool HasAnyUndefs;
4386 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
4387 HasAnyUndefs, ElementBits) ||
4388 SplatBitSize > ElementBits)
4389 return false;
4390 Cnt = SplatBits.getSExtValue();
4391 return true;
4392}
4393
4394/// isVShiftLImm - Check if this is a valid build_vector for the immediate
4395/// operand of a vector shift left operation. That value must be in the range:
4396/// 0 <= Value < ElementBits for a left shift; or
4397/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00004398static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00004399 assert(VT.isVector() && "vector shift count is not a vector type");
4400 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
4401 if (! getVShiftImm(Op, ElementBits, Cnt))
4402 return false;
4403 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
4404}
4405
4406/// isVShiftRImm - Check if this is a valid build_vector for the immediate
4407/// operand of a vector shift right operation. For a shift opcode, the value
4408/// is positive, but for an intrinsic the value count must be negative. The
4409/// absolute value must be in the range:
4410/// 1 <= |Value| <= ElementBits for a right shift; or
4411/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00004412static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00004413 int64_t &Cnt) {
4414 assert(VT.isVector() && "vector shift count is not a vector type");
4415 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
4416 if (! getVShiftImm(Op, ElementBits, Cnt))
4417 return false;
4418 if (isIntrinsic)
4419 Cnt = -Cnt;
4420 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
4421}
4422
4423/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
4424static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
4425 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
4426 switch (IntNo) {
4427 default:
4428 // Don't do anything for most intrinsics.
4429 break;
4430
4431 // Vector shifts: check for immediate versions and lower them.
4432 // Note: This is done during DAG combining instead of DAG legalizing because
4433 // the build_vectors for 64-bit vector element shift counts are generally
4434 // not legal, and it is hard to see their values after they get legalized to
4435 // loads from a constant pool.
4436 case Intrinsic::arm_neon_vshifts:
4437 case Intrinsic::arm_neon_vshiftu:
4438 case Intrinsic::arm_neon_vshiftls:
4439 case Intrinsic::arm_neon_vshiftlu:
4440 case Intrinsic::arm_neon_vshiftn:
4441 case Intrinsic::arm_neon_vrshifts:
4442 case Intrinsic::arm_neon_vrshiftu:
4443 case Intrinsic::arm_neon_vrshiftn:
4444 case Intrinsic::arm_neon_vqshifts:
4445 case Intrinsic::arm_neon_vqshiftu:
4446 case Intrinsic::arm_neon_vqshiftsu:
4447 case Intrinsic::arm_neon_vqshiftns:
4448 case Intrinsic::arm_neon_vqshiftnu:
4449 case Intrinsic::arm_neon_vqshiftnsu:
4450 case Intrinsic::arm_neon_vqrshiftns:
4451 case Intrinsic::arm_neon_vqrshiftnu:
4452 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00004453 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004454 int64_t Cnt;
4455 unsigned VShiftOpc = 0;
4456
4457 switch (IntNo) {
4458 case Intrinsic::arm_neon_vshifts:
4459 case Intrinsic::arm_neon_vshiftu:
4460 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
4461 VShiftOpc = ARMISD::VSHL;
4462 break;
4463 }
4464 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
4465 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
4466 ARMISD::VSHRs : ARMISD::VSHRu);
4467 break;
4468 }
4469 return SDValue();
4470
4471 case Intrinsic::arm_neon_vshiftls:
4472 case Intrinsic::arm_neon_vshiftlu:
4473 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
4474 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00004475 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004476
4477 case Intrinsic::arm_neon_vrshifts:
4478 case Intrinsic::arm_neon_vrshiftu:
4479 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
4480 break;
4481 return SDValue();
4482
4483 case Intrinsic::arm_neon_vqshifts:
4484 case Intrinsic::arm_neon_vqshiftu:
4485 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
4486 break;
4487 return SDValue();
4488
4489 case Intrinsic::arm_neon_vqshiftsu:
4490 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
4491 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00004492 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004493
4494 case Intrinsic::arm_neon_vshiftn:
4495 case Intrinsic::arm_neon_vrshiftn:
4496 case Intrinsic::arm_neon_vqshiftns:
4497 case Intrinsic::arm_neon_vqshiftnu:
4498 case Intrinsic::arm_neon_vqshiftnsu:
4499 case Intrinsic::arm_neon_vqrshiftns:
4500 case Intrinsic::arm_neon_vqrshiftnu:
4501 case Intrinsic::arm_neon_vqrshiftnsu:
4502 // Narrowing shifts require an immediate right shift.
4503 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
4504 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00004505 llvm_unreachable("invalid shift count for narrowing vector shift "
4506 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004507
4508 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004509 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00004510 }
4511
4512 switch (IntNo) {
4513 case Intrinsic::arm_neon_vshifts:
4514 case Intrinsic::arm_neon_vshiftu:
4515 // Opcode already set above.
4516 break;
4517 case Intrinsic::arm_neon_vshiftls:
4518 case Intrinsic::arm_neon_vshiftlu:
4519 if (Cnt == VT.getVectorElementType().getSizeInBits())
4520 VShiftOpc = ARMISD::VSHLLi;
4521 else
4522 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
4523 ARMISD::VSHLLs : ARMISD::VSHLLu);
4524 break;
4525 case Intrinsic::arm_neon_vshiftn:
4526 VShiftOpc = ARMISD::VSHRN; break;
4527 case Intrinsic::arm_neon_vrshifts:
4528 VShiftOpc = ARMISD::VRSHRs; break;
4529 case Intrinsic::arm_neon_vrshiftu:
4530 VShiftOpc = ARMISD::VRSHRu; break;
4531 case Intrinsic::arm_neon_vrshiftn:
4532 VShiftOpc = ARMISD::VRSHRN; break;
4533 case Intrinsic::arm_neon_vqshifts:
4534 VShiftOpc = ARMISD::VQSHLs; break;
4535 case Intrinsic::arm_neon_vqshiftu:
4536 VShiftOpc = ARMISD::VQSHLu; break;
4537 case Intrinsic::arm_neon_vqshiftsu:
4538 VShiftOpc = ARMISD::VQSHLsu; break;
4539 case Intrinsic::arm_neon_vqshiftns:
4540 VShiftOpc = ARMISD::VQSHRNs; break;
4541 case Intrinsic::arm_neon_vqshiftnu:
4542 VShiftOpc = ARMISD::VQSHRNu; break;
4543 case Intrinsic::arm_neon_vqshiftnsu:
4544 VShiftOpc = ARMISD::VQSHRNsu; break;
4545 case Intrinsic::arm_neon_vqrshiftns:
4546 VShiftOpc = ARMISD::VQRSHRNs; break;
4547 case Intrinsic::arm_neon_vqrshiftnu:
4548 VShiftOpc = ARMISD::VQRSHRNu; break;
4549 case Intrinsic::arm_neon_vqrshiftnsu:
4550 VShiftOpc = ARMISD::VQRSHRNsu; break;
4551 }
4552
4553 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004554 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004555 }
4556
4557 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00004558 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004559 int64_t Cnt;
4560 unsigned VShiftOpc = 0;
4561
4562 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
4563 VShiftOpc = ARMISD::VSLI;
4564 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
4565 VShiftOpc = ARMISD::VSRI;
4566 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00004567 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004568 }
4569
4570 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
4571 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00004572 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004573 }
4574
4575 case Intrinsic::arm_neon_vqrshifts:
4576 case Intrinsic::arm_neon_vqrshiftu:
4577 // No immediate versions of these to check for.
4578 break;
4579 }
4580
4581 return SDValue();
4582}
4583
4584/// PerformShiftCombine - Checks for immediate versions of vector shifts and
4585/// lowers them. As with the vector shift intrinsics, this is done during DAG
4586/// combining instead of DAG legalizing because the build_vectors for 64-bit
4587/// vector element shift counts are generally not legal, and it is hard to see
4588/// their values after they get legalized to loads from a constant pool.
4589static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
4590 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00004591 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00004592
4593 // Nothing to be done for scalar shifts.
4594 if (! VT.isVector())
4595 return SDValue();
4596
4597 assert(ST->hasNEON() && "unexpected vector shift");
4598 int64_t Cnt;
4599
4600 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004601 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00004602
4603 case ISD::SHL:
4604 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
4605 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004606 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004607 break;
4608
4609 case ISD::SRA:
4610 case ISD::SRL:
4611 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
4612 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
4613 ARMISD::VSHRs : ARMISD::VSHRu);
4614 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004615 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004616 }
4617 }
4618 return SDValue();
4619}
4620
4621/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
4622/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
4623static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
4624 const ARMSubtarget *ST) {
4625 SDValue N0 = N->getOperand(0);
4626
4627 // Check for sign- and zero-extensions of vector extract operations of 8-
4628 // and 16-bit vector elements. NEON supports these directly. They are
4629 // handled during DAG combining because type legalization will promote them
4630 // to 32-bit types and it is messy to recognize the operations after that.
4631 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
4632 SDValue Vec = N0.getOperand(0);
4633 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004634 EVT VT = N->getValueType(0);
4635 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004636 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4637
Owen Anderson825b72b2009-08-11 20:47:22 +00004638 if (VT == MVT::i32 &&
4639 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00004640 TLI.isTypeLegal(Vec.getValueType())) {
4641
4642 unsigned Opc = 0;
4643 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004644 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00004645 case ISD::SIGN_EXTEND:
4646 Opc = ARMISD::VGETLANEs;
4647 break;
4648 case ISD::ZERO_EXTEND:
4649 case ISD::ANY_EXTEND:
4650 Opc = ARMISD::VGETLANEu;
4651 break;
4652 }
4653 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
4654 }
4655 }
4656
4657 return SDValue();
4658}
4659
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004660/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
4661/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
4662static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
4663 const ARMSubtarget *ST) {
4664 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00004665 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004666 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
4667 // a NaN; only do the transformation when it matches that behavior.
4668
4669 // For now only do this when using NEON for FP operations; if using VFP, it
4670 // is not obvious that the benefit outweighs the cost of switching to the
4671 // NEON pipeline.
4672 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
4673 N->getValueType(0) != MVT::f32)
4674 return SDValue();
4675
4676 SDValue CondLHS = N->getOperand(0);
4677 SDValue CondRHS = N->getOperand(1);
4678 SDValue LHS = N->getOperand(2);
4679 SDValue RHS = N->getOperand(3);
4680 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
4681
4682 unsigned Opcode = 0;
4683 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00004684 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004685 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00004686 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004687 IsReversed = true ; // x CC y ? y : x
4688 } else {
4689 return SDValue();
4690 }
4691
Bob Wilsone742bb52010-02-24 22:15:53 +00004692 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004693 switch (CC) {
4694 default: break;
4695 case ISD::SETOLT:
4696 case ISD::SETOLE:
4697 case ISD::SETLT:
4698 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004699 case ISD::SETULT:
4700 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004701 // If LHS is NaN, an ordered comparison will be false and the result will
4702 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
4703 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4704 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
4705 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4706 break;
4707 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
4708 // will return -0, so vmin can only be used for unsafe math or if one of
4709 // the operands is known to be nonzero.
4710 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
4711 !UnsafeFPMath &&
4712 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4713 break;
4714 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004715 break;
4716
4717 case ISD::SETOGT:
4718 case ISD::SETOGE:
4719 case ISD::SETGT:
4720 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004721 case ISD::SETUGT:
4722 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004723 // If LHS is NaN, an ordered comparison will be false and the result will
4724 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
4725 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4726 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
4727 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4728 break;
4729 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
4730 // will return +0, so vmax can only be used for unsafe math or if one of
4731 // the operands is known to be nonzero.
4732 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
4733 !UnsafeFPMath &&
4734 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4735 break;
4736 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004737 break;
4738 }
4739
4740 if (!Opcode)
4741 return SDValue();
4742 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
4743}
4744
Dan Gohman475871a2008-07-27 21:46:04 +00004745SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004746 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004747 switch (N->getOpcode()) {
4748 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004749 case ISD::ADD: return PerformADDCombine(N, DCI);
4750 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004751 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004752 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Jim Grosbache5165492009-11-09 00:11:35 +00004753 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson9e82bf12010-07-14 01:22:12 +00004754 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004755 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004756 case ISD::SHL:
4757 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004758 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004759 case ISD::SIGN_EXTEND:
4760 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004761 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
4762 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004763 }
Dan Gohman475871a2008-07-27 21:46:04 +00004764 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004765}
4766
Bill Wendlingaf566342009-08-15 21:21:19 +00004767bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
4768 if (!Subtarget->hasV6Ops())
4769 // Pre-v6 does not support unaligned mem access.
4770 return false;
Bob Wilson86fe66d2010-06-25 04:12:31 +00004771
4772 // v6+ may or may not support unaligned mem access depending on the system
4773 // configuration.
4774 // FIXME: This is pretty conservative. Should we provide cmdline option to
4775 // control the behaviour?
4776 if (!Subtarget->isTargetDarwin())
4777 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00004778
4779 switch (VT.getSimpleVT().SimpleTy) {
4780 default:
4781 return false;
4782 case MVT::i8:
4783 case MVT::i16:
4784 case MVT::i32:
4785 return true;
4786 // FIXME: VLD1 etc with standard alignment is legal.
4787 }
4788}
4789
Evan Chenge6c835f2009-08-14 20:09:37 +00004790static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
4791 if (V < 0)
4792 return false;
4793
4794 unsigned Scale = 1;
4795 switch (VT.getSimpleVT().SimpleTy) {
4796 default: return false;
4797 case MVT::i1:
4798 case MVT::i8:
4799 // Scale == 1;
4800 break;
4801 case MVT::i16:
4802 // Scale == 2;
4803 Scale = 2;
4804 break;
4805 case MVT::i32:
4806 // Scale == 4;
4807 Scale = 4;
4808 break;
4809 }
4810
4811 if ((V & (Scale - 1)) != 0)
4812 return false;
4813 V /= Scale;
4814 return V == (V & ((1LL << 5) - 1));
4815}
4816
4817static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
4818 const ARMSubtarget *Subtarget) {
4819 bool isNeg = false;
4820 if (V < 0) {
4821 isNeg = true;
4822 V = - V;
4823 }
4824
4825 switch (VT.getSimpleVT().SimpleTy) {
4826 default: return false;
4827 case MVT::i1:
4828 case MVT::i8:
4829 case MVT::i16:
4830 case MVT::i32:
4831 // + imm12 or - imm8
4832 if (isNeg)
4833 return V == (V & ((1LL << 8) - 1));
4834 return V == (V & ((1LL << 12) - 1));
4835 case MVT::f32:
4836 case MVT::f64:
4837 // Same as ARM mode. FIXME: NEON?
4838 if (!Subtarget->hasVFP2())
4839 return false;
4840 if ((V & 3) != 0)
4841 return false;
4842 V >>= 2;
4843 return V == (V & ((1LL << 8) - 1));
4844 }
4845}
4846
Evan Chengb01fad62007-03-12 23:30:29 +00004847/// isLegalAddressImmediate - Return true if the integer value can be used
4848/// as the offset of the target addressing mode for load / store of the
4849/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00004850static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004851 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00004852 if (V == 0)
4853 return true;
4854
Evan Cheng65011532009-03-09 19:15:00 +00004855 if (!VT.isSimple())
4856 return false;
4857
Evan Chenge6c835f2009-08-14 20:09:37 +00004858 if (Subtarget->isThumb1Only())
4859 return isLegalT1AddressImmediate(V, VT);
4860 else if (Subtarget->isThumb2())
4861 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00004862
Evan Chenge6c835f2009-08-14 20:09:37 +00004863 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00004864 if (V < 0)
4865 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00004866 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00004867 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004868 case MVT::i1:
4869 case MVT::i8:
4870 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00004871 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004872 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004873 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00004874 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004875 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004876 case MVT::f32:
4877 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00004878 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00004879 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00004880 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00004881 return false;
4882 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004883 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00004884 }
Evan Chenga8e29892007-01-19 07:51:42 +00004885}
4886
Evan Chenge6c835f2009-08-14 20:09:37 +00004887bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
4888 EVT VT) const {
4889 int Scale = AM.Scale;
4890 if (Scale < 0)
4891 return false;
4892
4893 switch (VT.getSimpleVT().SimpleTy) {
4894 default: return false;
4895 case MVT::i1:
4896 case MVT::i8:
4897 case MVT::i16:
4898 case MVT::i32:
4899 if (Scale == 1)
4900 return true;
4901 // r + r << imm
4902 Scale = Scale & ~1;
4903 return Scale == 2 || Scale == 4 || Scale == 8;
4904 case MVT::i64:
4905 // r + r
4906 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
4907 return true;
4908 return false;
4909 case MVT::isVoid:
4910 // Note, we allow "void" uses (basically, uses that aren't loads or
4911 // stores), because arm allows folding a scale into many arithmetic
4912 // operations. This should be made more precise and revisited later.
4913
4914 // Allow r << imm, but the imm has to be a multiple of two.
4915 if (Scale & 1) return false;
4916 return isPowerOf2_32(Scale);
4917 }
4918}
4919
Chris Lattner37caf8c2007-04-09 23:33:39 +00004920/// isLegalAddressingMode - Return true if the addressing mode represented
4921/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004922bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004923 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004924 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00004925 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00004926 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004927
Chris Lattner37caf8c2007-04-09 23:33:39 +00004928 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004929 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004930 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004931
Chris Lattner37caf8c2007-04-09 23:33:39 +00004932 switch (AM.Scale) {
4933 case 0: // no scale reg, must be "r+i" or "r", or "i".
4934 break;
4935 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00004936 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00004937 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004938 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00004939 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004940 // ARM doesn't support any R+R*scale+imm addr modes.
4941 if (AM.BaseOffs)
4942 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004943
Bob Wilson2c7dab12009-04-08 17:55:28 +00004944 if (!VT.isSimple())
4945 return false;
4946
Evan Chenge6c835f2009-08-14 20:09:37 +00004947 if (Subtarget->isThumb2())
4948 return isLegalT2ScaledAddressingMode(AM, VT);
4949
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004950 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00004951 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00004952 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004953 case MVT::i1:
4954 case MVT::i8:
4955 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004956 if (Scale < 0) Scale = -Scale;
4957 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004958 return true;
4959 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00004960 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00004961 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00004962 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004963 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004964 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004965 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00004966 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004967
Owen Anderson825b72b2009-08-11 20:47:22 +00004968 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004969 // Note, we allow "void" uses (basically, uses that aren't loads or
4970 // stores), because arm allows folding a scale into many arithmetic
4971 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004972
Chris Lattner37caf8c2007-04-09 23:33:39 +00004973 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00004974 if (Scale & 1) return false;
4975 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00004976 }
4977 break;
Evan Chengb01fad62007-03-12 23:30:29 +00004978 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00004979 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00004980}
4981
Evan Cheng77e47512009-11-11 19:05:52 +00004982/// isLegalICmpImmediate - Return true if the specified immediate is legal
4983/// icmp immediate, that is the target has icmp instructions which can compare
4984/// a register against the immediate without having to materialize the
4985/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00004986bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00004987 if (!Subtarget->isThumb())
4988 return ARM_AM::getSOImmVal(Imm) != -1;
4989 if (Subtarget->isThumb2())
4990 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00004991 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00004992}
4993
Owen Andersone50ed302009-08-10 22:56:29 +00004994static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004995 bool isSEXTLoad, SDValue &Base,
4996 SDValue &Offset, bool &isInc,
4997 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00004998 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4999 return false;
5000
Owen Anderson825b72b2009-08-11 20:47:22 +00005001 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00005002 // AddressingMode 3
5003 Base = Ptr->getOperand(0);
5004 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005005 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005006 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005007 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00005008 isInc = false;
5009 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5010 return true;
5011 }
5012 }
5013 isInc = (Ptr->getOpcode() == ISD::ADD);
5014 Offset = Ptr->getOperand(1);
5015 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00005016 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00005017 // AddressingMode 2
5018 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005019 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005020 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005021 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00005022 isInc = false;
5023 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5024 Base = Ptr->getOperand(0);
5025 return true;
5026 }
5027 }
5028
5029 if (Ptr->getOpcode() == ISD::ADD) {
5030 isInc = true;
5031 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
5032 if (ShOpcVal != ARM_AM::no_shift) {
5033 Base = Ptr->getOperand(1);
5034 Offset = Ptr->getOperand(0);
5035 } else {
5036 Base = Ptr->getOperand(0);
5037 Offset = Ptr->getOperand(1);
5038 }
5039 return true;
5040 }
5041
5042 isInc = (Ptr->getOpcode() == ISD::ADD);
5043 Base = Ptr->getOperand(0);
5044 Offset = Ptr->getOperand(1);
5045 return true;
5046 }
5047
Jim Grosbache5165492009-11-09 00:11:35 +00005048 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00005049 return false;
5050}
5051
Owen Andersone50ed302009-08-10 22:56:29 +00005052static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00005053 bool isSEXTLoad, SDValue &Base,
5054 SDValue &Offset, bool &isInc,
5055 SelectionDAG &DAG) {
5056 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
5057 return false;
5058
5059 Base = Ptr->getOperand(0);
5060 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
5061 int RHSC = (int)RHS->getZExtValue();
5062 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
5063 assert(Ptr->getOpcode() == ISD::ADD);
5064 isInc = false;
5065 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5066 return true;
5067 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
5068 isInc = Ptr->getOpcode() == ISD::ADD;
5069 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
5070 return true;
5071 }
5072 }
5073
5074 return false;
5075}
5076
Evan Chenga8e29892007-01-19 07:51:42 +00005077/// getPreIndexedAddressParts - returns true by value, base pointer and
5078/// offset pointer and addressing mode by reference if the node's address
5079/// can be legally represented as pre-indexed load / store address.
5080bool
Dan Gohman475871a2008-07-27 21:46:04 +00005081ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
5082 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00005083 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00005084 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005085 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00005086 return false;
5087
Owen Andersone50ed302009-08-10 22:56:29 +00005088 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00005089 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00005090 bool isSEXTLoad = false;
5091 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5092 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005093 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00005094 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
5095 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
5096 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005097 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00005098 } else
5099 return false;
5100
5101 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00005102 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00005103 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00005104 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
5105 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00005106 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00005107 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00005108 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00005109 if (!isLegal)
5110 return false;
5111
5112 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
5113 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00005114}
5115
5116/// getPostIndexedAddressParts - returns true by value, base pointer and
5117/// offset pointer and addressing mode by reference if this node can be
5118/// combined with a load / store to form a post-indexed load / store.
5119bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00005120 SDValue &Base,
5121 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00005122 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00005123 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005124 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00005125 return false;
5126
Owen Andersone50ed302009-08-10 22:56:29 +00005127 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00005128 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00005129 bool isSEXTLoad = false;
5130 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005131 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00005132 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00005133 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
5134 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005135 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00005136 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00005137 } else
5138 return false;
5139
5140 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00005141 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00005142 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00005143 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00005144 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00005145 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00005146 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
5147 isInc, DAG);
5148 if (!isLegal)
5149 return false;
5150
Evan Cheng28dad2a2010-05-18 21:31:17 +00005151 if (Ptr != Base) {
5152 // Swap base ptr and offset to catch more post-index load / store when
5153 // it's legal. In Thumb2 mode, offset must be an immediate.
5154 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
5155 !Subtarget->isThumb2())
5156 std::swap(Base, Offset);
5157
5158 // Post-indexed load / store update the base pointer.
5159 if (Ptr != Base)
5160 return false;
5161 }
5162
Evan Chenge88d5ce2009-07-02 07:28:31 +00005163 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
5164 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00005165}
5166
Dan Gohman475871a2008-07-27 21:46:04 +00005167void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00005168 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00005169 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005170 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00005171 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00005172 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005173 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00005174 switch (Op.getOpcode()) {
5175 default: break;
5176 case ARMISD::CMOV: {
5177 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00005178 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00005179 if (KnownZero == 0 && KnownOne == 0) return;
5180
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005181 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00005182 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
5183 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00005184 KnownZero &= KnownZeroRHS;
5185 KnownOne &= KnownOneRHS;
5186 return;
5187 }
5188 }
5189}
5190
5191//===----------------------------------------------------------------------===//
5192// ARM Inline Assembly Support
5193//===----------------------------------------------------------------------===//
5194
5195/// getConstraintType - Given a constraint letter, return the type of
5196/// constraint it is for this target.
5197ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00005198ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
5199 if (Constraint.size() == 1) {
5200 switch (Constraint[0]) {
5201 default: break;
5202 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005203 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00005204 }
Evan Chenga8e29892007-01-19 07:51:42 +00005205 }
Chris Lattner4234f572007-03-25 02:14:49 +00005206 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00005207}
5208
Bob Wilson2dc4f542009-03-20 22:42:55 +00005209std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00005210ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00005211 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005212 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00005213 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00005214 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005215 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00005216 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00005217 return std::make_pair(0U, ARM::tGPRRegisterClass);
5218 else
5219 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005220 case 'r':
5221 return std::make_pair(0U, ARM::GPRRegisterClass);
5222 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00005223 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005224 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00005225 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005226 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00005227 if (VT.getSizeInBits() == 128)
5228 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005229 break;
Evan Chenga8e29892007-01-19 07:51:42 +00005230 }
5231 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00005232 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00005233 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00005234
Evan Chenga8e29892007-01-19 07:51:42 +00005235 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
5236}
5237
5238std::vector<unsigned> ARMTargetLowering::
5239getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00005240 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005241 if (Constraint.size() != 1)
5242 return std::vector<unsigned>();
5243
5244 switch (Constraint[0]) { // GCC ARM Constraint Letters
5245 default: break;
5246 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00005247 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
5248 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
5249 0);
Evan Chenga8e29892007-01-19 07:51:42 +00005250 case 'r':
5251 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
5252 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
5253 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
5254 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005255 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00005256 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005257 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
5258 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
5259 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
5260 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
5261 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
5262 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
5263 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
5264 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00005265 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005266 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
5267 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
5268 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
5269 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00005270 if (VT.getSizeInBits() == 128)
5271 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
5272 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005273 break;
Evan Chenga8e29892007-01-19 07:51:42 +00005274 }
5275
5276 return std::vector<unsigned>();
5277}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005278
5279/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
5280/// vector. If it is invalid, don't add anything to Ops.
5281void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
5282 char Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005283 std::vector<SDValue>&Ops,
5284 SelectionDAG &DAG) const {
5285 SDValue Result(0, 0);
5286
5287 switch (Constraint) {
5288 default: break;
5289 case 'I': case 'J': case 'K': case 'L':
5290 case 'M': case 'N': case 'O':
5291 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
5292 if (!C)
5293 return;
5294
5295 int64_t CVal64 = C->getSExtValue();
5296 int CVal = (int) CVal64;
5297 // None of these constraints allow values larger than 32 bits. Check
5298 // that the value fits in an int.
5299 if (CVal != CVal64)
5300 return;
5301
5302 switch (Constraint) {
5303 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005304 if (Subtarget->isThumb1Only()) {
5305 // This must be a constant between 0 and 255, for ADD
5306 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005307 if (CVal >= 0 && CVal <= 255)
5308 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005309 } else if (Subtarget->isThumb2()) {
5310 // A constant that can be used as an immediate value in a
5311 // data-processing instruction.
5312 if (ARM_AM::getT2SOImmVal(CVal) != -1)
5313 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005314 } else {
5315 // A constant that can be used as an immediate value in a
5316 // data-processing instruction.
5317 if (ARM_AM::getSOImmVal(CVal) != -1)
5318 break;
5319 }
5320 return;
5321
5322 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005323 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005324 // This must be a constant between -255 and -1, for negated ADD
5325 // immediates. This can be used in GCC with an "n" modifier that
5326 // prints the negated value, for use with SUB instructions. It is
5327 // not useful otherwise but is implemented for compatibility.
5328 if (CVal >= -255 && CVal <= -1)
5329 break;
5330 } else {
5331 // This must be a constant between -4095 and 4095. It is not clear
5332 // what this constraint is intended for. Implemented for
5333 // compatibility with GCC.
5334 if (CVal >= -4095 && CVal <= 4095)
5335 break;
5336 }
5337 return;
5338
5339 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005340 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005341 // A 32-bit value where only one byte has a nonzero value. Exclude
5342 // zero to match GCC. This constraint is used by GCC internally for
5343 // constants that can be loaded with a move/shift combination.
5344 // It is not useful otherwise but is implemented for compatibility.
5345 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
5346 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005347 } else if (Subtarget->isThumb2()) {
5348 // A constant whose bitwise inverse can be used as an immediate
5349 // value in a data-processing instruction. This can be used in GCC
5350 // with a "B" modifier that prints the inverted value, for use with
5351 // BIC and MVN instructions. It is not useful otherwise but is
5352 // implemented for compatibility.
5353 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
5354 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005355 } else {
5356 // A constant whose bitwise inverse can be used as an immediate
5357 // value in a data-processing instruction. This can be used in GCC
5358 // with a "B" modifier that prints the inverted value, for use with
5359 // BIC and MVN instructions. It is not useful otherwise but is
5360 // implemented for compatibility.
5361 if (ARM_AM::getSOImmVal(~CVal) != -1)
5362 break;
5363 }
5364 return;
5365
5366 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005367 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005368 // This must be a constant between -7 and 7,
5369 // for 3-operand ADD/SUB immediate instructions.
5370 if (CVal >= -7 && CVal < 7)
5371 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005372 } else if (Subtarget->isThumb2()) {
5373 // A constant whose negation can be used as an immediate value in a
5374 // data-processing instruction. This can be used in GCC with an "n"
5375 // modifier that prints the negated value, for use with SUB
5376 // instructions. It is not useful otherwise but is implemented for
5377 // compatibility.
5378 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
5379 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005380 } else {
5381 // A constant whose negation can be used as an immediate value in a
5382 // data-processing instruction. This can be used in GCC with an "n"
5383 // modifier that prints the negated value, for use with SUB
5384 // instructions. It is not useful otherwise but is implemented for
5385 // compatibility.
5386 if (ARM_AM::getSOImmVal(-CVal) != -1)
5387 break;
5388 }
5389 return;
5390
5391 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005392 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005393 // This must be a multiple of 4 between 0 and 1020, for
5394 // ADD sp + immediate.
5395 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
5396 break;
5397 } else {
5398 // A power of two or a constant between 0 and 32. This is used in
5399 // GCC for the shift amount on shifted register operands, but it is
5400 // useful in general for any shift amounts.
5401 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
5402 break;
5403 }
5404 return;
5405
5406 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005407 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005408 // This must be a constant between 0 and 31, for shift amounts.
5409 if (CVal >= 0 && CVal <= 31)
5410 break;
5411 }
5412 return;
5413
5414 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005415 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005416 // This must be a multiple of 4 between -508 and 508, for
5417 // ADD/SUB sp = sp + immediate.
5418 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
5419 break;
5420 }
5421 return;
5422 }
5423 Result = DAG.getTargetConstant(CVal, Op.getValueType());
5424 break;
5425 }
5426
5427 if (Result.getNode()) {
5428 Ops.push_back(Result);
5429 return;
5430 }
Dale Johannesen1784d162010-06-25 21:55:36 +00005431 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005432}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00005433
5434bool
5435ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
5436 // The ARM target isn't yet aware of offsets.
5437 return false;
5438}
Evan Cheng39382422009-10-28 01:44:26 +00005439
5440int ARM::getVFPf32Imm(const APFloat &FPImm) {
5441 APInt Imm = FPImm.bitcastToAPInt();
5442 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
5443 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
5444 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
5445
5446 // We can handle 4 bits of mantissa.
5447 // mantissa = (16+UInt(e:f:g:h))/16.
5448 if (Mantissa & 0x7ffff)
5449 return -1;
5450 Mantissa >>= 19;
5451 if ((Mantissa & 0xf) != Mantissa)
5452 return -1;
5453
5454 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
5455 if (Exp < -3 || Exp > 4)
5456 return -1;
5457 Exp = ((Exp+3) & 0x7) ^ 4;
5458
5459 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
5460}
5461
5462int ARM::getVFPf64Imm(const APFloat &FPImm) {
5463 APInt Imm = FPImm.bitcastToAPInt();
5464 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
5465 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
5466 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
5467
5468 // We can handle 4 bits of mantissa.
5469 // mantissa = (16+UInt(e:f:g:h))/16.
5470 if (Mantissa & 0xffffffffffffLL)
5471 return -1;
5472 Mantissa >>= 48;
5473 if ((Mantissa & 0xf) != Mantissa)
5474 return -1;
5475
5476 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
5477 if (Exp < -3 || Exp > 4)
5478 return -1;
5479 Exp = ((Exp+3) & 0x7) ^ 4;
5480
5481 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
5482}
5483
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005484bool ARM::isBitFieldInvertedMask(unsigned v) {
5485 if (v == 0xffffffff)
5486 return 0;
5487 // there can be 1's on either or both "outsides", all the "inside"
5488 // bits must be 0's
5489 unsigned int lsb = 0, msb = 31;
5490 while (v & (1 << msb)) --msb;
5491 while (v & (1 << lsb)) ++lsb;
5492 for (unsigned int i = lsb; i <= msb; ++i) {
5493 if (v & (1 << i))
5494 return 0;
5495 }
5496 return 1;
5497}
5498
Evan Cheng39382422009-10-28 01:44:26 +00005499/// isFPImmLegal - Returns true if the target can instruction select the
5500/// specified FP immediate natively. If false, the legalizer will
5501/// materialize the FP immediate as a load from a constant pool.
5502bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
5503 if (!Subtarget->hasVFP3())
5504 return false;
5505 if (VT == MVT::f32)
5506 return ARM::getVFPf32Imm(Imm) != -1;
5507 if (VT == MVT::f64)
5508 return ARM::getVFPf64Imm(Imm) != -1;
5509 return false;
5510}