blob: 07efb44acb2bbe4c75f8c7604f2e3a9025264b35 [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Dale Johannesen51e28e62010-06-03 21:09:53 +000015#define DEBUG_TYPE "arm-isel"
Evan Chenga8e29892007-01-19 07:51:42 +000016#include "ARM.h"
17#include "ARMAddressingModes.h"
18#include "ARMConstantPoolValue.h"
19#include "ARMISelLowering.h"
20#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000021#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000022#include "ARMRegisterInfo.h"
23#include "ARMSubtarget.h"
24#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000025#include "ARMTargetObjectFile.h"
Evan Chenga8e29892007-01-19 07:51:42 +000026#include "llvm/CallingConv.h"
27#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000028#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000029#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000030#include "llvm/Instruction.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000031#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000032#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000033#include "llvm/CodeGen/CallingConvLower.h"
Evan Chenga8e29892007-01-19 07:51:42 +000034#include "llvm/CodeGen/MachineBasicBlock.h"
35#include "llvm/CodeGen/MachineFrameInfo.h"
36#include "llvm/CodeGen/MachineFunction.h"
37#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000038#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000039#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000040#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000041#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000042#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000043#include "llvm/ADT/VectorExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000044#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000045#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000046#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000047#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000048#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000049#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000050using namespace llvm;
51
Dale Johannesen51e28e62010-06-03 21:09:53 +000052STATISTIC(NumTailCalls, "Number of tail calls");
53
54// This option should go away when tail calls fully work.
55static cl::opt<bool>
56EnableARMTailCalls("arm-tail-calls", cl::Hidden,
57 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
58 cl::init(false));
59
Jim Grosbache7b52522010-04-14 22:28:31 +000060static cl::opt<bool>
61EnableARMLongCalls("arm-long-calls", cl::Hidden,
62 cl::desc("Generate calls via indirect call instructions."),
63 cl::init(false));
64
Owen Andersone50ed302009-08-10 22:56:29 +000065static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000066 CCValAssign::LocInfo &LocInfo,
67 ISD::ArgFlagsTy &ArgFlags,
68 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000069static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000070 CCValAssign::LocInfo &LocInfo,
71 ISD::ArgFlagsTy &ArgFlags,
72 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000073static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000074 CCValAssign::LocInfo &LocInfo,
75 ISD::ArgFlagsTy &ArgFlags,
76 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000077static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000078 CCValAssign::LocInfo &LocInfo,
79 ISD::ArgFlagsTy &ArgFlags,
80 CCState &State);
81
Owen Andersone50ed302009-08-10 22:56:29 +000082void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
83 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000084 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000085 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000086 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
87 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000088
Owen Anderson70671842009-08-10 20:18:46 +000089 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000090 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000091 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000092 }
93
Owen Andersone50ed302009-08-10 22:56:29 +000094 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +000095 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +000096 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +000097 if (ElemTy == MVT::i8 || ElemTy == MVT::i16)
Owen Anderson70671842009-08-10 20:18:46 +000098 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +000099 if (ElemTy != MVT::i32) {
100 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
101 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
102 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
103 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
104 }
Owen Anderson70671842009-08-10 20:18:46 +0000105 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
106 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Evan Chengde8aa4e2010-05-05 18:28:36 +0000107 if (llvm::ModelWithRegSequence())
108 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
109 else
110 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Custom);
Anton Korobeynikov8e6c2b92009-08-21 12:40:35 +0000111 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
Bob Wilsond0910c42010-04-06 22:02:24 +0000112 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
113 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000114 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000115 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
116 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
117 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000118 }
119
120 // Promote all bit-wise operations.
121 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000122 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000123 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
124 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000125 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000126 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000127 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000128 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000129 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000130 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000131 }
Bob Wilson16330762009-09-16 00:17:28 +0000132
133 // Neon does not support vector divide/remainder operations.
134 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
135 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
136 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
137 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
138 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
139 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000140}
141
Owen Andersone50ed302009-08-10 22:56:29 +0000142void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000143 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000144 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000145}
146
Owen Andersone50ed302009-08-10 22:56:29 +0000147void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000148 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000149 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000150}
151
Chris Lattnerf0144122009-07-28 03:13:23 +0000152static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
153 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000154 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000155
Chris Lattner80ec2792009-08-02 00:34:36 +0000156 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000157}
158
Evan Chenga8e29892007-01-19 07:51:42 +0000159ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000160 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000161 Subtarget = &TM.getSubtarget<ARMSubtarget>();
162
Evan Chengb1df8f22007-04-27 08:15:43 +0000163 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000164 // Uses VFP for Thumb libfuncs if available.
165 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
166 // Single-precision floating-point arithmetic.
167 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
168 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
169 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
170 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000171
Evan Chengb1df8f22007-04-27 08:15:43 +0000172 // Double-precision floating-point arithmetic.
173 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
174 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
175 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
176 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000177
Evan Chengb1df8f22007-04-27 08:15:43 +0000178 // Single-precision comparisons.
179 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
180 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
181 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
182 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
183 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
184 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
185 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
186 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000187
Evan Chengb1df8f22007-04-27 08:15:43 +0000188 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
189 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
190 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
191 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
192 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
193 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
194 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
195 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000196
Evan Chengb1df8f22007-04-27 08:15:43 +0000197 // Double-precision comparisons.
198 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
199 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
200 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
201 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
202 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
203 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
204 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
205 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000206
Evan Chengb1df8f22007-04-27 08:15:43 +0000207 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
214 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000215
Evan Chengb1df8f22007-04-27 08:15:43 +0000216 // Floating-point to integer conversions.
217 // i64 conversions are done via library routines even when generating VFP
218 // instructions, so use the same ones.
219 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
220 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
221 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
222 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000223
Evan Chengb1df8f22007-04-27 08:15:43 +0000224 // Conversions between floating types.
225 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
226 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
227
228 // Integer to floating-point conversions.
229 // i64 conversions are done via library routines even when generating VFP
230 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000231 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
232 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000233 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
234 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
235 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
236 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
237 }
Evan Chenga8e29892007-01-19 07:51:42 +0000238 }
239
Bob Wilson2f954612009-05-22 17:38:41 +0000240 // These libcalls are not available in 32-bit.
241 setLibcallName(RTLIB::SHL_I128, 0);
242 setLibcallName(RTLIB::SRL_I128, 0);
243 setLibcallName(RTLIB::SRA_I128, 0);
244
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000245 // Libcalls should use the AAPCS base standard ABI, even if hard float
246 // is in effect, as per the ARM RTABI specification, section 4.1.2.
247 if (Subtarget->isAAPCS_ABI()) {
248 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
249 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
250 CallingConv::ARM_AAPCS);
251 }
252 }
253
David Goodwinf1daf7d2009-07-08 23:10:31 +0000254 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000255 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000256 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000258 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000259 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
260 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000261
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000263 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000264
265 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000266 addDRTypeForNEON(MVT::v2f32);
267 addDRTypeForNEON(MVT::v8i8);
268 addDRTypeForNEON(MVT::v4i16);
269 addDRTypeForNEON(MVT::v2i32);
270 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000271
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 addQRTypeForNEON(MVT::v4f32);
273 addQRTypeForNEON(MVT::v2f64);
274 addQRTypeForNEON(MVT::v16i8);
275 addQRTypeForNEON(MVT::v8i16);
276 addQRTypeForNEON(MVT::v4i32);
277 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000278
Bob Wilson74dc72e2009-09-15 23:55:57 +0000279 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
280 // neither Neon nor VFP support any arithmetic operations on it.
281 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
282 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
283 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
284 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
285 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
286 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
287 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
288 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
289 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
290 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
291 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
292 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
293 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
294 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
295 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
296 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
297 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
298 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
299 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
300 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
301 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
302 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
303 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
304 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
305
Bob Wilson642b3292009-09-16 00:32:15 +0000306 // Neon does not support some operations on v1i64 and v2i64 types.
307 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
308 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
309 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
310 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
311
Bob Wilson5bafff32009-06-22 23:27:02 +0000312 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
313 setTargetDAGCombine(ISD::SHL);
314 setTargetDAGCombine(ISD::SRL);
315 setTargetDAGCombine(ISD::SRA);
316 setTargetDAGCombine(ISD::SIGN_EXTEND);
317 setTargetDAGCombine(ISD::ZERO_EXTEND);
318 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000319 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson5bafff32009-06-22 23:27:02 +0000320 }
321
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000322 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000323
324 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000325 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000326
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000327 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000328 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000329
Evan Chenga8e29892007-01-19 07:51:42 +0000330 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000331 if (!Subtarget->isThumb1Only()) {
332 for (unsigned im = (unsigned)ISD::PRE_INC;
333 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000334 setIndexedLoadAction(im, MVT::i1, Legal);
335 setIndexedLoadAction(im, MVT::i8, Legal);
336 setIndexedLoadAction(im, MVT::i16, Legal);
337 setIndexedLoadAction(im, MVT::i32, Legal);
338 setIndexedStoreAction(im, MVT::i1, Legal);
339 setIndexedStoreAction(im, MVT::i8, Legal);
340 setIndexedStoreAction(im, MVT::i16, Legal);
341 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000342 }
Evan Chenga8e29892007-01-19 07:51:42 +0000343 }
344
345 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000346 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000347 setOperationAction(ISD::MUL, MVT::i64, Expand);
348 setOperationAction(ISD::MULHU, MVT::i32, Expand);
349 setOperationAction(ISD::MULHS, MVT::i32, Expand);
350 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
351 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000352 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000353 setOperationAction(ISD::MUL, MVT::i64, Expand);
354 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000355 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000356 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000357 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000358 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000359 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000360 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000361 setOperationAction(ISD::SRL, MVT::i64, Custom);
362 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000363
364 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000365 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000366 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000368 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000369 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000370
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000371 // Only ARMv6 has BSWAP.
372 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000373 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000374
Evan Chenga8e29892007-01-19 07:51:42 +0000375 // These are expanded into libcalls.
Jim Grosbach29402132010-05-05 23:44:43 +0000376 if (!Subtarget->hasDivide()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000377 // v7M has a hardware divider
378 setOperationAction(ISD::SDIV, MVT::i32, Expand);
379 setOperationAction(ISD::UDIV, MVT::i32, Expand);
380 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000381 setOperationAction(ISD::SREM, MVT::i32, Expand);
382 setOperationAction(ISD::UREM, MVT::i32, Expand);
383 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
384 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000385
Owen Anderson825b72b2009-08-11 20:47:22 +0000386 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
387 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
388 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
389 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000390 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000391
Evan Chengfb3611d2010-05-11 07:26:32 +0000392 setOperationAction(ISD::TRAP, MVT::Other, Legal);
393
Evan Chenga8e29892007-01-19 07:51:42 +0000394 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000395 setOperationAction(ISD::VASTART, MVT::Other, Custom);
396 setOperationAction(ISD::VAARG, MVT::Other, Expand);
397 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
398 setOperationAction(ISD::VAEND, MVT::Other, Expand);
399 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
400 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000401 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
402 // FIXME: Shouldn't need this, since no register is used, but the legalizer
403 // doesn't yet know how to not do that for SjLj.
404 setExceptionSelectorRegister(ARM::R0);
Evan Cheng3a1588a2010-04-15 22:20:34 +0000405 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Jim Grosbach3728e962009-12-10 00:11:09 +0000406 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000407
Jim Grosbach4b77f6a2010-05-07 18:34:55 +0000408 // If the subtarget does not have extract instructions, sign_extend_inreg
409 // needs to be expanded. Extract is available in ARM mode on v6 and up,
410 // and on most Thumb2 implementations.
411 if ((!Subtarget->isThumb() && !Subtarget->hasV6Ops())
412 || (Subtarget->isThumb2() && !Subtarget->hasT2ExtractPack())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000413 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
414 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000415 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000417
David Goodwinf1daf7d2009-07-08 23:10:31 +0000418 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000419 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
420 // iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000421 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000422
423 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000424 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000425
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 setOperationAction(ISD::SETCC, MVT::i32, Expand);
427 setOperationAction(ISD::SETCC, MVT::f32, Expand);
428 setOperationAction(ISD::SETCC, MVT::f64, Expand);
429 setOperationAction(ISD::SELECT, MVT::i32, Expand);
430 setOperationAction(ISD::SELECT, MVT::f32, Expand);
431 setOperationAction(ISD::SELECT, MVT::f64, Expand);
432 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
433 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
434 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000435
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
437 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
438 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
439 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
440 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000441
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000442 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setOperationAction(ISD::FSIN, MVT::f64, Expand);
444 setOperationAction(ISD::FSIN, MVT::f32, Expand);
445 setOperationAction(ISD::FCOS, MVT::f32, Expand);
446 setOperationAction(ISD::FCOS, MVT::f64, Expand);
447 setOperationAction(ISD::FREM, MVT::f64, Expand);
448 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000449 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000450 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
451 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000452 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000453 setOperationAction(ISD::FPOW, MVT::f64, Expand);
454 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000455
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000456 // Various VFP goodness
457 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000458 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
459 if (Subtarget->hasVFP2()) {
460 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
461 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
462 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
463 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
464 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000465 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000466 if (!Subtarget->hasFP16()) {
467 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
468 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000469 }
Evan Cheng110cf482008-04-01 01:50:16 +0000470 }
Evan Chenga8e29892007-01-19 07:51:42 +0000471
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000472 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000473 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000474 setTargetDAGCombine(ISD::ADD);
475 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000476 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000477
Evan Chenga8e29892007-01-19 07:51:42 +0000478 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000479
Evan Chengf7d87ee2010-05-21 00:43:17 +0000480 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
481 setSchedulingPreference(Sched::RegPressure);
482 else
483 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000484
Evan Chengbc9b7542009-08-15 07:59:10 +0000485 // FIXME: If-converter should use instruction latency to determine
486 // profitability rather than relying on fixed limits.
487 if (Subtarget->getCPUString() == "generic") {
488 // Generic (and overly aggressive) if-conversion limits.
489 setIfCvtBlockSizeLimit(10);
490 setIfCvtDupBlockSizeLimit(2);
Jim Grosbach35075a72010-03-24 16:15:14 +0000491 } else if (Subtarget->hasV7Ops()) {
Jim Grosbachfceabef2010-03-24 00:03:13 +0000492 setIfCvtBlockSizeLimit(3);
493 setIfCvtDupBlockSizeLimit(1);
Evan Chengbc9b7542009-08-15 07:59:10 +0000494 } else if (Subtarget->hasV6Ops()) {
495 setIfCvtBlockSizeLimit(2);
496 setIfCvtDupBlockSizeLimit(1);
497 } else {
498 setIfCvtBlockSizeLimit(3);
499 setIfCvtDupBlockSizeLimit(2);
Evan Cheng8557c2b2009-06-19 01:51:50 +0000500 }
501
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000502 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Bob Wilsone6abdff2009-05-18 20:55:32 +0000503 // Do not enable CodePlacementOpt for now: it currently runs after the
504 // ARMConstantIslandPass and messes up branch relaxation and placement
505 // of constant islands.
506 // benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000507}
508
Evan Chenga8e29892007-01-19 07:51:42 +0000509const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
510 switch (Opcode) {
511 default: return 0;
512 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000513 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
514 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000515 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000516 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
517 case ARMISD::tCALL: return "ARMISD::tCALL";
518 case ARMISD::BRCOND: return "ARMISD::BRCOND";
519 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000520 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000521 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
522 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
523 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000524 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000525 case ARMISD::CMPFP: return "ARMISD::CMPFP";
526 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
527 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
528 case ARMISD::CMOV: return "ARMISD::CMOV";
529 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000530
Jim Grosbach3482c802010-01-18 19:58:49 +0000531 case ARMISD::RBIT: return "ARMISD::RBIT";
532
Bob Wilson76a312b2010-03-19 22:51:32 +0000533 case ARMISD::FTOSI: return "ARMISD::FTOSI";
534 case ARMISD::FTOUI: return "ARMISD::FTOUI";
535 case ARMISD::SITOF: return "ARMISD::SITOF";
536 case ARMISD::UITOF: return "ARMISD::UITOF";
537
Evan Chenga8e29892007-01-19 07:51:42 +0000538 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
539 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
540 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000541
Jim Grosbache5165492009-11-09 00:11:35 +0000542 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
543 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000544
Evan Chengc5942082009-10-28 06:55:03 +0000545 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
546 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
547
Dale Johannesen51e28e62010-06-03 21:09:53 +0000548 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
549
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000550 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000551
Evan Cheng86198642009-08-07 00:34:42 +0000552 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
553
Jim Grosbach3728e962009-12-10 00:11:09 +0000554 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
555 case ARMISD::SYNCBARRIER: return "ARMISD::SYNCBARRIER";
556
Bob Wilson5bafff32009-06-22 23:27:02 +0000557 case ARMISD::VCEQ: return "ARMISD::VCEQ";
558 case ARMISD::VCGE: return "ARMISD::VCGE";
559 case ARMISD::VCGEU: return "ARMISD::VCGEU";
560 case ARMISD::VCGT: return "ARMISD::VCGT";
561 case ARMISD::VCGTU: return "ARMISD::VCGTU";
562 case ARMISD::VTST: return "ARMISD::VTST";
563
564 case ARMISD::VSHL: return "ARMISD::VSHL";
565 case ARMISD::VSHRs: return "ARMISD::VSHRs";
566 case ARMISD::VSHRu: return "ARMISD::VSHRu";
567 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
568 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
569 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
570 case ARMISD::VSHRN: return "ARMISD::VSHRN";
571 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
572 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
573 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
574 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
575 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
576 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
577 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
578 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
579 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
580 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
581 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
582 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
583 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
584 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000585 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000586 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000587 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000588 case ARMISD::VREV64: return "ARMISD::VREV64";
589 case ARMISD::VREV32: return "ARMISD::VREV32";
590 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000591 case ARMISD::VZIP: return "ARMISD::VZIP";
592 case ARMISD::VUZP: return "ARMISD::VUZP";
593 case ARMISD::VTRN: return "ARMISD::VTRN";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000594 case ARMISD::FMAX: return "ARMISD::FMAX";
595 case ARMISD::FMIN: return "ARMISD::FMIN";
Evan Chenga8e29892007-01-19 07:51:42 +0000596 }
597}
598
Evan Cheng06b666c2010-05-15 02:18:07 +0000599/// getRegClassFor - Return the register class that should be used for the
600/// specified value type.
601TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
602 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
603 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
604 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000605 if (Subtarget->hasNEON()) {
606 if (VT == MVT::v4i64)
607 return ARM::QQPRRegisterClass;
608 else if (VT == MVT::v8i64)
609 return ARM::QQQQPRRegisterClass;
610 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000611 return TargetLowering::getRegClassFor(VT);
612}
613
Bill Wendlingb4202b82009-07-01 18:50:55 +0000614/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000615unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Evan Cheng048e36f2009-10-02 06:57:25 +0000616 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 0 : 1;
Bill Wendling20c568f2009-06-30 22:38:32 +0000617}
618
Evan Cheng1cc39842010-05-20 23:26:43 +0000619Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000620 unsigned NumVals = N->getNumValues();
621 if (!NumVals)
622 return Sched::RegPressure;
623
624 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000625 EVT VT = N->getValueType(i);
626 if (VT.isFloatingPoint() || VT.isVector())
627 return Sched::Latency;
628 }
Evan Chengc10f5432010-05-28 23:25:23 +0000629
630 if (!N->isMachineOpcode())
631 return Sched::RegPressure;
632
633 // Load are scheduled for latency even if there instruction itinerary
634 // is not available.
635 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
636 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
637 if (TID.mayLoad())
638 return Sched::Latency;
639
640 const InstrItineraryData &Itins = getTargetMachine().getInstrItineraryData();
641 if (!Itins.isEmpty() && Itins.getStageLatency(TID.getSchedClass()) > 2)
642 return Sched::Latency;
Evan Cheng1cc39842010-05-20 23:26:43 +0000643 return Sched::RegPressure;
644}
645
Evan Chenga8e29892007-01-19 07:51:42 +0000646//===----------------------------------------------------------------------===//
647// Lowering Code
648//===----------------------------------------------------------------------===//
649
Evan Chenga8e29892007-01-19 07:51:42 +0000650/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
651static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
652 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000653 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000654 case ISD::SETNE: return ARMCC::NE;
655 case ISD::SETEQ: return ARMCC::EQ;
656 case ISD::SETGT: return ARMCC::GT;
657 case ISD::SETGE: return ARMCC::GE;
658 case ISD::SETLT: return ARMCC::LT;
659 case ISD::SETLE: return ARMCC::LE;
660 case ISD::SETUGT: return ARMCC::HI;
661 case ISD::SETUGE: return ARMCC::HS;
662 case ISD::SETULT: return ARMCC::LO;
663 case ISD::SETULE: return ARMCC::LS;
664 }
665}
666
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000667/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
668static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000669 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000670 CondCode2 = ARMCC::AL;
671 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000672 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000673 case ISD::SETEQ:
674 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
675 case ISD::SETGT:
676 case ISD::SETOGT: CondCode = ARMCC::GT; break;
677 case ISD::SETGE:
678 case ISD::SETOGE: CondCode = ARMCC::GE; break;
679 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000680 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000681 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
682 case ISD::SETO: CondCode = ARMCC::VC; break;
683 case ISD::SETUO: CondCode = ARMCC::VS; break;
684 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
685 case ISD::SETUGT: CondCode = ARMCC::HI; break;
686 case ISD::SETUGE: CondCode = ARMCC::PL; break;
687 case ISD::SETLT:
688 case ISD::SETULT: CondCode = ARMCC::LT; break;
689 case ISD::SETLE:
690 case ISD::SETULE: CondCode = ARMCC::LE; break;
691 case ISD::SETNE:
692 case ISD::SETUNE: CondCode = ARMCC::NE; break;
693 }
Evan Chenga8e29892007-01-19 07:51:42 +0000694}
695
Bob Wilson1f595bb2009-04-17 19:07:39 +0000696//===----------------------------------------------------------------------===//
697// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000698//===----------------------------------------------------------------------===//
699
700#include "ARMGenCallingConv.inc"
701
702// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000703static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000704 CCValAssign::LocInfo &LocInfo,
705 CCState &State, bool CanFail) {
706 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
707
708 // Try to get the first register.
709 if (unsigned Reg = State.AllocateReg(RegList, 4))
710 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
711 else {
712 // For the 2nd half of a v2f64, do not fail.
713 if (CanFail)
714 return false;
715
716 // Put the whole thing on the stack.
717 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
718 State.AllocateStack(8, 4),
719 LocVT, LocInfo));
720 return true;
721 }
722
723 // Try to get the second register.
724 if (unsigned Reg = State.AllocateReg(RegList, 4))
725 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
726 else
727 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
728 State.AllocateStack(4, 4),
729 LocVT, LocInfo));
730 return true;
731}
732
Owen Andersone50ed302009-08-10 22:56:29 +0000733static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000734 CCValAssign::LocInfo &LocInfo,
735 ISD::ArgFlagsTy &ArgFlags,
736 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000737 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
738 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000739 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000740 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
741 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000742 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000743}
744
745// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000746static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000747 CCValAssign::LocInfo &LocInfo,
748 CCState &State, bool CanFail) {
749 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
750 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
751
752 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
753 if (Reg == 0) {
754 // For the 2nd half of a v2f64, do not just fail.
755 if (CanFail)
756 return false;
757
758 // Put the whole thing on the stack.
759 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
760 State.AllocateStack(8, 8),
761 LocVT, LocInfo));
762 return true;
763 }
764
765 unsigned i;
766 for (i = 0; i < 2; ++i)
767 if (HiRegList[i] == Reg)
768 break;
769
770 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
771 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
772 LocVT, LocInfo));
773 return true;
774}
775
Owen Andersone50ed302009-08-10 22:56:29 +0000776static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000777 CCValAssign::LocInfo &LocInfo,
778 ISD::ArgFlagsTy &ArgFlags,
779 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000780 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
781 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000782 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000783 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
784 return false;
785 return true; // we handled it
786}
787
Owen Andersone50ed302009-08-10 22:56:29 +0000788static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000789 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000790 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
791 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
792
Bob Wilsone65586b2009-04-17 20:40:45 +0000793 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
794 if (Reg == 0)
795 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000796
Bob Wilsone65586b2009-04-17 20:40:45 +0000797 unsigned i;
798 for (i = 0; i < 2; ++i)
799 if (HiRegList[i] == Reg)
800 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000801
Bob Wilson5bafff32009-06-22 23:27:02 +0000802 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000803 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000804 LocVT, LocInfo));
805 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000806}
807
Owen Andersone50ed302009-08-10 22:56:29 +0000808static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000809 CCValAssign::LocInfo &LocInfo,
810 ISD::ArgFlagsTy &ArgFlags,
811 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000812 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
813 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000814 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000815 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000816 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000817}
818
Owen Andersone50ed302009-08-10 22:56:29 +0000819static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000820 CCValAssign::LocInfo &LocInfo,
821 ISD::ArgFlagsTy &ArgFlags,
822 CCState &State) {
823 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
824 State);
825}
826
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000827/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
828/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000829CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000830 bool Return,
831 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000832 switch (CC) {
833 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000834 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000835 case CallingConv::C:
836 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000837 // Use target triple & subtarget features to do actual dispatch.
838 if (Subtarget->isAAPCS_ABI()) {
839 if (Subtarget->hasVFP2() &&
840 FloatABIType == FloatABI::Hard && !isVarArg)
841 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
842 else
843 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
844 } else
845 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000846 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000847 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000848 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000849 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000850 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000851 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000852 }
853}
854
Dan Gohman98ca4f22009-08-05 01:29:28 +0000855/// LowerCallResult - Lower the result values of a call into the
856/// appropriate copies out of appropriate physical registers.
857SDValue
858ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000859 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000860 const SmallVectorImpl<ISD::InputArg> &Ins,
861 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000862 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000863
Bob Wilson1f595bb2009-04-17 19:07:39 +0000864 // Assign locations to each value returned by this call.
865 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000866 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000867 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000868 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000869 CCAssignFnForNode(CallConv, /* Return*/ true,
870 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000871
872 // Copy all of the result registers out of their specified physreg.
873 for (unsigned i = 0; i != RVLocs.size(); ++i) {
874 CCValAssign VA = RVLocs[i];
875
Bob Wilson80915242009-04-25 00:33:20 +0000876 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000877 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000878 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000879 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000880 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000881 Chain = Lo.getValue(1);
882 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000883 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000884 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000885 InFlag);
886 Chain = Hi.getValue(1);
887 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000888 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000889
Owen Anderson825b72b2009-08-11 20:47:22 +0000890 if (VA.getLocVT() == MVT::v2f64) {
891 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
892 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
893 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000894
895 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000896 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000897 Chain = Lo.getValue(1);
898 InFlag = Lo.getValue(2);
899 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000900 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000901 Chain = Hi.getValue(1);
902 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000903 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +0000904 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
905 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000906 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000907 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000908 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
909 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000910 Chain = Val.getValue(1);
911 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000912 }
Bob Wilson80915242009-04-25 00:33:20 +0000913
914 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000915 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000916 case CCValAssign::Full: break;
917 case CCValAssign::BCvt:
918 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
919 break;
920 }
921
Dan Gohman98ca4f22009-08-05 01:29:28 +0000922 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000923 }
924
Dan Gohman98ca4f22009-08-05 01:29:28 +0000925 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000926}
927
928/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
929/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000930/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000931/// a byval function parameter.
932/// Sometimes what we are copying is the end of a larger object, the part that
933/// does not fit in registers.
934static SDValue
935CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
936 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
937 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000938 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000939 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +0000940 /*isVolatile=*/false, /*AlwaysInline=*/false,
941 NULL, 0, NULL, 0);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000942}
943
Bob Wilsondee46d72009-04-17 20:35:10 +0000944/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000945SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000946ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
947 SDValue StackPtr, SDValue Arg,
948 DebugLoc dl, SelectionDAG &DAG,
949 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +0000950 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000951 unsigned LocMemOffset = VA.getLocMemOffset();
952 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
953 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
954 if (Flags.isByVal()) {
955 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
956 }
957 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene1b58cab2010-02-15 16:55:24 +0000958 PseudoSourceValue::getStack(), LocMemOffset,
959 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +0000960}
961
Dan Gohman98ca4f22009-08-05 01:29:28 +0000962void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000963 SDValue Chain, SDValue &Arg,
964 RegsToPassVector &RegsToPass,
965 CCValAssign &VA, CCValAssign &NextVA,
966 SDValue &StackPtr,
967 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +0000968 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +0000969
Jim Grosbache5165492009-11-09 00:11:35 +0000970 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000971 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000972 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
973
974 if (NextVA.isRegLoc())
975 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
976 else {
977 assert(NextVA.isMemLoc());
978 if (StackPtr.getNode() == 0)
979 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
980
Dan Gohman98ca4f22009-08-05 01:29:28 +0000981 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
982 dl, DAG, NextVA,
983 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000984 }
985}
986
Dan Gohman98ca4f22009-08-05 01:29:28 +0000987/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000988/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
989/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000990SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000991ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000992 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000993 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000994 const SmallVectorImpl<ISD::OutputArg> &Outs,
995 const SmallVectorImpl<ISD::InputArg> &Ins,
996 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000997 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +0000998 MachineFunction &MF = DAG.getMachineFunction();
999 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1000 bool IsSibCall = false;
1001 if (isTailCall) {
1002 // Check if it's really possible to do a tail call.
1003 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1004 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
1005 Outs, Ins, DAG);
1006 // Temporarily disable tail calls so things don't break.
1007 if (!EnableARMTailCalls)
1008 isTailCall = false;
1009 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1010 // detected sibcalls.
1011 if (isTailCall) {
1012 ++NumTailCalls;
1013 IsSibCall = true;
1014 }
1015 }
Evan Chenga8e29892007-01-19 07:51:42 +00001016
Bob Wilson1f595bb2009-04-17 19:07:39 +00001017 // Analyze operands of the call, assigning locations to each operand.
1018 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001019 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1020 *DAG.getContext());
1021 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001022 CCAssignFnForNode(CallConv, /* Return*/ false,
1023 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001024
Bob Wilson1f595bb2009-04-17 19:07:39 +00001025 // Get a count of how many bytes are to be pushed on the stack.
1026 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001027
Dale Johannesen51e28e62010-06-03 21:09:53 +00001028 // For tail calls, memory operands are available in our caller's stack.
1029 if (IsSibCall)
1030 NumBytes = 0;
1031
Evan Chenga8e29892007-01-19 07:51:42 +00001032 // Adjust the stack pointer for the new arguments...
1033 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001034 if (!IsSibCall)
1035 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001036
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001037 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001038
Bob Wilson5bafff32009-06-22 23:27:02 +00001039 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001040 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001041
Bob Wilson1f595bb2009-04-17 19:07:39 +00001042 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001043 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001044 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1045 i != e;
1046 ++i, ++realArgIdx) {
1047 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001048 SDValue Arg = Outs[realArgIdx].Val;
1049 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +00001050
Bob Wilson1f595bb2009-04-17 19:07:39 +00001051 // Promote the value if needed.
1052 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001053 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001054 case CCValAssign::Full: break;
1055 case CCValAssign::SExt:
1056 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1057 break;
1058 case CCValAssign::ZExt:
1059 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1060 break;
1061 case CCValAssign::AExt:
1062 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1063 break;
1064 case CCValAssign::BCvt:
1065 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1066 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001067 }
1068
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001069 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001070 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001071 if (VA.getLocVT() == MVT::v2f64) {
1072 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1073 DAG.getConstant(0, MVT::i32));
1074 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1075 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001076
Dan Gohman98ca4f22009-08-05 01:29:28 +00001077 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001078 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1079
1080 VA = ArgLocs[++i]; // skip ahead to next loc
1081 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001082 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001083 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1084 } else {
1085 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001086
Dan Gohman98ca4f22009-08-05 01:29:28 +00001087 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1088 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001089 }
1090 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001091 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001092 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001093 }
1094 } else if (VA.isRegLoc()) {
1095 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1096 } else {
1097 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001098
Dan Gohman98ca4f22009-08-05 01:29:28 +00001099 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1100 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001101 }
Evan Chenga8e29892007-01-19 07:51:42 +00001102 }
1103
1104 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001105 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001106 &MemOpChains[0], MemOpChains.size());
1107
1108 // Build a sequence of copy-to-reg nodes chained together with token chain
1109 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001110 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +00001111 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001112 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001113 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +00001114 InFlag = Chain.getValue(1);
1115 }
1116
Dale Johannesen51e28e62010-06-03 21:09:53 +00001117 // For tail calls lower the arguments to the 'real' stack slot.
1118 if (isTailCall) {
1119 // Force all the incoming stack arguments to be loaded from the stack
1120 // before any new outgoing arguments are stored to the stack, because the
1121 // outgoing stack slots may alias the incoming argument stack slots, and
1122 // the alias isn't otherwise explicit. This is slightly more conservative
1123 // than necessary, because it means that each store effectively depends
1124 // on every argument instead of just those arguments it would clobber.
1125
1126 // Do not flag preceeding copytoreg stuff together with the following stuff.
1127 InFlag = SDValue();
1128 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1129 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1130 RegsToPass[i].second, InFlag);
1131 InFlag = Chain.getValue(1);
1132 }
1133 InFlag =SDValue();
1134 }
1135
Bill Wendling056292f2008-09-16 21:48:12 +00001136 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1137 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1138 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001139 bool isDirect = false;
1140 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001141 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001142 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001143
1144 if (EnableARMLongCalls) {
1145 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1146 && "long-calls with non-static relocation model!");
1147 // Handle a global address or an external symbol. If it's not one of
1148 // those, the target's already in a register, so we don't need to do
1149 // anything extra.
1150 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001151 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001152 // Create a constant pool entry for the callee address
1153 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1154 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1155 ARMPCLabelIndex,
1156 ARMCP::CPValue, 0);
1157 // Get the address of the callee into a register
1158 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1159 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1160 Callee = DAG.getLoad(getPointerTy(), dl,
1161 DAG.getEntryNode(), CPAddr,
1162 PseudoSourceValue::getConstantPool(), 0,
1163 false, false, 0);
1164 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1165 const char *Sym = S->getSymbol();
1166
1167 // Create a constant pool entry for the callee address
1168 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1169 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1170 Sym, ARMPCLabelIndex, 0);
1171 // Get the address of the callee into a register
1172 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1173 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1174 Callee = DAG.getLoad(getPointerTy(), dl,
1175 DAG.getEntryNode(), CPAddr,
1176 PseudoSourceValue::getConstantPool(), 0,
1177 false, false, 0);
1178 }
1179 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001180 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001181 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001182 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001183 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001184 getTargetMachine().getRelocationModel() != Reloc::Static;
1185 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001186 // ARM call to a local ARM function is predicable.
1187 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +00001188 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001189 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001190 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001191 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001192 ARMPCLabelIndex,
1193 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001194 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001195 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001196 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001197 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001198 PseudoSourceValue::getConstantPool(), 0,
1199 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001200 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001201 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001202 getPointerTy(), Callee, PICLabel);
Jim Grosbache7b52522010-04-14 22:28:31 +00001203 } else
Evan Chengc60e76d2007-01-30 20:37:08 +00001204 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001205 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001206 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001207 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001208 getTargetMachine().getRelocationModel() != Reloc::Static;
1209 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001210 // tBX takes a register source operand.
1211 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001212 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001213 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001214 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001215 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001216 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001217 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001218 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001219 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001220 PseudoSourceValue::getConstantPool(), 0,
1221 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001222 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001223 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001224 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001225 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001226 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001227 }
1228
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001229 // FIXME: handle tail calls differently.
1230 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001231 if (Subtarget->isThumb()) {
1232 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001233 CallOpc = ARMISD::CALL_NOLINK;
1234 else
1235 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1236 } else {
1237 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001238 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1239 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001240 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001241 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001242 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001243 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001244 InFlag = Chain.getValue(1);
1245 }
1246
Dan Gohman475871a2008-07-27 21:46:04 +00001247 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001248 Ops.push_back(Chain);
1249 Ops.push_back(Callee);
1250
1251 // Add argument registers to the end of the list so that they are known live
1252 // into the call.
1253 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1254 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1255 RegsToPass[i].second.getValueType()));
1256
Gabor Greifba36cb52008-08-28 21:40:38 +00001257 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001258 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001259
1260 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1261 if (isTailCall) {
1262 // If this is the first return lowered for this function, add the regs
1263 // to the liveout set for the function.
1264 if (MF.getRegInfo().liveout_empty()) {
1265 SmallVector<CCValAssign, 16> RVLocs;
1266 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1267 *DAG.getContext());
1268 CCInfo.AnalyzeCallResult(Ins,
1269 CCAssignFnForNode(CallConv, /* Return*/ true,
1270 isVarArg));
1271 for (unsigned i = 0; i != RVLocs.size(); ++i)
1272 if (RVLocs[i].isRegLoc())
1273 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
1274 }
1275 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
1276 }
1277
Duncan Sands4bdcb612008-07-02 17:40:58 +00001278 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001279 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001280 InFlag = Chain.getValue(1);
1281
Chris Lattnere563bbc2008-10-11 22:08:30 +00001282 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1283 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001284 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001285 InFlag = Chain.getValue(1);
1286
Bob Wilson1f595bb2009-04-17 19:07:39 +00001287 // Handle result values, copying them out of physregs into vregs that we
1288 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001289 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1290 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001291}
1292
Dale Johannesen51e28e62010-06-03 21:09:53 +00001293/// MatchingStackOffset - Return true if the given stack call argument is
1294/// already available in the same position (relatively) of the caller's
1295/// incoming argument stack.
1296static
1297bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1298 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1299 const ARMInstrInfo *TII) {
1300 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1301 int FI = INT_MAX;
1302 if (Arg.getOpcode() == ISD::CopyFromReg) {
1303 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
1304 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
1305 return false;
1306 MachineInstr *Def = MRI->getVRegDef(VR);
1307 if (!Def)
1308 return false;
1309 if (!Flags.isByVal()) {
1310 if (!TII->isLoadFromStackSlot(Def, FI))
1311 return false;
1312 } else {
1313// unsigned Opcode = Def->getOpcode();
1314// if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
1315// Def->getOperand(1).isFI()) {
1316// FI = Def->getOperand(1).getIndex();
1317// Bytes = Flags.getByValSize();
1318// } else
1319 return false;
1320 }
1321 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1322 if (Flags.isByVal())
1323 // ByVal argument is passed in as a pointer but it's now being
1324 // dereferenced. e.g.
1325 // define @foo(%struct.X* %A) {
1326 // tail call @bar(%struct.X* byval %A)
1327 // }
1328 return false;
1329 SDValue Ptr = Ld->getBasePtr();
1330 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1331 if (!FINode)
1332 return false;
1333 FI = FINode->getIndex();
1334 } else
1335 return false;
1336
1337 assert(FI != INT_MAX);
1338 if (!MFI->isFixedObjectIndex(FI))
1339 return false;
1340 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1341}
1342
1343/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1344/// for tail call optimization. Targets which want to do tail call
1345/// optimization should implement this function.
1346bool
1347ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1348 CallingConv::ID CalleeCC,
1349 bool isVarArg,
1350 bool isCalleeStructRet,
1351 bool isCallerStructRet,
1352 const SmallVectorImpl<ISD::OutputArg> &Outs,
1353 const SmallVectorImpl<ISD::InputArg> &Ins,
1354 SelectionDAG& DAG) const {
1355
1356// const MachineFunction &MF = DAG.getMachineFunction();
1357 const Function *CallerF = DAG.getMachineFunction().getFunction();
1358 CallingConv::ID CallerCC = CallerF->getCallingConv();
1359 bool CCMatch = CallerCC == CalleeCC;
1360
1361 // Look for obvious safe cases to perform tail call optimization that do not
1362 // require ABI changes. This is what gcc calls sibcall.
1363
1364 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
1365 // emit a special epilogue.
1366 // Not sure yet if this is true on ARM.
1367//?? if (RegInfo->needsStackRealignment(MF))
1368//?? return false;
1369
1370 // Do not sibcall optimize vararg calls unless the call site is not passing any
1371 // arguments.
1372 if (isVarArg && !Outs.empty())
1373 return false;
1374
1375 // Also avoid sibcall optimization if either caller or callee uses struct
1376 // return semantics.
1377 if (isCalleeStructRet || isCallerStructRet)
1378 return false;
1379
1380 // If the calling conventions do not match, then we'd better make sure the
1381 // results are returned in the same way as what the caller expects.
1382 if (!CCMatch) {
1383 SmallVector<CCValAssign, 16> RVLocs1;
1384 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
1385 RVLocs1, *DAG.getContext());
1386 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1387
1388 SmallVector<CCValAssign, 16> RVLocs2;
1389 CCState CCInfo2(CallerCC, false, getTargetMachine(),
1390 RVLocs2, *DAG.getContext());
1391 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1392
1393 if (RVLocs1.size() != RVLocs2.size())
1394 return false;
1395 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1396 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1397 return false;
1398 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1399 return false;
1400 if (RVLocs1[i].isRegLoc()) {
1401 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1402 return false;
1403 } else {
1404 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1405 return false;
1406 }
1407 }
1408 }
1409
1410 // If the callee takes no arguments then go on to check the results of the
1411 // call.
1412 if (!Outs.empty()) {
1413 // Check if stack adjustment is needed. For now, do not do this if any
1414 // argument is passed on the stack.
1415 SmallVector<CCValAssign, 16> ArgLocs;
1416 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
1417 ArgLocs, *DAG.getContext());
1418 CCInfo.AnalyzeCallOperands(Outs,
1419 CCAssignFnForNode(CalleeCC, false, isVarArg));
1420 if (CCInfo.getNextStackOffset()) {
1421 MachineFunction &MF = DAG.getMachineFunction();
1422
1423 // Check if the arguments are already laid out in the right way as
1424 // the caller's fixed stack objects.
1425 MachineFrameInfo *MFI = MF.getFrameInfo();
1426 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1427 const ARMInstrInfo *TII =
1428 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
1429 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1430 CCValAssign &VA = ArgLocs[i];
1431 EVT RegVT = VA.getLocVT();
1432 SDValue Arg = Outs[i].Val;
1433 ISD::ArgFlagsTy Flags = Outs[i].Flags;
1434 if (VA.getLocInfo() == CCValAssign::Indirect)
1435 return false;
1436 if (!VA.isRegLoc()) {
1437 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1438 MFI, MRI, TII))
1439 return false;
1440 }
1441 }
1442 }
1443 }
1444
1445 return true;
1446}
1447
Dan Gohman98ca4f22009-08-05 01:29:28 +00001448SDValue
1449ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001450 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001451 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmand858e902010-04-17 15:26:15 +00001452 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001453
Bob Wilsondee46d72009-04-17 20:35:10 +00001454 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001455 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001456
Bob Wilsondee46d72009-04-17 20:35:10 +00001457 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001458 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1459 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001460
Dan Gohman98ca4f22009-08-05 01:29:28 +00001461 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001462 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1463 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001464
1465 // If this is the first return lowered for this function, add
1466 // the regs to the liveout set for the function.
1467 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1468 for (unsigned i = 0; i != RVLocs.size(); ++i)
1469 if (RVLocs[i].isRegLoc())
1470 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001471 }
1472
Bob Wilson1f595bb2009-04-17 19:07:39 +00001473 SDValue Flag;
1474
1475 // Copy the result values into the output registers.
1476 for (unsigned i = 0, realRVLocIdx = 0;
1477 i != RVLocs.size();
1478 ++i, ++realRVLocIdx) {
1479 CCValAssign &VA = RVLocs[i];
1480 assert(VA.isRegLoc() && "Can only return in registers!");
1481
Dan Gohman98ca4f22009-08-05 01:29:28 +00001482 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001483
1484 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001485 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001486 case CCValAssign::Full: break;
1487 case CCValAssign::BCvt:
1488 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1489 break;
1490 }
1491
Bob Wilson1f595bb2009-04-17 19:07:39 +00001492 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001493 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001494 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001495 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1496 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001497 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001498 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001499
1500 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1501 Flag = Chain.getValue(1);
1502 VA = RVLocs[++i]; // skip ahead to next loc
1503 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1504 HalfGPRs.getValue(1), Flag);
1505 Flag = Chain.getValue(1);
1506 VA = RVLocs[++i]; // skip ahead to next loc
1507
1508 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001509 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1510 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001511 }
1512 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1513 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001514 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001515 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001516 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001517 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001518 VA = RVLocs[++i]; // skip ahead to next loc
1519 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1520 Flag);
1521 } else
1522 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1523
Bob Wilsondee46d72009-04-17 20:35:10 +00001524 // Guarantee that all emitted copies are
1525 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001526 Flag = Chain.getValue(1);
1527 }
1528
1529 SDValue result;
1530 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001531 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001532 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001533 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001534
1535 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001536}
1537
Bob Wilsonb62d2572009-11-03 00:02:05 +00001538// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1539// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1540// one of the above mentioned nodes. It has to be wrapped because otherwise
1541// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1542// be used to form addressing mode. These wrapped nodes will be selected
1543// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001544static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001545 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001546 // FIXME there is no actual debug info here
1547 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001548 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001549 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001550 if (CP->isMachineConstantPoolEntry())
1551 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1552 CP->getAlignment());
1553 else
1554 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1555 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001556 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001557}
1558
Dan Gohmand858e902010-04-17 15:26:15 +00001559SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1560 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001561 MachineFunction &MF = DAG.getMachineFunction();
1562 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1563 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001564 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001565 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001566 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001567 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1568 SDValue CPAddr;
1569 if (RelocM == Reloc::Static) {
1570 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1571 } else {
1572 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001573 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001574 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1575 ARMCP::CPBlockAddress,
1576 PCAdj);
1577 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1578 }
1579 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1580 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001581 PseudoSourceValue::getConstantPool(), 0,
1582 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001583 if (RelocM == Reloc::Static)
1584 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001585 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001586 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001587}
1588
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001589// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001590SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001591ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001592 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001593 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001594 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001595 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001596 MachineFunction &MF = DAG.getMachineFunction();
1597 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1598 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001599 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001600 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001601 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001602 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001603 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001604 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
David Greene1b58cab2010-02-15 16:55:24 +00001605 PseudoSourceValue::getConstantPool(), 0,
1606 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001607 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001608
Evan Chenge7e0d622009-11-06 22:24:13 +00001609 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001610 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001611
1612 // call __tls_get_addr.
1613 ArgListTy Args;
1614 ArgListEntry Entry;
1615 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001616 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001617 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001618 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001619 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001620 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1621 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001622 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001623 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001624 return CallResult.first;
1625}
1626
1627// Lower ISD::GlobalTLSAddress using the "initial exec" or
1628// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001629SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001630ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001631 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001632 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001633 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001634 SDValue Offset;
1635 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001636 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001637 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001638 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001639
Chris Lattner4fb63d02009-07-15 04:12:33 +00001640 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001641 MachineFunction &MF = DAG.getMachineFunction();
1642 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1643 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1644 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001645 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1646 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001647 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001648 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001649 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001650 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001651 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001652 PseudoSourceValue::getConstantPool(), 0,
1653 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001654 Chain = Offset.getValue(1);
1655
Evan Chenge7e0d622009-11-06 22:24:13 +00001656 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001657 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001658
Evan Cheng9eda6892009-10-31 03:39:36 +00001659 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001660 PseudoSourceValue::getConstantPool(), 0,
1661 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001662 } else {
1663 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001664 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001665 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001666 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001667 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001668 PseudoSourceValue::getConstantPool(), 0,
1669 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001670 }
1671
1672 // The address of the thread local variable is the add of the thread
1673 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001674 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001675}
1676
Dan Gohman475871a2008-07-27 21:46:04 +00001677SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001678ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001679 // TODO: implement the "local dynamic" model
1680 assert(Subtarget->isTargetELF() &&
1681 "TLS not implemented for non-ELF targets");
1682 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1683 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1684 // otherwise use the "Local Exec" TLS Model
1685 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1686 return LowerToTLSGeneralDynamicModel(GA, DAG);
1687 else
1688 return LowerToTLSExecModels(GA, DAG);
1689}
1690
Dan Gohman475871a2008-07-27 21:46:04 +00001691SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001692 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001693 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001694 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001695 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001696 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1697 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001698 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001699 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001700 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001701 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001702 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001703 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001704 CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001705 PseudoSourceValue::getConstantPool(), 0,
1706 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001707 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001708 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001709 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001710 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001711 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001712 PseudoSourceValue::getGOT(), 0,
1713 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001714 return Result;
1715 } else {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001716 // If we have T2 ops, we can materialize the address directly via movt/movw
1717 // pair. This is always cheaper.
1718 if (Subtarget->useMovt()) {
1719 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
1720 DAG.getTargetGlobalAddress(GV, PtrVT));
1721 } else {
1722 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1723 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1724 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001725 PseudoSourceValue::getConstantPool(), 0,
1726 false, false, 0);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001727 }
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001728 }
1729}
1730
Dan Gohman475871a2008-07-27 21:46:04 +00001731SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001732 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001733 MachineFunction &MF = DAG.getMachineFunction();
1734 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1735 unsigned ARMPCLabelIndex = 0;
Owen Andersone50ed302009-08-10 22:56:29 +00001736 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001737 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001738 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001739 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001740 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001741 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001742 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001743 else {
Evan Chenge7e0d622009-11-06 22:24:13 +00001744 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001745 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1746 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001747 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001748 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001749 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001750 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001751
Evan Cheng9eda6892009-10-31 03:39:36 +00001752 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001753 PseudoSourceValue::getConstantPool(), 0,
1754 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001755 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001756
1757 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001758 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001759 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001760 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001761
Evan Cheng63476a82009-09-03 07:04:02 +00001762 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Evan Cheng9eda6892009-10-31 03:39:36 +00001763 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001764 PseudoSourceValue::getGOT(), 0,
1765 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001766
1767 return Result;
1768}
1769
Dan Gohman475871a2008-07-27 21:46:04 +00001770SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001771 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001772 assert(Subtarget->isTargetELF() &&
1773 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00001774 MachineFunction &MF = DAG.getMachineFunction();
1775 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1776 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Andersone50ed302009-08-10 22:56:29 +00001777 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001778 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001779 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001780 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1781 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001782 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001783 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001784 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001785 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001786 PseudoSourceValue::getConstantPool(), 0,
1787 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001788 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001789 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001790}
1791
Jim Grosbach0e0da732009-05-12 23:59:14 +00001792SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00001793ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
1794 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00001795 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00001796 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
1797 Op.getOperand(1), Val);
1798}
1799
1800SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00001801ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
1802 DebugLoc dl = Op.getDebugLoc();
1803 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
1804 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
1805}
1806
1807SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00001808ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001809 const ARMSubtarget *Subtarget)
1810 const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001811 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001812 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001813 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001814 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001815 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001816 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001817 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1818 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001819 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001820 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00001821 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1822 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001823 EVT PtrVT = getPointerTy();
1824 DebugLoc dl = Op.getDebugLoc();
1825 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1826 SDValue CPAddr;
1827 unsigned PCAdj = (RelocM != Reloc::PIC_)
1828 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001829 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001830 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1831 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001832 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001833 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001834 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00001835 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001836 PseudoSourceValue::getConstantPool(), 0,
1837 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001838 SDValue Chain = Result.getValue(1);
1839
1840 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001841 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001842 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1843 }
1844 return Result;
1845 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001846 }
1847}
1848
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001849static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
1850 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00001851 DebugLoc dl = Op.getDebugLoc();
1852 SDValue Op5 = Op.getOperand(5);
1853 SDValue Res;
1854 unsigned isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue();
1855 if (isDeviceBarrier) {
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001856 if (Subtarget->hasV7Ops())
1857 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0));
1858 else
1859 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0),
1860 DAG.getConstant(0, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00001861 } else {
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001862 if (Subtarget->hasV7Ops())
1863 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
1864 else
1865 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
1866 DAG.getConstant(0, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00001867 }
1868 return Res;
1869}
1870
Dan Gohman1e93df62010-04-17 14:41:14 +00001871static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
1872 MachineFunction &MF = DAG.getMachineFunction();
1873 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
1874
Evan Chenga8e29892007-01-19 07:51:42 +00001875 // vastart just stores the address of the VarArgsFrameIndex slot into the
1876 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001877 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001878 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00001879 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001880 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
David Greene1b58cab2010-02-15 16:55:24 +00001881 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
1882 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001883}
1884
Dan Gohman475871a2008-07-27 21:46:04 +00001885SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001886ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1887 SelectionDAG &DAG) const {
Evan Cheng86198642009-08-07 00:34:42 +00001888 SDNode *Node = Op.getNode();
1889 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001890 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001891 SDValue Chain = Op.getOperand(0);
1892 SDValue Size = Op.getOperand(1);
1893 SDValue Align = Op.getOperand(2);
1894
1895 // Chain the dynamic stack allocation so that it doesn't modify the stack
1896 // pointer when other instructions are using the stack.
1897 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1898
1899 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1900 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1901 if (AlignVal > StackAlign)
1902 // Do this now since selection pass cannot introduce new target
1903 // independent node.
1904 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1905
1906 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1907 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1908 // do even more horrible hack later.
1909 MachineFunction &MF = DAG.getMachineFunction();
1910 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1911 if (AFI->isThumb1OnlyFunction()) {
1912 bool Negate = true;
1913 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1914 if (C) {
1915 uint32_t Val = C->getZExtValue();
1916 if (Val <= 508 && ((Val & 3) == 0))
1917 Negate = false;
1918 }
1919 if (Negate)
1920 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1921 }
1922
Owen Anderson825b72b2009-08-11 20:47:22 +00001923 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001924 SDValue Ops1[] = { Chain, Size, Align };
1925 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1926 Chain = Res.getValue(1);
1927 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1928 DAG.getIntPtrConstant(0, true), SDValue());
1929 SDValue Ops2[] = { Res, Chain };
1930 return DAG.getMergeValues(Ops2, 2, dl);
1931}
1932
1933SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001934ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1935 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001936 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001937 MachineFunction &MF = DAG.getMachineFunction();
1938 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1939
1940 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001941 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001942 RC = ARM::tGPRRegisterClass;
1943 else
1944 RC = ARM::GPRRegisterClass;
1945
1946 // Transform the arguments stored in physical registers into virtual ones.
Evan Cheng2457f2c2010-05-22 01:47:14 +00001947 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001948 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001949
1950 SDValue ArgValue2;
1951 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001952 MachineFrameInfo *MFI = MF.getFrameInfo();
Bob Wilson6a234f02010-04-13 22:03:22 +00001953 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true, false);
Bob Wilson5bafff32009-06-22 23:27:02 +00001954
1955 // Create load node to retrieve arguments from the stack.
1956 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001957 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00001958 PseudoSourceValue::getFixedStack(FI), 0,
1959 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001960 } else {
1961 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001962 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001963 }
1964
Jim Grosbache5165492009-11-09 00:11:35 +00001965 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001966}
1967
1968SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001969ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001970 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001971 const SmallVectorImpl<ISD::InputArg>
1972 &Ins,
1973 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001974 SmallVectorImpl<SDValue> &InVals)
1975 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001976
Bob Wilson1f595bb2009-04-17 19:07:39 +00001977 MachineFunction &MF = DAG.getMachineFunction();
1978 MachineFrameInfo *MFI = MF.getFrameInfo();
1979
Bob Wilson1f595bb2009-04-17 19:07:39 +00001980 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1981
1982 // Assign locations to all of the incoming arguments.
1983 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001984 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1985 *DAG.getContext());
1986 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001987 CCAssignFnForNode(CallConv, /* Return*/ false,
1988 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001989
1990 SmallVector<SDValue, 16> ArgValues;
1991
1992 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1993 CCValAssign &VA = ArgLocs[i];
1994
Bob Wilsondee46d72009-04-17 20:35:10 +00001995 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001996 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001997 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00001998
Bob Wilson5bafff32009-06-22 23:27:02 +00001999 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002000 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002001 // f64 and vector types are split up into multiple registers or
2002 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002003 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002004 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002005 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002006 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002007 SDValue ArgValue2;
2008 if (VA.isMemLoc()) {
2009 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(),
2010 true, false);
2011 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2012 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
2013 PseudoSourceValue::getFixedStack(FI), 0,
2014 false, false, 0);
2015 } else {
2016 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2017 Chain, DAG, dl);
2018 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002019 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2020 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002021 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002022 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002023 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2024 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002025 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002026
Bob Wilson5bafff32009-06-22 23:27:02 +00002027 } else {
2028 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002029
Owen Anderson825b72b2009-08-11 20:47:22 +00002030 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002031 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002032 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002033 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002034 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002035 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002036 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002037 RC = (AFI->isThumb1OnlyFunction() ?
2038 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002039 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002040 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002041
2042 // Transform the arguments in physical registers into virtual ones.
2043 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002044 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002045 }
2046
2047 // If this is an 8 or 16-bit value, it is really passed promoted
2048 // to 32 bits. Insert an assert[sz]ext to capture this, then
2049 // truncate to the right size.
2050 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002051 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002052 case CCValAssign::Full: break;
2053 case CCValAssign::BCvt:
2054 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
2055 break;
2056 case CCValAssign::SExt:
2057 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2058 DAG.getValueType(VA.getValVT()));
2059 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2060 break;
2061 case CCValAssign::ZExt:
2062 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2063 DAG.getValueType(VA.getValVT()));
2064 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2065 break;
2066 }
2067
Dan Gohman98ca4f22009-08-05 01:29:28 +00002068 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002069
2070 } else { // VA.isRegLoc()
2071
2072 // sanity check
2073 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002074 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002075
2076 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00002077 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
2078 true, false);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002079
Bob Wilsondee46d72009-04-17 20:35:10 +00002080 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002081 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002082 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00002083 PseudoSourceValue::getFixedStack(FI), 0,
2084 false, false, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002085 }
2086 }
2087
2088 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00002089 if (isVarArg) {
2090 static const unsigned GPRArgRegs[] = {
2091 ARM::R0, ARM::R1, ARM::R2, ARM::R3
2092 };
2093
Bob Wilsondee46d72009-04-17 20:35:10 +00002094 unsigned NumGPRs = CCInfo.getFirstUnallocated
2095 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002096
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00002097 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
2098 unsigned VARegSize = (4 - NumGPRs) * 4;
2099 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00002100 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00002101 if (VARegSaveSize) {
2102 // If this function is vararg, store any remaining integer argument regs
2103 // to their spots on the stack so that they may be loaded by deferencing
2104 // the result of va_next.
2105 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00002106 AFI->setVarArgsFrameIndex(
2107 MFI->CreateFixedObject(VARegSaveSize,
2108 ArgOffset + VARegSaveSize - VARegSize,
2109 true, false));
2110 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2111 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00002112
Dan Gohman475871a2008-07-27 21:46:04 +00002113 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00002114 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002115 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002116 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00002117 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002118 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00002119 RC = ARM::GPRRegisterClass;
2120
Bob Wilson998e1252009-04-20 18:36:57 +00002121 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002122 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00002123 SDValue Store =
2124 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Jim Grosbach18f30e62010-06-02 21:53:11 +00002125 PseudoSourceValue::getFixedStack(AFI->getVarArgsFrameIndex()),
2126 0, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002127 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002128 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00002129 DAG.getConstant(4, getPointerTy()));
2130 }
2131 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002132 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002133 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00002134 } else
2135 // This will point to the next argument passed via stack.
Dan Gohman1e93df62010-04-17 14:41:14 +00002136 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset,
2137 true, false));
Evan Chenga8e29892007-01-19 07:51:42 +00002138 }
2139
Dan Gohman98ca4f22009-08-05 01:29:28 +00002140 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002141}
2142
2143/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002144static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002145 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002146 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002147 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002148 // Maybe this has already been legalized into the constant pool?
2149 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002150 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002151 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002152 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002153 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002154 }
2155 }
2156 return false;
2157}
2158
Evan Chenga8e29892007-01-19 07:51:42 +00002159/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2160/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002161SDValue
2162ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Dan Gohmand858e902010-04-17 15:26:15 +00002163 SDValue &ARMCC, SelectionDAG &DAG,
2164 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002165 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002166 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002167 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002168 // Constant does not fit, try adjusting it by one?
2169 switch (CC) {
2170 default: break;
2171 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002172 case ISD::SETGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00002173 if (isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002174 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002175 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002176 }
2177 break;
2178 case ISD::SETULT:
2179 case ISD::SETUGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00002180 if (C > 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002181 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002182 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002183 }
2184 break;
2185 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002186 case ISD::SETGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00002187 if (isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002188 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002189 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002190 }
2191 break;
2192 case ISD::SETULE:
2193 case ISD::SETUGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00002194 if (C < 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002195 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002196 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002197 }
2198 break;
2199 }
2200 }
2201 }
2202
2203 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002204 ARMISD::NodeType CompareType;
2205 switch (CondCode) {
2206 default:
2207 CompareType = ARMISD::CMP;
2208 break;
2209 case ARMCC::EQ:
2210 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002211 // Uses only Z Flag
2212 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002213 break;
2214 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002215 ARMCC = DAG.getConstant(CondCode, MVT::i32);
2216 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002217}
2218
2219/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00002220static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00002221 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00002222 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002223 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00002224 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002225 else
Owen Anderson825b72b2009-08-11 20:47:22 +00002226 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
2227 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002228}
2229
Dan Gohmand858e902010-04-17 15:26:15 +00002230SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002231 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002232 SDValue LHS = Op.getOperand(0);
2233 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002234 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002235 SDValue TrueVal = Op.getOperand(2);
2236 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002237 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002238
Owen Anderson825b72b2009-08-11 20:47:22 +00002239 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00002240 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002241 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00002242 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Dale Johannesende064702009-02-06 21:50:26 +00002243 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002244 }
2245
2246 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002247 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002248
Owen Anderson825b72b2009-08-11 20:47:22 +00002249 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
2250 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002251 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
2252 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00002253 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002254 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002255 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002256 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00002257 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002258 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00002259 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002260 }
2261 return Result;
2262}
2263
Dan Gohmand858e902010-04-17 15:26:15 +00002264SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002265 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002266 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002267 SDValue LHS = Op.getOperand(2);
2268 SDValue RHS = Op.getOperand(3);
2269 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002270 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002271
Owen Anderson825b72b2009-08-11 20:47:22 +00002272 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00002273 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002274 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00002275 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002276 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00002277 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002278 }
2279
Owen Anderson825b72b2009-08-11 20:47:22 +00002280 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00002281 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002282 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002283
Dale Johannesende064702009-02-06 21:50:26 +00002284 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002285 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
2286 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2287 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002288 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002289 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002290 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002291 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00002292 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002293 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002294 }
2295 return Res;
2296}
2297
Dan Gohmand858e902010-04-17 15:26:15 +00002298SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002299 SDValue Chain = Op.getOperand(0);
2300 SDValue Table = Op.getOperand(1);
2301 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002302 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002303
Owen Andersone50ed302009-08-10 22:56:29 +00002304 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002305 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2306 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002307 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002308 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002309 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002310 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2311 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002312 if (Subtarget->isThumb2()) {
2313 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2314 // which does another jump to the destination. This also makes it easier
2315 // to translate it to TBB / TBH later.
2316 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002317 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002318 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002319 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002320 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002321 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002322 PseudoSourceValue::getJumpTable(), 0,
2323 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002324 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002325 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002326 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002327 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002328 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002329 PseudoSourceValue::getJumpTable(), 0, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002330 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002331 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002332 }
Evan Chenga8e29892007-01-19 07:51:42 +00002333}
2334
Bob Wilson76a312b2010-03-19 22:51:32 +00002335static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2336 DebugLoc dl = Op.getDebugLoc();
2337 unsigned Opc;
2338
2339 switch (Op.getOpcode()) {
2340 default:
2341 assert(0 && "Invalid opcode!");
2342 case ISD::FP_TO_SINT:
2343 Opc = ARMISD::FTOSI;
2344 break;
2345 case ISD::FP_TO_UINT:
2346 Opc = ARMISD::FTOUI;
2347 break;
2348 }
2349 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
2350 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
2351}
2352
2353static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2354 EVT VT = Op.getValueType();
2355 DebugLoc dl = Op.getDebugLoc();
2356 unsigned Opc;
2357
2358 switch (Op.getOpcode()) {
2359 default:
2360 assert(0 && "Invalid opcode!");
2361 case ISD::SINT_TO_FP:
2362 Opc = ARMISD::SITOF;
2363 break;
2364 case ISD::UINT_TO_FP:
2365 Opc = ARMISD::UITOF;
2366 break;
2367 }
2368
2369 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
2370 return DAG.getNode(Opc, dl, VT, Op);
2371}
2372
Dan Gohman475871a2008-07-27 21:46:04 +00002373static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002374 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002375 SDValue Tmp0 = Op.getOperand(0);
2376 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002377 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002378 EVT VT = Op.getValueType();
2379 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00002380 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
2381 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002382 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
2383 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002384 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002385}
2386
Evan Cheng2457f2c2010-05-22 01:47:14 +00002387SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
2388 MachineFunction &MF = DAG.getMachineFunction();
2389 MachineFrameInfo *MFI = MF.getFrameInfo();
2390 MFI->setReturnAddressIsTaken(true);
2391
2392 EVT VT = Op.getValueType();
2393 DebugLoc dl = Op.getDebugLoc();
2394 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2395 if (Depth) {
2396 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
2397 SDValue Offset = DAG.getConstant(4, MVT::i32);
2398 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
2399 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
2400 NULL, 0, false, false, 0);
2401 }
2402
2403 // Return LR, which contains the return address. Mark it an implicit live-in.
Evan Chengc7cf10c2010-05-24 18:00:18 +00002404 unsigned Reg = MF.addLiveIn(ARM::LR, ARM::GPRRegisterClass);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002405 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
2406}
2407
Dan Gohmand858e902010-04-17 15:26:15 +00002408SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002409 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2410 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002411
Owen Andersone50ed302009-08-10 22:56:29 +00002412 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002413 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2414 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002415 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002416 ? ARM::R7 : ARM::R11;
2417 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2418 while (Depth--)
David Greene1b58cab2010-02-15 16:55:24 +00002419 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
2420 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002421 return FrameAddr;
2422}
2423
Bob Wilson9f3f0612010-04-17 05:30:19 +00002424/// ExpandBIT_CONVERT - If the target supports VFP, this function is called to
2425/// expand a bit convert where either the source or destination type is i64 to
2426/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2427/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2428/// vectors), since the legalizer won't know what to do with that.
Duncan Sands1607f052008-12-01 11:39:25 +00002429static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002430 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2431 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002432 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002433
Bob Wilson9f3f0612010-04-17 05:30:19 +00002434 // This function is only supposed to be called for i64 types, either as the
2435 // source or destination of the bit convert.
2436 EVT SrcVT = Op.getValueType();
2437 EVT DstVT = N->getValueType(0);
2438 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
2439 "ExpandBIT_CONVERT called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002440
Bob Wilson9f3f0612010-04-17 05:30:19 +00002441 // Turn i64->f64 into VMOVDRR.
2442 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002443 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2444 DAG.getConstant(0, MVT::i32));
2445 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2446 DAG.getConstant(1, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00002447 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002448 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002449
Jim Grosbache5165492009-11-09 00:11:35 +00002450 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002451 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2452 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2453 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2454 // Merge the pieces into a single i64 value.
2455 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2456 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002457
Bob Wilson9f3f0612010-04-17 05:30:19 +00002458 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002459}
2460
Bob Wilson5bafff32009-06-22 23:27:02 +00002461/// getZeroVector - Returns a vector of specified type with all zero elements.
2462///
Owen Andersone50ed302009-08-10 22:56:29 +00002463static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002464 assert(VT.isVector() && "Expected a vector type");
2465
2466 // Zero vectors are used to represent vector negation and in those cases
2467 // will be implemented with the NEON VNEG instruction. However, VNEG does
2468 // not support i64 elements, so sometimes the zero vectors will need to be
2469 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002470 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilson5bafff32009-06-22 23:27:02 +00002471 // to their dest type. This ensures they get CSE'd.
2472 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002473 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2474 SmallVector<SDValue, 8> Ops;
2475 MVT TVT;
2476
2477 if (VT.getSizeInBits() == 64) {
2478 Ops.assign(8, Cst); TVT = MVT::v8i8;
2479 } else {
2480 Ops.assign(16, Cst); TVT = MVT::v16i8;
2481 }
2482 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002483
2484 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2485}
2486
2487/// getOnesVector - Returns a vector of specified type with all bits set.
2488///
Owen Andersone50ed302009-08-10 22:56:29 +00002489static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002490 assert(VT.isVector() && "Expected a vector type");
2491
Bob Wilson929ffa22009-10-30 20:13:25 +00002492 // Always build ones vectors as <16 x i8> or <8 x i8> bitcasted to their
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002493 // dest type. This ensures they get CSE'd.
Bob Wilson5bafff32009-06-22 23:27:02 +00002494 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002495 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2496 SmallVector<SDValue, 8> Ops;
2497 MVT TVT;
2498
2499 if (VT.getSizeInBits() == 64) {
2500 Ops.assign(8, Cst); TVT = MVT::v8i8;
2501 } else {
2502 Ops.assign(16, Cst); TVT = MVT::v16i8;
2503 }
2504 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002505
2506 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2507}
2508
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002509/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2510/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002511SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2512 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002513 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2514 EVT VT = Op.getValueType();
2515 unsigned VTBits = VT.getSizeInBits();
2516 DebugLoc dl = Op.getDebugLoc();
2517 SDValue ShOpLo = Op.getOperand(0);
2518 SDValue ShOpHi = Op.getOperand(1);
2519 SDValue ShAmt = Op.getOperand(2);
2520 SDValue ARMCC;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002521 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002522
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002523 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2524
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002525 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2526 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2527 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2528 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2529 DAG.getConstant(VTBits, MVT::i32));
2530 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2531 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002532 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002533
2534 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2535 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002536 ARMCC, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002537 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002538 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC,
2539 CCR, Cmp);
2540
2541 SDValue Ops[2] = { Lo, Hi };
2542 return DAG.getMergeValues(Ops, 2, dl);
2543}
2544
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002545/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2546/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002547SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2548 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002549 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2550 EVT VT = Op.getValueType();
2551 unsigned VTBits = VT.getSizeInBits();
2552 DebugLoc dl = Op.getDebugLoc();
2553 SDValue ShOpLo = Op.getOperand(0);
2554 SDValue ShOpHi = Op.getOperand(1);
2555 SDValue ShAmt = Op.getOperand(2);
2556 SDValue ARMCC;
2557
2558 assert(Op.getOpcode() == ISD::SHL_PARTS);
2559 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2560 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2561 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2562 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2563 DAG.getConstant(VTBits, MVT::i32));
2564 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2565 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2566
2567 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2568 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2569 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002570 ARMCC, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002571 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
2572 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMCC,
2573 CCR, Cmp);
2574
2575 SDValue Ops[2] = { Lo, Hi };
2576 return DAG.getMergeValues(Ops, 2, dl);
2577}
2578
Jim Grosbach3482c802010-01-18 19:58:49 +00002579static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
2580 const ARMSubtarget *ST) {
2581 EVT VT = N->getValueType(0);
2582 DebugLoc dl = N->getDebugLoc();
2583
2584 if (!ST->hasV6T2Ops())
2585 return SDValue();
2586
2587 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
2588 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
2589}
2590
Bob Wilson5bafff32009-06-22 23:27:02 +00002591static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2592 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002593 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002594 DebugLoc dl = N->getDebugLoc();
2595
2596 // Lower vector shifts on NEON to use VSHL.
2597 if (VT.isVector()) {
2598 assert(ST->hasNEON() && "unexpected vector shift");
2599
2600 // Left shifts translate directly to the vshiftu intrinsic.
2601 if (N->getOpcode() == ISD::SHL)
2602 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002603 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002604 N->getOperand(0), N->getOperand(1));
2605
2606 assert((N->getOpcode() == ISD::SRA ||
2607 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2608
2609 // NEON uses the same intrinsics for both left and right shifts. For
2610 // right shifts, the shift amounts are negative, so negate the vector of
2611 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002612 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002613 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2614 getZeroVector(ShiftVT, DAG, dl),
2615 N->getOperand(1));
2616 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2617 Intrinsic::arm_neon_vshifts :
2618 Intrinsic::arm_neon_vshiftu);
2619 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002620 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002621 N->getOperand(0), NegatedCount);
2622 }
2623
Eli Friedmance392eb2009-08-22 03:13:10 +00002624 // We can get here for a node like i32 = ISD::SHL i32, i64
2625 if (VT != MVT::i64)
2626 return SDValue();
2627
2628 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002629 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002630
Chris Lattner27a6c732007-11-24 07:07:01 +00002631 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2632 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002633 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002634 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002635
Chris Lattner27a6c732007-11-24 07:07:01 +00002636 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002637 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002638
Chris Lattner27a6c732007-11-24 07:07:01 +00002639 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002640 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00002641 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00002642 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00002643 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002644
Chris Lattner27a6c732007-11-24 07:07:01 +00002645 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2646 // captures the result into a carry flag.
2647 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002648 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002649
Chris Lattner27a6c732007-11-24 07:07:01 +00002650 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002651 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002652
Chris Lattner27a6c732007-11-24 07:07:01 +00002653 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002654 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002655}
2656
Bob Wilson5bafff32009-06-22 23:27:02 +00002657static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2658 SDValue TmpOp0, TmpOp1;
2659 bool Invert = false;
2660 bool Swap = false;
2661 unsigned Opc = 0;
2662
2663 SDValue Op0 = Op.getOperand(0);
2664 SDValue Op1 = Op.getOperand(1);
2665 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002666 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002667 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2668 DebugLoc dl = Op.getDebugLoc();
2669
2670 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2671 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002672 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002673 case ISD::SETUNE:
2674 case ISD::SETNE: Invert = true; // Fallthrough
2675 case ISD::SETOEQ:
2676 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2677 case ISD::SETOLT:
2678 case ISD::SETLT: Swap = true; // Fallthrough
2679 case ISD::SETOGT:
2680 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2681 case ISD::SETOLE:
2682 case ISD::SETLE: Swap = true; // Fallthrough
2683 case ISD::SETOGE:
2684 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2685 case ISD::SETUGE: Swap = true; // Fallthrough
2686 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2687 case ISD::SETUGT: Swap = true; // Fallthrough
2688 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2689 case ISD::SETUEQ: Invert = true; // Fallthrough
2690 case ISD::SETONE:
2691 // Expand this to (OLT | OGT).
2692 TmpOp0 = Op0;
2693 TmpOp1 = Op1;
2694 Opc = ISD::OR;
2695 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2696 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2697 break;
2698 case ISD::SETUO: Invert = true; // Fallthrough
2699 case ISD::SETO:
2700 // Expand this to (OLT | OGE).
2701 TmpOp0 = Op0;
2702 TmpOp1 = Op1;
2703 Opc = ISD::OR;
2704 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2705 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2706 break;
2707 }
2708 } else {
2709 // Integer comparisons.
2710 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002711 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002712 case ISD::SETNE: Invert = true;
2713 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2714 case ISD::SETLT: Swap = true;
2715 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2716 case ISD::SETLE: Swap = true;
2717 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2718 case ISD::SETULT: Swap = true;
2719 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2720 case ISD::SETULE: Swap = true;
2721 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2722 }
2723
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002724 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002725 if (Opc == ARMISD::VCEQ) {
2726
2727 SDValue AndOp;
2728 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2729 AndOp = Op0;
2730 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2731 AndOp = Op1;
2732
2733 // Ignore bitconvert.
2734 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2735 AndOp = AndOp.getOperand(0);
2736
2737 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2738 Opc = ARMISD::VTST;
2739 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2740 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2741 Invert = !Invert;
2742 }
2743 }
2744 }
2745
2746 if (Swap)
2747 std::swap(Op0, Op1);
2748
2749 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2750
2751 if (Invert)
2752 Result = DAG.getNOT(dl, Result, VT);
2753
2754 return Result;
2755}
2756
2757/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2758/// VMOV instruction, and if so, return the constant being splatted.
2759static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2760 unsigned SplatBitSize, SelectionDAG &DAG) {
2761 switch (SplatBitSize) {
2762 case 8:
2763 // Any 1-byte value is OK.
2764 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson825b72b2009-08-11 20:47:22 +00002765 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilson5bafff32009-06-22 23:27:02 +00002766
2767 case 16:
2768 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2769 if ((SplatBits & ~0xff) == 0 ||
2770 (SplatBits & ~0xff00) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002771 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilson5bafff32009-06-22 23:27:02 +00002772 break;
2773
2774 case 32:
2775 // NEON's 32-bit VMOV supports splat values where:
2776 // * only one byte is nonzero, or
2777 // * the least significant byte is 0xff and the second byte is nonzero, or
2778 // * the least significant 2 bytes are 0xff and the third is nonzero.
2779 if ((SplatBits & ~0xff) == 0 ||
2780 (SplatBits & ~0xff00) == 0 ||
2781 (SplatBits & ~0xff0000) == 0 ||
2782 (SplatBits & ~0xff000000) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002783 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002784
2785 if ((SplatBits & ~0xffff) == 0 &&
2786 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002787 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002788
2789 if ((SplatBits & ~0xffffff) == 0 &&
2790 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002791 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002792
2793 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2794 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2795 // VMOV.I32. A (very) minor optimization would be to replicate the value
2796 // and fall through here to test for a valid 64-bit splat. But, then the
2797 // caller would also need to check and handle the change in size.
2798 break;
2799
2800 case 64: {
2801 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2802 uint64_t BitMask = 0xff;
2803 uint64_t Val = 0;
2804 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2805 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2806 Val |= BitMask;
2807 else if ((SplatBits & BitMask) != 0)
2808 return SDValue();
2809 BitMask <<= 8;
2810 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002811 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilson5bafff32009-06-22 23:27:02 +00002812 }
2813
2814 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002815 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilson5bafff32009-06-22 23:27:02 +00002816 break;
2817 }
2818
2819 return SDValue();
2820}
2821
2822/// getVMOVImm - If this is a build_vector of constants which can be
2823/// formed by using a VMOV instruction of the specified element size,
2824/// return the constant being splatted. The ByteSize field indicates the
2825/// number of bytes of each element [1248].
2826SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2827 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2828 APInt SplatBits, SplatUndef;
2829 unsigned SplatBitSize;
2830 bool HasAnyUndefs;
2831 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2832 HasAnyUndefs, ByteSize * 8))
2833 return SDValue();
2834
2835 if (SplatBitSize > ByteSize * 8)
2836 return SDValue();
2837
2838 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2839 SplatBitSize, DAG);
2840}
2841
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002842static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2843 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002844 unsigned NumElts = VT.getVectorNumElements();
2845 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002846 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002847
2848 // If this is a VEXT shuffle, the immediate value is the index of the first
2849 // element. The other shuffle indices must be the successive elements after
2850 // the first one.
2851 unsigned ExpectedElt = Imm;
2852 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002853 // Increment the expected index. If it wraps around, it may still be
2854 // a VEXT but the source vectors must be swapped.
2855 ExpectedElt += 1;
2856 if (ExpectedElt == NumElts * 2) {
2857 ExpectedElt = 0;
2858 ReverseVEXT = true;
2859 }
2860
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002861 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002862 return false;
2863 }
2864
2865 // Adjust the index value if the source operands will be swapped.
2866 if (ReverseVEXT)
2867 Imm -= NumElts;
2868
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002869 return true;
2870}
2871
Bob Wilson8bb9e482009-07-26 00:39:34 +00002872/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2873/// instruction with the specified blocksize. (The order of the elements
2874/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002875static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2876 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002877 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2878 "Only possible block sizes for VREV are: 16, 32, 64");
2879
Bob Wilson8bb9e482009-07-26 00:39:34 +00002880 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00002881 if (EltSz == 64)
2882 return false;
2883
2884 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002885 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002886
2887 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2888 return false;
2889
2890 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002891 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002892 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2893 return false;
2894 }
2895
2896 return true;
2897}
2898
Bob Wilsonc692cb72009-08-21 20:54:19 +00002899static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2900 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002901 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2902 if (EltSz == 64)
2903 return false;
2904
Bob Wilsonc692cb72009-08-21 20:54:19 +00002905 unsigned NumElts = VT.getVectorNumElements();
2906 WhichResult = (M[0] == 0 ? 0 : 1);
2907 for (unsigned i = 0; i < NumElts; i += 2) {
2908 if ((unsigned) M[i] != i + WhichResult ||
2909 (unsigned) M[i+1] != i + NumElts + WhichResult)
2910 return false;
2911 }
2912 return true;
2913}
2914
Bob Wilson324f4f12009-12-03 06:40:55 +00002915/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
2916/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2917/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
2918static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2919 unsigned &WhichResult) {
2920 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2921 if (EltSz == 64)
2922 return false;
2923
2924 unsigned NumElts = VT.getVectorNumElements();
2925 WhichResult = (M[0] == 0 ? 0 : 1);
2926 for (unsigned i = 0; i < NumElts; i += 2) {
2927 if ((unsigned) M[i] != i + WhichResult ||
2928 (unsigned) M[i+1] != i + WhichResult)
2929 return false;
2930 }
2931 return true;
2932}
2933
Bob Wilsonc692cb72009-08-21 20:54:19 +00002934static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2935 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002936 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2937 if (EltSz == 64)
2938 return false;
2939
Bob Wilsonc692cb72009-08-21 20:54:19 +00002940 unsigned NumElts = VT.getVectorNumElements();
2941 WhichResult = (M[0] == 0 ? 0 : 1);
2942 for (unsigned i = 0; i != NumElts; ++i) {
2943 if ((unsigned) M[i] != 2 * i + WhichResult)
2944 return false;
2945 }
2946
2947 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002948 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002949 return false;
2950
2951 return true;
2952}
2953
Bob Wilson324f4f12009-12-03 06:40:55 +00002954/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
2955/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2956/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
2957static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2958 unsigned &WhichResult) {
2959 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2960 if (EltSz == 64)
2961 return false;
2962
2963 unsigned Half = VT.getVectorNumElements() / 2;
2964 WhichResult = (M[0] == 0 ? 0 : 1);
2965 for (unsigned j = 0; j != 2; ++j) {
2966 unsigned Idx = WhichResult;
2967 for (unsigned i = 0; i != Half; ++i) {
2968 if ((unsigned) M[i + j * Half] != Idx)
2969 return false;
2970 Idx += 2;
2971 }
2972 }
2973
2974 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2975 if (VT.is64BitVector() && EltSz == 32)
2976 return false;
2977
2978 return true;
2979}
2980
Bob Wilsonc692cb72009-08-21 20:54:19 +00002981static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2982 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002983 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2984 if (EltSz == 64)
2985 return false;
2986
Bob Wilsonc692cb72009-08-21 20:54:19 +00002987 unsigned NumElts = VT.getVectorNumElements();
2988 WhichResult = (M[0] == 0 ? 0 : 1);
2989 unsigned Idx = WhichResult * NumElts / 2;
2990 for (unsigned i = 0; i != NumElts; i += 2) {
2991 if ((unsigned) M[i] != Idx ||
2992 (unsigned) M[i+1] != Idx + NumElts)
2993 return false;
2994 Idx += 1;
2995 }
2996
2997 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002998 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002999 return false;
3000
3001 return true;
3002}
3003
Bob Wilson324f4f12009-12-03 06:40:55 +00003004/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3005/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3006/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3007static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3008 unsigned &WhichResult) {
3009 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3010 if (EltSz == 64)
3011 return false;
3012
3013 unsigned NumElts = VT.getVectorNumElements();
3014 WhichResult = (M[0] == 0 ? 0 : 1);
3015 unsigned Idx = WhichResult * NumElts / 2;
3016 for (unsigned i = 0; i != NumElts; i += 2) {
3017 if ((unsigned) M[i] != Idx ||
3018 (unsigned) M[i+1] != Idx)
3019 return false;
3020 Idx += 1;
3021 }
3022
3023 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3024 if (VT.is64BitVector() && EltSz == 32)
3025 return false;
3026
3027 return true;
3028}
3029
3030
Owen Andersone50ed302009-08-10 22:56:29 +00003031static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003032 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00003033 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003034 if (ConstVal->isNullValue())
3035 return getZeroVector(VT, DAG, dl);
3036 if (ConstVal->isAllOnesValue())
3037 return getOnesVector(VT, DAG, dl);
3038
Owen Andersone50ed302009-08-10 22:56:29 +00003039 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00003040 if (VT.is64BitVector()) {
3041 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003042 case 8: CanonicalVT = MVT::v8i8; break;
3043 case 16: CanonicalVT = MVT::v4i16; break;
3044 case 32: CanonicalVT = MVT::v2i32; break;
3045 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003046 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003047 }
3048 } else {
3049 assert(VT.is128BitVector() && "unknown splat vector size");
3050 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003051 case 8: CanonicalVT = MVT::v16i8; break;
3052 case 16: CanonicalVT = MVT::v8i16; break;
3053 case 32: CanonicalVT = MVT::v4i32; break;
3054 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003055 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003056 }
3057 }
3058
3059 // Build a canonical splat for this value.
3060 SmallVector<SDValue, 8> Ops;
3061 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
3062 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
3063 Ops.size());
3064 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
3065}
3066
3067// If this is a case we can't handle, return null and let the default
3068// expansion code take care of it.
3069static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00003070 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003071 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003072 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003073
3074 APInt SplatBits, SplatUndef;
3075 unsigned SplatBitSize;
3076 bool HasAnyUndefs;
3077 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003078 if (SplatBitSize <= 64) {
3079 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
3080 SplatUndef.getZExtValue(), SplatBitSize, DAG);
3081 if (Val.getNode())
3082 return BuildSplat(Val, VT, DAG, dl);
3083 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003084 }
3085
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003086 // Scan through the operands to see if only one value is used.
3087 unsigned NumElts = VT.getVectorNumElements();
3088 bool isOnlyLowElement = true;
3089 bool usesOnlyOneValue = true;
3090 bool isConstant = true;
3091 SDValue Value;
3092 for (unsigned i = 0; i < NumElts; ++i) {
3093 SDValue V = Op.getOperand(i);
3094 if (V.getOpcode() == ISD::UNDEF)
3095 continue;
3096 if (i > 0)
3097 isOnlyLowElement = false;
3098 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3099 isConstant = false;
3100
3101 if (!Value.getNode())
3102 Value = V;
3103 else if (V != Value)
3104 usesOnlyOneValue = false;
3105 }
3106
3107 if (!Value.getNode())
3108 return DAG.getUNDEF(VT);
3109
3110 if (isOnlyLowElement)
3111 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3112
3113 // If all elements are constants, fall back to the default expansion, which
3114 // will generate a load from the constant pool.
3115 if (isConstant)
3116 return SDValue();
3117
3118 // Use VDUP for non-constant splats.
Bob Wilson069e4342010-05-23 05:42:31 +00003119 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3120 if (usesOnlyOneValue && EltSize <= 32)
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003121 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3122
3123 // Vectors with 32- or 64-bit elements can be built by directly assigning
3124 // the subregisters.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003125 if (EltSize >= 32) {
3126 // Do the expansion with floating-point types, since that is what the VFP
3127 // registers are defined to use, and since i64 is not legal.
3128 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3129 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
3130 SDValue Val = DAG.getUNDEF(VecVT);
3131 for (unsigned i = 0; i < NumElts; ++i) {
3132 SDValue Elt = Op.getOperand(i);
3133 if (Elt.getOpcode() == ISD::UNDEF)
3134 continue;
3135 Elt = DAG.getNode(ISD::BIT_CONVERT, dl, EltVT, Elt);
3136 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Val, Elt,
3137 DAG.getConstant(i, MVT::i32));
3138 }
3139 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003140 }
3141
3142 return SDValue();
3143}
3144
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003145/// isShuffleMaskLegal - Targets can use this to indicate that they only
3146/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3147/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3148/// are assumed to be legal.
3149bool
3150ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
3151 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003152 if (VT.getVectorNumElements() == 4 &&
3153 (VT.is128BitVector() || VT.is64BitVector())) {
3154 unsigned PFIndexes[4];
3155 for (unsigned i = 0; i != 4; ++i) {
3156 if (M[i] < 0)
3157 PFIndexes[i] = 8;
3158 else
3159 PFIndexes[i] = M[i];
3160 }
3161
3162 // Compute the index in the perfect shuffle table.
3163 unsigned PFTableIndex =
3164 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3165 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3166 unsigned Cost = (PFEntry >> 30);
3167
3168 if (Cost <= 4)
3169 return true;
3170 }
3171
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003172 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00003173 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003174
3175 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
3176 isVREVMask(M, VT, 64) ||
3177 isVREVMask(M, VT, 32) ||
3178 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00003179 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
3180 isVTRNMask(M, VT, WhichResult) ||
3181 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00003182 isVZIPMask(M, VT, WhichResult) ||
3183 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
3184 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
3185 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003186}
3187
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003188/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
3189/// the specified operations to build the shuffle.
3190static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
3191 SDValue RHS, SelectionDAG &DAG,
3192 DebugLoc dl) {
3193 unsigned OpNum = (PFEntry >> 26) & 0x0F;
3194 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
3195 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
3196
3197 enum {
3198 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
3199 OP_VREV,
3200 OP_VDUP0,
3201 OP_VDUP1,
3202 OP_VDUP2,
3203 OP_VDUP3,
3204 OP_VEXT1,
3205 OP_VEXT2,
3206 OP_VEXT3,
3207 OP_VUZPL, // VUZP, left result
3208 OP_VUZPR, // VUZP, right result
3209 OP_VZIPL, // VZIP, left result
3210 OP_VZIPR, // VZIP, right result
3211 OP_VTRNL, // VTRN, left result
3212 OP_VTRNR // VTRN, right result
3213 };
3214
3215 if (OpNum == OP_COPY) {
3216 if (LHSID == (1*9+2)*9+3) return LHS;
3217 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
3218 return RHS;
3219 }
3220
3221 SDValue OpLHS, OpRHS;
3222 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
3223 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
3224 EVT VT = OpLHS.getValueType();
3225
3226 switch (OpNum) {
3227 default: llvm_unreachable("Unknown shuffle opcode!");
3228 case OP_VREV:
3229 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
3230 case OP_VDUP0:
3231 case OP_VDUP1:
3232 case OP_VDUP2:
3233 case OP_VDUP3:
3234 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003235 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003236 case OP_VEXT1:
3237 case OP_VEXT2:
3238 case OP_VEXT3:
3239 return DAG.getNode(ARMISD::VEXT, dl, VT,
3240 OpLHS, OpRHS,
3241 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
3242 case OP_VUZPL:
3243 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003244 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003245 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
3246 case OP_VZIPL:
3247 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003248 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003249 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
3250 case OP_VTRNL:
3251 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003252 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3253 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003254 }
3255}
3256
Bob Wilson5bafff32009-06-22 23:27:02 +00003257static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003258 SDValue V1 = Op.getOperand(0);
3259 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00003260 DebugLoc dl = Op.getDebugLoc();
3261 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003262 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003263 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00003264
Bob Wilson28865062009-08-13 02:13:04 +00003265 // Convert shuffles that are directly supported on NEON to target-specific
3266 // DAG nodes, instead of keeping them as shuffles and matching them again
3267 // during code selection. This is more efficient and avoids the possibility
3268 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00003269 // FIXME: floating-point vectors should be canonicalized to integer vectors
3270 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003271 SVN->getMask(ShuffleMask);
3272
3273 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson0ce37102009-08-14 05:08:32 +00003274 int Lane = SVN->getSplatIndex();
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00003275 // If this is undef splat, generate it via "just" vdup, if possible.
3276 if (Lane == -1) Lane = 0;
3277
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003278 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3279 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00003280 }
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003281 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003282 DAG.getConstant(Lane, MVT::i32));
Bob Wilson0ce37102009-08-14 05:08:32 +00003283 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003284
3285 bool ReverseVEXT;
3286 unsigned Imm;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003287 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003288 if (ReverseVEXT)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003289 std::swap(V1, V2);
3290 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003291 DAG.getConstant(Imm, MVT::i32));
3292 }
3293
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003294 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003295 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003296 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003297 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003298 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003299 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
3300
Bob Wilsonc692cb72009-08-21 20:54:19 +00003301 // Check for Neon shuffles that modify both input vectors in place.
3302 // If both results are used, i.e., if there are two shuffles with the same
3303 // source operands and with masks corresponding to both results of one of
3304 // these operations, DAG memoization will ensure that a single node is
3305 // used for both shuffles.
3306 unsigned WhichResult;
3307 if (isVTRNMask(ShuffleMask, VT, WhichResult))
3308 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3309 V1, V2).getValue(WhichResult);
3310 if (isVUZPMask(ShuffleMask, VT, WhichResult))
3311 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3312 V1, V2).getValue(WhichResult);
3313 if (isVZIPMask(ShuffleMask, VT, WhichResult))
3314 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3315 V1, V2).getValue(WhichResult);
3316
Bob Wilson324f4f12009-12-03 06:40:55 +00003317 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
3318 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3319 V1, V1).getValue(WhichResult);
3320 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3321 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3322 V1, V1).getValue(WhichResult);
3323 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3324 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3325 V1, V1).getValue(WhichResult);
3326
Bob Wilsonc692cb72009-08-21 20:54:19 +00003327 // If the shuffle is not directly supported and it has 4 elements, use
3328 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003329 unsigned NumElts = VT.getVectorNumElements();
3330 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003331 unsigned PFIndexes[4];
3332 for (unsigned i = 0; i != 4; ++i) {
3333 if (ShuffleMask[i] < 0)
3334 PFIndexes[i] = 8;
3335 else
3336 PFIndexes[i] = ShuffleMask[i];
3337 }
3338
3339 // Compute the index in the perfect shuffle table.
3340 unsigned PFTableIndex =
3341 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003342 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3343 unsigned Cost = (PFEntry >> 30);
3344
3345 if (Cost <= 4)
3346 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
3347 }
Bob Wilsond8e17572009-08-12 22:31:50 +00003348
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003349 // Implement shuffles with 32- or 64-bit elements as subreg copies.
3350 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3351 if (EltSize >= 32) {
3352 // Do the expansion with floating-point types, since that is what the VFP
3353 // registers are defined to use, and since i64 is not legal.
3354 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3355 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
3356 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V1);
3357 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V2);
3358 SDValue Val = DAG.getUNDEF(VecVT);
3359 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00003360 if (ShuffleMask[i] < 0)
3361 continue;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003362 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
3363 ShuffleMask[i] < (int)NumElts ? V1 : V2,
3364 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
3365 MVT::i32));
3366 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Val,
Bob Wilson63b88452010-05-20 18:39:53 +00003367 Elt, DAG.getConstant(i, MVT::i32));
3368 }
3369 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
3370 }
3371
Bob Wilson22cac0d2009-08-14 05:16:33 +00003372 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003373}
3374
Bob Wilson5bafff32009-06-22 23:27:02 +00003375static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003376 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003377 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003378 SDValue Vec = Op.getOperand(0);
3379 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00003380 assert(VT == MVT::i32 &&
3381 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
3382 "unexpected type for custom-lowering vector extract");
3383 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00003384}
3385
Bob Wilsona6d65862009-08-03 20:36:38 +00003386static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3387 // The only time a CONCAT_VECTORS operation can have legal types is when
3388 // two 64-bit vectors are concatenated to a 128-bit vector.
3389 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
3390 "unexpected CONCAT_VECTORS");
3391 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00003392 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00003393 SDValue Op0 = Op.getOperand(0);
3394 SDValue Op1 = Op.getOperand(1);
3395 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003396 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3397 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00003398 DAG.getIntPtrConstant(0));
3399 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003400 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3401 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00003402 DAG.getIntPtrConstant(1));
3403 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003404}
3405
Dan Gohmand858e902010-04-17 15:26:15 +00003406SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003407 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003408 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00003409 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00003410 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003411 case ISD::GlobalAddress:
3412 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
3413 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00003414 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00003415 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
3416 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003417 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00003418 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00003419 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00003420 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00003421 case ISD::SINT_TO_FP:
3422 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
3423 case ISD::FP_TO_SINT:
3424 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003425 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003426 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003427 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003428 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00003429 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00003430 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00003431 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
3432 Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00003433 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003434 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00003435 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00003436 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00003437 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003438 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00003439 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00003440 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003441 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
3442 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3443 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003444 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00003445 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003446 }
Dan Gohman475871a2008-07-27 21:46:04 +00003447 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003448}
3449
Duncan Sands1607f052008-12-01 11:39:25 +00003450/// ReplaceNodeResults - Replace the results of node with an illegal result
3451/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00003452void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
3453 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00003454 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00003455 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00003456 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00003457 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003458 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003459 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003460 case ISD::BIT_CONVERT:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003461 Res = ExpandBIT_CONVERT(N, DAG);
3462 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00003463 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003464 case ISD::SRA:
3465 Res = LowerShift(N, DAG, Subtarget);
3466 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003467 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00003468 if (Res.getNode())
3469 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00003470}
Chris Lattner27a6c732007-11-24 07:07:01 +00003471
Evan Chenga8e29892007-01-19 07:51:42 +00003472//===----------------------------------------------------------------------===//
3473// ARM Scheduler Hooks
3474//===----------------------------------------------------------------------===//
3475
3476MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003477ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
3478 MachineBasicBlock *BB,
3479 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00003480 unsigned dest = MI->getOperand(0).getReg();
3481 unsigned ptr = MI->getOperand(1).getReg();
3482 unsigned oldval = MI->getOperand(2).getReg();
3483 unsigned newval = MI->getOperand(3).getReg();
3484 unsigned scratch = BB->getParent()->getRegInfo()
3485 .createVirtualRegister(ARM::GPRRegisterClass);
3486 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3487 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003488 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00003489
3490 unsigned ldrOpc, strOpc;
3491 switch (Size) {
3492 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003493 case 1:
3494 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
3495 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
3496 break;
3497 case 2:
3498 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3499 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3500 break;
3501 case 4:
3502 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3503 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3504 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00003505 }
3506
3507 MachineFunction *MF = BB->getParent();
3508 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3509 MachineFunction::iterator It = BB;
3510 ++It; // insert the new blocks after the current block
3511
3512 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3513 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3514 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3515 MF->insert(It, loop1MBB);
3516 MF->insert(It, loop2MBB);
3517 MF->insert(It, exitMBB);
3518 exitMBB->transferSuccessors(BB);
3519
3520 // thisMBB:
3521 // ...
3522 // fallthrough --> loop1MBB
3523 BB->addSuccessor(loop1MBB);
3524
3525 // loop1MBB:
3526 // ldrex dest, [ptr]
3527 // cmp dest, oldval
3528 // bne exitMBB
3529 BB = loop1MBB;
3530 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003531 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003532 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003533 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3534 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003535 BB->addSuccessor(loop2MBB);
3536 BB->addSuccessor(exitMBB);
3537
3538 // loop2MBB:
3539 // strex scratch, newval, [ptr]
3540 // cmp scratch, #0
3541 // bne loop1MBB
3542 BB = loop2MBB;
3543 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
3544 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003545 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003546 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003547 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3548 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003549 BB->addSuccessor(loop1MBB);
3550 BB->addSuccessor(exitMBB);
3551
3552 // exitMBB:
3553 // ...
3554 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00003555
3556 MF->DeleteMachineInstr(MI); // The instruction is gone now.
3557
Jim Grosbach5278eb82009-12-11 01:42:04 +00003558 return BB;
3559}
3560
3561MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003562ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
3563 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00003564 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
3565 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3566
3567 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003568 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003569 MachineFunction::iterator It = BB;
3570 ++It;
3571
3572 unsigned dest = MI->getOperand(0).getReg();
3573 unsigned ptr = MI->getOperand(1).getReg();
3574 unsigned incr = MI->getOperand(2).getReg();
3575 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00003576
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003577 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003578 unsigned ldrOpc, strOpc;
3579 switch (Size) {
3580 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003581 case 1:
3582 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00003583 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003584 break;
3585 case 2:
3586 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3587 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3588 break;
3589 case 4:
3590 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3591 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3592 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00003593 }
3594
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003595 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3596 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3597 MF->insert(It, loopMBB);
3598 MF->insert(It, exitMBB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003599 exitMBB->transferSuccessors(BB);
3600
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003601 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003602 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3603 unsigned scratch2 = (!BinOpcode) ? incr :
3604 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3605
3606 // thisMBB:
3607 // ...
3608 // fallthrough --> loopMBB
3609 BB->addSuccessor(loopMBB);
3610
3611 // loopMBB:
3612 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003613 // <binop> scratch2, dest, incr
3614 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00003615 // cmp scratch, #0
3616 // bne- loopMBB
3617 // fallthrough --> exitMBB
3618 BB = loopMBB;
3619 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00003620 if (BinOpcode) {
3621 // operand order needs to go the other way for NAND
3622 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
3623 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3624 addReg(incr).addReg(dest)).addReg(0);
3625 else
3626 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3627 addReg(dest).addReg(incr)).addReg(0);
3628 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00003629
3630 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
3631 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003632 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00003633 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003634 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3635 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003636
3637 BB->addSuccessor(loopMBB);
3638 BB->addSuccessor(exitMBB);
3639
3640 // exitMBB:
3641 // ...
3642 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00003643
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003644 MF->DeleteMachineInstr(MI); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00003645
Jim Grosbachc3c23542009-12-14 04:22:04 +00003646 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00003647}
3648
3649MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00003650ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003651 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003652 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00003653 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003654 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00003655 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00003656 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00003657 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00003658 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00003659
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003660 case ARM::ATOMIC_LOAD_ADD_I8:
3661 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3662 case ARM::ATOMIC_LOAD_ADD_I16:
3663 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3664 case ARM::ATOMIC_LOAD_ADD_I32:
3665 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003666
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003667 case ARM::ATOMIC_LOAD_AND_I8:
3668 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3669 case ARM::ATOMIC_LOAD_AND_I16:
3670 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3671 case ARM::ATOMIC_LOAD_AND_I32:
3672 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003673
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003674 case ARM::ATOMIC_LOAD_OR_I8:
3675 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3676 case ARM::ATOMIC_LOAD_OR_I16:
3677 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3678 case ARM::ATOMIC_LOAD_OR_I32:
3679 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003680
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003681 case ARM::ATOMIC_LOAD_XOR_I8:
3682 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3683 case ARM::ATOMIC_LOAD_XOR_I16:
3684 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3685 case ARM::ATOMIC_LOAD_XOR_I32:
3686 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003687
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003688 case ARM::ATOMIC_LOAD_NAND_I8:
3689 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3690 case ARM::ATOMIC_LOAD_NAND_I16:
3691 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3692 case ARM::ATOMIC_LOAD_NAND_I32:
3693 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003694
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003695 case ARM::ATOMIC_LOAD_SUB_I8:
3696 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3697 case ARM::ATOMIC_LOAD_SUB_I16:
3698 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3699 case ARM::ATOMIC_LOAD_SUB_I32:
3700 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003701
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003702 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
3703 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
3704 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00003705
3706 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
3707 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
3708 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003709
Evan Cheng007ea272009-08-12 05:17:19 +00003710 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00003711 // To "insert" a SELECT_CC instruction, we actually have to insert the
3712 // diamond control-flow pattern. The incoming instruction knows the
3713 // destination vreg to set, the condition code register to branch on, the
3714 // true/false values to select between, and a branch opcode to use.
3715 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003716 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00003717 ++It;
3718
3719 // thisMBB:
3720 // ...
3721 // TrueVal = ...
3722 // cmpTY ccX, r1, r2
3723 // bCC copy1MBB
3724 // fallthrough --> copy0MBB
3725 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003726 MachineFunction *F = BB->getParent();
3727 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3728 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00003729 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00003730 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003731 F->insert(It, copy0MBB);
3732 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00003733 // Update machine-CFG edges by first adding all successors of the current
3734 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00003735 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003736 E = BB->succ_end(); I != E; ++I)
Evan Chengce319102009-09-19 09:51:03 +00003737 sinkMBB->addSuccessor(*I);
Evan Chenga8e29892007-01-19 07:51:42 +00003738 // Next, remove all successors of the current block, and add the true
3739 // and fallthrough blocks as its successors.
Evan Chengce319102009-09-19 09:51:03 +00003740 while (!BB->succ_empty())
Evan Chenga8e29892007-01-19 07:51:42 +00003741 BB->removeSuccessor(BB->succ_begin());
3742 BB->addSuccessor(copy0MBB);
3743 BB->addSuccessor(sinkMBB);
3744
3745 // copy0MBB:
3746 // %FalseValue = ...
3747 // # fallthrough to sinkMBB
3748 BB = copy0MBB;
3749
3750 // Update machine-CFG edges
3751 BB->addSuccessor(sinkMBB);
3752
3753 // sinkMBB:
3754 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3755 // ...
3756 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00003757 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00003758 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
3759 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
3760
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003761 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00003762 return BB;
3763 }
Evan Cheng86198642009-08-07 00:34:42 +00003764
3765 case ARM::tANDsp:
3766 case ARM::tADDspr_:
3767 case ARM::tSUBspi_:
3768 case ARM::t2SUBrSPi_:
3769 case ARM::t2SUBrSPi12_:
3770 case ARM::t2SUBrSPs_: {
3771 MachineFunction *MF = BB->getParent();
3772 unsigned DstReg = MI->getOperand(0).getReg();
3773 unsigned SrcReg = MI->getOperand(1).getReg();
3774 bool DstIsDead = MI->getOperand(0).isDead();
3775 bool SrcIsKill = MI->getOperand(1).isKill();
3776
3777 if (SrcReg != ARM::SP) {
3778 // Copy the source to SP from virtual register.
3779 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
3780 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3781 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
3782 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
3783 .addReg(SrcReg, getKillRegState(SrcIsKill));
3784 }
3785
3786 unsigned OpOpc = 0;
3787 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
3788 switch (MI->getOpcode()) {
3789 default:
3790 llvm_unreachable("Unexpected pseudo instruction!");
3791 case ARM::tANDsp:
3792 OpOpc = ARM::tAND;
3793 NeedPred = true;
3794 break;
3795 case ARM::tADDspr_:
3796 OpOpc = ARM::tADDspr;
3797 break;
3798 case ARM::tSUBspi_:
3799 OpOpc = ARM::tSUBspi;
3800 break;
3801 case ARM::t2SUBrSPi_:
3802 OpOpc = ARM::t2SUBrSPi;
3803 NeedPred = true; NeedCC = true;
3804 break;
3805 case ARM::t2SUBrSPi12_:
3806 OpOpc = ARM::t2SUBrSPi12;
3807 NeedPred = true;
3808 break;
3809 case ARM::t2SUBrSPs_:
3810 OpOpc = ARM::t2SUBrSPs;
3811 NeedPred = true; NeedCC = true; NeedOp3 = true;
3812 break;
3813 }
3814 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
3815 if (OpOpc == ARM::tAND)
3816 AddDefaultT1CC(MIB);
3817 MIB.addReg(ARM::SP);
3818 MIB.addOperand(MI->getOperand(2));
3819 if (NeedOp3)
3820 MIB.addOperand(MI->getOperand(3));
3821 if (NeedPred)
3822 AddDefaultPred(MIB);
3823 if (NeedCC)
3824 AddDefaultCC(MIB);
3825
3826 // Copy the result from SP to virtual register.
3827 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
3828 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3829 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
3830 BuildMI(BB, dl, TII->get(CopyOpc))
3831 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
3832 .addReg(ARM::SP);
3833 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3834 return BB;
3835 }
Evan Chenga8e29892007-01-19 07:51:42 +00003836 }
3837}
3838
3839//===----------------------------------------------------------------------===//
3840// ARM Optimization Hooks
3841//===----------------------------------------------------------------------===//
3842
Chris Lattnerd1980a52009-03-12 06:52:53 +00003843static
3844SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3845 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00003846 SelectionDAG &DAG = DCI.DAG;
3847 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003848 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00003849 unsigned Opc = N->getOpcode();
3850 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3851 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3852 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3853 ISD::CondCode CC = ISD::SETCC_INVALID;
3854
3855 if (isSlctCC) {
3856 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
3857 } else {
3858 SDValue CCOp = Slct.getOperand(0);
3859 if (CCOp.getOpcode() == ISD::SETCC)
3860 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
3861 }
3862
3863 bool DoXform = false;
3864 bool InvCC = false;
3865 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
3866 "Bad input!");
3867
3868 if (LHS.getOpcode() == ISD::Constant &&
3869 cast<ConstantSDNode>(LHS)->isNullValue()) {
3870 DoXform = true;
3871 } else if (CC != ISD::SETCC_INVALID &&
3872 RHS.getOpcode() == ISD::Constant &&
3873 cast<ConstantSDNode>(RHS)->isNullValue()) {
3874 std::swap(LHS, RHS);
3875 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003876 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00003877 Op0.getOperand(0).getValueType();
3878 bool isInt = OpVT.isInteger();
3879 CC = ISD::getSetCCInverse(CC, isInt);
3880
3881 if (!TLI.isCondCodeLegal(CC, OpVT))
3882 return SDValue(); // Inverse operator isn't legal.
3883
3884 DoXform = true;
3885 InvCC = true;
3886 }
3887
3888 if (DoXform) {
3889 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3890 if (isSlctCC)
3891 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3892 Slct.getOperand(0), Slct.getOperand(1), CC);
3893 SDValue CCOp = Slct.getOperand(0);
3894 if (InvCC)
3895 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3896 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3897 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3898 CCOp, OtherOp, Result);
3899 }
3900 return SDValue();
3901}
3902
3903/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3904static SDValue PerformADDCombine(SDNode *N,
3905 TargetLowering::DAGCombinerInfo &DCI) {
3906 // added by evan in r37685 with no testcase.
3907 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003908
Chris Lattnerd1980a52009-03-12 06:52:53 +00003909 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3910 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3911 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3912 if (Result.getNode()) return Result;
3913 }
3914 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3915 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3916 if (Result.getNode()) return Result;
3917 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003918
Chris Lattnerd1980a52009-03-12 06:52:53 +00003919 return SDValue();
3920}
3921
3922/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3923static SDValue PerformSUBCombine(SDNode *N,
3924 TargetLowering::DAGCombinerInfo &DCI) {
3925 // added by evan in r37685 with no testcase.
3926 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003927
Chris Lattnerd1980a52009-03-12 06:52:53 +00003928 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3929 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3930 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3931 if (Result.getNode()) return Result;
3932 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003933
Chris Lattnerd1980a52009-03-12 06:52:53 +00003934 return SDValue();
3935}
3936
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003937static SDValue PerformMULCombine(SDNode *N,
3938 TargetLowering::DAGCombinerInfo &DCI,
3939 const ARMSubtarget *Subtarget) {
3940 SelectionDAG &DAG = DCI.DAG;
3941
3942 if (Subtarget->isThumb1Only())
3943 return SDValue();
3944
3945 if (DAG.getMachineFunction().
3946 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
3947 return SDValue();
3948
3949 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
3950 return SDValue();
3951
3952 EVT VT = N->getValueType(0);
3953 if (VT != MVT::i32)
3954 return SDValue();
3955
3956 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
3957 if (!C)
3958 return SDValue();
3959
3960 uint64_t MulAmt = C->getZExtValue();
3961 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
3962 ShiftAmt = ShiftAmt & (32 - 1);
3963 SDValue V = N->getOperand(0);
3964 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003965
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003966 SDValue Res;
3967 MulAmt >>= ShiftAmt;
3968 if (isPowerOf2_32(MulAmt - 1)) {
3969 // (mul x, 2^N + 1) => (add (shl x, N), x)
3970 Res = DAG.getNode(ISD::ADD, DL, VT,
3971 V, DAG.getNode(ISD::SHL, DL, VT,
3972 V, DAG.getConstant(Log2_32(MulAmt-1),
3973 MVT::i32)));
3974 } else if (isPowerOf2_32(MulAmt + 1)) {
3975 // (mul x, 2^N - 1) => (sub (shl x, N), x)
3976 Res = DAG.getNode(ISD::SUB, DL, VT,
3977 DAG.getNode(ISD::SHL, DL, VT,
3978 V, DAG.getConstant(Log2_32(MulAmt+1),
3979 MVT::i32)),
3980 V);
3981 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003982 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003983
3984 if (ShiftAmt != 0)
3985 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
3986 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003987
3988 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003989 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003990 return SDValue();
3991}
3992
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +00003993/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
3994/// ARMISD::VMOVRRD.
Jim Grosbache5165492009-11-09 00:11:35 +00003995static SDValue PerformVMOVRRDCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003996 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003997 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003998 SDValue InDouble = N->getOperand(0);
Jim Grosbache5165492009-11-09 00:11:35 +00003999 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004000 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004001 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004002}
4003
Bob Wilson5bafff32009-06-22 23:27:02 +00004004/// getVShiftImm - Check if this is a valid build_vector for the immediate
4005/// operand of a vector shift operation, where all the elements of the
4006/// build_vector must have the same constant integer value.
4007static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
4008 // Ignore bit_converts.
4009 while (Op.getOpcode() == ISD::BIT_CONVERT)
4010 Op = Op.getOperand(0);
4011 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
4012 APInt SplatBits, SplatUndef;
4013 unsigned SplatBitSize;
4014 bool HasAnyUndefs;
4015 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
4016 HasAnyUndefs, ElementBits) ||
4017 SplatBitSize > ElementBits)
4018 return false;
4019 Cnt = SplatBits.getSExtValue();
4020 return true;
4021}
4022
4023/// isVShiftLImm - Check if this is a valid build_vector for the immediate
4024/// operand of a vector shift left operation. That value must be in the range:
4025/// 0 <= Value < ElementBits for a left shift; or
4026/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00004027static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00004028 assert(VT.isVector() && "vector shift count is not a vector type");
4029 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
4030 if (! getVShiftImm(Op, ElementBits, Cnt))
4031 return false;
4032 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
4033}
4034
4035/// isVShiftRImm - Check if this is a valid build_vector for the immediate
4036/// operand of a vector shift right operation. For a shift opcode, the value
4037/// is positive, but for an intrinsic the value count must be negative. The
4038/// absolute value must be in the range:
4039/// 1 <= |Value| <= ElementBits for a right shift; or
4040/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00004041static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00004042 int64_t &Cnt) {
4043 assert(VT.isVector() && "vector shift count is not a vector type");
4044 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
4045 if (! getVShiftImm(Op, ElementBits, Cnt))
4046 return false;
4047 if (isIntrinsic)
4048 Cnt = -Cnt;
4049 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
4050}
4051
4052/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
4053static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
4054 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
4055 switch (IntNo) {
4056 default:
4057 // Don't do anything for most intrinsics.
4058 break;
4059
4060 // Vector shifts: check for immediate versions and lower them.
4061 // Note: This is done during DAG combining instead of DAG legalizing because
4062 // the build_vectors for 64-bit vector element shift counts are generally
4063 // not legal, and it is hard to see their values after they get legalized to
4064 // loads from a constant pool.
4065 case Intrinsic::arm_neon_vshifts:
4066 case Intrinsic::arm_neon_vshiftu:
4067 case Intrinsic::arm_neon_vshiftls:
4068 case Intrinsic::arm_neon_vshiftlu:
4069 case Intrinsic::arm_neon_vshiftn:
4070 case Intrinsic::arm_neon_vrshifts:
4071 case Intrinsic::arm_neon_vrshiftu:
4072 case Intrinsic::arm_neon_vrshiftn:
4073 case Intrinsic::arm_neon_vqshifts:
4074 case Intrinsic::arm_neon_vqshiftu:
4075 case Intrinsic::arm_neon_vqshiftsu:
4076 case Intrinsic::arm_neon_vqshiftns:
4077 case Intrinsic::arm_neon_vqshiftnu:
4078 case Intrinsic::arm_neon_vqshiftnsu:
4079 case Intrinsic::arm_neon_vqrshiftns:
4080 case Intrinsic::arm_neon_vqrshiftnu:
4081 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00004082 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004083 int64_t Cnt;
4084 unsigned VShiftOpc = 0;
4085
4086 switch (IntNo) {
4087 case Intrinsic::arm_neon_vshifts:
4088 case Intrinsic::arm_neon_vshiftu:
4089 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
4090 VShiftOpc = ARMISD::VSHL;
4091 break;
4092 }
4093 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
4094 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
4095 ARMISD::VSHRs : ARMISD::VSHRu);
4096 break;
4097 }
4098 return SDValue();
4099
4100 case Intrinsic::arm_neon_vshiftls:
4101 case Intrinsic::arm_neon_vshiftlu:
4102 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
4103 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00004104 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004105
4106 case Intrinsic::arm_neon_vrshifts:
4107 case Intrinsic::arm_neon_vrshiftu:
4108 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
4109 break;
4110 return SDValue();
4111
4112 case Intrinsic::arm_neon_vqshifts:
4113 case Intrinsic::arm_neon_vqshiftu:
4114 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
4115 break;
4116 return SDValue();
4117
4118 case Intrinsic::arm_neon_vqshiftsu:
4119 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
4120 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00004121 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004122
4123 case Intrinsic::arm_neon_vshiftn:
4124 case Intrinsic::arm_neon_vrshiftn:
4125 case Intrinsic::arm_neon_vqshiftns:
4126 case Intrinsic::arm_neon_vqshiftnu:
4127 case Intrinsic::arm_neon_vqshiftnsu:
4128 case Intrinsic::arm_neon_vqrshiftns:
4129 case Intrinsic::arm_neon_vqrshiftnu:
4130 case Intrinsic::arm_neon_vqrshiftnsu:
4131 // Narrowing shifts require an immediate right shift.
4132 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
4133 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00004134 llvm_unreachable("invalid shift count for narrowing vector shift "
4135 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004136
4137 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004138 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00004139 }
4140
4141 switch (IntNo) {
4142 case Intrinsic::arm_neon_vshifts:
4143 case Intrinsic::arm_neon_vshiftu:
4144 // Opcode already set above.
4145 break;
4146 case Intrinsic::arm_neon_vshiftls:
4147 case Intrinsic::arm_neon_vshiftlu:
4148 if (Cnt == VT.getVectorElementType().getSizeInBits())
4149 VShiftOpc = ARMISD::VSHLLi;
4150 else
4151 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
4152 ARMISD::VSHLLs : ARMISD::VSHLLu);
4153 break;
4154 case Intrinsic::arm_neon_vshiftn:
4155 VShiftOpc = ARMISD::VSHRN; break;
4156 case Intrinsic::arm_neon_vrshifts:
4157 VShiftOpc = ARMISD::VRSHRs; break;
4158 case Intrinsic::arm_neon_vrshiftu:
4159 VShiftOpc = ARMISD::VRSHRu; break;
4160 case Intrinsic::arm_neon_vrshiftn:
4161 VShiftOpc = ARMISD::VRSHRN; break;
4162 case Intrinsic::arm_neon_vqshifts:
4163 VShiftOpc = ARMISD::VQSHLs; break;
4164 case Intrinsic::arm_neon_vqshiftu:
4165 VShiftOpc = ARMISD::VQSHLu; break;
4166 case Intrinsic::arm_neon_vqshiftsu:
4167 VShiftOpc = ARMISD::VQSHLsu; break;
4168 case Intrinsic::arm_neon_vqshiftns:
4169 VShiftOpc = ARMISD::VQSHRNs; break;
4170 case Intrinsic::arm_neon_vqshiftnu:
4171 VShiftOpc = ARMISD::VQSHRNu; break;
4172 case Intrinsic::arm_neon_vqshiftnsu:
4173 VShiftOpc = ARMISD::VQSHRNsu; break;
4174 case Intrinsic::arm_neon_vqrshiftns:
4175 VShiftOpc = ARMISD::VQRSHRNs; break;
4176 case Intrinsic::arm_neon_vqrshiftnu:
4177 VShiftOpc = ARMISD::VQRSHRNu; break;
4178 case Intrinsic::arm_neon_vqrshiftnsu:
4179 VShiftOpc = ARMISD::VQRSHRNsu; break;
4180 }
4181
4182 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004183 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004184 }
4185
4186 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00004187 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004188 int64_t Cnt;
4189 unsigned VShiftOpc = 0;
4190
4191 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
4192 VShiftOpc = ARMISD::VSLI;
4193 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
4194 VShiftOpc = ARMISD::VSRI;
4195 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00004196 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004197 }
4198
4199 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
4200 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00004201 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004202 }
4203
4204 case Intrinsic::arm_neon_vqrshifts:
4205 case Intrinsic::arm_neon_vqrshiftu:
4206 // No immediate versions of these to check for.
4207 break;
4208 }
4209
4210 return SDValue();
4211}
4212
4213/// PerformShiftCombine - Checks for immediate versions of vector shifts and
4214/// lowers them. As with the vector shift intrinsics, this is done during DAG
4215/// combining instead of DAG legalizing because the build_vectors for 64-bit
4216/// vector element shift counts are generally not legal, and it is hard to see
4217/// their values after they get legalized to loads from a constant pool.
4218static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
4219 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00004220 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00004221
4222 // Nothing to be done for scalar shifts.
4223 if (! VT.isVector())
4224 return SDValue();
4225
4226 assert(ST->hasNEON() && "unexpected vector shift");
4227 int64_t Cnt;
4228
4229 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004230 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00004231
4232 case ISD::SHL:
4233 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
4234 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004235 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004236 break;
4237
4238 case ISD::SRA:
4239 case ISD::SRL:
4240 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
4241 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
4242 ARMISD::VSHRs : ARMISD::VSHRu);
4243 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004244 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004245 }
4246 }
4247 return SDValue();
4248}
4249
4250/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
4251/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
4252static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
4253 const ARMSubtarget *ST) {
4254 SDValue N0 = N->getOperand(0);
4255
4256 // Check for sign- and zero-extensions of vector extract operations of 8-
4257 // and 16-bit vector elements. NEON supports these directly. They are
4258 // handled during DAG combining because type legalization will promote them
4259 // to 32-bit types and it is messy to recognize the operations after that.
4260 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
4261 SDValue Vec = N0.getOperand(0);
4262 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004263 EVT VT = N->getValueType(0);
4264 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004265 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4266
Owen Anderson825b72b2009-08-11 20:47:22 +00004267 if (VT == MVT::i32 &&
4268 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00004269 TLI.isTypeLegal(Vec.getValueType())) {
4270
4271 unsigned Opc = 0;
4272 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004273 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00004274 case ISD::SIGN_EXTEND:
4275 Opc = ARMISD::VGETLANEs;
4276 break;
4277 case ISD::ZERO_EXTEND:
4278 case ISD::ANY_EXTEND:
4279 Opc = ARMISD::VGETLANEu;
4280 break;
4281 }
4282 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
4283 }
4284 }
4285
4286 return SDValue();
4287}
4288
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004289/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
4290/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
4291static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
4292 const ARMSubtarget *ST) {
4293 // If the target supports NEON, try to use vmax/vmin instructions for f32
4294 // selects like "x < y ? x : y". Unless the FiniteOnlyFPMath option is set,
4295 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
4296 // a NaN; only do the transformation when it matches that behavior.
4297
4298 // For now only do this when using NEON for FP operations; if using VFP, it
4299 // is not obvious that the benefit outweighs the cost of switching to the
4300 // NEON pipeline.
4301 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
4302 N->getValueType(0) != MVT::f32)
4303 return SDValue();
4304
4305 SDValue CondLHS = N->getOperand(0);
4306 SDValue CondRHS = N->getOperand(1);
4307 SDValue LHS = N->getOperand(2);
4308 SDValue RHS = N->getOperand(3);
4309 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
4310
4311 unsigned Opcode = 0;
4312 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00004313 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004314 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00004315 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004316 IsReversed = true ; // x CC y ? y : x
4317 } else {
4318 return SDValue();
4319 }
4320
Bob Wilsone742bb52010-02-24 22:15:53 +00004321 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004322 switch (CC) {
4323 default: break;
4324 case ISD::SETOLT:
4325 case ISD::SETOLE:
4326 case ISD::SETLT:
4327 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004328 case ISD::SETULT:
4329 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004330 // If LHS is NaN, an ordered comparison will be false and the result will
4331 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
4332 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4333 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
4334 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4335 break;
4336 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
4337 // will return -0, so vmin can only be used for unsafe math or if one of
4338 // the operands is known to be nonzero.
4339 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
4340 !UnsafeFPMath &&
4341 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4342 break;
4343 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004344 break;
4345
4346 case ISD::SETOGT:
4347 case ISD::SETOGE:
4348 case ISD::SETGT:
4349 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004350 case ISD::SETUGT:
4351 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004352 // If LHS is NaN, an ordered comparison will be false and the result will
4353 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
4354 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4355 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
4356 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4357 break;
4358 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
4359 // will return +0, so vmax can only be used for unsafe math or if one of
4360 // the operands is known to be nonzero.
4361 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
4362 !UnsafeFPMath &&
4363 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4364 break;
4365 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004366 break;
4367 }
4368
4369 if (!Opcode)
4370 return SDValue();
4371 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
4372}
4373
Dan Gohman475871a2008-07-27 21:46:04 +00004374SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004375 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004376 switch (N->getOpcode()) {
4377 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004378 case ISD::ADD: return PerformADDCombine(N, DCI);
4379 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004380 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbache5165492009-11-09 00:11:35 +00004381 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004382 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004383 case ISD::SHL:
4384 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004385 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004386 case ISD::SIGN_EXTEND:
4387 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004388 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
4389 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004390 }
Dan Gohman475871a2008-07-27 21:46:04 +00004391 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004392}
4393
Bill Wendlingaf566342009-08-15 21:21:19 +00004394bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
4395 if (!Subtarget->hasV6Ops())
4396 // Pre-v6 does not support unaligned mem access.
4397 return false;
Anton Korobeynikov90cfc132010-01-30 14:08:12 +00004398 else {
4399 // v6+ may or may not support unaligned mem access depending on the system
4400 // configuration.
4401 // FIXME: This is pretty conservative. Should we provide cmdline option to
4402 // control the behaviour?
Bill Wendlingaf566342009-08-15 21:21:19 +00004403 if (!Subtarget->isTargetDarwin())
4404 return false;
4405 }
4406
4407 switch (VT.getSimpleVT().SimpleTy) {
4408 default:
4409 return false;
4410 case MVT::i8:
4411 case MVT::i16:
4412 case MVT::i32:
4413 return true;
4414 // FIXME: VLD1 etc with standard alignment is legal.
4415 }
4416}
4417
Evan Chenge6c835f2009-08-14 20:09:37 +00004418static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
4419 if (V < 0)
4420 return false;
4421
4422 unsigned Scale = 1;
4423 switch (VT.getSimpleVT().SimpleTy) {
4424 default: return false;
4425 case MVT::i1:
4426 case MVT::i8:
4427 // Scale == 1;
4428 break;
4429 case MVT::i16:
4430 // Scale == 2;
4431 Scale = 2;
4432 break;
4433 case MVT::i32:
4434 // Scale == 4;
4435 Scale = 4;
4436 break;
4437 }
4438
4439 if ((V & (Scale - 1)) != 0)
4440 return false;
4441 V /= Scale;
4442 return V == (V & ((1LL << 5) - 1));
4443}
4444
4445static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
4446 const ARMSubtarget *Subtarget) {
4447 bool isNeg = false;
4448 if (V < 0) {
4449 isNeg = true;
4450 V = - V;
4451 }
4452
4453 switch (VT.getSimpleVT().SimpleTy) {
4454 default: return false;
4455 case MVT::i1:
4456 case MVT::i8:
4457 case MVT::i16:
4458 case MVT::i32:
4459 // + imm12 or - imm8
4460 if (isNeg)
4461 return V == (V & ((1LL << 8) - 1));
4462 return V == (V & ((1LL << 12) - 1));
4463 case MVT::f32:
4464 case MVT::f64:
4465 // Same as ARM mode. FIXME: NEON?
4466 if (!Subtarget->hasVFP2())
4467 return false;
4468 if ((V & 3) != 0)
4469 return false;
4470 V >>= 2;
4471 return V == (V & ((1LL << 8) - 1));
4472 }
4473}
4474
Evan Chengb01fad62007-03-12 23:30:29 +00004475/// isLegalAddressImmediate - Return true if the integer value can be used
4476/// as the offset of the target addressing mode for load / store of the
4477/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00004478static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004479 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00004480 if (V == 0)
4481 return true;
4482
Evan Cheng65011532009-03-09 19:15:00 +00004483 if (!VT.isSimple())
4484 return false;
4485
Evan Chenge6c835f2009-08-14 20:09:37 +00004486 if (Subtarget->isThumb1Only())
4487 return isLegalT1AddressImmediate(V, VT);
4488 else if (Subtarget->isThumb2())
4489 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00004490
Evan Chenge6c835f2009-08-14 20:09:37 +00004491 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00004492 if (V < 0)
4493 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00004494 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00004495 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004496 case MVT::i1:
4497 case MVT::i8:
4498 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00004499 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004500 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004501 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00004502 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004503 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004504 case MVT::f32:
4505 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00004506 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00004507 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00004508 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00004509 return false;
4510 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004511 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00004512 }
Evan Chenga8e29892007-01-19 07:51:42 +00004513}
4514
Evan Chenge6c835f2009-08-14 20:09:37 +00004515bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
4516 EVT VT) const {
4517 int Scale = AM.Scale;
4518 if (Scale < 0)
4519 return false;
4520
4521 switch (VT.getSimpleVT().SimpleTy) {
4522 default: return false;
4523 case MVT::i1:
4524 case MVT::i8:
4525 case MVT::i16:
4526 case MVT::i32:
4527 if (Scale == 1)
4528 return true;
4529 // r + r << imm
4530 Scale = Scale & ~1;
4531 return Scale == 2 || Scale == 4 || Scale == 8;
4532 case MVT::i64:
4533 // r + r
4534 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
4535 return true;
4536 return false;
4537 case MVT::isVoid:
4538 // Note, we allow "void" uses (basically, uses that aren't loads or
4539 // stores), because arm allows folding a scale into many arithmetic
4540 // operations. This should be made more precise and revisited later.
4541
4542 // Allow r << imm, but the imm has to be a multiple of two.
4543 if (Scale & 1) return false;
4544 return isPowerOf2_32(Scale);
4545 }
4546}
4547
Chris Lattner37caf8c2007-04-09 23:33:39 +00004548/// isLegalAddressingMode - Return true if the addressing mode represented
4549/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004550bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004551 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004552 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00004553 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00004554 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004555
Chris Lattner37caf8c2007-04-09 23:33:39 +00004556 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004557 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004558 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004559
Chris Lattner37caf8c2007-04-09 23:33:39 +00004560 switch (AM.Scale) {
4561 case 0: // no scale reg, must be "r+i" or "r", or "i".
4562 break;
4563 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00004564 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00004565 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004566 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00004567 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004568 // ARM doesn't support any R+R*scale+imm addr modes.
4569 if (AM.BaseOffs)
4570 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004571
Bob Wilson2c7dab12009-04-08 17:55:28 +00004572 if (!VT.isSimple())
4573 return false;
4574
Evan Chenge6c835f2009-08-14 20:09:37 +00004575 if (Subtarget->isThumb2())
4576 return isLegalT2ScaledAddressingMode(AM, VT);
4577
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004578 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00004579 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00004580 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004581 case MVT::i1:
4582 case MVT::i8:
4583 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004584 if (Scale < 0) Scale = -Scale;
4585 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004586 return true;
4587 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00004588 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00004589 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00004590 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004591 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004592 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004593 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00004594 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004595
Owen Anderson825b72b2009-08-11 20:47:22 +00004596 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004597 // Note, we allow "void" uses (basically, uses that aren't loads or
4598 // stores), because arm allows folding a scale into many arithmetic
4599 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004600
Chris Lattner37caf8c2007-04-09 23:33:39 +00004601 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00004602 if (Scale & 1) return false;
4603 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00004604 }
4605 break;
Evan Chengb01fad62007-03-12 23:30:29 +00004606 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00004607 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00004608}
4609
Evan Cheng77e47512009-11-11 19:05:52 +00004610/// isLegalICmpImmediate - Return true if the specified immediate is legal
4611/// icmp immediate, that is the target has icmp instructions which can compare
4612/// a register against the immediate without having to materialize the
4613/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00004614bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00004615 if (!Subtarget->isThumb())
4616 return ARM_AM::getSOImmVal(Imm) != -1;
4617 if (Subtarget->isThumb2())
4618 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00004619 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00004620}
4621
Owen Andersone50ed302009-08-10 22:56:29 +00004622static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004623 bool isSEXTLoad, SDValue &Base,
4624 SDValue &Offset, bool &isInc,
4625 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00004626 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4627 return false;
4628
Owen Anderson825b72b2009-08-11 20:47:22 +00004629 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00004630 // AddressingMode 3
4631 Base = Ptr->getOperand(0);
4632 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004633 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004634 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004635 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004636 isInc = false;
4637 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4638 return true;
4639 }
4640 }
4641 isInc = (Ptr->getOpcode() == ISD::ADD);
4642 Offset = Ptr->getOperand(1);
4643 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004644 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00004645 // AddressingMode 2
4646 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004647 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004648 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004649 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004650 isInc = false;
4651 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4652 Base = Ptr->getOperand(0);
4653 return true;
4654 }
4655 }
4656
4657 if (Ptr->getOpcode() == ISD::ADD) {
4658 isInc = true;
4659 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
4660 if (ShOpcVal != ARM_AM::no_shift) {
4661 Base = Ptr->getOperand(1);
4662 Offset = Ptr->getOperand(0);
4663 } else {
4664 Base = Ptr->getOperand(0);
4665 Offset = Ptr->getOperand(1);
4666 }
4667 return true;
4668 }
4669
4670 isInc = (Ptr->getOpcode() == ISD::ADD);
4671 Base = Ptr->getOperand(0);
4672 Offset = Ptr->getOperand(1);
4673 return true;
4674 }
4675
Jim Grosbache5165492009-11-09 00:11:35 +00004676 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00004677 return false;
4678}
4679
Owen Andersone50ed302009-08-10 22:56:29 +00004680static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004681 bool isSEXTLoad, SDValue &Base,
4682 SDValue &Offset, bool &isInc,
4683 SelectionDAG &DAG) {
4684 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4685 return false;
4686
4687 Base = Ptr->getOperand(0);
4688 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
4689 int RHSC = (int)RHS->getZExtValue();
4690 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
4691 assert(Ptr->getOpcode() == ISD::ADD);
4692 isInc = false;
4693 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4694 return true;
4695 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
4696 isInc = Ptr->getOpcode() == ISD::ADD;
4697 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
4698 return true;
4699 }
4700 }
4701
4702 return false;
4703}
4704
Evan Chenga8e29892007-01-19 07:51:42 +00004705/// getPreIndexedAddressParts - returns true by value, base pointer and
4706/// offset pointer and addressing mode by reference if the node's address
4707/// can be legally represented as pre-indexed load / store address.
4708bool
Dan Gohman475871a2008-07-27 21:46:04 +00004709ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
4710 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004711 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004712 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004713 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004714 return false;
4715
Owen Andersone50ed302009-08-10 22:56:29 +00004716 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004717 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004718 bool isSEXTLoad = false;
4719 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4720 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004721 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004722 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4723 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
4724 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004725 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004726 } else
4727 return false;
4728
4729 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004730 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004731 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004732 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
4733 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004734 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004735 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00004736 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00004737 if (!isLegal)
4738 return false;
4739
4740 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
4741 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004742}
4743
4744/// getPostIndexedAddressParts - returns true by value, base pointer and
4745/// offset pointer and addressing mode by reference if this node can be
4746/// combined with a load / store to form a post-indexed load / store.
4747bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00004748 SDValue &Base,
4749 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004750 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004751 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004752 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004753 return false;
4754
Owen Andersone50ed302009-08-10 22:56:29 +00004755 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004756 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004757 bool isSEXTLoad = false;
4758 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004759 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00004760 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00004761 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4762 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004763 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00004764 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00004765 } else
4766 return false;
4767
4768 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004769 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004770 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004771 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00004772 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004773 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004774 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
4775 isInc, DAG);
4776 if (!isLegal)
4777 return false;
4778
Evan Cheng28dad2a2010-05-18 21:31:17 +00004779 if (Ptr != Base) {
4780 // Swap base ptr and offset to catch more post-index load / store when
4781 // it's legal. In Thumb2 mode, offset must be an immediate.
4782 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
4783 !Subtarget->isThumb2())
4784 std::swap(Base, Offset);
4785
4786 // Post-indexed load / store update the base pointer.
4787 if (Ptr != Base)
4788 return false;
4789 }
4790
Evan Chenge88d5ce2009-07-02 07:28:31 +00004791 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
4792 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004793}
4794
Dan Gohman475871a2008-07-27 21:46:04 +00004795void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00004796 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004797 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004798 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00004799 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00004800 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004801 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004802 switch (Op.getOpcode()) {
4803 default: break;
4804 case ARMISD::CMOV: {
4805 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00004806 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004807 if (KnownZero == 0 && KnownOne == 0) return;
4808
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004809 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00004810 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
4811 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004812 KnownZero &= KnownZeroRHS;
4813 KnownOne &= KnownOneRHS;
4814 return;
4815 }
4816 }
4817}
4818
4819//===----------------------------------------------------------------------===//
4820// ARM Inline Assembly Support
4821//===----------------------------------------------------------------------===//
4822
4823/// getConstraintType - Given a constraint letter, return the type of
4824/// constraint it is for this target.
4825ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00004826ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
4827 if (Constraint.size() == 1) {
4828 switch (Constraint[0]) {
4829 default: break;
4830 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004831 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00004832 }
Evan Chenga8e29892007-01-19 07:51:42 +00004833 }
Chris Lattner4234f572007-03-25 02:14:49 +00004834 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00004835}
4836
Bob Wilson2dc4f542009-03-20 22:42:55 +00004837std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00004838ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004839 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004840 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004841 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00004842 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004843 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004844 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004845 return std::make_pair(0U, ARM::tGPRRegisterClass);
4846 else
4847 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004848 case 'r':
4849 return std::make_pair(0U, ARM::GPRRegisterClass);
4850 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004851 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004852 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00004853 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004854 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00004855 if (VT.getSizeInBits() == 128)
4856 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004857 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004858 }
4859 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00004860 if (StringRef("{cc}").equals_lower(Constraint))
4861 return std::make_pair(0U, ARM::CCRRegisterClass);
4862
Evan Chenga8e29892007-01-19 07:51:42 +00004863 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
4864}
4865
4866std::vector<unsigned> ARMTargetLowering::
4867getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004868 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004869 if (Constraint.size() != 1)
4870 return std::vector<unsigned>();
4871
4872 switch (Constraint[0]) { // GCC ARM Constraint Letters
4873 default: break;
4874 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004875 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4876 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4877 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004878 case 'r':
4879 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4880 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4881 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
4882 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004883 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004884 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004885 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
4886 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
4887 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
4888 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
4889 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
4890 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
4891 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
4892 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00004893 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004894 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
4895 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
4896 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
4897 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00004898 if (VT.getSizeInBits() == 128)
4899 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
4900 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004901 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004902 }
4903
4904 return std::vector<unsigned>();
4905}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004906
4907/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
4908/// vector. If it is invalid, don't add anything to Ops.
4909void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
4910 char Constraint,
4911 bool hasMemory,
4912 std::vector<SDValue>&Ops,
4913 SelectionDAG &DAG) const {
4914 SDValue Result(0, 0);
4915
4916 switch (Constraint) {
4917 default: break;
4918 case 'I': case 'J': case 'K': case 'L':
4919 case 'M': case 'N': case 'O':
4920 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
4921 if (!C)
4922 return;
4923
4924 int64_t CVal64 = C->getSExtValue();
4925 int CVal = (int) CVal64;
4926 // None of these constraints allow values larger than 32 bits. Check
4927 // that the value fits in an int.
4928 if (CVal != CVal64)
4929 return;
4930
4931 switch (Constraint) {
4932 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004933 if (Subtarget->isThumb1Only()) {
4934 // This must be a constant between 0 and 255, for ADD
4935 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004936 if (CVal >= 0 && CVal <= 255)
4937 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004938 } else if (Subtarget->isThumb2()) {
4939 // A constant that can be used as an immediate value in a
4940 // data-processing instruction.
4941 if (ARM_AM::getT2SOImmVal(CVal) != -1)
4942 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004943 } else {
4944 // A constant that can be used as an immediate value in a
4945 // data-processing instruction.
4946 if (ARM_AM::getSOImmVal(CVal) != -1)
4947 break;
4948 }
4949 return;
4950
4951 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004952 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004953 // This must be a constant between -255 and -1, for negated ADD
4954 // immediates. This can be used in GCC with an "n" modifier that
4955 // prints the negated value, for use with SUB instructions. It is
4956 // not useful otherwise but is implemented for compatibility.
4957 if (CVal >= -255 && CVal <= -1)
4958 break;
4959 } else {
4960 // This must be a constant between -4095 and 4095. It is not clear
4961 // what this constraint is intended for. Implemented for
4962 // compatibility with GCC.
4963 if (CVal >= -4095 && CVal <= 4095)
4964 break;
4965 }
4966 return;
4967
4968 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004969 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004970 // A 32-bit value where only one byte has a nonzero value. Exclude
4971 // zero to match GCC. This constraint is used by GCC internally for
4972 // constants that can be loaded with a move/shift combination.
4973 // It is not useful otherwise but is implemented for compatibility.
4974 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
4975 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004976 } else if (Subtarget->isThumb2()) {
4977 // A constant whose bitwise inverse can be used as an immediate
4978 // value in a data-processing instruction. This can be used in GCC
4979 // with a "B" modifier that prints the inverted value, for use with
4980 // BIC and MVN instructions. It is not useful otherwise but is
4981 // implemented for compatibility.
4982 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
4983 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004984 } else {
4985 // A constant whose bitwise inverse can be used as an immediate
4986 // value in a data-processing instruction. This can be used in GCC
4987 // with a "B" modifier that prints the inverted value, for use with
4988 // BIC and MVN instructions. It is not useful otherwise but is
4989 // implemented for compatibility.
4990 if (ARM_AM::getSOImmVal(~CVal) != -1)
4991 break;
4992 }
4993 return;
4994
4995 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004996 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004997 // This must be a constant between -7 and 7,
4998 // for 3-operand ADD/SUB immediate instructions.
4999 if (CVal >= -7 && CVal < 7)
5000 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005001 } else if (Subtarget->isThumb2()) {
5002 // A constant whose negation can be used as an immediate value in a
5003 // data-processing instruction. This can be used in GCC with an "n"
5004 // modifier that prints the negated value, for use with SUB
5005 // instructions. It is not useful otherwise but is implemented for
5006 // compatibility.
5007 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
5008 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005009 } else {
5010 // A constant whose negation can be used as an immediate value in a
5011 // data-processing instruction. This can be used in GCC with an "n"
5012 // modifier that prints the negated value, for use with SUB
5013 // instructions. It is not useful otherwise but is implemented for
5014 // compatibility.
5015 if (ARM_AM::getSOImmVal(-CVal) != -1)
5016 break;
5017 }
5018 return;
5019
5020 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005021 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005022 // This must be a multiple of 4 between 0 and 1020, for
5023 // ADD sp + immediate.
5024 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
5025 break;
5026 } else {
5027 // A power of two or a constant between 0 and 32. This is used in
5028 // GCC for the shift amount on shifted register operands, but it is
5029 // useful in general for any shift amounts.
5030 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
5031 break;
5032 }
5033 return;
5034
5035 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005036 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005037 // This must be a constant between 0 and 31, for shift amounts.
5038 if (CVal >= 0 && CVal <= 31)
5039 break;
5040 }
5041 return;
5042
5043 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005044 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005045 // This must be a multiple of 4 between -508 and 508, for
5046 // ADD/SUB sp = sp + immediate.
5047 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
5048 break;
5049 }
5050 return;
5051 }
5052 Result = DAG.getTargetConstant(CVal, Op.getValueType());
5053 break;
5054 }
5055
5056 if (Result.getNode()) {
5057 Ops.push_back(Result);
5058 return;
5059 }
5060 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
5061 Ops, DAG);
5062}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00005063
5064bool
5065ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
5066 // The ARM target isn't yet aware of offsets.
5067 return false;
5068}
Evan Cheng39382422009-10-28 01:44:26 +00005069
5070int ARM::getVFPf32Imm(const APFloat &FPImm) {
5071 APInt Imm = FPImm.bitcastToAPInt();
5072 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
5073 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
5074 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
5075
5076 // We can handle 4 bits of mantissa.
5077 // mantissa = (16+UInt(e:f:g:h))/16.
5078 if (Mantissa & 0x7ffff)
5079 return -1;
5080 Mantissa >>= 19;
5081 if ((Mantissa & 0xf) != Mantissa)
5082 return -1;
5083
5084 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
5085 if (Exp < -3 || Exp > 4)
5086 return -1;
5087 Exp = ((Exp+3) & 0x7) ^ 4;
5088
5089 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
5090}
5091
5092int ARM::getVFPf64Imm(const APFloat &FPImm) {
5093 APInt Imm = FPImm.bitcastToAPInt();
5094 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
5095 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
5096 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
5097
5098 // We can handle 4 bits of mantissa.
5099 // mantissa = (16+UInt(e:f:g:h))/16.
5100 if (Mantissa & 0xffffffffffffLL)
5101 return -1;
5102 Mantissa >>= 48;
5103 if ((Mantissa & 0xf) != Mantissa)
5104 return -1;
5105
5106 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
5107 if (Exp < -3 || Exp > 4)
5108 return -1;
5109 Exp = ((Exp+3) & 0x7) ^ 4;
5110
5111 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
5112}
5113
5114/// isFPImmLegal - Returns true if the target can instruction select the
5115/// specified FP immediate natively. If false, the legalizer will
5116/// materialize the FP immediate as a load from a constant pool.
5117bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
5118 if (!Subtarget->hasVFP3())
5119 return false;
5120 if (VT == MVT::f32)
5121 return ARM::getVFPf32Imm(Imm) != -1;
5122 if (VT == MVT::f64)
5123 return ARM::getVFPf64Imm(Imm) != -1;
5124 return false;
5125}