blob: 2fafb4fd008bfb73935166ee88c45da4ccfa5285 [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
Bob Wilson703af3a2010-08-13 22:43:33 +000054// This option should go away when tail calls fully work.
55static cl::opt<bool>
56EnableARMTailCalls("arm-tail-calls", cl::Hidden,
57 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
58 cl::init(false));
59
Dale Johannesenf630c712010-07-29 20:10:08 +000060// This option should go away when Machine LICM is smart enough to hoist a
61// reg-to-reg VDUP.
62static cl::opt<bool>
63EnableARMVDUPsplat("arm-vdup-splat", cl::Hidden,
64 cl::desc("Generate VDUP for integer constant splats (TEMPORARY OPTION)."),
65 cl::init(false));
66
Jim Grosbache7b52522010-04-14 22:28:31 +000067static cl::opt<bool>
68EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000069 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbache7b52522010-04-14 22:28:31 +000070 cl::init(false));
71
Evan Cheng46df4eb2010-06-16 07:35:02 +000072static cl::opt<bool>
73ARMInterworking("arm-interworking", cl::Hidden,
74 cl::desc("Enable / disable ARM interworking (for debugging only)"),
75 cl::init(true));
76
Evan Chengf6799392010-06-26 01:52:05 +000077static cl::opt<bool>
78EnableARMCodePlacement("arm-code-placement", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000079 cl::desc("Enable code placement pass for ARM"),
Evan Chengf6799392010-06-26 01:52:05 +000080 cl::init(false));
81
Owen Andersone50ed302009-08-10 22:56:29 +000082static bool CC_ARM_APCS_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);
Owen Andersone50ed302009-08-10 22:56:29 +000086static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000087 CCValAssign::LocInfo &LocInfo,
88 ISD::ArgFlagsTy &ArgFlags,
89 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000090static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000091 CCValAssign::LocInfo &LocInfo,
92 ISD::ArgFlagsTy &ArgFlags,
93 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000094static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000095 CCValAssign::LocInfo &LocInfo,
96 ISD::ArgFlagsTy &ArgFlags,
97 CCState &State);
98
Owen Andersone50ed302009-08-10 22:56:29 +000099void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
100 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000101 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000102 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000103 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
104 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000105
Owen Anderson70671842009-08-10 20:18:46 +0000106 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000107 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000108 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000109 }
110
Owen Andersone50ed302009-08-10 22:56:29 +0000111 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000112 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +0000113 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000114 if (ElemTy == MVT::i8 || ElemTy == MVT::i16)
Owen Anderson70671842009-08-10 20:18:46 +0000115 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +0000116 if (ElemTy != MVT::i32) {
117 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
118 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
119 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
120 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
121 }
Owen Anderson70671842009-08-10 20:18:46 +0000122 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
123 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +0000124 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Anton Korobeynikov8e6c2b92009-08-21 12:40:35 +0000125 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
Bob Wilsond0910c42010-04-06 22:02:24 +0000126 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
127 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000128 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000129 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
130 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
131 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000132 setLoadExtAction(ISD::SEXTLOAD, VT.getSimpleVT(), Expand);
133 setLoadExtAction(ISD::ZEXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000134 }
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000135 setLoadExtAction(ISD::EXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000136
137 // Promote all bit-wise operations.
138 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000139 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000140 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
141 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000142 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000143 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000144 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000145 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000146 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000147 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000148 }
Bob Wilson16330762009-09-16 00:17:28 +0000149
150 // Neon does not support vector divide/remainder operations.
151 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
152 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
153 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
154 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
155 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
156 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000157}
158
Owen Andersone50ed302009-08-10 22:56:29 +0000159void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000160 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000161 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000162}
163
Owen Andersone50ed302009-08-10 22:56:29 +0000164void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000165 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000166 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000167}
168
Chris Lattnerf0144122009-07-28 03:13:23 +0000169static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
170 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000171 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000172
Chris Lattner80ec2792009-08-02 00:34:36 +0000173 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000174}
175
Evan Chenga8e29892007-01-19 07:51:42 +0000176ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000177 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000178 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000179 RegInfo = TM.getRegisterInfo();
Evan Chenga8e29892007-01-19 07:51:42 +0000180
Evan Chengb1df8f22007-04-27 08:15:43 +0000181 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000182 // Uses VFP for Thumb libfuncs if available.
183 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
184 // Single-precision floating-point arithmetic.
185 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
186 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
187 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
188 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000189
Evan Chengb1df8f22007-04-27 08:15:43 +0000190 // Double-precision floating-point arithmetic.
191 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
192 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
193 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
194 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000195
Evan Chengb1df8f22007-04-27 08:15:43 +0000196 // Single-precision comparisons.
197 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
198 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
199 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
200 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
201 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
202 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
203 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
204 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000205
Evan Chengb1df8f22007-04-27 08:15:43 +0000206 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
207 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000214
Evan Chengb1df8f22007-04-27 08:15:43 +0000215 // Double-precision comparisons.
216 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
217 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
218 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
219 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
220 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
221 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
222 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
223 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000224
Evan Chengb1df8f22007-04-27 08:15:43 +0000225 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
226 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
227 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
228 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
229 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
230 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
231 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
232 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000233
Evan Chengb1df8f22007-04-27 08:15:43 +0000234 // Floating-point to integer conversions.
235 // i64 conversions are done via library routines even when generating VFP
236 // instructions, so use the same ones.
237 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
238 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
239 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
240 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000241
Evan Chengb1df8f22007-04-27 08:15:43 +0000242 // Conversions between floating types.
243 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
244 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
245
246 // Integer to floating-point conversions.
247 // i64 conversions are done via library routines even when generating VFP
248 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000249 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
250 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000251 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
252 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
253 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
254 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
255 }
Evan Chenga8e29892007-01-19 07:51:42 +0000256 }
257
Bob Wilson2f954612009-05-22 17:38:41 +0000258 // These libcalls are not available in 32-bit.
259 setLibcallName(RTLIB::SHL_I128, 0);
260 setLibcallName(RTLIB::SRL_I128, 0);
261 setLibcallName(RTLIB::SRA_I128, 0);
262
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000263 // Libcalls should use the AAPCS base standard ABI, even if hard float
264 // is in effect, as per the ARM RTABI specification, section 4.1.2.
265 if (Subtarget->isAAPCS_ABI()) {
266 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
267 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
268 CallingConv::ARM_AAPCS);
269 }
270 }
271
David Goodwinf1daf7d2009-07-08 23:10:31 +0000272 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000274 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000275 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000276 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000278 if (!Subtarget->isFPOnlySP())
279 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000280
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000282 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000283
284 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 addDRTypeForNEON(MVT::v2f32);
286 addDRTypeForNEON(MVT::v8i8);
287 addDRTypeForNEON(MVT::v4i16);
288 addDRTypeForNEON(MVT::v2i32);
289 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000290
Owen Anderson825b72b2009-08-11 20:47:22 +0000291 addQRTypeForNEON(MVT::v4f32);
292 addQRTypeForNEON(MVT::v2f64);
293 addQRTypeForNEON(MVT::v16i8);
294 addQRTypeForNEON(MVT::v8i16);
295 addQRTypeForNEON(MVT::v4i32);
296 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000297
Bob Wilson74dc72e2009-09-15 23:55:57 +0000298 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
299 // neither Neon nor VFP support any arithmetic operations on it.
300 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
301 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
302 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
303 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
304 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
305 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
306 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
307 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
308 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
309 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
310 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
311 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
312 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
313 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
314 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
315 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
316 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
317 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
318 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
319 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
320 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
321 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
322 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
323 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
324
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000325 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
326
Bob Wilson642b3292009-09-16 00:32:15 +0000327 // Neon does not support some operations on v1i64 and v2i64 types.
328 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
329 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
330 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
331 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
332
Bob Wilson5bafff32009-06-22 23:27:02 +0000333 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
334 setTargetDAGCombine(ISD::SHL);
335 setTargetDAGCombine(ISD::SRL);
336 setTargetDAGCombine(ISD::SRA);
337 setTargetDAGCombine(ISD::SIGN_EXTEND);
338 setTargetDAGCombine(ISD::ZERO_EXTEND);
339 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000340 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson5bafff32009-06-22 23:27:02 +0000341 }
342
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000343 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000344
345 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000346 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000347
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000348 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000349 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000350
Evan Chenga8e29892007-01-19 07:51:42 +0000351 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000352 if (!Subtarget->isThumb1Only()) {
353 for (unsigned im = (unsigned)ISD::PRE_INC;
354 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 setIndexedLoadAction(im, MVT::i1, Legal);
356 setIndexedLoadAction(im, MVT::i8, Legal);
357 setIndexedLoadAction(im, MVT::i16, Legal);
358 setIndexedLoadAction(im, MVT::i32, Legal);
359 setIndexedStoreAction(im, MVT::i1, Legal);
360 setIndexedStoreAction(im, MVT::i8, Legal);
361 setIndexedStoreAction(im, MVT::i16, Legal);
362 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000363 }
Evan Chenga8e29892007-01-19 07:51:42 +0000364 }
365
366 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000367 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000368 setOperationAction(ISD::MUL, MVT::i64, Expand);
369 setOperationAction(ISD::MULHU, MVT::i32, Expand);
370 setOperationAction(ISD::MULHS, MVT::i32, Expand);
371 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
372 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000373 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000374 setOperationAction(ISD::MUL, MVT::i64, Expand);
375 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000376 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000377 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000378 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000379 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000380 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000381 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 setOperationAction(ISD::SRL, MVT::i64, Custom);
383 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000384
385 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000386 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000387 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000388 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000389 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000390 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000391
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000392 // Only ARMv6 has BSWAP.
393 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000394 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000395
Evan Chenga8e29892007-01-19 07:51:42 +0000396 // These are expanded into libcalls.
Jim Grosbach29402132010-05-05 23:44:43 +0000397 if (!Subtarget->hasDivide()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000398 // v7M has a hardware divider
399 setOperationAction(ISD::SDIV, MVT::i32, Expand);
400 setOperationAction(ISD::UDIV, MVT::i32, Expand);
401 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000402 setOperationAction(ISD::SREM, MVT::i32, Expand);
403 setOperationAction(ISD::UREM, MVT::i32, Expand);
404 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
405 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000406
Owen Anderson825b72b2009-08-11 20:47:22 +0000407 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
408 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
409 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
410 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000411 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000412
Evan Chengfb3611d2010-05-11 07:26:32 +0000413 setOperationAction(ISD::TRAP, MVT::Other, Legal);
414
Evan Chenga8e29892007-01-19 07:51:42 +0000415 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::VASTART, MVT::Other, Custom);
417 setOperationAction(ISD::VAARG, MVT::Other, Expand);
418 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
419 setOperationAction(ISD::VAEND, MVT::Other, Expand);
420 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
421 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000422 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
423 // FIXME: Shouldn't need this, since no register is used, but the legalizer
424 // doesn't yet know how to not do that for SjLj.
425 setExceptionSelectorRegister(ARM::R0);
Evan Cheng3a1588a2010-04-15 22:20:34 +0000426 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000427 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
428 // the default expansion.
429 if (Subtarget->hasDataBarrier() ||
430 (Subtarget->hasV6Ops() && !Subtarget->isThumb1Only())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000431 // membarrier needs custom lowering; the rest are legal and handled
432 // normally.
433 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
434 } else {
435 // Set them all for expansion, which will force libcalls.
436 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
437 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Expand);
438 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Expand);
439 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000440 setOperationAction(ISD::ATOMIC_SWAP, MVT::i8, Expand);
441 setOperationAction(ISD::ATOMIC_SWAP, MVT::i16, Expand);
442 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000443 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i8, Expand);
444 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i16, Expand);
445 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
446 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Expand);
447 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Expand);
448 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
449 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i8, Expand);
450 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i16, Expand);
451 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
452 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i8, Expand);
453 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i16, Expand);
454 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
455 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i8, Expand);
456 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i16, Expand);
457 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
458 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i8, Expand);
459 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i16, Expand);
460 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000461 // Since the libcalls include locking, fold in the fences
462 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000463 }
464 // 64-bit versions are always libcalls (for now)
465 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000466 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000467 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Expand);
468 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Expand);
469 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Expand);
470 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Expand);
471 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Expand);
472 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000473
Eli Friedmana2c6f452010-06-26 04:36:50 +0000474 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
475 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000476 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
477 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000478 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000479 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000480
Nate Begemand1fb5832010-08-03 21:31:55 +0000481 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000482 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
483 // iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000484 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000485 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
486 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000487
488 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000489 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000490 if (Subtarget->isTargetDarwin()) {
491 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
492 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
493 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000494
Owen Anderson825b72b2009-08-11 20:47:22 +0000495 setOperationAction(ISD::SETCC, MVT::i32, Expand);
496 setOperationAction(ISD::SETCC, MVT::f32, Expand);
497 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000498 setOperationAction(ISD::SELECT, MVT::i32, Custom);
499 setOperationAction(ISD::SELECT, MVT::f32, Custom);
500 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000501 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
502 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
503 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000504
Owen Anderson825b72b2009-08-11 20:47:22 +0000505 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
506 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
507 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
508 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
509 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000510
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000511 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000512 setOperationAction(ISD::FSIN, MVT::f64, Expand);
513 setOperationAction(ISD::FSIN, MVT::f32, Expand);
514 setOperationAction(ISD::FCOS, MVT::f32, Expand);
515 setOperationAction(ISD::FCOS, MVT::f64, Expand);
516 setOperationAction(ISD::FREM, MVT::f64, Expand);
517 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000518 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000519 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
520 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000521 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000522 setOperationAction(ISD::FPOW, MVT::f64, Expand);
523 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000524
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000525 // Various VFP goodness
526 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000527 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
528 if (Subtarget->hasVFP2()) {
529 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
530 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
531 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
532 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
533 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000534 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000535 if (!Subtarget->hasFP16()) {
536 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
537 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000538 }
Evan Cheng110cf482008-04-01 01:50:16 +0000539 }
Evan Chenga8e29892007-01-19 07:51:42 +0000540
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000541 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000542 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000543 setTargetDAGCombine(ISD::ADD);
544 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000545 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000546
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000547 if (Subtarget->hasV6T2Ops())
548 setTargetDAGCombine(ISD::OR);
549
Evan Chenga8e29892007-01-19 07:51:42 +0000550 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000551
Evan Chengf7d87ee2010-05-21 00:43:17 +0000552 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
553 setSchedulingPreference(Sched::RegPressure);
554 else
555 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000556
557 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Evan Chengf6799392010-06-26 01:52:05 +0000558
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000559 // On ARM arguments smaller than 4 bytes are extended, so all arguments
560 // are at least 4 bytes aligned.
561 setMinStackArgumentAlignment(4);
562
Evan Chengf6799392010-06-26 01:52:05 +0000563 if (EnableARMCodePlacement)
564 benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000565}
566
Evan Cheng4f6b4672010-07-21 06:09:07 +0000567std::pair<const TargetRegisterClass*, uint8_t>
568ARMTargetLowering::findRepresentativeClass(EVT VT) const{
569 const TargetRegisterClass *RRC = 0;
570 uint8_t Cost = 1;
571 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000572 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000573 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000574 // Use DPR as representative register class for all floating point
575 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
576 // the cost is 1 for both f32 and f64.
577 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000578 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000579 RRC = ARM::DPRRegisterClass;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000580 break;
581 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
582 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000583 RRC = ARM::DPRRegisterClass;
584 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000585 break;
586 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000587 RRC = ARM::DPRRegisterClass;
588 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000589 break;
590 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000591 RRC = ARM::DPRRegisterClass;
592 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000593 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000594 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000595 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000596}
597
Evan Chenga8e29892007-01-19 07:51:42 +0000598const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
599 switch (Opcode) {
600 default: return 0;
601 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000602 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
603 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000604 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000605 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
606 case ARMISD::tCALL: return "ARMISD::tCALL";
607 case ARMISD::BRCOND: return "ARMISD::BRCOND";
608 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000609 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000610 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
611 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
Bill Wendling0b4aa7d2010-08-29 03:02:11 +0000612 case ARMISD::AND: return "ARMISD::AND";
Evan Chenga8e29892007-01-19 07:51:42 +0000613 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000614 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000615 case ARMISD::CMPFP: return "ARMISD::CMPFP";
616 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000617 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000618 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
619 case ARMISD::CMOV: return "ARMISD::CMOV";
620 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000621
Jim Grosbach3482c802010-01-18 19:58:49 +0000622 case ARMISD::RBIT: return "ARMISD::RBIT";
623
Bob Wilson76a312b2010-03-19 22:51:32 +0000624 case ARMISD::FTOSI: return "ARMISD::FTOSI";
625 case ARMISD::FTOUI: return "ARMISD::FTOUI";
626 case ARMISD::SITOF: return "ARMISD::SITOF";
627 case ARMISD::UITOF: return "ARMISD::UITOF";
628
Evan Chenga8e29892007-01-19 07:51:42 +0000629 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
630 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
631 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000632
Jim Grosbache5165492009-11-09 00:11:35 +0000633 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
634 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000635
Evan Chengc5942082009-10-28 06:55:03 +0000636 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
637 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
638
Dale Johannesen51e28e62010-06-03 21:09:53 +0000639 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
640
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000641 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000642
Evan Cheng86198642009-08-07 00:34:42 +0000643 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
644
Jim Grosbach3728e962009-12-10 00:11:09 +0000645 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
646 case ARMISD::SYNCBARRIER: return "ARMISD::SYNCBARRIER";
647
Bob Wilson5bafff32009-06-22 23:27:02 +0000648 case ARMISD::VCEQ: return "ARMISD::VCEQ";
649 case ARMISD::VCGE: return "ARMISD::VCGE";
650 case ARMISD::VCGEU: return "ARMISD::VCGEU";
651 case ARMISD::VCGT: return "ARMISD::VCGT";
652 case ARMISD::VCGTU: return "ARMISD::VCGTU";
653 case ARMISD::VTST: return "ARMISD::VTST";
654
655 case ARMISD::VSHL: return "ARMISD::VSHL";
656 case ARMISD::VSHRs: return "ARMISD::VSHRs";
657 case ARMISD::VSHRu: return "ARMISD::VSHRu";
658 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
659 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
660 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
661 case ARMISD::VSHRN: return "ARMISD::VSHRN";
662 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
663 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
664 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
665 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
666 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
667 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
668 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
669 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
670 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
671 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
672 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
673 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
674 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
675 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000676 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000677 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000678 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000679 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000680 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000681 case ARMISD::VREV64: return "ARMISD::VREV64";
682 case ARMISD::VREV32: return "ARMISD::VREV32";
683 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000684 case ARMISD::VZIP: return "ARMISD::VZIP";
685 case ARMISD::VUZP: return "ARMISD::VUZP";
686 case ARMISD::VTRN: return "ARMISD::VTRN";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000687 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000688 case ARMISD::FMAX: return "ARMISD::FMAX";
689 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000690 case ARMISD::BFI: return "ARMISD::BFI";
Evan Chenga8e29892007-01-19 07:51:42 +0000691 }
692}
693
Evan Cheng06b666c2010-05-15 02:18:07 +0000694/// getRegClassFor - Return the register class that should be used for the
695/// specified value type.
696TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
697 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
698 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
699 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000700 if (Subtarget->hasNEON()) {
701 if (VT == MVT::v4i64)
702 return ARM::QQPRRegisterClass;
703 else if (VT == MVT::v8i64)
704 return ARM::QQQQPRRegisterClass;
705 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000706 return TargetLowering::getRegClassFor(VT);
707}
708
Eric Christopherab695882010-07-21 22:26:11 +0000709// Create a fast isel object.
710FastISel *
711ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
712 return ARM::createFastISel(funcInfo);
713}
714
Bill Wendlingb4202b82009-07-01 18:50:55 +0000715/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000716unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Bob Wilsonb5b50572010-07-01 22:26:26 +0000717 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 1 : 2;
Bill Wendling20c568f2009-06-30 22:38:32 +0000718}
719
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000720/// getMaximalGlobalOffset - Returns the maximal possible offset which can
721/// be used for loads / stores from the global.
722unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
723 return (Subtarget->isThumb1Only() ? 127 : 4095);
724}
725
Evan Cheng1cc39842010-05-20 23:26:43 +0000726Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000727 unsigned NumVals = N->getNumValues();
728 if (!NumVals)
729 return Sched::RegPressure;
730
731 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000732 EVT VT = N->getValueType(i);
733 if (VT.isFloatingPoint() || VT.isVector())
734 return Sched::Latency;
735 }
Evan Chengc10f5432010-05-28 23:25:23 +0000736
737 if (!N->isMachineOpcode())
738 return Sched::RegPressure;
739
740 // Load are scheduled for latency even if there instruction itinerary
741 // is not available.
742 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
743 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
744 if (TID.mayLoad())
745 return Sched::Latency;
746
747 const InstrItineraryData &Itins = getTargetMachine().getInstrItineraryData();
748 if (!Itins.isEmpty() && Itins.getStageLatency(TID.getSchedClass()) > 2)
749 return Sched::Latency;
Evan Cheng1cc39842010-05-20 23:26:43 +0000750 return Sched::RegPressure;
751}
752
Evan Cheng31446872010-07-23 22:39:59 +0000753unsigned
754ARMTargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
755 MachineFunction &MF) const {
Evan Cheng31446872010-07-23 22:39:59 +0000756 switch (RC->getID()) {
757 default:
758 return 0;
759 case ARM::tGPRRegClassID:
Evan Chengac096802010-08-10 19:30:19 +0000760 return RegInfo->hasFP(MF) ? 4 : 5;
761 case ARM::GPRRegClassID: {
762 unsigned FP = RegInfo->hasFP(MF) ? 1 : 0;
763 return 10 - FP - (Subtarget->isR9Reserved() ? 1 : 0);
764 }
Evan Cheng31446872010-07-23 22:39:59 +0000765 case ARM::SPRRegClassID: // Currently not used as 'rep' register class.
766 case ARM::DPRRegClassID:
767 return 32 - 10;
768 }
769}
770
Evan Chenga8e29892007-01-19 07:51:42 +0000771//===----------------------------------------------------------------------===//
772// Lowering Code
773//===----------------------------------------------------------------------===//
774
Evan Chenga8e29892007-01-19 07:51:42 +0000775/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
776static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
777 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000778 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000779 case ISD::SETNE: return ARMCC::NE;
780 case ISD::SETEQ: return ARMCC::EQ;
781 case ISD::SETGT: return ARMCC::GT;
782 case ISD::SETGE: return ARMCC::GE;
783 case ISD::SETLT: return ARMCC::LT;
784 case ISD::SETLE: return ARMCC::LE;
785 case ISD::SETUGT: return ARMCC::HI;
786 case ISD::SETUGE: return ARMCC::HS;
787 case ISD::SETULT: return ARMCC::LO;
788 case ISD::SETULE: return ARMCC::LS;
789 }
790}
791
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000792/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
793static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000794 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000795 CondCode2 = ARMCC::AL;
796 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000797 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000798 case ISD::SETEQ:
799 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
800 case ISD::SETGT:
801 case ISD::SETOGT: CondCode = ARMCC::GT; break;
802 case ISD::SETGE:
803 case ISD::SETOGE: CondCode = ARMCC::GE; break;
804 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000805 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000806 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
807 case ISD::SETO: CondCode = ARMCC::VC; break;
808 case ISD::SETUO: CondCode = ARMCC::VS; break;
809 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
810 case ISD::SETUGT: CondCode = ARMCC::HI; break;
811 case ISD::SETUGE: CondCode = ARMCC::PL; break;
812 case ISD::SETLT:
813 case ISD::SETULT: CondCode = ARMCC::LT; break;
814 case ISD::SETLE:
815 case ISD::SETULE: CondCode = ARMCC::LE; break;
816 case ISD::SETNE:
817 case ISD::SETUNE: CondCode = ARMCC::NE; break;
818 }
Evan Chenga8e29892007-01-19 07:51:42 +0000819}
820
Bob Wilson1f595bb2009-04-17 19:07:39 +0000821//===----------------------------------------------------------------------===//
822// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000823//===----------------------------------------------------------------------===//
824
825#include "ARMGenCallingConv.inc"
826
827// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000828static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000829 CCValAssign::LocInfo &LocInfo,
830 CCState &State, bool CanFail) {
831 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
832
833 // Try to get the first register.
834 if (unsigned Reg = State.AllocateReg(RegList, 4))
835 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
836 else {
837 // For the 2nd half of a v2f64, do not fail.
838 if (CanFail)
839 return false;
840
841 // Put the whole thing on the stack.
842 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
843 State.AllocateStack(8, 4),
844 LocVT, LocInfo));
845 return true;
846 }
847
848 // Try to get the second register.
849 if (unsigned Reg = State.AllocateReg(RegList, 4))
850 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
851 else
852 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
853 State.AllocateStack(4, 4),
854 LocVT, LocInfo));
855 return true;
856}
857
Owen Andersone50ed302009-08-10 22:56:29 +0000858static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000859 CCValAssign::LocInfo &LocInfo,
860 ISD::ArgFlagsTy &ArgFlags,
861 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000862 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
863 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000864 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000865 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
866 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000867 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000868}
869
870// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000871static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000872 CCValAssign::LocInfo &LocInfo,
873 CCState &State, bool CanFail) {
874 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
875 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
Rafael Espindolabc565012010-07-21 11:38:30 +0000876 static const unsigned ShadowRegList[] = { ARM::R0, ARM::R1 };
Bob Wilson5bafff32009-06-22 23:27:02 +0000877
Rafael Espindolabc565012010-07-21 11:38:30 +0000878 unsigned Reg = State.AllocateReg(HiRegList, ShadowRegList, 2);
Bob Wilson5bafff32009-06-22 23:27:02 +0000879 if (Reg == 0) {
880 // For the 2nd half of a v2f64, do not just fail.
881 if (CanFail)
882 return false;
883
884 // Put the whole thing on the stack.
885 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
886 State.AllocateStack(8, 8),
887 LocVT, LocInfo));
888 return true;
889 }
890
891 unsigned i;
892 for (i = 0; i < 2; ++i)
893 if (HiRegList[i] == Reg)
894 break;
895
Rafael Espindolabc565012010-07-21 11:38:30 +0000896 unsigned T = State.AllocateReg(LoRegList[i]);
Chandler Carruth30d35b82010-07-22 08:02:25 +0000897 (void)T;
Rafael Espindolabc565012010-07-21 11:38:30 +0000898 assert(T == LoRegList[i] && "Could not allocate register");
899
Bob Wilson5bafff32009-06-22 23:27:02 +0000900 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
901 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
902 LocVT, LocInfo));
903 return true;
904}
905
Owen Andersone50ed302009-08-10 22:56:29 +0000906static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000907 CCValAssign::LocInfo &LocInfo,
908 ISD::ArgFlagsTy &ArgFlags,
909 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000910 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
911 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000912 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000913 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
914 return false;
915 return true; // we handled it
916}
917
Owen Andersone50ed302009-08-10 22:56:29 +0000918static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000919 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000920 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
921 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
922
Bob Wilsone65586b2009-04-17 20:40:45 +0000923 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
924 if (Reg == 0)
925 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000926
Bob Wilsone65586b2009-04-17 20:40:45 +0000927 unsigned i;
928 for (i = 0; i < 2; ++i)
929 if (HiRegList[i] == Reg)
930 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000931
Bob Wilson5bafff32009-06-22 23:27:02 +0000932 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000933 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000934 LocVT, LocInfo));
935 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000936}
937
Owen Andersone50ed302009-08-10 22:56:29 +0000938static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000939 CCValAssign::LocInfo &LocInfo,
940 ISD::ArgFlagsTy &ArgFlags,
941 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000942 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
943 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000944 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000945 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000946 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000947}
948
Owen Andersone50ed302009-08-10 22:56:29 +0000949static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000950 CCValAssign::LocInfo &LocInfo,
951 ISD::ArgFlagsTy &ArgFlags,
952 CCState &State) {
953 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
954 State);
955}
956
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000957/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
958/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000959CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000960 bool Return,
961 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000962 switch (CC) {
963 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000964 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000965 case CallingConv::C:
966 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000967 // Use target triple & subtarget features to do actual dispatch.
968 if (Subtarget->isAAPCS_ABI()) {
969 if (Subtarget->hasVFP2() &&
970 FloatABIType == FloatABI::Hard && !isVarArg)
971 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
972 else
973 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
974 } else
975 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000976 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000977 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000978 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000979 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000980 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000981 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000982 }
983}
984
Dan Gohman98ca4f22009-08-05 01:29:28 +0000985/// LowerCallResult - Lower the result values of a call into the
986/// appropriate copies out of appropriate physical registers.
987SDValue
988ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000989 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000990 const SmallVectorImpl<ISD::InputArg> &Ins,
991 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000992 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000993
Bob Wilson1f595bb2009-04-17 19:07:39 +0000994 // Assign locations to each value returned by this call.
995 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000996 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000997 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000998 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000999 CCAssignFnForNode(CallConv, /* Return*/ true,
1000 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001001
1002 // Copy all of the result registers out of their specified physreg.
1003 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1004 CCValAssign VA = RVLocs[i];
1005
Bob Wilson80915242009-04-25 00:33:20 +00001006 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001007 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001008 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001009 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001010 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001011 Chain = Lo.getValue(1);
1012 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001013 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001014 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001015 InFlag);
1016 Chain = Hi.getValue(1);
1017 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001018 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001019
Owen Anderson825b72b2009-08-11 20:47:22 +00001020 if (VA.getLocVT() == MVT::v2f64) {
1021 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1022 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1023 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001024
1025 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001026 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001027 Chain = Lo.getValue(1);
1028 InFlag = Lo.getValue(2);
1029 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001030 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001031 Chain = Hi.getValue(1);
1032 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001033 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001034 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1035 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001036 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001037 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001038 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1039 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001040 Chain = Val.getValue(1);
1041 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001042 }
Bob Wilson80915242009-04-25 00:33:20 +00001043
1044 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001045 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001046 case CCValAssign::Full: break;
1047 case CCValAssign::BCvt:
1048 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
1049 break;
1050 }
1051
Dan Gohman98ca4f22009-08-05 01:29:28 +00001052 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001053 }
1054
Dan Gohman98ca4f22009-08-05 01:29:28 +00001055 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001056}
1057
1058/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1059/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +00001060/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +00001061/// a byval function parameter.
1062/// Sometimes what we are copying is the end of a larger object, the part that
1063/// does not fit in registers.
1064static SDValue
1065CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
1066 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1067 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001068 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001069 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001070 /*isVolatile=*/false, /*AlwaysInline=*/false,
1071 NULL, 0, NULL, 0);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001072}
1073
Bob Wilsondee46d72009-04-17 20:35:10 +00001074/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001075SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001076ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1077 SDValue StackPtr, SDValue Arg,
1078 DebugLoc dl, SelectionDAG &DAG,
1079 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001080 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001081 unsigned LocMemOffset = VA.getLocMemOffset();
1082 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1083 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
1084 if (Flags.isByVal()) {
1085 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
1086 }
1087 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene1b58cab2010-02-15 16:55:24 +00001088 PseudoSourceValue::getStack(), LocMemOffset,
1089 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001090}
1091
Dan Gohman98ca4f22009-08-05 01:29:28 +00001092void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001093 SDValue Chain, SDValue &Arg,
1094 RegsToPassVector &RegsToPass,
1095 CCValAssign &VA, CCValAssign &NextVA,
1096 SDValue &StackPtr,
1097 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001098 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001099
Jim Grosbache5165492009-11-09 00:11:35 +00001100 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001101 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001102 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1103
1104 if (NextVA.isRegLoc())
1105 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1106 else {
1107 assert(NextVA.isMemLoc());
1108 if (StackPtr.getNode() == 0)
1109 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1110
Dan Gohman98ca4f22009-08-05 01:29:28 +00001111 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1112 dl, DAG, NextVA,
1113 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001114 }
1115}
1116
Dan Gohman98ca4f22009-08-05 01:29:28 +00001117/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001118/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1119/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001120SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001121ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001122 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001123 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001124 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001125 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001126 const SmallVectorImpl<ISD::InputArg> &Ins,
1127 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001128 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001129 MachineFunction &MF = DAG.getMachineFunction();
1130 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1131 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001132 // Temporarily disable tail calls so things don't break.
1133 if (!EnableARMTailCalls)
1134 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001135 if (isTailCall) {
1136 // Check if it's really possible to do a tail call.
1137 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1138 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001139 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001140 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1141 // detected sibcalls.
1142 if (isTailCall) {
1143 ++NumTailCalls;
1144 IsSibCall = true;
1145 }
1146 }
Evan Chenga8e29892007-01-19 07:51:42 +00001147
Bob Wilson1f595bb2009-04-17 19:07:39 +00001148 // Analyze operands of the call, assigning locations to each operand.
1149 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001150 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1151 *DAG.getContext());
1152 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001153 CCAssignFnForNode(CallConv, /* Return*/ false,
1154 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001155
Bob Wilson1f595bb2009-04-17 19:07:39 +00001156 // Get a count of how many bytes are to be pushed on the stack.
1157 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001158
Dale Johannesen51e28e62010-06-03 21:09:53 +00001159 // For tail calls, memory operands are available in our caller's stack.
1160 if (IsSibCall)
1161 NumBytes = 0;
1162
Evan Chenga8e29892007-01-19 07:51:42 +00001163 // Adjust the stack pointer for the new arguments...
1164 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001165 if (!IsSibCall)
1166 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001167
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001168 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001169
Bob Wilson5bafff32009-06-22 23:27:02 +00001170 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001171 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001172
Bob Wilson1f595bb2009-04-17 19:07:39 +00001173 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001174 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001175 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1176 i != e;
1177 ++i, ++realArgIdx) {
1178 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001179 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001180 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +00001181
Bob Wilson1f595bb2009-04-17 19:07:39 +00001182 // Promote the value if needed.
1183 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001184 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001185 case CCValAssign::Full: break;
1186 case CCValAssign::SExt:
1187 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1188 break;
1189 case CCValAssign::ZExt:
1190 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1191 break;
1192 case CCValAssign::AExt:
1193 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1194 break;
1195 case CCValAssign::BCvt:
1196 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1197 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001198 }
1199
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001200 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001201 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001202 if (VA.getLocVT() == MVT::v2f64) {
1203 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1204 DAG.getConstant(0, MVT::i32));
1205 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1206 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001207
Dan Gohman98ca4f22009-08-05 01:29:28 +00001208 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001209 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1210
1211 VA = ArgLocs[++i]; // skip ahead to next loc
1212 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001213 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001214 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1215 } else {
1216 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001217
Dan Gohman98ca4f22009-08-05 01:29:28 +00001218 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1219 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001220 }
1221 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001222 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001223 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001224 }
1225 } else if (VA.isRegLoc()) {
1226 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001227 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001228 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001229
Dan Gohman98ca4f22009-08-05 01:29:28 +00001230 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1231 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001232 }
Evan Chenga8e29892007-01-19 07:51:42 +00001233 }
1234
1235 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001236 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001237 &MemOpChains[0], MemOpChains.size());
1238
1239 // Build a sequence of copy-to-reg nodes chained together with token chain
1240 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001241 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001242 // Tail call byval lowering might overwrite argument registers so in case of
1243 // tail call optimization the copies to registers are lowered later.
1244 if (!isTailCall)
1245 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1246 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1247 RegsToPass[i].second, InFlag);
1248 InFlag = Chain.getValue(1);
1249 }
Evan Chenga8e29892007-01-19 07:51:42 +00001250
Dale Johannesen51e28e62010-06-03 21:09:53 +00001251 // For tail calls lower the arguments to the 'real' stack slot.
1252 if (isTailCall) {
1253 // Force all the incoming stack arguments to be loaded from the stack
1254 // before any new outgoing arguments are stored to the stack, because the
1255 // outgoing stack slots may alias the incoming argument stack slots, and
1256 // the alias isn't otherwise explicit. This is slightly more conservative
1257 // than necessary, because it means that each store effectively depends
1258 // on every argument instead of just those arguments it would clobber.
1259
1260 // Do not flag preceeding copytoreg stuff together with the following stuff.
1261 InFlag = SDValue();
1262 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1263 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1264 RegsToPass[i].second, InFlag);
1265 InFlag = Chain.getValue(1);
1266 }
1267 InFlag =SDValue();
1268 }
1269
Bill Wendling056292f2008-09-16 21:48:12 +00001270 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1271 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1272 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001273 bool isDirect = false;
1274 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001275 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001276 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001277
1278 if (EnableARMLongCalls) {
1279 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1280 && "long-calls with non-static relocation model!");
1281 // Handle a global address or an external symbol. If it's not one of
1282 // those, the target's already in a register, so we don't need to do
1283 // anything extra.
1284 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001285 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001286 // Create a constant pool entry for the callee address
1287 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1288 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1289 ARMPCLabelIndex,
1290 ARMCP::CPValue, 0);
1291 // Get the address of the callee into a register
1292 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1293 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1294 Callee = DAG.getLoad(getPointerTy(), dl,
1295 DAG.getEntryNode(), CPAddr,
1296 PseudoSourceValue::getConstantPool(), 0,
1297 false, false, 0);
1298 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1299 const char *Sym = S->getSymbol();
1300
1301 // Create a constant pool entry for the callee address
1302 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1303 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1304 Sym, ARMPCLabelIndex, 0);
1305 // Get the address of the callee into a register
1306 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1307 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1308 Callee = DAG.getLoad(getPointerTy(), dl,
1309 DAG.getEntryNode(), CPAddr,
1310 PseudoSourceValue::getConstantPool(), 0,
1311 false, false, 0);
1312 }
1313 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001314 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001315 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001316 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001317 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001318 getTargetMachine().getRelocationModel() != Reloc::Static;
1319 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001320 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001321 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001322 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001323 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001324 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001325 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001326 ARMPCLabelIndex,
1327 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001328 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001329 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001330 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001331 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001332 PseudoSourceValue::getConstantPool(), 0,
1333 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001334 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001335 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001336 getPointerTy(), Callee, PICLabel);
Jim Grosbache7b52522010-04-14 22:28:31 +00001337 } else
Devang Patel0d881da2010-07-06 22:08:15 +00001338 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001339 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001340 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001341 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001342 getTargetMachine().getRelocationModel() != Reloc::Static;
1343 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001344 // tBX takes a register source operand.
1345 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001346 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001347 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001348 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001349 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001350 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001351 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001352 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001353 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001354 PseudoSourceValue::getConstantPool(), 0,
1355 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001356 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001357 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001358 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001359 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001360 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001361 }
1362
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001363 // FIXME: handle tail calls differently.
1364 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001365 if (Subtarget->isThumb()) {
1366 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001367 CallOpc = ARMISD::CALL_NOLINK;
1368 else
1369 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1370 } else {
1371 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001372 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1373 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001374 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001375
Dan Gohman475871a2008-07-27 21:46:04 +00001376 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001377 Ops.push_back(Chain);
1378 Ops.push_back(Callee);
1379
1380 // Add argument registers to the end of the list so that they are known live
1381 // into the call.
1382 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1383 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1384 RegsToPass[i].second.getValueType()));
1385
Gabor Greifba36cb52008-08-28 21:40:38 +00001386 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001387 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001388
1389 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001390 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001391 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001392
Duncan Sands4bdcb612008-07-02 17:40:58 +00001393 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001394 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001395 InFlag = Chain.getValue(1);
1396
Chris Lattnere563bbc2008-10-11 22:08:30 +00001397 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1398 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001399 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001400 InFlag = Chain.getValue(1);
1401
Bob Wilson1f595bb2009-04-17 19:07:39 +00001402 // Handle result values, copying them out of physregs into vregs that we
1403 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001404 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1405 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001406}
1407
Dale Johannesen51e28e62010-06-03 21:09:53 +00001408/// MatchingStackOffset - Return true if the given stack call argument is
1409/// already available in the same position (relatively) of the caller's
1410/// incoming argument stack.
1411static
1412bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1413 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1414 const ARMInstrInfo *TII) {
1415 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1416 int FI = INT_MAX;
1417 if (Arg.getOpcode() == ISD::CopyFromReg) {
1418 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
1419 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
1420 return false;
1421 MachineInstr *Def = MRI->getVRegDef(VR);
1422 if (!Def)
1423 return false;
1424 if (!Flags.isByVal()) {
1425 if (!TII->isLoadFromStackSlot(Def, FI))
1426 return false;
1427 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001428 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001429 }
1430 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1431 if (Flags.isByVal())
1432 // ByVal argument is passed in as a pointer but it's now being
1433 // dereferenced. e.g.
1434 // define @foo(%struct.X* %A) {
1435 // tail call @bar(%struct.X* byval %A)
1436 // }
1437 return false;
1438 SDValue Ptr = Ld->getBasePtr();
1439 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1440 if (!FINode)
1441 return false;
1442 FI = FINode->getIndex();
1443 } else
1444 return false;
1445
1446 assert(FI != INT_MAX);
1447 if (!MFI->isFixedObjectIndex(FI))
1448 return false;
1449 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1450}
1451
1452/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1453/// for tail call optimization. Targets which want to do tail call
1454/// optimization should implement this function.
1455bool
1456ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1457 CallingConv::ID CalleeCC,
1458 bool isVarArg,
1459 bool isCalleeStructRet,
1460 bool isCallerStructRet,
1461 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001462 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001463 const SmallVectorImpl<ISD::InputArg> &Ins,
1464 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001465 const Function *CallerF = DAG.getMachineFunction().getFunction();
1466 CallingConv::ID CallerCC = CallerF->getCallingConv();
1467 bool CCMatch = CallerCC == CalleeCC;
1468
1469 // Look for obvious safe cases to perform tail call optimization that do not
1470 // require ABI changes. This is what gcc calls sibcall.
1471
Jim Grosbach7616b642010-06-16 23:45:49 +00001472 // Do not sibcall optimize vararg calls unless the call site is not passing
1473 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001474 if (isVarArg && !Outs.empty())
1475 return false;
1476
1477 // Also avoid sibcall optimization if either caller or callee uses struct
1478 // return semantics.
1479 if (isCalleeStructRet || isCallerStructRet)
1480 return false;
1481
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001482 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Evan Cheng0110ac62010-06-19 01:01:32 +00001483 // emitEpilogue is not ready for them.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001484 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1485 // LR. This means if we need to reload LR, it takes an extra instructions,
1486 // which outweighs the value of the tail call; but here we don't know yet
1487 // whether LR is going to be used. Probably the right approach is to
1488 // generate the tail call here and turn it back into CALL/RET in
1489 // emitEpilogue if LR is used.
Evan Cheng0110ac62010-06-19 01:01:32 +00001490 if (Subtarget->isThumb1Only())
1491 return false;
1492
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001493 // For the moment, we can only do this to functions defined in this
1494 // compilation, or to indirect calls. A Thumb B to an ARM function,
1495 // or vice versa, is not easily fixed up in the linker unlike BL.
1496 // (We could do this by loading the address of the callee into a register;
1497 // that is an extra instruction over the direct call and burns a register
1498 // as well, so is not likely to be a win.)
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001499
1500 // It might be safe to remove this restriction on non-Darwin.
1501
1502 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1503 // but we need to make sure there are enough registers; the only valid
1504 // registers are the 4 used for parameters. We don't currently do this
1505 // case.
Evan Cheng0110ac62010-06-19 01:01:32 +00001506 if (isa<ExternalSymbolSDNode>(Callee))
1507 return false;
1508
1509 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001510 const GlobalValue *GV = G->getGlobal();
1511 if (GV->isDeclaration() || GV->isWeakForLinker())
Evan Cheng0110ac62010-06-19 01:01:32 +00001512 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001513 }
1514
Dale Johannesen51e28e62010-06-03 21:09:53 +00001515 // If the calling conventions do not match, then we'd better make sure the
1516 // results are returned in the same way as what the caller expects.
1517 if (!CCMatch) {
1518 SmallVector<CCValAssign, 16> RVLocs1;
1519 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
1520 RVLocs1, *DAG.getContext());
1521 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1522
1523 SmallVector<CCValAssign, 16> RVLocs2;
1524 CCState CCInfo2(CallerCC, false, getTargetMachine(),
1525 RVLocs2, *DAG.getContext());
1526 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1527
1528 if (RVLocs1.size() != RVLocs2.size())
1529 return false;
1530 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1531 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1532 return false;
1533 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1534 return false;
1535 if (RVLocs1[i].isRegLoc()) {
1536 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1537 return false;
1538 } else {
1539 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1540 return false;
1541 }
1542 }
1543 }
1544
1545 // If the callee takes no arguments then go on to check the results of the
1546 // call.
1547 if (!Outs.empty()) {
1548 // Check if stack adjustment is needed. For now, do not do this if any
1549 // argument is passed on the stack.
1550 SmallVector<CCValAssign, 16> ArgLocs;
1551 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
1552 ArgLocs, *DAG.getContext());
1553 CCInfo.AnalyzeCallOperands(Outs,
1554 CCAssignFnForNode(CalleeCC, false, isVarArg));
1555 if (CCInfo.getNextStackOffset()) {
1556 MachineFunction &MF = DAG.getMachineFunction();
1557
1558 // Check if the arguments are already laid out in the right way as
1559 // the caller's fixed stack objects.
1560 MachineFrameInfo *MFI = MF.getFrameInfo();
1561 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1562 const ARMInstrInfo *TII =
1563 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001564 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1565 i != e;
1566 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001567 CCValAssign &VA = ArgLocs[i];
1568 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001569 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001570 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001571 if (VA.getLocInfo() == CCValAssign::Indirect)
1572 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001573 if (VA.needsCustom()) {
1574 // f64 and vector types are split into multiple registers or
1575 // register/stack-slot combinations. The types will not match
1576 // the registers; give up on memory f64 refs until we figure
1577 // out what to do about this.
1578 if (!VA.isRegLoc())
1579 return false;
1580 if (!ArgLocs[++i].isRegLoc())
1581 return false;
1582 if (RegVT == MVT::v2f64) {
1583 if (!ArgLocs[++i].isRegLoc())
1584 return false;
1585 if (!ArgLocs[++i].isRegLoc())
1586 return false;
1587 }
1588 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001589 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1590 MFI, MRI, TII))
1591 return false;
1592 }
1593 }
1594 }
1595 }
1596
1597 return true;
1598}
1599
Dan Gohman98ca4f22009-08-05 01:29:28 +00001600SDValue
1601ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001602 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001603 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001604 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001605 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001606
Bob Wilsondee46d72009-04-17 20:35:10 +00001607 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001608 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001609
Bob Wilsondee46d72009-04-17 20:35:10 +00001610 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001611 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1612 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001613
Dan Gohman98ca4f22009-08-05 01:29:28 +00001614 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001615 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1616 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001617
1618 // If this is the first return lowered for this function, add
1619 // the regs to the liveout set for the function.
1620 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1621 for (unsigned i = 0; i != RVLocs.size(); ++i)
1622 if (RVLocs[i].isRegLoc())
1623 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001624 }
1625
Bob Wilson1f595bb2009-04-17 19:07:39 +00001626 SDValue Flag;
1627
1628 // Copy the result values into the output registers.
1629 for (unsigned i = 0, realRVLocIdx = 0;
1630 i != RVLocs.size();
1631 ++i, ++realRVLocIdx) {
1632 CCValAssign &VA = RVLocs[i];
1633 assert(VA.isRegLoc() && "Can only return in registers!");
1634
Dan Gohmanc9403652010-07-07 15:54:55 +00001635 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001636
1637 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001638 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001639 case CCValAssign::Full: break;
1640 case CCValAssign::BCvt:
1641 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1642 break;
1643 }
1644
Bob Wilson1f595bb2009-04-17 19:07:39 +00001645 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001646 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001647 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001648 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1649 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001650 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001651 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001652
1653 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1654 Flag = Chain.getValue(1);
1655 VA = RVLocs[++i]; // skip ahead to next loc
1656 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1657 HalfGPRs.getValue(1), Flag);
1658 Flag = Chain.getValue(1);
1659 VA = RVLocs[++i]; // skip ahead to next loc
1660
1661 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001662 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1663 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001664 }
1665 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1666 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001667 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001668 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001669 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001670 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001671 VA = RVLocs[++i]; // skip ahead to next loc
1672 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1673 Flag);
1674 } else
1675 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1676
Bob Wilsondee46d72009-04-17 20:35:10 +00001677 // Guarantee that all emitted copies are
1678 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001679 Flag = Chain.getValue(1);
1680 }
1681
1682 SDValue result;
1683 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001684 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001685 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001686 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001687
1688 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001689}
1690
Bob Wilsonb62d2572009-11-03 00:02:05 +00001691// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1692// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1693// one of the above mentioned nodes. It has to be wrapped because otherwise
1694// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1695// be used to form addressing mode. These wrapped nodes will be selected
1696// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001697static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001698 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001699 // FIXME there is no actual debug info here
1700 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001701 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001702 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001703 if (CP->isMachineConstantPoolEntry())
1704 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1705 CP->getAlignment());
1706 else
1707 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1708 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001709 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001710}
1711
Jim Grosbache1102ca2010-07-19 17:20:38 +00001712unsigned ARMTargetLowering::getJumpTableEncoding() const {
1713 return MachineJumpTableInfo::EK_Inline;
1714}
1715
Dan Gohmand858e902010-04-17 15:26:15 +00001716SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1717 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001718 MachineFunction &MF = DAG.getMachineFunction();
1719 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1720 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001721 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001722 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001723 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001724 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1725 SDValue CPAddr;
1726 if (RelocM == Reloc::Static) {
1727 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1728 } else {
1729 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001730 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001731 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1732 ARMCP::CPBlockAddress,
1733 PCAdj);
1734 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1735 }
1736 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1737 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001738 PseudoSourceValue::getConstantPool(), 0,
1739 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001740 if (RelocM == Reloc::Static)
1741 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001742 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001743 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001744}
1745
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001746// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001747SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001748ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001749 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001750 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001751 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001752 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001753 MachineFunction &MF = DAG.getMachineFunction();
1754 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1755 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001756 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001757 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001758 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001759 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001760 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001761 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
David Greene1b58cab2010-02-15 16:55:24 +00001762 PseudoSourceValue::getConstantPool(), 0,
1763 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001764 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001765
Evan Chenge7e0d622009-11-06 22:24:13 +00001766 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001767 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001768
1769 // call __tls_get_addr.
1770 ArgListTy Args;
1771 ArgListEntry Entry;
1772 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001773 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001774 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001775 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001776 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001777 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1778 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001779 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001780 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001781 return CallResult.first;
1782}
1783
1784// Lower ISD::GlobalTLSAddress using the "initial exec" or
1785// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001786SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001787ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001788 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001789 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001790 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001791 SDValue Offset;
1792 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001793 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001794 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001795 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001796
Chris Lattner4fb63d02009-07-15 04:12:33 +00001797 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001798 MachineFunction &MF = DAG.getMachineFunction();
1799 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1800 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1801 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001802 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1803 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001804 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001805 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001806 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001807 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001808 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001809 PseudoSourceValue::getConstantPool(), 0,
1810 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001811 Chain = Offset.getValue(1);
1812
Evan Chenge7e0d622009-11-06 22:24:13 +00001813 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001814 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001815
Evan Cheng9eda6892009-10-31 03:39:36 +00001816 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001817 PseudoSourceValue::getConstantPool(), 0,
1818 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001819 } else {
1820 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001821 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001822 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001823 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001824 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001825 PseudoSourceValue::getConstantPool(), 0,
1826 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001827 }
1828
1829 // The address of the thread local variable is the add of the thread
1830 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001831 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001832}
1833
Dan Gohman475871a2008-07-27 21:46:04 +00001834SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001835ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001836 // TODO: implement the "local dynamic" model
1837 assert(Subtarget->isTargetELF() &&
1838 "TLS not implemented for non-ELF targets");
1839 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1840 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1841 // otherwise use the "Local Exec" TLS Model
1842 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1843 return LowerToTLSGeneralDynamicModel(GA, DAG);
1844 else
1845 return LowerToTLSExecModels(GA, DAG);
1846}
1847
Dan Gohman475871a2008-07-27 21:46:04 +00001848SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001849 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001850 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001851 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001852 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001853 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1854 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001855 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001856 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001857 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001858 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001859 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001860 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001861 CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001862 PseudoSourceValue::getConstantPool(), 0,
1863 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001864 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001865 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001866 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001867 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001868 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001869 PseudoSourceValue::getGOT(), 0,
1870 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001871 return Result;
1872 } else {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001873 // If we have T2 ops, we can materialize the address directly via movt/movw
1874 // pair. This is always cheaper.
1875 if (Subtarget->useMovt()) {
1876 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
Devang Patel0d881da2010-07-06 22:08:15 +00001877 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001878 } else {
1879 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1880 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1881 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001882 PseudoSourceValue::getConstantPool(), 0,
1883 false, false, 0);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001884 }
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001885 }
1886}
1887
Dan Gohman475871a2008-07-27 21:46:04 +00001888SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001889 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001890 MachineFunction &MF = DAG.getMachineFunction();
1891 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1892 unsigned ARMPCLabelIndex = 0;
Owen Andersone50ed302009-08-10 22:56:29 +00001893 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001894 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001895 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001896 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001897 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001898 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001899 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001900 else {
Evan Chenge7e0d622009-11-06 22:24:13 +00001901 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001902 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1903 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001904 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001905 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001906 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001907 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001908
Evan Cheng9eda6892009-10-31 03:39:36 +00001909 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001910 PseudoSourceValue::getConstantPool(), 0,
1911 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001912 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001913
1914 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001915 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001916 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001917 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001918
Evan Cheng63476a82009-09-03 07:04:02 +00001919 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Evan Cheng9eda6892009-10-31 03:39:36 +00001920 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001921 PseudoSourceValue::getGOT(), 0,
1922 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001923
1924 return Result;
1925}
1926
Dan Gohman475871a2008-07-27 21:46:04 +00001927SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001928 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001929 assert(Subtarget->isTargetELF() &&
1930 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00001931 MachineFunction &MF = DAG.getMachineFunction();
1932 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1933 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Andersone50ed302009-08-10 22:56:29 +00001934 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001935 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001936 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001937 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1938 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001939 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001940 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001941 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001942 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001943 PseudoSourceValue::getConstantPool(), 0,
1944 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001945 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001946 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001947}
1948
Jim Grosbach0e0da732009-05-12 23:59:14 +00001949SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00001950ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
1951 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00001952 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00001953 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
1954 Op.getOperand(1), Val);
1955}
1956
1957SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00001958ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
1959 DebugLoc dl = Op.getDebugLoc();
1960 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
1961 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
1962}
1963
1964SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00001965ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00001966 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001967 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001968 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001969 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001970 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001971 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001972 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001973 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1974 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001975 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001976 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00001977 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1978 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001979 EVT PtrVT = getPointerTy();
1980 DebugLoc dl = Op.getDebugLoc();
1981 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1982 SDValue CPAddr;
1983 unsigned PCAdj = (RelocM != Reloc::PIC_)
1984 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001985 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001986 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1987 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001988 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001989 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001990 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00001991 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001992 PseudoSourceValue::getConstantPool(), 0,
1993 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001994
1995 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001996 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001997 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1998 }
1999 return Result;
2000 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002001 }
2002}
2003
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002004static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002005 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002006 DebugLoc dl = Op.getDebugLoc();
2007 SDValue Op5 = Op.getOperand(5);
Jim Grosbach3728e962009-12-10 00:11:09 +00002008 unsigned isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue();
Evan Cheng11db0682010-08-11 06:22:01 +00002009 // Some subtargets which have dmb and dsb instructions can handle barriers
2010 // directly. Some ARMv6 cpus can support them with the help of mcr
2011 // instruction. Thumb1 and pre-v6 ARM mode use a libcall instead and should
Jim Grosbachc73993b2010-06-17 01:37:00 +00002012 // never get here.
2013 unsigned Opc = isDeviceBarrier ? ARMISD::SYNCBARRIER : ARMISD::MEMBARRIER;
Evan Cheng11db0682010-08-11 06:22:01 +00002014 if (Subtarget->hasDataBarrier())
Jim Grosbachc73993b2010-06-17 01:37:00 +00002015 return DAG.getNode(Opc, dl, MVT::Other, Op.getOperand(0));
Evan Cheng11db0682010-08-11 06:22:01 +00002016 else {
2017 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb1Only() &&
2018 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Jim Grosbachc73993b2010-06-17 01:37:00 +00002019 return DAG.getNode(Opc, dl, MVT::Other, Op.getOperand(0),
2020 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002021 }
Jim Grosbach3728e962009-12-10 00:11:09 +00002022}
2023
Dan Gohman1e93df62010-04-17 14:41:14 +00002024static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2025 MachineFunction &MF = DAG.getMachineFunction();
2026 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2027
Evan Chenga8e29892007-01-19 07:51:42 +00002028 // vastart just stores the address of the VarArgsFrameIndex slot into the
2029 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002030 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002031 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002032 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002033 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
David Greene1b58cab2010-02-15 16:55:24 +00002034 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
2035 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002036}
2037
Dan Gohman475871a2008-07-27 21:46:04 +00002038SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002039ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2040 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002041 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002042 MachineFunction &MF = DAG.getMachineFunction();
2043 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2044
2045 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002046 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002047 RC = ARM::tGPRRegisterClass;
2048 else
2049 RC = ARM::GPRRegisterClass;
2050
2051 // Transform the arguments stored in physical registers into virtual ones.
Evan Cheng2457f2c2010-05-22 01:47:14 +00002052 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002053 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002054
2055 SDValue ArgValue2;
2056 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002057 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002058 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002059
2060 // Create load node to retrieve arguments from the stack.
2061 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002062 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00002063 PseudoSourceValue::getFixedStack(FI), 0,
2064 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002065 } else {
2066 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002067 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002068 }
2069
Jim Grosbache5165492009-11-09 00:11:35 +00002070 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002071}
2072
2073SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002074ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002075 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002076 const SmallVectorImpl<ISD::InputArg>
2077 &Ins,
2078 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002079 SmallVectorImpl<SDValue> &InVals)
2080 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002081
Bob Wilson1f595bb2009-04-17 19:07:39 +00002082 MachineFunction &MF = DAG.getMachineFunction();
2083 MachineFrameInfo *MFI = MF.getFrameInfo();
2084
Bob Wilson1f595bb2009-04-17 19:07:39 +00002085 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2086
2087 // Assign locations to all of the incoming arguments.
2088 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002089 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
2090 *DAG.getContext());
2091 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002092 CCAssignFnForNode(CallConv, /* Return*/ false,
2093 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002094
2095 SmallVector<SDValue, 16> ArgValues;
2096
2097 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2098 CCValAssign &VA = ArgLocs[i];
2099
Bob Wilsondee46d72009-04-17 20:35:10 +00002100 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002101 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002102 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002103
Bob Wilson5bafff32009-06-22 23:27:02 +00002104 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002105 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002106 // f64 and vector types are split up into multiple registers or
2107 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002108 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002109 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002110 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002111 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002112 SDValue ArgValue2;
2113 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002114 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002115 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2116 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
2117 PseudoSourceValue::getFixedStack(FI), 0,
2118 false, false, 0);
2119 } else {
2120 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2121 Chain, DAG, dl);
2122 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002123 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2124 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002125 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002126 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002127 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2128 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002129 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002130
Bob Wilson5bafff32009-06-22 23:27:02 +00002131 } else {
2132 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002133
Owen Anderson825b72b2009-08-11 20:47:22 +00002134 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002135 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002136 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002137 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002138 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002139 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002140 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002141 RC = (AFI->isThumb1OnlyFunction() ?
2142 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002143 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002144 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002145
2146 // Transform the arguments in physical registers into virtual ones.
2147 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002148 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002149 }
2150
2151 // If this is an 8 or 16-bit value, it is really passed promoted
2152 // to 32 bits. Insert an assert[sz]ext to capture this, then
2153 // truncate to the right size.
2154 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002155 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002156 case CCValAssign::Full: break;
2157 case CCValAssign::BCvt:
2158 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
2159 break;
2160 case CCValAssign::SExt:
2161 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2162 DAG.getValueType(VA.getValVT()));
2163 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2164 break;
2165 case CCValAssign::ZExt:
2166 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2167 DAG.getValueType(VA.getValVT()));
2168 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2169 break;
2170 }
2171
Dan Gohman98ca4f22009-08-05 01:29:28 +00002172 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002173
2174 } else { // VA.isRegLoc()
2175
2176 // sanity check
2177 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002178 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002179
2180 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002181 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002182
Bob Wilsondee46d72009-04-17 20:35:10 +00002183 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002184 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002185 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00002186 PseudoSourceValue::getFixedStack(FI), 0,
2187 false, false, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002188 }
2189 }
2190
2191 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00002192 if (isVarArg) {
2193 static const unsigned GPRArgRegs[] = {
2194 ARM::R0, ARM::R1, ARM::R2, ARM::R3
2195 };
2196
Bob Wilsondee46d72009-04-17 20:35:10 +00002197 unsigned NumGPRs = CCInfo.getFirstUnallocated
2198 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002199
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00002200 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
2201 unsigned VARegSize = (4 - NumGPRs) * 4;
2202 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00002203 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00002204 if (VARegSaveSize) {
2205 // If this function is vararg, store any remaining integer argument regs
2206 // to their spots on the stack so that they may be loaded by deferencing
2207 // the result of va_next.
2208 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00002209 AFI->setVarArgsFrameIndex(
2210 MFI->CreateFixedObject(VARegSaveSize,
2211 ArgOffset + VARegSaveSize - VARegSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002212 true));
Dan Gohman1e93df62010-04-17 14:41:14 +00002213 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2214 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00002215
Dan Gohman475871a2008-07-27 21:46:04 +00002216 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00002217 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002218 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002219 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00002220 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002221 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00002222 RC = ARM::GPRRegisterClass;
2223
Bob Wilson998e1252009-04-20 18:36:57 +00002224 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002225 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00002226 SDValue Store =
2227 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Jim Grosbach18f30e62010-06-02 21:53:11 +00002228 PseudoSourceValue::getFixedStack(AFI->getVarArgsFrameIndex()),
2229 0, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002230 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002231 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00002232 DAG.getConstant(4, getPointerTy()));
2233 }
2234 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002235 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002236 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00002237 } else
2238 // This will point to the next argument passed via stack.
Evan Chenged2ae132010-07-03 00:40:23 +00002239 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
Evan Chenga8e29892007-01-19 07:51:42 +00002240 }
2241
Dan Gohman98ca4f22009-08-05 01:29:28 +00002242 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002243}
2244
2245/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002246static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002247 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002248 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002249 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002250 // Maybe this has already been legalized into the constant pool?
2251 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002252 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002253 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002254 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002255 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002256 }
2257 }
2258 return false;
2259}
2260
Evan Chenga8e29892007-01-19 07:51:42 +00002261/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2262/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002263SDValue
2264ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002265 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002266 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002267 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002268 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002269 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002270 // Constant does not fit, try adjusting it by one?
2271 switch (CC) {
2272 default: break;
2273 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002274 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002275 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002276 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002277 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002278 }
2279 break;
2280 case ISD::SETULT:
2281 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002282 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002283 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002284 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002285 }
2286 break;
2287 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002288 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002289 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002290 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002291 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002292 }
2293 break;
2294 case ISD::SETULE:
2295 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002296 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002297 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002298 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002299 }
2300 break;
2301 }
2302 }
2303 }
2304
2305 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002306 ARMISD::NodeType CompareType;
2307 switch (CondCode) {
2308 default:
2309 CompareType = ARMISD::CMP;
2310 break;
2311 case ARMCC::EQ:
2312 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002313 // Uses only Z Flag
2314 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002315 break;
2316 }
Evan Cheng218977b2010-07-13 19:27:42 +00002317 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002318 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002319}
2320
2321/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002322SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002323ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002324 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002325 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002326 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00002327 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002328 else
Owen Anderson825b72b2009-08-11 20:47:22 +00002329 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
2330 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002331}
2332
Bill Wendlingde2b1512010-08-11 08:43:16 +00002333SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2334 SDValue Cond = Op.getOperand(0);
2335 SDValue SelectTrue = Op.getOperand(1);
2336 SDValue SelectFalse = Op.getOperand(2);
2337 DebugLoc dl = Op.getDebugLoc();
2338
2339 // Convert:
2340 //
2341 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2342 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2343 //
2344 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2345 const ConstantSDNode *CMOVTrue =
2346 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2347 const ConstantSDNode *CMOVFalse =
2348 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2349
2350 if (CMOVTrue && CMOVFalse) {
2351 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2352 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2353
2354 SDValue True;
2355 SDValue False;
2356 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2357 True = SelectTrue;
2358 False = SelectFalse;
2359 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2360 True = SelectFalse;
2361 False = SelectTrue;
2362 }
2363
2364 if (True.getNode() && False.getNode()) {
2365 EVT VT = Cond.getValueType();
2366 SDValue ARMcc = Cond.getOperand(2);
2367 SDValue CCR = Cond.getOperand(3);
2368 SDValue Cmp = Cond.getOperand(4);
2369 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
2370 }
2371 }
2372 }
2373
2374 return DAG.getSelectCC(dl, Cond,
2375 DAG.getConstant(0, Cond.getValueType()),
2376 SelectTrue, SelectFalse, ISD::SETNE);
2377}
2378
Dan Gohmand858e902010-04-17 15:26:15 +00002379SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002380 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002381 SDValue LHS = Op.getOperand(0);
2382 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002383 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002384 SDValue TrueVal = Op.getOperand(2);
2385 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002386 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002387
Owen Anderson825b72b2009-08-11 20:47:22 +00002388 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002389 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002390 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002391 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2392 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002393 }
2394
2395 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002396 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002397
Evan Cheng218977b2010-07-13 19:27:42 +00002398 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2399 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002400 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002401 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002402 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002403 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002404 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002405 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002406 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002407 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002408 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002409 }
2410 return Result;
2411}
2412
Evan Cheng218977b2010-07-13 19:27:42 +00002413/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2414/// to morph to an integer compare sequence.
2415static bool canChangeToInt(SDValue Op, bool &SeenZero,
2416 const ARMSubtarget *Subtarget) {
2417 SDNode *N = Op.getNode();
2418 if (!N->hasOneUse())
2419 // Otherwise it requires moving the value from fp to integer registers.
2420 return false;
2421 if (!N->getNumValues())
2422 return false;
2423 EVT VT = Op.getValueType();
2424 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2425 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2426 // vmrs are very slow, e.g. cortex-a8.
2427 return false;
2428
2429 if (isFloatingPointZero(Op)) {
2430 SeenZero = true;
2431 return true;
2432 }
2433 return ISD::isNormalLoad(N);
2434}
2435
2436static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2437 if (isFloatingPointZero(Op))
2438 return DAG.getConstant(0, MVT::i32);
2439
2440 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2441 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2442 Ld->getChain(), Ld->getBasePtr(),
2443 Ld->getSrcValue(), Ld->getSrcValueOffset(),
2444 Ld->isVolatile(), Ld->isNonTemporal(),
2445 Ld->getAlignment());
2446
2447 llvm_unreachable("Unknown VFP cmp argument!");
2448}
2449
2450static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2451 SDValue &RetVal1, SDValue &RetVal2) {
2452 if (isFloatingPointZero(Op)) {
2453 RetVal1 = DAG.getConstant(0, MVT::i32);
2454 RetVal2 = DAG.getConstant(0, MVT::i32);
2455 return;
2456 }
2457
2458 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2459 SDValue Ptr = Ld->getBasePtr();
2460 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2461 Ld->getChain(), Ptr,
2462 Ld->getSrcValue(), Ld->getSrcValueOffset(),
2463 Ld->isVolatile(), Ld->isNonTemporal(),
2464 Ld->getAlignment());
2465
2466 EVT PtrType = Ptr.getValueType();
2467 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2468 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2469 PtrType, Ptr, DAG.getConstant(4, PtrType));
2470 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2471 Ld->getChain(), NewPtr,
2472 Ld->getSrcValue(), Ld->getSrcValueOffset() + 4,
2473 Ld->isVolatile(), Ld->isNonTemporal(),
2474 NewAlign);
2475 return;
2476 }
2477
2478 llvm_unreachable("Unknown VFP cmp argument!");
2479}
2480
2481/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2482/// f32 and even f64 comparisons to integer ones.
2483SDValue
2484ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2485 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002486 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002487 SDValue LHS = Op.getOperand(2);
2488 SDValue RHS = Op.getOperand(3);
2489 SDValue Dest = Op.getOperand(4);
2490 DebugLoc dl = Op.getDebugLoc();
2491
2492 bool SeenZero = false;
2493 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2494 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002495 // If one of the operand is zero, it's safe to ignore the NaN case since
2496 // we only care about equality comparisons.
2497 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Evan Cheng218977b2010-07-13 19:27:42 +00002498 // If unsafe fp math optimization is enabled and there are no othter uses of
2499 // the CMP operands, and the condition code is EQ oe NE, we can optimize it
2500 // to an integer comparison.
2501 if (CC == ISD::SETOEQ)
2502 CC = ISD::SETEQ;
2503 else if (CC == ISD::SETUNE)
2504 CC = ISD::SETNE;
2505
2506 SDValue ARMcc;
2507 if (LHS.getValueType() == MVT::f32) {
2508 LHS = bitcastf32Toi32(LHS, DAG);
2509 RHS = bitcastf32Toi32(RHS, DAG);
2510 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2511 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2512 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2513 Chain, Dest, ARMcc, CCR, Cmp);
2514 }
2515
2516 SDValue LHS1, LHS2;
2517 SDValue RHS1, RHS2;
2518 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2519 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2520 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2521 ARMcc = DAG.getConstant(CondCode, MVT::i32);
2522 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
2523 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2524 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2525 }
2526
2527 return SDValue();
2528}
2529
2530SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2531 SDValue Chain = Op.getOperand(0);
2532 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2533 SDValue LHS = Op.getOperand(2);
2534 SDValue RHS = Op.getOperand(3);
2535 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002536 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002537
Owen Anderson825b72b2009-08-11 20:47:22 +00002538 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002539 SDValue ARMcc;
2540 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002541 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002542 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002543 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002544 }
2545
Owen Anderson825b72b2009-08-11 20:47:22 +00002546 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002547
2548 if (UnsafeFPMath &&
2549 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2550 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2551 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2552 if (Result.getNode())
2553 return Result;
2554 }
2555
Evan Chenga8e29892007-01-19 07:51:42 +00002556 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002557 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002558
Evan Cheng218977b2010-07-13 19:27:42 +00002559 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2560 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002561 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2562 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng218977b2010-07-13 19:27:42 +00002563 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002564 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002565 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002566 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2567 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002568 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002569 }
2570 return Res;
2571}
2572
Dan Gohmand858e902010-04-17 15:26:15 +00002573SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002574 SDValue Chain = Op.getOperand(0);
2575 SDValue Table = Op.getOperand(1);
2576 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002577 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002578
Owen Andersone50ed302009-08-10 22:56:29 +00002579 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002580 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2581 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002582 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002583 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002584 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002585 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2586 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002587 if (Subtarget->isThumb2()) {
2588 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2589 // which does another jump to the destination. This also makes it easier
2590 // to translate it to TBB / TBH later.
2591 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002592 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002593 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002594 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002595 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002596 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002597 PseudoSourceValue::getJumpTable(), 0,
2598 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002599 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002600 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002601 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002602 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002603 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002604 PseudoSourceValue::getJumpTable(), 0, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002605 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002606 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002607 }
Evan Chenga8e29892007-01-19 07:51:42 +00002608}
2609
Bob Wilson76a312b2010-03-19 22:51:32 +00002610static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2611 DebugLoc dl = Op.getDebugLoc();
2612 unsigned Opc;
2613
2614 switch (Op.getOpcode()) {
2615 default:
2616 assert(0 && "Invalid opcode!");
2617 case ISD::FP_TO_SINT:
2618 Opc = ARMISD::FTOSI;
2619 break;
2620 case ISD::FP_TO_UINT:
2621 Opc = ARMISD::FTOUI;
2622 break;
2623 }
2624 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
2625 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
2626}
2627
2628static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2629 EVT VT = Op.getValueType();
2630 DebugLoc dl = Op.getDebugLoc();
2631 unsigned Opc;
2632
2633 switch (Op.getOpcode()) {
2634 default:
2635 assert(0 && "Invalid opcode!");
2636 case ISD::SINT_TO_FP:
2637 Opc = ARMISD::SITOF;
2638 break;
2639 case ISD::UINT_TO_FP:
2640 Opc = ARMISD::UITOF;
2641 break;
2642 }
2643
2644 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
2645 return DAG.getNode(Opc, dl, VT, Op);
2646}
2647
Evan Cheng515fe3a2010-07-08 02:08:50 +00002648SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002649 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002650 SDValue Tmp0 = Op.getOperand(0);
2651 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002652 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002653 EVT VT = Op.getValueType();
2654 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00002655 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
Evan Cheng218977b2010-07-13 19:27:42 +00002656 SDValue ARMcc = DAG.getConstant(ARMCC::LT, MVT::i32);
Evan Cheng515fe3a2010-07-08 02:08:50 +00002657 SDValue FP0 = DAG.getConstantFP(0.0, SrcVT);
Evan Cheng218977b2010-07-13 19:27:42 +00002658 SDValue Cmp = getVFPCmp(Tmp1, FP0, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002659 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002660 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002661}
2662
Evan Cheng2457f2c2010-05-22 01:47:14 +00002663SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
2664 MachineFunction &MF = DAG.getMachineFunction();
2665 MachineFrameInfo *MFI = MF.getFrameInfo();
2666 MFI->setReturnAddressIsTaken(true);
2667
2668 EVT VT = Op.getValueType();
2669 DebugLoc dl = Op.getDebugLoc();
2670 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2671 if (Depth) {
2672 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
2673 SDValue Offset = DAG.getConstant(4, MVT::i32);
2674 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
2675 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
2676 NULL, 0, false, false, 0);
2677 }
2678
2679 // Return LR, which contains the return address. Mark it an implicit live-in.
Jim Grosbachc2723a52010-07-23 23:50:35 +00002680 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00002681 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
2682}
2683
Dan Gohmand858e902010-04-17 15:26:15 +00002684SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002685 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2686 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002687
Owen Andersone50ed302009-08-10 22:56:29 +00002688 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002689 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2690 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002691 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002692 ? ARM::R7 : ARM::R11;
2693 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2694 while (Depth--)
David Greene1b58cab2010-02-15 16:55:24 +00002695 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
2696 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002697 return FrameAddr;
2698}
2699
Bob Wilson9f3f0612010-04-17 05:30:19 +00002700/// ExpandBIT_CONVERT - If the target supports VFP, this function is called to
2701/// expand a bit convert where either the source or destination type is i64 to
2702/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2703/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2704/// vectors), since the legalizer won't know what to do with that.
Duncan Sands1607f052008-12-01 11:39:25 +00002705static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002706 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2707 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002708 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002709
Bob Wilson9f3f0612010-04-17 05:30:19 +00002710 // This function is only supposed to be called for i64 types, either as the
2711 // source or destination of the bit convert.
2712 EVT SrcVT = Op.getValueType();
2713 EVT DstVT = N->getValueType(0);
2714 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
2715 "ExpandBIT_CONVERT called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002716
Bob Wilson9f3f0612010-04-17 05:30:19 +00002717 // Turn i64->f64 into VMOVDRR.
2718 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002719 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2720 DAG.getConstant(0, MVT::i32));
2721 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2722 DAG.getConstant(1, MVT::i32));
Bob Wilson1114f562010-06-11 22:45:25 +00002723 return DAG.getNode(ISD::BIT_CONVERT, dl, DstVT,
2724 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00002725 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002726
Jim Grosbache5165492009-11-09 00:11:35 +00002727 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002728 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2729 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2730 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2731 // Merge the pieces into a single i64 value.
2732 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2733 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002734
Bob Wilson9f3f0612010-04-17 05:30:19 +00002735 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002736}
2737
Bob Wilson5bafff32009-06-22 23:27:02 +00002738/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00002739/// Zero vectors are used to represent vector negation and in those cases
2740/// will be implemented with the NEON VNEG instruction. However, VNEG does
2741/// not support i64 elements, so sometimes the zero vectors will need to be
2742/// explicitly constructed. Regardless, use a canonical VMOV to create the
2743/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002744static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002745 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00002746 // The canonical modified immediate encoding of a zero vector is....0!
2747 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
2748 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
2749 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
2750 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00002751}
2752
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002753/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2754/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002755SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2756 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002757 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2758 EVT VT = Op.getValueType();
2759 unsigned VTBits = VT.getSizeInBits();
2760 DebugLoc dl = Op.getDebugLoc();
2761 SDValue ShOpLo = Op.getOperand(0);
2762 SDValue ShOpHi = Op.getOperand(1);
2763 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00002764 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002765 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002766
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002767 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2768
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002769 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2770 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2771 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2772 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2773 DAG.getConstant(VTBits, MVT::i32));
2774 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2775 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002776 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002777
2778 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2779 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00002780 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002781 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00002782 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002783 CCR, Cmp);
2784
2785 SDValue Ops[2] = { Lo, Hi };
2786 return DAG.getMergeValues(Ops, 2, dl);
2787}
2788
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002789/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2790/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002791SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2792 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002793 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2794 EVT VT = Op.getValueType();
2795 unsigned VTBits = VT.getSizeInBits();
2796 DebugLoc dl = Op.getDebugLoc();
2797 SDValue ShOpLo = Op.getOperand(0);
2798 SDValue ShOpHi = Op.getOperand(1);
2799 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00002800 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002801
2802 assert(Op.getOpcode() == ISD::SHL_PARTS);
2803 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2804 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2805 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2806 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2807 DAG.getConstant(VTBits, MVT::i32));
2808 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2809 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2810
2811 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2812 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2813 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00002814 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002815 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00002816 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002817 CCR, Cmp);
2818
2819 SDValue Ops[2] = { Lo, Hi };
2820 return DAG.getMergeValues(Ops, 2, dl);
2821}
2822
Nate Begemand1fb5832010-08-03 21:31:55 +00002823SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
2824 SelectionDAG &DAG) const {
2825 // The rounding mode is in bits 23:22 of the FPSCR.
2826 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
2827 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
2828 // so that the shift + and get folded into a bitfield extract.
2829 DebugLoc dl = Op.getDebugLoc();
2830 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
2831 DAG.getConstant(Intrinsic::arm_get_fpscr,
2832 MVT::i32));
2833 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
2834 DAG.getConstant(1U << 22, MVT::i32));
2835 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
2836 DAG.getConstant(22, MVT::i32));
2837 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
2838 DAG.getConstant(3, MVT::i32));
2839}
2840
Jim Grosbach3482c802010-01-18 19:58:49 +00002841static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
2842 const ARMSubtarget *ST) {
2843 EVT VT = N->getValueType(0);
2844 DebugLoc dl = N->getDebugLoc();
2845
2846 if (!ST->hasV6T2Ops())
2847 return SDValue();
2848
2849 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
2850 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
2851}
2852
Bob Wilson5bafff32009-06-22 23:27:02 +00002853static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2854 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002855 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002856 DebugLoc dl = N->getDebugLoc();
2857
2858 // Lower vector shifts on NEON to use VSHL.
2859 if (VT.isVector()) {
2860 assert(ST->hasNEON() && "unexpected vector shift");
2861
2862 // Left shifts translate directly to the vshiftu intrinsic.
2863 if (N->getOpcode() == ISD::SHL)
2864 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002865 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002866 N->getOperand(0), N->getOperand(1));
2867
2868 assert((N->getOpcode() == ISD::SRA ||
2869 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2870
2871 // NEON uses the same intrinsics for both left and right shifts. For
2872 // right shifts, the shift amounts are negative, so negate the vector of
2873 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002874 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002875 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2876 getZeroVector(ShiftVT, DAG, dl),
2877 N->getOperand(1));
2878 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2879 Intrinsic::arm_neon_vshifts :
2880 Intrinsic::arm_neon_vshiftu);
2881 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002882 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002883 N->getOperand(0), NegatedCount);
2884 }
2885
Eli Friedmance392eb2009-08-22 03:13:10 +00002886 // We can get here for a node like i32 = ISD::SHL i32, i64
2887 if (VT != MVT::i64)
2888 return SDValue();
2889
2890 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002891 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002892
Chris Lattner27a6c732007-11-24 07:07:01 +00002893 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2894 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002895 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002896 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002897
Chris Lattner27a6c732007-11-24 07:07:01 +00002898 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002899 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002900
Chris Lattner27a6c732007-11-24 07:07:01 +00002901 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002902 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00002903 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00002904 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00002905 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002906
Chris Lattner27a6c732007-11-24 07:07:01 +00002907 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2908 // captures the result into a carry flag.
2909 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002910 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002911
Chris Lattner27a6c732007-11-24 07:07:01 +00002912 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002913 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002914
Chris Lattner27a6c732007-11-24 07:07:01 +00002915 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002916 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002917}
2918
Bob Wilson5bafff32009-06-22 23:27:02 +00002919static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2920 SDValue TmpOp0, TmpOp1;
2921 bool Invert = false;
2922 bool Swap = false;
2923 unsigned Opc = 0;
2924
2925 SDValue Op0 = Op.getOperand(0);
2926 SDValue Op1 = Op.getOperand(1);
2927 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002928 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002929 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2930 DebugLoc dl = Op.getDebugLoc();
2931
2932 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2933 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002934 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002935 case ISD::SETUNE:
2936 case ISD::SETNE: Invert = true; // Fallthrough
2937 case ISD::SETOEQ:
2938 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2939 case ISD::SETOLT:
2940 case ISD::SETLT: Swap = true; // Fallthrough
2941 case ISD::SETOGT:
2942 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2943 case ISD::SETOLE:
2944 case ISD::SETLE: Swap = true; // Fallthrough
2945 case ISD::SETOGE:
2946 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2947 case ISD::SETUGE: Swap = true; // Fallthrough
2948 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2949 case ISD::SETUGT: Swap = true; // Fallthrough
2950 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2951 case ISD::SETUEQ: Invert = true; // Fallthrough
2952 case ISD::SETONE:
2953 // Expand this to (OLT | OGT).
2954 TmpOp0 = Op0;
2955 TmpOp1 = Op1;
2956 Opc = ISD::OR;
2957 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2958 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2959 break;
2960 case ISD::SETUO: Invert = true; // Fallthrough
2961 case ISD::SETO:
2962 // Expand this to (OLT | OGE).
2963 TmpOp0 = Op0;
2964 TmpOp1 = Op1;
2965 Opc = ISD::OR;
2966 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2967 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2968 break;
2969 }
2970 } else {
2971 // Integer comparisons.
2972 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002973 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002974 case ISD::SETNE: Invert = true;
2975 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2976 case ISD::SETLT: Swap = true;
2977 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2978 case ISD::SETLE: Swap = true;
2979 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2980 case ISD::SETULT: Swap = true;
2981 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2982 case ISD::SETULE: Swap = true;
2983 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2984 }
2985
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002986 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002987 if (Opc == ARMISD::VCEQ) {
2988
2989 SDValue AndOp;
2990 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2991 AndOp = Op0;
2992 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2993 AndOp = Op1;
2994
2995 // Ignore bitconvert.
2996 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2997 AndOp = AndOp.getOperand(0);
2998
2999 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3000 Opc = ARMISD::VTST;
3001 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
3002 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
3003 Invert = !Invert;
3004 }
3005 }
3006 }
3007
3008 if (Swap)
3009 std::swap(Op0, Op1);
3010
3011 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3012
3013 if (Invert)
3014 Result = DAG.getNOT(dl, Result, VT);
3015
3016 return Result;
3017}
3018
Bob Wilsond3c42842010-06-14 22:19:57 +00003019/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3020/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003021/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003022static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3023 unsigned SplatBitSize, SelectionDAG &DAG,
Bob Wilsoncba270d2010-07-13 21:16:48 +00003024 EVT &VT, bool is128Bits, bool isVMOV) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003025 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003026
Bob Wilson827b2102010-06-15 19:05:35 +00003027 // SplatBitSize is set to the smallest size that splats the vector, so a
3028 // zero vector will always have SplatBitSize == 8. However, NEON modified
3029 // immediate instructions others than VMOV do not support the 8-bit encoding
3030 // of a zero vector, and the default encoding of zero is supposed to be the
3031 // 32-bit version.
3032 if (SplatBits == 0)
3033 SplatBitSize = 32;
3034
Bob Wilson5bafff32009-06-22 23:27:02 +00003035 switch (SplatBitSize) {
3036 case 8:
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003037 if (!isVMOV)
3038 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003039 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003040 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003041 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003042 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003043 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003044 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003045
3046 case 16:
3047 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003048 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003049 if ((SplatBits & ~0xff) == 0) {
3050 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003051 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003052 Imm = SplatBits;
3053 break;
3054 }
3055 if ((SplatBits & ~0xff00) == 0) {
3056 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003057 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003058 Imm = SplatBits >> 8;
3059 break;
3060 }
3061 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003062
3063 case 32:
3064 // NEON's 32-bit VMOV supports splat values where:
3065 // * only one byte is nonzero, or
3066 // * the least significant byte is 0xff and the second byte is nonzero, or
3067 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003068 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003069 if ((SplatBits & ~0xff) == 0) {
3070 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003071 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003072 Imm = SplatBits;
3073 break;
3074 }
3075 if ((SplatBits & ~0xff00) == 0) {
3076 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003077 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003078 Imm = SplatBits >> 8;
3079 break;
3080 }
3081 if ((SplatBits & ~0xff0000) == 0) {
3082 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003083 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003084 Imm = SplatBits >> 16;
3085 break;
3086 }
3087 if ((SplatBits & ~0xff000000) == 0) {
3088 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003089 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003090 Imm = SplatBits >> 24;
3091 break;
3092 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003093
3094 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003095 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3096 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003097 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003098 Imm = SplatBits >> 8;
3099 SplatBits |= 0xff;
3100 break;
3101 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003102
3103 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003104 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3105 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003106 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003107 Imm = SplatBits >> 16;
3108 SplatBits |= 0xffff;
3109 break;
3110 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003111
3112 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3113 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3114 // VMOV.I32. A (very) minor optimization would be to replicate the value
3115 // and fall through here to test for a valid 64-bit splat. But, then the
3116 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003117 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003118
3119 case 64: {
Bob Wilson827b2102010-06-15 19:05:35 +00003120 if (!isVMOV)
3121 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003122 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003123 uint64_t BitMask = 0xff;
3124 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003125 unsigned ImmMask = 1;
3126 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003127 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003128 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003129 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003130 Imm |= ImmMask;
3131 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003132 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003133 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003134 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003135 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003136 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003137 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003138 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003139 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003140 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003141 break;
3142 }
3143
Bob Wilson1a913ed2010-06-11 21:34:50 +00003144 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003145 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003146 return SDValue();
3147 }
3148
Bob Wilsoncba270d2010-07-13 21:16:48 +00003149 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3150 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003151}
3152
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003153static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3154 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003155 unsigned NumElts = VT.getVectorNumElements();
3156 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003157
3158 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3159 if (M[0] < 0)
3160 return false;
3161
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003162 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003163
3164 // If this is a VEXT shuffle, the immediate value is the index of the first
3165 // element. The other shuffle indices must be the successive elements after
3166 // the first one.
3167 unsigned ExpectedElt = Imm;
3168 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003169 // Increment the expected index. If it wraps around, it may still be
3170 // a VEXT but the source vectors must be swapped.
3171 ExpectedElt += 1;
3172 if (ExpectedElt == NumElts * 2) {
3173 ExpectedElt = 0;
3174 ReverseVEXT = true;
3175 }
3176
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003177 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003178 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003179 return false;
3180 }
3181
3182 // Adjust the index value if the source operands will be swapped.
3183 if (ReverseVEXT)
3184 Imm -= NumElts;
3185
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003186 return true;
3187}
3188
Bob Wilson8bb9e482009-07-26 00:39:34 +00003189/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3190/// instruction with the specified blocksize. (The order of the elements
3191/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003192static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3193 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003194 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3195 "Only possible block sizes for VREV are: 16, 32, 64");
3196
Bob Wilson8bb9e482009-07-26 00:39:34 +00003197 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003198 if (EltSz == 64)
3199 return false;
3200
3201 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003202 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003203 // If the first shuffle index is UNDEF, be optimistic.
3204 if (M[0] < 0)
3205 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003206
3207 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3208 return false;
3209
3210 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003211 if (M[i] < 0) continue; // ignore UNDEF indices
3212 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003213 return false;
3214 }
3215
3216 return true;
3217}
3218
Bob Wilsonc692cb72009-08-21 20:54:19 +00003219static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3220 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003221 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3222 if (EltSz == 64)
3223 return false;
3224
Bob Wilsonc692cb72009-08-21 20:54:19 +00003225 unsigned NumElts = VT.getVectorNumElements();
3226 WhichResult = (M[0] == 0 ? 0 : 1);
3227 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003228 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3229 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003230 return false;
3231 }
3232 return true;
3233}
3234
Bob Wilson324f4f12009-12-03 06:40:55 +00003235/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3236/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3237/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3238static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3239 unsigned &WhichResult) {
3240 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3241 if (EltSz == 64)
3242 return false;
3243
3244 unsigned NumElts = VT.getVectorNumElements();
3245 WhichResult = (M[0] == 0 ? 0 : 1);
3246 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003247 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3248 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003249 return false;
3250 }
3251 return true;
3252}
3253
Bob Wilsonc692cb72009-08-21 20:54:19 +00003254static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3255 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003256 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3257 if (EltSz == 64)
3258 return false;
3259
Bob Wilsonc692cb72009-08-21 20:54:19 +00003260 unsigned NumElts = VT.getVectorNumElements();
3261 WhichResult = (M[0] == 0 ? 0 : 1);
3262 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003263 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003264 if ((unsigned) M[i] != 2 * i + WhichResult)
3265 return false;
3266 }
3267
3268 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003269 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003270 return false;
3271
3272 return true;
3273}
3274
Bob Wilson324f4f12009-12-03 06:40:55 +00003275/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3276/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3277/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3278static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3279 unsigned &WhichResult) {
3280 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3281 if (EltSz == 64)
3282 return false;
3283
3284 unsigned Half = VT.getVectorNumElements() / 2;
3285 WhichResult = (M[0] == 0 ? 0 : 1);
3286 for (unsigned j = 0; j != 2; ++j) {
3287 unsigned Idx = WhichResult;
3288 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003289 int MIdx = M[i + j * Half];
3290 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003291 return false;
3292 Idx += 2;
3293 }
3294 }
3295
3296 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3297 if (VT.is64BitVector() && EltSz == 32)
3298 return false;
3299
3300 return true;
3301}
3302
Bob Wilsonc692cb72009-08-21 20:54:19 +00003303static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3304 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003305 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3306 if (EltSz == 64)
3307 return false;
3308
Bob Wilsonc692cb72009-08-21 20:54:19 +00003309 unsigned NumElts = VT.getVectorNumElements();
3310 WhichResult = (M[0] == 0 ? 0 : 1);
3311 unsigned Idx = WhichResult * NumElts / 2;
3312 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003313 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3314 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003315 return false;
3316 Idx += 1;
3317 }
3318
3319 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003320 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003321 return false;
3322
3323 return true;
3324}
3325
Bob Wilson324f4f12009-12-03 06:40:55 +00003326/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3327/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3328/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3329static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3330 unsigned &WhichResult) {
3331 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3332 if (EltSz == 64)
3333 return false;
3334
3335 unsigned NumElts = VT.getVectorNumElements();
3336 WhichResult = (M[0] == 0 ? 0 : 1);
3337 unsigned Idx = WhichResult * NumElts / 2;
3338 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003339 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3340 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003341 return false;
3342 Idx += 1;
3343 }
3344
3345 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3346 if (VT.is64BitVector() && EltSz == 32)
3347 return false;
3348
3349 return true;
3350}
3351
Dale Johannesenf630c712010-07-29 20:10:08 +00003352// If N is an integer constant that can be moved into a register in one
3353// instruction, return an SDValue of such a constant (will become a MOV
3354// instruction). Otherwise return null.
3355static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3356 const ARMSubtarget *ST, DebugLoc dl) {
3357 uint64_t Val;
3358 if (!isa<ConstantSDNode>(N))
3359 return SDValue();
3360 Val = cast<ConstantSDNode>(N)->getZExtValue();
3361
3362 if (ST->isThumb1Only()) {
3363 if (Val <= 255 || ~Val <= 255)
3364 return DAG.getConstant(Val, MVT::i32);
3365 } else {
3366 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3367 return DAG.getConstant(Val, MVT::i32);
3368 }
3369 return SDValue();
3370}
3371
Bob Wilson5bafff32009-06-22 23:27:02 +00003372// If this is a case we can't handle, return null and let the default
3373// expansion code take care of it.
Dale Johannesenf630c712010-07-29 20:10:08 +00003374static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3375 const ARMSubtarget *ST) {
Bob Wilsond06791f2009-08-13 01:57:47 +00003376 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003377 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003378 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003379
3380 APInt SplatBits, SplatUndef;
3381 unsigned SplatBitSize;
3382 bool HasAnyUndefs;
3383 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003384 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003385 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003386 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003387 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003388 SplatUndef.getZExtValue(), SplatBitSize,
3389 DAG, VmovVT, VT.is128BitVector(), true);
3390 if (Val.getNode()) {
3391 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
3392 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vmov);
3393 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003394
3395 // Try an immediate VMVN.
3396 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3397 ((1LL << SplatBitSize) - 1));
3398 Val = isNEONModifiedImm(NegatedImm,
3399 SplatUndef.getZExtValue(), SplatBitSize,
3400 DAG, VmovVT, VT.is128BitVector(), false);
3401 if (Val.getNode()) {
3402 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
3403 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vmov);
3404 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003405 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003406 }
3407
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003408 // Scan through the operands to see if only one value is used.
3409 unsigned NumElts = VT.getVectorNumElements();
3410 bool isOnlyLowElement = true;
3411 bool usesOnlyOneValue = true;
3412 bool isConstant = true;
3413 SDValue Value;
3414 for (unsigned i = 0; i < NumElts; ++i) {
3415 SDValue V = Op.getOperand(i);
3416 if (V.getOpcode() == ISD::UNDEF)
3417 continue;
3418 if (i > 0)
3419 isOnlyLowElement = false;
3420 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3421 isConstant = false;
3422
3423 if (!Value.getNode())
3424 Value = V;
3425 else if (V != Value)
3426 usesOnlyOneValue = false;
3427 }
3428
3429 if (!Value.getNode())
3430 return DAG.getUNDEF(VT);
3431
3432 if (isOnlyLowElement)
3433 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3434
Dale Johannesenf630c712010-07-29 20:10:08 +00003435 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3436
3437 if (EnableARMVDUPsplat) {
3438 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3439 // i32 and try again.
3440 if (usesOnlyOneValue && EltSize <= 32) {
3441 if (!isConstant)
3442 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3443 if (VT.getVectorElementType().isFloatingPoint()) {
3444 SmallVector<SDValue, 8> Ops;
3445 for (unsigned i = 0; i < NumElts; ++i)
3446 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
3447 Op.getOperand(i)));
3448 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &Ops[0],
3449 NumElts);
3450 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3451 LowerBUILD_VECTOR(Val, DAG, ST));
3452 }
3453 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3454 if (Val.getNode())
3455 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
3456 }
3457 }
3458
3459 // If all elements are constants and the case above didn't get hit, fall back
3460 // to the default expansion, which will generate a load from the constant
3461 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003462 if (isConstant)
3463 return SDValue();
3464
Dale Johannesenf630c712010-07-29 20:10:08 +00003465 if (!EnableARMVDUPsplat) {
3466 // Use VDUP for non-constant splats.
3467 if (usesOnlyOneValue && EltSize <= 32)
3468 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3469 }
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003470
3471 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003472 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3473 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003474 if (EltSize >= 32) {
3475 // Do the expansion with floating-point types, since that is what the VFP
3476 // registers are defined to use, and since i64 is not legal.
3477 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3478 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003479 SmallVector<SDValue, 8> Ops;
3480 for (unsigned i = 0; i < NumElts; ++i)
3481 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, dl, EltVT, Op.getOperand(i)));
3482 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003483 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003484 }
3485
3486 return SDValue();
3487}
3488
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003489/// isShuffleMaskLegal - Targets can use this to indicate that they only
3490/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3491/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3492/// are assumed to be legal.
3493bool
3494ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
3495 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003496 if (VT.getVectorNumElements() == 4 &&
3497 (VT.is128BitVector() || VT.is64BitVector())) {
3498 unsigned PFIndexes[4];
3499 for (unsigned i = 0; i != 4; ++i) {
3500 if (M[i] < 0)
3501 PFIndexes[i] = 8;
3502 else
3503 PFIndexes[i] = M[i];
3504 }
3505
3506 // Compute the index in the perfect shuffle table.
3507 unsigned PFTableIndex =
3508 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3509 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3510 unsigned Cost = (PFEntry >> 30);
3511
3512 if (Cost <= 4)
3513 return true;
3514 }
3515
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003516 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00003517 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003518
Bob Wilson53dd2452010-06-07 23:53:38 +00003519 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3520 return (EltSize >= 32 ||
3521 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003522 isVREVMask(M, VT, 64) ||
3523 isVREVMask(M, VT, 32) ||
3524 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00003525 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
3526 isVTRNMask(M, VT, WhichResult) ||
3527 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00003528 isVZIPMask(M, VT, WhichResult) ||
3529 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
3530 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
3531 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003532}
3533
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003534/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
3535/// the specified operations to build the shuffle.
3536static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
3537 SDValue RHS, SelectionDAG &DAG,
3538 DebugLoc dl) {
3539 unsigned OpNum = (PFEntry >> 26) & 0x0F;
3540 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
3541 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
3542
3543 enum {
3544 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
3545 OP_VREV,
3546 OP_VDUP0,
3547 OP_VDUP1,
3548 OP_VDUP2,
3549 OP_VDUP3,
3550 OP_VEXT1,
3551 OP_VEXT2,
3552 OP_VEXT3,
3553 OP_VUZPL, // VUZP, left result
3554 OP_VUZPR, // VUZP, right result
3555 OP_VZIPL, // VZIP, left result
3556 OP_VZIPR, // VZIP, right result
3557 OP_VTRNL, // VTRN, left result
3558 OP_VTRNR // VTRN, right result
3559 };
3560
3561 if (OpNum == OP_COPY) {
3562 if (LHSID == (1*9+2)*9+3) return LHS;
3563 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
3564 return RHS;
3565 }
3566
3567 SDValue OpLHS, OpRHS;
3568 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
3569 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
3570 EVT VT = OpLHS.getValueType();
3571
3572 switch (OpNum) {
3573 default: llvm_unreachable("Unknown shuffle opcode!");
3574 case OP_VREV:
3575 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
3576 case OP_VDUP0:
3577 case OP_VDUP1:
3578 case OP_VDUP2:
3579 case OP_VDUP3:
3580 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003581 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003582 case OP_VEXT1:
3583 case OP_VEXT2:
3584 case OP_VEXT3:
3585 return DAG.getNode(ARMISD::VEXT, dl, VT,
3586 OpLHS, OpRHS,
3587 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
3588 case OP_VUZPL:
3589 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003590 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003591 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
3592 case OP_VZIPL:
3593 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003594 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003595 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
3596 case OP_VTRNL:
3597 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003598 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3599 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003600 }
3601}
3602
Bob Wilson5bafff32009-06-22 23:27:02 +00003603static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003604 SDValue V1 = Op.getOperand(0);
3605 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00003606 DebugLoc dl = Op.getDebugLoc();
3607 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003608 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003609 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00003610
Bob Wilson28865062009-08-13 02:13:04 +00003611 // Convert shuffles that are directly supported on NEON to target-specific
3612 // DAG nodes, instead of keeping them as shuffles and matching them again
3613 // during code selection. This is more efficient and avoids the possibility
3614 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00003615 // FIXME: floating-point vectors should be canonicalized to integer vectors
3616 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003617 SVN->getMask(ShuffleMask);
3618
Bob Wilson53dd2452010-06-07 23:53:38 +00003619 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3620 if (EltSize <= 32) {
3621 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
3622 int Lane = SVN->getSplatIndex();
3623 // If this is undef splat, generate it via "just" vdup, if possible.
3624 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00003625
Bob Wilson53dd2452010-06-07 23:53:38 +00003626 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3627 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
3628 }
3629 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
3630 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00003631 }
Bob Wilson53dd2452010-06-07 23:53:38 +00003632
3633 bool ReverseVEXT;
3634 unsigned Imm;
3635 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
3636 if (ReverseVEXT)
3637 std::swap(V1, V2);
3638 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
3639 DAG.getConstant(Imm, MVT::i32));
3640 }
3641
3642 if (isVREVMask(ShuffleMask, VT, 64))
3643 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
3644 if (isVREVMask(ShuffleMask, VT, 32))
3645 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
3646 if (isVREVMask(ShuffleMask, VT, 16))
3647 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
3648
3649 // Check for Neon shuffles that modify both input vectors in place.
3650 // If both results are used, i.e., if there are two shuffles with the same
3651 // source operands and with masks corresponding to both results of one of
3652 // these operations, DAG memoization will ensure that a single node is
3653 // used for both shuffles.
3654 unsigned WhichResult;
3655 if (isVTRNMask(ShuffleMask, VT, WhichResult))
3656 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3657 V1, V2).getValue(WhichResult);
3658 if (isVUZPMask(ShuffleMask, VT, WhichResult))
3659 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3660 V1, V2).getValue(WhichResult);
3661 if (isVZIPMask(ShuffleMask, VT, WhichResult))
3662 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3663 V1, V2).getValue(WhichResult);
3664
3665 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
3666 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3667 V1, V1).getValue(WhichResult);
3668 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3669 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3670 V1, V1).getValue(WhichResult);
3671 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3672 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3673 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00003674 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003675
Bob Wilsonc692cb72009-08-21 20:54:19 +00003676 // If the shuffle is not directly supported and it has 4 elements, use
3677 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003678 unsigned NumElts = VT.getVectorNumElements();
3679 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003680 unsigned PFIndexes[4];
3681 for (unsigned i = 0; i != 4; ++i) {
3682 if (ShuffleMask[i] < 0)
3683 PFIndexes[i] = 8;
3684 else
3685 PFIndexes[i] = ShuffleMask[i];
3686 }
3687
3688 // Compute the index in the perfect shuffle table.
3689 unsigned PFTableIndex =
3690 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003691 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3692 unsigned Cost = (PFEntry >> 30);
3693
3694 if (Cost <= 4)
3695 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
3696 }
Bob Wilsond8e17572009-08-12 22:31:50 +00003697
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003698 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003699 if (EltSize >= 32) {
3700 // Do the expansion with floating-point types, since that is what the VFP
3701 // registers are defined to use, and since i64 is not legal.
3702 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3703 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
3704 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V1);
3705 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003706 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003707 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00003708 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003709 Ops.push_back(DAG.getUNDEF(EltVT));
3710 else
3711 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
3712 ShuffleMask[i] < (int)NumElts ? V1 : V2,
3713 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
3714 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00003715 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003716 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Bob Wilson63b88452010-05-20 18:39:53 +00003717 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
3718 }
3719
Bob Wilson22cac0d2009-08-14 05:16:33 +00003720 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003721}
3722
Bob Wilson5bafff32009-06-22 23:27:02 +00003723static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003724 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003725 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003726 SDValue Vec = Op.getOperand(0);
3727 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00003728 assert(VT == MVT::i32 &&
3729 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
3730 "unexpected type for custom-lowering vector extract");
3731 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00003732}
3733
Bob Wilsona6d65862009-08-03 20:36:38 +00003734static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3735 // The only time a CONCAT_VECTORS operation can have legal types is when
3736 // two 64-bit vectors are concatenated to a 128-bit vector.
3737 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
3738 "unexpected CONCAT_VECTORS");
3739 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00003740 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00003741 SDValue Op0 = Op.getOperand(0);
3742 SDValue Op1 = Op.getOperand(1);
3743 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003744 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3745 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00003746 DAG.getIntPtrConstant(0));
3747 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003748 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3749 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00003750 DAG.getIntPtrConstant(1));
3751 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003752}
3753
Dan Gohmand858e902010-04-17 15:26:15 +00003754SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003755 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003756 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00003757 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00003758 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003759 case ISD::GlobalAddress:
3760 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
3761 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00003762 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00003763 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00003764 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
3765 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003766 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00003767 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00003768 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00003769 case ISD::SINT_TO_FP:
3770 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
3771 case ISD::FP_TO_SINT:
3772 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003773 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003774 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003775 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003776 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00003777 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00003778 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00003779 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
3780 Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00003781 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003782 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00003783 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00003784 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00003785 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003786 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00003787 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00003788 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003789 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00003790 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003791 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003792 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00003793 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00003794 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003795 }
Dan Gohman475871a2008-07-27 21:46:04 +00003796 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003797}
3798
Duncan Sands1607f052008-12-01 11:39:25 +00003799/// ReplaceNodeResults - Replace the results of node with an illegal result
3800/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00003801void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
3802 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00003803 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00003804 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00003805 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00003806 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003807 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003808 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003809 case ISD::BIT_CONVERT:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003810 Res = ExpandBIT_CONVERT(N, DAG);
3811 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00003812 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003813 case ISD::SRA:
3814 Res = LowerShift(N, DAG, Subtarget);
3815 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003816 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00003817 if (Res.getNode())
3818 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00003819}
Chris Lattner27a6c732007-11-24 07:07:01 +00003820
Evan Chenga8e29892007-01-19 07:51:42 +00003821//===----------------------------------------------------------------------===//
3822// ARM Scheduler Hooks
3823//===----------------------------------------------------------------------===//
3824
3825MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003826ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
3827 MachineBasicBlock *BB,
3828 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00003829 unsigned dest = MI->getOperand(0).getReg();
3830 unsigned ptr = MI->getOperand(1).getReg();
3831 unsigned oldval = MI->getOperand(2).getReg();
3832 unsigned newval = MI->getOperand(3).getReg();
3833 unsigned scratch = BB->getParent()->getRegInfo()
3834 .createVirtualRegister(ARM::GPRRegisterClass);
3835 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3836 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003837 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00003838
3839 unsigned ldrOpc, strOpc;
3840 switch (Size) {
3841 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003842 case 1:
3843 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
3844 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
3845 break;
3846 case 2:
3847 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3848 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3849 break;
3850 case 4:
3851 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3852 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3853 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00003854 }
3855
3856 MachineFunction *MF = BB->getParent();
3857 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3858 MachineFunction::iterator It = BB;
3859 ++It; // insert the new blocks after the current block
3860
3861 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3862 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3863 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3864 MF->insert(It, loop1MBB);
3865 MF->insert(It, loop2MBB);
3866 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00003867
3868 // Transfer the remainder of BB and its successor edges to exitMBB.
3869 exitMBB->splice(exitMBB->begin(), BB,
3870 llvm::next(MachineBasicBlock::iterator(MI)),
3871 BB->end());
3872 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003873
3874 // thisMBB:
3875 // ...
3876 // fallthrough --> loop1MBB
3877 BB->addSuccessor(loop1MBB);
3878
3879 // loop1MBB:
3880 // ldrex dest, [ptr]
3881 // cmp dest, oldval
3882 // bne exitMBB
3883 BB = loop1MBB;
3884 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003885 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003886 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003887 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3888 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003889 BB->addSuccessor(loop2MBB);
3890 BB->addSuccessor(exitMBB);
3891
3892 // loop2MBB:
3893 // strex scratch, newval, [ptr]
3894 // cmp scratch, #0
3895 // bne loop1MBB
3896 BB = loop2MBB;
3897 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
3898 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003899 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003900 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003901 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3902 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003903 BB->addSuccessor(loop1MBB);
3904 BB->addSuccessor(exitMBB);
3905
3906 // exitMBB:
3907 // ...
3908 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00003909
Dan Gohman14152b42010-07-06 20:24:04 +00003910 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00003911
Jim Grosbach5278eb82009-12-11 01:42:04 +00003912 return BB;
3913}
3914
3915MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003916ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
3917 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00003918 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
3919 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3920
3921 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003922 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003923 MachineFunction::iterator It = BB;
3924 ++It;
3925
3926 unsigned dest = MI->getOperand(0).getReg();
3927 unsigned ptr = MI->getOperand(1).getReg();
3928 unsigned incr = MI->getOperand(2).getReg();
3929 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00003930
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003931 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003932 unsigned ldrOpc, strOpc;
3933 switch (Size) {
3934 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003935 case 1:
3936 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00003937 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003938 break;
3939 case 2:
3940 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3941 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3942 break;
3943 case 4:
3944 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3945 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3946 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00003947 }
3948
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003949 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3950 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3951 MF->insert(It, loopMBB);
3952 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00003953
3954 // Transfer the remainder of BB and its successor edges to exitMBB.
3955 exitMBB->splice(exitMBB->begin(), BB,
3956 llvm::next(MachineBasicBlock::iterator(MI)),
3957 BB->end());
3958 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003959
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003960 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003961 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3962 unsigned scratch2 = (!BinOpcode) ? incr :
3963 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3964
3965 // thisMBB:
3966 // ...
3967 // fallthrough --> loopMBB
3968 BB->addSuccessor(loopMBB);
3969
3970 // loopMBB:
3971 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003972 // <binop> scratch2, dest, incr
3973 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00003974 // cmp scratch, #0
3975 // bne- loopMBB
3976 // fallthrough --> exitMBB
3977 BB = loopMBB;
3978 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00003979 if (BinOpcode) {
3980 // operand order needs to go the other way for NAND
3981 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
3982 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3983 addReg(incr).addReg(dest)).addReg(0);
3984 else
3985 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3986 addReg(dest).addReg(incr)).addReg(0);
3987 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00003988
3989 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
3990 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003991 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00003992 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003993 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3994 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003995
3996 BB->addSuccessor(loopMBB);
3997 BB->addSuccessor(exitMBB);
3998
3999 // exitMBB:
4000 // ...
4001 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00004002
Dan Gohman14152b42010-07-06 20:24:04 +00004003 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00004004
Jim Grosbachc3c23542009-12-14 04:22:04 +00004005 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00004006}
4007
Evan Cheng218977b2010-07-13 19:27:42 +00004008static
4009MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
4010 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
4011 E = MBB->succ_end(); I != E; ++I)
4012 if (*I != Succ)
4013 return *I;
4014 llvm_unreachable("Expecting a BB with two successors!");
4015}
4016
Jim Grosbache801dc42009-12-12 01:40:06 +00004017MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00004018ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00004019 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004020 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00004021 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004022 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00004023 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00004024 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00004025 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00004026 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00004027
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004028 case ARM::ATOMIC_LOAD_ADD_I8:
4029 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4030 case ARM::ATOMIC_LOAD_ADD_I16:
4031 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4032 case ARM::ATOMIC_LOAD_ADD_I32:
4033 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004034
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004035 case ARM::ATOMIC_LOAD_AND_I8:
4036 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4037 case ARM::ATOMIC_LOAD_AND_I16:
4038 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4039 case ARM::ATOMIC_LOAD_AND_I32:
4040 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004041
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004042 case ARM::ATOMIC_LOAD_OR_I8:
4043 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4044 case ARM::ATOMIC_LOAD_OR_I16:
4045 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4046 case ARM::ATOMIC_LOAD_OR_I32:
4047 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004048
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004049 case ARM::ATOMIC_LOAD_XOR_I8:
4050 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
4051 case ARM::ATOMIC_LOAD_XOR_I16:
4052 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
4053 case ARM::ATOMIC_LOAD_XOR_I32:
4054 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004055
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004056 case ARM::ATOMIC_LOAD_NAND_I8:
4057 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
4058 case ARM::ATOMIC_LOAD_NAND_I16:
4059 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
4060 case ARM::ATOMIC_LOAD_NAND_I32:
4061 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004062
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004063 case ARM::ATOMIC_LOAD_SUB_I8:
4064 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
4065 case ARM::ATOMIC_LOAD_SUB_I16:
4066 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
4067 case ARM::ATOMIC_LOAD_SUB_I32:
4068 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004069
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004070 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
4071 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
4072 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00004073
4074 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
4075 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
4076 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004077
Evan Cheng007ea272009-08-12 05:17:19 +00004078 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00004079 // To "insert" a SELECT_CC instruction, we actually have to insert the
4080 // diamond control-flow pattern. The incoming instruction knows the
4081 // destination vreg to set, the condition code register to branch on, the
4082 // true/false values to select between, and a branch opcode to use.
4083 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004084 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00004085 ++It;
4086
4087 // thisMBB:
4088 // ...
4089 // TrueVal = ...
4090 // cmpTY ccX, r1, r2
4091 // bCC copy1MBB
4092 // fallthrough --> copy0MBB
4093 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004094 MachineFunction *F = BB->getParent();
4095 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
4096 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00004097 F->insert(It, copy0MBB);
4098 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004099
4100 // Transfer the remainder of BB and its successor edges to sinkMBB.
4101 sinkMBB->splice(sinkMBB->begin(), BB,
4102 llvm::next(MachineBasicBlock::iterator(MI)),
4103 BB->end());
4104 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
4105
Dan Gohman258c58c2010-07-06 15:49:48 +00004106 BB->addSuccessor(copy0MBB);
4107 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00004108
Dan Gohman14152b42010-07-06 20:24:04 +00004109 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
4110 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
4111
Evan Chenga8e29892007-01-19 07:51:42 +00004112 // copy0MBB:
4113 // %FalseValue = ...
4114 // # fallthrough to sinkMBB
4115 BB = copy0MBB;
4116
4117 // Update machine-CFG edges
4118 BB->addSuccessor(sinkMBB);
4119
4120 // sinkMBB:
4121 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4122 // ...
4123 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00004124 BuildMI(*BB, BB->begin(), dl,
4125 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00004126 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4127 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4128
Dan Gohman14152b42010-07-06 20:24:04 +00004129 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00004130 return BB;
4131 }
Evan Cheng86198642009-08-07 00:34:42 +00004132
Evan Cheng218977b2010-07-13 19:27:42 +00004133 case ARM::BCCi64:
4134 case ARM::BCCZi64: {
4135 // Compare both parts that make up the double comparison separately for
4136 // equality.
4137 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
4138
4139 unsigned LHS1 = MI->getOperand(1).getReg();
4140 unsigned LHS2 = MI->getOperand(2).getReg();
4141 if (RHSisZero) {
4142 AddDefaultPred(BuildMI(BB, dl,
4143 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
4144 .addReg(LHS1).addImm(0));
4145 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
4146 .addReg(LHS2).addImm(0)
4147 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
4148 } else {
4149 unsigned RHS1 = MI->getOperand(3).getReg();
4150 unsigned RHS2 = MI->getOperand(4).getReg();
4151 AddDefaultPred(BuildMI(BB, dl,
4152 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
4153 .addReg(LHS1).addReg(RHS1));
4154 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
4155 .addReg(LHS2).addReg(RHS2)
4156 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
4157 }
4158
4159 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
4160 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
4161 if (MI->getOperand(0).getImm() == ARMCC::NE)
4162 std::swap(destMBB, exitMBB);
4163
4164 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4165 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
4166 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
4167 .addMBB(exitMBB);
4168
4169 MI->eraseFromParent(); // The pseudo instruction is gone now.
4170 return BB;
4171 }
Evan Chenga8e29892007-01-19 07:51:42 +00004172 }
4173}
4174
4175//===----------------------------------------------------------------------===//
4176// ARM Optimization Hooks
4177//===----------------------------------------------------------------------===//
4178
Chris Lattnerd1980a52009-03-12 06:52:53 +00004179static
4180SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
4181 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00004182 SelectionDAG &DAG = DCI.DAG;
4183 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004184 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00004185 unsigned Opc = N->getOpcode();
4186 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
4187 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
4188 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
4189 ISD::CondCode CC = ISD::SETCC_INVALID;
4190
4191 if (isSlctCC) {
4192 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
4193 } else {
4194 SDValue CCOp = Slct.getOperand(0);
4195 if (CCOp.getOpcode() == ISD::SETCC)
4196 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
4197 }
4198
4199 bool DoXform = false;
4200 bool InvCC = false;
4201 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
4202 "Bad input!");
4203
4204 if (LHS.getOpcode() == ISD::Constant &&
4205 cast<ConstantSDNode>(LHS)->isNullValue()) {
4206 DoXform = true;
4207 } else if (CC != ISD::SETCC_INVALID &&
4208 RHS.getOpcode() == ISD::Constant &&
4209 cast<ConstantSDNode>(RHS)->isNullValue()) {
4210 std::swap(LHS, RHS);
4211 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004212 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00004213 Op0.getOperand(0).getValueType();
4214 bool isInt = OpVT.isInteger();
4215 CC = ISD::getSetCCInverse(CC, isInt);
4216
4217 if (!TLI.isCondCodeLegal(CC, OpVT))
4218 return SDValue(); // Inverse operator isn't legal.
4219
4220 DoXform = true;
4221 InvCC = true;
4222 }
4223
4224 if (DoXform) {
4225 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
4226 if (isSlctCC)
4227 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
4228 Slct.getOperand(0), Slct.getOperand(1), CC);
4229 SDValue CCOp = Slct.getOperand(0);
4230 if (InvCC)
4231 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
4232 CCOp.getOperand(0), CCOp.getOperand(1), CC);
4233 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
4234 CCOp, OtherOp, Result);
4235 }
4236 return SDValue();
4237}
4238
Bob Wilson3d5792a2010-07-29 20:34:14 +00004239/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
4240/// operands N0 and N1. This is a helper for PerformADDCombine that is
4241/// called with the default operands, and if that fails, with commuted
4242/// operands.
4243static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
4244 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson67b453b2010-08-04 00:12:08 +00004245 SelectionDAG &DAG = DCI.DAG;
4246
Chris Lattnerd1980a52009-03-12 06:52:53 +00004247 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
4248 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
4249 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
4250 if (Result.getNode()) return Result;
4251 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00004252
Bob Wilson67b453b2010-08-04 00:12:08 +00004253 // fold (add (arm_neon_vabd a, b) c) -> (arm_neon_vaba c, a, b)
4254 EVT VT = N->getValueType(0);
4255 if (N0.getOpcode() == ISD::INTRINSIC_WO_CHAIN && VT.isInteger()) {
4256 unsigned IntNo = cast<ConstantSDNode>(N0.getOperand(0))->getZExtValue();
4257 if (IntNo == Intrinsic::arm_neon_vabds)
4258 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(), VT,
4259 DAG.getConstant(Intrinsic::arm_neon_vabas, MVT::i32),
4260 N1, N0.getOperand(1), N0.getOperand(2));
4261 if (IntNo == Intrinsic::arm_neon_vabdu)
4262 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(), VT,
4263 DAG.getConstant(Intrinsic::arm_neon_vabau, MVT::i32),
4264 N1, N0.getOperand(1), N0.getOperand(2));
4265 }
4266
Chris Lattnerd1980a52009-03-12 06:52:53 +00004267 return SDValue();
4268}
4269
Bob Wilson3d5792a2010-07-29 20:34:14 +00004270/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
4271///
4272static SDValue PerformADDCombine(SDNode *N,
4273 TargetLowering::DAGCombinerInfo &DCI) {
4274 SDValue N0 = N->getOperand(0);
4275 SDValue N1 = N->getOperand(1);
4276
4277 // First try with the default operand order.
4278 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI);
4279 if (Result.getNode())
4280 return Result;
4281
4282 // If that didn't work, try again with the operands commuted.
4283 return PerformADDCombineWithOperands(N, N1, N0, DCI);
4284}
4285
Chris Lattnerd1980a52009-03-12 06:52:53 +00004286/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00004287///
Chris Lattnerd1980a52009-03-12 06:52:53 +00004288static SDValue PerformSUBCombine(SDNode *N,
4289 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00004290 SDValue N0 = N->getOperand(0);
4291 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00004292
Chris Lattnerd1980a52009-03-12 06:52:53 +00004293 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
4294 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
4295 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
4296 if (Result.getNode()) return Result;
4297 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00004298
Chris Lattnerd1980a52009-03-12 06:52:53 +00004299 return SDValue();
4300}
4301
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004302static SDValue PerformMULCombine(SDNode *N,
4303 TargetLowering::DAGCombinerInfo &DCI,
4304 const ARMSubtarget *Subtarget) {
4305 SelectionDAG &DAG = DCI.DAG;
4306
4307 if (Subtarget->isThumb1Only())
4308 return SDValue();
4309
4310 if (DAG.getMachineFunction().
4311 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
4312 return SDValue();
4313
4314 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
4315 return SDValue();
4316
4317 EVT VT = N->getValueType(0);
4318 if (VT != MVT::i32)
4319 return SDValue();
4320
4321 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
4322 if (!C)
4323 return SDValue();
4324
4325 uint64_t MulAmt = C->getZExtValue();
4326 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
4327 ShiftAmt = ShiftAmt & (32 - 1);
4328 SDValue V = N->getOperand(0);
4329 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004330
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004331 SDValue Res;
4332 MulAmt >>= ShiftAmt;
4333 if (isPowerOf2_32(MulAmt - 1)) {
4334 // (mul x, 2^N + 1) => (add (shl x, N), x)
4335 Res = DAG.getNode(ISD::ADD, DL, VT,
4336 V, DAG.getNode(ISD::SHL, DL, VT,
4337 V, DAG.getConstant(Log2_32(MulAmt-1),
4338 MVT::i32)));
4339 } else if (isPowerOf2_32(MulAmt + 1)) {
4340 // (mul x, 2^N - 1) => (sub (shl x, N), x)
4341 Res = DAG.getNode(ISD::SUB, DL, VT,
4342 DAG.getNode(ISD::SHL, DL, VT,
4343 V, DAG.getConstant(Log2_32(MulAmt+1),
4344 MVT::i32)),
4345 V);
4346 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004347 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004348
4349 if (ShiftAmt != 0)
4350 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
4351 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004352
4353 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004354 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004355 return SDValue();
4356}
4357
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004358/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
4359static SDValue PerformORCombine(SDNode *N,
4360 TargetLowering::DAGCombinerInfo &DCI,
4361 const ARMSubtarget *Subtarget) {
Jim Grosbach54238562010-07-17 03:30:54 +00004362 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
4363 // reasonable.
4364
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004365 // BFI is only available on V6T2+
4366 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
4367 return SDValue();
4368
4369 SelectionDAG &DAG = DCI.DAG;
4370 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Jim Grosbach54238562010-07-17 03:30:54 +00004371 DebugLoc DL = N->getDebugLoc();
4372 // 1) or (and A, mask), val => ARMbfi A, val, mask
4373 // iff (val & mask) == val
4374 //
4375 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
4376 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
4377 // && CountPopulation_32(mask) == CountPopulation_32(~mask2)
4378 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
4379 // && CountPopulation_32(mask) == CountPopulation_32(~mask2)
4380 // (i.e., copy a bitfield value into another bitfield of the same width)
4381 if (N0.getOpcode() != ISD::AND)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004382 return SDValue();
4383
4384 EVT VT = N->getValueType(0);
4385 if (VT != MVT::i32)
4386 return SDValue();
4387
Jim Grosbach54238562010-07-17 03:30:54 +00004388
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004389 // The value and the mask need to be constants so we can verify this is
4390 // actually a bitfield set. If the mask is 0xffff, we can do better
4391 // via a movt instruction, so don't use BFI in that case.
4392 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
4393 if (!C)
4394 return SDValue();
4395 unsigned Mask = C->getZExtValue();
4396 if (Mask == 0xffff)
4397 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004398 SDValue Res;
4399 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
4400 if ((C = dyn_cast<ConstantSDNode>(N1))) {
4401 unsigned Val = C->getZExtValue();
4402 if (!ARM::isBitFieldInvertedMask(Mask) || (Val & ~Mask) != Val)
4403 return SDValue();
4404 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004405
Jim Grosbach54238562010-07-17 03:30:54 +00004406 Res = DAG.getNode(ARMISD::BFI, DL, VT, N0.getOperand(0),
4407 DAG.getConstant(Val, MVT::i32),
4408 DAG.getConstant(Mask, MVT::i32));
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004409
Jim Grosbach54238562010-07-17 03:30:54 +00004410 // Do not add new nodes to DAG combiner worklist.
4411 DCI.CombineTo(N, Res, false);
4412 } else if (N1.getOpcode() == ISD::AND) {
4413 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
4414 C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
4415 if (!C)
4416 return SDValue();
4417 unsigned Mask2 = C->getZExtValue();
4418
4419 if (ARM::isBitFieldInvertedMask(Mask) &&
4420 ARM::isBitFieldInvertedMask(~Mask2) &&
4421 (CountPopulation_32(Mask) == CountPopulation_32(~Mask2))) {
4422 // The pack halfword instruction works better for masks that fit it,
4423 // so use that when it's available.
4424 if (Subtarget->hasT2ExtractPack() &&
4425 (Mask == 0xffff || Mask == 0xffff0000))
4426 return SDValue();
4427 // 2a
4428 unsigned lsb = CountTrailingZeros_32(Mask2);
4429 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
4430 DAG.getConstant(lsb, MVT::i32));
4431 Res = DAG.getNode(ARMISD::BFI, DL, VT, N0.getOperand(0), Res,
4432 DAG.getConstant(Mask, MVT::i32));
4433 // Do not add new nodes to DAG combiner worklist.
4434 DCI.CombineTo(N, Res, false);
4435 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
4436 ARM::isBitFieldInvertedMask(Mask2) &&
4437 (CountPopulation_32(~Mask) == CountPopulation_32(Mask2))) {
4438 // The pack halfword instruction works better for masks that fit it,
4439 // so use that when it's available.
4440 if (Subtarget->hasT2ExtractPack() &&
4441 (Mask2 == 0xffff || Mask2 == 0xffff0000))
4442 return SDValue();
4443 // 2b
4444 unsigned lsb = CountTrailingZeros_32(Mask);
4445 Res = DAG.getNode(ISD::SRL, DL, VT, N0.getOperand(0),
4446 DAG.getConstant(lsb, MVT::i32));
4447 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
4448 DAG.getConstant(Mask2, MVT::i32));
4449 // Do not add new nodes to DAG combiner worklist.
4450 DCI.CombineTo(N, Res, false);
4451 }
4452 }
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004453
4454 return SDValue();
4455}
4456
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +00004457/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
4458/// ARMISD::VMOVRRD.
Jim Grosbache5165492009-11-09 00:11:35 +00004459static SDValue PerformVMOVRRDCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004460 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004461 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00004462 SDValue InDouble = N->getOperand(0);
Jim Grosbache5165492009-11-09 00:11:35 +00004463 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004464 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004465 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004466}
4467
Bob Wilson9e82bf12010-07-14 01:22:12 +00004468/// PerformVDUPLANECombine - Target-specific dag combine xforms for
4469/// ARMISD::VDUPLANE.
4470static SDValue PerformVDUPLANECombine(SDNode *N,
4471 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004472 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
4473 // redundant.
Bob Wilson9e82bf12010-07-14 01:22:12 +00004474 SDValue Op = N->getOperand(0);
4475 EVT VT = N->getValueType(0);
4476
4477 // Ignore bit_converts.
4478 while (Op.getOpcode() == ISD::BIT_CONVERT)
4479 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004480 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00004481 return SDValue();
4482
4483 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
4484 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
4485 // The canonical VMOV for a zero vector uses a 32-bit element size.
4486 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
4487 unsigned EltBits;
4488 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
4489 EltSize = 8;
4490 if (EltSize > VT.getVectorElementType().getSizeInBits())
4491 return SDValue();
4492
4493 SDValue Res = DCI.DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
4494 return DCI.CombineTo(N, Res, false);
4495}
4496
Bob Wilson5bafff32009-06-22 23:27:02 +00004497/// getVShiftImm - Check if this is a valid build_vector for the immediate
4498/// operand of a vector shift operation, where all the elements of the
4499/// build_vector must have the same constant integer value.
4500static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
4501 // Ignore bit_converts.
4502 while (Op.getOpcode() == ISD::BIT_CONVERT)
4503 Op = Op.getOperand(0);
4504 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
4505 APInt SplatBits, SplatUndef;
4506 unsigned SplatBitSize;
4507 bool HasAnyUndefs;
4508 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
4509 HasAnyUndefs, ElementBits) ||
4510 SplatBitSize > ElementBits)
4511 return false;
4512 Cnt = SplatBits.getSExtValue();
4513 return true;
4514}
4515
4516/// isVShiftLImm - Check if this is a valid build_vector for the immediate
4517/// operand of a vector shift left operation. That value must be in the range:
4518/// 0 <= Value < ElementBits for a left shift; or
4519/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00004520static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00004521 assert(VT.isVector() && "vector shift count is not a vector type");
4522 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
4523 if (! getVShiftImm(Op, ElementBits, Cnt))
4524 return false;
4525 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
4526}
4527
4528/// isVShiftRImm - Check if this is a valid build_vector for the immediate
4529/// operand of a vector shift right operation. For a shift opcode, the value
4530/// is positive, but for an intrinsic the value count must be negative. The
4531/// absolute value must be in the range:
4532/// 1 <= |Value| <= ElementBits for a right shift; or
4533/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00004534static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00004535 int64_t &Cnt) {
4536 assert(VT.isVector() && "vector shift count is not a vector type");
4537 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
4538 if (! getVShiftImm(Op, ElementBits, Cnt))
4539 return false;
4540 if (isIntrinsic)
4541 Cnt = -Cnt;
4542 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
4543}
4544
4545/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
4546static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
4547 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
4548 switch (IntNo) {
4549 default:
4550 // Don't do anything for most intrinsics.
4551 break;
4552
4553 // Vector shifts: check for immediate versions and lower them.
4554 // Note: This is done during DAG combining instead of DAG legalizing because
4555 // the build_vectors for 64-bit vector element shift counts are generally
4556 // not legal, and it is hard to see their values after they get legalized to
4557 // loads from a constant pool.
4558 case Intrinsic::arm_neon_vshifts:
4559 case Intrinsic::arm_neon_vshiftu:
4560 case Intrinsic::arm_neon_vshiftls:
4561 case Intrinsic::arm_neon_vshiftlu:
4562 case Intrinsic::arm_neon_vshiftn:
4563 case Intrinsic::arm_neon_vrshifts:
4564 case Intrinsic::arm_neon_vrshiftu:
4565 case Intrinsic::arm_neon_vrshiftn:
4566 case Intrinsic::arm_neon_vqshifts:
4567 case Intrinsic::arm_neon_vqshiftu:
4568 case Intrinsic::arm_neon_vqshiftsu:
4569 case Intrinsic::arm_neon_vqshiftns:
4570 case Intrinsic::arm_neon_vqshiftnu:
4571 case Intrinsic::arm_neon_vqshiftnsu:
4572 case Intrinsic::arm_neon_vqrshiftns:
4573 case Intrinsic::arm_neon_vqrshiftnu:
4574 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00004575 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004576 int64_t Cnt;
4577 unsigned VShiftOpc = 0;
4578
4579 switch (IntNo) {
4580 case Intrinsic::arm_neon_vshifts:
4581 case Intrinsic::arm_neon_vshiftu:
4582 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
4583 VShiftOpc = ARMISD::VSHL;
4584 break;
4585 }
4586 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
4587 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
4588 ARMISD::VSHRs : ARMISD::VSHRu);
4589 break;
4590 }
4591 return SDValue();
4592
4593 case Intrinsic::arm_neon_vshiftls:
4594 case Intrinsic::arm_neon_vshiftlu:
4595 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
4596 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00004597 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004598
4599 case Intrinsic::arm_neon_vrshifts:
4600 case Intrinsic::arm_neon_vrshiftu:
4601 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
4602 break;
4603 return SDValue();
4604
4605 case Intrinsic::arm_neon_vqshifts:
4606 case Intrinsic::arm_neon_vqshiftu:
4607 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
4608 break;
4609 return SDValue();
4610
4611 case Intrinsic::arm_neon_vqshiftsu:
4612 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
4613 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00004614 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004615
4616 case Intrinsic::arm_neon_vshiftn:
4617 case Intrinsic::arm_neon_vrshiftn:
4618 case Intrinsic::arm_neon_vqshiftns:
4619 case Intrinsic::arm_neon_vqshiftnu:
4620 case Intrinsic::arm_neon_vqshiftnsu:
4621 case Intrinsic::arm_neon_vqrshiftns:
4622 case Intrinsic::arm_neon_vqrshiftnu:
4623 case Intrinsic::arm_neon_vqrshiftnsu:
4624 // Narrowing shifts require an immediate right shift.
4625 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
4626 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00004627 llvm_unreachable("invalid shift count for narrowing vector shift "
4628 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004629
4630 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004631 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00004632 }
4633
4634 switch (IntNo) {
4635 case Intrinsic::arm_neon_vshifts:
4636 case Intrinsic::arm_neon_vshiftu:
4637 // Opcode already set above.
4638 break;
4639 case Intrinsic::arm_neon_vshiftls:
4640 case Intrinsic::arm_neon_vshiftlu:
4641 if (Cnt == VT.getVectorElementType().getSizeInBits())
4642 VShiftOpc = ARMISD::VSHLLi;
4643 else
4644 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
4645 ARMISD::VSHLLs : ARMISD::VSHLLu);
4646 break;
4647 case Intrinsic::arm_neon_vshiftn:
4648 VShiftOpc = ARMISD::VSHRN; break;
4649 case Intrinsic::arm_neon_vrshifts:
4650 VShiftOpc = ARMISD::VRSHRs; break;
4651 case Intrinsic::arm_neon_vrshiftu:
4652 VShiftOpc = ARMISD::VRSHRu; break;
4653 case Intrinsic::arm_neon_vrshiftn:
4654 VShiftOpc = ARMISD::VRSHRN; break;
4655 case Intrinsic::arm_neon_vqshifts:
4656 VShiftOpc = ARMISD::VQSHLs; break;
4657 case Intrinsic::arm_neon_vqshiftu:
4658 VShiftOpc = ARMISD::VQSHLu; break;
4659 case Intrinsic::arm_neon_vqshiftsu:
4660 VShiftOpc = ARMISD::VQSHLsu; break;
4661 case Intrinsic::arm_neon_vqshiftns:
4662 VShiftOpc = ARMISD::VQSHRNs; break;
4663 case Intrinsic::arm_neon_vqshiftnu:
4664 VShiftOpc = ARMISD::VQSHRNu; break;
4665 case Intrinsic::arm_neon_vqshiftnsu:
4666 VShiftOpc = ARMISD::VQSHRNsu; break;
4667 case Intrinsic::arm_neon_vqrshiftns:
4668 VShiftOpc = ARMISD::VQRSHRNs; break;
4669 case Intrinsic::arm_neon_vqrshiftnu:
4670 VShiftOpc = ARMISD::VQRSHRNu; break;
4671 case Intrinsic::arm_neon_vqrshiftnsu:
4672 VShiftOpc = ARMISD::VQRSHRNsu; break;
4673 }
4674
4675 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004676 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004677 }
4678
4679 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00004680 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004681 int64_t Cnt;
4682 unsigned VShiftOpc = 0;
4683
4684 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
4685 VShiftOpc = ARMISD::VSLI;
4686 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
4687 VShiftOpc = ARMISD::VSRI;
4688 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00004689 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004690 }
4691
4692 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
4693 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00004694 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004695 }
4696
4697 case Intrinsic::arm_neon_vqrshifts:
4698 case Intrinsic::arm_neon_vqrshiftu:
4699 // No immediate versions of these to check for.
4700 break;
4701 }
4702
4703 return SDValue();
4704}
4705
4706/// PerformShiftCombine - Checks for immediate versions of vector shifts and
4707/// lowers them. As with the vector shift intrinsics, this is done during DAG
4708/// combining instead of DAG legalizing because the build_vectors for 64-bit
4709/// vector element shift counts are generally not legal, and it is hard to see
4710/// their values after they get legalized to loads from a constant pool.
4711static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
4712 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00004713 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00004714
4715 // Nothing to be done for scalar shifts.
4716 if (! VT.isVector())
4717 return SDValue();
4718
4719 assert(ST->hasNEON() && "unexpected vector shift");
4720 int64_t Cnt;
4721
4722 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004723 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00004724
4725 case ISD::SHL:
4726 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
4727 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004728 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004729 break;
4730
4731 case ISD::SRA:
4732 case ISD::SRL:
4733 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
4734 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
4735 ARMISD::VSHRs : ARMISD::VSHRu);
4736 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004737 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004738 }
4739 }
4740 return SDValue();
4741}
4742
4743/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
4744/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
4745static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
4746 const ARMSubtarget *ST) {
4747 SDValue N0 = N->getOperand(0);
4748
4749 // Check for sign- and zero-extensions of vector extract operations of 8-
4750 // and 16-bit vector elements. NEON supports these directly. They are
4751 // handled during DAG combining because type legalization will promote them
4752 // to 32-bit types and it is messy to recognize the operations after that.
4753 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
4754 SDValue Vec = N0.getOperand(0);
4755 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004756 EVT VT = N->getValueType(0);
4757 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004758 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4759
Owen Anderson825b72b2009-08-11 20:47:22 +00004760 if (VT == MVT::i32 &&
4761 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00004762 TLI.isTypeLegal(Vec.getValueType())) {
4763
4764 unsigned Opc = 0;
4765 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004766 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00004767 case ISD::SIGN_EXTEND:
4768 Opc = ARMISD::VGETLANEs;
4769 break;
4770 case ISD::ZERO_EXTEND:
4771 case ISD::ANY_EXTEND:
4772 Opc = ARMISD::VGETLANEu;
4773 break;
4774 }
4775 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
4776 }
4777 }
4778
4779 return SDValue();
4780}
4781
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004782/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
4783/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
4784static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
4785 const ARMSubtarget *ST) {
4786 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00004787 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004788 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
4789 // a NaN; only do the transformation when it matches that behavior.
4790
4791 // For now only do this when using NEON for FP operations; if using VFP, it
4792 // is not obvious that the benefit outweighs the cost of switching to the
4793 // NEON pipeline.
4794 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
4795 N->getValueType(0) != MVT::f32)
4796 return SDValue();
4797
4798 SDValue CondLHS = N->getOperand(0);
4799 SDValue CondRHS = N->getOperand(1);
4800 SDValue LHS = N->getOperand(2);
4801 SDValue RHS = N->getOperand(3);
4802 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
4803
4804 unsigned Opcode = 0;
4805 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00004806 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004807 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00004808 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004809 IsReversed = true ; // x CC y ? y : x
4810 } else {
4811 return SDValue();
4812 }
4813
Bob Wilsone742bb52010-02-24 22:15:53 +00004814 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004815 switch (CC) {
4816 default: break;
4817 case ISD::SETOLT:
4818 case ISD::SETOLE:
4819 case ISD::SETLT:
4820 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004821 case ISD::SETULT:
4822 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004823 // If LHS is NaN, an ordered comparison will be false and the result will
4824 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
4825 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4826 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
4827 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4828 break;
4829 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
4830 // will return -0, so vmin can only be used for unsafe math or if one of
4831 // the operands is known to be nonzero.
4832 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
4833 !UnsafeFPMath &&
4834 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4835 break;
4836 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004837 break;
4838
4839 case ISD::SETOGT:
4840 case ISD::SETOGE:
4841 case ISD::SETGT:
4842 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004843 case ISD::SETUGT:
4844 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004845 // If LHS is NaN, an ordered comparison will be false and the result will
4846 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
4847 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4848 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
4849 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4850 break;
4851 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
4852 // will return +0, so vmax can only be used for unsafe math or if one of
4853 // the operands is known to be nonzero.
4854 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
4855 !UnsafeFPMath &&
4856 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4857 break;
4858 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004859 break;
4860 }
4861
4862 if (!Opcode)
4863 return SDValue();
4864 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
4865}
4866
Dan Gohman475871a2008-07-27 21:46:04 +00004867SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004868 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004869 switch (N->getOpcode()) {
4870 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004871 case ISD::ADD: return PerformADDCombine(N, DCI);
4872 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004873 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004874 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Jim Grosbache5165492009-11-09 00:11:35 +00004875 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson9e82bf12010-07-14 01:22:12 +00004876 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004877 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004878 case ISD::SHL:
4879 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004880 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004881 case ISD::SIGN_EXTEND:
4882 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004883 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
4884 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004885 }
Dan Gohman475871a2008-07-27 21:46:04 +00004886 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004887}
4888
Bill Wendlingaf566342009-08-15 21:21:19 +00004889bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
4890 if (!Subtarget->hasV6Ops())
4891 // Pre-v6 does not support unaligned mem access.
4892 return false;
Bob Wilson86fe66d2010-06-25 04:12:31 +00004893
4894 // v6+ may or may not support unaligned mem access depending on the system
4895 // configuration.
4896 // FIXME: This is pretty conservative. Should we provide cmdline option to
4897 // control the behaviour?
4898 if (!Subtarget->isTargetDarwin())
4899 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00004900
4901 switch (VT.getSimpleVT().SimpleTy) {
4902 default:
4903 return false;
4904 case MVT::i8:
4905 case MVT::i16:
4906 case MVT::i32:
4907 return true;
4908 // FIXME: VLD1 etc with standard alignment is legal.
4909 }
4910}
4911
Evan Chenge6c835f2009-08-14 20:09:37 +00004912static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
4913 if (V < 0)
4914 return false;
4915
4916 unsigned Scale = 1;
4917 switch (VT.getSimpleVT().SimpleTy) {
4918 default: return false;
4919 case MVT::i1:
4920 case MVT::i8:
4921 // Scale == 1;
4922 break;
4923 case MVT::i16:
4924 // Scale == 2;
4925 Scale = 2;
4926 break;
4927 case MVT::i32:
4928 // Scale == 4;
4929 Scale = 4;
4930 break;
4931 }
4932
4933 if ((V & (Scale - 1)) != 0)
4934 return false;
4935 V /= Scale;
4936 return V == (V & ((1LL << 5) - 1));
4937}
4938
4939static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
4940 const ARMSubtarget *Subtarget) {
4941 bool isNeg = false;
4942 if (V < 0) {
4943 isNeg = true;
4944 V = - V;
4945 }
4946
4947 switch (VT.getSimpleVT().SimpleTy) {
4948 default: return false;
4949 case MVT::i1:
4950 case MVT::i8:
4951 case MVT::i16:
4952 case MVT::i32:
4953 // + imm12 or - imm8
4954 if (isNeg)
4955 return V == (V & ((1LL << 8) - 1));
4956 return V == (V & ((1LL << 12) - 1));
4957 case MVT::f32:
4958 case MVT::f64:
4959 // Same as ARM mode. FIXME: NEON?
4960 if (!Subtarget->hasVFP2())
4961 return false;
4962 if ((V & 3) != 0)
4963 return false;
4964 V >>= 2;
4965 return V == (V & ((1LL << 8) - 1));
4966 }
4967}
4968
Evan Chengb01fad62007-03-12 23:30:29 +00004969/// isLegalAddressImmediate - Return true if the integer value can be used
4970/// as the offset of the target addressing mode for load / store of the
4971/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00004972static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004973 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00004974 if (V == 0)
4975 return true;
4976
Evan Cheng65011532009-03-09 19:15:00 +00004977 if (!VT.isSimple())
4978 return false;
4979
Evan Chenge6c835f2009-08-14 20:09:37 +00004980 if (Subtarget->isThumb1Only())
4981 return isLegalT1AddressImmediate(V, VT);
4982 else if (Subtarget->isThumb2())
4983 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00004984
Evan Chenge6c835f2009-08-14 20:09:37 +00004985 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00004986 if (V < 0)
4987 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00004988 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00004989 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004990 case MVT::i1:
4991 case MVT::i8:
4992 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00004993 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004994 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004995 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00004996 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004997 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004998 case MVT::f32:
4999 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00005000 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00005001 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00005002 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00005003 return false;
5004 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00005005 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00005006 }
Evan Chenga8e29892007-01-19 07:51:42 +00005007}
5008
Evan Chenge6c835f2009-08-14 20:09:37 +00005009bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
5010 EVT VT) const {
5011 int Scale = AM.Scale;
5012 if (Scale < 0)
5013 return false;
5014
5015 switch (VT.getSimpleVT().SimpleTy) {
5016 default: return false;
5017 case MVT::i1:
5018 case MVT::i8:
5019 case MVT::i16:
5020 case MVT::i32:
5021 if (Scale == 1)
5022 return true;
5023 // r + r << imm
5024 Scale = Scale & ~1;
5025 return Scale == 2 || Scale == 4 || Scale == 8;
5026 case MVT::i64:
5027 // r + r
5028 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
5029 return true;
5030 return false;
5031 case MVT::isVoid:
5032 // Note, we allow "void" uses (basically, uses that aren't loads or
5033 // stores), because arm allows folding a scale into many arithmetic
5034 // operations. This should be made more precise and revisited later.
5035
5036 // Allow r << imm, but the imm has to be a multiple of two.
5037 if (Scale & 1) return false;
5038 return isPowerOf2_32(Scale);
5039 }
5040}
5041
Chris Lattner37caf8c2007-04-09 23:33:39 +00005042/// isLegalAddressingMode - Return true if the addressing mode represented
5043/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005044bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00005045 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005046 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00005047 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00005048 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005049
Chris Lattner37caf8c2007-04-09 23:33:39 +00005050 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005051 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005052 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005053
Chris Lattner37caf8c2007-04-09 23:33:39 +00005054 switch (AM.Scale) {
5055 case 0: // no scale reg, must be "r+i" or "r", or "i".
5056 break;
5057 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00005058 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00005059 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00005060 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00005061 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00005062 // ARM doesn't support any R+R*scale+imm addr modes.
5063 if (AM.BaseOffs)
5064 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005065
Bob Wilson2c7dab12009-04-08 17:55:28 +00005066 if (!VT.isSimple())
5067 return false;
5068
Evan Chenge6c835f2009-08-14 20:09:37 +00005069 if (Subtarget->isThumb2())
5070 return isLegalT2ScaledAddressingMode(AM, VT);
5071
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005072 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00005073 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00005074 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00005075 case MVT::i1:
5076 case MVT::i8:
5077 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005078 if (Scale < 0) Scale = -Scale;
5079 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005080 return true;
5081 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00005082 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00005083 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00005084 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00005085 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005086 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005087 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00005088 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005089
Owen Anderson825b72b2009-08-11 20:47:22 +00005090 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00005091 // Note, we allow "void" uses (basically, uses that aren't loads or
5092 // stores), because arm allows folding a scale into many arithmetic
5093 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005094
Chris Lattner37caf8c2007-04-09 23:33:39 +00005095 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00005096 if (Scale & 1) return false;
5097 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00005098 }
5099 break;
Evan Chengb01fad62007-03-12 23:30:29 +00005100 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00005101 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00005102}
5103
Evan Cheng77e47512009-11-11 19:05:52 +00005104/// isLegalICmpImmediate - Return true if the specified immediate is legal
5105/// icmp immediate, that is the target has icmp instructions which can compare
5106/// a register against the immediate without having to materialize the
5107/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00005108bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00005109 if (!Subtarget->isThumb())
5110 return ARM_AM::getSOImmVal(Imm) != -1;
5111 if (Subtarget->isThumb2())
5112 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00005113 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00005114}
5115
Owen Andersone50ed302009-08-10 22:56:29 +00005116static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00005117 bool isSEXTLoad, SDValue &Base,
5118 SDValue &Offset, bool &isInc,
5119 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00005120 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
5121 return false;
5122
Owen Anderson825b72b2009-08-11 20:47:22 +00005123 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00005124 // AddressingMode 3
5125 Base = Ptr->getOperand(0);
5126 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005127 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005128 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005129 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00005130 isInc = false;
5131 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5132 return true;
5133 }
5134 }
5135 isInc = (Ptr->getOpcode() == ISD::ADD);
5136 Offset = Ptr->getOperand(1);
5137 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00005138 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00005139 // AddressingMode 2
5140 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005141 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005142 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005143 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00005144 isInc = false;
5145 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5146 Base = Ptr->getOperand(0);
5147 return true;
5148 }
5149 }
5150
5151 if (Ptr->getOpcode() == ISD::ADD) {
5152 isInc = true;
5153 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
5154 if (ShOpcVal != ARM_AM::no_shift) {
5155 Base = Ptr->getOperand(1);
5156 Offset = Ptr->getOperand(0);
5157 } else {
5158 Base = Ptr->getOperand(0);
5159 Offset = Ptr->getOperand(1);
5160 }
5161 return true;
5162 }
5163
5164 isInc = (Ptr->getOpcode() == ISD::ADD);
5165 Base = Ptr->getOperand(0);
5166 Offset = Ptr->getOperand(1);
5167 return true;
5168 }
5169
Jim Grosbache5165492009-11-09 00:11:35 +00005170 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00005171 return false;
5172}
5173
Owen Andersone50ed302009-08-10 22:56:29 +00005174static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00005175 bool isSEXTLoad, SDValue &Base,
5176 SDValue &Offset, bool &isInc,
5177 SelectionDAG &DAG) {
5178 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
5179 return false;
5180
5181 Base = Ptr->getOperand(0);
5182 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
5183 int RHSC = (int)RHS->getZExtValue();
5184 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
5185 assert(Ptr->getOpcode() == ISD::ADD);
5186 isInc = false;
5187 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5188 return true;
5189 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
5190 isInc = Ptr->getOpcode() == ISD::ADD;
5191 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
5192 return true;
5193 }
5194 }
5195
5196 return false;
5197}
5198
Evan Chenga8e29892007-01-19 07:51:42 +00005199/// getPreIndexedAddressParts - returns true by value, base pointer and
5200/// offset pointer and addressing mode by reference if the node's address
5201/// can be legally represented as pre-indexed load / store address.
5202bool
Dan Gohman475871a2008-07-27 21:46:04 +00005203ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
5204 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00005205 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00005206 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005207 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00005208 return false;
5209
Owen Andersone50ed302009-08-10 22:56:29 +00005210 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00005211 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00005212 bool isSEXTLoad = false;
5213 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5214 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005215 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00005216 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
5217 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
5218 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005219 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00005220 } else
5221 return false;
5222
5223 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00005224 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00005225 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00005226 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
5227 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00005228 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00005229 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00005230 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00005231 if (!isLegal)
5232 return false;
5233
5234 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
5235 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00005236}
5237
5238/// getPostIndexedAddressParts - returns true by value, base pointer and
5239/// offset pointer and addressing mode by reference if this node can be
5240/// combined with a load / store to form a post-indexed load / store.
5241bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00005242 SDValue &Base,
5243 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00005244 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00005245 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005246 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00005247 return false;
5248
Owen Andersone50ed302009-08-10 22:56:29 +00005249 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00005250 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00005251 bool isSEXTLoad = false;
5252 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005253 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00005254 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00005255 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
5256 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005257 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00005258 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00005259 } else
5260 return false;
5261
5262 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00005263 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00005264 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00005265 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00005266 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00005267 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00005268 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
5269 isInc, DAG);
5270 if (!isLegal)
5271 return false;
5272
Evan Cheng28dad2a2010-05-18 21:31:17 +00005273 if (Ptr != Base) {
5274 // Swap base ptr and offset to catch more post-index load / store when
5275 // it's legal. In Thumb2 mode, offset must be an immediate.
5276 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
5277 !Subtarget->isThumb2())
5278 std::swap(Base, Offset);
5279
5280 // Post-indexed load / store update the base pointer.
5281 if (Ptr != Base)
5282 return false;
5283 }
5284
Evan Chenge88d5ce2009-07-02 07:28:31 +00005285 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
5286 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00005287}
5288
Dan Gohman475871a2008-07-27 21:46:04 +00005289void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00005290 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00005291 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005292 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00005293 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00005294 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005295 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00005296 switch (Op.getOpcode()) {
5297 default: break;
5298 case ARMISD::CMOV: {
5299 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00005300 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00005301 if (KnownZero == 0 && KnownOne == 0) return;
5302
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005303 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00005304 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
5305 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00005306 KnownZero &= KnownZeroRHS;
5307 KnownOne &= KnownOneRHS;
5308 return;
5309 }
5310 }
5311}
5312
5313//===----------------------------------------------------------------------===//
5314// ARM Inline Assembly Support
5315//===----------------------------------------------------------------------===//
5316
5317/// getConstraintType - Given a constraint letter, return the type of
5318/// constraint it is for this target.
5319ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00005320ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
5321 if (Constraint.size() == 1) {
5322 switch (Constraint[0]) {
5323 default: break;
5324 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005325 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00005326 }
Evan Chenga8e29892007-01-19 07:51:42 +00005327 }
Chris Lattner4234f572007-03-25 02:14:49 +00005328 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00005329}
5330
Bob Wilson2dc4f542009-03-20 22:42:55 +00005331std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00005332ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00005333 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005334 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00005335 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00005336 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005337 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00005338 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00005339 return std::make_pair(0U, ARM::tGPRRegisterClass);
5340 else
5341 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005342 case 'r':
5343 return std::make_pair(0U, ARM::GPRRegisterClass);
5344 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00005345 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005346 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00005347 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005348 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00005349 if (VT.getSizeInBits() == 128)
5350 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005351 break;
Evan Chenga8e29892007-01-19 07:51:42 +00005352 }
5353 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00005354 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00005355 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00005356
Evan Chenga8e29892007-01-19 07:51:42 +00005357 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
5358}
5359
5360std::vector<unsigned> ARMTargetLowering::
5361getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00005362 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005363 if (Constraint.size() != 1)
5364 return std::vector<unsigned>();
5365
5366 switch (Constraint[0]) { // GCC ARM Constraint Letters
5367 default: break;
5368 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00005369 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
5370 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
5371 0);
Evan Chenga8e29892007-01-19 07:51:42 +00005372 case 'r':
5373 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
5374 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
5375 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
5376 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005377 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00005378 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005379 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
5380 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
5381 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
5382 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
5383 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
5384 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
5385 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
5386 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00005387 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005388 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
5389 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
5390 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
5391 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00005392 if (VT.getSizeInBits() == 128)
5393 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
5394 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005395 break;
Evan Chenga8e29892007-01-19 07:51:42 +00005396 }
5397
5398 return std::vector<unsigned>();
5399}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005400
5401/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
5402/// vector. If it is invalid, don't add anything to Ops.
5403void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
5404 char Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005405 std::vector<SDValue>&Ops,
5406 SelectionDAG &DAG) const {
5407 SDValue Result(0, 0);
5408
5409 switch (Constraint) {
5410 default: break;
5411 case 'I': case 'J': case 'K': case 'L':
5412 case 'M': case 'N': case 'O':
5413 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
5414 if (!C)
5415 return;
5416
5417 int64_t CVal64 = C->getSExtValue();
5418 int CVal = (int) CVal64;
5419 // None of these constraints allow values larger than 32 bits. Check
5420 // that the value fits in an int.
5421 if (CVal != CVal64)
5422 return;
5423
5424 switch (Constraint) {
5425 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005426 if (Subtarget->isThumb1Only()) {
5427 // This must be a constant between 0 and 255, for ADD
5428 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005429 if (CVal >= 0 && CVal <= 255)
5430 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005431 } else if (Subtarget->isThumb2()) {
5432 // A constant that can be used as an immediate value in a
5433 // data-processing instruction.
5434 if (ARM_AM::getT2SOImmVal(CVal) != -1)
5435 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005436 } else {
5437 // A constant that can be used as an immediate value in a
5438 // data-processing instruction.
5439 if (ARM_AM::getSOImmVal(CVal) != -1)
5440 break;
5441 }
5442 return;
5443
5444 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005445 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005446 // This must be a constant between -255 and -1, for negated ADD
5447 // immediates. This can be used in GCC with an "n" modifier that
5448 // prints the negated value, for use with SUB instructions. It is
5449 // not useful otherwise but is implemented for compatibility.
5450 if (CVal >= -255 && CVal <= -1)
5451 break;
5452 } else {
5453 // This must be a constant between -4095 and 4095. It is not clear
5454 // what this constraint is intended for. Implemented for
5455 // compatibility with GCC.
5456 if (CVal >= -4095 && CVal <= 4095)
5457 break;
5458 }
5459 return;
5460
5461 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005462 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005463 // A 32-bit value where only one byte has a nonzero value. Exclude
5464 // zero to match GCC. This constraint is used by GCC internally for
5465 // constants that can be loaded with a move/shift combination.
5466 // It is not useful otherwise but is implemented for compatibility.
5467 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
5468 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005469 } else if (Subtarget->isThumb2()) {
5470 // A constant whose bitwise inverse can be used as an immediate
5471 // value in a data-processing instruction. This can be used in GCC
5472 // with a "B" modifier that prints the inverted value, for use with
5473 // BIC and MVN instructions. It is not useful otherwise but is
5474 // implemented for compatibility.
5475 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
5476 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005477 } else {
5478 // A constant whose bitwise inverse can be used as an immediate
5479 // value in a data-processing instruction. This can be used in GCC
5480 // with a "B" modifier that prints the inverted value, for use with
5481 // BIC and MVN instructions. It is not useful otherwise but is
5482 // implemented for compatibility.
5483 if (ARM_AM::getSOImmVal(~CVal) != -1)
5484 break;
5485 }
5486 return;
5487
5488 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005489 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005490 // This must be a constant between -7 and 7,
5491 // for 3-operand ADD/SUB immediate instructions.
5492 if (CVal >= -7 && CVal < 7)
5493 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005494 } else if (Subtarget->isThumb2()) {
5495 // A constant whose negation can be used as an immediate value in a
5496 // data-processing instruction. This can be used in GCC with an "n"
5497 // modifier that prints the negated value, for use with SUB
5498 // instructions. It is not useful otherwise but is implemented for
5499 // compatibility.
5500 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
5501 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005502 } else {
5503 // A constant whose negation can be used as an immediate value in a
5504 // data-processing instruction. This can be used in GCC with an "n"
5505 // modifier that prints the negated value, for use with SUB
5506 // instructions. It is not useful otherwise but is implemented for
5507 // compatibility.
5508 if (ARM_AM::getSOImmVal(-CVal) != -1)
5509 break;
5510 }
5511 return;
5512
5513 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005514 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005515 // This must be a multiple of 4 between 0 and 1020, for
5516 // ADD sp + immediate.
5517 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
5518 break;
5519 } else {
5520 // A power of two or a constant between 0 and 32. This is used in
5521 // GCC for the shift amount on shifted register operands, but it is
5522 // useful in general for any shift amounts.
5523 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
5524 break;
5525 }
5526 return;
5527
5528 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005529 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005530 // This must be a constant between 0 and 31, for shift amounts.
5531 if (CVal >= 0 && CVal <= 31)
5532 break;
5533 }
5534 return;
5535
5536 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005537 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005538 // This must be a multiple of 4 between -508 and 508, for
5539 // ADD/SUB sp = sp + immediate.
5540 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
5541 break;
5542 }
5543 return;
5544 }
5545 Result = DAG.getTargetConstant(CVal, Op.getValueType());
5546 break;
5547 }
5548
5549 if (Result.getNode()) {
5550 Ops.push_back(Result);
5551 return;
5552 }
Dale Johannesen1784d162010-06-25 21:55:36 +00005553 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005554}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00005555
5556bool
5557ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
5558 // The ARM target isn't yet aware of offsets.
5559 return false;
5560}
Evan Cheng39382422009-10-28 01:44:26 +00005561
5562int ARM::getVFPf32Imm(const APFloat &FPImm) {
5563 APInt Imm = FPImm.bitcastToAPInt();
5564 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
5565 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
5566 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
5567
5568 // We can handle 4 bits of mantissa.
5569 // mantissa = (16+UInt(e:f:g:h))/16.
5570 if (Mantissa & 0x7ffff)
5571 return -1;
5572 Mantissa >>= 19;
5573 if ((Mantissa & 0xf) != Mantissa)
5574 return -1;
5575
5576 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
5577 if (Exp < -3 || Exp > 4)
5578 return -1;
5579 Exp = ((Exp+3) & 0x7) ^ 4;
5580
5581 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
5582}
5583
5584int ARM::getVFPf64Imm(const APFloat &FPImm) {
5585 APInt Imm = FPImm.bitcastToAPInt();
5586 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
5587 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
5588 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
5589
5590 // We can handle 4 bits of mantissa.
5591 // mantissa = (16+UInt(e:f:g:h))/16.
5592 if (Mantissa & 0xffffffffffffLL)
5593 return -1;
5594 Mantissa >>= 48;
5595 if ((Mantissa & 0xf) != Mantissa)
5596 return -1;
5597
5598 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
5599 if (Exp < -3 || Exp > 4)
5600 return -1;
5601 Exp = ((Exp+3) & 0x7) ^ 4;
5602
5603 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
5604}
5605
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005606bool ARM::isBitFieldInvertedMask(unsigned v) {
5607 if (v == 0xffffffff)
5608 return 0;
5609 // there can be 1's on either or both "outsides", all the "inside"
5610 // bits must be 0's
5611 unsigned int lsb = 0, msb = 31;
5612 while (v & (1 << msb)) --msb;
5613 while (v & (1 << lsb)) ++lsb;
5614 for (unsigned int i = lsb; i <= msb; ++i) {
5615 if (v & (1 << i))
5616 return 0;
5617 }
5618 return 1;
5619}
5620
Evan Cheng39382422009-10-28 01:44:26 +00005621/// isFPImmLegal - Returns true if the target can instruction select the
5622/// specified FP immediate natively. If false, the legalizer will
5623/// materialize the FP immediate as a load from a constant pool.
5624bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
5625 if (!Subtarget->hasVFP3())
5626 return false;
5627 if (VT == MVT::f32)
5628 return ARM::getVFPf32Imm(Imm) != -1;
5629 if (VT == MVT::f64)
5630 return ARM::getVFPf64Imm(Imm) != -1;
5631 return false;
5632}