blob: d0c40229d4035c37425b18a281dc3c75a9d326ed [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Dale Johannesen51e28e62010-06-03 21:09:53 +000015#define DEBUG_TYPE "arm-isel"
Evan Chenga8e29892007-01-19 07:51:42 +000016#include "ARM.h"
17#include "ARMAddressingModes.h"
18#include "ARMConstantPoolValue.h"
19#include "ARMISelLowering.h"
20#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000021#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000022#include "ARMRegisterInfo.h"
23#include "ARMSubtarget.h"
24#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000025#include "ARMTargetObjectFile.h"
Evan Chenga8e29892007-01-19 07:51:42 +000026#include "llvm/CallingConv.h"
27#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000028#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000029#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000030#include "llvm/Instruction.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000031#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000032#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000033#include "llvm/CodeGen/CallingConvLower.h"
Evan Chenga8e29892007-01-19 07:51:42 +000034#include "llvm/CodeGen/MachineBasicBlock.h"
35#include "llvm/CodeGen/MachineFrameInfo.h"
36#include "llvm/CodeGen/MachineFunction.h"
37#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000038#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000039#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000040#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000041#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000042#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000043#include "llvm/ADT/VectorExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000044#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000045#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000046#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000047#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000048#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000049#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000050using namespace llvm;
51
Dale Johannesen51e28e62010-06-03 21:09:53 +000052STATISTIC(NumTailCalls, "Number of tail calls");
53
54// This option should go away when tail calls fully work.
55static cl::opt<bool>
56EnableARMTailCalls("arm-tail-calls", cl::Hidden,
57 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
Dale Johannesenc66cdf72010-06-18 19:00:18 +000058 cl::init(true));
Dale Johannesen51e28e62010-06-03 21:09:53 +000059
Jim Grosbache7b52522010-04-14 22:28:31 +000060static cl::opt<bool>
61EnableARMLongCalls("arm-long-calls", cl::Hidden,
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);
Bob Wilson07f6e802010-06-16 21:34:01 +0000112 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Anton Korobeynikov8e6c2b92009-08-21 12:40:35 +0000113 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
Bob Wilsond0910c42010-04-06 22:02:24 +0000114 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
115 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000116 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000117 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
118 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
119 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000120 }
121
122 // Promote all bit-wise operations.
123 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000124 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000125 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
126 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000127 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000128 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000129 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000130 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000131 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000132 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000133 }
Bob Wilson16330762009-09-16 00:17:28 +0000134
135 // Neon does not support vector divide/remainder operations.
136 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
137 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
138 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
139 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
140 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
141 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000142}
143
Owen Andersone50ed302009-08-10 22:56:29 +0000144void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000145 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000146 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000147}
148
Owen Andersone50ed302009-08-10 22:56:29 +0000149void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000150 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000151 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000152}
153
Chris Lattnerf0144122009-07-28 03:13:23 +0000154static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
155 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000156 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000157
Chris Lattner80ec2792009-08-02 00:34:36 +0000158 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000159}
160
Evan Chenga8e29892007-01-19 07:51:42 +0000161ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000162 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000163 Subtarget = &TM.getSubtarget<ARMSubtarget>();
164
Evan Chengb1df8f22007-04-27 08:15:43 +0000165 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000166 // Uses VFP for Thumb libfuncs if available.
167 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
168 // Single-precision floating-point arithmetic.
169 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
170 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
171 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
172 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000173
Evan Chengb1df8f22007-04-27 08:15:43 +0000174 // Double-precision floating-point arithmetic.
175 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
176 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
177 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
178 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000179
Evan Chengb1df8f22007-04-27 08:15:43 +0000180 // Single-precision comparisons.
181 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
182 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
183 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
184 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
185 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
186 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
187 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
188 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000189
Evan Chengb1df8f22007-04-27 08:15:43 +0000190 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
191 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
192 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
193 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
194 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
195 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
196 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
197 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000198
Evan Chengb1df8f22007-04-27 08:15:43 +0000199 // Double-precision comparisons.
200 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
201 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
202 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
203 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
204 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
205 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
206 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
207 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000208
Evan Chengb1df8f22007-04-27 08:15:43 +0000209 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
214 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
215 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
216 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000217
Evan Chengb1df8f22007-04-27 08:15:43 +0000218 // Floating-point to integer conversions.
219 // i64 conversions are done via library routines even when generating VFP
220 // instructions, so use the same ones.
221 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
222 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
223 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
224 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000225
Evan Chengb1df8f22007-04-27 08:15:43 +0000226 // Conversions between floating types.
227 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
228 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
229
230 // Integer to floating-point conversions.
231 // i64 conversions are done via library routines even when generating VFP
232 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000233 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
234 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000235 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
236 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
237 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
238 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
239 }
Evan Chenga8e29892007-01-19 07:51:42 +0000240 }
241
Bob Wilson2f954612009-05-22 17:38:41 +0000242 // These libcalls are not available in 32-bit.
243 setLibcallName(RTLIB::SHL_I128, 0);
244 setLibcallName(RTLIB::SRL_I128, 0);
245 setLibcallName(RTLIB::SRA_I128, 0);
246
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000247 // Libcalls should use the AAPCS base standard ABI, even if hard float
248 // is in effect, as per the ARM RTABI specification, section 4.1.2.
249 if (Subtarget->isAAPCS_ABI()) {
250 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
251 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
252 CallingConv::ARM_AAPCS);
253 }
254 }
255
David Goodwinf1daf7d2009-07-08 23:10:31 +0000256 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000258 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000259 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000260 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
262 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000263
Owen Anderson825b72b2009-08-11 20:47:22 +0000264 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000265 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000266
267 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 addDRTypeForNEON(MVT::v2f32);
269 addDRTypeForNEON(MVT::v8i8);
270 addDRTypeForNEON(MVT::v4i16);
271 addDRTypeForNEON(MVT::v2i32);
272 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000273
Owen Anderson825b72b2009-08-11 20:47:22 +0000274 addQRTypeForNEON(MVT::v4f32);
275 addQRTypeForNEON(MVT::v2f64);
276 addQRTypeForNEON(MVT::v16i8);
277 addQRTypeForNEON(MVT::v8i16);
278 addQRTypeForNEON(MVT::v4i32);
279 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000280
Bob Wilson74dc72e2009-09-15 23:55:57 +0000281 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
282 // neither Neon nor VFP support any arithmetic operations on it.
283 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
284 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
285 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
286 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
287 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
288 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
289 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
290 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
291 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
292 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
293 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
294 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
295 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
296 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
297 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
298 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
299 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
300 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
301 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
302 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
303 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
304 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
305 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
306 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
307
Bob Wilson642b3292009-09-16 00:32:15 +0000308 // Neon does not support some operations on v1i64 and v2i64 types.
309 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
310 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
311 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
312 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
313
Bob Wilson5bafff32009-06-22 23:27:02 +0000314 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
315 setTargetDAGCombine(ISD::SHL);
316 setTargetDAGCombine(ISD::SRL);
317 setTargetDAGCombine(ISD::SRA);
318 setTargetDAGCombine(ISD::SIGN_EXTEND);
319 setTargetDAGCombine(ISD::ZERO_EXTEND);
320 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000321 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson5bafff32009-06-22 23:27:02 +0000322 }
323
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000324 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000325
326 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000327 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000328
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000329 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000330 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000331
Evan Chenga8e29892007-01-19 07:51:42 +0000332 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000333 if (!Subtarget->isThumb1Only()) {
334 for (unsigned im = (unsigned)ISD::PRE_INC;
335 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000336 setIndexedLoadAction(im, MVT::i1, Legal);
337 setIndexedLoadAction(im, MVT::i8, Legal);
338 setIndexedLoadAction(im, MVT::i16, Legal);
339 setIndexedLoadAction(im, MVT::i32, Legal);
340 setIndexedStoreAction(im, MVT::i1, Legal);
341 setIndexedStoreAction(im, MVT::i8, Legal);
342 setIndexedStoreAction(im, MVT::i16, Legal);
343 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000344 }
Evan Chenga8e29892007-01-19 07:51:42 +0000345 }
346
347 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000348 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000349 setOperationAction(ISD::MUL, MVT::i64, Expand);
350 setOperationAction(ISD::MULHU, MVT::i32, Expand);
351 setOperationAction(ISD::MULHS, MVT::i32, Expand);
352 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
353 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000354 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 setOperationAction(ISD::MUL, MVT::i64, Expand);
356 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000357 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000358 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000359 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000360 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000361 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000362 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000363 setOperationAction(ISD::SRL, MVT::i64, Custom);
364 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000365
366 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000368 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000369 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000370 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000372
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000373 // Only ARMv6 has BSWAP.
374 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000375 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000376
Evan Chenga8e29892007-01-19 07:51:42 +0000377 // These are expanded into libcalls.
Jim Grosbach29402132010-05-05 23:44:43 +0000378 if (!Subtarget->hasDivide()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000379 // v7M has a hardware divider
380 setOperationAction(ISD::SDIV, MVT::i32, Expand);
381 setOperationAction(ISD::UDIV, MVT::i32, Expand);
382 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000383 setOperationAction(ISD::SREM, MVT::i32, Expand);
384 setOperationAction(ISD::UREM, MVT::i32, Expand);
385 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
386 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000387
Owen Anderson825b72b2009-08-11 20:47:22 +0000388 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
389 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
390 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
391 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000392 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000393
Evan Chengfb3611d2010-05-11 07:26:32 +0000394 setOperationAction(ISD::TRAP, MVT::Other, Legal);
395
Evan Chenga8e29892007-01-19 07:51:42 +0000396 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000397 setOperationAction(ISD::VASTART, MVT::Other, Custom);
398 setOperationAction(ISD::VAARG, MVT::Other, Expand);
399 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
400 setOperationAction(ISD::VAEND, MVT::Other, Expand);
401 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
402 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000403 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
404 // FIXME: Shouldn't need this, since no register is used, but the legalizer
405 // doesn't yet know how to not do that for SjLj.
406 setExceptionSelectorRegister(ARM::R0);
Evan Cheng3a1588a2010-04-15 22:20:34 +0000407 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Jim Grosbach7072cf62010-06-17 02:02:03 +0000408 // Handle atomics directly for ARMv[67] (except for Thumb1), otherwise
409 // use the default expansion.
Jim Grosbach68741be2010-06-18 22:35:32 +0000410 bool canHandleAtomics =
Jim Grosbach7072cf62010-06-17 02:02:03 +0000411 (Subtarget->hasV7Ops() ||
Jim Grosbach68741be2010-06-18 22:35:32 +0000412 (Subtarget->hasV6Ops() && !Subtarget->isThumb1Only()));
413 if (canHandleAtomics) {
414 // membarrier needs custom lowering; the rest are legal and handled
415 // normally.
416 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
417 } else {
418 // Set them all for expansion, which will force libcalls.
419 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
420 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Expand);
421 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Expand);
422 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000423 setOperationAction(ISD::ATOMIC_SWAP, MVT::i8, Expand);
424 setOperationAction(ISD::ATOMIC_SWAP, MVT::i16, Expand);
425 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000426 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i8, Expand);
427 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i16, Expand);
428 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
429 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Expand);
430 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Expand);
431 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
432 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i8, Expand);
433 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i16, Expand);
434 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
435 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i8, Expand);
436 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i16, Expand);
437 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
438 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i8, Expand);
439 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i16, Expand);
440 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
441 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i8, Expand);
442 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i16, Expand);
443 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
444 }
445 // 64-bit versions are always libcalls (for now)
446 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000447 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000448 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Expand);
449 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Expand);
450 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Expand);
451 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Expand);
452 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Expand);
453 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000454
Jim Grosbach4b77f6a2010-05-07 18:34:55 +0000455 // If the subtarget does not have extract instructions, sign_extend_inreg
456 // needs to be expanded. Extract is available in ARM mode on v6 and up,
457 // and on most Thumb2 implementations.
458 if ((!Subtarget->isThumb() && !Subtarget->hasV6Ops())
459 || (Subtarget->isThumb2() && !Subtarget->hasT2ExtractPack())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000460 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
461 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000462 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000464
David Goodwinf1daf7d2009-07-08 23:10:31 +0000465 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000466 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
467 // iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000468 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000469
470 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000471 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000472
Owen Anderson825b72b2009-08-11 20:47:22 +0000473 setOperationAction(ISD::SETCC, MVT::i32, Expand);
474 setOperationAction(ISD::SETCC, MVT::f32, Expand);
475 setOperationAction(ISD::SETCC, MVT::f64, Expand);
476 setOperationAction(ISD::SELECT, MVT::i32, Expand);
477 setOperationAction(ISD::SELECT, MVT::f32, Expand);
478 setOperationAction(ISD::SELECT, MVT::f64, Expand);
479 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
480 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
481 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000482
Owen Anderson825b72b2009-08-11 20:47:22 +0000483 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
484 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
485 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
486 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
487 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000488
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000489 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000490 setOperationAction(ISD::FSIN, MVT::f64, Expand);
491 setOperationAction(ISD::FSIN, MVT::f32, Expand);
492 setOperationAction(ISD::FCOS, MVT::f32, Expand);
493 setOperationAction(ISD::FCOS, MVT::f64, Expand);
494 setOperationAction(ISD::FREM, MVT::f64, Expand);
495 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000496 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000497 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
498 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000499 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000500 setOperationAction(ISD::FPOW, MVT::f64, Expand);
501 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000502
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000503 // Various VFP goodness
504 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000505 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
506 if (Subtarget->hasVFP2()) {
507 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
508 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
509 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
510 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
511 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000512 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000513 if (!Subtarget->hasFP16()) {
514 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
515 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000516 }
Evan Cheng110cf482008-04-01 01:50:16 +0000517 }
Evan Chenga8e29892007-01-19 07:51:42 +0000518
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000519 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000520 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000521 setTargetDAGCombine(ISD::ADD);
522 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000523 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000524
Evan Chenga8e29892007-01-19 07:51:42 +0000525 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000526
Evan Chengf7d87ee2010-05-21 00:43:17 +0000527 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
528 setSchedulingPreference(Sched::RegPressure);
529 else
530 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000531
Evan Chengbc9b7542009-08-15 07:59:10 +0000532 // FIXME: If-converter should use instruction latency to determine
533 // profitability rather than relying on fixed limits.
534 if (Subtarget->getCPUString() == "generic") {
535 // Generic (and overly aggressive) if-conversion limits.
536 setIfCvtBlockSizeLimit(10);
537 setIfCvtDupBlockSizeLimit(2);
Jim Grosbach35075a72010-03-24 16:15:14 +0000538 } else if (Subtarget->hasV7Ops()) {
Jim Grosbachfceabef2010-03-24 00:03:13 +0000539 setIfCvtBlockSizeLimit(3);
540 setIfCvtDupBlockSizeLimit(1);
Evan Chengbc9b7542009-08-15 07:59:10 +0000541 } else if (Subtarget->hasV6Ops()) {
542 setIfCvtBlockSizeLimit(2);
543 setIfCvtDupBlockSizeLimit(1);
544 } else {
545 setIfCvtBlockSizeLimit(3);
546 setIfCvtDupBlockSizeLimit(2);
Evan Cheng8557c2b2009-06-19 01:51:50 +0000547 }
548
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000549 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Bob Wilsone6abdff2009-05-18 20:55:32 +0000550 // Do not enable CodePlacementOpt for now: it currently runs after the
551 // ARMConstantIslandPass and messes up branch relaxation and placement
552 // of constant islands.
553 // benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000554}
555
Evan Chenga8e29892007-01-19 07:51:42 +0000556const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
557 switch (Opcode) {
558 default: return 0;
559 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000560 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
561 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000562 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000563 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
564 case ARMISD::tCALL: return "ARMISD::tCALL";
565 case ARMISD::BRCOND: return "ARMISD::BRCOND";
566 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000567 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000568 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
569 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
570 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000571 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000572 case ARMISD::CMPFP: return "ARMISD::CMPFP";
573 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
574 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
575 case ARMISD::CMOV: return "ARMISD::CMOV";
576 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000577
Jim Grosbach3482c802010-01-18 19:58:49 +0000578 case ARMISD::RBIT: return "ARMISD::RBIT";
579
Bob Wilson76a312b2010-03-19 22:51:32 +0000580 case ARMISD::FTOSI: return "ARMISD::FTOSI";
581 case ARMISD::FTOUI: return "ARMISD::FTOUI";
582 case ARMISD::SITOF: return "ARMISD::SITOF";
583 case ARMISD::UITOF: return "ARMISD::UITOF";
584
Evan Chenga8e29892007-01-19 07:51:42 +0000585 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
586 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
587 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000588
Jim Grosbache5165492009-11-09 00:11:35 +0000589 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
590 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000591
Evan Chengc5942082009-10-28 06:55:03 +0000592 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
593 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
594
Dale Johannesen51e28e62010-06-03 21:09:53 +0000595 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
596
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000597 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000598
Evan Cheng86198642009-08-07 00:34:42 +0000599 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
600
Jim Grosbach3728e962009-12-10 00:11:09 +0000601 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
602 case ARMISD::SYNCBARRIER: return "ARMISD::SYNCBARRIER";
603
Bob Wilson5bafff32009-06-22 23:27:02 +0000604 case ARMISD::VCEQ: return "ARMISD::VCEQ";
605 case ARMISD::VCGE: return "ARMISD::VCGE";
606 case ARMISD::VCGEU: return "ARMISD::VCGEU";
607 case ARMISD::VCGT: return "ARMISD::VCGT";
608 case ARMISD::VCGTU: return "ARMISD::VCGTU";
609 case ARMISD::VTST: return "ARMISD::VTST";
610
611 case ARMISD::VSHL: return "ARMISD::VSHL";
612 case ARMISD::VSHRs: return "ARMISD::VSHRs";
613 case ARMISD::VSHRu: return "ARMISD::VSHRu";
614 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
615 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
616 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
617 case ARMISD::VSHRN: return "ARMISD::VSHRN";
618 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
619 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
620 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
621 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
622 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
623 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
624 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
625 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
626 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
627 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
628 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
629 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
630 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
631 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000632 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000633 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000634 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000635 case ARMISD::VREV64: return "ARMISD::VREV64";
636 case ARMISD::VREV32: return "ARMISD::VREV32";
637 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000638 case ARMISD::VZIP: return "ARMISD::VZIP";
639 case ARMISD::VUZP: return "ARMISD::VUZP";
640 case ARMISD::VTRN: return "ARMISD::VTRN";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000641 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000642 case ARMISD::FMAX: return "ARMISD::FMAX";
643 case ARMISD::FMIN: return "ARMISD::FMIN";
Evan Chenga8e29892007-01-19 07:51:42 +0000644 }
645}
646
Evan Cheng06b666c2010-05-15 02:18:07 +0000647/// getRegClassFor - Return the register class that should be used for the
648/// specified value type.
649TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
650 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
651 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
652 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000653 if (Subtarget->hasNEON()) {
654 if (VT == MVT::v4i64)
655 return ARM::QQPRRegisterClass;
656 else if (VT == MVT::v8i64)
657 return ARM::QQQQPRRegisterClass;
658 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000659 return TargetLowering::getRegClassFor(VT);
660}
661
Bill Wendlingb4202b82009-07-01 18:50:55 +0000662/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000663unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Evan Cheng048e36f2009-10-02 06:57:25 +0000664 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 0 : 1;
Bill Wendling20c568f2009-06-30 22:38:32 +0000665}
666
Evan Cheng1cc39842010-05-20 23:26:43 +0000667Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000668 unsigned NumVals = N->getNumValues();
669 if (!NumVals)
670 return Sched::RegPressure;
671
672 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000673 EVT VT = N->getValueType(i);
674 if (VT.isFloatingPoint() || VT.isVector())
675 return Sched::Latency;
676 }
Evan Chengc10f5432010-05-28 23:25:23 +0000677
678 if (!N->isMachineOpcode())
679 return Sched::RegPressure;
680
681 // Load are scheduled for latency even if there instruction itinerary
682 // is not available.
683 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
684 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
685 if (TID.mayLoad())
686 return Sched::Latency;
687
688 const InstrItineraryData &Itins = getTargetMachine().getInstrItineraryData();
689 if (!Itins.isEmpty() && Itins.getStageLatency(TID.getSchedClass()) > 2)
690 return Sched::Latency;
Evan Cheng1cc39842010-05-20 23:26:43 +0000691 return Sched::RegPressure;
692}
693
Evan Chenga8e29892007-01-19 07:51:42 +0000694//===----------------------------------------------------------------------===//
695// Lowering Code
696//===----------------------------------------------------------------------===//
697
Evan Chenga8e29892007-01-19 07:51:42 +0000698/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
699static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
700 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000701 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000702 case ISD::SETNE: return ARMCC::NE;
703 case ISD::SETEQ: return ARMCC::EQ;
704 case ISD::SETGT: return ARMCC::GT;
705 case ISD::SETGE: return ARMCC::GE;
706 case ISD::SETLT: return ARMCC::LT;
707 case ISD::SETLE: return ARMCC::LE;
708 case ISD::SETUGT: return ARMCC::HI;
709 case ISD::SETUGE: return ARMCC::HS;
710 case ISD::SETULT: return ARMCC::LO;
711 case ISD::SETULE: return ARMCC::LS;
712 }
713}
714
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000715/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
716static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000717 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000718 CondCode2 = ARMCC::AL;
719 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000720 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000721 case ISD::SETEQ:
722 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
723 case ISD::SETGT:
724 case ISD::SETOGT: CondCode = ARMCC::GT; break;
725 case ISD::SETGE:
726 case ISD::SETOGE: CondCode = ARMCC::GE; break;
727 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000728 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000729 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
730 case ISD::SETO: CondCode = ARMCC::VC; break;
731 case ISD::SETUO: CondCode = ARMCC::VS; break;
732 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
733 case ISD::SETUGT: CondCode = ARMCC::HI; break;
734 case ISD::SETUGE: CondCode = ARMCC::PL; break;
735 case ISD::SETLT:
736 case ISD::SETULT: CondCode = ARMCC::LT; break;
737 case ISD::SETLE:
738 case ISD::SETULE: CondCode = ARMCC::LE; break;
739 case ISD::SETNE:
740 case ISD::SETUNE: CondCode = ARMCC::NE; break;
741 }
Evan Chenga8e29892007-01-19 07:51:42 +0000742}
743
Bob Wilson1f595bb2009-04-17 19:07:39 +0000744//===----------------------------------------------------------------------===//
745// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000746//===----------------------------------------------------------------------===//
747
748#include "ARMGenCallingConv.inc"
749
750// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000751static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000752 CCValAssign::LocInfo &LocInfo,
753 CCState &State, bool CanFail) {
754 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
755
756 // Try to get the first register.
757 if (unsigned Reg = State.AllocateReg(RegList, 4))
758 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
759 else {
760 // For the 2nd half of a v2f64, do not 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, 4),
767 LocVT, LocInfo));
768 return true;
769 }
770
771 // Try to get the second register.
772 if (unsigned Reg = State.AllocateReg(RegList, 4))
773 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
774 else
775 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
776 State.AllocateStack(4, 4),
777 LocVT, LocInfo));
778 return true;
779}
780
Owen Andersone50ed302009-08-10 22:56:29 +0000781static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000782 CCValAssign::LocInfo &LocInfo,
783 ISD::ArgFlagsTy &ArgFlags,
784 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000785 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
786 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000787 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000788 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
789 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000790 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000791}
792
793// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000794static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000795 CCValAssign::LocInfo &LocInfo,
796 CCState &State, bool CanFail) {
797 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
798 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
799
800 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
801 if (Reg == 0) {
802 // For the 2nd half of a v2f64, do not just fail.
803 if (CanFail)
804 return false;
805
806 // Put the whole thing on the stack.
807 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
808 State.AllocateStack(8, 8),
809 LocVT, LocInfo));
810 return true;
811 }
812
813 unsigned i;
814 for (i = 0; i < 2; ++i)
815 if (HiRegList[i] == Reg)
816 break;
817
818 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
819 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
820 LocVT, LocInfo));
821 return true;
822}
823
Owen Andersone50ed302009-08-10 22:56:29 +0000824static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000825 CCValAssign::LocInfo &LocInfo,
826 ISD::ArgFlagsTy &ArgFlags,
827 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000828 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
829 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000830 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000831 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
832 return false;
833 return true; // we handled it
834}
835
Owen Andersone50ed302009-08-10 22:56:29 +0000836static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000837 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000838 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
839 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
840
Bob Wilsone65586b2009-04-17 20:40:45 +0000841 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
842 if (Reg == 0)
843 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000844
Bob Wilsone65586b2009-04-17 20:40:45 +0000845 unsigned i;
846 for (i = 0; i < 2; ++i)
847 if (HiRegList[i] == Reg)
848 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000849
Bob Wilson5bafff32009-06-22 23:27:02 +0000850 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000851 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000852 LocVT, LocInfo));
853 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000854}
855
Owen Andersone50ed302009-08-10 22:56:29 +0000856static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000857 CCValAssign::LocInfo &LocInfo,
858 ISD::ArgFlagsTy &ArgFlags,
859 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000860 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
861 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000862 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000863 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000864 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000865}
866
Owen Andersone50ed302009-08-10 22:56:29 +0000867static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000868 CCValAssign::LocInfo &LocInfo,
869 ISD::ArgFlagsTy &ArgFlags,
870 CCState &State) {
871 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
872 State);
873}
874
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000875/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
876/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000877CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000878 bool Return,
879 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000880 switch (CC) {
881 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000882 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000883 case CallingConv::C:
884 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000885 // Use target triple & subtarget features to do actual dispatch.
886 if (Subtarget->isAAPCS_ABI()) {
887 if (Subtarget->hasVFP2() &&
888 FloatABIType == FloatABI::Hard && !isVarArg)
889 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
890 else
891 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
892 } else
893 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000894 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000895 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000896 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000897 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000898 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000899 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000900 }
901}
902
Dan Gohman98ca4f22009-08-05 01:29:28 +0000903/// LowerCallResult - Lower the result values of a call into the
904/// appropriate copies out of appropriate physical registers.
905SDValue
906ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000907 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000908 const SmallVectorImpl<ISD::InputArg> &Ins,
909 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000910 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000911
Bob Wilson1f595bb2009-04-17 19:07:39 +0000912 // Assign locations to each value returned by this call.
913 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000914 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000915 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000916 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000917 CCAssignFnForNode(CallConv, /* Return*/ true,
918 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000919
920 // Copy all of the result registers out of their specified physreg.
921 for (unsigned i = 0; i != RVLocs.size(); ++i) {
922 CCValAssign VA = RVLocs[i];
923
Bob Wilson80915242009-04-25 00:33:20 +0000924 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000925 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000926 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000927 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000928 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000929 Chain = Lo.getValue(1);
930 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000931 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000932 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000933 InFlag);
934 Chain = Hi.getValue(1);
935 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000936 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000937
Owen Anderson825b72b2009-08-11 20:47:22 +0000938 if (VA.getLocVT() == MVT::v2f64) {
939 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
940 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
941 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000942
943 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000944 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000945 Chain = Lo.getValue(1);
946 InFlag = Lo.getValue(2);
947 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000948 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000949 Chain = Hi.getValue(1);
950 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000951 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +0000952 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
953 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000954 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000955 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000956 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
957 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000958 Chain = Val.getValue(1);
959 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000960 }
Bob Wilson80915242009-04-25 00:33:20 +0000961
962 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000963 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000964 case CCValAssign::Full: break;
965 case CCValAssign::BCvt:
966 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
967 break;
968 }
969
Dan Gohman98ca4f22009-08-05 01:29:28 +0000970 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000971 }
972
Dan Gohman98ca4f22009-08-05 01:29:28 +0000973 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000974}
975
976/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
977/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000978/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000979/// a byval function parameter.
980/// Sometimes what we are copying is the end of a larger object, the part that
981/// does not fit in registers.
982static SDValue
983CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
984 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
985 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000986 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000987 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +0000988 /*isVolatile=*/false, /*AlwaysInline=*/false,
989 NULL, 0, NULL, 0);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000990}
991
Bob Wilsondee46d72009-04-17 20:35:10 +0000992/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000993SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000994ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
995 SDValue StackPtr, SDValue Arg,
996 DebugLoc dl, SelectionDAG &DAG,
997 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +0000998 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000999 unsigned LocMemOffset = VA.getLocMemOffset();
1000 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1001 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
1002 if (Flags.isByVal()) {
1003 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
1004 }
1005 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene1b58cab2010-02-15 16:55:24 +00001006 PseudoSourceValue::getStack(), LocMemOffset,
1007 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001008}
1009
Dan Gohman98ca4f22009-08-05 01:29:28 +00001010void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001011 SDValue Chain, SDValue &Arg,
1012 RegsToPassVector &RegsToPass,
1013 CCValAssign &VA, CCValAssign &NextVA,
1014 SDValue &StackPtr,
1015 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001016 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001017
Jim Grosbache5165492009-11-09 00:11:35 +00001018 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001019 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001020 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1021
1022 if (NextVA.isRegLoc())
1023 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1024 else {
1025 assert(NextVA.isMemLoc());
1026 if (StackPtr.getNode() == 0)
1027 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1028
Dan Gohman98ca4f22009-08-05 01:29:28 +00001029 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1030 dl, DAG, NextVA,
1031 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001032 }
1033}
1034
Dan Gohman98ca4f22009-08-05 01:29:28 +00001035/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001036/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1037/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001038SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001039ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001040 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001041 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001042 const SmallVectorImpl<ISD::OutputArg> &Outs,
1043 const SmallVectorImpl<ISD::InputArg> &Ins,
1044 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001045 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001046 MachineFunction &MF = DAG.getMachineFunction();
1047 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1048 bool IsSibCall = false;
Dale Johannesen8fa8e7f2010-06-04 18:04:24 +00001049 // Temporarily disable tail calls so things don't break.
1050 if (!EnableARMTailCalls)
1051 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001052 if (isTailCall) {
1053 // Check if it's really possible to do a tail call.
1054 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1055 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
1056 Outs, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001057 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1058 // detected sibcalls.
1059 if (isTailCall) {
1060 ++NumTailCalls;
1061 IsSibCall = true;
1062 }
1063 }
Evan Chenga8e29892007-01-19 07:51:42 +00001064
Bob Wilson1f595bb2009-04-17 19:07:39 +00001065 // Analyze operands of the call, assigning locations to each operand.
1066 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001067 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1068 *DAG.getContext());
1069 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001070 CCAssignFnForNode(CallConv, /* Return*/ false,
1071 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001072
Bob Wilson1f595bb2009-04-17 19:07:39 +00001073 // Get a count of how many bytes are to be pushed on the stack.
1074 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001075
Dale Johannesen51e28e62010-06-03 21:09:53 +00001076 // For tail calls, memory operands are available in our caller's stack.
1077 if (IsSibCall)
1078 NumBytes = 0;
1079
Evan Chenga8e29892007-01-19 07:51:42 +00001080 // Adjust the stack pointer for the new arguments...
1081 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001082 if (!IsSibCall)
1083 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001084
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001085 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001086
Bob Wilson5bafff32009-06-22 23:27:02 +00001087 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001088 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001089
Bob Wilson1f595bb2009-04-17 19:07:39 +00001090 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001091 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001092 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1093 i != e;
1094 ++i, ++realArgIdx) {
1095 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001096 SDValue Arg = Outs[realArgIdx].Val;
1097 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +00001098
Bob Wilson1f595bb2009-04-17 19:07:39 +00001099 // Promote the value if needed.
1100 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001101 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001102 case CCValAssign::Full: break;
1103 case CCValAssign::SExt:
1104 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1105 break;
1106 case CCValAssign::ZExt:
1107 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1108 break;
1109 case CCValAssign::AExt:
1110 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1111 break;
1112 case CCValAssign::BCvt:
1113 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1114 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001115 }
1116
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001117 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001118 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001119 if (VA.getLocVT() == MVT::v2f64) {
1120 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1121 DAG.getConstant(0, MVT::i32));
1122 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1123 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001124
Dan Gohman98ca4f22009-08-05 01:29:28 +00001125 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001126 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1127
1128 VA = ArgLocs[++i]; // skip ahead to next loc
1129 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001130 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001131 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1132 } else {
1133 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001134
Dan Gohman98ca4f22009-08-05 01:29:28 +00001135 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1136 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001137 }
1138 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001139 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001140 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001141 }
1142 } else if (VA.isRegLoc()) {
1143 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001144 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001145 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001146
Dan Gohman98ca4f22009-08-05 01:29:28 +00001147 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1148 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001149 }
Evan Chenga8e29892007-01-19 07:51:42 +00001150 }
1151
1152 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001153 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001154 &MemOpChains[0], MemOpChains.size());
1155
1156 // Build a sequence of copy-to-reg nodes chained together with token chain
1157 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001158 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001159 // Tail call byval lowering might overwrite argument registers so in case of
1160 // tail call optimization the copies to registers are lowered later.
1161 if (!isTailCall)
1162 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1163 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1164 RegsToPass[i].second, InFlag);
1165 InFlag = Chain.getValue(1);
1166 }
Evan Chenga8e29892007-01-19 07:51:42 +00001167
Dale Johannesen51e28e62010-06-03 21:09:53 +00001168 // For tail calls lower the arguments to the 'real' stack slot.
1169 if (isTailCall) {
1170 // Force all the incoming stack arguments to be loaded from the stack
1171 // before any new outgoing arguments are stored to the stack, because the
1172 // outgoing stack slots may alias the incoming argument stack slots, and
1173 // the alias isn't otherwise explicit. This is slightly more conservative
1174 // than necessary, because it means that each store effectively depends
1175 // on every argument instead of just those arguments it would clobber.
1176
1177 // Do not flag preceeding copytoreg stuff together with the following stuff.
1178 InFlag = SDValue();
1179 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1180 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1181 RegsToPass[i].second, InFlag);
1182 InFlag = Chain.getValue(1);
1183 }
1184 InFlag =SDValue();
1185 }
1186
Bill Wendling056292f2008-09-16 21:48:12 +00001187 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1188 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1189 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001190 bool isDirect = false;
1191 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001192 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001193 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001194
1195 if (EnableARMLongCalls) {
1196 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1197 && "long-calls with non-static relocation model!");
1198 // Handle a global address or an external symbol. If it's not one of
1199 // those, the target's already in a register, so we don't need to do
1200 // anything extra.
1201 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001202 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001203 // Create a constant pool entry for the callee address
1204 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1205 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1206 ARMPCLabelIndex,
1207 ARMCP::CPValue, 0);
1208 // Get the address of the callee into a register
1209 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1210 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1211 Callee = DAG.getLoad(getPointerTy(), dl,
1212 DAG.getEntryNode(), CPAddr,
1213 PseudoSourceValue::getConstantPool(), 0,
1214 false, false, 0);
1215 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1216 const char *Sym = S->getSymbol();
1217
1218 // Create a constant pool entry for the callee address
1219 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1220 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1221 Sym, ARMPCLabelIndex, 0);
1222 // Get the address of the callee into a register
1223 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1224 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1225 Callee = DAG.getLoad(getPointerTy(), dl,
1226 DAG.getEntryNode(), CPAddr,
1227 PseudoSourceValue::getConstantPool(), 0,
1228 false, false, 0);
1229 }
1230 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001231 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001232 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001233 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001234 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001235 getTargetMachine().getRelocationModel() != Reloc::Static;
1236 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001237 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001238 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001239 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001240 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001241 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001242 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001243 ARMPCLabelIndex,
1244 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001245 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001246 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001247 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001248 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001249 PseudoSourceValue::getConstantPool(), 0,
1250 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001251 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001252 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001253 getPointerTy(), Callee, PICLabel);
Jim Grosbache7b52522010-04-14 22:28:31 +00001254 } else
Evan Chengc60e76d2007-01-30 20:37:08 +00001255 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001256 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001257 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001258 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001259 getTargetMachine().getRelocationModel() != Reloc::Static;
1260 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001261 // tBX takes a register source operand.
1262 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001263 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001264 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001265 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001266 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001267 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001268 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001269 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001270 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001271 PseudoSourceValue::getConstantPool(), 0,
1272 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001273 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001274 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001275 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001276 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001277 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001278 }
1279
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001280 // FIXME: handle tail calls differently.
1281 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001282 if (Subtarget->isThumb()) {
1283 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001284 CallOpc = ARMISD::CALL_NOLINK;
1285 else
1286 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1287 } else {
1288 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001289 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1290 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001291 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001292 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001293 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001294 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001295 InFlag = Chain.getValue(1);
1296 }
1297
Dan Gohman475871a2008-07-27 21:46:04 +00001298 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001299 Ops.push_back(Chain);
1300 Ops.push_back(Callee);
1301
1302 // Add argument registers to the end of the list so that they are known live
1303 // into the call.
1304 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1305 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1306 RegsToPass[i].second.getValueType()));
1307
Gabor Greifba36cb52008-08-28 21:40:38 +00001308 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001309 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001310
1311 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001312 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001313 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001314
Duncan Sands4bdcb612008-07-02 17:40:58 +00001315 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001316 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001317 InFlag = Chain.getValue(1);
1318
Chris Lattnere563bbc2008-10-11 22:08:30 +00001319 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1320 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001321 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001322 InFlag = Chain.getValue(1);
1323
Bob Wilson1f595bb2009-04-17 19:07:39 +00001324 // Handle result values, copying them out of physregs into vregs that we
1325 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001326 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1327 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001328}
1329
Dale Johannesen51e28e62010-06-03 21:09:53 +00001330/// MatchingStackOffset - Return true if the given stack call argument is
1331/// already available in the same position (relatively) of the caller's
1332/// incoming argument stack.
1333static
1334bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1335 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1336 const ARMInstrInfo *TII) {
1337 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1338 int FI = INT_MAX;
1339 if (Arg.getOpcode() == ISD::CopyFromReg) {
1340 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
1341 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
1342 return false;
1343 MachineInstr *Def = MRI->getVRegDef(VR);
1344 if (!Def)
1345 return false;
1346 if (!Flags.isByVal()) {
1347 if (!TII->isLoadFromStackSlot(Def, FI))
1348 return false;
1349 } else {
1350// unsigned Opcode = Def->getOpcode();
1351// if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
1352// Def->getOperand(1).isFI()) {
1353// FI = Def->getOperand(1).getIndex();
1354// Bytes = Flags.getByValSize();
1355// } else
1356 return false;
1357 }
1358 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1359 if (Flags.isByVal())
1360 // ByVal argument is passed in as a pointer but it's now being
1361 // dereferenced. e.g.
1362 // define @foo(%struct.X* %A) {
1363 // tail call @bar(%struct.X* byval %A)
1364 // }
1365 return false;
1366 SDValue Ptr = Ld->getBasePtr();
1367 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1368 if (!FINode)
1369 return false;
1370 FI = FINode->getIndex();
1371 } else
1372 return false;
1373
1374 assert(FI != INT_MAX);
1375 if (!MFI->isFixedObjectIndex(FI))
1376 return false;
1377 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1378}
1379
1380/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1381/// for tail call optimization. Targets which want to do tail call
1382/// optimization should implement this function.
1383bool
1384ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1385 CallingConv::ID CalleeCC,
1386 bool isVarArg,
1387 bool isCalleeStructRet,
1388 bool isCallerStructRet,
1389 const SmallVectorImpl<ISD::OutputArg> &Outs,
1390 const SmallVectorImpl<ISD::InputArg> &Ins,
1391 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001392 const Function *CallerF = DAG.getMachineFunction().getFunction();
1393 CallingConv::ID CallerCC = CallerF->getCallingConv();
1394 bool CCMatch = CallerCC == CalleeCC;
1395
1396 // Look for obvious safe cases to perform tail call optimization that do not
1397 // require ABI changes. This is what gcc calls sibcall.
1398
Jim Grosbach7616b642010-06-16 23:45:49 +00001399 // Do not sibcall optimize vararg calls unless the call site is not passing
1400 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001401 if (isVarArg && !Outs.empty())
1402 return false;
1403
1404 // Also avoid sibcall optimization if either caller or callee uses struct
1405 // return semantics.
1406 if (isCalleeStructRet || isCallerStructRet)
1407 return false;
1408
Evan Cheng0110ac62010-06-19 01:01:32 +00001409 // FIXME: Completely disable sibcal for Thumb1 since Thumb1RegisterInfo::
1410 // emitEpilogue is not ready for them.
1411 if (Subtarget->isThumb1Only())
1412 return false;
1413
1414 if (isa<ExternalSymbolSDNode>(Callee))
1415 return false;
1416
1417 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1418 if (Subtarget->isThumb1Only())
1419 return false;
1420
1421 // On Thumb, for the moment, we can only do this to functions defined in this
1422 // compilation, or to indirect calls. A Thumb B to an ARM function is not
1423 // easily fixed up in the linker, unlike BL.
1424 if (Subtarget->isThumb()) {
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001425 const GlobalValue *GV = G->getGlobal();
1426 if (GV->isDeclaration() || GV->isWeakForLinker())
1427 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001428 }
1429 }
1430
Evan Cheng0110ac62010-06-19 01:01:32 +00001431
Dale Johannesen51e28e62010-06-03 21:09:53 +00001432 // If the calling conventions do not match, then we'd better make sure the
1433 // results are returned in the same way as what the caller expects.
1434 if (!CCMatch) {
1435 SmallVector<CCValAssign, 16> RVLocs1;
1436 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
1437 RVLocs1, *DAG.getContext());
1438 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1439
1440 SmallVector<CCValAssign, 16> RVLocs2;
1441 CCState CCInfo2(CallerCC, false, getTargetMachine(),
1442 RVLocs2, *DAG.getContext());
1443 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1444
1445 if (RVLocs1.size() != RVLocs2.size())
1446 return false;
1447 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1448 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1449 return false;
1450 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1451 return false;
1452 if (RVLocs1[i].isRegLoc()) {
1453 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1454 return false;
1455 } else {
1456 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1457 return false;
1458 }
1459 }
1460 }
1461
1462 // If the callee takes no arguments then go on to check the results of the
1463 // call.
1464 if (!Outs.empty()) {
1465 // Check if stack adjustment is needed. For now, do not do this if any
1466 // argument is passed on the stack.
1467 SmallVector<CCValAssign, 16> ArgLocs;
1468 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
1469 ArgLocs, *DAG.getContext());
1470 CCInfo.AnalyzeCallOperands(Outs,
1471 CCAssignFnForNode(CalleeCC, false, isVarArg));
1472 if (CCInfo.getNextStackOffset()) {
1473 MachineFunction &MF = DAG.getMachineFunction();
1474
1475 // Check if the arguments are already laid out in the right way as
1476 // the caller's fixed stack objects.
1477 MachineFrameInfo *MFI = MF.getFrameInfo();
1478 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1479 const ARMInstrInfo *TII =
1480 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001481 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1482 i != e;
1483 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001484 CCValAssign &VA = ArgLocs[i];
1485 EVT RegVT = VA.getLocVT();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001486 SDValue Arg = Outs[realArgIdx].Val;
1487 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001488 if (VA.getLocInfo() == CCValAssign::Indirect)
1489 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001490 if (VA.needsCustom()) {
1491 // f64 and vector types are split into multiple registers or
1492 // register/stack-slot combinations. The types will not match
1493 // the registers; give up on memory f64 refs until we figure
1494 // out what to do about this.
1495 if (!VA.isRegLoc())
1496 return false;
1497 if (!ArgLocs[++i].isRegLoc())
1498 return false;
1499 if (RegVT == MVT::v2f64) {
1500 if (!ArgLocs[++i].isRegLoc())
1501 return false;
1502 if (!ArgLocs[++i].isRegLoc())
1503 return false;
1504 }
1505 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001506 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1507 MFI, MRI, TII))
1508 return false;
1509 }
1510 }
1511 }
1512 }
1513
1514 return true;
1515}
1516
Dan Gohman98ca4f22009-08-05 01:29:28 +00001517SDValue
1518ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001519 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001520 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmand858e902010-04-17 15:26:15 +00001521 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001522
Bob Wilsondee46d72009-04-17 20:35:10 +00001523 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001524 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001525
Bob Wilsondee46d72009-04-17 20:35:10 +00001526 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001527 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1528 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001529
Dan Gohman98ca4f22009-08-05 01:29:28 +00001530 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001531 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1532 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001533
1534 // If this is the first return lowered for this function, add
1535 // the regs to the liveout set for the function.
1536 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1537 for (unsigned i = 0; i != RVLocs.size(); ++i)
1538 if (RVLocs[i].isRegLoc())
1539 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001540 }
1541
Bob Wilson1f595bb2009-04-17 19:07:39 +00001542 SDValue Flag;
1543
1544 // Copy the result values into the output registers.
1545 for (unsigned i = 0, realRVLocIdx = 0;
1546 i != RVLocs.size();
1547 ++i, ++realRVLocIdx) {
1548 CCValAssign &VA = RVLocs[i];
1549 assert(VA.isRegLoc() && "Can only return in registers!");
1550
Dan Gohman98ca4f22009-08-05 01:29:28 +00001551 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001552
1553 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001554 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001555 case CCValAssign::Full: break;
1556 case CCValAssign::BCvt:
1557 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1558 break;
1559 }
1560
Bob Wilson1f595bb2009-04-17 19:07:39 +00001561 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001562 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001563 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001564 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1565 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001566 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001567 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001568
1569 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1570 Flag = Chain.getValue(1);
1571 VA = RVLocs[++i]; // skip ahead to next loc
1572 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1573 HalfGPRs.getValue(1), Flag);
1574 Flag = Chain.getValue(1);
1575 VA = RVLocs[++i]; // skip ahead to next loc
1576
1577 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001578 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1579 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001580 }
1581 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1582 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001583 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001584 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001585 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001586 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001587 VA = RVLocs[++i]; // skip ahead to next loc
1588 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1589 Flag);
1590 } else
1591 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1592
Bob Wilsondee46d72009-04-17 20:35:10 +00001593 // Guarantee that all emitted copies are
1594 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001595 Flag = Chain.getValue(1);
1596 }
1597
1598 SDValue result;
1599 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001600 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001601 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001602 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001603
1604 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001605}
1606
Bob Wilsonb62d2572009-11-03 00:02:05 +00001607// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1608// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1609// one of the above mentioned nodes. It has to be wrapped because otherwise
1610// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1611// be used to form addressing mode. These wrapped nodes will be selected
1612// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001613static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001614 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001615 // FIXME there is no actual debug info here
1616 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001617 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001618 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001619 if (CP->isMachineConstantPoolEntry())
1620 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1621 CP->getAlignment());
1622 else
1623 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1624 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001625 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001626}
1627
Dan Gohmand858e902010-04-17 15:26:15 +00001628SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1629 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001630 MachineFunction &MF = DAG.getMachineFunction();
1631 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1632 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001633 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001634 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001635 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001636 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1637 SDValue CPAddr;
1638 if (RelocM == Reloc::Static) {
1639 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1640 } else {
1641 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001642 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001643 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1644 ARMCP::CPBlockAddress,
1645 PCAdj);
1646 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1647 }
1648 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1649 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001650 PseudoSourceValue::getConstantPool(), 0,
1651 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001652 if (RelocM == Reloc::Static)
1653 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001654 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001655 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001656}
1657
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001658// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001659SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001660ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001661 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001662 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001663 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001664 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001665 MachineFunction &MF = DAG.getMachineFunction();
1666 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1667 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001668 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001669 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001670 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001671 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001672 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001673 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
David Greene1b58cab2010-02-15 16:55:24 +00001674 PseudoSourceValue::getConstantPool(), 0,
1675 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001676 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001677
Evan Chenge7e0d622009-11-06 22:24:13 +00001678 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001679 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001680
1681 // call __tls_get_addr.
1682 ArgListTy Args;
1683 ArgListEntry Entry;
1684 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001685 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001686 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001687 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001688 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001689 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1690 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001691 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001692 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001693 return CallResult.first;
1694}
1695
1696// Lower ISD::GlobalTLSAddress using the "initial exec" or
1697// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001698SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001699ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001700 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001701 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001702 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001703 SDValue Offset;
1704 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001705 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001706 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001707 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001708
Chris Lattner4fb63d02009-07-15 04:12:33 +00001709 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001710 MachineFunction &MF = DAG.getMachineFunction();
1711 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1712 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1713 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001714 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1715 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001716 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001717 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001718 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001719 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001720 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001721 PseudoSourceValue::getConstantPool(), 0,
1722 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001723 Chain = Offset.getValue(1);
1724
Evan Chenge7e0d622009-11-06 22:24:13 +00001725 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001726 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001727
Evan Cheng9eda6892009-10-31 03:39:36 +00001728 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001729 PseudoSourceValue::getConstantPool(), 0,
1730 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001731 } else {
1732 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001733 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001734 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001735 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001736 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001737 PseudoSourceValue::getConstantPool(), 0,
1738 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001739 }
1740
1741 // The address of the thread local variable is the add of the thread
1742 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001743 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001744}
1745
Dan Gohman475871a2008-07-27 21:46:04 +00001746SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001747ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001748 // TODO: implement the "local dynamic" model
1749 assert(Subtarget->isTargetELF() &&
1750 "TLS not implemented for non-ELF targets");
1751 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1752 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1753 // otherwise use the "Local Exec" TLS Model
1754 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1755 return LowerToTLSGeneralDynamicModel(GA, DAG);
1756 else
1757 return LowerToTLSExecModels(GA, DAG);
1758}
1759
Dan Gohman475871a2008-07-27 21:46:04 +00001760SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001761 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001762 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001763 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001764 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001765 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1766 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001767 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001768 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001769 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001770 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001771 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001772 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001773 CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001774 PseudoSourceValue::getConstantPool(), 0,
1775 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001776 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001777 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001778 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001779 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001780 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001781 PseudoSourceValue::getGOT(), 0,
1782 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001783 return Result;
1784 } else {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001785 // If we have T2 ops, we can materialize the address directly via movt/movw
1786 // pair. This is always cheaper.
1787 if (Subtarget->useMovt()) {
1788 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
1789 DAG.getTargetGlobalAddress(GV, PtrVT));
1790 } else {
1791 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1792 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1793 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001794 PseudoSourceValue::getConstantPool(), 0,
1795 false, false, 0);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001796 }
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001797 }
1798}
1799
Dan Gohman475871a2008-07-27 21:46:04 +00001800SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001801 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001802 MachineFunction &MF = DAG.getMachineFunction();
1803 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1804 unsigned ARMPCLabelIndex = 0;
Owen Andersone50ed302009-08-10 22:56:29 +00001805 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001806 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001807 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001808 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001809 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001810 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001811 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001812 else {
Evan Chenge7e0d622009-11-06 22:24:13 +00001813 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001814 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1815 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001816 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001817 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001818 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001819 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001820
Evan Cheng9eda6892009-10-31 03:39:36 +00001821 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001822 PseudoSourceValue::getConstantPool(), 0,
1823 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001824 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001825
1826 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001827 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001828 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001829 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001830
Evan Cheng63476a82009-09-03 07:04:02 +00001831 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Evan Cheng9eda6892009-10-31 03:39:36 +00001832 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001833 PseudoSourceValue::getGOT(), 0,
1834 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001835
1836 return Result;
1837}
1838
Dan Gohman475871a2008-07-27 21:46:04 +00001839SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001840 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001841 assert(Subtarget->isTargetELF() &&
1842 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00001843 MachineFunction &MF = DAG.getMachineFunction();
1844 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1845 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Andersone50ed302009-08-10 22:56:29 +00001846 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001847 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001848 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001849 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1850 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001851 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001852 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001853 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001854 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001855 PseudoSourceValue::getConstantPool(), 0,
1856 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001857 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001858 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001859}
1860
Jim Grosbach0e0da732009-05-12 23:59:14 +00001861SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00001862ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
1863 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00001864 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00001865 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
1866 Op.getOperand(1), Val);
1867}
1868
1869SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00001870ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
1871 DebugLoc dl = Op.getDebugLoc();
1872 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
1873 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
1874}
1875
1876SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00001877ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00001878 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001879 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001880 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001881 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001882 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001883 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001884 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001885 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1886 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001887 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001888 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00001889 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1890 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001891 EVT PtrVT = getPointerTy();
1892 DebugLoc dl = Op.getDebugLoc();
1893 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1894 SDValue CPAddr;
1895 unsigned PCAdj = (RelocM != Reloc::PIC_)
1896 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001897 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001898 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1899 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001900 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001901 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001902 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00001903 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001904 PseudoSourceValue::getConstantPool(), 0,
1905 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001906 SDValue Chain = Result.getValue(1);
1907
1908 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001909 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001910 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1911 }
1912 return Result;
1913 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001914 }
1915}
1916
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001917static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00001918 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00001919 DebugLoc dl = Op.getDebugLoc();
1920 SDValue Op5 = Op.getOperand(5);
Jim Grosbach3728e962009-12-10 00:11:09 +00001921 unsigned isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue();
Jim Grosbachc73993b2010-06-17 01:37:00 +00001922 // v6 and v7 can both handle barriers directly, but need handled a bit
1923 // differently. Thumb1 and pre-v6 ARM mode use a libcall instead and should
1924 // never get here.
1925 unsigned Opc = isDeviceBarrier ? ARMISD::SYNCBARRIER : ARMISD::MEMBARRIER;
1926 if (Subtarget->hasV7Ops())
1927 return DAG.getNode(Opc, dl, MVT::Other, Op.getOperand(0));
1928 else if (Subtarget->hasV6Ops() && !Subtarget->isThumb1Only())
1929 return DAG.getNode(Opc, dl, MVT::Other, Op.getOperand(0),
1930 DAG.getConstant(0, MVT::i32));
1931 assert(0 && "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
1932 return SDValue();
Jim Grosbach3728e962009-12-10 00:11:09 +00001933}
1934
Dan Gohman1e93df62010-04-17 14:41:14 +00001935static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
1936 MachineFunction &MF = DAG.getMachineFunction();
1937 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
1938
Evan Chenga8e29892007-01-19 07:51:42 +00001939 // vastart just stores the address of the VarArgsFrameIndex slot into the
1940 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001941 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001942 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00001943 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001944 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
David Greene1b58cab2010-02-15 16:55:24 +00001945 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
1946 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001947}
1948
Dan Gohman475871a2008-07-27 21:46:04 +00001949SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001950ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1951 SelectionDAG &DAG) const {
Evan Cheng86198642009-08-07 00:34:42 +00001952 SDNode *Node = Op.getNode();
1953 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001954 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001955 SDValue Chain = Op.getOperand(0);
1956 SDValue Size = Op.getOperand(1);
1957 SDValue Align = Op.getOperand(2);
1958
1959 // Chain the dynamic stack allocation so that it doesn't modify the stack
1960 // pointer when other instructions are using the stack.
1961 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1962
1963 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1964 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1965 if (AlignVal > StackAlign)
1966 // Do this now since selection pass cannot introduce new target
1967 // independent node.
1968 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1969
1970 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1971 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1972 // do even more horrible hack later.
1973 MachineFunction &MF = DAG.getMachineFunction();
1974 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1975 if (AFI->isThumb1OnlyFunction()) {
1976 bool Negate = true;
1977 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1978 if (C) {
1979 uint32_t Val = C->getZExtValue();
1980 if (Val <= 508 && ((Val & 3) == 0))
1981 Negate = false;
1982 }
1983 if (Negate)
1984 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1985 }
1986
Owen Anderson825b72b2009-08-11 20:47:22 +00001987 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001988 SDValue Ops1[] = { Chain, Size, Align };
1989 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1990 Chain = Res.getValue(1);
1991 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1992 DAG.getIntPtrConstant(0, true), SDValue());
1993 SDValue Ops2[] = { Res, Chain };
1994 return DAG.getMergeValues(Ops2, 2, dl);
1995}
1996
1997SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001998ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1999 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002000 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002001 MachineFunction &MF = DAG.getMachineFunction();
2002 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2003
2004 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002005 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002006 RC = ARM::tGPRRegisterClass;
2007 else
2008 RC = ARM::GPRRegisterClass;
2009
2010 // Transform the arguments stored in physical registers into virtual ones.
Evan Cheng2457f2c2010-05-22 01:47:14 +00002011 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002012 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002013
2014 SDValue ArgValue2;
2015 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002016 MachineFrameInfo *MFI = MF.getFrameInfo();
Bob Wilson6a234f02010-04-13 22:03:22 +00002017 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true, false);
Bob Wilson5bafff32009-06-22 23:27:02 +00002018
2019 // Create load node to retrieve arguments from the stack.
2020 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002021 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00002022 PseudoSourceValue::getFixedStack(FI), 0,
2023 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002024 } else {
2025 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002026 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002027 }
2028
Jim Grosbache5165492009-11-09 00:11:35 +00002029 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002030}
2031
2032SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002033ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002034 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002035 const SmallVectorImpl<ISD::InputArg>
2036 &Ins,
2037 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002038 SmallVectorImpl<SDValue> &InVals)
2039 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002040
Bob Wilson1f595bb2009-04-17 19:07:39 +00002041 MachineFunction &MF = DAG.getMachineFunction();
2042 MachineFrameInfo *MFI = MF.getFrameInfo();
2043
Bob Wilson1f595bb2009-04-17 19:07:39 +00002044 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2045
2046 // Assign locations to all of the incoming arguments.
2047 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002048 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
2049 *DAG.getContext());
2050 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002051 CCAssignFnForNode(CallConv, /* Return*/ false,
2052 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002053
2054 SmallVector<SDValue, 16> ArgValues;
2055
2056 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2057 CCValAssign &VA = ArgLocs[i];
2058
Bob Wilsondee46d72009-04-17 20:35:10 +00002059 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002060 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002061 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002062
Bob Wilson5bafff32009-06-22 23:27:02 +00002063 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002064 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002065 // f64 and vector types are split up into multiple registers or
2066 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002067 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002068 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002069 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002070 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002071 SDValue ArgValue2;
2072 if (VA.isMemLoc()) {
2073 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(),
2074 true, false);
2075 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2076 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
2077 PseudoSourceValue::getFixedStack(FI), 0,
2078 false, false, 0);
2079 } else {
2080 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2081 Chain, DAG, dl);
2082 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002083 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2084 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002085 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002086 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002087 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2088 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002089 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002090
Bob Wilson5bafff32009-06-22 23:27:02 +00002091 } else {
2092 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002093
Owen Anderson825b72b2009-08-11 20:47:22 +00002094 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002095 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002096 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002097 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002098 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002099 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002100 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002101 RC = (AFI->isThumb1OnlyFunction() ?
2102 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002103 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002104 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002105
2106 // Transform the arguments in physical registers into virtual ones.
2107 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002108 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002109 }
2110
2111 // If this is an 8 or 16-bit value, it is really passed promoted
2112 // to 32 bits. Insert an assert[sz]ext to capture this, then
2113 // truncate to the right size.
2114 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002115 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002116 case CCValAssign::Full: break;
2117 case CCValAssign::BCvt:
2118 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
2119 break;
2120 case CCValAssign::SExt:
2121 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2122 DAG.getValueType(VA.getValVT()));
2123 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2124 break;
2125 case CCValAssign::ZExt:
2126 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2127 DAG.getValueType(VA.getValVT()));
2128 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2129 break;
2130 }
2131
Dan Gohman98ca4f22009-08-05 01:29:28 +00002132 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002133
2134 } else { // VA.isRegLoc()
2135
2136 // sanity check
2137 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002138 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002139
2140 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00002141 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
2142 true, false);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002143
Bob Wilsondee46d72009-04-17 20:35:10 +00002144 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002145 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002146 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00002147 PseudoSourceValue::getFixedStack(FI), 0,
2148 false, false, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002149 }
2150 }
2151
2152 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00002153 if (isVarArg) {
2154 static const unsigned GPRArgRegs[] = {
2155 ARM::R0, ARM::R1, ARM::R2, ARM::R3
2156 };
2157
Bob Wilsondee46d72009-04-17 20:35:10 +00002158 unsigned NumGPRs = CCInfo.getFirstUnallocated
2159 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002160
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00002161 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
2162 unsigned VARegSize = (4 - NumGPRs) * 4;
2163 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00002164 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00002165 if (VARegSaveSize) {
2166 // If this function is vararg, store any remaining integer argument regs
2167 // to their spots on the stack so that they may be loaded by deferencing
2168 // the result of va_next.
2169 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00002170 AFI->setVarArgsFrameIndex(
2171 MFI->CreateFixedObject(VARegSaveSize,
2172 ArgOffset + VARegSaveSize - VARegSize,
2173 true, false));
2174 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2175 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00002176
Dan Gohman475871a2008-07-27 21:46:04 +00002177 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00002178 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002179 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002180 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00002181 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002182 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00002183 RC = ARM::GPRRegisterClass;
2184
Bob Wilson998e1252009-04-20 18:36:57 +00002185 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002186 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00002187 SDValue Store =
2188 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Jim Grosbach18f30e62010-06-02 21:53:11 +00002189 PseudoSourceValue::getFixedStack(AFI->getVarArgsFrameIndex()),
2190 0, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002191 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002192 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00002193 DAG.getConstant(4, getPointerTy()));
2194 }
2195 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002196 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002197 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00002198 } else
2199 // This will point to the next argument passed via stack.
Dan Gohman1e93df62010-04-17 14:41:14 +00002200 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset,
2201 true, false));
Evan Chenga8e29892007-01-19 07:51:42 +00002202 }
2203
Dan Gohman98ca4f22009-08-05 01:29:28 +00002204 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002205}
2206
2207/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002208static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002209 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002210 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002211 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002212 // Maybe this has already been legalized into the constant pool?
2213 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002214 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002215 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002216 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002217 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002218 }
2219 }
2220 return false;
2221}
2222
Evan Chenga8e29892007-01-19 07:51:42 +00002223/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2224/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002225SDValue
2226ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Dan Gohmand858e902010-04-17 15:26:15 +00002227 SDValue &ARMCC, SelectionDAG &DAG,
2228 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002229 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002230 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002231 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002232 // Constant does not fit, try adjusting it by one?
2233 switch (CC) {
2234 default: break;
2235 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002236 case ISD::SETGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00002237 if (isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002238 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002239 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002240 }
2241 break;
2242 case ISD::SETULT:
2243 case ISD::SETUGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00002244 if (C > 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002245 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002246 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002247 }
2248 break;
2249 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002250 case ISD::SETGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00002251 if (isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002252 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002253 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002254 }
2255 break;
2256 case ISD::SETULE:
2257 case ISD::SETUGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00002258 if (C < 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002259 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002260 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002261 }
2262 break;
2263 }
2264 }
2265 }
2266
2267 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002268 ARMISD::NodeType CompareType;
2269 switch (CondCode) {
2270 default:
2271 CompareType = ARMISD::CMP;
2272 break;
2273 case ARMCC::EQ:
2274 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002275 // Uses only Z Flag
2276 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002277 break;
2278 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002279 ARMCC = DAG.getConstant(CondCode, MVT::i32);
2280 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002281}
2282
2283/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00002284static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00002285 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00002286 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002287 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00002288 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002289 else
Owen Anderson825b72b2009-08-11 20:47:22 +00002290 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
2291 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002292}
2293
Dan Gohmand858e902010-04-17 15:26:15 +00002294SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002295 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002296 SDValue LHS = Op.getOperand(0);
2297 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002298 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002299 SDValue TrueVal = Op.getOperand(2);
2300 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002301 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002302
Owen Anderson825b72b2009-08-11 20:47:22 +00002303 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00002304 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002305 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00002306 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Dale Johannesende064702009-02-06 21:50:26 +00002307 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002308 }
2309
2310 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002311 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002312
Owen Anderson825b72b2009-08-11 20:47:22 +00002313 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
2314 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002315 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
2316 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00002317 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002318 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002319 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002320 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00002321 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002322 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00002323 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002324 }
2325 return Result;
2326}
2327
Dan Gohmand858e902010-04-17 15:26:15 +00002328SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002329 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002330 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002331 SDValue LHS = Op.getOperand(2);
2332 SDValue RHS = Op.getOperand(3);
2333 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002334 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002335
Owen Anderson825b72b2009-08-11 20:47:22 +00002336 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00002337 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002338 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00002339 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002340 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00002341 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002342 }
2343
Owen Anderson825b72b2009-08-11 20:47:22 +00002344 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00002345 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002346 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002347
Dale Johannesende064702009-02-06 21:50:26 +00002348 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002349 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
2350 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2351 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002352 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002353 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002354 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002355 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00002356 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002357 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002358 }
2359 return Res;
2360}
2361
Dan Gohmand858e902010-04-17 15:26:15 +00002362SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002363 SDValue Chain = Op.getOperand(0);
2364 SDValue Table = Op.getOperand(1);
2365 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002366 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002367
Owen Andersone50ed302009-08-10 22:56:29 +00002368 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002369 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2370 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002371 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002372 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002373 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002374 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2375 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002376 if (Subtarget->isThumb2()) {
2377 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2378 // which does another jump to the destination. This also makes it easier
2379 // to translate it to TBB / TBH later.
2380 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002381 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002382 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002383 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002384 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002385 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002386 PseudoSourceValue::getJumpTable(), 0,
2387 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002388 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002389 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002390 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002391 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002392 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002393 PseudoSourceValue::getJumpTable(), 0, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002394 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002395 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002396 }
Evan Chenga8e29892007-01-19 07:51:42 +00002397}
2398
Bob Wilson76a312b2010-03-19 22:51:32 +00002399static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2400 DebugLoc dl = Op.getDebugLoc();
2401 unsigned Opc;
2402
2403 switch (Op.getOpcode()) {
2404 default:
2405 assert(0 && "Invalid opcode!");
2406 case ISD::FP_TO_SINT:
2407 Opc = ARMISD::FTOSI;
2408 break;
2409 case ISD::FP_TO_UINT:
2410 Opc = ARMISD::FTOUI;
2411 break;
2412 }
2413 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
2414 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
2415}
2416
2417static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2418 EVT VT = Op.getValueType();
2419 DebugLoc dl = Op.getDebugLoc();
2420 unsigned Opc;
2421
2422 switch (Op.getOpcode()) {
2423 default:
2424 assert(0 && "Invalid opcode!");
2425 case ISD::SINT_TO_FP:
2426 Opc = ARMISD::SITOF;
2427 break;
2428 case ISD::UINT_TO_FP:
2429 Opc = ARMISD::UITOF;
2430 break;
2431 }
2432
2433 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
2434 return DAG.getNode(Opc, dl, VT, Op);
2435}
2436
Dan Gohman475871a2008-07-27 21:46:04 +00002437static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002438 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002439 SDValue Tmp0 = Op.getOperand(0);
2440 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002441 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002442 EVT VT = Op.getValueType();
2443 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00002444 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
2445 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002446 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
2447 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002448 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002449}
2450
Evan Cheng2457f2c2010-05-22 01:47:14 +00002451SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
2452 MachineFunction &MF = DAG.getMachineFunction();
2453 MachineFrameInfo *MFI = MF.getFrameInfo();
2454 MFI->setReturnAddressIsTaken(true);
2455
2456 EVT VT = Op.getValueType();
2457 DebugLoc dl = Op.getDebugLoc();
2458 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2459 if (Depth) {
2460 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
2461 SDValue Offset = DAG.getConstant(4, MVT::i32);
2462 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
2463 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
2464 NULL, 0, false, false, 0);
2465 }
2466
2467 // Return LR, which contains the return address. Mark it an implicit live-in.
Evan Chengc7cf10c2010-05-24 18:00:18 +00002468 unsigned Reg = MF.addLiveIn(ARM::LR, ARM::GPRRegisterClass);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002469 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
2470}
2471
Dan Gohmand858e902010-04-17 15:26:15 +00002472SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002473 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2474 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002475
Owen Andersone50ed302009-08-10 22:56:29 +00002476 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002477 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2478 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002479 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002480 ? ARM::R7 : ARM::R11;
2481 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2482 while (Depth--)
David Greene1b58cab2010-02-15 16:55:24 +00002483 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
2484 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002485 return FrameAddr;
2486}
2487
Bob Wilson9f3f0612010-04-17 05:30:19 +00002488/// ExpandBIT_CONVERT - If the target supports VFP, this function is called to
2489/// expand a bit convert where either the source or destination type is i64 to
2490/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2491/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2492/// vectors), since the legalizer won't know what to do with that.
Duncan Sands1607f052008-12-01 11:39:25 +00002493static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002494 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2495 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002496 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002497
Bob Wilson9f3f0612010-04-17 05:30:19 +00002498 // This function is only supposed to be called for i64 types, either as the
2499 // source or destination of the bit convert.
2500 EVT SrcVT = Op.getValueType();
2501 EVT DstVT = N->getValueType(0);
2502 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
2503 "ExpandBIT_CONVERT called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002504
Bob Wilson9f3f0612010-04-17 05:30:19 +00002505 // Turn i64->f64 into VMOVDRR.
2506 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002507 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2508 DAG.getConstant(0, MVT::i32));
2509 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2510 DAG.getConstant(1, MVT::i32));
Bob Wilson1114f562010-06-11 22:45:25 +00002511 return DAG.getNode(ISD::BIT_CONVERT, dl, DstVT,
2512 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00002513 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002514
Jim Grosbache5165492009-11-09 00:11:35 +00002515 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002516 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2517 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2518 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2519 // Merge the pieces into a single i64 value.
2520 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2521 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002522
Bob Wilson9f3f0612010-04-17 05:30:19 +00002523 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002524}
2525
Bob Wilson5bafff32009-06-22 23:27:02 +00002526/// getZeroVector - Returns a vector of specified type with all zero elements.
2527///
Owen Andersone50ed302009-08-10 22:56:29 +00002528static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002529 assert(VT.isVector() && "Expected a vector type");
2530
2531 // Zero vectors are used to represent vector negation and in those cases
2532 // will be implemented with the NEON VNEG instruction. However, VNEG does
2533 // not support i64 elements, so sometimes the zero vectors will need to be
2534 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002535 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilson5bafff32009-06-22 23:27:02 +00002536 // to their dest type. This ensures they get CSE'd.
2537 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002538 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2539 SmallVector<SDValue, 8> Ops;
2540 MVT TVT;
2541
2542 if (VT.getSizeInBits() == 64) {
2543 Ops.assign(8, Cst); TVT = MVT::v8i8;
2544 } else {
2545 Ops.assign(16, Cst); TVT = MVT::v16i8;
2546 }
2547 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002548
2549 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2550}
2551
2552/// getOnesVector - Returns a vector of specified type with all bits set.
2553///
Owen Andersone50ed302009-08-10 22:56:29 +00002554static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002555 assert(VT.isVector() && "Expected a vector type");
2556
Bob Wilson929ffa22009-10-30 20:13:25 +00002557 // Always build ones vectors as <16 x i8> or <8 x i8> bitcasted to their
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002558 // dest type. This ensures they get CSE'd.
Bob Wilson5bafff32009-06-22 23:27:02 +00002559 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002560 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2561 SmallVector<SDValue, 8> Ops;
2562 MVT TVT;
2563
2564 if (VT.getSizeInBits() == 64) {
2565 Ops.assign(8, Cst); TVT = MVT::v8i8;
2566 } else {
2567 Ops.assign(16, Cst); TVT = MVT::v16i8;
2568 }
2569 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002570
2571 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2572}
2573
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002574/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2575/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002576SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2577 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002578 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2579 EVT VT = Op.getValueType();
2580 unsigned VTBits = VT.getSizeInBits();
2581 DebugLoc dl = Op.getDebugLoc();
2582 SDValue ShOpLo = Op.getOperand(0);
2583 SDValue ShOpHi = Op.getOperand(1);
2584 SDValue ShAmt = Op.getOperand(2);
2585 SDValue ARMCC;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002586 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002587
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002588 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2589
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002590 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2591 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2592 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2593 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2594 DAG.getConstant(VTBits, MVT::i32));
2595 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2596 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002597 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002598
2599 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2600 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002601 ARMCC, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002602 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002603 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC,
2604 CCR, Cmp);
2605
2606 SDValue Ops[2] = { Lo, Hi };
2607 return DAG.getMergeValues(Ops, 2, dl);
2608}
2609
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002610/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2611/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002612SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2613 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002614 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2615 EVT VT = Op.getValueType();
2616 unsigned VTBits = VT.getSizeInBits();
2617 DebugLoc dl = Op.getDebugLoc();
2618 SDValue ShOpLo = Op.getOperand(0);
2619 SDValue ShOpHi = Op.getOperand(1);
2620 SDValue ShAmt = Op.getOperand(2);
2621 SDValue ARMCC;
2622
2623 assert(Op.getOpcode() == ISD::SHL_PARTS);
2624 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2625 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2626 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2627 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2628 DAG.getConstant(VTBits, MVT::i32));
2629 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2630 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2631
2632 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2633 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2634 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002635 ARMCC, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002636 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
2637 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMCC,
2638 CCR, Cmp);
2639
2640 SDValue Ops[2] = { Lo, Hi };
2641 return DAG.getMergeValues(Ops, 2, dl);
2642}
2643
Jim Grosbach3482c802010-01-18 19:58:49 +00002644static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
2645 const ARMSubtarget *ST) {
2646 EVT VT = N->getValueType(0);
2647 DebugLoc dl = N->getDebugLoc();
2648
2649 if (!ST->hasV6T2Ops())
2650 return SDValue();
2651
2652 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
2653 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
2654}
2655
Bob Wilson5bafff32009-06-22 23:27:02 +00002656static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2657 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002658 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002659 DebugLoc dl = N->getDebugLoc();
2660
2661 // Lower vector shifts on NEON to use VSHL.
2662 if (VT.isVector()) {
2663 assert(ST->hasNEON() && "unexpected vector shift");
2664
2665 // Left shifts translate directly to the vshiftu intrinsic.
2666 if (N->getOpcode() == ISD::SHL)
2667 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002668 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002669 N->getOperand(0), N->getOperand(1));
2670
2671 assert((N->getOpcode() == ISD::SRA ||
2672 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2673
2674 // NEON uses the same intrinsics for both left and right shifts. For
2675 // right shifts, the shift amounts are negative, so negate the vector of
2676 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002677 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002678 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2679 getZeroVector(ShiftVT, DAG, dl),
2680 N->getOperand(1));
2681 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2682 Intrinsic::arm_neon_vshifts :
2683 Intrinsic::arm_neon_vshiftu);
2684 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002685 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002686 N->getOperand(0), NegatedCount);
2687 }
2688
Eli Friedmance392eb2009-08-22 03:13:10 +00002689 // We can get here for a node like i32 = ISD::SHL i32, i64
2690 if (VT != MVT::i64)
2691 return SDValue();
2692
2693 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002694 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002695
Chris Lattner27a6c732007-11-24 07:07:01 +00002696 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2697 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002698 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002699 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002700
Chris Lattner27a6c732007-11-24 07:07:01 +00002701 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002702 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002703
Chris Lattner27a6c732007-11-24 07:07:01 +00002704 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002705 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00002706 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00002707 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00002708 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002709
Chris Lattner27a6c732007-11-24 07:07:01 +00002710 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2711 // captures the result into a carry flag.
2712 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002713 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002714
Chris Lattner27a6c732007-11-24 07:07:01 +00002715 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002716 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002717
Chris Lattner27a6c732007-11-24 07:07:01 +00002718 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002719 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002720}
2721
Bob Wilson5bafff32009-06-22 23:27:02 +00002722static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2723 SDValue TmpOp0, TmpOp1;
2724 bool Invert = false;
2725 bool Swap = false;
2726 unsigned Opc = 0;
2727
2728 SDValue Op0 = Op.getOperand(0);
2729 SDValue Op1 = Op.getOperand(1);
2730 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002731 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002732 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2733 DebugLoc dl = Op.getDebugLoc();
2734
2735 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2736 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002737 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002738 case ISD::SETUNE:
2739 case ISD::SETNE: Invert = true; // Fallthrough
2740 case ISD::SETOEQ:
2741 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2742 case ISD::SETOLT:
2743 case ISD::SETLT: Swap = true; // Fallthrough
2744 case ISD::SETOGT:
2745 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2746 case ISD::SETOLE:
2747 case ISD::SETLE: Swap = true; // Fallthrough
2748 case ISD::SETOGE:
2749 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2750 case ISD::SETUGE: Swap = true; // Fallthrough
2751 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2752 case ISD::SETUGT: Swap = true; // Fallthrough
2753 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2754 case ISD::SETUEQ: Invert = true; // Fallthrough
2755 case ISD::SETONE:
2756 // Expand this to (OLT | OGT).
2757 TmpOp0 = Op0;
2758 TmpOp1 = Op1;
2759 Opc = ISD::OR;
2760 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2761 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2762 break;
2763 case ISD::SETUO: Invert = true; // Fallthrough
2764 case ISD::SETO:
2765 // Expand this to (OLT | OGE).
2766 TmpOp0 = Op0;
2767 TmpOp1 = Op1;
2768 Opc = ISD::OR;
2769 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2770 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2771 break;
2772 }
2773 } else {
2774 // Integer comparisons.
2775 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002776 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002777 case ISD::SETNE: Invert = true;
2778 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2779 case ISD::SETLT: Swap = true;
2780 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2781 case ISD::SETLE: Swap = true;
2782 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2783 case ISD::SETULT: Swap = true;
2784 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2785 case ISD::SETULE: Swap = true;
2786 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2787 }
2788
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002789 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002790 if (Opc == ARMISD::VCEQ) {
2791
2792 SDValue AndOp;
2793 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2794 AndOp = Op0;
2795 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2796 AndOp = Op1;
2797
2798 // Ignore bitconvert.
2799 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2800 AndOp = AndOp.getOperand(0);
2801
2802 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2803 Opc = ARMISD::VTST;
2804 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2805 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2806 Invert = !Invert;
2807 }
2808 }
2809 }
2810
2811 if (Swap)
2812 std::swap(Op0, Op1);
2813
2814 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2815
2816 if (Invert)
2817 Result = DAG.getNOT(dl, Result, VT);
2818
2819 return Result;
2820}
2821
Bob Wilsond3c42842010-06-14 22:19:57 +00002822/// isNEONModifiedImm - Check if the specified splat value corresponds to a
2823/// valid vector constant for a NEON instruction with a "modified immediate"
2824/// operand (e.g., VMOV). If so, return either the constant being
2825/// splatted or the encoded value, depending on the DoEncode parameter. The
2826/// format of the encoded value is: bit12=Op, bits11-8=Cmode,
2827/// bits7-0=Immediate.
2828static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
2829 unsigned SplatBitSize, SelectionDAG &DAG,
Bob Wilson827b2102010-06-15 19:05:35 +00002830 bool isVMOV, bool DoEncode) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00002831 unsigned Op, Cmode, Imm;
2832 EVT VT;
2833
Bob Wilson827b2102010-06-15 19:05:35 +00002834 // SplatBitSize is set to the smallest size that splats the vector, so a
2835 // zero vector will always have SplatBitSize == 8. However, NEON modified
2836 // immediate instructions others than VMOV do not support the 8-bit encoding
2837 // of a zero vector, and the default encoding of zero is supposed to be the
2838 // 32-bit version.
2839 if (SplatBits == 0)
2840 SplatBitSize = 32;
2841
Bob Wilson1a913ed2010-06-11 21:34:50 +00002842 Op = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00002843 switch (SplatBitSize) {
2844 case 8:
Bob Wilson1a913ed2010-06-11 21:34:50 +00002845 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00002846 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson1a913ed2010-06-11 21:34:50 +00002847 Cmode = 0xe;
2848 Imm = SplatBits;
2849 VT = MVT::i8;
2850 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002851
2852 case 16:
2853 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilson1a913ed2010-06-11 21:34:50 +00002854 VT = MVT::i16;
2855 if ((SplatBits & ~0xff) == 0) {
2856 // Value = 0x00nn: Op=x, Cmode=100x.
2857 Cmode = 0x8;
2858 Imm = SplatBits;
2859 break;
2860 }
2861 if ((SplatBits & ~0xff00) == 0) {
2862 // Value = 0xnn00: Op=x, Cmode=101x.
2863 Cmode = 0xa;
2864 Imm = SplatBits >> 8;
2865 break;
2866 }
2867 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002868
2869 case 32:
2870 // NEON's 32-bit VMOV supports splat values where:
2871 // * only one byte is nonzero, or
2872 // * the least significant byte is 0xff and the second byte is nonzero, or
2873 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilson1a913ed2010-06-11 21:34:50 +00002874 VT = MVT::i32;
2875 if ((SplatBits & ~0xff) == 0) {
2876 // Value = 0x000000nn: Op=x, Cmode=000x.
2877 Cmode = 0;
2878 Imm = SplatBits;
2879 break;
2880 }
2881 if ((SplatBits & ~0xff00) == 0) {
2882 // Value = 0x0000nn00: Op=x, Cmode=001x.
2883 Cmode = 0x2;
2884 Imm = SplatBits >> 8;
2885 break;
2886 }
2887 if ((SplatBits & ~0xff0000) == 0) {
2888 // Value = 0x00nn0000: Op=x, Cmode=010x.
2889 Cmode = 0x4;
2890 Imm = SplatBits >> 16;
2891 break;
2892 }
2893 if ((SplatBits & ~0xff000000) == 0) {
2894 // Value = 0xnn000000: Op=x, Cmode=011x.
2895 Cmode = 0x6;
2896 Imm = SplatBits >> 24;
2897 break;
2898 }
Bob Wilson5bafff32009-06-22 23:27:02 +00002899
2900 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00002901 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
2902 // Value = 0x0000nnff: Op=x, Cmode=1100.
2903 Cmode = 0xc;
2904 Imm = SplatBits >> 8;
2905 SplatBits |= 0xff;
2906 break;
2907 }
Bob Wilson5bafff32009-06-22 23:27:02 +00002908
2909 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00002910 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
2911 // Value = 0x00nnffff: Op=x, Cmode=1101.
2912 Cmode = 0xd;
2913 Imm = SplatBits >> 16;
2914 SplatBits |= 0xffff;
2915 break;
2916 }
Bob Wilson5bafff32009-06-22 23:27:02 +00002917
2918 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2919 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2920 // VMOV.I32. A (very) minor optimization would be to replicate the value
2921 // and fall through here to test for a valid 64-bit splat. But, then the
2922 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00002923 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002924
2925 case 64: {
2926 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson827b2102010-06-15 19:05:35 +00002927 if (!isVMOV)
2928 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002929 uint64_t BitMask = 0xff;
2930 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002931 unsigned ImmMask = 1;
2932 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00002933 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00002934 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002935 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002936 Imm |= ImmMask;
2937 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002938 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00002939 }
Bob Wilson5bafff32009-06-22 23:27:02 +00002940 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002941 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00002942 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00002943 // Op=1, Cmode=1110.
2944 Op = 1;
2945 Cmode = 0xe;
2946 SplatBits = Val;
2947 VT = MVT::i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00002948 break;
2949 }
2950
Bob Wilson1a913ed2010-06-11 21:34:50 +00002951 default:
2952 llvm_unreachable("unexpected size for EncodeNEONModImm");
2953 return SDValue();
2954 }
2955
2956 if (DoEncode)
2957 return DAG.getTargetConstant((Op << 12) | (Cmode << 8) | Imm, MVT::i32);
2958 return DAG.getTargetConstant(SplatBits, VT);
Bob Wilson5bafff32009-06-22 23:27:02 +00002959}
2960
Bob Wilsond3c42842010-06-14 22:19:57 +00002961
2962/// getNEONModImm - If this is a valid vector constant for a NEON instruction
2963/// with a "modified immediate" operand (e.g., VMOV) of the specified element
2964/// size, return the encoded value for that immediate. The ByteSize field
2965/// indicates the number of bytes of each element [1248].
Bob Wilson827b2102010-06-15 19:05:35 +00002966SDValue ARM::getNEONModImm(SDNode *N, unsigned ByteSize, bool isVMOV,
2967 SelectionDAG &DAG) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002968 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2969 APInt SplatBits, SplatUndef;
2970 unsigned SplatBitSize;
2971 bool HasAnyUndefs;
2972 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2973 HasAnyUndefs, ByteSize * 8))
2974 return SDValue();
2975
2976 if (SplatBitSize > ByteSize * 8)
2977 return SDValue();
2978
Bob Wilsond3c42842010-06-14 22:19:57 +00002979 return isNEONModifiedImm(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
Bob Wilson827b2102010-06-15 19:05:35 +00002980 SplatBitSize, DAG, isVMOV, true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002981}
2982
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002983static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2984 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002985 unsigned NumElts = VT.getVectorNumElements();
2986 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002987 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002988
2989 // If this is a VEXT shuffle, the immediate value is the index of the first
2990 // element. The other shuffle indices must be the successive elements after
2991 // the first one.
2992 unsigned ExpectedElt = Imm;
2993 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002994 // Increment the expected index. If it wraps around, it may still be
2995 // a VEXT but the source vectors must be swapped.
2996 ExpectedElt += 1;
2997 if (ExpectedElt == NumElts * 2) {
2998 ExpectedElt = 0;
2999 ReverseVEXT = true;
3000 }
3001
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003002 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003003 return false;
3004 }
3005
3006 // Adjust the index value if the source operands will be swapped.
3007 if (ReverseVEXT)
3008 Imm -= NumElts;
3009
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003010 return true;
3011}
3012
Bob Wilson8bb9e482009-07-26 00:39:34 +00003013/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3014/// instruction with the specified blocksize. (The order of the elements
3015/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003016static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3017 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003018 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3019 "Only possible block sizes for VREV are: 16, 32, 64");
3020
Bob Wilson8bb9e482009-07-26 00:39:34 +00003021 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003022 if (EltSz == 64)
3023 return false;
3024
3025 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003026 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003027
3028 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3029 return false;
3030
3031 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003032 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00003033 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
3034 return false;
3035 }
3036
3037 return true;
3038}
3039
Bob Wilsonc692cb72009-08-21 20:54:19 +00003040static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3041 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003042 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3043 if (EltSz == 64)
3044 return false;
3045
Bob Wilsonc692cb72009-08-21 20:54:19 +00003046 unsigned NumElts = VT.getVectorNumElements();
3047 WhichResult = (M[0] == 0 ? 0 : 1);
3048 for (unsigned i = 0; i < NumElts; i += 2) {
3049 if ((unsigned) M[i] != i + WhichResult ||
3050 (unsigned) M[i+1] != i + NumElts + WhichResult)
3051 return false;
3052 }
3053 return true;
3054}
3055
Bob Wilson324f4f12009-12-03 06:40:55 +00003056/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3057/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3058/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3059static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3060 unsigned &WhichResult) {
3061 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3062 if (EltSz == 64)
3063 return false;
3064
3065 unsigned NumElts = VT.getVectorNumElements();
3066 WhichResult = (M[0] == 0 ? 0 : 1);
3067 for (unsigned i = 0; i < NumElts; i += 2) {
3068 if ((unsigned) M[i] != i + WhichResult ||
3069 (unsigned) M[i+1] != i + WhichResult)
3070 return false;
3071 }
3072 return true;
3073}
3074
Bob Wilsonc692cb72009-08-21 20:54:19 +00003075static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3076 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003077 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3078 if (EltSz == 64)
3079 return false;
3080
Bob Wilsonc692cb72009-08-21 20:54:19 +00003081 unsigned NumElts = VT.getVectorNumElements();
3082 WhichResult = (M[0] == 0 ? 0 : 1);
3083 for (unsigned i = 0; i != NumElts; ++i) {
3084 if ((unsigned) M[i] != 2 * i + WhichResult)
3085 return false;
3086 }
3087
3088 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003089 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003090 return false;
3091
3092 return true;
3093}
3094
Bob Wilson324f4f12009-12-03 06:40:55 +00003095/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3096/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3097/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3098static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3099 unsigned &WhichResult) {
3100 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3101 if (EltSz == 64)
3102 return false;
3103
3104 unsigned Half = VT.getVectorNumElements() / 2;
3105 WhichResult = (M[0] == 0 ? 0 : 1);
3106 for (unsigned j = 0; j != 2; ++j) {
3107 unsigned Idx = WhichResult;
3108 for (unsigned i = 0; i != Half; ++i) {
3109 if ((unsigned) M[i + j * Half] != Idx)
3110 return false;
3111 Idx += 2;
3112 }
3113 }
3114
3115 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3116 if (VT.is64BitVector() && EltSz == 32)
3117 return false;
3118
3119 return true;
3120}
3121
Bob Wilsonc692cb72009-08-21 20:54:19 +00003122static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3123 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003124 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3125 if (EltSz == 64)
3126 return false;
3127
Bob Wilsonc692cb72009-08-21 20:54:19 +00003128 unsigned NumElts = VT.getVectorNumElements();
3129 WhichResult = (M[0] == 0 ? 0 : 1);
3130 unsigned Idx = WhichResult * NumElts / 2;
3131 for (unsigned i = 0; i != NumElts; i += 2) {
3132 if ((unsigned) M[i] != Idx ||
3133 (unsigned) M[i+1] != Idx + NumElts)
3134 return false;
3135 Idx += 1;
3136 }
3137
3138 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003139 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003140 return false;
3141
3142 return true;
3143}
3144
Bob Wilson324f4f12009-12-03 06:40:55 +00003145/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3146/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3147/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3148static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3149 unsigned &WhichResult) {
3150 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3151 if (EltSz == 64)
3152 return false;
3153
3154 unsigned NumElts = VT.getVectorNumElements();
3155 WhichResult = (M[0] == 0 ? 0 : 1);
3156 unsigned Idx = WhichResult * NumElts / 2;
3157 for (unsigned i = 0; i != NumElts; i += 2) {
3158 if ((unsigned) M[i] != Idx ||
3159 (unsigned) M[i+1] != Idx)
3160 return false;
3161 Idx += 1;
3162 }
3163
3164 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3165 if (VT.is64BitVector() && EltSz == 32)
3166 return false;
3167
3168 return true;
3169}
3170
3171
Owen Andersone50ed302009-08-10 22:56:29 +00003172static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003173 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00003174 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003175 if (ConstVal->isNullValue())
3176 return getZeroVector(VT, DAG, dl);
3177 if (ConstVal->isAllOnesValue())
3178 return getOnesVector(VT, DAG, dl);
3179
Owen Andersone50ed302009-08-10 22:56:29 +00003180 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00003181 if (VT.is64BitVector()) {
3182 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003183 case 8: CanonicalVT = MVT::v8i8; break;
3184 case 16: CanonicalVT = MVT::v4i16; break;
3185 case 32: CanonicalVT = MVT::v2i32; break;
3186 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003187 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003188 }
3189 } else {
3190 assert(VT.is128BitVector() && "unknown splat vector size");
3191 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003192 case 8: CanonicalVT = MVT::v16i8; break;
3193 case 16: CanonicalVT = MVT::v8i16; break;
3194 case 32: CanonicalVT = MVT::v4i32; break;
3195 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003196 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003197 }
3198 }
3199
3200 // Build a canonical splat for this value.
3201 SmallVector<SDValue, 8> Ops;
3202 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
3203 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
3204 Ops.size());
3205 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
3206}
3207
3208// If this is a case we can't handle, return null and let the default
3209// expansion code take care of it.
3210static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00003211 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003212 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003213 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003214
3215 APInt SplatBits, SplatUndef;
3216 unsigned SplatBitSize;
3217 bool HasAnyUndefs;
3218 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003219 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003220 // Check if an immediate VMOV works.
3221 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
3222 SplatUndef.getZExtValue(),
Bob Wilson827b2102010-06-15 19:05:35 +00003223 SplatBitSize, DAG, true, false);
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003224 if (Val.getNode())
3225 return BuildSplat(Val, VT, DAG, dl);
3226 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003227 }
3228
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003229 // Scan through the operands to see if only one value is used.
3230 unsigned NumElts = VT.getVectorNumElements();
3231 bool isOnlyLowElement = true;
3232 bool usesOnlyOneValue = true;
3233 bool isConstant = true;
3234 SDValue Value;
3235 for (unsigned i = 0; i < NumElts; ++i) {
3236 SDValue V = Op.getOperand(i);
3237 if (V.getOpcode() == ISD::UNDEF)
3238 continue;
3239 if (i > 0)
3240 isOnlyLowElement = false;
3241 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3242 isConstant = false;
3243
3244 if (!Value.getNode())
3245 Value = V;
3246 else if (V != Value)
3247 usesOnlyOneValue = false;
3248 }
3249
3250 if (!Value.getNode())
3251 return DAG.getUNDEF(VT);
3252
3253 if (isOnlyLowElement)
3254 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3255
3256 // If all elements are constants, fall back to the default expansion, which
3257 // will generate a load from the constant pool.
3258 if (isConstant)
3259 return SDValue();
3260
3261 // Use VDUP for non-constant splats.
Bob Wilson069e4342010-05-23 05:42:31 +00003262 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3263 if (usesOnlyOneValue && EltSize <= 32)
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003264 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3265
3266 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003267 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3268 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003269 if (EltSize >= 32) {
3270 // Do the expansion with floating-point types, since that is what the VFP
3271 // registers are defined to use, and since i64 is not legal.
3272 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3273 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003274 SmallVector<SDValue, 8> Ops;
3275 for (unsigned i = 0; i < NumElts; ++i)
3276 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, dl, EltVT, Op.getOperand(i)));
3277 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003278 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003279 }
3280
3281 return SDValue();
3282}
3283
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003284/// isShuffleMaskLegal - Targets can use this to indicate that they only
3285/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3286/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3287/// are assumed to be legal.
3288bool
3289ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
3290 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003291 if (VT.getVectorNumElements() == 4 &&
3292 (VT.is128BitVector() || VT.is64BitVector())) {
3293 unsigned PFIndexes[4];
3294 for (unsigned i = 0; i != 4; ++i) {
3295 if (M[i] < 0)
3296 PFIndexes[i] = 8;
3297 else
3298 PFIndexes[i] = M[i];
3299 }
3300
3301 // Compute the index in the perfect shuffle table.
3302 unsigned PFTableIndex =
3303 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3304 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3305 unsigned Cost = (PFEntry >> 30);
3306
3307 if (Cost <= 4)
3308 return true;
3309 }
3310
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003311 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00003312 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003313
Bob Wilson53dd2452010-06-07 23:53:38 +00003314 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3315 return (EltSize >= 32 ||
3316 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003317 isVREVMask(M, VT, 64) ||
3318 isVREVMask(M, VT, 32) ||
3319 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00003320 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
3321 isVTRNMask(M, VT, WhichResult) ||
3322 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00003323 isVZIPMask(M, VT, WhichResult) ||
3324 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
3325 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
3326 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003327}
3328
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003329/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
3330/// the specified operations to build the shuffle.
3331static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
3332 SDValue RHS, SelectionDAG &DAG,
3333 DebugLoc dl) {
3334 unsigned OpNum = (PFEntry >> 26) & 0x0F;
3335 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
3336 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
3337
3338 enum {
3339 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
3340 OP_VREV,
3341 OP_VDUP0,
3342 OP_VDUP1,
3343 OP_VDUP2,
3344 OP_VDUP3,
3345 OP_VEXT1,
3346 OP_VEXT2,
3347 OP_VEXT3,
3348 OP_VUZPL, // VUZP, left result
3349 OP_VUZPR, // VUZP, right result
3350 OP_VZIPL, // VZIP, left result
3351 OP_VZIPR, // VZIP, right result
3352 OP_VTRNL, // VTRN, left result
3353 OP_VTRNR // VTRN, right result
3354 };
3355
3356 if (OpNum == OP_COPY) {
3357 if (LHSID == (1*9+2)*9+3) return LHS;
3358 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
3359 return RHS;
3360 }
3361
3362 SDValue OpLHS, OpRHS;
3363 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
3364 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
3365 EVT VT = OpLHS.getValueType();
3366
3367 switch (OpNum) {
3368 default: llvm_unreachable("Unknown shuffle opcode!");
3369 case OP_VREV:
3370 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
3371 case OP_VDUP0:
3372 case OP_VDUP1:
3373 case OP_VDUP2:
3374 case OP_VDUP3:
3375 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003376 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003377 case OP_VEXT1:
3378 case OP_VEXT2:
3379 case OP_VEXT3:
3380 return DAG.getNode(ARMISD::VEXT, dl, VT,
3381 OpLHS, OpRHS,
3382 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
3383 case OP_VUZPL:
3384 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003385 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003386 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
3387 case OP_VZIPL:
3388 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003389 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003390 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
3391 case OP_VTRNL:
3392 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003393 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3394 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003395 }
3396}
3397
Bob Wilson5bafff32009-06-22 23:27:02 +00003398static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003399 SDValue V1 = Op.getOperand(0);
3400 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00003401 DebugLoc dl = Op.getDebugLoc();
3402 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003403 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003404 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00003405
Bob Wilson28865062009-08-13 02:13:04 +00003406 // Convert shuffles that are directly supported on NEON to target-specific
3407 // DAG nodes, instead of keeping them as shuffles and matching them again
3408 // during code selection. This is more efficient and avoids the possibility
3409 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00003410 // FIXME: floating-point vectors should be canonicalized to integer vectors
3411 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003412 SVN->getMask(ShuffleMask);
3413
Bob Wilson53dd2452010-06-07 23:53:38 +00003414 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3415 if (EltSize <= 32) {
3416 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
3417 int Lane = SVN->getSplatIndex();
3418 // If this is undef splat, generate it via "just" vdup, if possible.
3419 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00003420
Bob Wilson53dd2452010-06-07 23:53:38 +00003421 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3422 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
3423 }
3424 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
3425 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00003426 }
Bob Wilson53dd2452010-06-07 23:53:38 +00003427
3428 bool ReverseVEXT;
3429 unsigned Imm;
3430 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
3431 if (ReverseVEXT)
3432 std::swap(V1, V2);
3433 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
3434 DAG.getConstant(Imm, MVT::i32));
3435 }
3436
3437 if (isVREVMask(ShuffleMask, VT, 64))
3438 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
3439 if (isVREVMask(ShuffleMask, VT, 32))
3440 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
3441 if (isVREVMask(ShuffleMask, VT, 16))
3442 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
3443
3444 // Check for Neon shuffles that modify both input vectors in place.
3445 // If both results are used, i.e., if there are two shuffles with the same
3446 // source operands and with masks corresponding to both results of one of
3447 // these operations, DAG memoization will ensure that a single node is
3448 // used for both shuffles.
3449 unsigned WhichResult;
3450 if (isVTRNMask(ShuffleMask, VT, WhichResult))
3451 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3452 V1, V2).getValue(WhichResult);
3453 if (isVUZPMask(ShuffleMask, VT, WhichResult))
3454 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3455 V1, V2).getValue(WhichResult);
3456 if (isVZIPMask(ShuffleMask, VT, WhichResult))
3457 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3458 V1, V2).getValue(WhichResult);
3459
3460 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
3461 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3462 V1, V1).getValue(WhichResult);
3463 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3464 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3465 V1, V1).getValue(WhichResult);
3466 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3467 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3468 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00003469 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003470
Bob Wilsonc692cb72009-08-21 20:54:19 +00003471 // If the shuffle is not directly supported and it has 4 elements, use
3472 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003473 unsigned NumElts = VT.getVectorNumElements();
3474 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003475 unsigned PFIndexes[4];
3476 for (unsigned i = 0; i != 4; ++i) {
3477 if (ShuffleMask[i] < 0)
3478 PFIndexes[i] = 8;
3479 else
3480 PFIndexes[i] = ShuffleMask[i];
3481 }
3482
3483 // Compute the index in the perfect shuffle table.
3484 unsigned PFTableIndex =
3485 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003486 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3487 unsigned Cost = (PFEntry >> 30);
3488
3489 if (Cost <= 4)
3490 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
3491 }
Bob Wilsond8e17572009-08-12 22:31:50 +00003492
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003493 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003494 if (EltSize >= 32) {
3495 // Do the expansion with floating-point types, since that is what the VFP
3496 // registers are defined to use, and since i64 is not legal.
3497 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3498 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
3499 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V1);
3500 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003501 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003502 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00003503 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003504 Ops.push_back(DAG.getUNDEF(EltVT));
3505 else
3506 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
3507 ShuffleMask[i] < (int)NumElts ? V1 : V2,
3508 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
3509 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00003510 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003511 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Bob Wilson63b88452010-05-20 18:39:53 +00003512 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
3513 }
3514
Bob Wilson22cac0d2009-08-14 05:16:33 +00003515 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003516}
3517
Bob Wilson5bafff32009-06-22 23:27:02 +00003518static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003519 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003520 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003521 SDValue Vec = Op.getOperand(0);
3522 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00003523 assert(VT == MVT::i32 &&
3524 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
3525 "unexpected type for custom-lowering vector extract");
3526 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00003527}
3528
Bob Wilsona6d65862009-08-03 20:36:38 +00003529static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3530 // The only time a CONCAT_VECTORS operation can have legal types is when
3531 // two 64-bit vectors are concatenated to a 128-bit vector.
3532 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
3533 "unexpected CONCAT_VECTORS");
3534 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00003535 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00003536 SDValue Op0 = Op.getOperand(0);
3537 SDValue Op1 = Op.getOperand(1);
3538 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003539 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3540 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00003541 DAG.getIntPtrConstant(0));
3542 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003543 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3544 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00003545 DAG.getIntPtrConstant(1));
3546 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003547}
3548
Dan Gohmand858e902010-04-17 15:26:15 +00003549SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003550 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003551 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00003552 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00003553 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003554 case ISD::GlobalAddress:
3555 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
3556 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00003557 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00003558 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
3559 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003560 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00003561 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00003562 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00003563 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00003564 case ISD::SINT_TO_FP:
3565 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
3566 case ISD::FP_TO_SINT:
3567 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003568 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003569 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003570 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003571 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00003572 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00003573 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00003574 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
3575 Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00003576 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003577 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00003578 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00003579 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00003580 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003581 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00003582 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00003583 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003584 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
3585 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3586 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003587 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00003588 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003589 }
Dan Gohman475871a2008-07-27 21:46:04 +00003590 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003591}
3592
Duncan Sands1607f052008-12-01 11:39:25 +00003593/// ReplaceNodeResults - Replace the results of node with an illegal result
3594/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00003595void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
3596 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00003597 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00003598 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00003599 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00003600 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003601 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003602 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003603 case ISD::BIT_CONVERT:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003604 Res = ExpandBIT_CONVERT(N, DAG);
3605 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00003606 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003607 case ISD::SRA:
3608 Res = LowerShift(N, DAG, Subtarget);
3609 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003610 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00003611 if (Res.getNode())
3612 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00003613}
Chris Lattner27a6c732007-11-24 07:07:01 +00003614
Evan Chenga8e29892007-01-19 07:51:42 +00003615//===----------------------------------------------------------------------===//
3616// ARM Scheduler Hooks
3617//===----------------------------------------------------------------------===//
3618
3619MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003620ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
3621 MachineBasicBlock *BB,
3622 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00003623 unsigned dest = MI->getOperand(0).getReg();
3624 unsigned ptr = MI->getOperand(1).getReg();
3625 unsigned oldval = MI->getOperand(2).getReg();
3626 unsigned newval = MI->getOperand(3).getReg();
3627 unsigned scratch = BB->getParent()->getRegInfo()
3628 .createVirtualRegister(ARM::GPRRegisterClass);
3629 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3630 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003631 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00003632
3633 unsigned ldrOpc, strOpc;
3634 switch (Size) {
3635 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003636 case 1:
3637 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
3638 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
3639 break;
3640 case 2:
3641 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3642 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3643 break;
3644 case 4:
3645 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3646 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3647 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00003648 }
3649
3650 MachineFunction *MF = BB->getParent();
3651 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3652 MachineFunction::iterator It = BB;
3653 ++It; // insert the new blocks after the current block
3654
3655 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3656 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3657 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3658 MF->insert(It, loop1MBB);
3659 MF->insert(It, loop2MBB);
3660 MF->insert(It, exitMBB);
3661 exitMBB->transferSuccessors(BB);
3662
3663 // thisMBB:
3664 // ...
3665 // fallthrough --> loop1MBB
3666 BB->addSuccessor(loop1MBB);
3667
3668 // loop1MBB:
3669 // ldrex dest, [ptr]
3670 // cmp dest, oldval
3671 // bne exitMBB
3672 BB = loop1MBB;
3673 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003674 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003675 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003676 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3677 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003678 BB->addSuccessor(loop2MBB);
3679 BB->addSuccessor(exitMBB);
3680
3681 // loop2MBB:
3682 // strex scratch, newval, [ptr]
3683 // cmp scratch, #0
3684 // bne loop1MBB
3685 BB = loop2MBB;
3686 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
3687 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003688 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003689 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003690 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3691 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003692 BB->addSuccessor(loop1MBB);
3693 BB->addSuccessor(exitMBB);
3694
3695 // exitMBB:
3696 // ...
3697 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00003698
3699 MF->DeleteMachineInstr(MI); // The instruction is gone now.
3700
Jim Grosbach5278eb82009-12-11 01:42:04 +00003701 return BB;
3702}
3703
3704MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003705ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
3706 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00003707 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
3708 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3709
3710 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003711 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003712 MachineFunction::iterator It = BB;
3713 ++It;
3714
3715 unsigned dest = MI->getOperand(0).getReg();
3716 unsigned ptr = MI->getOperand(1).getReg();
3717 unsigned incr = MI->getOperand(2).getReg();
3718 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00003719
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003720 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003721 unsigned ldrOpc, strOpc;
3722 switch (Size) {
3723 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003724 case 1:
3725 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00003726 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003727 break;
3728 case 2:
3729 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3730 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3731 break;
3732 case 4:
3733 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3734 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3735 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00003736 }
3737
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003738 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3739 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3740 MF->insert(It, loopMBB);
3741 MF->insert(It, exitMBB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003742 exitMBB->transferSuccessors(BB);
3743
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003744 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003745 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3746 unsigned scratch2 = (!BinOpcode) ? incr :
3747 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3748
3749 // thisMBB:
3750 // ...
3751 // fallthrough --> loopMBB
3752 BB->addSuccessor(loopMBB);
3753
3754 // loopMBB:
3755 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003756 // <binop> scratch2, dest, incr
3757 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00003758 // cmp scratch, #0
3759 // bne- loopMBB
3760 // fallthrough --> exitMBB
3761 BB = loopMBB;
3762 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00003763 if (BinOpcode) {
3764 // operand order needs to go the other way for NAND
3765 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
3766 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3767 addReg(incr).addReg(dest)).addReg(0);
3768 else
3769 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3770 addReg(dest).addReg(incr)).addReg(0);
3771 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00003772
3773 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
3774 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003775 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00003776 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003777 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3778 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003779
3780 BB->addSuccessor(loopMBB);
3781 BB->addSuccessor(exitMBB);
3782
3783 // exitMBB:
3784 // ...
3785 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00003786
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003787 MF->DeleteMachineInstr(MI); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00003788
Jim Grosbachc3c23542009-12-14 04:22:04 +00003789 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00003790}
3791
3792MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00003793ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003794 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003795 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00003796 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003797 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00003798 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00003799 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00003800 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00003801 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00003802
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003803 case ARM::ATOMIC_LOAD_ADD_I8:
3804 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3805 case ARM::ATOMIC_LOAD_ADD_I16:
3806 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3807 case ARM::ATOMIC_LOAD_ADD_I32:
3808 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003809
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003810 case ARM::ATOMIC_LOAD_AND_I8:
3811 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3812 case ARM::ATOMIC_LOAD_AND_I16:
3813 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3814 case ARM::ATOMIC_LOAD_AND_I32:
3815 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003816
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003817 case ARM::ATOMIC_LOAD_OR_I8:
3818 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3819 case ARM::ATOMIC_LOAD_OR_I16:
3820 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3821 case ARM::ATOMIC_LOAD_OR_I32:
3822 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003823
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003824 case ARM::ATOMIC_LOAD_XOR_I8:
3825 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3826 case ARM::ATOMIC_LOAD_XOR_I16:
3827 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3828 case ARM::ATOMIC_LOAD_XOR_I32:
3829 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003830
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003831 case ARM::ATOMIC_LOAD_NAND_I8:
3832 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3833 case ARM::ATOMIC_LOAD_NAND_I16:
3834 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3835 case ARM::ATOMIC_LOAD_NAND_I32:
3836 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003837
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003838 case ARM::ATOMIC_LOAD_SUB_I8:
3839 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3840 case ARM::ATOMIC_LOAD_SUB_I16:
3841 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3842 case ARM::ATOMIC_LOAD_SUB_I32:
3843 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003844
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003845 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
3846 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
3847 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00003848
3849 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
3850 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
3851 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003852
Evan Cheng007ea272009-08-12 05:17:19 +00003853 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00003854 // To "insert" a SELECT_CC instruction, we actually have to insert the
3855 // diamond control-flow pattern. The incoming instruction knows the
3856 // destination vreg to set, the condition code register to branch on, the
3857 // true/false values to select between, and a branch opcode to use.
3858 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003859 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00003860 ++It;
3861
3862 // thisMBB:
3863 // ...
3864 // TrueVal = ...
3865 // cmpTY ccX, r1, r2
3866 // bCC copy1MBB
3867 // fallthrough --> copy0MBB
3868 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003869 MachineFunction *F = BB->getParent();
3870 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3871 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00003872 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00003873 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003874 F->insert(It, copy0MBB);
3875 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00003876 // Update machine-CFG edges by first adding all successors of the current
3877 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00003878 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003879 E = BB->succ_end(); I != E; ++I)
Evan Chengce319102009-09-19 09:51:03 +00003880 sinkMBB->addSuccessor(*I);
Evan Chenga8e29892007-01-19 07:51:42 +00003881 // Next, remove all successors of the current block, and add the true
3882 // and fallthrough blocks as its successors.
Evan Chengce319102009-09-19 09:51:03 +00003883 while (!BB->succ_empty())
Evan Chenga8e29892007-01-19 07:51:42 +00003884 BB->removeSuccessor(BB->succ_begin());
3885 BB->addSuccessor(copy0MBB);
3886 BB->addSuccessor(sinkMBB);
3887
3888 // copy0MBB:
3889 // %FalseValue = ...
3890 // # fallthrough to sinkMBB
3891 BB = copy0MBB;
3892
3893 // Update machine-CFG edges
3894 BB->addSuccessor(sinkMBB);
3895
3896 // sinkMBB:
3897 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3898 // ...
3899 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00003900 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00003901 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
3902 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
3903
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003904 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00003905 return BB;
3906 }
Evan Cheng86198642009-08-07 00:34:42 +00003907
3908 case ARM::tANDsp:
3909 case ARM::tADDspr_:
3910 case ARM::tSUBspi_:
3911 case ARM::t2SUBrSPi_:
3912 case ARM::t2SUBrSPi12_:
3913 case ARM::t2SUBrSPs_: {
3914 MachineFunction *MF = BB->getParent();
3915 unsigned DstReg = MI->getOperand(0).getReg();
3916 unsigned SrcReg = MI->getOperand(1).getReg();
3917 bool DstIsDead = MI->getOperand(0).isDead();
3918 bool SrcIsKill = MI->getOperand(1).isKill();
3919
3920 if (SrcReg != ARM::SP) {
3921 // Copy the source to SP from virtual register.
3922 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
3923 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3924 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
3925 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
3926 .addReg(SrcReg, getKillRegState(SrcIsKill));
3927 }
3928
3929 unsigned OpOpc = 0;
3930 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
3931 switch (MI->getOpcode()) {
3932 default:
3933 llvm_unreachable("Unexpected pseudo instruction!");
3934 case ARM::tANDsp:
3935 OpOpc = ARM::tAND;
3936 NeedPred = true;
3937 break;
3938 case ARM::tADDspr_:
3939 OpOpc = ARM::tADDspr;
3940 break;
3941 case ARM::tSUBspi_:
3942 OpOpc = ARM::tSUBspi;
3943 break;
3944 case ARM::t2SUBrSPi_:
3945 OpOpc = ARM::t2SUBrSPi;
3946 NeedPred = true; NeedCC = true;
3947 break;
3948 case ARM::t2SUBrSPi12_:
3949 OpOpc = ARM::t2SUBrSPi12;
3950 NeedPred = true;
3951 break;
3952 case ARM::t2SUBrSPs_:
3953 OpOpc = ARM::t2SUBrSPs;
3954 NeedPred = true; NeedCC = true; NeedOp3 = true;
3955 break;
3956 }
3957 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
3958 if (OpOpc == ARM::tAND)
3959 AddDefaultT1CC(MIB);
3960 MIB.addReg(ARM::SP);
3961 MIB.addOperand(MI->getOperand(2));
3962 if (NeedOp3)
3963 MIB.addOperand(MI->getOperand(3));
3964 if (NeedPred)
3965 AddDefaultPred(MIB);
3966 if (NeedCC)
3967 AddDefaultCC(MIB);
3968
3969 // Copy the result from SP to virtual register.
3970 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
3971 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3972 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
3973 BuildMI(BB, dl, TII->get(CopyOpc))
3974 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
3975 .addReg(ARM::SP);
3976 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3977 return BB;
3978 }
Evan Chenga8e29892007-01-19 07:51:42 +00003979 }
3980}
3981
3982//===----------------------------------------------------------------------===//
3983// ARM Optimization Hooks
3984//===----------------------------------------------------------------------===//
3985
Chris Lattnerd1980a52009-03-12 06:52:53 +00003986static
3987SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3988 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00003989 SelectionDAG &DAG = DCI.DAG;
3990 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003991 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00003992 unsigned Opc = N->getOpcode();
3993 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3994 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3995 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3996 ISD::CondCode CC = ISD::SETCC_INVALID;
3997
3998 if (isSlctCC) {
3999 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
4000 } else {
4001 SDValue CCOp = Slct.getOperand(0);
4002 if (CCOp.getOpcode() == ISD::SETCC)
4003 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
4004 }
4005
4006 bool DoXform = false;
4007 bool InvCC = false;
4008 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
4009 "Bad input!");
4010
4011 if (LHS.getOpcode() == ISD::Constant &&
4012 cast<ConstantSDNode>(LHS)->isNullValue()) {
4013 DoXform = true;
4014 } else if (CC != ISD::SETCC_INVALID &&
4015 RHS.getOpcode() == ISD::Constant &&
4016 cast<ConstantSDNode>(RHS)->isNullValue()) {
4017 std::swap(LHS, RHS);
4018 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004019 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00004020 Op0.getOperand(0).getValueType();
4021 bool isInt = OpVT.isInteger();
4022 CC = ISD::getSetCCInverse(CC, isInt);
4023
4024 if (!TLI.isCondCodeLegal(CC, OpVT))
4025 return SDValue(); // Inverse operator isn't legal.
4026
4027 DoXform = true;
4028 InvCC = true;
4029 }
4030
4031 if (DoXform) {
4032 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
4033 if (isSlctCC)
4034 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
4035 Slct.getOperand(0), Slct.getOperand(1), CC);
4036 SDValue CCOp = Slct.getOperand(0);
4037 if (InvCC)
4038 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
4039 CCOp.getOperand(0), CCOp.getOperand(1), CC);
4040 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
4041 CCOp, OtherOp, Result);
4042 }
4043 return SDValue();
4044}
4045
4046/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
4047static SDValue PerformADDCombine(SDNode *N,
4048 TargetLowering::DAGCombinerInfo &DCI) {
4049 // added by evan in r37685 with no testcase.
4050 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00004051
Chris Lattnerd1980a52009-03-12 06:52:53 +00004052 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
4053 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
4054 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
4055 if (Result.getNode()) return Result;
4056 }
4057 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
4058 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
4059 if (Result.getNode()) return Result;
4060 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00004061
Chris Lattnerd1980a52009-03-12 06:52:53 +00004062 return SDValue();
4063}
4064
4065/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
4066static SDValue PerformSUBCombine(SDNode *N,
4067 TargetLowering::DAGCombinerInfo &DCI) {
4068 // added by evan in r37685 with no testcase.
4069 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00004070
Chris Lattnerd1980a52009-03-12 06:52:53 +00004071 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
4072 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
4073 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
4074 if (Result.getNode()) return Result;
4075 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00004076
Chris Lattnerd1980a52009-03-12 06:52:53 +00004077 return SDValue();
4078}
4079
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004080static SDValue PerformMULCombine(SDNode *N,
4081 TargetLowering::DAGCombinerInfo &DCI,
4082 const ARMSubtarget *Subtarget) {
4083 SelectionDAG &DAG = DCI.DAG;
4084
4085 if (Subtarget->isThumb1Only())
4086 return SDValue();
4087
4088 if (DAG.getMachineFunction().
4089 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
4090 return SDValue();
4091
4092 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
4093 return SDValue();
4094
4095 EVT VT = N->getValueType(0);
4096 if (VT != MVT::i32)
4097 return SDValue();
4098
4099 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
4100 if (!C)
4101 return SDValue();
4102
4103 uint64_t MulAmt = C->getZExtValue();
4104 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
4105 ShiftAmt = ShiftAmt & (32 - 1);
4106 SDValue V = N->getOperand(0);
4107 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004108
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004109 SDValue Res;
4110 MulAmt >>= ShiftAmt;
4111 if (isPowerOf2_32(MulAmt - 1)) {
4112 // (mul x, 2^N + 1) => (add (shl x, N), x)
4113 Res = DAG.getNode(ISD::ADD, DL, VT,
4114 V, DAG.getNode(ISD::SHL, DL, VT,
4115 V, DAG.getConstant(Log2_32(MulAmt-1),
4116 MVT::i32)));
4117 } else if (isPowerOf2_32(MulAmt + 1)) {
4118 // (mul x, 2^N - 1) => (sub (shl x, N), x)
4119 Res = DAG.getNode(ISD::SUB, DL, VT,
4120 DAG.getNode(ISD::SHL, DL, VT,
4121 V, DAG.getConstant(Log2_32(MulAmt+1),
4122 MVT::i32)),
4123 V);
4124 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004125 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004126
4127 if (ShiftAmt != 0)
4128 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
4129 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004130
4131 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004132 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004133 return SDValue();
4134}
4135
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +00004136/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
4137/// ARMISD::VMOVRRD.
Jim Grosbache5165492009-11-09 00:11:35 +00004138static SDValue PerformVMOVRRDCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004139 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004140 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00004141 SDValue InDouble = N->getOperand(0);
Jim Grosbache5165492009-11-09 00:11:35 +00004142 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004143 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004144 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004145}
4146
Bob Wilson5bafff32009-06-22 23:27:02 +00004147/// getVShiftImm - Check if this is a valid build_vector for the immediate
4148/// operand of a vector shift operation, where all the elements of the
4149/// build_vector must have the same constant integer value.
4150static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
4151 // Ignore bit_converts.
4152 while (Op.getOpcode() == ISD::BIT_CONVERT)
4153 Op = Op.getOperand(0);
4154 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
4155 APInt SplatBits, SplatUndef;
4156 unsigned SplatBitSize;
4157 bool HasAnyUndefs;
4158 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
4159 HasAnyUndefs, ElementBits) ||
4160 SplatBitSize > ElementBits)
4161 return false;
4162 Cnt = SplatBits.getSExtValue();
4163 return true;
4164}
4165
4166/// isVShiftLImm - Check if this is a valid build_vector for the immediate
4167/// operand of a vector shift left operation. That value must be in the range:
4168/// 0 <= Value < ElementBits for a left shift; or
4169/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00004170static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00004171 assert(VT.isVector() && "vector shift count is not a vector type");
4172 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
4173 if (! getVShiftImm(Op, ElementBits, Cnt))
4174 return false;
4175 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
4176}
4177
4178/// isVShiftRImm - Check if this is a valid build_vector for the immediate
4179/// operand of a vector shift right operation. For a shift opcode, the value
4180/// is positive, but for an intrinsic the value count must be negative. The
4181/// absolute value must be in the range:
4182/// 1 <= |Value| <= ElementBits for a right shift; or
4183/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00004184static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00004185 int64_t &Cnt) {
4186 assert(VT.isVector() && "vector shift count is not a vector type");
4187 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
4188 if (! getVShiftImm(Op, ElementBits, Cnt))
4189 return false;
4190 if (isIntrinsic)
4191 Cnt = -Cnt;
4192 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
4193}
4194
4195/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
4196static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
4197 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
4198 switch (IntNo) {
4199 default:
4200 // Don't do anything for most intrinsics.
4201 break;
4202
4203 // Vector shifts: check for immediate versions and lower them.
4204 // Note: This is done during DAG combining instead of DAG legalizing because
4205 // the build_vectors for 64-bit vector element shift counts are generally
4206 // not legal, and it is hard to see their values after they get legalized to
4207 // loads from a constant pool.
4208 case Intrinsic::arm_neon_vshifts:
4209 case Intrinsic::arm_neon_vshiftu:
4210 case Intrinsic::arm_neon_vshiftls:
4211 case Intrinsic::arm_neon_vshiftlu:
4212 case Intrinsic::arm_neon_vshiftn:
4213 case Intrinsic::arm_neon_vrshifts:
4214 case Intrinsic::arm_neon_vrshiftu:
4215 case Intrinsic::arm_neon_vrshiftn:
4216 case Intrinsic::arm_neon_vqshifts:
4217 case Intrinsic::arm_neon_vqshiftu:
4218 case Intrinsic::arm_neon_vqshiftsu:
4219 case Intrinsic::arm_neon_vqshiftns:
4220 case Intrinsic::arm_neon_vqshiftnu:
4221 case Intrinsic::arm_neon_vqshiftnsu:
4222 case Intrinsic::arm_neon_vqrshiftns:
4223 case Intrinsic::arm_neon_vqrshiftnu:
4224 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00004225 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004226 int64_t Cnt;
4227 unsigned VShiftOpc = 0;
4228
4229 switch (IntNo) {
4230 case Intrinsic::arm_neon_vshifts:
4231 case Intrinsic::arm_neon_vshiftu:
4232 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
4233 VShiftOpc = ARMISD::VSHL;
4234 break;
4235 }
4236 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
4237 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
4238 ARMISD::VSHRs : ARMISD::VSHRu);
4239 break;
4240 }
4241 return SDValue();
4242
4243 case Intrinsic::arm_neon_vshiftls:
4244 case Intrinsic::arm_neon_vshiftlu:
4245 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
4246 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00004247 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004248
4249 case Intrinsic::arm_neon_vrshifts:
4250 case Intrinsic::arm_neon_vrshiftu:
4251 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
4252 break;
4253 return SDValue();
4254
4255 case Intrinsic::arm_neon_vqshifts:
4256 case Intrinsic::arm_neon_vqshiftu:
4257 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
4258 break;
4259 return SDValue();
4260
4261 case Intrinsic::arm_neon_vqshiftsu:
4262 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
4263 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00004264 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004265
4266 case Intrinsic::arm_neon_vshiftn:
4267 case Intrinsic::arm_neon_vrshiftn:
4268 case Intrinsic::arm_neon_vqshiftns:
4269 case Intrinsic::arm_neon_vqshiftnu:
4270 case Intrinsic::arm_neon_vqshiftnsu:
4271 case Intrinsic::arm_neon_vqrshiftns:
4272 case Intrinsic::arm_neon_vqrshiftnu:
4273 case Intrinsic::arm_neon_vqrshiftnsu:
4274 // Narrowing shifts require an immediate right shift.
4275 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
4276 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00004277 llvm_unreachable("invalid shift count for narrowing vector shift "
4278 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004279
4280 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004281 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00004282 }
4283
4284 switch (IntNo) {
4285 case Intrinsic::arm_neon_vshifts:
4286 case Intrinsic::arm_neon_vshiftu:
4287 // Opcode already set above.
4288 break;
4289 case Intrinsic::arm_neon_vshiftls:
4290 case Intrinsic::arm_neon_vshiftlu:
4291 if (Cnt == VT.getVectorElementType().getSizeInBits())
4292 VShiftOpc = ARMISD::VSHLLi;
4293 else
4294 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
4295 ARMISD::VSHLLs : ARMISD::VSHLLu);
4296 break;
4297 case Intrinsic::arm_neon_vshiftn:
4298 VShiftOpc = ARMISD::VSHRN; break;
4299 case Intrinsic::arm_neon_vrshifts:
4300 VShiftOpc = ARMISD::VRSHRs; break;
4301 case Intrinsic::arm_neon_vrshiftu:
4302 VShiftOpc = ARMISD::VRSHRu; break;
4303 case Intrinsic::arm_neon_vrshiftn:
4304 VShiftOpc = ARMISD::VRSHRN; break;
4305 case Intrinsic::arm_neon_vqshifts:
4306 VShiftOpc = ARMISD::VQSHLs; break;
4307 case Intrinsic::arm_neon_vqshiftu:
4308 VShiftOpc = ARMISD::VQSHLu; break;
4309 case Intrinsic::arm_neon_vqshiftsu:
4310 VShiftOpc = ARMISD::VQSHLsu; break;
4311 case Intrinsic::arm_neon_vqshiftns:
4312 VShiftOpc = ARMISD::VQSHRNs; break;
4313 case Intrinsic::arm_neon_vqshiftnu:
4314 VShiftOpc = ARMISD::VQSHRNu; break;
4315 case Intrinsic::arm_neon_vqshiftnsu:
4316 VShiftOpc = ARMISD::VQSHRNsu; break;
4317 case Intrinsic::arm_neon_vqrshiftns:
4318 VShiftOpc = ARMISD::VQRSHRNs; break;
4319 case Intrinsic::arm_neon_vqrshiftnu:
4320 VShiftOpc = ARMISD::VQRSHRNu; break;
4321 case Intrinsic::arm_neon_vqrshiftnsu:
4322 VShiftOpc = ARMISD::VQRSHRNsu; break;
4323 }
4324
4325 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004326 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004327 }
4328
4329 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00004330 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004331 int64_t Cnt;
4332 unsigned VShiftOpc = 0;
4333
4334 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
4335 VShiftOpc = ARMISD::VSLI;
4336 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
4337 VShiftOpc = ARMISD::VSRI;
4338 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00004339 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004340 }
4341
4342 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
4343 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00004344 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004345 }
4346
4347 case Intrinsic::arm_neon_vqrshifts:
4348 case Intrinsic::arm_neon_vqrshiftu:
4349 // No immediate versions of these to check for.
4350 break;
4351 }
4352
4353 return SDValue();
4354}
4355
4356/// PerformShiftCombine - Checks for immediate versions of vector shifts and
4357/// lowers them. As with the vector shift intrinsics, this is done during DAG
4358/// combining instead of DAG legalizing because the build_vectors for 64-bit
4359/// vector element shift counts are generally not legal, and it is hard to see
4360/// their values after they get legalized to loads from a constant pool.
4361static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
4362 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00004363 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00004364
4365 // Nothing to be done for scalar shifts.
4366 if (! VT.isVector())
4367 return SDValue();
4368
4369 assert(ST->hasNEON() && "unexpected vector shift");
4370 int64_t Cnt;
4371
4372 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004373 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00004374
4375 case ISD::SHL:
4376 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
4377 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004378 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004379 break;
4380
4381 case ISD::SRA:
4382 case ISD::SRL:
4383 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
4384 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
4385 ARMISD::VSHRs : ARMISD::VSHRu);
4386 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004387 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004388 }
4389 }
4390 return SDValue();
4391}
4392
4393/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
4394/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
4395static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
4396 const ARMSubtarget *ST) {
4397 SDValue N0 = N->getOperand(0);
4398
4399 // Check for sign- and zero-extensions of vector extract operations of 8-
4400 // and 16-bit vector elements. NEON supports these directly. They are
4401 // handled during DAG combining because type legalization will promote them
4402 // to 32-bit types and it is messy to recognize the operations after that.
4403 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
4404 SDValue Vec = N0.getOperand(0);
4405 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004406 EVT VT = N->getValueType(0);
4407 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004408 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4409
Owen Anderson825b72b2009-08-11 20:47:22 +00004410 if (VT == MVT::i32 &&
4411 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00004412 TLI.isTypeLegal(Vec.getValueType())) {
4413
4414 unsigned Opc = 0;
4415 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004416 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00004417 case ISD::SIGN_EXTEND:
4418 Opc = ARMISD::VGETLANEs;
4419 break;
4420 case ISD::ZERO_EXTEND:
4421 case ISD::ANY_EXTEND:
4422 Opc = ARMISD::VGETLANEu;
4423 break;
4424 }
4425 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
4426 }
4427 }
4428
4429 return SDValue();
4430}
4431
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004432/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
4433/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
4434static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
4435 const ARMSubtarget *ST) {
4436 // If the target supports NEON, try to use vmax/vmin instructions for f32
4437 // selects like "x < y ? x : y". Unless the FiniteOnlyFPMath option is set,
4438 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
4439 // a NaN; only do the transformation when it matches that behavior.
4440
4441 // For now only do this when using NEON for FP operations; if using VFP, it
4442 // is not obvious that the benefit outweighs the cost of switching to the
4443 // NEON pipeline.
4444 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
4445 N->getValueType(0) != MVT::f32)
4446 return SDValue();
4447
4448 SDValue CondLHS = N->getOperand(0);
4449 SDValue CondRHS = N->getOperand(1);
4450 SDValue LHS = N->getOperand(2);
4451 SDValue RHS = N->getOperand(3);
4452 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
4453
4454 unsigned Opcode = 0;
4455 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00004456 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004457 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00004458 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004459 IsReversed = true ; // x CC y ? y : x
4460 } else {
4461 return SDValue();
4462 }
4463
Bob Wilsone742bb52010-02-24 22:15:53 +00004464 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004465 switch (CC) {
4466 default: break;
4467 case ISD::SETOLT:
4468 case ISD::SETOLE:
4469 case ISD::SETLT:
4470 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004471 case ISD::SETULT:
4472 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004473 // If LHS is NaN, an ordered comparison will be false and the result will
4474 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
4475 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4476 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
4477 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4478 break;
4479 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
4480 // will return -0, so vmin can only be used for unsafe math or if one of
4481 // the operands is known to be nonzero.
4482 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
4483 !UnsafeFPMath &&
4484 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4485 break;
4486 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004487 break;
4488
4489 case ISD::SETOGT:
4490 case ISD::SETOGE:
4491 case ISD::SETGT:
4492 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004493 case ISD::SETUGT:
4494 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004495 // If LHS is NaN, an ordered comparison will be false and the result will
4496 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
4497 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4498 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
4499 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4500 break;
4501 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
4502 // will return +0, so vmax can only be used for unsafe math or if one of
4503 // the operands is known to be nonzero.
4504 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
4505 !UnsafeFPMath &&
4506 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4507 break;
4508 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004509 break;
4510 }
4511
4512 if (!Opcode)
4513 return SDValue();
4514 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
4515}
4516
Dan Gohman475871a2008-07-27 21:46:04 +00004517SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004518 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004519 switch (N->getOpcode()) {
4520 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004521 case ISD::ADD: return PerformADDCombine(N, DCI);
4522 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004523 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbache5165492009-11-09 00:11:35 +00004524 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004525 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004526 case ISD::SHL:
4527 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004528 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004529 case ISD::SIGN_EXTEND:
4530 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004531 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
4532 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004533 }
Dan Gohman475871a2008-07-27 21:46:04 +00004534 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004535}
4536
Bill Wendlingaf566342009-08-15 21:21:19 +00004537bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
4538 if (!Subtarget->hasV6Ops())
4539 // Pre-v6 does not support unaligned mem access.
4540 return false;
Anton Korobeynikov90cfc132010-01-30 14:08:12 +00004541 else {
4542 // v6+ may or may not support unaligned mem access depending on the system
4543 // configuration.
4544 // FIXME: This is pretty conservative. Should we provide cmdline option to
4545 // control the behaviour?
Bill Wendlingaf566342009-08-15 21:21:19 +00004546 if (!Subtarget->isTargetDarwin())
4547 return false;
4548 }
4549
4550 switch (VT.getSimpleVT().SimpleTy) {
4551 default:
4552 return false;
4553 case MVT::i8:
4554 case MVT::i16:
4555 case MVT::i32:
4556 return true;
4557 // FIXME: VLD1 etc with standard alignment is legal.
4558 }
4559}
4560
Evan Chenge6c835f2009-08-14 20:09:37 +00004561static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
4562 if (V < 0)
4563 return false;
4564
4565 unsigned Scale = 1;
4566 switch (VT.getSimpleVT().SimpleTy) {
4567 default: return false;
4568 case MVT::i1:
4569 case MVT::i8:
4570 // Scale == 1;
4571 break;
4572 case MVT::i16:
4573 // Scale == 2;
4574 Scale = 2;
4575 break;
4576 case MVT::i32:
4577 // Scale == 4;
4578 Scale = 4;
4579 break;
4580 }
4581
4582 if ((V & (Scale - 1)) != 0)
4583 return false;
4584 V /= Scale;
4585 return V == (V & ((1LL << 5) - 1));
4586}
4587
4588static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
4589 const ARMSubtarget *Subtarget) {
4590 bool isNeg = false;
4591 if (V < 0) {
4592 isNeg = true;
4593 V = - V;
4594 }
4595
4596 switch (VT.getSimpleVT().SimpleTy) {
4597 default: return false;
4598 case MVT::i1:
4599 case MVT::i8:
4600 case MVT::i16:
4601 case MVT::i32:
4602 // + imm12 or - imm8
4603 if (isNeg)
4604 return V == (V & ((1LL << 8) - 1));
4605 return V == (V & ((1LL << 12) - 1));
4606 case MVT::f32:
4607 case MVT::f64:
4608 // Same as ARM mode. FIXME: NEON?
4609 if (!Subtarget->hasVFP2())
4610 return false;
4611 if ((V & 3) != 0)
4612 return false;
4613 V >>= 2;
4614 return V == (V & ((1LL << 8) - 1));
4615 }
4616}
4617
Evan Chengb01fad62007-03-12 23:30:29 +00004618/// isLegalAddressImmediate - Return true if the integer value can be used
4619/// as the offset of the target addressing mode for load / store of the
4620/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00004621static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004622 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00004623 if (V == 0)
4624 return true;
4625
Evan Cheng65011532009-03-09 19:15:00 +00004626 if (!VT.isSimple())
4627 return false;
4628
Evan Chenge6c835f2009-08-14 20:09:37 +00004629 if (Subtarget->isThumb1Only())
4630 return isLegalT1AddressImmediate(V, VT);
4631 else if (Subtarget->isThumb2())
4632 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00004633
Evan Chenge6c835f2009-08-14 20:09:37 +00004634 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00004635 if (V < 0)
4636 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00004637 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00004638 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004639 case MVT::i1:
4640 case MVT::i8:
4641 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00004642 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004643 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004644 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00004645 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004646 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004647 case MVT::f32:
4648 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00004649 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00004650 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00004651 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00004652 return false;
4653 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004654 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00004655 }
Evan Chenga8e29892007-01-19 07:51:42 +00004656}
4657
Evan Chenge6c835f2009-08-14 20:09:37 +00004658bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
4659 EVT VT) const {
4660 int Scale = AM.Scale;
4661 if (Scale < 0)
4662 return false;
4663
4664 switch (VT.getSimpleVT().SimpleTy) {
4665 default: return false;
4666 case MVT::i1:
4667 case MVT::i8:
4668 case MVT::i16:
4669 case MVT::i32:
4670 if (Scale == 1)
4671 return true;
4672 // r + r << imm
4673 Scale = Scale & ~1;
4674 return Scale == 2 || Scale == 4 || Scale == 8;
4675 case MVT::i64:
4676 // r + r
4677 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
4678 return true;
4679 return false;
4680 case MVT::isVoid:
4681 // Note, we allow "void" uses (basically, uses that aren't loads or
4682 // stores), because arm allows folding a scale into many arithmetic
4683 // operations. This should be made more precise and revisited later.
4684
4685 // Allow r << imm, but the imm has to be a multiple of two.
4686 if (Scale & 1) return false;
4687 return isPowerOf2_32(Scale);
4688 }
4689}
4690
Chris Lattner37caf8c2007-04-09 23:33:39 +00004691/// isLegalAddressingMode - Return true if the addressing mode represented
4692/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004693bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004694 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004695 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00004696 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00004697 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004698
Chris Lattner37caf8c2007-04-09 23:33:39 +00004699 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004700 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004701 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004702
Chris Lattner37caf8c2007-04-09 23:33:39 +00004703 switch (AM.Scale) {
4704 case 0: // no scale reg, must be "r+i" or "r", or "i".
4705 break;
4706 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00004707 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00004708 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004709 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00004710 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004711 // ARM doesn't support any R+R*scale+imm addr modes.
4712 if (AM.BaseOffs)
4713 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004714
Bob Wilson2c7dab12009-04-08 17:55:28 +00004715 if (!VT.isSimple())
4716 return false;
4717
Evan Chenge6c835f2009-08-14 20:09:37 +00004718 if (Subtarget->isThumb2())
4719 return isLegalT2ScaledAddressingMode(AM, VT);
4720
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004721 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00004722 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00004723 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004724 case MVT::i1:
4725 case MVT::i8:
4726 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004727 if (Scale < 0) Scale = -Scale;
4728 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004729 return true;
4730 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00004731 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00004732 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00004733 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004734 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004735 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004736 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00004737 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004738
Owen Anderson825b72b2009-08-11 20:47:22 +00004739 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004740 // Note, we allow "void" uses (basically, uses that aren't loads or
4741 // stores), because arm allows folding a scale into many arithmetic
4742 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004743
Chris Lattner37caf8c2007-04-09 23:33:39 +00004744 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00004745 if (Scale & 1) return false;
4746 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00004747 }
4748 break;
Evan Chengb01fad62007-03-12 23:30:29 +00004749 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00004750 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00004751}
4752
Evan Cheng77e47512009-11-11 19:05:52 +00004753/// isLegalICmpImmediate - Return true if the specified immediate is legal
4754/// icmp immediate, that is the target has icmp instructions which can compare
4755/// a register against the immediate without having to materialize the
4756/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00004757bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00004758 if (!Subtarget->isThumb())
4759 return ARM_AM::getSOImmVal(Imm) != -1;
4760 if (Subtarget->isThumb2())
4761 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00004762 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00004763}
4764
Owen Andersone50ed302009-08-10 22:56:29 +00004765static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004766 bool isSEXTLoad, SDValue &Base,
4767 SDValue &Offset, bool &isInc,
4768 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00004769 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4770 return false;
4771
Owen Anderson825b72b2009-08-11 20:47:22 +00004772 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00004773 // AddressingMode 3
4774 Base = Ptr->getOperand(0);
4775 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004776 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004777 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004778 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004779 isInc = false;
4780 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4781 return true;
4782 }
4783 }
4784 isInc = (Ptr->getOpcode() == ISD::ADD);
4785 Offset = Ptr->getOperand(1);
4786 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004787 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00004788 // AddressingMode 2
4789 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004790 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004791 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004792 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004793 isInc = false;
4794 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4795 Base = Ptr->getOperand(0);
4796 return true;
4797 }
4798 }
4799
4800 if (Ptr->getOpcode() == ISD::ADD) {
4801 isInc = true;
4802 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
4803 if (ShOpcVal != ARM_AM::no_shift) {
4804 Base = Ptr->getOperand(1);
4805 Offset = Ptr->getOperand(0);
4806 } else {
4807 Base = Ptr->getOperand(0);
4808 Offset = Ptr->getOperand(1);
4809 }
4810 return true;
4811 }
4812
4813 isInc = (Ptr->getOpcode() == ISD::ADD);
4814 Base = Ptr->getOperand(0);
4815 Offset = Ptr->getOperand(1);
4816 return true;
4817 }
4818
Jim Grosbache5165492009-11-09 00:11:35 +00004819 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00004820 return false;
4821}
4822
Owen Andersone50ed302009-08-10 22:56:29 +00004823static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004824 bool isSEXTLoad, SDValue &Base,
4825 SDValue &Offset, bool &isInc,
4826 SelectionDAG &DAG) {
4827 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4828 return false;
4829
4830 Base = Ptr->getOperand(0);
4831 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
4832 int RHSC = (int)RHS->getZExtValue();
4833 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
4834 assert(Ptr->getOpcode() == ISD::ADD);
4835 isInc = false;
4836 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4837 return true;
4838 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
4839 isInc = Ptr->getOpcode() == ISD::ADD;
4840 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
4841 return true;
4842 }
4843 }
4844
4845 return false;
4846}
4847
Evan Chenga8e29892007-01-19 07:51:42 +00004848/// getPreIndexedAddressParts - returns true by value, base pointer and
4849/// offset pointer and addressing mode by reference if the node's address
4850/// can be legally represented as pre-indexed load / store address.
4851bool
Dan Gohman475871a2008-07-27 21:46:04 +00004852ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
4853 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004854 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004855 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004856 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004857 return false;
4858
Owen Andersone50ed302009-08-10 22:56:29 +00004859 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004860 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004861 bool isSEXTLoad = false;
4862 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4863 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004864 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004865 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4866 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
4867 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004868 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004869 } else
4870 return false;
4871
4872 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004873 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004874 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004875 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
4876 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004877 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004878 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00004879 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00004880 if (!isLegal)
4881 return false;
4882
4883 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
4884 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004885}
4886
4887/// getPostIndexedAddressParts - returns true by value, base pointer and
4888/// offset pointer and addressing mode by reference if this node can be
4889/// combined with a load / store to form a post-indexed load / store.
4890bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00004891 SDValue &Base,
4892 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004893 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004894 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004895 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004896 return false;
4897
Owen Andersone50ed302009-08-10 22:56:29 +00004898 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004899 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004900 bool isSEXTLoad = false;
4901 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004902 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00004903 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00004904 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4905 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004906 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00004907 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00004908 } else
4909 return false;
4910
4911 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004912 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004913 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004914 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00004915 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004916 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004917 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
4918 isInc, DAG);
4919 if (!isLegal)
4920 return false;
4921
Evan Cheng28dad2a2010-05-18 21:31:17 +00004922 if (Ptr != Base) {
4923 // Swap base ptr and offset to catch more post-index load / store when
4924 // it's legal. In Thumb2 mode, offset must be an immediate.
4925 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
4926 !Subtarget->isThumb2())
4927 std::swap(Base, Offset);
4928
4929 // Post-indexed load / store update the base pointer.
4930 if (Ptr != Base)
4931 return false;
4932 }
4933
Evan Chenge88d5ce2009-07-02 07:28:31 +00004934 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
4935 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004936}
4937
Dan Gohman475871a2008-07-27 21:46:04 +00004938void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00004939 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004940 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004941 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00004942 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00004943 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004944 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004945 switch (Op.getOpcode()) {
4946 default: break;
4947 case ARMISD::CMOV: {
4948 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00004949 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004950 if (KnownZero == 0 && KnownOne == 0) return;
4951
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004952 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00004953 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
4954 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004955 KnownZero &= KnownZeroRHS;
4956 KnownOne &= KnownOneRHS;
4957 return;
4958 }
4959 }
4960}
4961
4962//===----------------------------------------------------------------------===//
4963// ARM Inline Assembly Support
4964//===----------------------------------------------------------------------===//
4965
4966/// getConstraintType - Given a constraint letter, return the type of
4967/// constraint it is for this target.
4968ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00004969ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
4970 if (Constraint.size() == 1) {
4971 switch (Constraint[0]) {
4972 default: break;
4973 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004974 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00004975 }
Evan Chenga8e29892007-01-19 07:51:42 +00004976 }
Chris Lattner4234f572007-03-25 02:14:49 +00004977 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00004978}
4979
Bob Wilson2dc4f542009-03-20 22:42:55 +00004980std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00004981ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004982 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004983 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004984 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00004985 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004986 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004987 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004988 return std::make_pair(0U, ARM::tGPRRegisterClass);
4989 else
4990 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004991 case 'r':
4992 return std::make_pair(0U, ARM::GPRRegisterClass);
4993 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004994 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004995 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00004996 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004997 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00004998 if (VT.getSizeInBits() == 128)
4999 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005000 break;
Evan Chenga8e29892007-01-19 07:51:42 +00005001 }
5002 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00005003 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00005004 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00005005
Evan Chenga8e29892007-01-19 07:51:42 +00005006 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
5007}
5008
5009std::vector<unsigned> ARMTargetLowering::
5010getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00005011 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005012 if (Constraint.size() != 1)
5013 return std::vector<unsigned>();
5014
5015 switch (Constraint[0]) { // GCC ARM Constraint Letters
5016 default: break;
5017 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00005018 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
5019 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
5020 0);
Evan Chenga8e29892007-01-19 07:51:42 +00005021 case 'r':
5022 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
5023 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
5024 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
5025 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005026 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00005027 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005028 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
5029 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
5030 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
5031 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
5032 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
5033 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
5034 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
5035 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00005036 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005037 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
5038 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
5039 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
5040 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00005041 if (VT.getSizeInBits() == 128)
5042 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
5043 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005044 break;
Evan Chenga8e29892007-01-19 07:51:42 +00005045 }
5046
5047 return std::vector<unsigned>();
5048}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005049
5050/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
5051/// vector. If it is invalid, don't add anything to Ops.
5052void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
5053 char Constraint,
5054 bool hasMemory,
5055 std::vector<SDValue>&Ops,
5056 SelectionDAG &DAG) const {
5057 SDValue Result(0, 0);
5058
5059 switch (Constraint) {
5060 default: break;
5061 case 'I': case 'J': case 'K': case 'L':
5062 case 'M': case 'N': case 'O':
5063 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
5064 if (!C)
5065 return;
5066
5067 int64_t CVal64 = C->getSExtValue();
5068 int CVal = (int) CVal64;
5069 // None of these constraints allow values larger than 32 bits. Check
5070 // that the value fits in an int.
5071 if (CVal != CVal64)
5072 return;
5073
5074 switch (Constraint) {
5075 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005076 if (Subtarget->isThumb1Only()) {
5077 // This must be a constant between 0 and 255, for ADD
5078 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005079 if (CVal >= 0 && CVal <= 255)
5080 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005081 } else if (Subtarget->isThumb2()) {
5082 // A constant that can be used as an immediate value in a
5083 // data-processing instruction.
5084 if (ARM_AM::getT2SOImmVal(CVal) != -1)
5085 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005086 } else {
5087 // A constant that can be used as an immediate value in a
5088 // data-processing instruction.
5089 if (ARM_AM::getSOImmVal(CVal) != -1)
5090 break;
5091 }
5092 return;
5093
5094 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005095 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005096 // This must be a constant between -255 and -1, for negated ADD
5097 // immediates. This can be used in GCC with an "n" modifier that
5098 // prints the negated value, for use with SUB instructions. It is
5099 // not useful otherwise but is implemented for compatibility.
5100 if (CVal >= -255 && CVal <= -1)
5101 break;
5102 } else {
5103 // This must be a constant between -4095 and 4095. It is not clear
5104 // what this constraint is intended for. Implemented for
5105 // compatibility with GCC.
5106 if (CVal >= -4095 && CVal <= 4095)
5107 break;
5108 }
5109 return;
5110
5111 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005112 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005113 // A 32-bit value where only one byte has a nonzero value. Exclude
5114 // zero to match GCC. This constraint is used by GCC internally for
5115 // constants that can be loaded with a move/shift combination.
5116 // It is not useful otherwise but is implemented for compatibility.
5117 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
5118 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005119 } else if (Subtarget->isThumb2()) {
5120 // A constant whose bitwise inverse can be used as an immediate
5121 // value in a data-processing instruction. This can be used in GCC
5122 // with a "B" modifier that prints the inverted value, for use with
5123 // BIC and MVN instructions. It is not useful otherwise but is
5124 // implemented for compatibility.
5125 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
5126 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005127 } else {
5128 // A constant whose bitwise inverse can be used as an immediate
5129 // value in a data-processing instruction. This can be used in GCC
5130 // with a "B" modifier that prints the inverted value, for use with
5131 // BIC and MVN instructions. It is not useful otherwise but is
5132 // implemented for compatibility.
5133 if (ARM_AM::getSOImmVal(~CVal) != -1)
5134 break;
5135 }
5136 return;
5137
5138 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005139 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005140 // This must be a constant between -7 and 7,
5141 // for 3-operand ADD/SUB immediate instructions.
5142 if (CVal >= -7 && CVal < 7)
5143 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005144 } else if (Subtarget->isThumb2()) {
5145 // A constant whose negation can be used as an immediate value in a
5146 // data-processing instruction. This can be used in GCC with an "n"
5147 // modifier that prints the negated value, for use with SUB
5148 // instructions. It is not useful otherwise but is implemented for
5149 // compatibility.
5150 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
5151 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005152 } else {
5153 // A constant whose negation can be used as an immediate value in a
5154 // data-processing instruction. This can be used in GCC with an "n"
5155 // modifier that prints the negated value, for use with SUB
5156 // instructions. It is not useful otherwise but is implemented for
5157 // compatibility.
5158 if (ARM_AM::getSOImmVal(-CVal) != -1)
5159 break;
5160 }
5161 return;
5162
5163 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005164 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005165 // This must be a multiple of 4 between 0 and 1020, for
5166 // ADD sp + immediate.
5167 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
5168 break;
5169 } else {
5170 // A power of two or a constant between 0 and 32. This is used in
5171 // GCC for the shift amount on shifted register operands, but it is
5172 // useful in general for any shift amounts.
5173 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
5174 break;
5175 }
5176 return;
5177
5178 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005179 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005180 // This must be a constant between 0 and 31, for shift amounts.
5181 if (CVal >= 0 && CVal <= 31)
5182 break;
5183 }
5184 return;
5185
5186 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005187 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005188 // This must be a multiple of 4 between -508 and 508, for
5189 // ADD/SUB sp = sp + immediate.
5190 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
5191 break;
5192 }
5193 return;
5194 }
5195 Result = DAG.getTargetConstant(CVal, Op.getValueType());
5196 break;
5197 }
5198
5199 if (Result.getNode()) {
5200 Ops.push_back(Result);
5201 return;
5202 }
5203 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
5204 Ops, DAG);
5205}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00005206
5207bool
5208ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
5209 // The ARM target isn't yet aware of offsets.
5210 return false;
5211}
Evan Cheng39382422009-10-28 01:44:26 +00005212
5213int ARM::getVFPf32Imm(const APFloat &FPImm) {
5214 APInt Imm = FPImm.bitcastToAPInt();
5215 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
5216 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
5217 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
5218
5219 // We can handle 4 bits of mantissa.
5220 // mantissa = (16+UInt(e:f:g:h))/16.
5221 if (Mantissa & 0x7ffff)
5222 return -1;
5223 Mantissa >>= 19;
5224 if ((Mantissa & 0xf) != Mantissa)
5225 return -1;
5226
5227 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
5228 if (Exp < -3 || Exp > 4)
5229 return -1;
5230 Exp = ((Exp+3) & 0x7) ^ 4;
5231
5232 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
5233}
5234
5235int ARM::getVFPf64Imm(const APFloat &FPImm) {
5236 APInt Imm = FPImm.bitcastToAPInt();
5237 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
5238 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
5239 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
5240
5241 // We can handle 4 bits of mantissa.
5242 // mantissa = (16+UInt(e:f:g:h))/16.
5243 if (Mantissa & 0xffffffffffffLL)
5244 return -1;
5245 Mantissa >>= 48;
5246 if ((Mantissa & 0xf) != Mantissa)
5247 return -1;
5248
5249 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
5250 if (Exp < -3 || Exp > 4)
5251 return -1;
5252 Exp = ((Exp+3) & 0x7) ^ 4;
5253
5254 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
5255}
5256
5257/// isFPImmLegal - Returns true if the target can instruction select the
5258/// specified FP immediate natively. If false, the legalizer will
5259/// materialize the FP immediate as a load from a constant pool.
5260bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
5261 if (!Subtarget->hasVFP3())
5262 return false;
5263 if (VT == MVT::f32)
5264 return ARM::getVFPf32Imm(Imm) != -1;
5265 if (VT == MVT::f64)
5266 return ARM::getVFPf64Imm(Imm) != -1;
5267 return false;
5268}