blob: 78ce2f0f8f55d295c227c8bf0f0271e6187533f2 [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
Evan Cheng46df4eb2010-06-16 07:35:02 +000065static cl::opt<bool>
66ARMInterworking("arm-interworking", cl::Hidden,
67 cl::desc("Enable / disable ARM interworking (for debugging only)"),
68 cl::init(true));
69
Owen Andersone50ed302009-08-10 22:56:29 +000070static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000071 CCValAssign::LocInfo &LocInfo,
72 ISD::ArgFlagsTy &ArgFlags,
73 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000074static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000075 CCValAssign::LocInfo &LocInfo,
76 ISD::ArgFlagsTy &ArgFlags,
77 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000078static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000079 CCValAssign::LocInfo &LocInfo,
80 ISD::ArgFlagsTy &ArgFlags,
81 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000082static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000083 CCValAssign::LocInfo &LocInfo,
84 ISD::ArgFlagsTy &ArgFlags,
85 CCState &State);
86
Owen Andersone50ed302009-08-10 22:56:29 +000087void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
88 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000089 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000090 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000091 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
92 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000093
Owen Anderson70671842009-08-10 20:18:46 +000094 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000095 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000096 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000097 }
98
Owen Andersone50ed302009-08-10 22:56:29 +000099 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000100 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +0000101 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000102 if (ElemTy == MVT::i8 || ElemTy == MVT::i16)
Owen Anderson70671842009-08-10 20:18:46 +0000103 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +0000104 if (ElemTy != MVT::i32) {
105 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
106 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
107 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
108 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
109 }
Owen Anderson70671842009-08-10 20:18:46 +0000110 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
111 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Evan Chengde8aa4e2010-05-05 18:28:36 +0000112 if (llvm::ModelWithRegSequence())
113 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
114 else
115 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Custom);
Anton Korobeynikov8e6c2b92009-08-21 12:40:35 +0000116 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
Bob Wilsond0910c42010-04-06 22:02:24 +0000117 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
118 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000119 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000120 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
121 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
122 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000123 }
124
125 // Promote all bit-wise operations.
126 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000127 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000128 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
129 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000130 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000131 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000132 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000133 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000134 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000135 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000136 }
Bob Wilson16330762009-09-16 00:17:28 +0000137
138 // Neon does not support vector divide/remainder operations.
139 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
140 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
141 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
142 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
143 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
144 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000145}
146
Owen Andersone50ed302009-08-10 22:56:29 +0000147void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000148 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000149 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000150}
151
Owen Andersone50ed302009-08-10 22:56:29 +0000152void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000153 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000154 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000155}
156
Chris Lattnerf0144122009-07-28 03:13:23 +0000157static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
158 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000159 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000160
Chris Lattner80ec2792009-08-02 00:34:36 +0000161 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000162}
163
Evan Chenga8e29892007-01-19 07:51:42 +0000164ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000165 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000166 Subtarget = &TM.getSubtarget<ARMSubtarget>();
167
Evan Chengb1df8f22007-04-27 08:15:43 +0000168 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000169 // Uses VFP for Thumb libfuncs if available.
170 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
171 // Single-precision floating-point arithmetic.
172 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
173 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
174 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
175 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000176
Evan Chengb1df8f22007-04-27 08:15:43 +0000177 // Double-precision floating-point arithmetic.
178 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
179 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
180 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
181 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000182
Evan Chengb1df8f22007-04-27 08:15:43 +0000183 // Single-precision comparisons.
184 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
185 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
186 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
187 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
188 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
189 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
190 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
191 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000192
Evan Chengb1df8f22007-04-27 08:15:43 +0000193 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
194 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
195 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
196 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
197 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
198 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
199 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
200 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000201
Evan Chengb1df8f22007-04-27 08:15:43 +0000202 // Double-precision comparisons.
203 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
204 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
205 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
206 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
207 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
208 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
209 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
210 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000211
Evan Chengb1df8f22007-04-27 08:15:43 +0000212 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
214 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
215 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
216 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
217 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
218 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
219 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000220
Evan Chengb1df8f22007-04-27 08:15:43 +0000221 // Floating-point to integer conversions.
222 // i64 conversions are done via library routines even when generating VFP
223 // instructions, so use the same ones.
224 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
225 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
226 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
227 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000228
Evan Chengb1df8f22007-04-27 08:15:43 +0000229 // Conversions between floating types.
230 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
231 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
232
233 // Integer to floating-point conversions.
234 // i64 conversions are done via library routines even when generating VFP
235 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000236 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
237 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000238 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
239 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
240 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
241 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
242 }
Evan Chenga8e29892007-01-19 07:51:42 +0000243 }
244
Bob Wilson2f954612009-05-22 17:38:41 +0000245 // These libcalls are not available in 32-bit.
246 setLibcallName(RTLIB::SHL_I128, 0);
247 setLibcallName(RTLIB::SRL_I128, 0);
248 setLibcallName(RTLIB::SRA_I128, 0);
249
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000250 // Libcalls should use the AAPCS base standard ABI, even if hard float
251 // is in effect, as per the ARM RTABI specification, section 4.1.2.
252 if (Subtarget->isAAPCS_ABI()) {
253 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
254 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
255 CallingConv::ARM_AAPCS);
256 }
257 }
258
David Goodwinf1daf7d2009-07-08 23:10:31 +0000259 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000260 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000261 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000263 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000264 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
265 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000266
Owen Anderson825b72b2009-08-11 20:47:22 +0000267 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000268 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000269
270 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000271 addDRTypeForNEON(MVT::v2f32);
272 addDRTypeForNEON(MVT::v8i8);
273 addDRTypeForNEON(MVT::v4i16);
274 addDRTypeForNEON(MVT::v2i32);
275 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000276
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 addQRTypeForNEON(MVT::v4f32);
278 addQRTypeForNEON(MVT::v2f64);
279 addQRTypeForNEON(MVT::v16i8);
280 addQRTypeForNEON(MVT::v8i16);
281 addQRTypeForNEON(MVT::v4i32);
282 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000283
Bob Wilson74dc72e2009-09-15 23:55:57 +0000284 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
285 // neither Neon nor VFP support any arithmetic operations on it.
286 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
287 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
288 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
289 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
290 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
291 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
292 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
293 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
294 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
295 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
296 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
297 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
298 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
299 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
300 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
301 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
302 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
303 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
304 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
305 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
306 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
307 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
308 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
309 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
310
Bob Wilson642b3292009-09-16 00:32:15 +0000311 // Neon does not support some operations on v1i64 and v2i64 types.
312 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
313 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
314 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
315 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
316
Bob Wilson5bafff32009-06-22 23:27:02 +0000317 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
318 setTargetDAGCombine(ISD::SHL);
319 setTargetDAGCombine(ISD::SRL);
320 setTargetDAGCombine(ISD::SRA);
321 setTargetDAGCombine(ISD::SIGN_EXTEND);
322 setTargetDAGCombine(ISD::ZERO_EXTEND);
323 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000324 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson5bafff32009-06-22 23:27:02 +0000325 }
326
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000327 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000328
329 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000330 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000331
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000332 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000333 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000334
Evan Chenga8e29892007-01-19 07:51:42 +0000335 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000336 if (!Subtarget->isThumb1Only()) {
337 for (unsigned im = (unsigned)ISD::PRE_INC;
338 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000339 setIndexedLoadAction(im, MVT::i1, Legal);
340 setIndexedLoadAction(im, MVT::i8, Legal);
341 setIndexedLoadAction(im, MVT::i16, Legal);
342 setIndexedLoadAction(im, MVT::i32, Legal);
343 setIndexedStoreAction(im, MVT::i1, Legal);
344 setIndexedStoreAction(im, MVT::i8, Legal);
345 setIndexedStoreAction(im, MVT::i16, Legal);
346 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000347 }
Evan Chenga8e29892007-01-19 07:51:42 +0000348 }
349
350 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000351 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000352 setOperationAction(ISD::MUL, MVT::i64, Expand);
353 setOperationAction(ISD::MULHU, MVT::i32, Expand);
354 setOperationAction(ISD::MULHS, MVT::i32, Expand);
355 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
356 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000357 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000358 setOperationAction(ISD::MUL, MVT::i64, Expand);
359 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000360 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000361 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000362 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000363 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000364 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000365 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000366 setOperationAction(ISD::SRL, MVT::i64, Custom);
367 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000368
369 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000370 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000371 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000373 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000374 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000375
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000376 // Only ARMv6 has BSWAP.
377 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000378 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000379
Evan Chenga8e29892007-01-19 07:51:42 +0000380 // These are expanded into libcalls.
Jim Grosbach29402132010-05-05 23:44:43 +0000381 if (!Subtarget->hasDivide()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000382 // v7M has a hardware divider
383 setOperationAction(ISD::SDIV, MVT::i32, Expand);
384 setOperationAction(ISD::UDIV, MVT::i32, Expand);
385 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000386 setOperationAction(ISD::SREM, MVT::i32, Expand);
387 setOperationAction(ISD::UREM, MVT::i32, Expand);
388 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
389 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000390
Owen Anderson825b72b2009-08-11 20:47:22 +0000391 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
392 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
393 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
394 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000395 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000396
Evan Chengfb3611d2010-05-11 07:26:32 +0000397 setOperationAction(ISD::TRAP, MVT::Other, Legal);
398
Evan Chenga8e29892007-01-19 07:51:42 +0000399 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000400 setOperationAction(ISD::VASTART, MVT::Other, Custom);
401 setOperationAction(ISD::VAARG, MVT::Other, Expand);
402 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
403 setOperationAction(ISD::VAEND, MVT::Other, Expand);
404 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
405 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000406 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
407 // FIXME: Shouldn't need this, since no register is used, but the legalizer
408 // doesn't yet know how to not do that for SjLj.
409 setExceptionSelectorRegister(ARM::R0);
Evan Cheng3a1588a2010-04-15 22:20:34 +0000410 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Jim Grosbach3728e962009-12-10 00:11:09 +0000411 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000412
Jim Grosbach4b77f6a2010-05-07 18:34:55 +0000413 // If the subtarget does not have extract instructions, sign_extend_inreg
414 // needs to be expanded. Extract is available in ARM mode on v6 and up,
415 // and on most Thumb2 implementations.
416 if ((!Subtarget->isThumb() && !Subtarget->hasV6Ops())
417 || (Subtarget->isThumb2() && !Subtarget->hasT2ExtractPack())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000418 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
419 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000420 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000421 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000422
David Goodwinf1daf7d2009-07-08 23:10:31 +0000423 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000424 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
425 // iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000427
428 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000429 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000430
Owen Anderson825b72b2009-08-11 20:47:22 +0000431 setOperationAction(ISD::SETCC, MVT::i32, Expand);
432 setOperationAction(ISD::SETCC, MVT::f32, Expand);
433 setOperationAction(ISD::SETCC, MVT::f64, Expand);
434 setOperationAction(ISD::SELECT, MVT::i32, Expand);
435 setOperationAction(ISD::SELECT, MVT::f32, Expand);
436 setOperationAction(ISD::SELECT, MVT::f64, Expand);
437 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
438 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
439 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000440
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
442 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
443 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
444 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
445 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000446
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000447 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000448 setOperationAction(ISD::FSIN, MVT::f64, Expand);
449 setOperationAction(ISD::FSIN, MVT::f32, Expand);
450 setOperationAction(ISD::FCOS, MVT::f32, Expand);
451 setOperationAction(ISD::FCOS, MVT::f64, Expand);
452 setOperationAction(ISD::FREM, MVT::f64, Expand);
453 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000454 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000455 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
456 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000457 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000458 setOperationAction(ISD::FPOW, MVT::f64, Expand);
459 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000460
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000461 // Various VFP goodness
462 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000463 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
464 if (Subtarget->hasVFP2()) {
465 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
466 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
467 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
468 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
469 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000470 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000471 if (!Subtarget->hasFP16()) {
472 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
473 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000474 }
Evan Cheng110cf482008-04-01 01:50:16 +0000475 }
Evan Chenga8e29892007-01-19 07:51:42 +0000476
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000477 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000478 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000479 setTargetDAGCombine(ISD::ADD);
480 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000481 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000482
Evan Chenga8e29892007-01-19 07:51:42 +0000483 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000484
Evan Chengf7d87ee2010-05-21 00:43:17 +0000485 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
486 setSchedulingPreference(Sched::RegPressure);
487 else
488 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000489
Evan Chengbc9b7542009-08-15 07:59:10 +0000490 // FIXME: If-converter should use instruction latency to determine
491 // profitability rather than relying on fixed limits.
492 if (Subtarget->getCPUString() == "generic") {
493 // Generic (and overly aggressive) if-conversion limits.
494 setIfCvtBlockSizeLimit(10);
495 setIfCvtDupBlockSizeLimit(2);
Jim Grosbach35075a72010-03-24 16:15:14 +0000496 } else if (Subtarget->hasV7Ops()) {
Jim Grosbachfceabef2010-03-24 00:03:13 +0000497 setIfCvtBlockSizeLimit(3);
498 setIfCvtDupBlockSizeLimit(1);
Evan Chengbc9b7542009-08-15 07:59:10 +0000499 } else if (Subtarget->hasV6Ops()) {
500 setIfCvtBlockSizeLimit(2);
501 setIfCvtDupBlockSizeLimit(1);
502 } else {
503 setIfCvtBlockSizeLimit(3);
504 setIfCvtDupBlockSizeLimit(2);
Evan Cheng8557c2b2009-06-19 01:51:50 +0000505 }
506
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000507 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Bob Wilsone6abdff2009-05-18 20:55:32 +0000508 // Do not enable CodePlacementOpt for now: it currently runs after the
509 // ARMConstantIslandPass and messes up branch relaxation and placement
510 // of constant islands.
511 // benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000512}
513
Evan Chenga8e29892007-01-19 07:51:42 +0000514const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
515 switch (Opcode) {
516 default: return 0;
517 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000518 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
519 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000520 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000521 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
522 case ARMISD::tCALL: return "ARMISD::tCALL";
523 case ARMISD::BRCOND: return "ARMISD::BRCOND";
524 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000525 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000526 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
527 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
528 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000529 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000530 case ARMISD::CMPFP: return "ARMISD::CMPFP";
531 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
532 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
533 case ARMISD::CMOV: return "ARMISD::CMOV";
534 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000535
Jim Grosbach3482c802010-01-18 19:58:49 +0000536 case ARMISD::RBIT: return "ARMISD::RBIT";
537
Bob Wilson76a312b2010-03-19 22:51:32 +0000538 case ARMISD::FTOSI: return "ARMISD::FTOSI";
539 case ARMISD::FTOUI: return "ARMISD::FTOUI";
540 case ARMISD::SITOF: return "ARMISD::SITOF";
541 case ARMISD::UITOF: return "ARMISD::UITOF";
542
Evan Chenga8e29892007-01-19 07:51:42 +0000543 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
544 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
545 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000546
Jim Grosbache5165492009-11-09 00:11:35 +0000547 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
548 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000549
Evan Chengc5942082009-10-28 06:55:03 +0000550 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
551 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
552
Dale Johannesen51e28e62010-06-03 21:09:53 +0000553 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
554
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000555 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000556
Evan Cheng86198642009-08-07 00:34:42 +0000557 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
558
Jim Grosbach3728e962009-12-10 00:11:09 +0000559 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
560 case ARMISD::SYNCBARRIER: return "ARMISD::SYNCBARRIER";
561
Bob Wilson5bafff32009-06-22 23:27:02 +0000562 case ARMISD::VCEQ: return "ARMISD::VCEQ";
563 case ARMISD::VCGE: return "ARMISD::VCGE";
564 case ARMISD::VCGEU: return "ARMISD::VCGEU";
565 case ARMISD::VCGT: return "ARMISD::VCGT";
566 case ARMISD::VCGTU: return "ARMISD::VCGTU";
567 case ARMISD::VTST: return "ARMISD::VTST";
568
569 case ARMISD::VSHL: return "ARMISD::VSHL";
570 case ARMISD::VSHRs: return "ARMISD::VSHRs";
571 case ARMISD::VSHRu: return "ARMISD::VSHRu";
572 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
573 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
574 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
575 case ARMISD::VSHRN: return "ARMISD::VSHRN";
576 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
577 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
578 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
579 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
580 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
581 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
582 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
583 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
584 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
585 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
586 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
587 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
588 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
589 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000590 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000591 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000592 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000593 case ARMISD::VREV64: return "ARMISD::VREV64";
594 case ARMISD::VREV32: return "ARMISD::VREV32";
595 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000596 case ARMISD::VZIP: return "ARMISD::VZIP";
597 case ARMISD::VUZP: return "ARMISD::VUZP";
598 case ARMISD::VTRN: return "ARMISD::VTRN";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000599 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000600 case ARMISD::FMAX: return "ARMISD::FMAX";
601 case ARMISD::FMIN: return "ARMISD::FMIN";
Evan Chenga8e29892007-01-19 07:51:42 +0000602 }
603}
604
Evan Cheng06b666c2010-05-15 02:18:07 +0000605/// getRegClassFor - Return the register class that should be used for the
606/// specified value type.
607TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
608 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
609 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
610 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000611 if (Subtarget->hasNEON()) {
612 if (VT == MVT::v4i64)
613 return ARM::QQPRRegisterClass;
614 else if (VT == MVT::v8i64)
615 return ARM::QQQQPRRegisterClass;
616 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000617 return TargetLowering::getRegClassFor(VT);
618}
619
Bill Wendlingb4202b82009-07-01 18:50:55 +0000620/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000621unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Evan Cheng048e36f2009-10-02 06:57:25 +0000622 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 0 : 1;
Bill Wendling20c568f2009-06-30 22:38:32 +0000623}
624
Evan Cheng1cc39842010-05-20 23:26:43 +0000625Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000626 unsigned NumVals = N->getNumValues();
627 if (!NumVals)
628 return Sched::RegPressure;
629
630 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000631 EVT VT = N->getValueType(i);
632 if (VT.isFloatingPoint() || VT.isVector())
633 return Sched::Latency;
634 }
Evan Chengc10f5432010-05-28 23:25:23 +0000635
636 if (!N->isMachineOpcode())
637 return Sched::RegPressure;
638
639 // Load are scheduled for latency even if there instruction itinerary
640 // is not available.
641 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
642 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
643 if (TID.mayLoad())
644 return Sched::Latency;
645
646 const InstrItineraryData &Itins = getTargetMachine().getInstrItineraryData();
647 if (!Itins.isEmpty() && Itins.getStageLatency(TID.getSchedClass()) > 2)
648 return Sched::Latency;
Evan Cheng1cc39842010-05-20 23:26:43 +0000649 return Sched::RegPressure;
650}
651
Evan Chenga8e29892007-01-19 07:51:42 +0000652//===----------------------------------------------------------------------===//
653// Lowering Code
654//===----------------------------------------------------------------------===//
655
Evan Chenga8e29892007-01-19 07:51:42 +0000656/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
657static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
658 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000659 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000660 case ISD::SETNE: return ARMCC::NE;
661 case ISD::SETEQ: return ARMCC::EQ;
662 case ISD::SETGT: return ARMCC::GT;
663 case ISD::SETGE: return ARMCC::GE;
664 case ISD::SETLT: return ARMCC::LT;
665 case ISD::SETLE: return ARMCC::LE;
666 case ISD::SETUGT: return ARMCC::HI;
667 case ISD::SETUGE: return ARMCC::HS;
668 case ISD::SETULT: return ARMCC::LO;
669 case ISD::SETULE: return ARMCC::LS;
670 }
671}
672
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000673/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
674static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000675 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000676 CondCode2 = ARMCC::AL;
677 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000678 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000679 case ISD::SETEQ:
680 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
681 case ISD::SETGT:
682 case ISD::SETOGT: CondCode = ARMCC::GT; break;
683 case ISD::SETGE:
684 case ISD::SETOGE: CondCode = ARMCC::GE; break;
685 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000686 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000687 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
688 case ISD::SETO: CondCode = ARMCC::VC; break;
689 case ISD::SETUO: CondCode = ARMCC::VS; break;
690 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
691 case ISD::SETUGT: CondCode = ARMCC::HI; break;
692 case ISD::SETUGE: CondCode = ARMCC::PL; break;
693 case ISD::SETLT:
694 case ISD::SETULT: CondCode = ARMCC::LT; break;
695 case ISD::SETLE:
696 case ISD::SETULE: CondCode = ARMCC::LE; break;
697 case ISD::SETNE:
698 case ISD::SETUNE: CondCode = ARMCC::NE; break;
699 }
Evan Chenga8e29892007-01-19 07:51:42 +0000700}
701
Bob Wilson1f595bb2009-04-17 19:07:39 +0000702//===----------------------------------------------------------------------===//
703// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000704//===----------------------------------------------------------------------===//
705
706#include "ARMGenCallingConv.inc"
707
708// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000709static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000710 CCValAssign::LocInfo &LocInfo,
711 CCState &State, bool CanFail) {
712 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
713
714 // Try to get the first register.
715 if (unsigned Reg = State.AllocateReg(RegList, 4))
716 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
717 else {
718 // For the 2nd half of a v2f64, do not fail.
719 if (CanFail)
720 return false;
721
722 // Put the whole thing on the stack.
723 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
724 State.AllocateStack(8, 4),
725 LocVT, LocInfo));
726 return true;
727 }
728
729 // Try to get the second register.
730 if (unsigned Reg = State.AllocateReg(RegList, 4))
731 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
732 else
733 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
734 State.AllocateStack(4, 4),
735 LocVT, LocInfo));
736 return true;
737}
738
Owen Andersone50ed302009-08-10 22:56:29 +0000739static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000740 CCValAssign::LocInfo &LocInfo,
741 ISD::ArgFlagsTy &ArgFlags,
742 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000743 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
744 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000745 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000746 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
747 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000748 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000749}
750
751// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000752static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000753 CCValAssign::LocInfo &LocInfo,
754 CCState &State, bool CanFail) {
755 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
756 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
757
758 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
759 if (Reg == 0) {
760 // For the 2nd half of a v2f64, do not just fail.
761 if (CanFail)
762 return false;
763
764 // Put the whole thing on the stack.
765 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
766 State.AllocateStack(8, 8),
767 LocVT, LocInfo));
768 return true;
769 }
770
771 unsigned i;
772 for (i = 0; i < 2; ++i)
773 if (HiRegList[i] == Reg)
774 break;
775
776 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
777 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
778 LocVT, LocInfo));
779 return true;
780}
781
Owen Andersone50ed302009-08-10 22:56:29 +0000782static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000783 CCValAssign::LocInfo &LocInfo,
784 ISD::ArgFlagsTy &ArgFlags,
785 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000786 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
787 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000788 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000789 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
790 return false;
791 return true; // we handled it
792}
793
Owen Andersone50ed302009-08-10 22:56:29 +0000794static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000795 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000796 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
797 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
798
Bob Wilsone65586b2009-04-17 20:40:45 +0000799 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
800 if (Reg == 0)
801 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000802
Bob Wilsone65586b2009-04-17 20:40:45 +0000803 unsigned i;
804 for (i = 0; i < 2; ++i)
805 if (HiRegList[i] == Reg)
806 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000807
Bob Wilson5bafff32009-06-22 23:27:02 +0000808 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000809 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000810 LocVT, LocInfo));
811 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000812}
813
Owen Andersone50ed302009-08-10 22:56:29 +0000814static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000815 CCValAssign::LocInfo &LocInfo,
816 ISD::ArgFlagsTy &ArgFlags,
817 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000818 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
819 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000820 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000821 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000822 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000823}
824
Owen Andersone50ed302009-08-10 22:56:29 +0000825static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000826 CCValAssign::LocInfo &LocInfo,
827 ISD::ArgFlagsTy &ArgFlags,
828 CCState &State) {
829 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
830 State);
831}
832
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000833/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
834/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000835CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000836 bool Return,
837 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000838 switch (CC) {
839 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000840 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000841 case CallingConv::C:
842 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000843 // Use target triple & subtarget features to do actual dispatch.
844 if (Subtarget->isAAPCS_ABI()) {
845 if (Subtarget->hasVFP2() &&
846 FloatABIType == FloatABI::Hard && !isVarArg)
847 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
848 else
849 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
850 } else
851 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000852 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000853 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000854 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000855 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000856 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000857 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000858 }
859}
860
Dan Gohman98ca4f22009-08-05 01:29:28 +0000861/// LowerCallResult - Lower the result values of a call into the
862/// appropriate copies out of appropriate physical registers.
863SDValue
864ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000865 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000866 const SmallVectorImpl<ISD::InputArg> &Ins,
867 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000868 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000869
Bob Wilson1f595bb2009-04-17 19:07:39 +0000870 // Assign locations to each value returned by this call.
871 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000872 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000873 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000874 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000875 CCAssignFnForNode(CallConv, /* Return*/ true,
876 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000877
878 // Copy all of the result registers out of their specified physreg.
879 for (unsigned i = 0; i != RVLocs.size(); ++i) {
880 CCValAssign VA = RVLocs[i];
881
Bob Wilson80915242009-04-25 00:33:20 +0000882 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000883 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000884 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000885 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000886 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000887 Chain = Lo.getValue(1);
888 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000889 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000890 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000891 InFlag);
892 Chain = Hi.getValue(1);
893 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000894 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000895
Owen Anderson825b72b2009-08-11 20:47:22 +0000896 if (VA.getLocVT() == MVT::v2f64) {
897 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
898 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
899 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000900
901 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000902 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000903 Chain = Lo.getValue(1);
904 InFlag = Lo.getValue(2);
905 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000906 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000907 Chain = Hi.getValue(1);
908 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000909 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +0000910 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
911 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000912 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000913 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000914 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
915 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000916 Chain = Val.getValue(1);
917 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000918 }
Bob Wilson80915242009-04-25 00:33:20 +0000919
920 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000921 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000922 case CCValAssign::Full: break;
923 case CCValAssign::BCvt:
924 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
925 break;
926 }
927
Dan Gohman98ca4f22009-08-05 01:29:28 +0000928 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000929 }
930
Dan Gohman98ca4f22009-08-05 01:29:28 +0000931 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000932}
933
934/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
935/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000936/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000937/// a byval function parameter.
938/// Sometimes what we are copying is the end of a larger object, the part that
939/// does not fit in registers.
940static SDValue
941CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
942 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
943 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000944 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000945 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +0000946 /*isVolatile=*/false, /*AlwaysInline=*/false,
947 NULL, 0, NULL, 0);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000948}
949
Bob Wilsondee46d72009-04-17 20:35:10 +0000950/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000951SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000952ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
953 SDValue StackPtr, SDValue Arg,
954 DebugLoc dl, SelectionDAG &DAG,
955 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +0000956 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000957 unsigned LocMemOffset = VA.getLocMemOffset();
958 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
959 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
960 if (Flags.isByVal()) {
961 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
962 }
963 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene1b58cab2010-02-15 16:55:24 +0000964 PseudoSourceValue::getStack(), LocMemOffset,
965 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +0000966}
967
Dan Gohman98ca4f22009-08-05 01:29:28 +0000968void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000969 SDValue Chain, SDValue &Arg,
970 RegsToPassVector &RegsToPass,
971 CCValAssign &VA, CCValAssign &NextVA,
972 SDValue &StackPtr,
973 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +0000974 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +0000975
Jim Grosbache5165492009-11-09 00:11:35 +0000976 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000977 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000978 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
979
980 if (NextVA.isRegLoc())
981 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
982 else {
983 assert(NextVA.isMemLoc());
984 if (StackPtr.getNode() == 0)
985 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
986
Dan Gohman98ca4f22009-08-05 01:29:28 +0000987 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
988 dl, DAG, NextVA,
989 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000990 }
991}
992
Dan Gohman98ca4f22009-08-05 01:29:28 +0000993/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000994/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
995/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000996SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000997ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000998 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000999 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001000 const SmallVectorImpl<ISD::OutputArg> &Outs,
1001 const SmallVectorImpl<ISD::InputArg> &Ins,
1002 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001003 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001004 MachineFunction &MF = DAG.getMachineFunction();
1005 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1006 bool IsSibCall = false;
Dale Johannesen8fa8e7f2010-06-04 18:04:24 +00001007 // Temporarily disable tail calls so things don't break.
1008 if (!EnableARMTailCalls)
1009 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001010 if (isTailCall) {
1011 // Check if it's really possible to do a tail call.
1012 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1013 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
1014 Outs, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001015 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1016 // detected sibcalls.
1017 if (isTailCall) {
1018 ++NumTailCalls;
1019 IsSibCall = true;
1020 }
1021 }
Evan Chenga8e29892007-01-19 07:51:42 +00001022
Bob Wilson1f595bb2009-04-17 19:07:39 +00001023 // Analyze operands of the call, assigning locations to each operand.
1024 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001025 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1026 *DAG.getContext());
1027 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001028 CCAssignFnForNode(CallConv, /* Return*/ false,
1029 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001030
Bob Wilson1f595bb2009-04-17 19:07:39 +00001031 // Get a count of how many bytes are to be pushed on the stack.
1032 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001033
Dale Johannesen51e28e62010-06-03 21:09:53 +00001034 // For tail calls, memory operands are available in our caller's stack.
1035 if (IsSibCall)
1036 NumBytes = 0;
1037
Evan Chenga8e29892007-01-19 07:51:42 +00001038 // Adjust the stack pointer for the new arguments...
1039 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001040 if (!IsSibCall)
1041 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001042
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001043 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001044
Bob Wilson5bafff32009-06-22 23:27:02 +00001045 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001046 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001047
Bob Wilson1f595bb2009-04-17 19:07:39 +00001048 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001049 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001050 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1051 i != e;
1052 ++i, ++realArgIdx) {
1053 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001054 SDValue Arg = Outs[realArgIdx].Val;
1055 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +00001056
Bob Wilson1f595bb2009-04-17 19:07:39 +00001057 // Promote the value if needed.
1058 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001059 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001060 case CCValAssign::Full: break;
1061 case CCValAssign::SExt:
1062 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1063 break;
1064 case CCValAssign::ZExt:
1065 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1066 break;
1067 case CCValAssign::AExt:
1068 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1069 break;
1070 case CCValAssign::BCvt:
1071 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1072 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001073 }
1074
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001075 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001076 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001077 if (VA.getLocVT() == MVT::v2f64) {
1078 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1079 DAG.getConstant(0, MVT::i32));
1080 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1081 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001082
Dan Gohman98ca4f22009-08-05 01:29:28 +00001083 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001084 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1085
1086 VA = ArgLocs[++i]; // skip ahead to next loc
1087 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001088 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001089 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1090 } else {
1091 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001092
Dan Gohman98ca4f22009-08-05 01:29:28 +00001093 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1094 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001095 }
1096 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001097 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001098 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001099 }
1100 } else if (VA.isRegLoc()) {
1101 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1102 } else {
1103 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001104
Dan Gohman98ca4f22009-08-05 01:29:28 +00001105 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1106 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001107 }
Evan Chenga8e29892007-01-19 07:51:42 +00001108 }
1109
1110 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001111 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001112 &MemOpChains[0], MemOpChains.size());
1113
1114 // Build a sequence of copy-to-reg nodes chained together with token chain
1115 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001116 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001117 // Tail call byval lowering might overwrite argument registers so in case of
1118 // tail call optimization the copies to registers are lowered later.
1119 if (!isTailCall)
1120 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1121 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1122 RegsToPass[i].second, InFlag);
1123 InFlag = Chain.getValue(1);
1124 }
Evan Chenga8e29892007-01-19 07:51:42 +00001125
Dale Johannesen51e28e62010-06-03 21:09:53 +00001126 // For tail calls lower the arguments to the 'real' stack slot.
1127 if (isTailCall) {
1128 // Force all the incoming stack arguments to be loaded from the stack
1129 // before any new outgoing arguments are stored to the stack, because the
1130 // outgoing stack slots may alias the incoming argument stack slots, and
1131 // the alias isn't otherwise explicit. This is slightly more conservative
1132 // than necessary, because it means that each store effectively depends
1133 // on every argument instead of just those arguments it would clobber.
1134
1135 // Do not flag preceeding copytoreg stuff together with the following stuff.
1136 InFlag = SDValue();
1137 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1138 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1139 RegsToPass[i].second, InFlag);
1140 InFlag = Chain.getValue(1);
1141 }
1142 InFlag =SDValue();
1143 }
1144
Bill Wendling056292f2008-09-16 21:48:12 +00001145 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1146 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1147 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001148 bool isDirect = false;
1149 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001150 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001151 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001152
1153 if (EnableARMLongCalls) {
1154 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1155 && "long-calls with non-static relocation model!");
1156 // Handle a global address or an external symbol. If it's not one of
1157 // those, the target's already in a register, so we don't need to do
1158 // anything extra.
1159 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001160 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001161 // Create a constant pool entry for the callee address
1162 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1163 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1164 ARMPCLabelIndex,
1165 ARMCP::CPValue, 0);
1166 // Get the address of the callee into a register
1167 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1168 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1169 Callee = DAG.getLoad(getPointerTy(), dl,
1170 DAG.getEntryNode(), CPAddr,
1171 PseudoSourceValue::getConstantPool(), 0,
1172 false, false, 0);
1173 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1174 const char *Sym = S->getSymbol();
1175
1176 // Create a constant pool entry for the callee address
1177 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1178 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1179 Sym, ARMPCLabelIndex, 0);
1180 // Get the address of the callee into a register
1181 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1182 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1183 Callee = DAG.getLoad(getPointerTy(), dl,
1184 DAG.getEntryNode(), CPAddr,
1185 PseudoSourceValue::getConstantPool(), 0,
1186 false, false, 0);
1187 }
1188 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001189 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001190 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001191 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001192 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001193 getTargetMachine().getRelocationModel() != Reloc::Static;
1194 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001195 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001196 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001197 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001198 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001199 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001200 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001201 ARMPCLabelIndex,
1202 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001203 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001204 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001205 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001206 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001207 PseudoSourceValue::getConstantPool(), 0,
1208 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001209 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001210 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001211 getPointerTy(), Callee, PICLabel);
Jim Grosbache7b52522010-04-14 22:28:31 +00001212 } else
Evan Chengc60e76d2007-01-30 20:37:08 +00001213 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001214 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001215 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001216 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001217 getTargetMachine().getRelocationModel() != Reloc::Static;
1218 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001219 // tBX takes a register source operand.
1220 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001221 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001222 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001223 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001224 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001225 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001226 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001227 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001228 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001229 PseudoSourceValue::getConstantPool(), 0,
1230 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001231 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001232 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001233 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001234 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001235 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001236 }
1237
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001238 // FIXME: handle tail calls differently.
1239 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001240 if (Subtarget->isThumb()) {
1241 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001242 CallOpc = ARMISD::CALL_NOLINK;
1243 else
1244 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1245 } else {
1246 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001247 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1248 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001249 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001250 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001251 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001252 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001253 InFlag = Chain.getValue(1);
1254 }
1255
Dan Gohman475871a2008-07-27 21:46:04 +00001256 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001257 Ops.push_back(Chain);
1258 Ops.push_back(Callee);
1259
1260 // Add argument registers to the end of the list so that they are known live
1261 // into the call.
1262 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1263 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1264 RegsToPass[i].second.getValueType()));
1265
Gabor Greifba36cb52008-08-28 21:40:38 +00001266 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001267 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001268
1269 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001270 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001271 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001272
Duncan Sands4bdcb612008-07-02 17:40:58 +00001273 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001274 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001275 InFlag = Chain.getValue(1);
1276
Chris Lattnere563bbc2008-10-11 22:08:30 +00001277 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1278 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001279 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001280 InFlag = Chain.getValue(1);
1281
Bob Wilson1f595bb2009-04-17 19:07:39 +00001282 // Handle result values, copying them out of physregs into vregs that we
1283 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001284 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1285 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001286}
1287
Dale Johannesen51e28e62010-06-03 21:09:53 +00001288/// MatchingStackOffset - Return true if the given stack call argument is
1289/// already available in the same position (relatively) of the caller's
1290/// incoming argument stack.
1291static
1292bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1293 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1294 const ARMInstrInfo *TII) {
1295 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1296 int FI = INT_MAX;
1297 if (Arg.getOpcode() == ISD::CopyFromReg) {
1298 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
1299 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
1300 return false;
1301 MachineInstr *Def = MRI->getVRegDef(VR);
1302 if (!Def)
1303 return false;
1304 if (!Flags.isByVal()) {
1305 if (!TII->isLoadFromStackSlot(Def, FI))
1306 return false;
1307 } else {
1308// unsigned Opcode = Def->getOpcode();
1309// if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
1310// Def->getOperand(1).isFI()) {
1311// FI = Def->getOperand(1).getIndex();
1312// Bytes = Flags.getByValSize();
1313// } else
1314 return false;
1315 }
1316 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1317 if (Flags.isByVal())
1318 // ByVal argument is passed in as a pointer but it's now being
1319 // dereferenced. e.g.
1320 // define @foo(%struct.X* %A) {
1321 // tail call @bar(%struct.X* byval %A)
1322 // }
1323 return false;
1324 SDValue Ptr = Ld->getBasePtr();
1325 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1326 if (!FINode)
1327 return false;
1328 FI = FINode->getIndex();
1329 } else
1330 return false;
1331
1332 assert(FI != INT_MAX);
1333 if (!MFI->isFixedObjectIndex(FI))
1334 return false;
1335 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1336}
1337
1338/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1339/// for tail call optimization. Targets which want to do tail call
1340/// optimization should implement this function.
1341bool
1342ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1343 CallingConv::ID CalleeCC,
1344 bool isVarArg,
1345 bool isCalleeStructRet,
1346 bool isCallerStructRet,
1347 const SmallVectorImpl<ISD::OutputArg> &Outs,
1348 const SmallVectorImpl<ISD::InputArg> &Ins,
1349 SelectionDAG& DAG) const {
1350
Dale Johannesen51e28e62010-06-03 21:09:53 +00001351 const Function *CallerF = DAG.getMachineFunction().getFunction();
1352 CallingConv::ID CallerCC = CallerF->getCallingConv();
1353 bool CCMatch = CallerCC == CalleeCC;
1354
1355 // Look for obvious safe cases to perform tail call optimization that do not
1356 // require ABI changes. This is what gcc calls sibcall.
1357
1358 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
1359 // emit a special epilogue.
1360 // Not sure yet if this is true on ARM.
1361//?? if (RegInfo->needsStackRealignment(MF))
1362//?? return false;
1363
1364 // Do not sibcall optimize vararg calls unless the call site is not passing any
1365 // arguments.
1366 if (isVarArg && !Outs.empty())
1367 return false;
1368
1369 // Also avoid sibcall optimization if either caller or callee uses struct
1370 // return semantics.
1371 if (isCalleeStructRet || isCallerStructRet)
1372 return false;
1373
1374 // If the calling conventions do not match, then we'd better make sure the
1375 // results are returned in the same way as what the caller expects.
1376 if (!CCMatch) {
1377 SmallVector<CCValAssign, 16> RVLocs1;
1378 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
1379 RVLocs1, *DAG.getContext());
1380 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1381
1382 SmallVector<CCValAssign, 16> RVLocs2;
1383 CCState CCInfo2(CallerCC, false, getTargetMachine(),
1384 RVLocs2, *DAG.getContext());
1385 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1386
1387 if (RVLocs1.size() != RVLocs2.size())
1388 return false;
1389 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1390 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1391 return false;
1392 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1393 return false;
1394 if (RVLocs1[i].isRegLoc()) {
1395 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1396 return false;
1397 } else {
1398 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1399 return false;
1400 }
1401 }
1402 }
1403
1404 // If the callee takes no arguments then go on to check the results of the
1405 // call.
1406 if (!Outs.empty()) {
1407 // Check if stack adjustment is needed. For now, do not do this if any
1408 // argument is passed on the stack.
1409 SmallVector<CCValAssign, 16> ArgLocs;
1410 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
1411 ArgLocs, *DAG.getContext());
1412 CCInfo.AnalyzeCallOperands(Outs,
1413 CCAssignFnForNode(CalleeCC, false, isVarArg));
1414 if (CCInfo.getNextStackOffset()) {
1415 MachineFunction &MF = DAG.getMachineFunction();
1416
1417 // Check if the arguments are already laid out in the right way as
1418 // the caller's fixed stack objects.
1419 MachineFrameInfo *MFI = MF.getFrameInfo();
1420 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1421 const ARMInstrInfo *TII =
1422 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001423 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1424 i != e;
1425 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001426 CCValAssign &VA = ArgLocs[i];
1427 EVT RegVT = VA.getLocVT();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001428 SDValue Arg = Outs[realArgIdx].Val;
1429 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001430 if (VA.getLocInfo() == CCValAssign::Indirect)
1431 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001432 if (VA.needsCustom()) {
1433 // f64 and vector types are split into multiple registers or
1434 // register/stack-slot combinations. The types will not match
1435 // the registers; give up on memory f64 refs until we figure
1436 // out what to do about this.
1437 if (!VA.isRegLoc())
1438 return false;
1439 if (!ArgLocs[++i].isRegLoc())
1440 return false;
1441 if (RegVT == MVT::v2f64) {
1442 if (!ArgLocs[++i].isRegLoc())
1443 return false;
1444 if (!ArgLocs[++i].isRegLoc())
1445 return false;
1446 }
1447 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001448 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1449 MFI, MRI, TII))
1450 return false;
1451 }
1452 }
1453 }
1454 }
1455
1456 return true;
1457}
1458
Dan Gohman98ca4f22009-08-05 01:29:28 +00001459SDValue
1460ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001461 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001462 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmand858e902010-04-17 15:26:15 +00001463 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001464
Bob Wilsondee46d72009-04-17 20:35:10 +00001465 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001466 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001467
Bob Wilsondee46d72009-04-17 20:35:10 +00001468 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001469 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1470 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001471
Dan Gohman98ca4f22009-08-05 01:29:28 +00001472 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001473 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1474 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001475
1476 // If this is the first return lowered for this function, add
1477 // the regs to the liveout set for the function.
1478 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1479 for (unsigned i = 0; i != RVLocs.size(); ++i)
1480 if (RVLocs[i].isRegLoc())
1481 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001482 }
1483
Bob Wilson1f595bb2009-04-17 19:07:39 +00001484 SDValue Flag;
1485
1486 // Copy the result values into the output registers.
1487 for (unsigned i = 0, realRVLocIdx = 0;
1488 i != RVLocs.size();
1489 ++i, ++realRVLocIdx) {
1490 CCValAssign &VA = RVLocs[i];
1491 assert(VA.isRegLoc() && "Can only return in registers!");
1492
Dan Gohman98ca4f22009-08-05 01:29:28 +00001493 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001494
1495 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001496 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001497 case CCValAssign::Full: break;
1498 case CCValAssign::BCvt:
1499 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1500 break;
1501 }
1502
Bob Wilson1f595bb2009-04-17 19:07:39 +00001503 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001504 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001505 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001506 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1507 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001508 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001509 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001510
1511 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1512 Flag = Chain.getValue(1);
1513 VA = RVLocs[++i]; // skip ahead to next loc
1514 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1515 HalfGPRs.getValue(1), Flag);
1516 Flag = Chain.getValue(1);
1517 VA = RVLocs[++i]; // skip ahead to next loc
1518
1519 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001520 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1521 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001522 }
1523 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1524 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001525 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001526 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001527 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001528 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001529 VA = RVLocs[++i]; // skip ahead to next loc
1530 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1531 Flag);
1532 } else
1533 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1534
Bob Wilsondee46d72009-04-17 20:35:10 +00001535 // Guarantee that all emitted copies are
1536 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001537 Flag = Chain.getValue(1);
1538 }
1539
1540 SDValue result;
1541 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001542 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001543 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001544 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001545
1546 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001547}
1548
Bob Wilsonb62d2572009-11-03 00:02:05 +00001549// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1550// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1551// one of the above mentioned nodes. It has to be wrapped because otherwise
1552// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1553// be used to form addressing mode. These wrapped nodes will be selected
1554// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001555static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001556 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001557 // FIXME there is no actual debug info here
1558 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001559 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001560 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001561 if (CP->isMachineConstantPoolEntry())
1562 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1563 CP->getAlignment());
1564 else
1565 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1566 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001567 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001568}
1569
Dan Gohmand858e902010-04-17 15:26:15 +00001570SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1571 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001572 MachineFunction &MF = DAG.getMachineFunction();
1573 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1574 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001575 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001576 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001577 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001578 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1579 SDValue CPAddr;
1580 if (RelocM == Reloc::Static) {
1581 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1582 } else {
1583 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001584 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001585 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1586 ARMCP::CPBlockAddress,
1587 PCAdj);
1588 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1589 }
1590 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1591 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001592 PseudoSourceValue::getConstantPool(), 0,
1593 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001594 if (RelocM == Reloc::Static)
1595 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001596 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001597 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001598}
1599
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001600// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001601SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001602ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001603 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001604 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001605 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001606 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001607 MachineFunction &MF = DAG.getMachineFunction();
1608 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1609 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001610 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001611 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001612 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001613 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001614 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001615 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
David Greene1b58cab2010-02-15 16:55:24 +00001616 PseudoSourceValue::getConstantPool(), 0,
1617 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001618 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001619
Evan Chenge7e0d622009-11-06 22:24:13 +00001620 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001621 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001622
1623 // call __tls_get_addr.
1624 ArgListTy Args;
1625 ArgListEntry Entry;
1626 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001627 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001628 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001629 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001630 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001631 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1632 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001633 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001634 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001635 return CallResult.first;
1636}
1637
1638// Lower ISD::GlobalTLSAddress using the "initial exec" or
1639// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001640SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001641ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001642 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001643 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001644 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001645 SDValue Offset;
1646 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001647 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001648 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001649 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001650
Chris Lattner4fb63d02009-07-15 04:12:33 +00001651 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001652 MachineFunction &MF = DAG.getMachineFunction();
1653 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1654 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1655 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001656 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1657 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001658 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001659 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001660 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001661 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001662 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001663 PseudoSourceValue::getConstantPool(), 0,
1664 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001665 Chain = Offset.getValue(1);
1666
Evan Chenge7e0d622009-11-06 22:24:13 +00001667 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001668 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001669
Evan Cheng9eda6892009-10-31 03:39:36 +00001670 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001671 PseudoSourceValue::getConstantPool(), 0,
1672 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001673 } else {
1674 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001675 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001676 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001677 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001678 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001679 PseudoSourceValue::getConstantPool(), 0,
1680 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001681 }
1682
1683 // The address of the thread local variable is the add of the thread
1684 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001685 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001686}
1687
Dan Gohman475871a2008-07-27 21:46:04 +00001688SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001689ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001690 // TODO: implement the "local dynamic" model
1691 assert(Subtarget->isTargetELF() &&
1692 "TLS not implemented for non-ELF targets");
1693 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1694 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1695 // otherwise use the "Local Exec" TLS Model
1696 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1697 return LowerToTLSGeneralDynamicModel(GA, DAG);
1698 else
1699 return LowerToTLSExecModels(GA, DAG);
1700}
1701
Dan Gohman475871a2008-07-27 21:46:04 +00001702SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001703 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001704 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001705 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001706 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001707 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1708 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001709 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001710 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001711 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001712 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001713 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001714 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001715 CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001716 PseudoSourceValue::getConstantPool(), 0,
1717 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001718 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001719 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001720 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001721 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001722 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001723 PseudoSourceValue::getGOT(), 0,
1724 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001725 return Result;
1726 } else {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001727 // If we have T2 ops, we can materialize the address directly via movt/movw
1728 // pair. This is always cheaper.
1729 if (Subtarget->useMovt()) {
1730 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
1731 DAG.getTargetGlobalAddress(GV, PtrVT));
1732 } else {
1733 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1734 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1735 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001736 PseudoSourceValue::getConstantPool(), 0,
1737 false, false, 0);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001738 }
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001739 }
1740}
1741
Dan Gohman475871a2008-07-27 21:46:04 +00001742SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001743 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001744 MachineFunction &MF = DAG.getMachineFunction();
1745 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1746 unsigned ARMPCLabelIndex = 0;
Owen Andersone50ed302009-08-10 22:56:29 +00001747 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001748 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001749 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001750 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001751 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001752 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001753 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001754 else {
Evan Chenge7e0d622009-11-06 22:24:13 +00001755 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001756 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1757 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001758 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001759 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001760 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001761 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001762
Evan Cheng9eda6892009-10-31 03:39:36 +00001763 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001764 PseudoSourceValue::getConstantPool(), 0,
1765 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001766 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001767
1768 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001769 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001770 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001771 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001772
Evan Cheng63476a82009-09-03 07:04:02 +00001773 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Evan Cheng9eda6892009-10-31 03:39:36 +00001774 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001775 PseudoSourceValue::getGOT(), 0,
1776 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001777
1778 return Result;
1779}
1780
Dan Gohman475871a2008-07-27 21:46:04 +00001781SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001782 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001783 assert(Subtarget->isTargetELF() &&
1784 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00001785 MachineFunction &MF = DAG.getMachineFunction();
1786 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1787 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Andersone50ed302009-08-10 22:56:29 +00001788 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001789 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001790 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001791 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1792 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001793 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001794 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001795 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001796 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001797 PseudoSourceValue::getConstantPool(), 0,
1798 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001799 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001800 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001801}
1802
Jim Grosbach0e0da732009-05-12 23:59:14 +00001803SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00001804ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
1805 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00001806 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00001807 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
1808 Op.getOperand(1), Val);
1809}
1810
1811SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00001812ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
1813 DebugLoc dl = Op.getDebugLoc();
1814 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
1815 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
1816}
1817
1818SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00001819ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001820 const ARMSubtarget *Subtarget)
1821 const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001822 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001823 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001824 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001825 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001826 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001827 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001828 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1829 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001830 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001831 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00001832 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1833 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001834 EVT PtrVT = getPointerTy();
1835 DebugLoc dl = Op.getDebugLoc();
1836 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1837 SDValue CPAddr;
1838 unsigned PCAdj = (RelocM != Reloc::PIC_)
1839 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001840 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001841 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1842 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001843 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001844 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001845 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00001846 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001847 PseudoSourceValue::getConstantPool(), 0,
1848 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001849 SDValue Chain = Result.getValue(1);
1850
1851 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001852 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001853 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1854 }
1855 return Result;
1856 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001857 }
1858}
1859
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001860static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
1861 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00001862 DebugLoc dl = Op.getDebugLoc();
1863 SDValue Op5 = Op.getOperand(5);
1864 SDValue Res;
1865 unsigned isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue();
1866 if (isDeviceBarrier) {
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001867 if (Subtarget->hasV7Ops())
1868 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0));
1869 else
1870 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0),
1871 DAG.getConstant(0, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00001872 } else {
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001873 if (Subtarget->hasV7Ops())
1874 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
1875 else
1876 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
1877 DAG.getConstant(0, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00001878 }
1879 return Res;
1880}
1881
Dan Gohman1e93df62010-04-17 14:41:14 +00001882static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
1883 MachineFunction &MF = DAG.getMachineFunction();
1884 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
1885
Evan Chenga8e29892007-01-19 07:51:42 +00001886 // vastart just stores the address of the VarArgsFrameIndex slot into the
1887 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001888 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001889 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00001890 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001891 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
David Greene1b58cab2010-02-15 16:55:24 +00001892 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
1893 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001894}
1895
Dan Gohman475871a2008-07-27 21:46:04 +00001896SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001897ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1898 SelectionDAG &DAG) const {
Evan Cheng86198642009-08-07 00:34:42 +00001899 SDNode *Node = Op.getNode();
1900 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001901 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001902 SDValue Chain = Op.getOperand(0);
1903 SDValue Size = Op.getOperand(1);
1904 SDValue Align = Op.getOperand(2);
1905
1906 // Chain the dynamic stack allocation so that it doesn't modify the stack
1907 // pointer when other instructions are using the stack.
1908 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1909
1910 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1911 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1912 if (AlignVal > StackAlign)
1913 // Do this now since selection pass cannot introduce new target
1914 // independent node.
1915 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1916
1917 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1918 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1919 // do even more horrible hack later.
1920 MachineFunction &MF = DAG.getMachineFunction();
1921 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1922 if (AFI->isThumb1OnlyFunction()) {
1923 bool Negate = true;
1924 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1925 if (C) {
1926 uint32_t Val = C->getZExtValue();
1927 if (Val <= 508 && ((Val & 3) == 0))
1928 Negate = false;
1929 }
1930 if (Negate)
1931 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1932 }
1933
Owen Anderson825b72b2009-08-11 20:47:22 +00001934 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001935 SDValue Ops1[] = { Chain, Size, Align };
1936 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1937 Chain = Res.getValue(1);
1938 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1939 DAG.getIntPtrConstant(0, true), SDValue());
1940 SDValue Ops2[] = { Res, Chain };
1941 return DAG.getMergeValues(Ops2, 2, dl);
1942}
1943
1944SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001945ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1946 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001947 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001948 MachineFunction &MF = DAG.getMachineFunction();
1949 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1950
1951 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001952 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001953 RC = ARM::tGPRRegisterClass;
1954 else
1955 RC = ARM::GPRRegisterClass;
1956
1957 // Transform the arguments stored in physical registers into virtual ones.
Evan Cheng2457f2c2010-05-22 01:47:14 +00001958 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001959 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001960
1961 SDValue ArgValue2;
1962 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001963 MachineFrameInfo *MFI = MF.getFrameInfo();
Bob Wilson6a234f02010-04-13 22:03:22 +00001964 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true, false);
Bob Wilson5bafff32009-06-22 23:27:02 +00001965
1966 // Create load node to retrieve arguments from the stack.
1967 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001968 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00001969 PseudoSourceValue::getFixedStack(FI), 0,
1970 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001971 } else {
1972 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001973 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001974 }
1975
Jim Grosbache5165492009-11-09 00:11:35 +00001976 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001977}
1978
1979SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001980ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001981 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001982 const SmallVectorImpl<ISD::InputArg>
1983 &Ins,
1984 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001985 SmallVectorImpl<SDValue> &InVals)
1986 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001987
Bob Wilson1f595bb2009-04-17 19:07:39 +00001988 MachineFunction &MF = DAG.getMachineFunction();
1989 MachineFrameInfo *MFI = MF.getFrameInfo();
1990
Bob Wilson1f595bb2009-04-17 19:07:39 +00001991 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1992
1993 // Assign locations to all of the incoming arguments.
1994 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001995 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1996 *DAG.getContext());
1997 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001998 CCAssignFnForNode(CallConv, /* Return*/ false,
1999 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002000
2001 SmallVector<SDValue, 16> ArgValues;
2002
2003 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2004 CCValAssign &VA = ArgLocs[i];
2005
Bob Wilsondee46d72009-04-17 20:35:10 +00002006 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002007 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002008 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002009
Bob Wilson5bafff32009-06-22 23:27:02 +00002010 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002011 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002012 // f64 and vector types are split up into multiple registers or
2013 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002014 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002015 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002016 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002017 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002018 SDValue ArgValue2;
2019 if (VA.isMemLoc()) {
2020 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(),
2021 true, false);
2022 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2023 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
2024 PseudoSourceValue::getFixedStack(FI), 0,
2025 false, false, 0);
2026 } else {
2027 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2028 Chain, DAG, dl);
2029 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002030 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2031 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002032 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002033 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002034 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2035 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002036 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002037
Bob Wilson5bafff32009-06-22 23:27:02 +00002038 } else {
2039 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002040
Owen Anderson825b72b2009-08-11 20:47:22 +00002041 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002042 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002043 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002044 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002045 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002046 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002047 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002048 RC = (AFI->isThumb1OnlyFunction() ?
2049 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002050 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002051 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002052
2053 // Transform the arguments in physical registers into virtual ones.
2054 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002055 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002056 }
2057
2058 // If this is an 8 or 16-bit value, it is really passed promoted
2059 // to 32 bits. Insert an assert[sz]ext to capture this, then
2060 // truncate to the right size.
2061 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002062 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002063 case CCValAssign::Full: break;
2064 case CCValAssign::BCvt:
2065 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
2066 break;
2067 case CCValAssign::SExt:
2068 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2069 DAG.getValueType(VA.getValVT()));
2070 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2071 break;
2072 case CCValAssign::ZExt:
2073 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2074 DAG.getValueType(VA.getValVT()));
2075 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2076 break;
2077 }
2078
Dan Gohman98ca4f22009-08-05 01:29:28 +00002079 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002080
2081 } else { // VA.isRegLoc()
2082
2083 // sanity check
2084 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002085 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002086
2087 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00002088 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
2089 true, false);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002090
Bob Wilsondee46d72009-04-17 20:35:10 +00002091 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002092 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002093 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00002094 PseudoSourceValue::getFixedStack(FI), 0,
2095 false, false, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002096 }
2097 }
2098
2099 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00002100 if (isVarArg) {
2101 static const unsigned GPRArgRegs[] = {
2102 ARM::R0, ARM::R1, ARM::R2, ARM::R3
2103 };
2104
Bob Wilsondee46d72009-04-17 20:35:10 +00002105 unsigned NumGPRs = CCInfo.getFirstUnallocated
2106 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002107
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00002108 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
2109 unsigned VARegSize = (4 - NumGPRs) * 4;
2110 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00002111 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00002112 if (VARegSaveSize) {
2113 // If this function is vararg, store any remaining integer argument regs
2114 // to their spots on the stack so that they may be loaded by deferencing
2115 // the result of va_next.
2116 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00002117 AFI->setVarArgsFrameIndex(
2118 MFI->CreateFixedObject(VARegSaveSize,
2119 ArgOffset + VARegSaveSize - VARegSize,
2120 true, false));
2121 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2122 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00002123
Dan Gohman475871a2008-07-27 21:46:04 +00002124 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00002125 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002126 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002127 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00002128 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002129 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00002130 RC = ARM::GPRRegisterClass;
2131
Bob Wilson998e1252009-04-20 18:36:57 +00002132 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002133 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00002134 SDValue Store =
2135 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Jim Grosbach18f30e62010-06-02 21:53:11 +00002136 PseudoSourceValue::getFixedStack(AFI->getVarArgsFrameIndex()),
2137 0, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002138 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002139 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00002140 DAG.getConstant(4, getPointerTy()));
2141 }
2142 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002143 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002144 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00002145 } else
2146 // This will point to the next argument passed via stack.
Dan Gohman1e93df62010-04-17 14:41:14 +00002147 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset,
2148 true, false));
Evan Chenga8e29892007-01-19 07:51:42 +00002149 }
2150
Dan Gohman98ca4f22009-08-05 01:29:28 +00002151 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002152}
2153
2154/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002155static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002156 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002157 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002158 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002159 // Maybe this has already been legalized into the constant pool?
2160 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002161 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002162 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002163 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002164 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002165 }
2166 }
2167 return false;
2168}
2169
Evan Chenga8e29892007-01-19 07:51:42 +00002170/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2171/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002172SDValue
2173ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Dan Gohmand858e902010-04-17 15:26:15 +00002174 SDValue &ARMCC, SelectionDAG &DAG,
2175 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002176 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002177 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002178 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002179 // Constant does not fit, try adjusting it by one?
2180 switch (CC) {
2181 default: break;
2182 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002183 case ISD::SETGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00002184 if (isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002185 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002186 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002187 }
2188 break;
2189 case ISD::SETULT:
2190 case ISD::SETUGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00002191 if (C > 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002192 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002193 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002194 }
2195 break;
2196 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002197 case ISD::SETGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00002198 if (isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002199 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002200 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002201 }
2202 break;
2203 case ISD::SETULE:
2204 case ISD::SETUGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00002205 if (C < 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002206 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002207 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002208 }
2209 break;
2210 }
2211 }
2212 }
2213
2214 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002215 ARMISD::NodeType CompareType;
2216 switch (CondCode) {
2217 default:
2218 CompareType = ARMISD::CMP;
2219 break;
2220 case ARMCC::EQ:
2221 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002222 // Uses only Z Flag
2223 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002224 break;
2225 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002226 ARMCC = DAG.getConstant(CondCode, MVT::i32);
2227 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002228}
2229
2230/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00002231static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00002232 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00002233 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002234 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00002235 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002236 else
Owen Anderson825b72b2009-08-11 20:47:22 +00002237 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
2238 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002239}
2240
Dan Gohmand858e902010-04-17 15:26:15 +00002241SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002242 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002243 SDValue LHS = Op.getOperand(0);
2244 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002245 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002246 SDValue TrueVal = Op.getOperand(2);
2247 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002248 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002249
Owen Anderson825b72b2009-08-11 20:47:22 +00002250 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00002251 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002252 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00002253 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Dale Johannesende064702009-02-06 21:50:26 +00002254 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002255 }
2256
2257 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002258 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002259
Owen Anderson825b72b2009-08-11 20:47:22 +00002260 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
2261 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002262 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
2263 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00002264 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002265 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002266 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002267 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00002268 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002269 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00002270 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002271 }
2272 return Result;
2273}
2274
Dan Gohmand858e902010-04-17 15:26:15 +00002275SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002276 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002277 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002278 SDValue LHS = Op.getOperand(2);
2279 SDValue RHS = Op.getOperand(3);
2280 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002281 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002282
Owen Anderson825b72b2009-08-11 20:47:22 +00002283 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00002284 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002285 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00002286 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002287 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00002288 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002289 }
2290
Owen Anderson825b72b2009-08-11 20:47:22 +00002291 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00002292 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002293 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002294
Dale Johannesende064702009-02-06 21:50:26 +00002295 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002296 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
2297 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2298 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002299 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002300 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002301 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002302 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00002303 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002304 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002305 }
2306 return Res;
2307}
2308
Dan Gohmand858e902010-04-17 15:26:15 +00002309SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002310 SDValue Chain = Op.getOperand(0);
2311 SDValue Table = Op.getOperand(1);
2312 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002313 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002314
Owen Andersone50ed302009-08-10 22:56:29 +00002315 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002316 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2317 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002318 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002319 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002320 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002321 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2322 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002323 if (Subtarget->isThumb2()) {
2324 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2325 // which does another jump to the destination. This also makes it easier
2326 // to translate it to TBB / TBH later.
2327 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002328 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002329 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002330 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002331 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002332 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002333 PseudoSourceValue::getJumpTable(), 0,
2334 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002335 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002336 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002337 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002338 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002339 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002340 PseudoSourceValue::getJumpTable(), 0, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002341 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002342 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002343 }
Evan Chenga8e29892007-01-19 07:51:42 +00002344}
2345
Bob Wilson76a312b2010-03-19 22:51:32 +00002346static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2347 DebugLoc dl = Op.getDebugLoc();
2348 unsigned Opc;
2349
2350 switch (Op.getOpcode()) {
2351 default:
2352 assert(0 && "Invalid opcode!");
2353 case ISD::FP_TO_SINT:
2354 Opc = ARMISD::FTOSI;
2355 break;
2356 case ISD::FP_TO_UINT:
2357 Opc = ARMISD::FTOUI;
2358 break;
2359 }
2360 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
2361 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
2362}
2363
2364static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2365 EVT VT = Op.getValueType();
2366 DebugLoc dl = Op.getDebugLoc();
2367 unsigned Opc;
2368
2369 switch (Op.getOpcode()) {
2370 default:
2371 assert(0 && "Invalid opcode!");
2372 case ISD::SINT_TO_FP:
2373 Opc = ARMISD::SITOF;
2374 break;
2375 case ISD::UINT_TO_FP:
2376 Opc = ARMISD::UITOF;
2377 break;
2378 }
2379
2380 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
2381 return DAG.getNode(Opc, dl, VT, Op);
2382}
2383
Dan Gohman475871a2008-07-27 21:46:04 +00002384static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002385 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002386 SDValue Tmp0 = Op.getOperand(0);
2387 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002388 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002389 EVT VT = Op.getValueType();
2390 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00002391 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
2392 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002393 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
2394 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002395 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002396}
2397
Evan Cheng2457f2c2010-05-22 01:47:14 +00002398SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
2399 MachineFunction &MF = DAG.getMachineFunction();
2400 MachineFrameInfo *MFI = MF.getFrameInfo();
2401 MFI->setReturnAddressIsTaken(true);
2402
2403 EVT VT = Op.getValueType();
2404 DebugLoc dl = Op.getDebugLoc();
2405 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2406 if (Depth) {
2407 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
2408 SDValue Offset = DAG.getConstant(4, MVT::i32);
2409 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
2410 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
2411 NULL, 0, false, false, 0);
2412 }
2413
2414 // Return LR, which contains the return address. Mark it an implicit live-in.
Evan Chengc7cf10c2010-05-24 18:00:18 +00002415 unsigned Reg = MF.addLiveIn(ARM::LR, ARM::GPRRegisterClass);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002416 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
2417}
2418
Dan Gohmand858e902010-04-17 15:26:15 +00002419SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002420 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2421 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002422
Owen Andersone50ed302009-08-10 22:56:29 +00002423 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002424 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2425 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002426 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002427 ? ARM::R7 : ARM::R11;
2428 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2429 while (Depth--)
David Greene1b58cab2010-02-15 16:55:24 +00002430 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
2431 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002432 return FrameAddr;
2433}
2434
Bob Wilson9f3f0612010-04-17 05:30:19 +00002435/// ExpandBIT_CONVERT - If the target supports VFP, this function is called to
2436/// expand a bit convert where either the source or destination type is i64 to
2437/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2438/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2439/// vectors), since the legalizer won't know what to do with that.
Duncan Sands1607f052008-12-01 11:39:25 +00002440static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002441 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2442 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002443 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002444
Bob Wilson9f3f0612010-04-17 05:30:19 +00002445 // This function is only supposed to be called for i64 types, either as the
2446 // source or destination of the bit convert.
2447 EVT SrcVT = Op.getValueType();
2448 EVT DstVT = N->getValueType(0);
2449 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
2450 "ExpandBIT_CONVERT called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002451
Bob Wilson9f3f0612010-04-17 05:30:19 +00002452 // Turn i64->f64 into VMOVDRR.
2453 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002454 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2455 DAG.getConstant(0, MVT::i32));
2456 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2457 DAG.getConstant(1, MVT::i32));
Bob Wilson1114f562010-06-11 22:45:25 +00002458 return DAG.getNode(ISD::BIT_CONVERT, dl, DstVT,
2459 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00002460 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002461
Jim Grosbache5165492009-11-09 00:11:35 +00002462 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002463 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2464 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2465 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2466 // Merge the pieces into a single i64 value.
2467 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2468 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002469
Bob Wilson9f3f0612010-04-17 05:30:19 +00002470 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002471}
2472
Bob Wilson5bafff32009-06-22 23:27:02 +00002473/// getZeroVector - Returns a vector of specified type with all zero elements.
2474///
Owen Andersone50ed302009-08-10 22:56:29 +00002475static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002476 assert(VT.isVector() && "Expected a vector type");
2477
2478 // Zero vectors are used to represent vector negation and in those cases
2479 // will be implemented with the NEON VNEG instruction. However, VNEG does
2480 // not support i64 elements, so sometimes the zero vectors will need to be
2481 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002482 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilson5bafff32009-06-22 23:27:02 +00002483 // to their dest type. This ensures they get CSE'd.
2484 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002485 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2486 SmallVector<SDValue, 8> Ops;
2487 MVT TVT;
2488
2489 if (VT.getSizeInBits() == 64) {
2490 Ops.assign(8, Cst); TVT = MVT::v8i8;
2491 } else {
2492 Ops.assign(16, Cst); TVT = MVT::v16i8;
2493 }
2494 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002495
2496 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2497}
2498
2499/// getOnesVector - Returns a vector of specified type with all bits set.
2500///
Owen Andersone50ed302009-08-10 22:56:29 +00002501static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002502 assert(VT.isVector() && "Expected a vector type");
2503
Bob Wilson929ffa22009-10-30 20:13:25 +00002504 // Always build ones vectors as <16 x i8> or <8 x i8> bitcasted to their
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002505 // dest type. This ensures they get CSE'd.
Bob Wilson5bafff32009-06-22 23:27:02 +00002506 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002507 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2508 SmallVector<SDValue, 8> Ops;
2509 MVT TVT;
2510
2511 if (VT.getSizeInBits() == 64) {
2512 Ops.assign(8, Cst); TVT = MVT::v8i8;
2513 } else {
2514 Ops.assign(16, Cst); TVT = MVT::v16i8;
2515 }
2516 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002517
2518 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2519}
2520
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002521/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2522/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002523SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2524 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002525 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2526 EVT VT = Op.getValueType();
2527 unsigned VTBits = VT.getSizeInBits();
2528 DebugLoc dl = Op.getDebugLoc();
2529 SDValue ShOpLo = Op.getOperand(0);
2530 SDValue ShOpHi = Op.getOperand(1);
2531 SDValue ShAmt = Op.getOperand(2);
2532 SDValue ARMCC;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002533 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002534
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002535 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2536
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002537 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2538 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2539 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2540 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2541 DAG.getConstant(VTBits, MVT::i32));
2542 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2543 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002544 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002545
2546 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2547 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002548 ARMCC, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002549 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002550 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC,
2551 CCR, Cmp);
2552
2553 SDValue Ops[2] = { Lo, Hi };
2554 return DAG.getMergeValues(Ops, 2, dl);
2555}
2556
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002557/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2558/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002559SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2560 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002561 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2562 EVT VT = Op.getValueType();
2563 unsigned VTBits = VT.getSizeInBits();
2564 DebugLoc dl = Op.getDebugLoc();
2565 SDValue ShOpLo = Op.getOperand(0);
2566 SDValue ShOpHi = Op.getOperand(1);
2567 SDValue ShAmt = Op.getOperand(2);
2568 SDValue ARMCC;
2569
2570 assert(Op.getOpcode() == ISD::SHL_PARTS);
2571 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2572 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2573 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2574 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2575 DAG.getConstant(VTBits, MVT::i32));
2576 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2577 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2578
2579 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2580 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2581 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002582 ARMCC, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002583 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
2584 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMCC,
2585 CCR, Cmp);
2586
2587 SDValue Ops[2] = { Lo, Hi };
2588 return DAG.getMergeValues(Ops, 2, dl);
2589}
2590
Jim Grosbach3482c802010-01-18 19:58:49 +00002591static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
2592 const ARMSubtarget *ST) {
2593 EVT VT = N->getValueType(0);
2594 DebugLoc dl = N->getDebugLoc();
2595
2596 if (!ST->hasV6T2Ops())
2597 return SDValue();
2598
2599 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
2600 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
2601}
2602
Bob Wilson5bafff32009-06-22 23:27:02 +00002603static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2604 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002605 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002606 DebugLoc dl = N->getDebugLoc();
2607
2608 // Lower vector shifts on NEON to use VSHL.
2609 if (VT.isVector()) {
2610 assert(ST->hasNEON() && "unexpected vector shift");
2611
2612 // Left shifts translate directly to the vshiftu intrinsic.
2613 if (N->getOpcode() == ISD::SHL)
2614 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002615 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002616 N->getOperand(0), N->getOperand(1));
2617
2618 assert((N->getOpcode() == ISD::SRA ||
2619 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2620
2621 // NEON uses the same intrinsics for both left and right shifts. For
2622 // right shifts, the shift amounts are negative, so negate the vector of
2623 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002624 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002625 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2626 getZeroVector(ShiftVT, DAG, dl),
2627 N->getOperand(1));
2628 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2629 Intrinsic::arm_neon_vshifts :
2630 Intrinsic::arm_neon_vshiftu);
2631 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002632 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002633 N->getOperand(0), NegatedCount);
2634 }
2635
Eli Friedmance392eb2009-08-22 03:13:10 +00002636 // We can get here for a node like i32 = ISD::SHL i32, i64
2637 if (VT != MVT::i64)
2638 return SDValue();
2639
2640 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002641 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002642
Chris Lattner27a6c732007-11-24 07:07:01 +00002643 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2644 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002645 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002646 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002647
Chris Lattner27a6c732007-11-24 07:07:01 +00002648 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002649 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002650
Chris Lattner27a6c732007-11-24 07:07:01 +00002651 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002652 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00002653 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00002654 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00002655 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002656
Chris Lattner27a6c732007-11-24 07:07:01 +00002657 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2658 // captures the result into a carry flag.
2659 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002660 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002661
Chris Lattner27a6c732007-11-24 07:07:01 +00002662 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002663 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002664
Chris Lattner27a6c732007-11-24 07:07:01 +00002665 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002666 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002667}
2668
Bob Wilson5bafff32009-06-22 23:27:02 +00002669static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2670 SDValue TmpOp0, TmpOp1;
2671 bool Invert = false;
2672 bool Swap = false;
2673 unsigned Opc = 0;
2674
2675 SDValue Op0 = Op.getOperand(0);
2676 SDValue Op1 = Op.getOperand(1);
2677 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002678 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002679 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2680 DebugLoc dl = Op.getDebugLoc();
2681
2682 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2683 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002684 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002685 case ISD::SETUNE:
2686 case ISD::SETNE: Invert = true; // Fallthrough
2687 case ISD::SETOEQ:
2688 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2689 case ISD::SETOLT:
2690 case ISD::SETLT: Swap = true; // Fallthrough
2691 case ISD::SETOGT:
2692 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2693 case ISD::SETOLE:
2694 case ISD::SETLE: Swap = true; // Fallthrough
2695 case ISD::SETOGE:
2696 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2697 case ISD::SETUGE: Swap = true; // Fallthrough
2698 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2699 case ISD::SETUGT: Swap = true; // Fallthrough
2700 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2701 case ISD::SETUEQ: Invert = true; // Fallthrough
2702 case ISD::SETONE:
2703 // Expand this to (OLT | OGT).
2704 TmpOp0 = Op0;
2705 TmpOp1 = Op1;
2706 Opc = ISD::OR;
2707 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2708 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2709 break;
2710 case ISD::SETUO: Invert = true; // Fallthrough
2711 case ISD::SETO:
2712 // Expand this to (OLT | OGE).
2713 TmpOp0 = Op0;
2714 TmpOp1 = Op1;
2715 Opc = ISD::OR;
2716 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2717 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2718 break;
2719 }
2720 } else {
2721 // Integer comparisons.
2722 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002723 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002724 case ISD::SETNE: Invert = true;
2725 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2726 case ISD::SETLT: Swap = true;
2727 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2728 case ISD::SETLE: Swap = true;
2729 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2730 case ISD::SETULT: Swap = true;
2731 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2732 case ISD::SETULE: Swap = true;
2733 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2734 }
2735
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002736 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002737 if (Opc == ARMISD::VCEQ) {
2738
2739 SDValue AndOp;
2740 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2741 AndOp = Op0;
2742 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2743 AndOp = Op1;
2744
2745 // Ignore bitconvert.
2746 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2747 AndOp = AndOp.getOperand(0);
2748
2749 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2750 Opc = ARMISD::VTST;
2751 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2752 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2753 Invert = !Invert;
2754 }
2755 }
2756 }
2757
2758 if (Swap)
2759 std::swap(Op0, Op1);
2760
2761 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2762
2763 if (Invert)
2764 Result = DAG.getNOT(dl, Result, VT);
2765
2766 return Result;
2767}
2768
Bob Wilsond3c42842010-06-14 22:19:57 +00002769/// isNEONModifiedImm - Check if the specified splat value corresponds to a
2770/// valid vector constant for a NEON instruction with a "modified immediate"
2771/// operand (e.g., VMOV). If so, return either the constant being
2772/// splatted or the encoded value, depending on the DoEncode parameter. The
2773/// format of the encoded value is: bit12=Op, bits11-8=Cmode,
2774/// bits7-0=Immediate.
2775static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
2776 unsigned SplatBitSize, SelectionDAG &DAG,
Bob Wilson827b2102010-06-15 19:05:35 +00002777 bool isVMOV, bool DoEncode) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00002778 unsigned Op, Cmode, Imm;
2779 EVT VT;
2780
Bob Wilson827b2102010-06-15 19:05:35 +00002781 // SplatBitSize is set to the smallest size that splats the vector, so a
2782 // zero vector will always have SplatBitSize == 8. However, NEON modified
2783 // immediate instructions others than VMOV do not support the 8-bit encoding
2784 // of a zero vector, and the default encoding of zero is supposed to be the
2785 // 32-bit version.
2786 if (SplatBits == 0)
2787 SplatBitSize = 32;
2788
Bob Wilson1a913ed2010-06-11 21:34:50 +00002789 Op = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00002790 switch (SplatBitSize) {
2791 case 8:
Bob Wilson1a913ed2010-06-11 21:34:50 +00002792 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00002793 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson1a913ed2010-06-11 21:34:50 +00002794 Cmode = 0xe;
2795 Imm = SplatBits;
2796 VT = MVT::i8;
2797 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002798
2799 case 16:
2800 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilson1a913ed2010-06-11 21:34:50 +00002801 VT = MVT::i16;
2802 if ((SplatBits & ~0xff) == 0) {
2803 // Value = 0x00nn: Op=x, Cmode=100x.
2804 Cmode = 0x8;
2805 Imm = SplatBits;
2806 break;
2807 }
2808 if ((SplatBits & ~0xff00) == 0) {
2809 // Value = 0xnn00: Op=x, Cmode=101x.
2810 Cmode = 0xa;
2811 Imm = SplatBits >> 8;
2812 break;
2813 }
2814 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002815
2816 case 32:
2817 // NEON's 32-bit VMOV supports splat values where:
2818 // * only one byte is nonzero, or
2819 // * the least significant byte is 0xff and the second byte is nonzero, or
2820 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilson1a913ed2010-06-11 21:34:50 +00002821 VT = MVT::i32;
2822 if ((SplatBits & ~0xff) == 0) {
2823 // Value = 0x000000nn: Op=x, Cmode=000x.
2824 Cmode = 0;
2825 Imm = SplatBits;
2826 break;
2827 }
2828 if ((SplatBits & ~0xff00) == 0) {
2829 // Value = 0x0000nn00: Op=x, Cmode=001x.
2830 Cmode = 0x2;
2831 Imm = SplatBits >> 8;
2832 break;
2833 }
2834 if ((SplatBits & ~0xff0000) == 0) {
2835 // Value = 0x00nn0000: Op=x, Cmode=010x.
2836 Cmode = 0x4;
2837 Imm = SplatBits >> 16;
2838 break;
2839 }
2840 if ((SplatBits & ~0xff000000) == 0) {
2841 // Value = 0xnn000000: Op=x, Cmode=011x.
2842 Cmode = 0x6;
2843 Imm = SplatBits >> 24;
2844 break;
2845 }
Bob Wilson5bafff32009-06-22 23:27:02 +00002846
2847 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00002848 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
2849 // Value = 0x0000nnff: Op=x, Cmode=1100.
2850 Cmode = 0xc;
2851 Imm = SplatBits >> 8;
2852 SplatBits |= 0xff;
2853 break;
2854 }
Bob Wilson5bafff32009-06-22 23:27:02 +00002855
2856 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00002857 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
2858 // Value = 0x00nnffff: Op=x, Cmode=1101.
2859 Cmode = 0xd;
2860 Imm = SplatBits >> 16;
2861 SplatBits |= 0xffff;
2862 break;
2863 }
Bob Wilson5bafff32009-06-22 23:27:02 +00002864
2865 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2866 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2867 // VMOV.I32. A (very) minor optimization would be to replicate the value
2868 // and fall through here to test for a valid 64-bit splat. But, then the
2869 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00002870 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002871
2872 case 64: {
2873 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson827b2102010-06-15 19:05:35 +00002874 if (!isVMOV)
2875 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002876 uint64_t BitMask = 0xff;
2877 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002878 unsigned ImmMask = 1;
2879 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00002880 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00002881 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002882 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002883 Imm |= ImmMask;
2884 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002885 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00002886 }
Bob Wilson5bafff32009-06-22 23:27:02 +00002887 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002888 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00002889 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00002890 // Op=1, Cmode=1110.
2891 Op = 1;
2892 Cmode = 0xe;
2893 SplatBits = Val;
2894 VT = MVT::i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00002895 break;
2896 }
2897
Bob Wilson1a913ed2010-06-11 21:34:50 +00002898 default:
2899 llvm_unreachable("unexpected size for EncodeNEONModImm");
2900 return SDValue();
2901 }
2902
2903 if (DoEncode)
2904 return DAG.getTargetConstant((Op << 12) | (Cmode << 8) | Imm, MVT::i32);
2905 return DAG.getTargetConstant(SplatBits, VT);
Bob Wilson5bafff32009-06-22 23:27:02 +00002906}
2907
Bob Wilsond3c42842010-06-14 22:19:57 +00002908
2909/// getNEONModImm - If this is a valid vector constant for a NEON instruction
2910/// with a "modified immediate" operand (e.g., VMOV) of the specified element
2911/// size, return the encoded value for that immediate. The ByteSize field
2912/// indicates the number of bytes of each element [1248].
Bob Wilson827b2102010-06-15 19:05:35 +00002913SDValue ARM::getNEONModImm(SDNode *N, unsigned ByteSize, bool isVMOV,
2914 SelectionDAG &DAG) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002915 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2916 APInt SplatBits, SplatUndef;
2917 unsigned SplatBitSize;
2918 bool HasAnyUndefs;
2919 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2920 HasAnyUndefs, ByteSize * 8))
2921 return SDValue();
2922
2923 if (SplatBitSize > ByteSize * 8)
2924 return SDValue();
2925
Bob Wilsond3c42842010-06-14 22:19:57 +00002926 return isNEONModifiedImm(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
Bob Wilson827b2102010-06-15 19:05:35 +00002927 SplatBitSize, DAG, isVMOV, true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002928}
2929
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002930static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2931 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002932 unsigned NumElts = VT.getVectorNumElements();
2933 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002934 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002935
2936 // If this is a VEXT shuffle, the immediate value is the index of the first
2937 // element. The other shuffle indices must be the successive elements after
2938 // the first one.
2939 unsigned ExpectedElt = Imm;
2940 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002941 // Increment the expected index. If it wraps around, it may still be
2942 // a VEXT but the source vectors must be swapped.
2943 ExpectedElt += 1;
2944 if (ExpectedElt == NumElts * 2) {
2945 ExpectedElt = 0;
2946 ReverseVEXT = true;
2947 }
2948
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002949 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002950 return false;
2951 }
2952
2953 // Adjust the index value if the source operands will be swapped.
2954 if (ReverseVEXT)
2955 Imm -= NumElts;
2956
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002957 return true;
2958}
2959
Bob Wilson8bb9e482009-07-26 00:39:34 +00002960/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2961/// instruction with the specified blocksize. (The order of the elements
2962/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002963static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2964 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002965 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2966 "Only possible block sizes for VREV are: 16, 32, 64");
2967
Bob Wilson8bb9e482009-07-26 00:39:34 +00002968 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00002969 if (EltSz == 64)
2970 return false;
2971
2972 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002973 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002974
2975 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2976 return false;
2977
2978 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002979 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002980 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2981 return false;
2982 }
2983
2984 return true;
2985}
2986
Bob Wilsonc692cb72009-08-21 20:54:19 +00002987static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2988 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002989 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2990 if (EltSz == 64)
2991 return false;
2992
Bob Wilsonc692cb72009-08-21 20:54:19 +00002993 unsigned NumElts = VT.getVectorNumElements();
2994 WhichResult = (M[0] == 0 ? 0 : 1);
2995 for (unsigned i = 0; i < NumElts; i += 2) {
2996 if ((unsigned) M[i] != i + WhichResult ||
2997 (unsigned) M[i+1] != i + NumElts + WhichResult)
2998 return false;
2999 }
3000 return true;
3001}
3002
Bob Wilson324f4f12009-12-03 06:40:55 +00003003/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3004/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3005/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3006static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3007 unsigned &WhichResult) {
3008 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3009 if (EltSz == 64)
3010 return false;
3011
3012 unsigned NumElts = VT.getVectorNumElements();
3013 WhichResult = (M[0] == 0 ? 0 : 1);
3014 for (unsigned i = 0; i < NumElts; i += 2) {
3015 if ((unsigned) M[i] != i + WhichResult ||
3016 (unsigned) M[i+1] != i + WhichResult)
3017 return false;
3018 }
3019 return true;
3020}
3021
Bob Wilsonc692cb72009-08-21 20:54:19 +00003022static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3023 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003024 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3025 if (EltSz == 64)
3026 return false;
3027
Bob Wilsonc692cb72009-08-21 20:54:19 +00003028 unsigned NumElts = VT.getVectorNumElements();
3029 WhichResult = (M[0] == 0 ? 0 : 1);
3030 for (unsigned i = 0; i != NumElts; ++i) {
3031 if ((unsigned) M[i] != 2 * i + WhichResult)
3032 return false;
3033 }
3034
3035 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003036 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003037 return false;
3038
3039 return true;
3040}
3041
Bob Wilson324f4f12009-12-03 06:40:55 +00003042/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3043/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3044/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3045static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3046 unsigned &WhichResult) {
3047 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3048 if (EltSz == 64)
3049 return false;
3050
3051 unsigned Half = VT.getVectorNumElements() / 2;
3052 WhichResult = (M[0] == 0 ? 0 : 1);
3053 for (unsigned j = 0; j != 2; ++j) {
3054 unsigned Idx = WhichResult;
3055 for (unsigned i = 0; i != Half; ++i) {
3056 if ((unsigned) M[i + j * Half] != Idx)
3057 return false;
3058 Idx += 2;
3059 }
3060 }
3061
3062 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3063 if (VT.is64BitVector() && EltSz == 32)
3064 return false;
3065
3066 return true;
3067}
3068
Bob Wilsonc692cb72009-08-21 20:54:19 +00003069static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3070 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003071 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3072 if (EltSz == 64)
3073 return false;
3074
Bob Wilsonc692cb72009-08-21 20:54:19 +00003075 unsigned NumElts = VT.getVectorNumElements();
3076 WhichResult = (M[0] == 0 ? 0 : 1);
3077 unsigned Idx = WhichResult * NumElts / 2;
3078 for (unsigned i = 0; i != NumElts; i += 2) {
3079 if ((unsigned) M[i] != Idx ||
3080 (unsigned) M[i+1] != Idx + NumElts)
3081 return false;
3082 Idx += 1;
3083 }
3084
3085 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003086 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003087 return false;
3088
3089 return true;
3090}
3091
Bob Wilson324f4f12009-12-03 06:40:55 +00003092/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3093/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3094/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3095static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3096 unsigned &WhichResult) {
3097 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3098 if (EltSz == 64)
3099 return false;
3100
3101 unsigned NumElts = VT.getVectorNumElements();
3102 WhichResult = (M[0] == 0 ? 0 : 1);
3103 unsigned Idx = WhichResult * NumElts / 2;
3104 for (unsigned i = 0; i != NumElts; i += 2) {
3105 if ((unsigned) M[i] != Idx ||
3106 (unsigned) M[i+1] != Idx)
3107 return false;
3108 Idx += 1;
3109 }
3110
3111 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3112 if (VT.is64BitVector() && EltSz == 32)
3113 return false;
3114
3115 return true;
3116}
3117
3118
Owen Andersone50ed302009-08-10 22:56:29 +00003119static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003120 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00003121 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003122 if (ConstVal->isNullValue())
3123 return getZeroVector(VT, DAG, dl);
3124 if (ConstVal->isAllOnesValue())
3125 return getOnesVector(VT, DAG, dl);
3126
Owen Andersone50ed302009-08-10 22:56:29 +00003127 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00003128 if (VT.is64BitVector()) {
3129 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003130 case 8: CanonicalVT = MVT::v8i8; break;
3131 case 16: CanonicalVT = MVT::v4i16; break;
3132 case 32: CanonicalVT = MVT::v2i32; break;
3133 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003134 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003135 }
3136 } else {
3137 assert(VT.is128BitVector() && "unknown splat vector size");
3138 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003139 case 8: CanonicalVT = MVT::v16i8; break;
3140 case 16: CanonicalVT = MVT::v8i16; break;
3141 case 32: CanonicalVT = MVT::v4i32; break;
3142 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003143 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003144 }
3145 }
3146
3147 // Build a canonical splat for this value.
3148 SmallVector<SDValue, 8> Ops;
3149 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
3150 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
3151 Ops.size());
3152 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
3153}
3154
3155// If this is a case we can't handle, return null and let the default
3156// expansion code take care of it.
3157static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00003158 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003159 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003160 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003161
3162 APInt SplatBits, SplatUndef;
3163 unsigned SplatBitSize;
3164 bool HasAnyUndefs;
3165 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003166 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003167 // Check if an immediate VMOV works.
3168 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
3169 SplatUndef.getZExtValue(),
Bob Wilson827b2102010-06-15 19:05:35 +00003170 SplatBitSize, DAG, true, false);
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003171 if (Val.getNode())
3172 return BuildSplat(Val, VT, DAG, dl);
3173 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003174 }
3175
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003176 // Scan through the operands to see if only one value is used.
3177 unsigned NumElts = VT.getVectorNumElements();
3178 bool isOnlyLowElement = true;
3179 bool usesOnlyOneValue = true;
3180 bool isConstant = true;
3181 SDValue Value;
3182 for (unsigned i = 0; i < NumElts; ++i) {
3183 SDValue V = Op.getOperand(i);
3184 if (V.getOpcode() == ISD::UNDEF)
3185 continue;
3186 if (i > 0)
3187 isOnlyLowElement = false;
3188 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3189 isConstant = false;
3190
3191 if (!Value.getNode())
3192 Value = V;
3193 else if (V != Value)
3194 usesOnlyOneValue = false;
3195 }
3196
3197 if (!Value.getNode())
3198 return DAG.getUNDEF(VT);
3199
3200 if (isOnlyLowElement)
3201 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3202
3203 // If all elements are constants, fall back to the default expansion, which
3204 // will generate a load from the constant pool.
3205 if (isConstant)
3206 return SDValue();
3207
3208 // Use VDUP for non-constant splats.
Bob Wilson069e4342010-05-23 05:42:31 +00003209 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3210 if (usesOnlyOneValue && EltSize <= 32)
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003211 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3212
3213 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003214 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3215 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003216 if (EltSize >= 32) {
3217 // Do the expansion with floating-point types, since that is what the VFP
3218 // registers are defined to use, and since i64 is not legal.
3219 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3220 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003221 SmallVector<SDValue, 8> Ops;
3222 for (unsigned i = 0; i < NumElts; ++i)
3223 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, dl, EltVT, Op.getOperand(i)));
3224 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003225 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003226 }
3227
3228 return SDValue();
3229}
3230
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003231/// isShuffleMaskLegal - Targets can use this to indicate that they only
3232/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3233/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3234/// are assumed to be legal.
3235bool
3236ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
3237 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003238 if (VT.getVectorNumElements() == 4 &&
3239 (VT.is128BitVector() || VT.is64BitVector())) {
3240 unsigned PFIndexes[4];
3241 for (unsigned i = 0; i != 4; ++i) {
3242 if (M[i] < 0)
3243 PFIndexes[i] = 8;
3244 else
3245 PFIndexes[i] = M[i];
3246 }
3247
3248 // Compute the index in the perfect shuffle table.
3249 unsigned PFTableIndex =
3250 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3251 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3252 unsigned Cost = (PFEntry >> 30);
3253
3254 if (Cost <= 4)
3255 return true;
3256 }
3257
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003258 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00003259 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003260
Bob Wilson53dd2452010-06-07 23:53:38 +00003261 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3262 return (EltSize >= 32 ||
3263 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003264 isVREVMask(M, VT, 64) ||
3265 isVREVMask(M, VT, 32) ||
3266 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00003267 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
3268 isVTRNMask(M, VT, WhichResult) ||
3269 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00003270 isVZIPMask(M, VT, WhichResult) ||
3271 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
3272 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
3273 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003274}
3275
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003276/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
3277/// the specified operations to build the shuffle.
3278static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
3279 SDValue RHS, SelectionDAG &DAG,
3280 DebugLoc dl) {
3281 unsigned OpNum = (PFEntry >> 26) & 0x0F;
3282 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
3283 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
3284
3285 enum {
3286 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
3287 OP_VREV,
3288 OP_VDUP0,
3289 OP_VDUP1,
3290 OP_VDUP2,
3291 OP_VDUP3,
3292 OP_VEXT1,
3293 OP_VEXT2,
3294 OP_VEXT3,
3295 OP_VUZPL, // VUZP, left result
3296 OP_VUZPR, // VUZP, right result
3297 OP_VZIPL, // VZIP, left result
3298 OP_VZIPR, // VZIP, right result
3299 OP_VTRNL, // VTRN, left result
3300 OP_VTRNR // VTRN, right result
3301 };
3302
3303 if (OpNum == OP_COPY) {
3304 if (LHSID == (1*9+2)*9+3) return LHS;
3305 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
3306 return RHS;
3307 }
3308
3309 SDValue OpLHS, OpRHS;
3310 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
3311 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
3312 EVT VT = OpLHS.getValueType();
3313
3314 switch (OpNum) {
3315 default: llvm_unreachable("Unknown shuffle opcode!");
3316 case OP_VREV:
3317 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
3318 case OP_VDUP0:
3319 case OP_VDUP1:
3320 case OP_VDUP2:
3321 case OP_VDUP3:
3322 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003323 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003324 case OP_VEXT1:
3325 case OP_VEXT2:
3326 case OP_VEXT3:
3327 return DAG.getNode(ARMISD::VEXT, dl, VT,
3328 OpLHS, OpRHS,
3329 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
3330 case OP_VUZPL:
3331 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003332 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003333 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
3334 case OP_VZIPL:
3335 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003336 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003337 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
3338 case OP_VTRNL:
3339 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003340 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3341 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003342 }
3343}
3344
Bob Wilson5bafff32009-06-22 23:27:02 +00003345static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003346 SDValue V1 = Op.getOperand(0);
3347 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00003348 DebugLoc dl = Op.getDebugLoc();
3349 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003350 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003351 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00003352
Bob Wilson28865062009-08-13 02:13:04 +00003353 // Convert shuffles that are directly supported on NEON to target-specific
3354 // DAG nodes, instead of keeping them as shuffles and matching them again
3355 // during code selection. This is more efficient and avoids the possibility
3356 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00003357 // FIXME: floating-point vectors should be canonicalized to integer vectors
3358 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003359 SVN->getMask(ShuffleMask);
3360
Bob Wilson53dd2452010-06-07 23:53:38 +00003361 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3362 if (EltSize <= 32) {
3363 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
3364 int Lane = SVN->getSplatIndex();
3365 // If this is undef splat, generate it via "just" vdup, if possible.
3366 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00003367
Bob Wilson53dd2452010-06-07 23:53:38 +00003368 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3369 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
3370 }
3371 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
3372 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00003373 }
Bob Wilson53dd2452010-06-07 23:53:38 +00003374
3375 bool ReverseVEXT;
3376 unsigned Imm;
3377 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
3378 if (ReverseVEXT)
3379 std::swap(V1, V2);
3380 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
3381 DAG.getConstant(Imm, MVT::i32));
3382 }
3383
3384 if (isVREVMask(ShuffleMask, VT, 64))
3385 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
3386 if (isVREVMask(ShuffleMask, VT, 32))
3387 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
3388 if (isVREVMask(ShuffleMask, VT, 16))
3389 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
3390
3391 // Check for Neon shuffles that modify both input vectors in place.
3392 // If both results are used, i.e., if there are two shuffles with the same
3393 // source operands and with masks corresponding to both results of one of
3394 // these operations, DAG memoization will ensure that a single node is
3395 // used for both shuffles.
3396 unsigned WhichResult;
3397 if (isVTRNMask(ShuffleMask, VT, WhichResult))
3398 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3399 V1, V2).getValue(WhichResult);
3400 if (isVUZPMask(ShuffleMask, VT, WhichResult))
3401 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3402 V1, V2).getValue(WhichResult);
3403 if (isVZIPMask(ShuffleMask, VT, WhichResult))
3404 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3405 V1, V2).getValue(WhichResult);
3406
3407 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
3408 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3409 V1, V1).getValue(WhichResult);
3410 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3411 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3412 V1, V1).getValue(WhichResult);
3413 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3414 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3415 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00003416 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003417
Bob Wilsonc692cb72009-08-21 20:54:19 +00003418 // If the shuffle is not directly supported and it has 4 elements, use
3419 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003420 unsigned NumElts = VT.getVectorNumElements();
3421 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003422 unsigned PFIndexes[4];
3423 for (unsigned i = 0; i != 4; ++i) {
3424 if (ShuffleMask[i] < 0)
3425 PFIndexes[i] = 8;
3426 else
3427 PFIndexes[i] = ShuffleMask[i];
3428 }
3429
3430 // Compute the index in the perfect shuffle table.
3431 unsigned PFTableIndex =
3432 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003433 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3434 unsigned Cost = (PFEntry >> 30);
3435
3436 if (Cost <= 4)
3437 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
3438 }
Bob Wilsond8e17572009-08-12 22:31:50 +00003439
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003440 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003441 if (EltSize >= 32) {
3442 // Do the expansion with floating-point types, since that is what the VFP
3443 // registers are defined to use, and since i64 is not legal.
3444 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3445 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
3446 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V1);
3447 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003448 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003449 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00003450 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003451 Ops.push_back(DAG.getUNDEF(EltVT));
3452 else
3453 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
3454 ShuffleMask[i] < (int)NumElts ? V1 : V2,
3455 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
3456 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00003457 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003458 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Bob Wilson63b88452010-05-20 18:39:53 +00003459 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
3460 }
3461
Bob Wilson22cac0d2009-08-14 05:16:33 +00003462 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003463}
3464
Bob Wilson5bafff32009-06-22 23:27:02 +00003465static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003466 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003467 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003468 SDValue Vec = Op.getOperand(0);
3469 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00003470 assert(VT == MVT::i32 &&
3471 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
3472 "unexpected type for custom-lowering vector extract");
3473 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00003474}
3475
Bob Wilsona6d65862009-08-03 20:36:38 +00003476static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3477 // The only time a CONCAT_VECTORS operation can have legal types is when
3478 // two 64-bit vectors are concatenated to a 128-bit vector.
3479 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
3480 "unexpected CONCAT_VECTORS");
3481 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00003482 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00003483 SDValue Op0 = Op.getOperand(0);
3484 SDValue Op1 = Op.getOperand(1);
3485 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003486 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3487 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00003488 DAG.getIntPtrConstant(0));
3489 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003490 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3491 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00003492 DAG.getIntPtrConstant(1));
3493 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003494}
3495
Dan Gohmand858e902010-04-17 15:26:15 +00003496SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003497 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003498 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00003499 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00003500 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003501 case ISD::GlobalAddress:
3502 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
3503 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00003504 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00003505 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
3506 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003507 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00003508 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00003509 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00003510 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00003511 case ISD::SINT_TO_FP:
3512 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
3513 case ISD::FP_TO_SINT:
3514 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003515 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003516 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003517 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003518 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00003519 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00003520 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00003521 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
3522 Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00003523 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003524 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00003525 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00003526 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00003527 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003528 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00003529 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00003530 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003531 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
3532 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3533 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003534 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00003535 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003536 }
Dan Gohman475871a2008-07-27 21:46:04 +00003537 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003538}
3539
Duncan Sands1607f052008-12-01 11:39:25 +00003540/// ReplaceNodeResults - Replace the results of node with an illegal result
3541/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00003542void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
3543 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00003544 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00003545 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00003546 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00003547 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003548 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003549 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003550 case ISD::BIT_CONVERT:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003551 Res = ExpandBIT_CONVERT(N, DAG);
3552 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00003553 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003554 case ISD::SRA:
3555 Res = LowerShift(N, DAG, Subtarget);
3556 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003557 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00003558 if (Res.getNode())
3559 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00003560}
Chris Lattner27a6c732007-11-24 07:07:01 +00003561
Evan Chenga8e29892007-01-19 07:51:42 +00003562//===----------------------------------------------------------------------===//
3563// ARM Scheduler Hooks
3564//===----------------------------------------------------------------------===//
3565
3566MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003567ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
3568 MachineBasicBlock *BB,
3569 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00003570 unsigned dest = MI->getOperand(0).getReg();
3571 unsigned ptr = MI->getOperand(1).getReg();
3572 unsigned oldval = MI->getOperand(2).getReg();
3573 unsigned newval = MI->getOperand(3).getReg();
3574 unsigned scratch = BB->getParent()->getRegInfo()
3575 .createVirtualRegister(ARM::GPRRegisterClass);
3576 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3577 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003578 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00003579
3580 unsigned ldrOpc, strOpc;
3581 switch (Size) {
3582 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003583 case 1:
3584 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
3585 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
3586 break;
3587 case 2:
3588 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3589 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3590 break;
3591 case 4:
3592 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3593 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3594 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00003595 }
3596
3597 MachineFunction *MF = BB->getParent();
3598 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3599 MachineFunction::iterator It = BB;
3600 ++It; // insert the new blocks after the current block
3601
3602 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3603 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3604 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3605 MF->insert(It, loop1MBB);
3606 MF->insert(It, loop2MBB);
3607 MF->insert(It, exitMBB);
3608 exitMBB->transferSuccessors(BB);
3609
3610 // thisMBB:
3611 // ...
3612 // fallthrough --> loop1MBB
3613 BB->addSuccessor(loop1MBB);
3614
3615 // loop1MBB:
3616 // ldrex dest, [ptr]
3617 // cmp dest, oldval
3618 // bne exitMBB
3619 BB = loop1MBB;
3620 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003621 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003622 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003623 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3624 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003625 BB->addSuccessor(loop2MBB);
3626 BB->addSuccessor(exitMBB);
3627
3628 // loop2MBB:
3629 // strex scratch, newval, [ptr]
3630 // cmp scratch, #0
3631 // bne loop1MBB
3632 BB = loop2MBB;
3633 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
3634 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003635 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003636 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003637 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3638 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003639 BB->addSuccessor(loop1MBB);
3640 BB->addSuccessor(exitMBB);
3641
3642 // exitMBB:
3643 // ...
3644 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00003645
3646 MF->DeleteMachineInstr(MI); // The instruction is gone now.
3647
Jim Grosbach5278eb82009-12-11 01:42:04 +00003648 return BB;
3649}
3650
3651MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003652ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
3653 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00003654 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
3655 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3656
3657 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003658 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003659 MachineFunction::iterator It = BB;
3660 ++It;
3661
3662 unsigned dest = MI->getOperand(0).getReg();
3663 unsigned ptr = MI->getOperand(1).getReg();
3664 unsigned incr = MI->getOperand(2).getReg();
3665 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00003666
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003667 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003668 unsigned ldrOpc, strOpc;
3669 switch (Size) {
3670 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003671 case 1:
3672 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00003673 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003674 break;
3675 case 2:
3676 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3677 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3678 break;
3679 case 4:
3680 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3681 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3682 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00003683 }
3684
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003685 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3686 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3687 MF->insert(It, loopMBB);
3688 MF->insert(It, exitMBB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003689 exitMBB->transferSuccessors(BB);
3690
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003691 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003692 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3693 unsigned scratch2 = (!BinOpcode) ? incr :
3694 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3695
3696 // thisMBB:
3697 // ...
3698 // fallthrough --> loopMBB
3699 BB->addSuccessor(loopMBB);
3700
3701 // loopMBB:
3702 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003703 // <binop> scratch2, dest, incr
3704 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00003705 // cmp scratch, #0
3706 // bne- loopMBB
3707 // fallthrough --> exitMBB
3708 BB = loopMBB;
3709 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00003710 if (BinOpcode) {
3711 // operand order needs to go the other way for NAND
3712 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
3713 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3714 addReg(incr).addReg(dest)).addReg(0);
3715 else
3716 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3717 addReg(dest).addReg(incr)).addReg(0);
3718 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00003719
3720 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
3721 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003722 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00003723 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003724 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3725 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003726
3727 BB->addSuccessor(loopMBB);
3728 BB->addSuccessor(exitMBB);
3729
3730 // exitMBB:
3731 // ...
3732 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00003733
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003734 MF->DeleteMachineInstr(MI); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00003735
Jim Grosbachc3c23542009-12-14 04:22:04 +00003736 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00003737}
3738
3739MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00003740ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003741 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003742 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00003743 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003744 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00003745 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00003746 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00003747 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00003748 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00003749
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003750 case ARM::ATOMIC_LOAD_ADD_I8:
3751 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3752 case ARM::ATOMIC_LOAD_ADD_I16:
3753 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3754 case ARM::ATOMIC_LOAD_ADD_I32:
3755 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003756
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003757 case ARM::ATOMIC_LOAD_AND_I8:
3758 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3759 case ARM::ATOMIC_LOAD_AND_I16:
3760 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3761 case ARM::ATOMIC_LOAD_AND_I32:
3762 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003763
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003764 case ARM::ATOMIC_LOAD_OR_I8:
3765 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3766 case ARM::ATOMIC_LOAD_OR_I16:
3767 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3768 case ARM::ATOMIC_LOAD_OR_I32:
3769 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003770
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003771 case ARM::ATOMIC_LOAD_XOR_I8:
3772 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3773 case ARM::ATOMIC_LOAD_XOR_I16:
3774 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3775 case ARM::ATOMIC_LOAD_XOR_I32:
3776 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003777
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003778 case ARM::ATOMIC_LOAD_NAND_I8:
3779 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3780 case ARM::ATOMIC_LOAD_NAND_I16:
3781 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3782 case ARM::ATOMIC_LOAD_NAND_I32:
3783 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003784
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003785 case ARM::ATOMIC_LOAD_SUB_I8:
3786 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3787 case ARM::ATOMIC_LOAD_SUB_I16:
3788 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3789 case ARM::ATOMIC_LOAD_SUB_I32:
3790 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003791
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003792 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
3793 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
3794 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00003795
3796 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
3797 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
3798 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003799
Evan Cheng007ea272009-08-12 05:17:19 +00003800 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00003801 // To "insert" a SELECT_CC instruction, we actually have to insert the
3802 // diamond control-flow pattern. The incoming instruction knows the
3803 // destination vreg to set, the condition code register to branch on, the
3804 // true/false values to select between, and a branch opcode to use.
3805 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003806 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00003807 ++It;
3808
3809 // thisMBB:
3810 // ...
3811 // TrueVal = ...
3812 // cmpTY ccX, r1, r2
3813 // bCC copy1MBB
3814 // fallthrough --> copy0MBB
3815 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003816 MachineFunction *F = BB->getParent();
3817 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3818 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00003819 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00003820 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003821 F->insert(It, copy0MBB);
3822 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00003823 // Update machine-CFG edges by first adding all successors of the current
3824 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00003825 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003826 E = BB->succ_end(); I != E; ++I)
Evan Chengce319102009-09-19 09:51:03 +00003827 sinkMBB->addSuccessor(*I);
Evan Chenga8e29892007-01-19 07:51:42 +00003828 // Next, remove all successors of the current block, and add the true
3829 // and fallthrough blocks as its successors.
Evan Chengce319102009-09-19 09:51:03 +00003830 while (!BB->succ_empty())
Evan Chenga8e29892007-01-19 07:51:42 +00003831 BB->removeSuccessor(BB->succ_begin());
3832 BB->addSuccessor(copy0MBB);
3833 BB->addSuccessor(sinkMBB);
3834
3835 // copy0MBB:
3836 // %FalseValue = ...
3837 // # fallthrough to sinkMBB
3838 BB = copy0MBB;
3839
3840 // Update machine-CFG edges
3841 BB->addSuccessor(sinkMBB);
3842
3843 // sinkMBB:
3844 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3845 // ...
3846 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00003847 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00003848 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
3849 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
3850
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003851 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00003852 return BB;
3853 }
Evan Cheng86198642009-08-07 00:34:42 +00003854
3855 case ARM::tANDsp:
3856 case ARM::tADDspr_:
3857 case ARM::tSUBspi_:
3858 case ARM::t2SUBrSPi_:
3859 case ARM::t2SUBrSPi12_:
3860 case ARM::t2SUBrSPs_: {
3861 MachineFunction *MF = BB->getParent();
3862 unsigned DstReg = MI->getOperand(0).getReg();
3863 unsigned SrcReg = MI->getOperand(1).getReg();
3864 bool DstIsDead = MI->getOperand(0).isDead();
3865 bool SrcIsKill = MI->getOperand(1).isKill();
3866
3867 if (SrcReg != ARM::SP) {
3868 // Copy the source to SP from virtual register.
3869 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
3870 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3871 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
3872 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
3873 .addReg(SrcReg, getKillRegState(SrcIsKill));
3874 }
3875
3876 unsigned OpOpc = 0;
3877 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
3878 switch (MI->getOpcode()) {
3879 default:
3880 llvm_unreachable("Unexpected pseudo instruction!");
3881 case ARM::tANDsp:
3882 OpOpc = ARM::tAND;
3883 NeedPred = true;
3884 break;
3885 case ARM::tADDspr_:
3886 OpOpc = ARM::tADDspr;
3887 break;
3888 case ARM::tSUBspi_:
3889 OpOpc = ARM::tSUBspi;
3890 break;
3891 case ARM::t2SUBrSPi_:
3892 OpOpc = ARM::t2SUBrSPi;
3893 NeedPred = true; NeedCC = true;
3894 break;
3895 case ARM::t2SUBrSPi12_:
3896 OpOpc = ARM::t2SUBrSPi12;
3897 NeedPred = true;
3898 break;
3899 case ARM::t2SUBrSPs_:
3900 OpOpc = ARM::t2SUBrSPs;
3901 NeedPred = true; NeedCC = true; NeedOp3 = true;
3902 break;
3903 }
3904 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
3905 if (OpOpc == ARM::tAND)
3906 AddDefaultT1CC(MIB);
3907 MIB.addReg(ARM::SP);
3908 MIB.addOperand(MI->getOperand(2));
3909 if (NeedOp3)
3910 MIB.addOperand(MI->getOperand(3));
3911 if (NeedPred)
3912 AddDefaultPred(MIB);
3913 if (NeedCC)
3914 AddDefaultCC(MIB);
3915
3916 // Copy the result from SP to virtual register.
3917 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
3918 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3919 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
3920 BuildMI(BB, dl, TII->get(CopyOpc))
3921 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
3922 .addReg(ARM::SP);
3923 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3924 return BB;
3925 }
Evan Chenga8e29892007-01-19 07:51:42 +00003926 }
3927}
3928
3929//===----------------------------------------------------------------------===//
3930// ARM Optimization Hooks
3931//===----------------------------------------------------------------------===//
3932
Chris Lattnerd1980a52009-03-12 06:52:53 +00003933static
3934SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3935 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00003936 SelectionDAG &DAG = DCI.DAG;
3937 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003938 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00003939 unsigned Opc = N->getOpcode();
3940 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3941 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3942 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3943 ISD::CondCode CC = ISD::SETCC_INVALID;
3944
3945 if (isSlctCC) {
3946 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
3947 } else {
3948 SDValue CCOp = Slct.getOperand(0);
3949 if (CCOp.getOpcode() == ISD::SETCC)
3950 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
3951 }
3952
3953 bool DoXform = false;
3954 bool InvCC = false;
3955 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
3956 "Bad input!");
3957
3958 if (LHS.getOpcode() == ISD::Constant &&
3959 cast<ConstantSDNode>(LHS)->isNullValue()) {
3960 DoXform = true;
3961 } else if (CC != ISD::SETCC_INVALID &&
3962 RHS.getOpcode() == ISD::Constant &&
3963 cast<ConstantSDNode>(RHS)->isNullValue()) {
3964 std::swap(LHS, RHS);
3965 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003966 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00003967 Op0.getOperand(0).getValueType();
3968 bool isInt = OpVT.isInteger();
3969 CC = ISD::getSetCCInverse(CC, isInt);
3970
3971 if (!TLI.isCondCodeLegal(CC, OpVT))
3972 return SDValue(); // Inverse operator isn't legal.
3973
3974 DoXform = true;
3975 InvCC = true;
3976 }
3977
3978 if (DoXform) {
3979 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3980 if (isSlctCC)
3981 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3982 Slct.getOperand(0), Slct.getOperand(1), CC);
3983 SDValue CCOp = Slct.getOperand(0);
3984 if (InvCC)
3985 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3986 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3987 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3988 CCOp, OtherOp, Result);
3989 }
3990 return SDValue();
3991}
3992
3993/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3994static SDValue PerformADDCombine(SDNode *N,
3995 TargetLowering::DAGCombinerInfo &DCI) {
3996 // added by evan in r37685 with no testcase.
3997 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003998
Chris Lattnerd1980a52009-03-12 06:52:53 +00003999 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
4000 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
4001 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
4002 if (Result.getNode()) return Result;
4003 }
4004 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
4005 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
4006 if (Result.getNode()) return Result;
4007 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00004008
Chris Lattnerd1980a52009-03-12 06:52:53 +00004009 return SDValue();
4010}
4011
4012/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
4013static SDValue PerformSUBCombine(SDNode *N,
4014 TargetLowering::DAGCombinerInfo &DCI) {
4015 // added by evan in r37685 with no testcase.
4016 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00004017
Chris Lattnerd1980a52009-03-12 06:52:53 +00004018 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
4019 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
4020 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
4021 if (Result.getNode()) return Result;
4022 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00004023
Chris Lattnerd1980a52009-03-12 06:52:53 +00004024 return SDValue();
4025}
4026
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004027static SDValue PerformMULCombine(SDNode *N,
4028 TargetLowering::DAGCombinerInfo &DCI,
4029 const ARMSubtarget *Subtarget) {
4030 SelectionDAG &DAG = DCI.DAG;
4031
4032 if (Subtarget->isThumb1Only())
4033 return SDValue();
4034
4035 if (DAG.getMachineFunction().
4036 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
4037 return SDValue();
4038
4039 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
4040 return SDValue();
4041
4042 EVT VT = N->getValueType(0);
4043 if (VT != MVT::i32)
4044 return SDValue();
4045
4046 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
4047 if (!C)
4048 return SDValue();
4049
4050 uint64_t MulAmt = C->getZExtValue();
4051 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
4052 ShiftAmt = ShiftAmt & (32 - 1);
4053 SDValue V = N->getOperand(0);
4054 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004055
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004056 SDValue Res;
4057 MulAmt >>= ShiftAmt;
4058 if (isPowerOf2_32(MulAmt - 1)) {
4059 // (mul x, 2^N + 1) => (add (shl x, N), x)
4060 Res = DAG.getNode(ISD::ADD, DL, VT,
4061 V, DAG.getNode(ISD::SHL, DL, VT,
4062 V, DAG.getConstant(Log2_32(MulAmt-1),
4063 MVT::i32)));
4064 } else if (isPowerOf2_32(MulAmt + 1)) {
4065 // (mul x, 2^N - 1) => (sub (shl x, N), x)
4066 Res = DAG.getNode(ISD::SUB, DL, VT,
4067 DAG.getNode(ISD::SHL, DL, VT,
4068 V, DAG.getConstant(Log2_32(MulAmt+1),
4069 MVT::i32)),
4070 V);
4071 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004072 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004073
4074 if (ShiftAmt != 0)
4075 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
4076 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004077
4078 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004079 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004080 return SDValue();
4081}
4082
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +00004083/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
4084/// ARMISD::VMOVRRD.
Jim Grosbache5165492009-11-09 00:11:35 +00004085static SDValue PerformVMOVRRDCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004086 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004087 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00004088 SDValue InDouble = N->getOperand(0);
Jim Grosbache5165492009-11-09 00:11:35 +00004089 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004090 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004091 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004092}
4093
Bob Wilson5bafff32009-06-22 23:27:02 +00004094/// getVShiftImm - Check if this is a valid build_vector for the immediate
4095/// operand of a vector shift operation, where all the elements of the
4096/// build_vector must have the same constant integer value.
4097static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
4098 // Ignore bit_converts.
4099 while (Op.getOpcode() == ISD::BIT_CONVERT)
4100 Op = Op.getOperand(0);
4101 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
4102 APInt SplatBits, SplatUndef;
4103 unsigned SplatBitSize;
4104 bool HasAnyUndefs;
4105 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
4106 HasAnyUndefs, ElementBits) ||
4107 SplatBitSize > ElementBits)
4108 return false;
4109 Cnt = SplatBits.getSExtValue();
4110 return true;
4111}
4112
4113/// isVShiftLImm - Check if this is a valid build_vector for the immediate
4114/// operand of a vector shift left operation. That value must be in the range:
4115/// 0 <= Value < ElementBits for a left shift; or
4116/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00004117static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00004118 assert(VT.isVector() && "vector shift count is not a vector type");
4119 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
4120 if (! getVShiftImm(Op, ElementBits, Cnt))
4121 return false;
4122 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
4123}
4124
4125/// isVShiftRImm - Check if this is a valid build_vector for the immediate
4126/// operand of a vector shift right operation. For a shift opcode, the value
4127/// is positive, but for an intrinsic the value count must be negative. The
4128/// absolute value must be in the range:
4129/// 1 <= |Value| <= ElementBits for a right shift; or
4130/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00004131static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00004132 int64_t &Cnt) {
4133 assert(VT.isVector() && "vector shift count is not a vector type");
4134 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
4135 if (! getVShiftImm(Op, ElementBits, Cnt))
4136 return false;
4137 if (isIntrinsic)
4138 Cnt = -Cnt;
4139 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
4140}
4141
4142/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
4143static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
4144 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
4145 switch (IntNo) {
4146 default:
4147 // Don't do anything for most intrinsics.
4148 break;
4149
4150 // Vector shifts: check for immediate versions and lower them.
4151 // Note: This is done during DAG combining instead of DAG legalizing because
4152 // the build_vectors for 64-bit vector element shift counts are generally
4153 // not legal, and it is hard to see their values after they get legalized to
4154 // loads from a constant pool.
4155 case Intrinsic::arm_neon_vshifts:
4156 case Intrinsic::arm_neon_vshiftu:
4157 case Intrinsic::arm_neon_vshiftls:
4158 case Intrinsic::arm_neon_vshiftlu:
4159 case Intrinsic::arm_neon_vshiftn:
4160 case Intrinsic::arm_neon_vrshifts:
4161 case Intrinsic::arm_neon_vrshiftu:
4162 case Intrinsic::arm_neon_vrshiftn:
4163 case Intrinsic::arm_neon_vqshifts:
4164 case Intrinsic::arm_neon_vqshiftu:
4165 case Intrinsic::arm_neon_vqshiftsu:
4166 case Intrinsic::arm_neon_vqshiftns:
4167 case Intrinsic::arm_neon_vqshiftnu:
4168 case Intrinsic::arm_neon_vqshiftnsu:
4169 case Intrinsic::arm_neon_vqrshiftns:
4170 case Intrinsic::arm_neon_vqrshiftnu:
4171 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00004172 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004173 int64_t Cnt;
4174 unsigned VShiftOpc = 0;
4175
4176 switch (IntNo) {
4177 case Intrinsic::arm_neon_vshifts:
4178 case Intrinsic::arm_neon_vshiftu:
4179 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
4180 VShiftOpc = ARMISD::VSHL;
4181 break;
4182 }
4183 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
4184 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
4185 ARMISD::VSHRs : ARMISD::VSHRu);
4186 break;
4187 }
4188 return SDValue();
4189
4190 case Intrinsic::arm_neon_vshiftls:
4191 case Intrinsic::arm_neon_vshiftlu:
4192 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
4193 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00004194 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004195
4196 case Intrinsic::arm_neon_vrshifts:
4197 case Intrinsic::arm_neon_vrshiftu:
4198 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
4199 break;
4200 return SDValue();
4201
4202 case Intrinsic::arm_neon_vqshifts:
4203 case Intrinsic::arm_neon_vqshiftu:
4204 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
4205 break;
4206 return SDValue();
4207
4208 case Intrinsic::arm_neon_vqshiftsu:
4209 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
4210 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00004211 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004212
4213 case Intrinsic::arm_neon_vshiftn:
4214 case Intrinsic::arm_neon_vrshiftn:
4215 case Intrinsic::arm_neon_vqshiftns:
4216 case Intrinsic::arm_neon_vqshiftnu:
4217 case Intrinsic::arm_neon_vqshiftnsu:
4218 case Intrinsic::arm_neon_vqrshiftns:
4219 case Intrinsic::arm_neon_vqrshiftnu:
4220 case Intrinsic::arm_neon_vqrshiftnsu:
4221 // Narrowing shifts require an immediate right shift.
4222 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
4223 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00004224 llvm_unreachable("invalid shift count for narrowing vector shift "
4225 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004226
4227 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004228 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00004229 }
4230
4231 switch (IntNo) {
4232 case Intrinsic::arm_neon_vshifts:
4233 case Intrinsic::arm_neon_vshiftu:
4234 // Opcode already set above.
4235 break;
4236 case Intrinsic::arm_neon_vshiftls:
4237 case Intrinsic::arm_neon_vshiftlu:
4238 if (Cnt == VT.getVectorElementType().getSizeInBits())
4239 VShiftOpc = ARMISD::VSHLLi;
4240 else
4241 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
4242 ARMISD::VSHLLs : ARMISD::VSHLLu);
4243 break;
4244 case Intrinsic::arm_neon_vshiftn:
4245 VShiftOpc = ARMISD::VSHRN; break;
4246 case Intrinsic::arm_neon_vrshifts:
4247 VShiftOpc = ARMISD::VRSHRs; break;
4248 case Intrinsic::arm_neon_vrshiftu:
4249 VShiftOpc = ARMISD::VRSHRu; break;
4250 case Intrinsic::arm_neon_vrshiftn:
4251 VShiftOpc = ARMISD::VRSHRN; break;
4252 case Intrinsic::arm_neon_vqshifts:
4253 VShiftOpc = ARMISD::VQSHLs; break;
4254 case Intrinsic::arm_neon_vqshiftu:
4255 VShiftOpc = ARMISD::VQSHLu; break;
4256 case Intrinsic::arm_neon_vqshiftsu:
4257 VShiftOpc = ARMISD::VQSHLsu; break;
4258 case Intrinsic::arm_neon_vqshiftns:
4259 VShiftOpc = ARMISD::VQSHRNs; break;
4260 case Intrinsic::arm_neon_vqshiftnu:
4261 VShiftOpc = ARMISD::VQSHRNu; break;
4262 case Intrinsic::arm_neon_vqshiftnsu:
4263 VShiftOpc = ARMISD::VQSHRNsu; break;
4264 case Intrinsic::arm_neon_vqrshiftns:
4265 VShiftOpc = ARMISD::VQRSHRNs; break;
4266 case Intrinsic::arm_neon_vqrshiftnu:
4267 VShiftOpc = ARMISD::VQRSHRNu; break;
4268 case Intrinsic::arm_neon_vqrshiftnsu:
4269 VShiftOpc = ARMISD::VQRSHRNsu; break;
4270 }
4271
4272 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004273 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004274 }
4275
4276 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00004277 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004278 int64_t Cnt;
4279 unsigned VShiftOpc = 0;
4280
4281 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
4282 VShiftOpc = ARMISD::VSLI;
4283 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
4284 VShiftOpc = ARMISD::VSRI;
4285 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00004286 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004287 }
4288
4289 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
4290 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00004291 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004292 }
4293
4294 case Intrinsic::arm_neon_vqrshifts:
4295 case Intrinsic::arm_neon_vqrshiftu:
4296 // No immediate versions of these to check for.
4297 break;
4298 }
4299
4300 return SDValue();
4301}
4302
4303/// PerformShiftCombine - Checks for immediate versions of vector shifts and
4304/// lowers them. As with the vector shift intrinsics, this is done during DAG
4305/// combining instead of DAG legalizing because the build_vectors for 64-bit
4306/// vector element shift counts are generally not legal, and it is hard to see
4307/// their values after they get legalized to loads from a constant pool.
4308static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
4309 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00004310 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00004311
4312 // Nothing to be done for scalar shifts.
4313 if (! VT.isVector())
4314 return SDValue();
4315
4316 assert(ST->hasNEON() && "unexpected vector shift");
4317 int64_t Cnt;
4318
4319 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004320 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00004321
4322 case ISD::SHL:
4323 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
4324 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004325 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004326 break;
4327
4328 case ISD::SRA:
4329 case ISD::SRL:
4330 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
4331 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
4332 ARMISD::VSHRs : ARMISD::VSHRu);
4333 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004334 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004335 }
4336 }
4337 return SDValue();
4338}
4339
4340/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
4341/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
4342static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
4343 const ARMSubtarget *ST) {
4344 SDValue N0 = N->getOperand(0);
4345
4346 // Check for sign- and zero-extensions of vector extract operations of 8-
4347 // and 16-bit vector elements. NEON supports these directly. They are
4348 // handled during DAG combining because type legalization will promote them
4349 // to 32-bit types and it is messy to recognize the operations after that.
4350 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
4351 SDValue Vec = N0.getOperand(0);
4352 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004353 EVT VT = N->getValueType(0);
4354 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004355 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4356
Owen Anderson825b72b2009-08-11 20:47:22 +00004357 if (VT == MVT::i32 &&
4358 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00004359 TLI.isTypeLegal(Vec.getValueType())) {
4360
4361 unsigned Opc = 0;
4362 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004363 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00004364 case ISD::SIGN_EXTEND:
4365 Opc = ARMISD::VGETLANEs;
4366 break;
4367 case ISD::ZERO_EXTEND:
4368 case ISD::ANY_EXTEND:
4369 Opc = ARMISD::VGETLANEu;
4370 break;
4371 }
4372 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
4373 }
4374 }
4375
4376 return SDValue();
4377}
4378
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004379/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
4380/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
4381static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
4382 const ARMSubtarget *ST) {
4383 // If the target supports NEON, try to use vmax/vmin instructions for f32
4384 // selects like "x < y ? x : y". Unless the FiniteOnlyFPMath option is set,
4385 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
4386 // a NaN; only do the transformation when it matches that behavior.
4387
4388 // For now only do this when using NEON for FP operations; if using VFP, it
4389 // is not obvious that the benefit outweighs the cost of switching to the
4390 // NEON pipeline.
4391 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
4392 N->getValueType(0) != MVT::f32)
4393 return SDValue();
4394
4395 SDValue CondLHS = N->getOperand(0);
4396 SDValue CondRHS = N->getOperand(1);
4397 SDValue LHS = N->getOperand(2);
4398 SDValue RHS = N->getOperand(3);
4399 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
4400
4401 unsigned Opcode = 0;
4402 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00004403 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004404 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00004405 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004406 IsReversed = true ; // x CC y ? y : x
4407 } else {
4408 return SDValue();
4409 }
4410
Bob Wilsone742bb52010-02-24 22:15:53 +00004411 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004412 switch (CC) {
4413 default: break;
4414 case ISD::SETOLT:
4415 case ISD::SETOLE:
4416 case ISD::SETLT:
4417 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004418 case ISD::SETULT:
4419 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004420 // If LHS is NaN, an ordered comparison will be false and the result will
4421 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
4422 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4423 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
4424 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4425 break;
4426 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
4427 // will return -0, so vmin can only be used for unsafe math or if one of
4428 // the operands is known to be nonzero.
4429 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
4430 !UnsafeFPMath &&
4431 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4432 break;
4433 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004434 break;
4435
4436 case ISD::SETOGT:
4437 case ISD::SETOGE:
4438 case ISD::SETGT:
4439 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004440 case ISD::SETUGT:
4441 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004442 // If LHS is NaN, an ordered comparison will be false and the result will
4443 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
4444 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4445 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
4446 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4447 break;
4448 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
4449 // will return +0, so vmax can only be used for unsafe math or if one of
4450 // the operands is known to be nonzero.
4451 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
4452 !UnsafeFPMath &&
4453 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4454 break;
4455 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004456 break;
4457 }
4458
4459 if (!Opcode)
4460 return SDValue();
4461 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
4462}
4463
Dan Gohman475871a2008-07-27 21:46:04 +00004464SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004465 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004466 switch (N->getOpcode()) {
4467 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004468 case ISD::ADD: return PerformADDCombine(N, DCI);
4469 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004470 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbache5165492009-11-09 00:11:35 +00004471 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004472 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004473 case ISD::SHL:
4474 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004475 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004476 case ISD::SIGN_EXTEND:
4477 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004478 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
4479 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004480 }
Dan Gohman475871a2008-07-27 21:46:04 +00004481 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004482}
4483
Bill Wendlingaf566342009-08-15 21:21:19 +00004484bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
4485 if (!Subtarget->hasV6Ops())
4486 // Pre-v6 does not support unaligned mem access.
4487 return false;
Anton Korobeynikov90cfc132010-01-30 14:08:12 +00004488 else {
4489 // v6+ may or may not support unaligned mem access depending on the system
4490 // configuration.
4491 // FIXME: This is pretty conservative. Should we provide cmdline option to
4492 // control the behaviour?
Bill Wendlingaf566342009-08-15 21:21:19 +00004493 if (!Subtarget->isTargetDarwin())
4494 return false;
4495 }
4496
4497 switch (VT.getSimpleVT().SimpleTy) {
4498 default:
4499 return false;
4500 case MVT::i8:
4501 case MVT::i16:
4502 case MVT::i32:
4503 return true;
4504 // FIXME: VLD1 etc with standard alignment is legal.
4505 }
4506}
4507
Evan Chenge6c835f2009-08-14 20:09:37 +00004508static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
4509 if (V < 0)
4510 return false;
4511
4512 unsigned Scale = 1;
4513 switch (VT.getSimpleVT().SimpleTy) {
4514 default: return false;
4515 case MVT::i1:
4516 case MVT::i8:
4517 // Scale == 1;
4518 break;
4519 case MVT::i16:
4520 // Scale == 2;
4521 Scale = 2;
4522 break;
4523 case MVT::i32:
4524 // Scale == 4;
4525 Scale = 4;
4526 break;
4527 }
4528
4529 if ((V & (Scale - 1)) != 0)
4530 return false;
4531 V /= Scale;
4532 return V == (V & ((1LL << 5) - 1));
4533}
4534
4535static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
4536 const ARMSubtarget *Subtarget) {
4537 bool isNeg = false;
4538 if (V < 0) {
4539 isNeg = true;
4540 V = - V;
4541 }
4542
4543 switch (VT.getSimpleVT().SimpleTy) {
4544 default: return false;
4545 case MVT::i1:
4546 case MVT::i8:
4547 case MVT::i16:
4548 case MVT::i32:
4549 // + imm12 or - imm8
4550 if (isNeg)
4551 return V == (V & ((1LL << 8) - 1));
4552 return V == (V & ((1LL << 12) - 1));
4553 case MVT::f32:
4554 case MVT::f64:
4555 // Same as ARM mode. FIXME: NEON?
4556 if (!Subtarget->hasVFP2())
4557 return false;
4558 if ((V & 3) != 0)
4559 return false;
4560 V >>= 2;
4561 return V == (V & ((1LL << 8) - 1));
4562 }
4563}
4564
Evan Chengb01fad62007-03-12 23:30:29 +00004565/// isLegalAddressImmediate - Return true if the integer value can be used
4566/// as the offset of the target addressing mode for load / store of the
4567/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00004568static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004569 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00004570 if (V == 0)
4571 return true;
4572
Evan Cheng65011532009-03-09 19:15:00 +00004573 if (!VT.isSimple())
4574 return false;
4575
Evan Chenge6c835f2009-08-14 20:09:37 +00004576 if (Subtarget->isThumb1Only())
4577 return isLegalT1AddressImmediate(V, VT);
4578 else if (Subtarget->isThumb2())
4579 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00004580
Evan Chenge6c835f2009-08-14 20:09:37 +00004581 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00004582 if (V < 0)
4583 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00004584 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00004585 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004586 case MVT::i1:
4587 case MVT::i8:
4588 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00004589 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004590 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004591 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00004592 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004593 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004594 case MVT::f32:
4595 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00004596 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00004597 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00004598 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00004599 return false;
4600 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004601 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00004602 }
Evan Chenga8e29892007-01-19 07:51:42 +00004603}
4604
Evan Chenge6c835f2009-08-14 20:09:37 +00004605bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
4606 EVT VT) const {
4607 int Scale = AM.Scale;
4608 if (Scale < 0)
4609 return false;
4610
4611 switch (VT.getSimpleVT().SimpleTy) {
4612 default: return false;
4613 case MVT::i1:
4614 case MVT::i8:
4615 case MVT::i16:
4616 case MVT::i32:
4617 if (Scale == 1)
4618 return true;
4619 // r + r << imm
4620 Scale = Scale & ~1;
4621 return Scale == 2 || Scale == 4 || Scale == 8;
4622 case MVT::i64:
4623 // r + r
4624 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
4625 return true;
4626 return false;
4627 case MVT::isVoid:
4628 // Note, we allow "void" uses (basically, uses that aren't loads or
4629 // stores), because arm allows folding a scale into many arithmetic
4630 // operations. This should be made more precise and revisited later.
4631
4632 // Allow r << imm, but the imm has to be a multiple of two.
4633 if (Scale & 1) return false;
4634 return isPowerOf2_32(Scale);
4635 }
4636}
4637
Chris Lattner37caf8c2007-04-09 23:33:39 +00004638/// isLegalAddressingMode - Return true if the addressing mode represented
4639/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004640bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004641 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004642 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00004643 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00004644 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004645
Chris Lattner37caf8c2007-04-09 23:33:39 +00004646 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004647 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004648 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004649
Chris Lattner37caf8c2007-04-09 23:33:39 +00004650 switch (AM.Scale) {
4651 case 0: // no scale reg, must be "r+i" or "r", or "i".
4652 break;
4653 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00004654 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00004655 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004656 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00004657 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004658 // ARM doesn't support any R+R*scale+imm addr modes.
4659 if (AM.BaseOffs)
4660 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004661
Bob Wilson2c7dab12009-04-08 17:55:28 +00004662 if (!VT.isSimple())
4663 return false;
4664
Evan Chenge6c835f2009-08-14 20:09:37 +00004665 if (Subtarget->isThumb2())
4666 return isLegalT2ScaledAddressingMode(AM, VT);
4667
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004668 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00004669 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00004670 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004671 case MVT::i1:
4672 case MVT::i8:
4673 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004674 if (Scale < 0) Scale = -Scale;
4675 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004676 return true;
4677 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00004678 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00004679 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00004680 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004681 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004682 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004683 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00004684 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004685
Owen Anderson825b72b2009-08-11 20:47:22 +00004686 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004687 // Note, we allow "void" uses (basically, uses that aren't loads or
4688 // stores), because arm allows folding a scale into many arithmetic
4689 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004690
Chris Lattner37caf8c2007-04-09 23:33:39 +00004691 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00004692 if (Scale & 1) return false;
4693 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00004694 }
4695 break;
Evan Chengb01fad62007-03-12 23:30:29 +00004696 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00004697 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00004698}
4699
Evan Cheng77e47512009-11-11 19:05:52 +00004700/// isLegalICmpImmediate - Return true if the specified immediate is legal
4701/// icmp immediate, that is the target has icmp instructions which can compare
4702/// a register against the immediate without having to materialize the
4703/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00004704bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00004705 if (!Subtarget->isThumb())
4706 return ARM_AM::getSOImmVal(Imm) != -1;
4707 if (Subtarget->isThumb2())
4708 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00004709 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00004710}
4711
Owen Andersone50ed302009-08-10 22:56:29 +00004712static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004713 bool isSEXTLoad, SDValue &Base,
4714 SDValue &Offset, bool &isInc,
4715 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00004716 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4717 return false;
4718
Owen Anderson825b72b2009-08-11 20:47:22 +00004719 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00004720 // AddressingMode 3
4721 Base = Ptr->getOperand(0);
4722 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004723 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004724 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004725 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004726 isInc = false;
4727 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4728 return true;
4729 }
4730 }
4731 isInc = (Ptr->getOpcode() == ISD::ADD);
4732 Offset = Ptr->getOperand(1);
4733 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004734 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00004735 // AddressingMode 2
4736 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004737 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004738 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004739 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004740 isInc = false;
4741 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4742 Base = Ptr->getOperand(0);
4743 return true;
4744 }
4745 }
4746
4747 if (Ptr->getOpcode() == ISD::ADD) {
4748 isInc = true;
4749 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
4750 if (ShOpcVal != ARM_AM::no_shift) {
4751 Base = Ptr->getOperand(1);
4752 Offset = Ptr->getOperand(0);
4753 } else {
4754 Base = Ptr->getOperand(0);
4755 Offset = Ptr->getOperand(1);
4756 }
4757 return true;
4758 }
4759
4760 isInc = (Ptr->getOpcode() == ISD::ADD);
4761 Base = Ptr->getOperand(0);
4762 Offset = Ptr->getOperand(1);
4763 return true;
4764 }
4765
Jim Grosbache5165492009-11-09 00:11:35 +00004766 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00004767 return false;
4768}
4769
Owen Andersone50ed302009-08-10 22:56:29 +00004770static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004771 bool isSEXTLoad, SDValue &Base,
4772 SDValue &Offset, bool &isInc,
4773 SelectionDAG &DAG) {
4774 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4775 return false;
4776
4777 Base = Ptr->getOperand(0);
4778 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
4779 int RHSC = (int)RHS->getZExtValue();
4780 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
4781 assert(Ptr->getOpcode() == ISD::ADD);
4782 isInc = false;
4783 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4784 return true;
4785 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
4786 isInc = Ptr->getOpcode() == ISD::ADD;
4787 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
4788 return true;
4789 }
4790 }
4791
4792 return false;
4793}
4794
Evan Chenga8e29892007-01-19 07:51:42 +00004795/// getPreIndexedAddressParts - returns true by value, base pointer and
4796/// offset pointer and addressing mode by reference if the node's address
4797/// can be legally represented as pre-indexed load / store address.
4798bool
Dan Gohman475871a2008-07-27 21:46:04 +00004799ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
4800 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004801 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004802 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004803 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004804 return false;
4805
Owen Andersone50ed302009-08-10 22:56:29 +00004806 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004807 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004808 bool isSEXTLoad = false;
4809 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4810 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004811 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004812 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4813 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
4814 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004815 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004816 } else
4817 return false;
4818
4819 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004820 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004821 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004822 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
4823 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004824 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004825 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00004826 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00004827 if (!isLegal)
4828 return false;
4829
4830 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
4831 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004832}
4833
4834/// getPostIndexedAddressParts - returns true by value, base pointer and
4835/// offset pointer and addressing mode by reference if this node can be
4836/// combined with a load / store to form a post-indexed load / store.
4837bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00004838 SDValue &Base,
4839 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004840 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004841 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004842 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004843 return false;
4844
Owen Andersone50ed302009-08-10 22:56:29 +00004845 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004846 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004847 bool isSEXTLoad = false;
4848 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004849 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00004850 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00004851 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4852 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004853 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00004854 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00004855 } else
4856 return false;
4857
4858 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004859 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004860 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004861 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00004862 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004863 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004864 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
4865 isInc, DAG);
4866 if (!isLegal)
4867 return false;
4868
Evan Cheng28dad2a2010-05-18 21:31:17 +00004869 if (Ptr != Base) {
4870 // Swap base ptr and offset to catch more post-index load / store when
4871 // it's legal. In Thumb2 mode, offset must be an immediate.
4872 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
4873 !Subtarget->isThumb2())
4874 std::swap(Base, Offset);
4875
4876 // Post-indexed load / store update the base pointer.
4877 if (Ptr != Base)
4878 return false;
4879 }
4880
Evan Chenge88d5ce2009-07-02 07:28:31 +00004881 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
4882 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004883}
4884
Dan Gohman475871a2008-07-27 21:46:04 +00004885void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00004886 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004887 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004888 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00004889 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00004890 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004891 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004892 switch (Op.getOpcode()) {
4893 default: break;
4894 case ARMISD::CMOV: {
4895 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00004896 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004897 if (KnownZero == 0 && KnownOne == 0) return;
4898
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004899 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00004900 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
4901 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004902 KnownZero &= KnownZeroRHS;
4903 KnownOne &= KnownOneRHS;
4904 return;
4905 }
4906 }
4907}
4908
4909//===----------------------------------------------------------------------===//
4910// ARM Inline Assembly Support
4911//===----------------------------------------------------------------------===//
4912
4913/// getConstraintType - Given a constraint letter, return the type of
4914/// constraint it is for this target.
4915ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00004916ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
4917 if (Constraint.size() == 1) {
4918 switch (Constraint[0]) {
4919 default: break;
4920 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004921 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00004922 }
Evan Chenga8e29892007-01-19 07:51:42 +00004923 }
Chris Lattner4234f572007-03-25 02:14:49 +00004924 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00004925}
4926
Bob Wilson2dc4f542009-03-20 22:42:55 +00004927std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00004928ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004929 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004930 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004931 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00004932 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004933 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004934 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004935 return std::make_pair(0U, ARM::tGPRRegisterClass);
4936 else
4937 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004938 case 'r':
4939 return std::make_pair(0U, ARM::GPRRegisterClass);
4940 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004941 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004942 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00004943 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004944 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00004945 if (VT.getSizeInBits() == 128)
4946 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004947 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004948 }
4949 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00004950 if (StringRef("{cc}").equals_lower(Constraint))
4951 return std::make_pair(0U, ARM::CCRRegisterClass);
4952
Evan Chenga8e29892007-01-19 07:51:42 +00004953 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
4954}
4955
4956std::vector<unsigned> ARMTargetLowering::
4957getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004958 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004959 if (Constraint.size() != 1)
4960 return std::vector<unsigned>();
4961
4962 switch (Constraint[0]) { // GCC ARM Constraint Letters
4963 default: break;
4964 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004965 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4966 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4967 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004968 case 'r':
4969 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4970 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4971 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
4972 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004973 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004974 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004975 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
4976 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
4977 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
4978 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
4979 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
4980 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
4981 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
4982 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00004983 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004984 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
4985 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
4986 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
4987 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00004988 if (VT.getSizeInBits() == 128)
4989 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
4990 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004991 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004992 }
4993
4994 return std::vector<unsigned>();
4995}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004996
4997/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
4998/// vector. If it is invalid, don't add anything to Ops.
4999void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
5000 char Constraint,
5001 bool hasMemory,
5002 std::vector<SDValue>&Ops,
5003 SelectionDAG &DAG) const {
5004 SDValue Result(0, 0);
5005
5006 switch (Constraint) {
5007 default: break;
5008 case 'I': case 'J': case 'K': case 'L':
5009 case 'M': case 'N': case 'O':
5010 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
5011 if (!C)
5012 return;
5013
5014 int64_t CVal64 = C->getSExtValue();
5015 int CVal = (int) CVal64;
5016 // None of these constraints allow values larger than 32 bits. Check
5017 // that the value fits in an int.
5018 if (CVal != CVal64)
5019 return;
5020
5021 switch (Constraint) {
5022 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005023 if (Subtarget->isThumb1Only()) {
5024 // This must be a constant between 0 and 255, for ADD
5025 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005026 if (CVal >= 0 && CVal <= 255)
5027 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005028 } else if (Subtarget->isThumb2()) {
5029 // A constant that can be used as an immediate value in a
5030 // data-processing instruction.
5031 if (ARM_AM::getT2SOImmVal(CVal) != -1)
5032 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005033 } else {
5034 // A constant that can be used as an immediate value in a
5035 // data-processing instruction.
5036 if (ARM_AM::getSOImmVal(CVal) != -1)
5037 break;
5038 }
5039 return;
5040
5041 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005042 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005043 // This must be a constant between -255 and -1, for negated ADD
5044 // immediates. This can be used in GCC with an "n" modifier that
5045 // prints the negated value, for use with SUB instructions. It is
5046 // not useful otherwise but is implemented for compatibility.
5047 if (CVal >= -255 && CVal <= -1)
5048 break;
5049 } else {
5050 // This must be a constant between -4095 and 4095. It is not clear
5051 // what this constraint is intended for. Implemented for
5052 // compatibility with GCC.
5053 if (CVal >= -4095 && CVal <= 4095)
5054 break;
5055 }
5056 return;
5057
5058 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005059 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005060 // A 32-bit value where only one byte has a nonzero value. Exclude
5061 // zero to match GCC. This constraint is used by GCC internally for
5062 // constants that can be loaded with a move/shift combination.
5063 // It is not useful otherwise but is implemented for compatibility.
5064 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
5065 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005066 } else if (Subtarget->isThumb2()) {
5067 // A constant whose bitwise inverse can be used as an immediate
5068 // value in a data-processing instruction. This can be used in GCC
5069 // with a "B" modifier that prints the inverted value, for use with
5070 // BIC and MVN instructions. It is not useful otherwise but is
5071 // implemented for compatibility.
5072 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
5073 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005074 } else {
5075 // A constant whose bitwise inverse can be used as an immediate
5076 // value in a data-processing instruction. This can be used in GCC
5077 // with a "B" modifier that prints the inverted value, for use with
5078 // BIC and MVN instructions. It is not useful otherwise but is
5079 // implemented for compatibility.
5080 if (ARM_AM::getSOImmVal(~CVal) != -1)
5081 break;
5082 }
5083 return;
5084
5085 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005086 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005087 // This must be a constant between -7 and 7,
5088 // for 3-operand ADD/SUB immediate instructions.
5089 if (CVal >= -7 && CVal < 7)
5090 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005091 } else if (Subtarget->isThumb2()) {
5092 // A constant whose negation can be used as an immediate value in a
5093 // data-processing instruction. This can be used in GCC with an "n"
5094 // modifier that prints the negated value, for use with SUB
5095 // instructions. It is not useful otherwise but is implemented for
5096 // compatibility.
5097 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
5098 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005099 } else {
5100 // A constant whose negation can be used as an immediate value in a
5101 // data-processing instruction. This can be used in GCC with an "n"
5102 // modifier that prints the negated value, for use with SUB
5103 // instructions. It is not useful otherwise but is implemented for
5104 // compatibility.
5105 if (ARM_AM::getSOImmVal(-CVal) != -1)
5106 break;
5107 }
5108 return;
5109
5110 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005111 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005112 // This must be a multiple of 4 between 0 and 1020, for
5113 // ADD sp + immediate.
5114 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
5115 break;
5116 } else {
5117 // A power of two or a constant between 0 and 32. This is used in
5118 // GCC for the shift amount on shifted register operands, but it is
5119 // useful in general for any shift amounts.
5120 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
5121 break;
5122 }
5123 return;
5124
5125 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005126 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005127 // This must be a constant between 0 and 31, for shift amounts.
5128 if (CVal >= 0 && CVal <= 31)
5129 break;
5130 }
5131 return;
5132
5133 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005134 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005135 // This must be a multiple of 4 between -508 and 508, for
5136 // ADD/SUB sp = sp + immediate.
5137 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
5138 break;
5139 }
5140 return;
5141 }
5142 Result = DAG.getTargetConstant(CVal, Op.getValueType());
5143 break;
5144 }
5145
5146 if (Result.getNode()) {
5147 Ops.push_back(Result);
5148 return;
5149 }
5150 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
5151 Ops, DAG);
5152}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00005153
5154bool
5155ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
5156 // The ARM target isn't yet aware of offsets.
5157 return false;
5158}
Evan Cheng39382422009-10-28 01:44:26 +00005159
5160int ARM::getVFPf32Imm(const APFloat &FPImm) {
5161 APInt Imm = FPImm.bitcastToAPInt();
5162 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
5163 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
5164 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
5165
5166 // We can handle 4 bits of mantissa.
5167 // mantissa = (16+UInt(e:f:g:h))/16.
5168 if (Mantissa & 0x7ffff)
5169 return -1;
5170 Mantissa >>= 19;
5171 if ((Mantissa & 0xf) != Mantissa)
5172 return -1;
5173
5174 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
5175 if (Exp < -3 || Exp > 4)
5176 return -1;
5177 Exp = ((Exp+3) & 0x7) ^ 4;
5178
5179 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
5180}
5181
5182int ARM::getVFPf64Imm(const APFloat &FPImm) {
5183 APInt Imm = FPImm.bitcastToAPInt();
5184 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
5185 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
5186 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
5187
5188 // We can handle 4 bits of mantissa.
5189 // mantissa = (16+UInt(e:f:g:h))/16.
5190 if (Mantissa & 0xffffffffffffLL)
5191 return -1;
5192 Mantissa >>= 48;
5193 if ((Mantissa & 0xf) != Mantissa)
5194 return -1;
5195
5196 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
5197 if (Exp < -3 || Exp > 4)
5198 return -1;
5199 Exp = ((Exp+3) & 0x7) ^ 4;
5200
5201 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
5202}
5203
5204/// isFPImmLegal - Returns true if the target can instruction select the
5205/// specified FP immediate natively. If false, the legalizer will
5206/// materialize the FP immediate as a load from a constant pool.
5207bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
5208 if (!Subtarget->hasVFP3())
5209 return false;
5210 if (VT == MVT::f32)
5211 return ARM::getVFPf32Imm(Imm) != -1;
5212 if (VT == MVT::f64)
5213 return ARM::getVFPf64Imm(Imm) != -1;
5214 return false;
5215}