blob: f6d25d8706c2a6214b478513245e6394046af924 [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 Chengd70f57b2010-07-19 22:15:08 +0000553const TargetRegisterClass *
554ARMTargetLowering::findRepresentativeClass(const TargetRegisterClass *RC) const{
555 switch (RC->getID()) {
556 default:
557 return RC;
558 case ARM::tGPRRegClassID:
559 case ARM::GPRRegClassID:
560 return ARM::GPRRegisterClass;
561 case ARM::SPRRegClassID:
562 case ARM::DPRRegClassID:
563 return ARM::DPRRegisterClass;
564 case ARM::QPRRegClassID:
565 return ARM::QPRRegisterClass;
566 }
567}
568
Evan Chenga8e29892007-01-19 07:51:42 +0000569const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
570 switch (Opcode) {
571 default: return 0;
572 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000573 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
574 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000575 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000576 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
577 case ARMISD::tCALL: return "ARMISD::tCALL";
578 case ARMISD::BRCOND: return "ARMISD::BRCOND";
579 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000580 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000581 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
582 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
583 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000584 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000585 case ARMISD::CMPFP: return "ARMISD::CMPFP";
586 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000587 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000588 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
589 case ARMISD::CMOV: return "ARMISD::CMOV";
590 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000591
Jim Grosbach3482c802010-01-18 19:58:49 +0000592 case ARMISD::RBIT: return "ARMISD::RBIT";
593
Bob Wilson76a312b2010-03-19 22:51:32 +0000594 case ARMISD::FTOSI: return "ARMISD::FTOSI";
595 case ARMISD::FTOUI: return "ARMISD::FTOUI";
596 case ARMISD::SITOF: return "ARMISD::SITOF";
597 case ARMISD::UITOF: return "ARMISD::UITOF";
598
Evan Chenga8e29892007-01-19 07:51:42 +0000599 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
600 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
601 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000602
Jim Grosbache5165492009-11-09 00:11:35 +0000603 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
604 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000605
Evan Chengc5942082009-10-28 06:55:03 +0000606 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
607 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
608
Dale Johannesen51e28e62010-06-03 21:09:53 +0000609 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
610
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000611 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000612
Evan Cheng86198642009-08-07 00:34:42 +0000613 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
614
Jim Grosbach3728e962009-12-10 00:11:09 +0000615 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
616 case ARMISD::SYNCBARRIER: return "ARMISD::SYNCBARRIER";
617
Bob Wilson5bafff32009-06-22 23:27:02 +0000618 case ARMISD::VCEQ: return "ARMISD::VCEQ";
619 case ARMISD::VCGE: return "ARMISD::VCGE";
620 case ARMISD::VCGEU: return "ARMISD::VCGEU";
621 case ARMISD::VCGT: return "ARMISD::VCGT";
622 case ARMISD::VCGTU: return "ARMISD::VCGTU";
623 case ARMISD::VTST: return "ARMISD::VTST";
624
625 case ARMISD::VSHL: return "ARMISD::VSHL";
626 case ARMISD::VSHRs: return "ARMISD::VSHRs";
627 case ARMISD::VSHRu: return "ARMISD::VSHRu";
628 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
629 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
630 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
631 case ARMISD::VSHRN: return "ARMISD::VSHRN";
632 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
633 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
634 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
635 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
636 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
637 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
638 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
639 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
640 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
641 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
642 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
643 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
644 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
645 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000646 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000647 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000648 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000649 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000650 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000651 case ARMISD::VREV64: return "ARMISD::VREV64";
652 case ARMISD::VREV32: return "ARMISD::VREV32";
653 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000654 case ARMISD::VZIP: return "ARMISD::VZIP";
655 case ARMISD::VUZP: return "ARMISD::VUZP";
656 case ARMISD::VTRN: return "ARMISD::VTRN";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000657 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000658 case ARMISD::FMAX: return "ARMISD::FMAX";
659 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000660 case ARMISD::BFI: return "ARMISD::BFI";
Evan Chenga8e29892007-01-19 07:51:42 +0000661 }
662}
663
Evan Cheng06b666c2010-05-15 02:18:07 +0000664/// getRegClassFor - Return the register class that should be used for the
665/// specified value type.
666TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
667 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
668 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
669 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000670 if (Subtarget->hasNEON()) {
671 if (VT == MVT::v4i64)
672 return ARM::QQPRRegisterClass;
673 else if (VT == MVT::v8i64)
674 return ARM::QQQQPRRegisterClass;
675 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000676 return TargetLowering::getRegClassFor(VT);
677}
678
Bill Wendlingb4202b82009-07-01 18:50:55 +0000679/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000680unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Bob Wilsonb5b50572010-07-01 22:26:26 +0000681 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 1 : 2;
Bill Wendling20c568f2009-06-30 22:38:32 +0000682}
683
Evan Cheng1cc39842010-05-20 23:26:43 +0000684Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000685 unsigned NumVals = N->getNumValues();
686 if (!NumVals)
687 return Sched::RegPressure;
688
689 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000690 EVT VT = N->getValueType(i);
691 if (VT.isFloatingPoint() || VT.isVector())
692 return Sched::Latency;
693 }
Evan Chengc10f5432010-05-28 23:25:23 +0000694
695 if (!N->isMachineOpcode())
696 return Sched::RegPressure;
697
698 // Load are scheduled for latency even if there instruction itinerary
699 // is not available.
700 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
701 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
702 if (TID.mayLoad())
703 return Sched::Latency;
704
705 const InstrItineraryData &Itins = getTargetMachine().getInstrItineraryData();
706 if (!Itins.isEmpty() && Itins.getStageLatency(TID.getSchedClass()) > 2)
707 return Sched::Latency;
Evan Cheng1cc39842010-05-20 23:26:43 +0000708 return Sched::RegPressure;
709}
710
Evan Chenga8e29892007-01-19 07:51:42 +0000711//===----------------------------------------------------------------------===//
712// Lowering Code
713//===----------------------------------------------------------------------===//
714
Evan Chenga8e29892007-01-19 07:51:42 +0000715/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
716static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
717 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000718 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000719 case ISD::SETNE: return ARMCC::NE;
720 case ISD::SETEQ: return ARMCC::EQ;
721 case ISD::SETGT: return ARMCC::GT;
722 case ISD::SETGE: return ARMCC::GE;
723 case ISD::SETLT: return ARMCC::LT;
724 case ISD::SETLE: return ARMCC::LE;
725 case ISD::SETUGT: return ARMCC::HI;
726 case ISD::SETUGE: return ARMCC::HS;
727 case ISD::SETULT: return ARMCC::LO;
728 case ISD::SETULE: return ARMCC::LS;
729 }
730}
731
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000732/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
733static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000734 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000735 CondCode2 = ARMCC::AL;
736 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000737 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000738 case ISD::SETEQ:
739 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
740 case ISD::SETGT:
741 case ISD::SETOGT: CondCode = ARMCC::GT; break;
742 case ISD::SETGE:
743 case ISD::SETOGE: CondCode = ARMCC::GE; break;
744 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000745 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000746 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
747 case ISD::SETO: CondCode = ARMCC::VC; break;
748 case ISD::SETUO: CondCode = ARMCC::VS; break;
749 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
750 case ISD::SETUGT: CondCode = ARMCC::HI; break;
751 case ISD::SETUGE: CondCode = ARMCC::PL; break;
752 case ISD::SETLT:
753 case ISD::SETULT: CondCode = ARMCC::LT; break;
754 case ISD::SETLE:
755 case ISD::SETULE: CondCode = ARMCC::LE; break;
756 case ISD::SETNE:
757 case ISD::SETUNE: CondCode = ARMCC::NE; break;
758 }
Evan Chenga8e29892007-01-19 07:51:42 +0000759}
760
Bob Wilson1f595bb2009-04-17 19:07:39 +0000761//===----------------------------------------------------------------------===//
762// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000763//===----------------------------------------------------------------------===//
764
765#include "ARMGenCallingConv.inc"
766
767// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000768static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000769 CCValAssign::LocInfo &LocInfo,
770 CCState &State, bool CanFail) {
771 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
772
773 // Try to get the first register.
774 if (unsigned Reg = State.AllocateReg(RegList, 4))
775 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
776 else {
777 // For the 2nd half of a v2f64, do not fail.
778 if (CanFail)
779 return false;
780
781 // Put the whole thing on the stack.
782 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
783 State.AllocateStack(8, 4),
784 LocVT, LocInfo));
785 return true;
786 }
787
788 // Try to get the second register.
789 if (unsigned Reg = State.AllocateReg(RegList, 4))
790 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
791 else
792 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
793 State.AllocateStack(4, 4),
794 LocVT, LocInfo));
795 return true;
796}
797
Owen Andersone50ed302009-08-10 22:56:29 +0000798static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000799 CCValAssign::LocInfo &LocInfo,
800 ISD::ArgFlagsTy &ArgFlags,
801 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000802 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
803 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000804 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000805 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
806 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000807 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000808}
809
810// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000811static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000812 CCValAssign::LocInfo &LocInfo,
813 CCState &State, bool CanFail) {
814 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
815 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
816
817 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
818 if (Reg == 0) {
819 // For the 2nd half of a v2f64, do not just fail.
820 if (CanFail)
821 return false;
822
823 // Put the whole thing on the stack.
824 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
825 State.AllocateStack(8, 8),
826 LocVT, LocInfo));
827 return true;
828 }
829
830 unsigned i;
831 for (i = 0; i < 2; ++i)
832 if (HiRegList[i] == Reg)
833 break;
834
835 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
836 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
837 LocVT, LocInfo));
838 return true;
839}
840
Owen Andersone50ed302009-08-10 22:56:29 +0000841static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000842 CCValAssign::LocInfo &LocInfo,
843 ISD::ArgFlagsTy &ArgFlags,
844 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000845 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
846 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000847 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000848 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
849 return false;
850 return true; // we handled it
851}
852
Owen Andersone50ed302009-08-10 22:56:29 +0000853static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000854 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000855 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
856 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
857
Bob Wilsone65586b2009-04-17 20:40:45 +0000858 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
859 if (Reg == 0)
860 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000861
Bob Wilsone65586b2009-04-17 20:40:45 +0000862 unsigned i;
863 for (i = 0; i < 2; ++i)
864 if (HiRegList[i] == Reg)
865 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000866
Bob Wilson5bafff32009-06-22 23:27:02 +0000867 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000868 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000869 LocVT, LocInfo));
870 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000871}
872
Owen Andersone50ed302009-08-10 22:56:29 +0000873static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000874 CCValAssign::LocInfo &LocInfo,
875 ISD::ArgFlagsTy &ArgFlags,
876 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000877 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
878 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000879 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000880 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000881 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000882}
883
Owen Andersone50ed302009-08-10 22:56:29 +0000884static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000885 CCValAssign::LocInfo &LocInfo,
886 ISD::ArgFlagsTy &ArgFlags,
887 CCState &State) {
888 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
889 State);
890}
891
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000892/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
893/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000894CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000895 bool Return,
896 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000897 switch (CC) {
898 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000899 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000900 case CallingConv::C:
901 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000902 // Use target triple & subtarget features to do actual dispatch.
903 if (Subtarget->isAAPCS_ABI()) {
904 if (Subtarget->hasVFP2() &&
905 FloatABIType == FloatABI::Hard && !isVarArg)
906 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
907 else
908 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
909 } else
910 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000911 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000912 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000913 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000914 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000915 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000916 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000917 }
918}
919
Dan Gohman98ca4f22009-08-05 01:29:28 +0000920/// LowerCallResult - Lower the result values of a call into the
921/// appropriate copies out of appropriate physical registers.
922SDValue
923ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000924 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000925 const SmallVectorImpl<ISD::InputArg> &Ins,
926 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000927 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000928
Bob Wilson1f595bb2009-04-17 19:07:39 +0000929 // Assign locations to each value returned by this call.
930 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000931 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000932 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000933 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000934 CCAssignFnForNode(CallConv, /* Return*/ true,
935 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000936
937 // Copy all of the result registers out of their specified physreg.
938 for (unsigned i = 0; i != RVLocs.size(); ++i) {
939 CCValAssign VA = RVLocs[i];
940
Bob Wilson80915242009-04-25 00:33:20 +0000941 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000942 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000943 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000944 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000945 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000946 Chain = Lo.getValue(1);
947 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000948 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000949 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000950 InFlag);
951 Chain = Hi.getValue(1);
952 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000953 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000954
Owen Anderson825b72b2009-08-11 20:47:22 +0000955 if (VA.getLocVT() == MVT::v2f64) {
956 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
957 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
958 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000959
960 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000961 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000962 Chain = Lo.getValue(1);
963 InFlag = Lo.getValue(2);
964 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000965 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000966 Chain = Hi.getValue(1);
967 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000968 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +0000969 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
970 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000971 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000972 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000973 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
974 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000975 Chain = Val.getValue(1);
976 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000977 }
Bob Wilson80915242009-04-25 00:33:20 +0000978
979 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000980 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000981 case CCValAssign::Full: break;
982 case CCValAssign::BCvt:
983 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
984 break;
985 }
986
Dan Gohman98ca4f22009-08-05 01:29:28 +0000987 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000988 }
989
Dan Gohman98ca4f22009-08-05 01:29:28 +0000990 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000991}
992
993/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
994/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000995/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000996/// a byval function parameter.
997/// Sometimes what we are copying is the end of a larger object, the part that
998/// does not fit in registers.
999static SDValue
1000CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
1001 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1002 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001003 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001004 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001005 /*isVolatile=*/false, /*AlwaysInline=*/false,
1006 NULL, 0, NULL, 0);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001007}
1008
Bob Wilsondee46d72009-04-17 20:35:10 +00001009/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001010SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001011ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1012 SDValue StackPtr, SDValue Arg,
1013 DebugLoc dl, SelectionDAG &DAG,
1014 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001015 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001016 unsigned LocMemOffset = VA.getLocMemOffset();
1017 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1018 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
1019 if (Flags.isByVal()) {
1020 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
1021 }
1022 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene1b58cab2010-02-15 16:55:24 +00001023 PseudoSourceValue::getStack(), LocMemOffset,
1024 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001025}
1026
Dan Gohman98ca4f22009-08-05 01:29:28 +00001027void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001028 SDValue Chain, SDValue &Arg,
1029 RegsToPassVector &RegsToPass,
1030 CCValAssign &VA, CCValAssign &NextVA,
1031 SDValue &StackPtr,
1032 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001033 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001034
Jim Grosbache5165492009-11-09 00:11:35 +00001035 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001036 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001037 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1038
1039 if (NextVA.isRegLoc())
1040 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1041 else {
1042 assert(NextVA.isMemLoc());
1043 if (StackPtr.getNode() == 0)
1044 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1045
Dan Gohman98ca4f22009-08-05 01:29:28 +00001046 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1047 dl, DAG, NextVA,
1048 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001049 }
1050}
1051
Dan Gohman98ca4f22009-08-05 01:29:28 +00001052/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001053/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1054/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001055SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001056ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001057 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001058 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001059 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001060 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001061 const SmallVectorImpl<ISD::InputArg> &Ins,
1062 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001063 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001064 MachineFunction &MF = DAG.getMachineFunction();
1065 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1066 bool IsSibCall = false;
Dale Johannesen8fa8e7f2010-06-04 18:04:24 +00001067 // Temporarily disable tail calls so things don't break.
1068 if (!EnableARMTailCalls)
1069 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001070 if (isTailCall) {
1071 // Check if it's really possible to do a tail call.
1072 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1073 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001074 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001075 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1076 // detected sibcalls.
1077 if (isTailCall) {
1078 ++NumTailCalls;
1079 IsSibCall = true;
1080 }
1081 }
Evan Chenga8e29892007-01-19 07:51:42 +00001082
Bob Wilson1f595bb2009-04-17 19:07:39 +00001083 // Analyze operands of the call, assigning locations to each operand.
1084 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001085 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1086 *DAG.getContext());
1087 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001088 CCAssignFnForNode(CallConv, /* Return*/ false,
1089 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001090
Bob Wilson1f595bb2009-04-17 19:07:39 +00001091 // Get a count of how many bytes are to be pushed on the stack.
1092 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001093
Dale Johannesen51e28e62010-06-03 21:09:53 +00001094 // For tail calls, memory operands are available in our caller's stack.
1095 if (IsSibCall)
1096 NumBytes = 0;
1097
Evan Chenga8e29892007-01-19 07:51:42 +00001098 // Adjust the stack pointer for the new arguments...
1099 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001100 if (!IsSibCall)
1101 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001102
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001103 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001104
Bob Wilson5bafff32009-06-22 23:27:02 +00001105 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001106 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001107
Bob Wilson1f595bb2009-04-17 19:07:39 +00001108 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001109 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001110 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1111 i != e;
1112 ++i, ++realArgIdx) {
1113 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001114 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001115 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +00001116
Bob Wilson1f595bb2009-04-17 19:07:39 +00001117 // Promote the value if needed.
1118 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001119 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001120 case CCValAssign::Full: break;
1121 case CCValAssign::SExt:
1122 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1123 break;
1124 case CCValAssign::ZExt:
1125 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1126 break;
1127 case CCValAssign::AExt:
1128 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1129 break;
1130 case CCValAssign::BCvt:
1131 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1132 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001133 }
1134
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001135 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001136 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001137 if (VA.getLocVT() == MVT::v2f64) {
1138 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1139 DAG.getConstant(0, MVT::i32));
1140 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1141 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001142
Dan Gohman98ca4f22009-08-05 01:29:28 +00001143 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001144 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1145
1146 VA = ArgLocs[++i]; // skip ahead to next loc
1147 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001148 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001149 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1150 } else {
1151 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001152
Dan Gohman98ca4f22009-08-05 01:29:28 +00001153 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1154 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001155 }
1156 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001157 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001158 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001159 }
1160 } else if (VA.isRegLoc()) {
1161 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001162 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001163 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001164
Dan Gohman98ca4f22009-08-05 01:29:28 +00001165 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1166 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001167 }
Evan Chenga8e29892007-01-19 07:51:42 +00001168 }
1169
1170 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001171 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001172 &MemOpChains[0], MemOpChains.size());
1173
1174 // Build a sequence of copy-to-reg nodes chained together with token chain
1175 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001176 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001177 // Tail call byval lowering might overwrite argument registers so in case of
1178 // tail call optimization the copies to registers are lowered later.
1179 if (!isTailCall)
1180 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1181 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1182 RegsToPass[i].second, InFlag);
1183 InFlag = Chain.getValue(1);
1184 }
Evan Chenga8e29892007-01-19 07:51:42 +00001185
Dale Johannesen51e28e62010-06-03 21:09:53 +00001186 // For tail calls lower the arguments to the 'real' stack slot.
1187 if (isTailCall) {
1188 // Force all the incoming stack arguments to be loaded from the stack
1189 // before any new outgoing arguments are stored to the stack, because the
1190 // outgoing stack slots may alias the incoming argument stack slots, and
1191 // the alias isn't otherwise explicit. This is slightly more conservative
1192 // than necessary, because it means that each store effectively depends
1193 // on every argument instead of just those arguments it would clobber.
1194
1195 // Do not flag preceeding copytoreg stuff together with the following stuff.
1196 InFlag = SDValue();
1197 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1198 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1199 RegsToPass[i].second, InFlag);
1200 InFlag = Chain.getValue(1);
1201 }
1202 InFlag =SDValue();
1203 }
1204
Bill Wendling056292f2008-09-16 21:48:12 +00001205 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1206 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1207 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001208 bool isDirect = false;
1209 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001210 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001211 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001212
1213 if (EnableARMLongCalls) {
1214 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1215 && "long-calls with non-static relocation model!");
1216 // Handle a global address or an external symbol. If it's not one of
1217 // those, the target's already in a register, so we don't need to do
1218 // anything extra.
1219 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001220 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001221 // Create a constant pool entry for the callee address
1222 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1223 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1224 ARMPCLabelIndex,
1225 ARMCP::CPValue, 0);
1226 // Get the address of the callee into a register
1227 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1228 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1229 Callee = DAG.getLoad(getPointerTy(), dl,
1230 DAG.getEntryNode(), CPAddr,
1231 PseudoSourceValue::getConstantPool(), 0,
1232 false, false, 0);
1233 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1234 const char *Sym = S->getSymbol();
1235
1236 // Create a constant pool entry for the callee address
1237 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1238 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1239 Sym, ARMPCLabelIndex, 0);
1240 // Get the address of the callee into a register
1241 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1242 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1243 Callee = DAG.getLoad(getPointerTy(), dl,
1244 DAG.getEntryNode(), CPAddr,
1245 PseudoSourceValue::getConstantPool(), 0,
1246 false, false, 0);
1247 }
1248 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001249 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001250 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001251 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001252 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001253 getTargetMachine().getRelocationModel() != Reloc::Static;
1254 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001255 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001256 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001257 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001258 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001259 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001260 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001261 ARMPCLabelIndex,
1262 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001263 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001264 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001265 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001266 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001267 PseudoSourceValue::getConstantPool(), 0,
1268 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001269 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001270 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001271 getPointerTy(), Callee, PICLabel);
Jim Grosbache7b52522010-04-14 22:28:31 +00001272 } else
Devang Patel0d881da2010-07-06 22:08:15 +00001273 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001274 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001275 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001276 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001277 getTargetMachine().getRelocationModel() != Reloc::Static;
1278 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001279 // tBX takes a register source operand.
1280 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001281 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001282 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001283 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001284 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001285 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001286 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001287 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001288 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001289 PseudoSourceValue::getConstantPool(), 0,
1290 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001291 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001292 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001293 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001294 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001295 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001296 }
1297
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001298 // FIXME: handle tail calls differently.
1299 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001300 if (Subtarget->isThumb()) {
1301 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001302 CallOpc = ARMISD::CALL_NOLINK;
1303 else
1304 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1305 } else {
1306 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001307 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1308 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001309 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001310
Dan Gohman475871a2008-07-27 21:46:04 +00001311 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001312 Ops.push_back(Chain);
1313 Ops.push_back(Callee);
1314
1315 // Add argument registers to the end of the list so that they are known live
1316 // into the call.
1317 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1318 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1319 RegsToPass[i].second.getValueType()));
1320
Gabor Greifba36cb52008-08-28 21:40:38 +00001321 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001322 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001323
1324 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001325 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001326 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001327
Duncan Sands4bdcb612008-07-02 17:40:58 +00001328 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001329 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001330 InFlag = Chain.getValue(1);
1331
Chris Lattnere563bbc2008-10-11 22:08:30 +00001332 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1333 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001334 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001335 InFlag = Chain.getValue(1);
1336
Bob Wilson1f595bb2009-04-17 19:07:39 +00001337 // Handle result values, copying them out of physregs into vregs that we
1338 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001339 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1340 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001341}
1342
Dale Johannesen51e28e62010-06-03 21:09:53 +00001343/// MatchingStackOffset - Return true if the given stack call argument is
1344/// already available in the same position (relatively) of the caller's
1345/// incoming argument stack.
1346static
1347bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1348 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1349 const ARMInstrInfo *TII) {
1350 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1351 int FI = INT_MAX;
1352 if (Arg.getOpcode() == ISD::CopyFromReg) {
1353 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
1354 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
1355 return false;
1356 MachineInstr *Def = MRI->getVRegDef(VR);
1357 if (!Def)
1358 return false;
1359 if (!Flags.isByVal()) {
1360 if (!TII->isLoadFromStackSlot(Def, FI))
1361 return false;
1362 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001363 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001364 }
1365 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1366 if (Flags.isByVal())
1367 // ByVal argument is passed in as a pointer but it's now being
1368 // dereferenced. e.g.
1369 // define @foo(%struct.X* %A) {
1370 // tail call @bar(%struct.X* byval %A)
1371 // }
1372 return false;
1373 SDValue Ptr = Ld->getBasePtr();
1374 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1375 if (!FINode)
1376 return false;
1377 FI = FINode->getIndex();
1378 } else
1379 return false;
1380
1381 assert(FI != INT_MAX);
1382 if (!MFI->isFixedObjectIndex(FI))
1383 return false;
1384 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1385}
1386
1387/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1388/// for tail call optimization. Targets which want to do tail call
1389/// optimization should implement this function.
1390bool
1391ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1392 CallingConv::ID CalleeCC,
1393 bool isVarArg,
1394 bool isCalleeStructRet,
1395 bool isCallerStructRet,
1396 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001397 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001398 const SmallVectorImpl<ISD::InputArg> &Ins,
1399 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001400 const Function *CallerF = DAG.getMachineFunction().getFunction();
1401 CallingConv::ID CallerCC = CallerF->getCallingConv();
1402 bool CCMatch = CallerCC == CalleeCC;
1403
1404 // Look for obvious safe cases to perform tail call optimization that do not
1405 // require ABI changes. This is what gcc calls sibcall.
1406
Jim Grosbach7616b642010-06-16 23:45:49 +00001407 // Do not sibcall optimize vararg calls unless the call site is not passing
1408 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001409 if (isVarArg && !Outs.empty())
1410 return false;
1411
1412 // Also avoid sibcall optimization if either caller or callee uses struct
1413 // return semantics.
1414 if (isCalleeStructRet || isCallerStructRet)
1415 return false;
1416
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001417 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Evan Cheng0110ac62010-06-19 01:01:32 +00001418 // emitEpilogue is not ready for them.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001419 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1420 // LR. This means if we need to reload LR, it takes an extra instructions,
1421 // which outweighs the value of the tail call; but here we don't know yet
1422 // whether LR is going to be used. Probably the right approach is to
1423 // generate the tail call here and turn it back into CALL/RET in
1424 // emitEpilogue if LR is used.
Evan Cheng0110ac62010-06-19 01:01:32 +00001425 if (Subtarget->isThumb1Only())
1426 return false;
1427
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001428 // For the moment, we can only do this to functions defined in this
1429 // compilation, or to indirect calls. A Thumb B to an ARM function,
1430 // or vice versa, is not easily fixed up in the linker unlike BL.
1431 // (We could do this by loading the address of the callee into a register;
1432 // that is an extra instruction over the direct call and burns a register
1433 // as well, so is not likely to be a win.)
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001434
1435 // It might be safe to remove this restriction on non-Darwin.
1436
1437 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1438 // but we need to make sure there are enough registers; the only valid
1439 // registers are the 4 used for parameters. We don't currently do this
1440 // case.
Evan Cheng0110ac62010-06-19 01:01:32 +00001441 if (isa<ExternalSymbolSDNode>(Callee))
1442 return false;
1443
1444 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001445 const GlobalValue *GV = G->getGlobal();
1446 if (GV->isDeclaration() || GV->isWeakForLinker())
Evan Cheng0110ac62010-06-19 01:01:32 +00001447 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001448 }
1449
Dale Johannesen51e28e62010-06-03 21:09:53 +00001450 // If the calling conventions do not match, then we'd better make sure the
1451 // results are returned in the same way as what the caller expects.
1452 if (!CCMatch) {
1453 SmallVector<CCValAssign, 16> RVLocs1;
1454 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
1455 RVLocs1, *DAG.getContext());
1456 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1457
1458 SmallVector<CCValAssign, 16> RVLocs2;
1459 CCState CCInfo2(CallerCC, false, getTargetMachine(),
1460 RVLocs2, *DAG.getContext());
1461 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1462
1463 if (RVLocs1.size() != RVLocs2.size())
1464 return false;
1465 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1466 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1467 return false;
1468 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1469 return false;
1470 if (RVLocs1[i].isRegLoc()) {
1471 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1472 return false;
1473 } else {
1474 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1475 return false;
1476 }
1477 }
1478 }
1479
1480 // If the callee takes no arguments then go on to check the results of the
1481 // call.
1482 if (!Outs.empty()) {
1483 // Check if stack adjustment is needed. For now, do not do this if any
1484 // argument is passed on the stack.
1485 SmallVector<CCValAssign, 16> ArgLocs;
1486 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
1487 ArgLocs, *DAG.getContext());
1488 CCInfo.AnalyzeCallOperands(Outs,
1489 CCAssignFnForNode(CalleeCC, false, isVarArg));
1490 if (CCInfo.getNextStackOffset()) {
1491 MachineFunction &MF = DAG.getMachineFunction();
1492
1493 // Check if the arguments are already laid out in the right way as
1494 // the caller's fixed stack objects.
1495 MachineFrameInfo *MFI = MF.getFrameInfo();
1496 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1497 const ARMInstrInfo *TII =
1498 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001499 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1500 i != e;
1501 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001502 CCValAssign &VA = ArgLocs[i];
1503 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001504 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001505 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001506 if (VA.getLocInfo() == CCValAssign::Indirect)
1507 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001508 if (VA.needsCustom()) {
1509 // f64 and vector types are split into multiple registers or
1510 // register/stack-slot combinations. The types will not match
1511 // the registers; give up on memory f64 refs until we figure
1512 // out what to do about this.
1513 if (!VA.isRegLoc())
1514 return false;
1515 if (!ArgLocs[++i].isRegLoc())
1516 return false;
1517 if (RegVT == MVT::v2f64) {
1518 if (!ArgLocs[++i].isRegLoc())
1519 return false;
1520 if (!ArgLocs[++i].isRegLoc())
1521 return false;
1522 }
1523 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001524 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1525 MFI, MRI, TII))
1526 return false;
1527 }
1528 }
1529 }
1530 }
1531
1532 return true;
1533}
1534
Dan Gohman98ca4f22009-08-05 01:29:28 +00001535SDValue
1536ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001537 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001538 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001539 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001540 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001541
Bob Wilsondee46d72009-04-17 20:35:10 +00001542 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001543 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001544
Bob Wilsondee46d72009-04-17 20:35:10 +00001545 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001546 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1547 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001548
Dan Gohman98ca4f22009-08-05 01:29:28 +00001549 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001550 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1551 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001552
1553 // If this is the first return lowered for this function, add
1554 // the regs to the liveout set for the function.
1555 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1556 for (unsigned i = 0; i != RVLocs.size(); ++i)
1557 if (RVLocs[i].isRegLoc())
1558 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001559 }
1560
Bob Wilson1f595bb2009-04-17 19:07:39 +00001561 SDValue Flag;
1562
1563 // Copy the result values into the output registers.
1564 for (unsigned i = 0, realRVLocIdx = 0;
1565 i != RVLocs.size();
1566 ++i, ++realRVLocIdx) {
1567 CCValAssign &VA = RVLocs[i];
1568 assert(VA.isRegLoc() && "Can only return in registers!");
1569
Dan Gohmanc9403652010-07-07 15:54:55 +00001570 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001571
1572 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001573 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001574 case CCValAssign::Full: break;
1575 case CCValAssign::BCvt:
1576 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1577 break;
1578 }
1579
Bob Wilson1f595bb2009-04-17 19:07:39 +00001580 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001581 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001582 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001583 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1584 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001585 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001586 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001587
1588 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1589 Flag = Chain.getValue(1);
1590 VA = RVLocs[++i]; // skip ahead to next loc
1591 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1592 HalfGPRs.getValue(1), Flag);
1593 Flag = Chain.getValue(1);
1594 VA = RVLocs[++i]; // skip ahead to next loc
1595
1596 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001597 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1598 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001599 }
1600 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1601 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001602 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001603 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001604 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001605 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001606 VA = RVLocs[++i]; // skip ahead to next loc
1607 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1608 Flag);
1609 } else
1610 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1611
Bob Wilsondee46d72009-04-17 20:35:10 +00001612 // Guarantee that all emitted copies are
1613 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001614 Flag = Chain.getValue(1);
1615 }
1616
1617 SDValue result;
1618 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001619 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001620 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001621 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001622
1623 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001624}
1625
Bob Wilsonb62d2572009-11-03 00:02:05 +00001626// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1627// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1628// one of the above mentioned nodes. It has to be wrapped because otherwise
1629// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1630// be used to form addressing mode. These wrapped nodes will be selected
1631// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001632static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001633 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001634 // FIXME there is no actual debug info here
1635 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001636 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001637 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001638 if (CP->isMachineConstantPoolEntry())
1639 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1640 CP->getAlignment());
1641 else
1642 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1643 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001644 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001645}
1646
Jim Grosbache1102ca2010-07-19 17:20:38 +00001647unsigned ARMTargetLowering::getJumpTableEncoding() const {
1648 return MachineJumpTableInfo::EK_Inline;
1649}
1650
Dan Gohmand858e902010-04-17 15:26:15 +00001651SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1652 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001653 MachineFunction &MF = DAG.getMachineFunction();
1654 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1655 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001656 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001657 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001658 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001659 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1660 SDValue CPAddr;
1661 if (RelocM == Reloc::Static) {
1662 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1663 } else {
1664 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001665 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001666 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1667 ARMCP::CPBlockAddress,
1668 PCAdj);
1669 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1670 }
1671 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1672 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001673 PseudoSourceValue::getConstantPool(), 0,
1674 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001675 if (RelocM == Reloc::Static)
1676 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001677 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001678 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001679}
1680
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001681// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001682SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001683ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001684 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001685 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001686 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001687 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001688 MachineFunction &MF = DAG.getMachineFunction();
1689 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1690 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001691 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001692 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001693 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001694 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001695 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001696 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
David Greene1b58cab2010-02-15 16:55:24 +00001697 PseudoSourceValue::getConstantPool(), 0,
1698 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001699 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001700
Evan Chenge7e0d622009-11-06 22:24:13 +00001701 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001702 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001703
1704 // call __tls_get_addr.
1705 ArgListTy Args;
1706 ArgListEntry Entry;
1707 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001708 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001709 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001710 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001711 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001712 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1713 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001714 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001715 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001716 return CallResult.first;
1717}
1718
1719// Lower ISD::GlobalTLSAddress using the "initial exec" or
1720// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001721SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001722ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001723 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001724 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001725 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001726 SDValue Offset;
1727 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001728 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001729 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001730 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001731
Chris Lattner4fb63d02009-07-15 04:12:33 +00001732 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001733 MachineFunction &MF = DAG.getMachineFunction();
1734 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1735 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1736 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001737 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1738 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001739 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001740 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001741 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001742 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001743 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001744 PseudoSourceValue::getConstantPool(), 0,
1745 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001746 Chain = Offset.getValue(1);
1747
Evan Chenge7e0d622009-11-06 22:24:13 +00001748 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001749 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001750
Evan Cheng9eda6892009-10-31 03:39:36 +00001751 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001752 PseudoSourceValue::getConstantPool(), 0,
1753 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001754 } else {
1755 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001756 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001757 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001758 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001759 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001760 PseudoSourceValue::getConstantPool(), 0,
1761 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001762 }
1763
1764 // The address of the thread local variable is the add of the thread
1765 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001766 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001767}
1768
Dan Gohman475871a2008-07-27 21:46:04 +00001769SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001770ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001771 // TODO: implement the "local dynamic" model
1772 assert(Subtarget->isTargetELF() &&
1773 "TLS not implemented for non-ELF targets");
1774 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1775 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1776 // otherwise use the "Local Exec" TLS Model
1777 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1778 return LowerToTLSGeneralDynamicModel(GA, DAG);
1779 else
1780 return LowerToTLSExecModels(GA, DAG);
1781}
1782
Dan Gohman475871a2008-07-27 21:46:04 +00001783SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001784 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001785 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001786 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001787 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001788 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1789 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001790 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001791 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001792 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001793 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001794 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001795 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001796 CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001797 PseudoSourceValue::getConstantPool(), 0,
1798 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001799 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001800 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001801 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001802 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001803 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001804 PseudoSourceValue::getGOT(), 0,
1805 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001806 return Result;
1807 } else {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001808 // If we have T2 ops, we can materialize the address directly via movt/movw
1809 // pair. This is always cheaper.
1810 if (Subtarget->useMovt()) {
1811 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
Devang Patel0d881da2010-07-06 22:08:15 +00001812 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001813 } else {
1814 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1815 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1816 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001817 PseudoSourceValue::getConstantPool(), 0,
1818 false, false, 0);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001819 }
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001820 }
1821}
1822
Dan Gohman475871a2008-07-27 21:46:04 +00001823SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001824 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001825 MachineFunction &MF = DAG.getMachineFunction();
1826 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1827 unsigned ARMPCLabelIndex = 0;
Owen Andersone50ed302009-08-10 22:56:29 +00001828 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001829 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001830 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001831 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001832 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001833 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001834 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001835 else {
Evan Chenge7e0d622009-11-06 22:24:13 +00001836 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001837 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1838 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001839 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001840 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001841 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001842 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001843
Evan Cheng9eda6892009-10-31 03:39:36 +00001844 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001845 PseudoSourceValue::getConstantPool(), 0,
1846 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001847 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001848
1849 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001850 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001851 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001852 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001853
Evan Cheng63476a82009-09-03 07:04:02 +00001854 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Evan Cheng9eda6892009-10-31 03:39:36 +00001855 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001856 PseudoSourceValue::getGOT(), 0,
1857 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001858
1859 return Result;
1860}
1861
Dan Gohman475871a2008-07-27 21:46:04 +00001862SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001863 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001864 assert(Subtarget->isTargetELF() &&
1865 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00001866 MachineFunction &MF = DAG.getMachineFunction();
1867 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1868 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Andersone50ed302009-08-10 22:56:29 +00001869 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001870 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001871 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001872 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1873 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001874 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001875 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001876 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001877 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001878 PseudoSourceValue::getConstantPool(), 0,
1879 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001880 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001881 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001882}
1883
Jim Grosbach0e0da732009-05-12 23:59:14 +00001884SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00001885ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
1886 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00001887 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00001888 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
1889 Op.getOperand(1), Val);
1890}
1891
1892SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00001893ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
1894 DebugLoc dl = Op.getDebugLoc();
1895 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
1896 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
1897}
1898
1899SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00001900ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00001901 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001902 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001903 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001904 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001905 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001906 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001907 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001908 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1909 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001910 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001911 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00001912 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1913 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001914 EVT PtrVT = getPointerTy();
1915 DebugLoc dl = Op.getDebugLoc();
1916 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1917 SDValue CPAddr;
1918 unsigned PCAdj = (RelocM != Reloc::PIC_)
1919 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001920 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001921 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1922 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001923 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001924 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001925 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00001926 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001927 PseudoSourceValue::getConstantPool(), 0,
1928 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001929
1930 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001931 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001932 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1933 }
1934 return Result;
1935 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001936 }
1937}
1938
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001939static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00001940 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00001941 DebugLoc dl = Op.getDebugLoc();
1942 SDValue Op5 = Op.getOperand(5);
Jim Grosbach3728e962009-12-10 00:11:09 +00001943 unsigned isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue();
Jim Grosbachc73993b2010-06-17 01:37:00 +00001944 // v6 and v7 can both handle barriers directly, but need handled a bit
1945 // differently. Thumb1 and pre-v6 ARM mode use a libcall instead and should
1946 // never get here.
1947 unsigned Opc = isDeviceBarrier ? ARMISD::SYNCBARRIER : ARMISD::MEMBARRIER;
1948 if (Subtarget->hasV7Ops())
1949 return DAG.getNode(Opc, dl, MVT::Other, Op.getOperand(0));
1950 else if (Subtarget->hasV6Ops() && !Subtarget->isThumb1Only())
1951 return DAG.getNode(Opc, dl, MVT::Other, Op.getOperand(0),
1952 DAG.getConstant(0, MVT::i32));
1953 assert(0 && "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
1954 return SDValue();
Jim Grosbach3728e962009-12-10 00:11:09 +00001955}
1956
Dan Gohman1e93df62010-04-17 14:41:14 +00001957static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
1958 MachineFunction &MF = DAG.getMachineFunction();
1959 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
1960
Evan Chenga8e29892007-01-19 07:51:42 +00001961 // vastart just stores the address of the VarArgsFrameIndex slot into the
1962 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001963 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001964 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00001965 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001966 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
David Greene1b58cab2010-02-15 16:55:24 +00001967 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
1968 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001969}
1970
Dan Gohman475871a2008-07-27 21:46:04 +00001971SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001972ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1973 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001974 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001975 MachineFunction &MF = DAG.getMachineFunction();
1976 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1977
1978 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001979 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001980 RC = ARM::tGPRRegisterClass;
1981 else
1982 RC = ARM::GPRRegisterClass;
1983
1984 // Transform the arguments stored in physical registers into virtual ones.
Evan Cheng2457f2c2010-05-22 01:47:14 +00001985 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001986 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001987
1988 SDValue ArgValue2;
1989 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001990 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00001991 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00001992
1993 // Create load node to retrieve arguments from the stack.
1994 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001995 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00001996 PseudoSourceValue::getFixedStack(FI), 0,
1997 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001998 } else {
1999 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002000 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002001 }
2002
Jim Grosbache5165492009-11-09 00:11:35 +00002003 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002004}
2005
2006SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002007ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002008 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002009 const SmallVectorImpl<ISD::InputArg>
2010 &Ins,
2011 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002012 SmallVectorImpl<SDValue> &InVals)
2013 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002014
Bob Wilson1f595bb2009-04-17 19:07:39 +00002015 MachineFunction &MF = DAG.getMachineFunction();
2016 MachineFrameInfo *MFI = MF.getFrameInfo();
2017
Bob Wilson1f595bb2009-04-17 19:07:39 +00002018 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2019
2020 // Assign locations to all of the incoming arguments.
2021 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002022 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
2023 *DAG.getContext());
2024 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002025 CCAssignFnForNode(CallConv, /* Return*/ false,
2026 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002027
2028 SmallVector<SDValue, 16> ArgValues;
2029
2030 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2031 CCValAssign &VA = ArgLocs[i];
2032
Bob Wilsondee46d72009-04-17 20:35:10 +00002033 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002034 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002035 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002036
Bob Wilson5bafff32009-06-22 23:27:02 +00002037 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002038 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002039 // f64 and vector types are split up into multiple registers or
2040 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002041 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002042 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002043 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002044 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002045 SDValue ArgValue2;
2046 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002047 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002048 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2049 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
2050 PseudoSourceValue::getFixedStack(FI), 0,
2051 false, false, 0);
2052 } else {
2053 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2054 Chain, DAG, dl);
2055 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002056 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2057 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002058 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002059 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002060 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2061 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002062 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002063
Bob Wilson5bafff32009-06-22 23:27:02 +00002064 } else {
2065 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002066
Owen Anderson825b72b2009-08-11 20:47:22 +00002067 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002068 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002069 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002070 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002071 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002072 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002073 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002074 RC = (AFI->isThumb1OnlyFunction() ?
2075 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002076 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002077 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002078
2079 // Transform the arguments in physical registers into virtual ones.
2080 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002081 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002082 }
2083
2084 // If this is an 8 or 16-bit value, it is really passed promoted
2085 // to 32 bits. Insert an assert[sz]ext to capture this, then
2086 // truncate to the right size.
2087 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002088 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002089 case CCValAssign::Full: break;
2090 case CCValAssign::BCvt:
2091 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
2092 break;
2093 case CCValAssign::SExt:
2094 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2095 DAG.getValueType(VA.getValVT()));
2096 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2097 break;
2098 case CCValAssign::ZExt:
2099 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2100 DAG.getValueType(VA.getValVT()));
2101 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2102 break;
2103 }
2104
Dan Gohman98ca4f22009-08-05 01:29:28 +00002105 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002106
2107 } else { // VA.isRegLoc()
2108
2109 // sanity check
2110 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002111 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002112
2113 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002114 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002115
Bob Wilsondee46d72009-04-17 20:35:10 +00002116 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002117 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002118 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00002119 PseudoSourceValue::getFixedStack(FI), 0,
2120 false, false, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002121 }
2122 }
2123
2124 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00002125 if (isVarArg) {
2126 static const unsigned GPRArgRegs[] = {
2127 ARM::R0, ARM::R1, ARM::R2, ARM::R3
2128 };
2129
Bob Wilsondee46d72009-04-17 20:35:10 +00002130 unsigned NumGPRs = CCInfo.getFirstUnallocated
2131 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002132
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00002133 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
2134 unsigned VARegSize = (4 - NumGPRs) * 4;
2135 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00002136 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00002137 if (VARegSaveSize) {
2138 // If this function is vararg, store any remaining integer argument regs
2139 // to their spots on the stack so that they may be loaded by deferencing
2140 // the result of va_next.
2141 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00002142 AFI->setVarArgsFrameIndex(
2143 MFI->CreateFixedObject(VARegSaveSize,
2144 ArgOffset + VARegSaveSize - VARegSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002145 true));
Dan Gohman1e93df62010-04-17 14:41:14 +00002146 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2147 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00002148
Dan Gohman475871a2008-07-27 21:46:04 +00002149 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00002150 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002151 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002152 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00002153 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002154 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00002155 RC = ARM::GPRRegisterClass;
2156
Bob Wilson998e1252009-04-20 18:36:57 +00002157 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002158 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00002159 SDValue Store =
2160 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Jim Grosbach18f30e62010-06-02 21:53:11 +00002161 PseudoSourceValue::getFixedStack(AFI->getVarArgsFrameIndex()),
2162 0, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002163 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002164 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00002165 DAG.getConstant(4, getPointerTy()));
2166 }
2167 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002168 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002169 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00002170 } else
2171 // This will point to the next argument passed via stack.
Evan Chenged2ae132010-07-03 00:40:23 +00002172 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
Evan Chenga8e29892007-01-19 07:51:42 +00002173 }
2174
Dan Gohman98ca4f22009-08-05 01:29:28 +00002175 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002176}
2177
2178/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002179static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002180 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002181 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002182 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002183 // Maybe this has already been legalized into the constant pool?
2184 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002185 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002186 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002187 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002188 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002189 }
2190 }
2191 return false;
2192}
2193
Evan Chenga8e29892007-01-19 07:51:42 +00002194/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2195/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002196SDValue
2197ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002198 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002199 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002200 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002201 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002202 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002203 // Constant does not fit, try adjusting it by one?
2204 switch (CC) {
2205 default: break;
2206 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002207 case ISD::SETGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00002208 if (isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002209 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002210 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002211 }
2212 break;
2213 case ISD::SETULT:
2214 case ISD::SETUGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00002215 if (C > 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002216 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002217 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002218 }
2219 break;
2220 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002221 case ISD::SETGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00002222 if (isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002223 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002224 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002225 }
2226 break;
2227 case ISD::SETULE:
2228 case ISD::SETUGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00002229 if (C < 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002230 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002231 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002232 }
2233 break;
2234 }
2235 }
2236 }
2237
2238 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002239 ARMISD::NodeType CompareType;
2240 switch (CondCode) {
2241 default:
2242 CompareType = ARMISD::CMP;
2243 break;
2244 case ARMCC::EQ:
2245 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002246 // Uses only Z Flag
2247 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002248 break;
2249 }
Evan Cheng218977b2010-07-13 19:27:42 +00002250 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002251 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002252}
2253
2254/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002255SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002256ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002257 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002258 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002259 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00002260 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002261 else
Owen Anderson825b72b2009-08-11 20:47:22 +00002262 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
2263 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002264}
2265
Dan Gohmand858e902010-04-17 15:26:15 +00002266SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002267 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002268 SDValue LHS = Op.getOperand(0);
2269 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002270 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002271 SDValue TrueVal = Op.getOperand(2);
2272 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002273 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002274
Owen Anderson825b72b2009-08-11 20:47:22 +00002275 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002276 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002277 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002278 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2279 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002280 }
2281
2282 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002283 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002284
Evan Cheng218977b2010-07-13 19:27:42 +00002285 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2286 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002287 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002288 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002289 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002290 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002291 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002292 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002293 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002294 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002295 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002296 }
2297 return Result;
2298}
2299
Evan Cheng218977b2010-07-13 19:27:42 +00002300/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2301/// to morph to an integer compare sequence.
2302static bool canChangeToInt(SDValue Op, bool &SeenZero,
2303 const ARMSubtarget *Subtarget) {
2304 SDNode *N = Op.getNode();
2305 if (!N->hasOneUse())
2306 // Otherwise it requires moving the value from fp to integer registers.
2307 return false;
2308 if (!N->getNumValues())
2309 return false;
2310 EVT VT = Op.getValueType();
2311 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2312 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2313 // vmrs are very slow, e.g. cortex-a8.
2314 return false;
2315
2316 if (isFloatingPointZero(Op)) {
2317 SeenZero = true;
2318 return true;
2319 }
2320 return ISD::isNormalLoad(N);
2321}
2322
2323static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2324 if (isFloatingPointZero(Op))
2325 return DAG.getConstant(0, MVT::i32);
2326
2327 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2328 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2329 Ld->getChain(), Ld->getBasePtr(),
2330 Ld->getSrcValue(), Ld->getSrcValueOffset(),
2331 Ld->isVolatile(), Ld->isNonTemporal(),
2332 Ld->getAlignment());
2333
2334 llvm_unreachable("Unknown VFP cmp argument!");
2335}
2336
2337static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2338 SDValue &RetVal1, SDValue &RetVal2) {
2339 if (isFloatingPointZero(Op)) {
2340 RetVal1 = DAG.getConstant(0, MVT::i32);
2341 RetVal2 = DAG.getConstant(0, MVT::i32);
2342 return;
2343 }
2344
2345 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2346 SDValue Ptr = Ld->getBasePtr();
2347 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2348 Ld->getChain(), Ptr,
2349 Ld->getSrcValue(), Ld->getSrcValueOffset(),
2350 Ld->isVolatile(), Ld->isNonTemporal(),
2351 Ld->getAlignment());
2352
2353 EVT PtrType = Ptr.getValueType();
2354 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2355 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2356 PtrType, Ptr, DAG.getConstant(4, PtrType));
2357 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2358 Ld->getChain(), NewPtr,
2359 Ld->getSrcValue(), Ld->getSrcValueOffset() + 4,
2360 Ld->isVolatile(), Ld->isNonTemporal(),
2361 NewAlign);
2362 return;
2363 }
2364
2365 llvm_unreachable("Unknown VFP cmp argument!");
2366}
2367
2368/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2369/// f32 and even f64 comparisons to integer ones.
2370SDValue
2371ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2372 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002373 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002374 SDValue LHS = Op.getOperand(2);
2375 SDValue RHS = Op.getOperand(3);
2376 SDValue Dest = Op.getOperand(4);
2377 DebugLoc dl = Op.getDebugLoc();
2378
2379 bool SeenZero = false;
2380 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2381 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002382 // If one of the operand is zero, it's safe to ignore the NaN case since
2383 // we only care about equality comparisons.
2384 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Evan Cheng218977b2010-07-13 19:27:42 +00002385 // If unsafe fp math optimization is enabled and there are no othter uses of
2386 // the CMP operands, and the condition code is EQ oe NE, we can optimize it
2387 // to an integer comparison.
2388 if (CC == ISD::SETOEQ)
2389 CC = ISD::SETEQ;
2390 else if (CC == ISD::SETUNE)
2391 CC = ISD::SETNE;
2392
2393 SDValue ARMcc;
2394 if (LHS.getValueType() == MVT::f32) {
2395 LHS = bitcastf32Toi32(LHS, DAG);
2396 RHS = bitcastf32Toi32(RHS, DAG);
2397 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2398 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2399 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2400 Chain, Dest, ARMcc, CCR, Cmp);
2401 }
2402
2403 SDValue LHS1, LHS2;
2404 SDValue RHS1, RHS2;
2405 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2406 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2407 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2408 ARMcc = DAG.getConstant(CondCode, MVT::i32);
2409 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
2410 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2411 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2412 }
2413
2414 return SDValue();
2415}
2416
2417SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2418 SDValue Chain = Op.getOperand(0);
2419 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2420 SDValue LHS = Op.getOperand(2);
2421 SDValue RHS = Op.getOperand(3);
2422 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002423 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002424
Owen Anderson825b72b2009-08-11 20:47:22 +00002425 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002426 SDValue ARMcc;
2427 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002428 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002429 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002430 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002431 }
2432
Owen Anderson825b72b2009-08-11 20:47:22 +00002433 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002434
2435 if (UnsafeFPMath &&
2436 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2437 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2438 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2439 if (Result.getNode())
2440 return Result;
2441 }
2442
Evan Chenga8e29892007-01-19 07:51:42 +00002443 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002444 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002445
Evan Cheng218977b2010-07-13 19:27:42 +00002446 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2447 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002448 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2449 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng218977b2010-07-13 19:27:42 +00002450 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002451 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002452 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002453 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2454 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002455 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002456 }
2457 return Res;
2458}
2459
Dan Gohmand858e902010-04-17 15:26:15 +00002460SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002461 SDValue Chain = Op.getOperand(0);
2462 SDValue Table = Op.getOperand(1);
2463 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002464 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002465
Owen Andersone50ed302009-08-10 22:56:29 +00002466 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002467 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2468 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002469 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002470 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002471 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002472 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2473 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002474 if (Subtarget->isThumb2()) {
2475 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2476 // which does another jump to the destination. This also makes it easier
2477 // to translate it to TBB / TBH later.
2478 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002479 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002480 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002481 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002482 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002483 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002484 PseudoSourceValue::getJumpTable(), 0,
2485 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002486 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002487 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002488 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002489 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002490 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002491 PseudoSourceValue::getJumpTable(), 0, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002492 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002493 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002494 }
Evan Chenga8e29892007-01-19 07:51:42 +00002495}
2496
Bob Wilson76a312b2010-03-19 22:51:32 +00002497static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2498 DebugLoc dl = Op.getDebugLoc();
2499 unsigned Opc;
2500
2501 switch (Op.getOpcode()) {
2502 default:
2503 assert(0 && "Invalid opcode!");
2504 case ISD::FP_TO_SINT:
2505 Opc = ARMISD::FTOSI;
2506 break;
2507 case ISD::FP_TO_UINT:
2508 Opc = ARMISD::FTOUI;
2509 break;
2510 }
2511 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
2512 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
2513}
2514
2515static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2516 EVT VT = Op.getValueType();
2517 DebugLoc dl = Op.getDebugLoc();
2518 unsigned Opc;
2519
2520 switch (Op.getOpcode()) {
2521 default:
2522 assert(0 && "Invalid opcode!");
2523 case ISD::SINT_TO_FP:
2524 Opc = ARMISD::SITOF;
2525 break;
2526 case ISD::UINT_TO_FP:
2527 Opc = ARMISD::UITOF;
2528 break;
2529 }
2530
2531 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
2532 return DAG.getNode(Opc, dl, VT, Op);
2533}
2534
Evan Cheng515fe3a2010-07-08 02:08:50 +00002535SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002536 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002537 SDValue Tmp0 = Op.getOperand(0);
2538 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002539 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002540 EVT VT = Op.getValueType();
2541 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00002542 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
Evan Cheng218977b2010-07-13 19:27:42 +00002543 SDValue ARMcc = DAG.getConstant(ARMCC::LT, MVT::i32);
Evan Cheng515fe3a2010-07-08 02:08:50 +00002544 SDValue FP0 = DAG.getConstantFP(0.0, SrcVT);
Evan Cheng218977b2010-07-13 19:27:42 +00002545 SDValue Cmp = getVFPCmp(Tmp1, FP0, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002546 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002547 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002548}
2549
Evan Cheng2457f2c2010-05-22 01:47:14 +00002550SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
2551 MachineFunction &MF = DAG.getMachineFunction();
2552 MachineFrameInfo *MFI = MF.getFrameInfo();
2553 MFI->setReturnAddressIsTaken(true);
2554
2555 EVT VT = Op.getValueType();
2556 DebugLoc dl = Op.getDebugLoc();
2557 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2558 if (Depth) {
2559 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
2560 SDValue Offset = DAG.getConstant(4, MVT::i32);
2561 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
2562 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
2563 NULL, 0, false, false, 0);
2564 }
2565
2566 // Return LR, which contains the return address. Mark it an implicit live-in.
Evan Chengc7cf10c2010-05-24 18:00:18 +00002567 unsigned Reg = MF.addLiveIn(ARM::LR, ARM::GPRRegisterClass);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002568 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
2569}
2570
Dan Gohmand858e902010-04-17 15:26:15 +00002571SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002572 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2573 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002574
Owen Andersone50ed302009-08-10 22:56:29 +00002575 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002576 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2577 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002578 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002579 ? ARM::R7 : ARM::R11;
2580 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2581 while (Depth--)
David Greene1b58cab2010-02-15 16:55:24 +00002582 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
2583 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002584 return FrameAddr;
2585}
2586
Bob Wilson9f3f0612010-04-17 05:30:19 +00002587/// ExpandBIT_CONVERT - If the target supports VFP, this function is called to
2588/// expand a bit convert where either the source or destination type is i64 to
2589/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2590/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2591/// vectors), since the legalizer won't know what to do with that.
Duncan Sands1607f052008-12-01 11:39:25 +00002592static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002593 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2594 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002595 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002596
Bob Wilson9f3f0612010-04-17 05:30:19 +00002597 // This function is only supposed to be called for i64 types, either as the
2598 // source or destination of the bit convert.
2599 EVT SrcVT = Op.getValueType();
2600 EVT DstVT = N->getValueType(0);
2601 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
2602 "ExpandBIT_CONVERT called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002603
Bob Wilson9f3f0612010-04-17 05:30:19 +00002604 // Turn i64->f64 into VMOVDRR.
2605 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002606 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2607 DAG.getConstant(0, MVT::i32));
2608 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2609 DAG.getConstant(1, MVT::i32));
Bob Wilson1114f562010-06-11 22:45:25 +00002610 return DAG.getNode(ISD::BIT_CONVERT, dl, DstVT,
2611 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00002612 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002613
Jim Grosbache5165492009-11-09 00:11:35 +00002614 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002615 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2616 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2617 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2618 // Merge the pieces into a single i64 value.
2619 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2620 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002621
Bob Wilson9f3f0612010-04-17 05:30:19 +00002622 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002623}
2624
Bob Wilson5bafff32009-06-22 23:27:02 +00002625/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00002626/// Zero vectors are used to represent vector negation and in those cases
2627/// will be implemented with the NEON VNEG instruction. However, VNEG does
2628/// not support i64 elements, so sometimes the zero vectors will need to be
2629/// explicitly constructed. Regardless, use a canonical VMOV to create the
2630/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002631static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002632 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00002633 // The canonical modified immediate encoding of a zero vector is....0!
2634 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
2635 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
2636 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
2637 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00002638}
2639
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002640/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2641/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002642SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2643 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002644 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2645 EVT VT = Op.getValueType();
2646 unsigned VTBits = VT.getSizeInBits();
2647 DebugLoc dl = Op.getDebugLoc();
2648 SDValue ShOpLo = Op.getOperand(0);
2649 SDValue ShOpHi = Op.getOperand(1);
2650 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00002651 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002652 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002653
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002654 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2655
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002656 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2657 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2658 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2659 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2660 DAG.getConstant(VTBits, MVT::i32));
2661 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2662 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002663 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002664
2665 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2666 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00002667 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002668 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00002669 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002670 CCR, Cmp);
2671
2672 SDValue Ops[2] = { Lo, Hi };
2673 return DAG.getMergeValues(Ops, 2, dl);
2674}
2675
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002676/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2677/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002678SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2679 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002680 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2681 EVT VT = Op.getValueType();
2682 unsigned VTBits = VT.getSizeInBits();
2683 DebugLoc dl = Op.getDebugLoc();
2684 SDValue ShOpLo = Op.getOperand(0);
2685 SDValue ShOpHi = Op.getOperand(1);
2686 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00002687 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002688
2689 assert(Op.getOpcode() == ISD::SHL_PARTS);
2690 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2691 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2692 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2693 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2694 DAG.getConstant(VTBits, MVT::i32));
2695 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2696 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2697
2698 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2699 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2700 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00002701 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002702 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00002703 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002704 CCR, Cmp);
2705
2706 SDValue Ops[2] = { Lo, Hi };
2707 return DAG.getMergeValues(Ops, 2, dl);
2708}
2709
Jim Grosbach3482c802010-01-18 19:58:49 +00002710static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
2711 const ARMSubtarget *ST) {
2712 EVT VT = N->getValueType(0);
2713 DebugLoc dl = N->getDebugLoc();
2714
2715 if (!ST->hasV6T2Ops())
2716 return SDValue();
2717
2718 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
2719 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
2720}
2721
Bob Wilson5bafff32009-06-22 23:27:02 +00002722static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2723 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002724 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002725 DebugLoc dl = N->getDebugLoc();
2726
2727 // Lower vector shifts on NEON to use VSHL.
2728 if (VT.isVector()) {
2729 assert(ST->hasNEON() && "unexpected vector shift");
2730
2731 // Left shifts translate directly to the vshiftu intrinsic.
2732 if (N->getOpcode() == ISD::SHL)
2733 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002734 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002735 N->getOperand(0), N->getOperand(1));
2736
2737 assert((N->getOpcode() == ISD::SRA ||
2738 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2739
2740 // NEON uses the same intrinsics for both left and right shifts. For
2741 // right shifts, the shift amounts are negative, so negate the vector of
2742 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002743 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002744 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2745 getZeroVector(ShiftVT, DAG, dl),
2746 N->getOperand(1));
2747 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2748 Intrinsic::arm_neon_vshifts :
2749 Intrinsic::arm_neon_vshiftu);
2750 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002751 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002752 N->getOperand(0), NegatedCount);
2753 }
2754
Eli Friedmance392eb2009-08-22 03:13:10 +00002755 // We can get here for a node like i32 = ISD::SHL i32, i64
2756 if (VT != MVT::i64)
2757 return SDValue();
2758
2759 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002760 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002761
Chris Lattner27a6c732007-11-24 07:07:01 +00002762 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2763 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002764 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002765 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002766
Chris Lattner27a6c732007-11-24 07:07:01 +00002767 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002768 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002769
Chris Lattner27a6c732007-11-24 07:07:01 +00002770 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002771 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00002772 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00002773 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00002774 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002775
Chris Lattner27a6c732007-11-24 07:07:01 +00002776 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2777 // captures the result into a carry flag.
2778 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002779 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002780
Chris Lattner27a6c732007-11-24 07:07:01 +00002781 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002782 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002783
Chris Lattner27a6c732007-11-24 07:07:01 +00002784 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002785 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002786}
2787
Bob Wilson5bafff32009-06-22 23:27:02 +00002788static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2789 SDValue TmpOp0, TmpOp1;
2790 bool Invert = false;
2791 bool Swap = false;
2792 unsigned Opc = 0;
2793
2794 SDValue Op0 = Op.getOperand(0);
2795 SDValue Op1 = Op.getOperand(1);
2796 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002797 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002798 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2799 DebugLoc dl = Op.getDebugLoc();
2800
2801 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2802 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002803 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002804 case ISD::SETUNE:
2805 case ISD::SETNE: Invert = true; // Fallthrough
2806 case ISD::SETOEQ:
2807 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2808 case ISD::SETOLT:
2809 case ISD::SETLT: Swap = true; // Fallthrough
2810 case ISD::SETOGT:
2811 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2812 case ISD::SETOLE:
2813 case ISD::SETLE: Swap = true; // Fallthrough
2814 case ISD::SETOGE:
2815 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2816 case ISD::SETUGE: Swap = true; // Fallthrough
2817 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2818 case ISD::SETUGT: Swap = true; // Fallthrough
2819 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2820 case ISD::SETUEQ: Invert = true; // Fallthrough
2821 case ISD::SETONE:
2822 // Expand this to (OLT | OGT).
2823 TmpOp0 = Op0;
2824 TmpOp1 = Op1;
2825 Opc = ISD::OR;
2826 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2827 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2828 break;
2829 case ISD::SETUO: Invert = true; // Fallthrough
2830 case ISD::SETO:
2831 // Expand this to (OLT | OGE).
2832 TmpOp0 = Op0;
2833 TmpOp1 = Op1;
2834 Opc = ISD::OR;
2835 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2836 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2837 break;
2838 }
2839 } else {
2840 // Integer comparisons.
2841 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002842 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002843 case ISD::SETNE: Invert = true;
2844 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2845 case ISD::SETLT: Swap = true;
2846 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2847 case ISD::SETLE: Swap = true;
2848 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2849 case ISD::SETULT: Swap = true;
2850 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2851 case ISD::SETULE: Swap = true;
2852 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2853 }
2854
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002855 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002856 if (Opc == ARMISD::VCEQ) {
2857
2858 SDValue AndOp;
2859 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2860 AndOp = Op0;
2861 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2862 AndOp = Op1;
2863
2864 // Ignore bitconvert.
2865 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2866 AndOp = AndOp.getOperand(0);
2867
2868 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2869 Opc = ARMISD::VTST;
2870 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2871 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2872 Invert = !Invert;
2873 }
2874 }
2875 }
2876
2877 if (Swap)
2878 std::swap(Op0, Op1);
2879
2880 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2881
2882 if (Invert)
2883 Result = DAG.getNOT(dl, Result, VT);
2884
2885 return Result;
2886}
2887
Bob Wilsond3c42842010-06-14 22:19:57 +00002888/// isNEONModifiedImm - Check if the specified splat value corresponds to a
2889/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00002890/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00002891static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
2892 unsigned SplatBitSize, SelectionDAG &DAG,
Bob Wilsoncba270d2010-07-13 21:16:48 +00002893 EVT &VT, bool is128Bits, bool isVMOV) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00002894 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002895
Bob Wilson827b2102010-06-15 19:05:35 +00002896 // SplatBitSize is set to the smallest size that splats the vector, so a
2897 // zero vector will always have SplatBitSize == 8. However, NEON modified
2898 // immediate instructions others than VMOV do not support the 8-bit encoding
2899 // of a zero vector, and the default encoding of zero is supposed to be the
2900 // 32-bit version.
2901 if (SplatBits == 0)
2902 SplatBitSize = 32;
2903
Bob Wilson5bafff32009-06-22 23:27:02 +00002904 switch (SplatBitSize) {
2905 case 8:
Bob Wilson7e3f0d22010-07-14 06:31:50 +00002906 if (!isVMOV)
2907 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00002908 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00002909 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00002910 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002911 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00002912 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002913 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002914
2915 case 16:
2916 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00002917 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002918 if ((SplatBits & ~0xff) == 0) {
2919 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00002920 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002921 Imm = SplatBits;
2922 break;
2923 }
2924 if ((SplatBits & ~0xff00) == 0) {
2925 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00002926 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002927 Imm = SplatBits >> 8;
2928 break;
2929 }
2930 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002931
2932 case 32:
2933 // NEON's 32-bit VMOV supports splat values where:
2934 // * only one byte is nonzero, or
2935 // * the least significant byte is 0xff and the second byte is nonzero, or
2936 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00002937 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002938 if ((SplatBits & ~0xff) == 0) {
2939 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00002940 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002941 Imm = SplatBits;
2942 break;
2943 }
2944 if ((SplatBits & ~0xff00) == 0) {
2945 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00002946 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002947 Imm = SplatBits >> 8;
2948 break;
2949 }
2950 if ((SplatBits & ~0xff0000) == 0) {
2951 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00002952 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002953 Imm = SplatBits >> 16;
2954 break;
2955 }
2956 if ((SplatBits & ~0xff000000) == 0) {
2957 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00002958 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002959 Imm = SplatBits >> 24;
2960 break;
2961 }
Bob Wilson5bafff32009-06-22 23:27:02 +00002962
2963 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00002964 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
2965 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00002966 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002967 Imm = SplatBits >> 8;
2968 SplatBits |= 0xff;
2969 break;
2970 }
Bob Wilson5bafff32009-06-22 23:27:02 +00002971
2972 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00002973 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
2974 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00002975 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002976 Imm = SplatBits >> 16;
2977 SplatBits |= 0xffff;
2978 break;
2979 }
Bob Wilson5bafff32009-06-22 23:27:02 +00002980
2981 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2982 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2983 // VMOV.I32. A (very) minor optimization would be to replicate the value
2984 // and fall through here to test for a valid 64-bit splat. But, then the
2985 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00002986 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002987
2988 case 64: {
Bob Wilson827b2102010-06-15 19:05:35 +00002989 if (!isVMOV)
2990 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00002991 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00002992 uint64_t BitMask = 0xff;
2993 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002994 unsigned ImmMask = 1;
2995 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00002996 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00002997 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002998 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002999 Imm |= ImmMask;
3000 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003001 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003002 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003003 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003004 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003005 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003006 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003007 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003008 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003009 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003010 break;
3011 }
3012
Bob Wilson1a913ed2010-06-11 21:34:50 +00003013 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003014 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003015 return SDValue();
3016 }
3017
Bob Wilsoncba270d2010-07-13 21:16:48 +00003018 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3019 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003020}
3021
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003022static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3023 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003024 unsigned NumElts = VT.getVectorNumElements();
3025 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003026 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003027
3028 // If this is a VEXT shuffle, the immediate value is the index of the first
3029 // element. The other shuffle indices must be the successive elements after
3030 // the first one.
3031 unsigned ExpectedElt = Imm;
3032 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003033 // Increment the expected index. If it wraps around, it may still be
3034 // a VEXT but the source vectors must be swapped.
3035 ExpectedElt += 1;
3036 if (ExpectedElt == NumElts * 2) {
3037 ExpectedElt = 0;
3038 ReverseVEXT = true;
3039 }
3040
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003041 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003042 return false;
3043 }
3044
3045 // Adjust the index value if the source operands will be swapped.
3046 if (ReverseVEXT)
3047 Imm -= NumElts;
3048
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003049 return true;
3050}
3051
Bob Wilson8bb9e482009-07-26 00:39:34 +00003052/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3053/// instruction with the specified blocksize. (The order of the elements
3054/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003055static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3056 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003057 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3058 "Only possible block sizes for VREV are: 16, 32, 64");
3059
Bob Wilson8bb9e482009-07-26 00:39:34 +00003060 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003061 if (EltSz == 64)
3062 return false;
3063
3064 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003065 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003066
3067 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3068 return false;
3069
3070 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003071 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00003072 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
3073 return false;
3074 }
3075
3076 return true;
3077}
3078
Bob Wilsonc692cb72009-08-21 20:54:19 +00003079static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3080 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003081 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3082 if (EltSz == 64)
3083 return false;
3084
Bob Wilsonc692cb72009-08-21 20:54:19 +00003085 unsigned NumElts = VT.getVectorNumElements();
3086 WhichResult = (M[0] == 0 ? 0 : 1);
3087 for (unsigned i = 0; i < NumElts; i += 2) {
3088 if ((unsigned) M[i] != i + WhichResult ||
3089 (unsigned) M[i+1] != i + NumElts + WhichResult)
3090 return false;
3091 }
3092 return true;
3093}
3094
Bob Wilson324f4f12009-12-03 06:40:55 +00003095/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3096/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3097/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3098static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3099 unsigned &WhichResult) {
3100 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3101 if (EltSz == 64)
3102 return false;
3103
3104 unsigned NumElts = VT.getVectorNumElements();
3105 WhichResult = (M[0] == 0 ? 0 : 1);
3106 for (unsigned i = 0; i < NumElts; i += 2) {
3107 if ((unsigned) M[i] != i + WhichResult ||
3108 (unsigned) M[i+1] != i + WhichResult)
3109 return false;
3110 }
3111 return true;
3112}
3113
Bob Wilsonc692cb72009-08-21 20:54:19 +00003114static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3115 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003116 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3117 if (EltSz == 64)
3118 return false;
3119
Bob Wilsonc692cb72009-08-21 20:54:19 +00003120 unsigned NumElts = VT.getVectorNumElements();
3121 WhichResult = (M[0] == 0 ? 0 : 1);
3122 for (unsigned i = 0; i != NumElts; ++i) {
3123 if ((unsigned) M[i] != 2 * i + WhichResult)
3124 return false;
3125 }
3126
3127 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003128 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003129 return false;
3130
3131 return true;
3132}
3133
Bob Wilson324f4f12009-12-03 06:40:55 +00003134/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3135/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3136/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3137static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3138 unsigned &WhichResult) {
3139 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3140 if (EltSz == 64)
3141 return false;
3142
3143 unsigned Half = VT.getVectorNumElements() / 2;
3144 WhichResult = (M[0] == 0 ? 0 : 1);
3145 for (unsigned j = 0; j != 2; ++j) {
3146 unsigned Idx = WhichResult;
3147 for (unsigned i = 0; i != Half; ++i) {
3148 if ((unsigned) M[i + j * Half] != Idx)
3149 return false;
3150 Idx += 2;
3151 }
3152 }
3153
3154 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3155 if (VT.is64BitVector() && EltSz == 32)
3156 return false;
3157
3158 return true;
3159}
3160
Bob Wilsonc692cb72009-08-21 20:54:19 +00003161static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3162 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003163 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3164 if (EltSz == 64)
3165 return false;
3166
Bob Wilsonc692cb72009-08-21 20:54:19 +00003167 unsigned NumElts = VT.getVectorNumElements();
3168 WhichResult = (M[0] == 0 ? 0 : 1);
3169 unsigned Idx = WhichResult * NumElts / 2;
3170 for (unsigned i = 0; i != NumElts; i += 2) {
3171 if ((unsigned) M[i] != Idx ||
3172 (unsigned) M[i+1] != Idx + NumElts)
3173 return false;
3174 Idx += 1;
3175 }
3176
3177 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003178 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003179 return false;
3180
3181 return true;
3182}
3183
Bob Wilson324f4f12009-12-03 06:40:55 +00003184/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3185/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3186/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3187static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3188 unsigned &WhichResult) {
3189 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3190 if (EltSz == 64)
3191 return false;
3192
3193 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)
3199 return false;
3200 Idx += 1;
3201 }
3202
3203 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3204 if (VT.is64BitVector() && EltSz == 32)
3205 return false;
3206
3207 return true;
3208}
3209
Bob Wilson5bafff32009-06-22 23:27:02 +00003210// If this is a case we can't handle, return null and let the default
3211// expansion code take care of it.
3212static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00003213 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003214 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003215 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003216
3217 APInt SplatBits, SplatUndef;
3218 unsigned SplatBitSize;
3219 bool HasAnyUndefs;
3220 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003221 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003222 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003223 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003224 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003225 SplatUndef.getZExtValue(), SplatBitSize,
3226 DAG, VmovVT, VT.is128BitVector(), true);
3227 if (Val.getNode()) {
3228 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
3229 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vmov);
3230 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003231
3232 // Try an immediate VMVN.
3233 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3234 ((1LL << SplatBitSize) - 1));
3235 Val = isNEONModifiedImm(NegatedImm,
3236 SplatUndef.getZExtValue(), SplatBitSize,
3237 DAG, VmovVT, VT.is128BitVector(), false);
3238 if (Val.getNode()) {
3239 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
3240 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vmov);
3241 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003242 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003243 }
3244
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003245 // Scan through the operands to see if only one value is used.
3246 unsigned NumElts = VT.getVectorNumElements();
3247 bool isOnlyLowElement = true;
3248 bool usesOnlyOneValue = true;
3249 bool isConstant = true;
3250 SDValue Value;
3251 for (unsigned i = 0; i < NumElts; ++i) {
3252 SDValue V = Op.getOperand(i);
3253 if (V.getOpcode() == ISD::UNDEF)
3254 continue;
3255 if (i > 0)
3256 isOnlyLowElement = false;
3257 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3258 isConstant = false;
3259
3260 if (!Value.getNode())
3261 Value = V;
3262 else if (V != Value)
3263 usesOnlyOneValue = false;
3264 }
3265
3266 if (!Value.getNode())
3267 return DAG.getUNDEF(VT);
3268
3269 if (isOnlyLowElement)
3270 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3271
3272 // If all elements are constants, fall back to the default expansion, which
3273 // will generate a load from the constant pool.
3274 if (isConstant)
3275 return SDValue();
3276
3277 // Use VDUP for non-constant splats.
Bob Wilson069e4342010-05-23 05:42:31 +00003278 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3279 if (usesOnlyOneValue && EltSize <= 32)
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003280 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3281
3282 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003283 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3284 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003285 if (EltSize >= 32) {
3286 // Do the expansion with floating-point types, since that is what the VFP
3287 // registers are defined to use, and since i64 is not legal.
3288 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3289 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003290 SmallVector<SDValue, 8> Ops;
3291 for (unsigned i = 0; i < NumElts; ++i)
3292 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, dl, EltVT, Op.getOperand(i)));
3293 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003294 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003295 }
3296
3297 return SDValue();
3298}
3299
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003300/// isShuffleMaskLegal - Targets can use this to indicate that they only
3301/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3302/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3303/// are assumed to be legal.
3304bool
3305ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
3306 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003307 if (VT.getVectorNumElements() == 4 &&
3308 (VT.is128BitVector() || VT.is64BitVector())) {
3309 unsigned PFIndexes[4];
3310 for (unsigned i = 0; i != 4; ++i) {
3311 if (M[i] < 0)
3312 PFIndexes[i] = 8;
3313 else
3314 PFIndexes[i] = M[i];
3315 }
3316
3317 // Compute the index in the perfect shuffle table.
3318 unsigned PFTableIndex =
3319 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3320 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3321 unsigned Cost = (PFEntry >> 30);
3322
3323 if (Cost <= 4)
3324 return true;
3325 }
3326
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003327 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00003328 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003329
Bob Wilson53dd2452010-06-07 23:53:38 +00003330 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3331 return (EltSize >= 32 ||
3332 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003333 isVREVMask(M, VT, 64) ||
3334 isVREVMask(M, VT, 32) ||
3335 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00003336 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
3337 isVTRNMask(M, VT, WhichResult) ||
3338 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00003339 isVZIPMask(M, VT, WhichResult) ||
3340 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
3341 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
3342 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003343}
3344
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003345/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
3346/// the specified operations to build the shuffle.
3347static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
3348 SDValue RHS, SelectionDAG &DAG,
3349 DebugLoc dl) {
3350 unsigned OpNum = (PFEntry >> 26) & 0x0F;
3351 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
3352 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
3353
3354 enum {
3355 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
3356 OP_VREV,
3357 OP_VDUP0,
3358 OP_VDUP1,
3359 OP_VDUP2,
3360 OP_VDUP3,
3361 OP_VEXT1,
3362 OP_VEXT2,
3363 OP_VEXT3,
3364 OP_VUZPL, // VUZP, left result
3365 OP_VUZPR, // VUZP, right result
3366 OP_VZIPL, // VZIP, left result
3367 OP_VZIPR, // VZIP, right result
3368 OP_VTRNL, // VTRN, left result
3369 OP_VTRNR // VTRN, right result
3370 };
3371
3372 if (OpNum == OP_COPY) {
3373 if (LHSID == (1*9+2)*9+3) return LHS;
3374 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
3375 return RHS;
3376 }
3377
3378 SDValue OpLHS, OpRHS;
3379 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
3380 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
3381 EVT VT = OpLHS.getValueType();
3382
3383 switch (OpNum) {
3384 default: llvm_unreachable("Unknown shuffle opcode!");
3385 case OP_VREV:
3386 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
3387 case OP_VDUP0:
3388 case OP_VDUP1:
3389 case OP_VDUP2:
3390 case OP_VDUP3:
3391 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003392 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003393 case OP_VEXT1:
3394 case OP_VEXT2:
3395 case OP_VEXT3:
3396 return DAG.getNode(ARMISD::VEXT, dl, VT,
3397 OpLHS, OpRHS,
3398 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
3399 case OP_VUZPL:
3400 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003401 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003402 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
3403 case OP_VZIPL:
3404 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003405 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003406 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
3407 case OP_VTRNL:
3408 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003409 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3410 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003411 }
3412}
3413
Bob Wilson5bafff32009-06-22 23:27:02 +00003414static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003415 SDValue V1 = Op.getOperand(0);
3416 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00003417 DebugLoc dl = Op.getDebugLoc();
3418 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003419 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003420 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00003421
Bob Wilson28865062009-08-13 02:13:04 +00003422 // Convert shuffles that are directly supported on NEON to target-specific
3423 // DAG nodes, instead of keeping them as shuffles and matching them again
3424 // during code selection. This is more efficient and avoids the possibility
3425 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00003426 // FIXME: floating-point vectors should be canonicalized to integer vectors
3427 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003428 SVN->getMask(ShuffleMask);
3429
Bob Wilson53dd2452010-06-07 23:53:38 +00003430 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3431 if (EltSize <= 32) {
3432 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
3433 int Lane = SVN->getSplatIndex();
3434 // If this is undef splat, generate it via "just" vdup, if possible.
3435 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00003436
Bob Wilson53dd2452010-06-07 23:53:38 +00003437 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3438 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
3439 }
3440 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
3441 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00003442 }
Bob Wilson53dd2452010-06-07 23:53:38 +00003443
3444 bool ReverseVEXT;
3445 unsigned Imm;
3446 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
3447 if (ReverseVEXT)
3448 std::swap(V1, V2);
3449 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
3450 DAG.getConstant(Imm, MVT::i32));
3451 }
3452
3453 if (isVREVMask(ShuffleMask, VT, 64))
3454 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
3455 if (isVREVMask(ShuffleMask, VT, 32))
3456 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
3457 if (isVREVMask(ShuffleMask, VT, 16))
3458 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
3459
3460 // Check for Neon shuffles that modify both input vectors in place.
3461 // If both results are used, i.e., if there are two shuffles with the same
3462 // source operands and with masks corresponding to both results of one of
3463 // these operations, DAG memoization will ensure that a single node is
3464 // used for both shuffles.
3465 unsigned WhichResult;
3466 if (isVTRNMask(ShuffleMask, VT, WhichResult))
3467 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3468 V1, V2).getValue(WhichResult);
3469 if (isVUZPMask(ShuffleMask, VT, WhichResult))
3470 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3471 V1, V2).getValue(WhichResult);
3472 if (isVZIPMask(ShuffleMask, VT, WhichResult))
3473 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3474 V1, V2).getValue(WhichResult);
3475
3476 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
3477 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3478 V1, V1).getValue(WhichResult);
3479 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3480 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3481 V1, V1).getValue(WhichResult);
3482 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3483 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3484 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00003485 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003486
Bob Wilsonc692cb72009-08-21 20:54:19 +00003487 // If the shuffle is not directly supported and it has 4 elements, use
3488 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003489 unsigned NumElts = VT.getVectorNumElements();
3490 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003491 unsigned PFIndexes[4];
3492 for (unsigned i = 0; i != 4; ++i) {
3493 if (ShuffleMask[i] < 0)
3494 PFIndexes[i] = 8;
3495 else
3496 PFIndexes[i] = ShuffleMask[i];
3497 }
3498
3499 // Compute the index in the perfect shuffle table.
3500 unsigned PFTableIndex =
3501 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003502 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3503 unsigned Cost = (PFEntry >> 30);
3504
3505 if (Cost <= 4)
3506 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
3507 }
Bob Wilsond8e17572009-08-12 22:31:50 +00003508
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003509 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003510 if (EltSize >= 32) {
3511 // Do the expansion with floating-point types, since that is what the VFP
3512 // registers are defined to use, and since i64 is not legal.
3513 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3514 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
3515 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V1);
3516 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003517 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003518 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00003519 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003520 Ops.push_back(DAG.getUNDEF(EltVT));
3521 else
3522 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
3523 ShuffleMask[i] < (int)NumElts ? V1 : V2,
3524 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
3525 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00003526 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003527 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Bob Wilson63b88452010-05-20 18:39:53 +00003528 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
3529 }
3530
Bob Wilson22cac0d2009-08-14 05:16:33 +00003531 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003532}
3533
Bob Wilson5bafff32009-06-22 23:27:02 +00003534static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003535 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003536 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003537 SDValue Vec = Op.getOperand(0);
3538 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00003539 assert(VT == MVT::i32 &&
3540 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
3541 "unexpected type for custom-lowering vector extract");
3542 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00003543}
3544
Bob Wilsona6d65862009-08-03 20:36:38 +00003545static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3546 // The only time a CONCAT_VECTORS operation can have legal types is when
3547 // two 64-bit vectors are concatenated to a 128-bit vector.
3548 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
3549 "unexpected CONCAT_VECTORS");
3550 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00003551 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00003552 SDValue Op0 = Op.getOperand(0);
3553 SDValue Op1 = Op.getOperand(1);
3554 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003555 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3556 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00003557 DAG.getIntPtrConstant(0));
3558 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003559 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3560 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00003561 DAG.getIntPtrConstant(1));
3562 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003563}
3564
Dan Gohmand858e902010-04-17 15:26:15 +00003565SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003566 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003567 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00003568 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00003569 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003570 case ISD::GlobalAddress:
3571 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
3572 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00003573 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00003574 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
3575 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003576 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00003577 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00003578 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00003579 case ISD::SINT_TO_FP:
3580 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
3581 case ISD::FP_TO_SINT:
3582 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003583 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003584 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003585 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003586 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00003587 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00003588 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00003589 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
3590 Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00003591 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003592 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00003593 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00003594 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00003595 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003596 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00003597 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00003598 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003599 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
3600 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3601 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003602 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00003603 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003604 }
Dan Gohman475871a2008-07-27 21:46:04 +00003605 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003606}
3607
Duncan Sands1607f052008-12-01 11:39:25 +00003608/// ReplaceNodeResults - Replace the results of node with an illegal result
3609/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00003610void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
3611 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00003612 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00003613 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00003614 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00003615 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003616 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003617 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003618 case ISD::BIT_CONVERT:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003619 Res = ExpandBIT_CONVERT(N, DAG);
3620 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00003621 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003622 case ISD::SRA:
3623 Res = LowerShift(N, DAG, Subtarget);
3624 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003625 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00003626 if (Res.getNode())
3627 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00003628}
Chris Lattner27a6c732007-11-24 07:07:01 +00003629
Evan Chenga8e29892007-01-19 07:51:42 +00003630//===----------------------------------------------------------------------===//
3631// ARM Scheduler Hooks
3632//===----------------------------------------------------------------------===//
3633
3634MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003635ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
3636 MachineBasicBlock *BB,
3637 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00003638 unsigned dest = MI->getOperand(0).getReg();
3639 unsigned ptr = MI->getOperand(1).getReg();
3640 unsigned oldval = MI->getOperand(2).getReg();
3641 unsigned newval = MI->getOperand(3).getReg();
3642 unsigned scratch = BB->getParent()->getRegInfo()
3643 .createVirtualRegister(ARM::GPRRegisterClass);
3644 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3645 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003646 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00003647
3648 unsigned ldrOpc, strOpc;
3649 switch (Size) {
3650 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003651 case 1:
3652 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
3653 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
3654 break;
3655 case 2:
3656 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3657 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3658 break;
3659 case 4:
3660 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3661 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3662 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00003663 }
3664
3665 MachineFunction *MF = BB->getParent();
3666 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3667 MachineFunction::iterator It = BB;
3668 ++It; // insert the new blocks after the current block
3669
3670 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3671 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3672 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3673 MF->insert(It, loop1MBB);
3674 MF->insert(It, loop2MBB);
3675 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00003676
3677 // Transfer the remainder of BB and its successor edges to exitMBB.
3678 exitMBB->splice(exitMBB->begin(), BB,
3679 llvm::next(MachineBasicBlock::iterator(MI)),
3680 BB->end());
3681 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003682
3683 // thisMBB:
3684 // ...
3685 // fallthrough --> loop1MBB
3686 BB->addSuccessor(loop1MBB);
3687
3688 // loop1MBB:
3689 // ldrex dest, [ptr]
3690 // cmp dest, oldval
3691 // bne exitMBB
3692 BB = loop1MBB;
3693 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003694 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003695 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003696 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3697 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003698 BB->addSuccessor(loop2MBB);
3699 BB->addSuccessor(exitMBB);
3700
3701 // loop2MBB:
3702 // strex scratch, newval, [ptr]
3703 // cmp scratch, #0
3704 // bne loop1MBB
3705 BB = loop2MBB;
3706 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
3707 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003708 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003709 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003710 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3711 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003712 BB->addSuccessor(loop1MBB);
3713 BB->addSuccessor(exitMBB);
3714
3715 // exitMBB:
3716 // ...
3717 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00003718
Dan Gohman14152b42010-07-06 20:24:04 +00003719 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00003720
Jim Grosbach5278eb82009-12-11 01:42:04 +00003721 return BB;
3722}
3723
3724MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003725ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
3726 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00003727 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
3728 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3729
3730 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003731 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003732 MachineFunction::iterator It = BB;
3733 ++It;
3734
3735 unsigned dest = MI->getOperand(0).getReg();
3736 unsigned ptr = MI->getOperand(1).getReg();
3737 unsigned incr = MI->getOperand(2).getReg();
3738 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00003739
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003740 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003741 unsigned ldrOpc, strOpc;
3742 switch (Size) {
3743 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003744 case 1:
3745 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00003746 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003747 break;
3748 case 2:
3749 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3750 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3751 break;
3752 case 4:
3753 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3754 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3755 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00003756 }
3757
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003758 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3759 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3760 MF->insert(It, loopMBB);
3761 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00003762
3763 // Transfer the remainder of BB and its successor edges to exitMBB.
3764 exitMBB->splice(exitMBB->begin(), BB,
3765 llvm::next(MachineBasicBlock::iterator(MI)),
3766 BB->end());
3767 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003768
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003769 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003770 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3771 unsigned scratch2 = (!BinOpcode) ? incr :
3772 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3773
3774 // thisMBB:
3775 // ...
3776 // fallthrough --> loopMBB
3777 BB->addSuccessor(loopMBB);
3778
3779 // loopMBB:
3780 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003781 // <binop> scratch2, dest, incr
3782 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00003783 // cmp scratch, #0
3784 // bne- loopMBB
3785 // fallthrough --> exitMBB
3786 BB = loopMBB;
3787 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00003788 if (BinOpcode) {
3789 // operand order needs to go the other way for NAND
3790 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
3791 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3792 addReg(incr).addReg(dest)).addReg(0);
3793 else
3794 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3795 addReg(dest).addReg(incr)).addReg(0);
3796 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00003797
3798 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
3799 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003800 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00003801 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003802 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3803 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003804
3805 BB->addSuccessor(loopMBB);
3806 BB->addSuccessor(exitMBB);
3807
3808 // exitMBB:
3809 // ...
3810 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00003811
Dan Gohman14152b42010-07-06 20:24:04 +00003812 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00003813
Jim Grosbachc3c23542009-12-14 04:22:04 +00003814 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00003815}
3816
Evan Cheng218977b2010-07-13 19:27:42 +00003817static
3818MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
3819 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
3820 E = MBB->succ_end(); I != E; ++I)
3821 if (*I != Succ)
3822 return *I;
3823 llvm_unreachable("Expecting a BB with two successors!");
3824}
3825
Jim Grosbache801dc42009-12-12 01:40:06 +00003826MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00003827ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003828 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003829 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00003830 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003831 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00003832 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00003833 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00003834 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00003835 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00003836
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003837 case ARM::ATOMIC_LOAD_ADD_I8:
3838 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3839 case ARM::ATOMIC_LOAD_ADD_I16:
3840 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3841 case ARM::ATOMIC_LOAD_ADD_I32:
3842 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003843
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003844 case ARM::ATOMIC_LOAD_AND_I8:
3845 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3846 case ARM::ATOMIC_LOAD_AND_I16:
3847 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3848 case ARM::ATOMIC_LOAD_AND_I32:
3849 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003850
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003851 case ARM::ATOMIC_LOAD_OR_I8:
3852 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3853 case ARM::ATOMIC_LOAD_OR_I16:
3854 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3855 case ARM::ATOMIC_LOAD_OR_I32:
3856 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003857
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003858 case ARM::ATOMIC_LOAD_XOR_I8:
3859 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3860 case ARM::ATOMIC_LOAD_XOR_I16:
3861 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3862 case ARM::ATOMIC_LOAD_XOR_I32:
3863 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003864
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003865 case ARM::ATOMIC_LOAD_NAND_I8:
3866 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3867 case ARM::ATOMIC_LOAD_NAND_I16:
3868 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3869 case ARM::ATOMIC_LOAD_NAND_I32:
3870 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003871
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003872 case ARM::ATOMIC_LOAD_SUB_I8:
3873 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3874 case ARM::ATOMIC_LOAD_SUB_I16:
3875 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3876 case ARM::ATOMIC_LOAD_SUB_I32:
3877 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003878
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003879 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
3880 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
3881 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00003882
3883 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
3884 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
3885 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003886
Evan Cheng007ea272009-08-12 05:17:19 +00003887 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00003888 // To "insert" a SELECT_CC instruction, we actually have to insert the
3889 // diamond control-flow pattern. The incoming instruction knows the
3890 // destination vreg to set, the condition code register to branch on, the
3891 // true/false values to select between, and a branch opcode to use.
3892 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003893 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00003894 ++It;
3895
3896 // thisMBB:
3897 // ...
3898 // TrueVal = ...
3899 // cmpTY ccX, r1, r2
3900 // bCC copy1MBB
3901 // fallthrough --> copy0MBB
3902 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003903 MachineFunction *F = BB->getParent();
3904 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3905 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00003906 F->insert(It, copy0MBB);
3907 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00003908
3909 // Transfer the remainder of BB and its successor edges to sinkMBB.
3910 sinkMBB->splice(sinkMBB->begin(), BB,
3911 llvm::next(MachineBasicBlock::iterator(MI)),
3912 BB->end());
3913 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
3914
Dan Gohman258c58c2010-07-06 15:49:48 +00003915 BB->addSuccessor(copy0MBB);
3916 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00003917
Dan Gohman14152b42010-07-06 20:24:04 +00003918 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
3919 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
3920
Evan Chenga8e29892007-01-19 07:51:42 +00003921 // copy0MBB:
3922 // %FalseValue = ...
3923 // # fallthrough to sinkMBB
3924 BB = copy0MBB;
3925
3926 // Update machine-CFG edges
3927 BB->addSuccessor(sinkMBB);
3928
3929 // sinkMBB:
3930 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3931 // ...
3932 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00003933 BuildMI(*BB, BB->begin(), dl,
3934 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00003935 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
3936 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
3937
Dan Gohman14152b42010-07-06 20:24:04 +00003938 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00003939 return BB;
3940 }
Evan Cheng86198642009-08-07 00:34:42 +00003941
Evan Cheng218977b2010-07-13 19:27:42 +00003942 case ARM::BCCi64:
3943 case ARM::BCCZi64: {
3944 // Compare both parts that make up the double comparison separately for
3945 // equality.
3946 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
3947
3948 unsigned LHS1 = MI->getOperand(1).getReg();
3949 unsigned LHS2 = MI->getOperand(2).getReg();
3950 if (RHSisZero) {
3951 AddDefaultPred(BuildMI(BB, dl,
3952 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
3953 .addReg(LHS1).addImm(0));
3954 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
3955 .addReg(LHS2).addImm(0)
3956 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
3957 } else {
3958 unsigned RHS1 = MI->getOperand(3).getReg();
3959 unsigned RHS2 = MI->getOperand(4).getReg();
3960 AddDefaultPred(BuildMI(BB, dl,
3961 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
3962 .addReg(LHS1).addReg(RHS1));
3963 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
3964 .addReg(LHS2).addReg(RHS2)
3965 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
3966 }
3967
3968 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
3969 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
3970 if (MI->getOperand(0).getImm() == ARMCC::NE)
3971 std::swap(destMBB, exitMBB);
3972
3973 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3974 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
3975 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
3976 .addMBB(exitMBB);
3977
3978 MI->eraseFromParent(); // The pseudo instruction is gone now.
3979 return BB;
3980 }
3981
Evan Cheng86198642009-08-07 00:34:42 +00003982 case ARM::tANDsp:
3983 case ARM::tADDspr_:
3984 case ARM::tSUBspi_:
3985 case ARM::t2SUBrSPi_:
3986 case ARM::t2SUBrSPi12_:
3987 case ARM::t2SUBrSPs_: {
3988 MachineFunction *MF = BB->getParent();
3989 unsigned DstReg = MI->getOperand(0).getReg();
3990 unsigned SrcReg = MI->getOperand(1).getReg();
3991 bool DstIsDead = MI->getOperand(0).isDead();
3992 bool SrcIsKill = MI->getOperand(1).isKill();
3993
3994 if (SrcReg != ARM::SP) {
3995 // Copy the source to SP from virtual register.
3996 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
3997 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3998 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
Dan Gohman14152b42010-07-06 20:24:04 +00003999 BuildMI(*BB, MI, dl, TII->get(CopyOpc), ARM::SP)
Evan Cheng86198642009-08-07 00:34:42 +00004000 .addReg(SrcReg, getKillRegState(SrcIsKill));
4001 }
4002
4003 unsigned OpOpc = 0;
4004 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
4005 switch (MI->getOpcode()) {
4006 default:
4007 llvm_unreachable("Unexpected pseudo instruction!");
4008 case ARM::tANDsp:
4009 OpOpc = ARM::tAND;
4010 NeedPred = true;
4011 break;
4012 case ARM::tADDspr_:
4013 OpOpc = ARM::tADDspr;
4014 break;
4015 case ARM::tSUBspi_:
4016 OpOpc = ARM::tSUBspi;
4017 break;
4018 case ARM::t2SUBrSPi_:
4019 OpOpc = ARM::t2SUBrSPi;
4020 NeedPred = true; NeedCC = true;
4021 break;
4022 case ARM::t2SUBrSPi12_:
4023 OpOpc = ARM::t2SUBrSPi12;
4024 NeedPred = true;
4025 break;
4026 case ARM::t2SUBrSPs_:
4027 OpOpc = ARM::t2SUBrSPs;
4028 NeedPred = true; NeedCC = true; NeedOp3 = true;
4029 break;
4030 }
Dan Gohman14152b42010-07-06 20:24:04 +00004031 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(OpOpc), ARM::SP);
Evan Cheng86198642009-08-07 00:34:42 +00004032 if (OpOpc == ARM::tAND)
4033 AddDefaultT1CC(MIB);
4034 MIB.addReg(ARM::SP);
4035 MIB.addOperand(MI->getOperand(2));
4036 if (NeedOp3)
4037 MIB.addOperand(MI->getOperand(3));
4038 if (NeedPred)
4039 AddDefaultPred(MIB);
4040 if (NeedCC)
4041 AddDefaultCC(MIB);
4042
4043 // Copy the result from SP to virtual register.
4044 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
4045 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
4046 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
Dan Gohman14152b42010-07-06 20:24:04 +00004047 BuildMI(*BB, MI, dl, TII->get(CopyOpc))
Evan Cheng86198642009-08-07 00:34:42 +00004048 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
4049 .addReg(ARM::SP);
Dan Gohman14152b42010-07-06 20:24:04 +00004050 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng86198642009-08-07 00:34:42 +00004051 return BB;
4052 }
Evan Chenga8e29892007-01-19 07:51:42 +00004053 }
4054}
4055
4056//===----------------------------------------------------------------------===//
4057// ARM Optimization Hooks
4058//===----------------------------------------------------------------------===//
4059
Chris Lattnerd1980a52009-03-12 06:52:53 +00004060static
4061SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
4062 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00004063 SelectionDAG &DAG = DCI.DAG;
4064 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004065 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00004066 unsigned Opc = N->getOpcode();
4067 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
4068 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
4069 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
4070 ISD::CondCode CC = ISD::SETCC_INVALID;
4071
4072 if (isSlctCC) {
4073 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
4074 } else {
4075 SDValue CCOp = Slct.getOperand(0);
4076 if (CCOp.getOpcode() == ISD::SETCC)
4077 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
4078 }
4079
4080 bool DoXform = false;
4081 bool InvCC = false;
4082 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
4083 "Bad input!");
4084
4085 if (LHS.getOpcode() == ISD::Constant &&
4086 cast<ConstantSDNode>(LHS)->isNullValue()) {
4087 DoXform = true;
4088 } else if (CC != ISD::SETCC_INVALID &&
4089 RHS.getOpcode() == ISD::Constant &&
4090 cast<ConstantSDNode>(RHS)->isNullValue()) {
4091 std::swap(LHS, RHS);
4092 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004093 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00004094 Op0.getOperand(0).getValueType();
4095 bool isInt = OpVT.isInteger();
4096 CC = ISD::getSetCCInverse(CC, isInt);
4097
4098 if (!TLI.isCondCodeLegal(CC, OpVT))
4099 return SDValue(); // Inverse operator isn't legal.
4100
4101 DoXform = true;
4102 InvCC = true;
4103 }
4104
4105 if (DoXform) {
4106 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
4107 if (isSlctCC)
4108 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
4109 Slct.getOperand(0), Slct.getOperand(1), CC);
4110 SDValue CCOp = Slct.getOperand(0);
4111 if (InvCC)
4112 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
4113 CCOp.getOperand(0), CCOp.getOperand(1), CC);
4114 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
4115 CCOp, OtherOp, Result);
4116 }
4117 return SDValue();
4118}
4119
4120/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
4121static SDValue PerformADDCombine(SDNode *N,
4122 TargetLowering::DAGCombinerInfo &DCI) {
4123 // added by evan in r37685 with no testcase.
4124 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00004125
Chris Lattnerd1980a52009-03-12 06:52:53 +00004126 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
4127 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
4128 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
4129 if (Result.getNode()) return Result;
4130 }
4131 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
4132 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
4133 if (Result.getNode()) return Result;
4134 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00004135
Chris Lattnerd1980a52009-03-12 06:52:53 +00004136 return SDValue();
4137}
4138
4139/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
4140static SDValue PerformSUBCombine(SDNode *N,
4141 TargetLowering::DAGCombinerInfo &DCI) {
4142 // added by evan in r37685 with no testcase.
4143 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00004144
Chris Lattnerd1980a52009-03-12 06:52:53 +00004145 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
4146 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
4147 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
4148 if (Result.getNode()) return Result;
4149 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00004150
Chris Lattnerd1980a52009-03-12 06:52:53 +00004151 return SDValue();
4152}
4153
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004154static SDValue PerformMULCombine(SDNode *N,
4155 TargetLowering::DAGCombinerInfo &DCI,
4156 const ARMSubtarget *Subtarget) {
4157 SelectionDAG &DAG = DCI.DAG;
4158
4159 if (Subtarget->isThumb1Only())
4160 return SDValue();
4161
4162 if (DAG.getMachineFunction().
4163 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
4164 return SDValue();
4165
4166 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
4167 return SDValue();
4168
4169 EVT VT = N->getValueType(0);
4170 if (VT != MVT::i32)
4171 return SDValue();
4172
4173 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
4174 if (!C)
4175 return SDValue();
4176
4177 uint64_t MulAmt = C->getZExtValue();
4178 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
4179 ShiftAmt = ShiftAmt & (32 - 1);
4180 SDValue V = N->getOperand(0);
4181 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004182
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004183 SDValue Res;
4184 MulAmt >>= ShiftAmt;
4185 if (isPowerOf2_32(MulAmt - 1)) {
4186 // (mul x, 2^N + 1) => (add (shl x, N), x)
4187 Res = DAG.getNode(ISD::ADD, DL, VT,
4188 V, DAG.getNode(ISD::SHL, DL, VT,
4189 V, DAG.getConstant(Log2_32(MulAmt-1),
4190 MVT::i32)));
4191 } else if (isPowerOf2_32(MulAmt + 1)) {
4192 // (mul x, 2^N - 1) => (sub (shl x, N), x)
4193 Res = DAG.getNode(ISD::SUB, DL, VT,
4194 DAG.getNode(ISD::SHL, DL, VT,
4195 V, DAG.getConstant(Log2_32(MulAmt+1),
4196 MVT::i32)),
4197 V);
4198 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004199 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004200
4201 if (ShiftAmt != 0)
4202 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
4203 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004204
4205 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004206 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004207 return SDValue();
4208}
4209
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004210/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
4211static SDValue PerformORCombine(SDNode *N,
4212 TargetLowering::DAGCombinerInfo &DCI,
4213 const ARMSubtarget *Subtarget) {
Jim Grosbach54238562010-07-17 03:30:54 +00004214 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
4215 // reasonable.
4216
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004217 // BFI is only available on V6T2+
4218 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
4219 return SDValue();
4220
4221 SelectionDAG &DAG = DCI.DAG;
4222 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Jim Grosbach54238562010-07-17 03:30:54 +00004223 DebugLoc DL = N->getDebugLoc();
4224 // 1) or (and A, mask), val => ARMbfi A, val, mask
4225 // iff (val & mask) == val
4226 //
4227 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
4228 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
4229 // && CountPopulation_32(mask) == CountPopulation_32(~mask2)
4230 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
4231 // && CountPopulation_32(mask) == CountPopulation_32(~mask2)
4232 // (i.e., copy a bitfield value into another bitfield of the same width)
4233 if (N0.getOpcode() != ISD::AND)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004234 return SDValue();
4235
4236 EVT VT = N->getValueType(0);
4237 if (VT != MVT::i32)
4238 return SDValue();
4239
Jim Grosbach54238562010-07-17 03:30:54 +00004240
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004241 // The value and the mask need to be constants so we can verify this is
4242 // actually a bitfield set. If the mask is 0xffff, we can do better
4243 // via a movt instruction, so don't use BFI in that case.
4244 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
4245 if (!C)
4246 return SDValue();
4247 unsigned Mask = C->getZExtValue();
4248 if (Mask == 0xffff)
4249 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004250 SDValue Res;
4251 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
4252 if ((C = dyn_cast<ConstantSDNode>(N1))) {
4253 unsigned Val = C->getZExtValue();
4254 if (!ARM::isBitFieldInvertedMask(Mask) || (Val & ~Mask) != Val)
4255 return SDValue();
4256 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004257
Jim Grosbach54238562010-07-17 03:30:54 +00004258 Res = DAG.getNode(ARMISD::BFI, DL, VT, N0.getOperand(0),
4259 DAG.getConstant(Val, MVT::i32),
4260 DAG.getConstant(Mask, MVT::i32));
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004261
Jim Grosbach54238562010-07-17 03:30:54 +00004262 // Do not add new nodes to DAG combiner worklist.
4263 DCI.CombineTo(N, Res, false);
4264 } else if (N1.getOpcode() == ISD::AND) {
4265 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
4266 C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
4267 if (!C)
4268 return SDValue();
4269 unsigned Mask2 = C->getZExtValue();
4270
4271 if (ARM::isBitFieldInvertedMask(Mask) &&
4272 ARM::isBitFieldInvertedMask(~Mask2) &&
4273 (CountPopulation_32(Mask) == CountPopulation_32(~Mask2))) {
4274 // The pack halfword instruction works better for masks that fit it,
4275 // so use that when it's available.
4276 if (Subtarget->hasT2ExtractPack() &&
4277 (Mask == 0xffff || Mask == 0xffff0000))
4278 return SDValue();
4279 // 2a
4280 unsigned lsb = CountTrailingZeros_32(Mask2);
4281 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
4282 DAG.getConstant(lsb, MVT::i32));
4283 Res = DAG.getNode(ARMISD::BFI, DL, VT, N0.getOperand(0), Res,
4284 DAG.getConstant(Mask, MVT::i32));
4285 // Do not add new nodes to DAG combiner worklist.
4286 DCI.CombineTo(N, Res, false);
4287 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
4288 ARM::isBitFieldInvertedMask(Mask2) &&
4289 (CountPopulation_32(~Mask) == CountPopulation_32(Mask2))) {
4290 // The pack halfword instruction works better for masks that fit it,
4291 // so use that when it's available.
4292 if (Subtarget->hasT2ExtractPack() &&
4293 (Mask2 == 0xffff || Mask2 == 0xffff0000))
4294 return SDValue();
4295 // 2b
4296 unsigned lsb = CountTrailingZeros_32(Mask);
4297 Res = DAG.getNode(ISD::SRL, DL, VT, N0.getOperand(0),
4298 DAG.getConstant(lsb, MVT::i32));
4299 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
4300 DAG.getConstant(Mask2, MVT::i32));
4301 // Do not add new nodes to DAG combiner worklist.
4302 DCI.CombineTo(N, Res, false);
4303 }
4304 }
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004305
4306 return SDValue();
4307}
4308
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +00004309/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
4310/// ARMISD::VMOVRRD.
Jim Grosbache5165492009-11-09 00:11:35 +00004311static SDValue PerformVMOVRRDCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004312 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004313 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00004314 SDValue InDouble = N->getOperand(0);
Jim Grosbache5165492009-11-09 00:11:35 +00004315 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004316 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004317 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004318}
4319
Bob Wilson9e82bf12010-07-14 01:22:12 +00004320/// PerformVDUPLANECombine - Target-specific dag combine xforms for
4321/// ARMISD::VDUPLANE.
4322static SDValue PerformVDUPLANECombine(SDNode *N,
4323 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004324 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
4325 // redundant.
Bob Wilson9e82bf12010-07-14 01:22:12 +00004326 SDValue Op = N->getOperand(0);
4327 EVT VT = N->getValueType(0);
4328
4329 // Ignore bit_converts.
4330 while (Op.getOpcode() == ISD::BIT_CONVERT)
4331 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004332 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00004333 return SDValue();
4334
4335 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
4336 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
4337 // The canonical VMOV for a zero vector uses a 32-bit element size.
4338 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
4339 unsigned EltBits;
4340 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
4341 EltSize = 8;
4342 if (EltSize > VT.getVectorElementType().getSizeInBits())
4343 return SDValue();
4344
4345 SDValue Res = DCI.DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
4346 return DCI.CombineTo(N, Res, false);
4347}
4348
Bob Wilson5bafff32009-06-22 23:27:02 +00004349/// getVShiftImm - Check if this is a valid build_vector for the immediate
4350/// operand of a vector shift operation, where all the elements of the
4351/// build_vector must have the same constant integer value.
4352static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
4353 // Ignore bit_converts.
4354 while (Op.getOpcode() == ISD::BIT_CONVERT)
4355 Op = Op.getOperand(0);
4356 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
4357 APInt SplatBits, SplatUndef;
4358 unsigned SplatBitSize;
4359 bool HasAnyUndefs;
4360 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
4361 HasAnyUndefs, ElementBits) ||
4362 SplatBitSize > ElementBits)
4363 return false;
4364 Cnt = SplatBits.getSExtValue();
4365 return true;
4366}
4367
4368/// isVShiftLImm - Check if this is a valid build_vector for the immediate
4369/// operand of a vector shift left operation. That value must be in the range:
4370/// 0 <= Value < ElementBits for a left shift; or
4371/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00004372static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00004373 assert(VT.isVector() && "vector shift count is not a vector type");
4374 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
4375 if (! getVShiftImm(Op, ElementBits, Cnt))
4376 return false;
4377 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
4378}
4379
4380/// isVShiftRImm - Check if this is a valid build_vector for the immediate
4381/// operand of a vector shift right operation. For a shift opcode, the value
4382/// is positive, but for an intrinsic the value count must be negative. The
4383/// absolute value must be in the range:
4384/// 1 <= |Value| <= ElementBits for a right shift; or
4385/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00004386static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00004387 int64_t &Cnt) {
4388 assert(VT.isVector() && "vector shift count is not a vector type");
4389 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
4390 if (! getVShiftImm(Op, ElementBits, Cnt))
4391 return false;
4392 if (isIntrinsic)
4393 Cnt = -Cnt;
4394 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
4395}
4396
4397/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
4398static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
4399 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
4400 switch (IntNo) {
4401 default:
4402 // Don't do anything for most intrinsics.
4403 break;
4404
4405 // Vector shifts: check for immediate versions and lower them.
4406 // Note: This is done during DAG combining instead of DAG legalizing because
4407 // the build_vectors for 64-bit vector element shift counts are generally
4408 // not legal, and it is hard to see their values after they get legalized to
4409 // loads from a constant pool.
4410 case Intrinsic::arm_neon_vshifts:
4411 case Intrinsic::arm_neon_vshiftu:
4412 case Intrinsic::arm_neon_vshiftls:
4413 case Intrinsic::arm_neon_vshiftlu:
4414 case Intrinsic::arm_neon_vshiftn:
4415 case Intrinsic::arm_neon_vrshifts:
4416 case Intrinsic::arm_neon_vrshiftu:
4417 case Intrinsic::arm_neon_vrshiftn:
4418 case Intrinsic::arm_neon_vqshifts:
4419 case Intrinsic::arm_neon_vqshiftu:
4420 case Intrinsic::arm_neon_vqshiftsu:
4421 case Intrinsic::arm_neon_vqshiftns:
4422 case Intrinsic::arm_neon_vqshiftnu:
4423 case Intrinsic::arm_neon_vqshiftnsu:
4424 case Intrinsic::arm_neon_vqrshiftns:
4425 case Intrinsic::arm_neon_vqrshiftnu:
4426 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00004427 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004428 int64_t Cnt;
4429 unsigned VShiftOpc = 0;
4430
4431 switch (IntNo) {
4432 case Intrinsic::arm_neon_vshifts:
4433 case Intrinsic::arm_neon_vshiftu:
4434 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
4435 VShiftOpc = ARMISD::VSHL;
4436 break;
4437 }
4438 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
4439 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
4440 ARMISD::VSHRs : ARMISD::VSHRu);
4441 break;
4442 }
4443 return SDValue();
4444
4445 case Intrinsic::arm_neon_vshiftls:
4446 case Intrinsic::arm_neon_vshiftlu:
4447 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
4448 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00004449 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004450
4451 case Intrinsic::arm_neon_vrshifts:
4452 case Intrinsic::arm_neon_vrshiftu:
4453 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
4454 break;
4455 return SDValue();
4456
4457 case Intrinsic::arm_neon_vqshifts:
4458 case Intrinsic::arm_neon_vqshiftu:
4459 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
4460 break;
4461 return SDValue();
4462
4463 case Intrinsic::arm_neon_vqshiftsu:
4464 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
4465 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00004466 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004467
4468 case Intrinsic::arm_neon_vshiftn:
4469 case Intrinsic::arm_neon_vrshiftn:
4470 case Intrinsic::arm_neon_vqshiftns:
4471 case Intrinsic::arm_neon_vqshiftnu:
4472 case Intrinsic::arm_neon_vqshiftnsu:
4473 case Intrinsic::arm_neon_vqrshiftns:
4474 case Intrinsic::arm_neon_vqrshiftnu:
4475 case Intrinsic::arm_neon_vqrshiftnsu:
4476 // Narrowing shifts require an immediate right shift.
4477 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
4478 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00004479 llvm_unreachable("invalid shift count for narrowing vector shift "
4480 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004481
4482 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004483 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00004484 }
4485
4486 switch (IntNo) {
4487 case Intrinsic::arm_neon_vshifts:
4488 case Intrinsic::arm_neon_vshiftu:
4489 // Opcode already set above.
4490 break;
4491 case Intrinsic::arm_neon_vshiftls:
4492 case Intrinsic::arm_neon_vshiftlu:
4493 if (Cnt == VT.getVectorElementType().getSizeInBits())
4494 VShiftOpc = ARMISD::VSHLLi;
4495 else
4496 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
4497 ARMISD::VSHLLs : ARMISD::VSHLLu);
4498 break;
4499 case Intrinsic::arm_neon_vshiftn:
4500 VShiftOpc = ARMISD::VSHRN; break;
4501 case Intrinsic::arm_neon_vrshifts:
4502 VShiftOpc = ARMISD::VRSHRs; break;
4503 case Intrinsic::arm_neon_vrshiftu:
4504 VShiftOpc = ARMISD::VRSHRu; break;
4505 case Intrinsic::arm_neon_vrshiftn:
4506 VShiftOpc = ARMISD::VRSHRN; break;
4507 case Intrinsic::arm_neon_vqshifts:
4508 VShiftOpc = ARMISD::VQSHLs; break;
4509 case Intrinsic::arm_neon_vqshiftu:
4510 VShiftOpc = ARMISD::VQSHLu; break;
4511 case Intrinsic::arm_neon_vqshiftsu:
4512 VShiftOpc = ARMISD::VQSHLsu; break;
4513 case Intrinsic::arm_neon_vqshiftns:
4514 VShiftOpc = ARMISD::VQSHRNs; break;
4515 case Intrinsic::arm_neon_vqshiftnu:
4516 VShiftOpc = ARMISD::VQSHRNu; break;
4517 case Intrinsic::arm_neon_vqshiftnsu:
4518 VShiftOpc = ARMISD::VQSHRNsu; break;
4519 case Intrinsic::arm_neon_vqrshiftns:
4520 VShiftOpc = ARMISD::VQRSHRNs; break;
4521 case Intrinsic::arm_neon_vqrshiftnu:
4522 VShiftOpc = ARMISD::VQRSHRNu; break;
4523 case Intrinsic::arm_neon_vqrshiftnsu:
4524 VShiftOpc = ARMISD::VQRSHRNsu; break;
4525 }
4526
4527 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004528 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004529 }
4530
4531 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00004532 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004533 int64_t Cnt;
4534 unsigned VShiftOpc = 0;
4535
4536 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
4537 VShiftOpc = ARMISD::VSLI;
4538 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
4539 VShiftOpc = ARMISD::VSRI;
4540 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00004541 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004542 }
4543
4544 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
4545 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00004546 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004547 }
4548
4549 case Intrinsic::arm_neon_vqrshifts:
4550 case Intrinsic::arm_neon_vqrshiftu:
4551 // No immediate versions of these to check for.
4552 break;
4553 }
4554
4555 return SDValue();
4556}
4557
4558/// PerformShiftCombine - Checks for immediate versions of vector shifts and
4559/// lowers them. As with the vector shift intrinsics, this is done during DAG
4560/// combining instead of DAG legalizing because the build_vectors for 64-bit
4561/// vector element shift counts are generally not legal, and it is hard to see
4562/// their values after they get legalized to loads from a constant pool.
4563static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
4564 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00004565 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00004566
4567 // Nothing to be done for scalar shifts.
4568 if (! VT.isVector())
4569 return SDValue();
4570
4571 assert(ST->hasNEON() && "unexpected vector shift");
4572 int64_t Cnt;
4573
4574 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004575 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00004576
4577 case ISD::SHL:
4578 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
4579 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004580 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004581 break;
4582
4583 case ISD::SRA:
4584 case ISD::SRL:
4585 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
4586 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
4587 ARMISD::VSHRs : ARMISD::VSHRu);
4588 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004589 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004590 }
4591 }
4592 return SDValue();
4593}
4594
4595/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
4596/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
4597static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
4598 const ARMSubtarget *ST) {
4599 SDValue N0 = N->getOperand(0);
4600
4601 // Check for sign- and zero-extensions of vector extract operations of 8-
4602 // and 16-bit vector elements. NEON supports these directly. They are
4603 // handled during DAG combining because type legalization will promote them
4604 // to 32-bit types and it is messy to recognize the operations after that.
4605 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
4606 SDValue Vec = N0.getOperand(0);
4607 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004608 EVT VT = N->getValueType(0);
4609 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004610 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4611
Owen Anderson825b72b2009-08-11 20:47:22 +00004612 if (VT == MVT::i32 &&
4613 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00004614 TLI.isTypeLegal(Vec.getValueType())) {
4615
4616 unsigned Opc = 0;
4617 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004618 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00004619 case ISD::SIGN_EXTEND:
4620 Opc = ARMISD::VGETLANEs;
4621 break;
4622 case ISD::ZERO_EXTEND:
4623 case ISD::ANY_EXTEND:
4624 Opc = ARMISD::VGETLANEu;
4625 break;
4626 }
4627 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
4628 }
4629 }
4630
4631 return SDValue();
4632}
4633
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004634/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
4635/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
4636static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
4637 const ARMSubtarget *ST) {
4638 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00004639 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004640 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
4641 // a NaN; only do the transformation when it matches that behavior.
4642
4643 // For now only do this when using NEON for FP operations; if using VFP, it
4644 // is not obvious that the benefit outweighs the cost of switching to the
4645 // NEON pipeline.
4646 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
4647 N->getValueType(0) != MVT::f32)
4648 return SDValue();
4649
4650 SDValue CondLHS = N->getOperand(0);
4651 SDValue CondRHS = N->getOperand(1);
4652 SDValue LHS = N->getOperand(2);
4653 SDValue RHS = N->getOperand(3);
4654 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
4655
4656 unsigned Opcode = 0;
4657 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00004658 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004659 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00004660 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004661 IsReversed = true ; // x CC y ? y : x
4662 } else {
4663 return SDValue();
4664 }
4665
Bob Wilsone742bb52010-02-24 22:15:53 +00004666 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004667 switch (CC) {
4668 default: break;
4669 case ISD::SETOLT:
4670 case ISD::SETOLE:
4671 case ISD::SETLT:
4672 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004673 case ISD::SETULT:
4674 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004675 // If LHS is NaN, an ordered comparison will be false and the result will
4676 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
4677 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4678 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
4679 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4680 break;
4681 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
4682 // will return -0, so vmin can only be used for unsafe math or if one of
4683 // the operands is known to be nonzero.
4684 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
4685 !UnsafeFPMath &&
4686 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4687 break;
4688 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004689 break;
4690
4691 case ISD::SETOGT:
4692 case ISD::SETOGE:
4693 case ISD::SETGT:
4694 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004695 case ISD::SETUGT:
4696 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004697 // If LHS is NaN, an ordered comparison will be false and the result will
4698 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
4699 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4700 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
4701 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4702 break;
4703 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
4704 // will return +0, so vmax can only be used for unsafe math or if one of
4705 // the operands is known to be nonzero.
4706 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
4707 !UnsafeFPMath &&
4708 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4709 break;
4710 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004711 break;
4712 }
4713
4714 if (!Opcode)
4715 return SDValue();
4716 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
4717}
4718
Dan Gohman475871a2008-07-27 21:46:04 +00004719SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004720 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004721 switch (N->getOpcode()) {
4722 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004723 case ISD::ADD: return PerformADDCombine(N, DCI);
4724 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004725 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004726 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Jim Grosbache5165492009-11-09 00:11:35 +00004727 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson9e82bf12010-07-14 01:22:12 +00004728 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004729 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004730 case ISD::SHL:
4731 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004732 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004733 case ISD::SIGN_EXTEND:
4734 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004735 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
4736 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004737 }
Dan Gohman475871a2008-07-27 21:46:04 +00004738 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004739}
4740
Bill Wendlingaf566342009-08-15 21:21:19 +00004741bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
4742 if (!Subtarget->hasV6Ops())
4743 // Pre-v6 does not support unaligned mem access.
4744 return false;
Bob Wilson86fe66d2010-06-25 04:12:31 +00004745
4746 // v6+ may or may not support unaligned mem access depending on the system
4747 // configuration.
4748 // FIXME: This is pretty conservative. Should we provide cmdline option to
4749 // control the behaviour?
4750 if (!Subtarget->isTargetDarwin())
4751 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00004752
4753 switch (VT.getSimpleVT().SimpleTy) {
4754 default:
4755 return false;
4756 case MVT::i8:
4757 case MVT::i16:
4758 case MVT::i32:
4759 return true;
4760 // FIXME: VLD1 etc with standard alignment is legal.
4761 }
4762}
4763
Evan Chenge6c835f2009-08-14 20:09:37 +00004764static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
4765 if (V < 0)
4766 return false;
4767
4768 unsigned Scale = 1;
4769 switch (VT.getSimpleVT().SimpleTy) {
4770 default: return false;
4771 case MVT::i1:
4772 case MVT::i8:
4773 // Scale == 1;
4774 break;
4775 case MVT::i16:
4776 // Scale == 2;
4777 Scale = 2;
4778 break;
4779 case MVT::i32:
4780 // Scale == 4;
4781 Scale = 4;
4782 break;
4783 }
4784
4785 if ((V & (Scale - 1)) != 0)
4786 return false;
4787 V /= Scale;
4788 return V == (V & ((1LL << 5) - 1));
4789}
4790
4791static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
4792 const ARMSubtarget *Subtarget) {
4793 bool isNeg = false;
4794 if (V < 0) {
4795 isNeg = true;
4796 V = - V;
4797 }
4798
4799 switch (VT.getSimpleVT().SimpleTy) {
4800 default: return false;
4801 case MVT::i1:
4802 case MVT::i8:
4803 case MVT::i16:
4804 case MVT::i32:
4805 // + imm12 or - imm8
4806 if (isNeg)
4807 return V == (V & ((1LL << 8) - 1));
4808 return V == (V & ((1LL << 12) - 1));
4809 case MVT::f32:
4810 case MVT::f64:
4811 // Same as ARM mode. FIXME: NEON?
4812 if (!Subtarget->hasVFP2())
4813 return false;
4814 if ((V & 3) != 0)
4815 return false;
4816 V >>= 2;
4817 return V == (V & ((1LL << 8) - 1));
4818 }
4819}
4820
Evan Chengb01fad62007-03-12 23:30:29 +00004821/// isLegalAddressImmediate - Return true if the integer value can be used
4822/// as the offset of the target addressing mode for load / store of the
4823/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00004824static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004825 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00004826 if (V == 0)
4827 return true;
4828
Evan Cheng65011532009-03-09 19:15:00 +00004829 if (!VT.isSimple())
4830 return false;
4831
Evan Chenge6c835f2009-08-14 20:09:37 +00004832 if (Subtarget->isThumb1Only())
4833 return isLegalT1AddressImmediate(V, VT);
4834 else if (Subtarget->isThumb2())
4835 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00004836
Evan Chenge6c835f2009-08-14 20:09:37 +00004837 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00004838 if (V < 0)
4839 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00004840 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00004841 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004842 case MVT::i1:
4843 case MVT::i8:
4844 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00004845 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004846 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004847 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00004848 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004849 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004850 case MVT::f32:
4851 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00004852 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00004853 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00004854 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00004855 return false;
4856 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004857 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00004858 }
Evan Chenga8e29892007-01-19 07:51:42 +00004859}
4860
Evan Chenge6c835f2009-08-14 20:09:37 +00004861bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
4862 EVT VT) const {
4863 int Scale = AM.Scale;
4864 if (Scale < 0)
4865 return false;
4866
4867 switch (VT.getSimpleVT().SimpleTy) {
4868 default: return false;
4869 case MVT::i1:
4870 case MVT::i8:
4871 case MVT::i16:
4872 case MVT::i32:
4873 if (Scale == 1)
4874 return true;
4875 // r + r << imm
4876 Scale = Scale & ~1;
4877 return Scale == 2 || Scale == 4 || Scale == 8;
4878 case MVT::i64:
4879 // r + r
4880 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
4881 return true;
4882 return false;
4883 case MVT::isVoid:
4884 // Note, we allow "void" uses (basically, uses that aren't loads or
4885 // stores), because arm allows folding a scale into many arithmetic
4886 // operations. This should be made more precise and revisited later.
4887
4888 // Allow r << imm, but the imm has to be a multiple of two.
4889 if (Scale & 1) return false;
4890 return isPowerOf2_32(Scale);
4891 }
4892}
4893
Chris Lattner37caf8c2007-04-09 23:33:39 +00004894/// isLegalAddressingMode - Return true if the addressing mode represented
4895/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004896bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004897 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004898 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00004899 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00004900 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004901
Chris Lattner37caf8c2007-04-09 23:33:39 +00004902 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004903 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004904 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004905
Chris Lattner37caf8c2007-04-09 23:33:39 +00004906 switch (AM.Scale) {
4907 case 0: // no scale reg, must be "r+i" or "r", or "i".
4908 break;
4909 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00004910 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00004911 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004912 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00004913 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004914 // ARM doesn't support any R+R*scale+imm addr modes.
4915 if (AM.BaseOffs)
4916 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004917
Bob Wilson2c7dab12009-04-08 17:55:28 +00004918 if (!VT.isSimple())
4919 return false;
4920
Evan Chenge6c835f2009-08-14 20:09:37 +00004921 if (Subtarget->isThumb2())
4922 return isLegalT2ScaledAddressingMode(AM, VT);
4923
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004924 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00004925 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00004926 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004927 case MVT::i1:
4928 case MVT::i8:
4929 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004930 if (Scale < 0) Scale = -Scale;
4931 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004932 return true;
4933 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00004934 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00004935 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00004936 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004937 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004938 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004939 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00004940 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004941
Owen Anderson825b72b2009-08-11 20:47:22 +00004942 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004943 // Note, we allow "void" uses (basically, uses that aren't loads or
4944 // stores), because arm allows folding a scale into many arithmetic
4945 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004946
Chris Lattner37caf8c2007-04-09 23:33:39 +00004947 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00004948 if (Scale & 1) return false;
4949 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00004950 }
4951 break;
Evan Chengb01fad62007-03-12 23:30:29 +00004952 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00004953 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00004954}
4955
Evan Cheng77e47512009-11-11 19:05:52 +00004956/// isLegalICmpImmediate - Return true if the specified immediate is legal
4957/// icmp immediate, that is the target has icmp instructions which can compare
4958/// a register against the immediate without having to materialize the
4959/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00004960bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00004961 if (!Subtarget->isThumb())
4962 return ARM_AM::getSOImmVal(Imm) != -1;
4963 if (Subtarget->isThumb2())
4964 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00004965 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00004966}
4967
Owen Andersone50ed302009-08-10 22:56:29 +00004968static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004969 bool isSEXTLoad, SDValue &Base,
4970 SDValue &Offset, bool &isInc,
4971 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00004972 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4973 return false;
4974
Owen Anderson825b72b2009-08-11 20:47:22 +00004975 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00004976 // AddressingMode 3
4977 Base = Ptr->getOperand(0);
4978 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004979 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004980 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004981 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004982 isInc = false;
4983 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4984 return true;
4985 }
4986 }
4987 isInc = (Ptr->getOpcode() == ISD::ADD);
4988 Offset = Ptr->getOperand(1);
4989 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004990 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00004991 // AddressingMode 2
4992 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004993 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004994 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004995 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004996 isInc = false;
4997 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4998 Base = Ptr->getOperand(0);
4999 return true;
5000 }
5001 }
5002
5003 if (Ptr->getOpcode() == ISD::ADD) {
5004 isInc = true;
5005 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
5006 if (ShOpcVal != ARM_AM::no_shift) {
5007 Base = Ptr->getOperand(1);
5008 Offset = Ptr->getOperand(0);
5009 } else {
5010 Base = Ptr->getOperand(0);
5011 Offset = Ptr->getOperand(1);
5012 }
5013 return true;
5014 }
5015
5016 isInc = (Ptr->getOpcode() == ISD::ADD);
5017 Base = Ptr->getOperand(0);
5018 Offset = Ptr->getOperand(1);
5019 return true;
5020 }
5021
Jim Grosbache5165492009-11-09 00:11:35 +00005022 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00005023 return false;
5024}
5025
Owen Andersone50ed302009-08-10 22:56:29 +00005026static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00005027 bool isSEXTLoad, SDValue &Base,
5028 SDValue &Offset, bool &isInc,
5029 SelectionDAG &DAG) {
5030 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
5031 return false;
5032
5033 Base = Ptr->getOperand(0);
5034 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
5035 int RHSC = (int)RHS->getZExtValue();
5036 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
5037 assert(Ptr->getOpcode() == ISD::ADD);
5038 isInc = false;
5039 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5040 return true;
5041 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
5042 isInc = Ptr->getOpcode() == ISD::ADD;
5043 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
5044 return true;
5045 }
5046 }
5047
5048 return false;
5049}
5050
Evan Chenga8e29892007-01-19 07:51:42 +00005051/// getPreIndexedAddressParts - returns true by value, base pointer and
5052/// offset pointer and addressing mode by reference if the node's address
5053/// can be legally represented as pre-indexed load / store address.
5054bool
Dan Gohman475871a2008-07-27 21:46:04 +00005055ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
5056 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00005057 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00005058 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005059 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00005060 return false;
5061
Owen Andersone50ed302009-08-10 22:56:29 +00005062 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00005063 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00005064 bool isSEXTLoad = false;
5065 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5066 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005067 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00005068 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
5069 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
5070 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005071 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00005072 } else
5073 return false;
5074
5075 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00005076 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00005077 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00005078 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
5079 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00005080 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00005081 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00005082 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00005083 if (!isLegal)
5084 return false;
5085
5086 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
5087 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00005088}
5089
5090/// getPostIndexedAddressParts - returns true by value, base pointer and
5091/// offset pointer and addressing mode by reference if this node can be
5092/// combined with a load / store to form a post-indexed load / store.
5093bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00005094 SDValue &Base,
5095 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00005096 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00005097 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005098 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00005099 return false;
5100
Owen Andersone50ed302009-08-10 22:56:29 +00005101 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00005102 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00005103 bool isSEXTLoad = false;
5104 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005105 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00005106 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00005107 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
5108 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005109 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00005110 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00005111 } else
5112 return false;
5113
5114 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00005115 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00005116 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00005117 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00005118 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00005119 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00005120 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
5121 isInc, DAG);
5122 if (!isLegal)
5123 return false;
5124
Evan Cheng28dad2a2010-05-18 21:31:17 +00005125 if (Ptr != Base) {
5126 // Swap base ptr and offset to catch more post-index load / store when
5127 // it's legal. In Thumb2 mode, offset must be an immediate.
5128 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
5129 !Subtarget->isThumb2())
5130 std::swap(Base, Offset);
5131
5132 // Post-indexed load / store update the base pointer.
5133 if (Ptr != Base)
5134 return false;
5135 }
5136
Evan Chenge88d5ce2009-07-02 07:28:31 +00005137 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
5138 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00005139}
5140
Dan Gohman475871a2008-07-27 21:46:04 +00005141void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00005142 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00005143 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005144 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00005145 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00005146 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005147 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00005148 switch (Op.getOpcode()) {
5149 default: break;
5150 case ARMISD::CMOV: {
5151 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00005152 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00005153 if (KnownZero == 0 && KnownOne == 0) return;
5154
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005155 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00005156 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
5157 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00005158 KnownZero &= KnownZeroRHS;
5159 KnownOne &= KnownOneRHS;
5160 return;
5161 }
5162 }
5163}
5164
5165//===----------------------------------------------------------------------===//
5166// ARM Inline Assembly Support
5167//===----------------------------------------------------------------------===//
5168
5169/// getConstraintType - Given a constraint letter, return the type of
5170/// constraint it is for this target.
5171ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00005172ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
5173 if (Constraint.size() == 1) {
5174 switch (Constraint[0]) {
5175 default: break;
5176 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005177 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00005178 }
Evan Chenga8e29892007-01-19 07:51:42 +00005179 }
Chris Lattner4234f572007-03-25 02:14:49 +00005180 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00005181}
5182
Bob Wilson2dc4f542009-03-20 22:42:55 +00005183std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00005184ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00005185 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005186 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00005187 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00005188 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005189 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00005190 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00005191 return std::make_pair(0U, ARM::tGPRRegisterClass);
5192 else
5193 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005194 case 'r':
5195 return std::make_pair(0U, ARM::GPRRegisterClass);
5196 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00005197 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005198 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00005199 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005200 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00005201 if (VT.getSizeInBits() == 128)
5202 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005203 break;
Evan Chenga8e29892007-01-19 07:51:42 +00005204 }
5205 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00005206 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00005207 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00005208
Evan Chenga8e29892007-01-19 07:51:42 +00005209 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
5210}
5211
5212std::vector<unsigned> ARMTargetLowering::
5213getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00005214 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005215 if (Constraint.size() != 1)
5216 return std::vector<unsigned>();
5217
5218 switch (Constraint[0]) { // GCC ARM Constraint Letters
5219 default: break;
5220 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00005221 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
5222 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
5223 0);
Evan Chenga8e29892007-01-19 07:51:42 +00005224 case 'r':
5225 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
5226 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
5227 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
5228 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005229 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00005230 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005231 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
5232 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
5233 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
5234 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
5235 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
5236 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
5237 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
5238 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00005239 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005240 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
5241 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
5242 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
5243 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00005244 if (VT.getSizeInBits() == 128)
5245 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
5246 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005247 break;
Evan Chenga8e29892007-01-19 07:51:42 +00005248 }
5249
5250 return std::vector<unsigned>();
5251}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005252
5253/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
5254/// vector. If it is invalid, don't add anything to Ops.
5255void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
5256 char Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005257 std::vector<SDValue>&Ops,
5258 SelectionDAG &DAG) const {
5259 SDValue Result(0, 0);
5260
5261 switch (Constraint) {
5262 default: break;
5263 case 'I': case 'J': case 'K': case 'L':
5264 case 'M': case 'N': case 'O':
5265 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
5266 if (!C)
5267 return;
5268
5269 int64_t CVal64 = C->getSExtValue();
5270 int CVal = (int) CVal64;
5271 // None of these constraints allow values larger than 32 bits. Check
5272 // that the value fits in an int.
5273 if (CVal != CVal64)
5274 return;
5275
5276 switch (Constraint) {
5277 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005278 if (Subtarget->isThumb1Only()) {
5279 // This must be a constant between 0 and 255, for ADD
5280 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005281 if (CVal >= 0 && CVal <= 255)
5282 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005283 } else if (Subtarget->isThumb2()) {
5284 // A constant that can be used as an immediate value in a
5285 // data-processing instruction.
5286 if (ARM_AM::getT2SOImmVal(CVal) != -1)
5287 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005288 } else {
5289 // A constant that can be used as an immediate value in a
5290 // data-processing instruction.
5291 if (ARM_AM::getSOImmVal(CVal) != -1)
5292 break;
5293 }
5294 return;
5295
5296 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005297 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005298 // This must be a constant between -255 and -1, for negated ADD
5299 // immediates. This can be used in GCC with an "n" modifier that
5300 // prints the negated value, for use with SUB instructions. It is
5301 // not useful otherwise but is implemented for compatibility.
5302 if (CVal >= -255 && CVal <= -1)
5303 break;
5304 } else {
5305 // This must be a constant between -4095 and 4095. It is not clear
5306 // what this constraint is intended for. Implemented for
5307 // compatibility with GCC.
5308 if (CVal >= -4095 && CVal <= 4095)
5309 break;
5310 }
5311 return;
5312
5313 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005314 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005315 // A 32-bit value where only one byte has a nonzero value. Exclude
5316 // zero to match GCC. This constraint is used by GCC internally for
5317 // constants that can be loaded with a move/shift combination.
5318 // It is not useful otherwise but is implemented for compatibility.
5319 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
5320 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005321 } else if (Subtarget->isThumb2()) {
5322 // A constant whose bitwise inverse can be used as an immediate
5323 // value in a data-processing instruction. This can be used in GCC
5324 // with a "B" modifier that prints the inverted value, for use with
5325 // BIC and MVN instructions. It is not useful otherwise but is
5326 // implemented for compatibility.
5327 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
5328 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005329 } else {
5330 // A constant whose bitwise inverse can be used as an immediate
5331 // value in a data-processing instruction. This can be used in GCC
5332 // with a "B" modifier that prints the inverted value, for use with
5333 // BIC and MVN instructions. It is not useful otherwise but is
5334 // implemented for compatibility.
5335 if (ARM_AM::getSOImmVal(~CVal) != -1)
5336 break;
5337 }
5338 return;
5339
5340 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005341 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005342 // This must be a constant between -7 and 7,
5343 // for 3-operand ADD/SUB immediate instructions.
5344 if (CVal >= -7 && CVal < 7)
5345 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005346 } else if (Subtarget->isThumb2()) {
5347 // A constant whose negation can be used as an immediate value in a
5348 // data-processing instruction. This can be used in GCC with an "n"
5349 // modifier that prints the negated value, for use with SUB
5350 // instructions. It is not useful otherwise but is implemented for
5351 // compatibility.
5352 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
5353 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005354 } else {
5355 // A constant whose negation can be used as an immediate value in a
5356 // data-processing instruction. This can be used in GCC with an "n"
5357 // modifier that prints the negated value, for use with SUB
5358 // instructions. It is not useful otherwise but is implemented for
5359 // compatibility.
5360 if (ARM_AM::getSOImmVal(-CVal) != -1)
5361 break;
5362 }
5363 return;
5364
5365 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005366 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005367 // This must be a multiple of 4 between 0 and 1020, for
5368 // ADD sp + immediate.
5369 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
5370 break;
5371 } else {
5372 // A power of two or a constant between 0 and 32. This is used in
5373 // GCC for the shift amount on shifted register operands, but it is
5374 // useful in general for any shift amounts.
5375 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
5376 break;
5377 }
5378 return;
5379
5380 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005381 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005382 // This must be a constant between 0 and 31, for shift amounts.
5383 if (CVal >= 0 && CVal <= 31)
5384 break;
5385 }
5386 return;
5387
5388 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005389 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005390 // This must be a multiple of 4 between -508 and 508, for
5391 // ADD/SUB sp = sp + immediate.
5392 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
5393 break;
5394 }
5395 return;
5396 }
5397 Result = DAG.getTargetConstant(CVal, Op.getValueType());
5398 break;
5399 }
5400
5401 if (Result.getNode()) {
5402 Ops.push_back(Result);
5403 return;
5404 }
Dale Johannesen1784d162010-06-25 21:55:36 +00005405 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005406}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00005407
5408bool
5409ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
5410 // The ARM target isn't yet aware of offsets.
5411 return false;
5412}
Evan Cheng39382422009-10-28 01:44:26 +00005413
5414int ARM::getVFPf32Imm(const APFloat &FPImm) {
5415 APInt Imm = FPImm.bitcastToAPInt();
5416 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
5417 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
5418 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
5419
5420 // We can handle 4 bits of mantissa.
5421 // mantissa = (16+UInt(e:f:g:h))/16.
5422 if (Mantissa & 0x7ffff)
5423 return -1;
5424 Mantissa >>= 19;
5425 if ((Mantissa & 0xf) != Mantissa)
5426 return -1;
5427
5428 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
5429 if (Exp < -3 || Exp > 4)
5430 return -1;
5431 Exp = ((Exp+3) & 0x7) ^ 4;
5432
5433 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
5434}
5435
5436int ARM::getVFPf64Imm(const APFloat &FPImm) {
5437 APInt Imm = FPImm.bitcastToAPInt();
5438 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
5439 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
5440 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
5441
5442 // We can handle 4 bits of mantissa.
5443 // mantissa = (16+UInt(e:f:g:h))/16.
5444 if (Mantissa & 0xffffffffffffLL)
5445 return -1;
5446 Mantissa >>= 48;
5447 if ((Mantissa & 0xf) != Mantissa)
5448 return -1;
5449
5450 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
5451 if (Exp < -3 || Exp > 4)
5452 return -1;
5453 Exp = ((Exp+3) & 0x7) ^ 4;
5454
5455 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
5456}
5457
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005458bool ARM::isBitFieldInvertedMask(unsigned v) {
5459 if (v == 0xffffffff)
5460 return 0;
5461 // there can be 1's on either or both "outsides", all the "inside"
5462 // bits must be 0's
5463 unsigned int lsb = 0, msb = 31;
5464 while (v & (1 << msb)) --msb;
5465 while (v & (1 << lsb)) ++lsb;
5466 for (unsigned int i = lsb; i <= msb; ++i) {
5467 if (v & (1 << i))
5468 return 0;
5469 }
5470 return 1;
5471}
5472
Evan Cheng39382422009-10-28 01:44:26 +00005473/// isFPImmLegal - Returns true if the target can instruction select the
5474/// specified FP immediate natively. If false, the legalizer will
5475/// materialize the FP immediate as a load from a constant pool.
5476bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
5477 if (!Subtarget->hasVFP3())
5478 return false;
5479 if (VT == MVT::f32)
5480 return ARM::getVFPf32Imm(Imm) != -1;
5481 if (VT == MVT::f64)
5482 return ARM::getVFPf64Imm(Imm) != -1;
5483 return false;
5484}