blob: 08054cbae322a18c595cb1d65b9300e38cdfd8de [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Dale Johannesen51e28e62010-06-03 21:09:53 +000015#define DEBUG_TYPE "arm-isel"
Evan Chenga8e29892007-01-19 07:51:42 +000016#include "ARM.h"
17#include "ARMAddressingModes.h"
18#include "ARMConstantPoolValue.h"
19#include "ARMISelLowering.h"
20#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000021#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000022#include "ARMRegisterInfo.h"
23#include "ARMSubtarget.h"
24#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000025#include "ARMTargetObjectFile.h"
Evan Chenga8e29892007-01-19 07:51:42 +000026#include "llvm/CallingConv.h"
27#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000028#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000029#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000030#include "llvm/Instruction.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000031#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000032#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000033#include "llvm/CodeGen/CallingConvLower.h"
Evan Chenga8e29892007-01-19 07:51:42 +000034#include "llvm/CodeGen/MachineBasicBlock.h"
35#include "llvm/CodeGen/MachineFrameInfo.h"
36#include "llvm/CodeGen/MachineFunction.h"
37#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000038#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000039#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000040#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000041#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000042#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000043#include "llvm/ADT/VectorExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000044#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000045#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000046#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000047#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000048#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000049#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000050using namespace llvm;
51
Dale Johannesen51e28e62010-06-03 21:09:53 +000052STATISTIC(NumTailCalls, "Number of tail calls");
53
54// This option should go away when tail calls fully work.
55static cl::opt<bool>
56EnableARMTailCalls("arm-tail-calls", cl::Hidden,
57 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
Dale Johannesenc66cdf72010-06-18 19:00:18 +000058 cl::init(true));
Dale Johannesen51e28e62010-06-03 21:09:53 +000059
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 Wilson5bafff32009-06-22 23:27:02 +0000132 }
133
134 // Promote all bit-wise operations.
135 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000136 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000137 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
138 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000139 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000140 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000141 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000142 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000143 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000144 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000145 }
Bob Wilson16330762009-09-16 00:17:28 +0000146
147 // Neon does not support vector divide/remainder operations.
148 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
149 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
150 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
151 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
152 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
153 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000154}
155
Owen Andersone50ed302009-08-10 22:56:29 +0000156void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000157 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000158 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000159}
160
Owen Andersone50ed302009-08-10 22:56:29 +0000161void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000162 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000163 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000164}
165
Chris Lattnerf0144122009-07-28 03:13:23 +0000166static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
167 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000168 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000169
Chris Lattner80ec2792009-08-02 00:34:36 +0000170 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000171}
172
Evan Chenga8e29892007-01-19 07:51:42 +0000173ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000174 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000175 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000176 RegInfo = TM.getRegisterInfo();
Evan Chenga8e29892007-01-19 07:51:42 +0000177
Evan Chengb1df8f22007-04-27 08:15:43 +0000178 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000179 // Uses VFP for Thumb libfuncs if available.
180 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
181 // Single-precision floating-point arithmetic.
182 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
183 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
184 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
185 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000186
Evan Chengb1df8f22007-04-27 08:15:43 +0000187 // Double-precision floating-point arithmetic.
188 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
189 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
190 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
191 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000192
Evan Chengb1df8f22007-04-27 08:15:43 +0000193 // Single-precision comparisons.
194 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
195 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
196 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
197 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
198 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
199 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
200 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
201 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000202
Evan Chengb1df8f22007-04-27 08:15:43 +0000203 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
204 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
205 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
206 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
207 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000211
Evan Chengb1df8f22007-04-27 08:15:43 +0000212 // Double-precision comparisons.
213 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
214 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
215 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
216 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
217 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
218 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
219 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
220 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000221
Evan Chengb1df8f22007-04-27 08:15:43 +0000222 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
223 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
224 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
225 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
226 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
227 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
228 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
229 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000230
Evan Chengb1df8f22007-04-27 08:15:43 +0000231 // Floating-point to integer conversions.
232 // i64 conversions are done via library routines even when generating VFP
233 // instructions, so use the same ones.
234 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
235 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
236 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
237 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000238
Evan Chengb1df8f22007-04-27 08:15:43 +0000239 // Conversions between floating types.
240 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
241 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
242
243 // Integer to floating-point conversions.
244 // i64 conversions are done via library routines even when generating VFP
245 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000246 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
247 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000248 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
249 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
250 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
251 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
252 }
Evan Chenga8e29892007-01-19 07:51:42 +0000253 }
254
Bob Wilson2f954612009-05-22 17:38:41 +0000255 // These libcalls are not available in 32-bit.
256 setLibcallName(RTLIB::SHL_I128, 0);
257 setLibcallName(RTLIB::SRL_I128, 0);
258 setLibcallName(RTLIB::SRA_I128, 0);
259
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000260 // Libcalls should use the AAPCS base standard ABI, even if hard float
261 // is in effect, as per the ARM RTABI specification, section 4.1.2.
262 if (Subtarget->isAAPCS_ABI()) {
263 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
264 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
265 CallingConv::ARM_AAPCS);
266 }
267 }
268
David Goodwinf1daf7d2009-07-08 23:10:31 +0000269 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000271 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000273 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000274 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
275 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000276
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000278 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000279
280 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 addDRTypeForNEON(MVT::v2f32);
282 addDRTypeForNEON(MVT::v8i8);
283 addDRTypeForNEON(MVT::v4i16);
284 addDRTypeForNEON(MVT::v2i32);
285 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000286
Owen Anderson825b72b2009-08-11 20:47:22 +0000287 addQRTypeForNEON(MVT::v4f32);
288 addQRTypeForNEON(MVT::v2f64);
289 addQRTypeForNEON(MVT::v16i8);
290 addQRTypeForNEON(MVT::v8i16);
291 addQRTypeForNEON(MVT::v4i32);
292 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000293
Bob Wilson74dc72e2009-09-15 23:55:57 +0000294 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
295 // neither Neon nor VFP support any arithmetic operations on it.
296 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
297 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
298 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
299 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
300 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
301 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
302 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
303 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
304 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
305 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
306 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
307 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
308 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
309 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
310 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
311 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
312 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
313 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
314 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
315 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
316 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
317 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
318 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
319 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
320
Bob Wilson642b3292009-09-16 00:32:15 +0000321 // Neon does not support some operations on v1i64 and v2i64 types.
322 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
323 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
324 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
325 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
326
Bob Wilson5bafff32009-06-22 23:27:02 +0000327 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
328 setTargetDAGCombine(ISD::SHL);
329 setTargetDAGCombine(ISD::SRL);
330 setTargetDAGCombine(ISD::SRA);
331 setTargetDAGCombine(ISD::SIGN_EXTEND);
332 setTargetDAGCombine(ISD::ZERO_EXTEND);
333 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000334 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson5bafff32009-06-22 23:27:02 +0000335 }
336
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000337 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000338
339 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000340 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000341
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000342 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000344
Evan Chenga8e29892007-01-19 07:51:42 +0000345 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000346 if (!Subtarget->isThumb1Only()) {
347 for (unsigned im = (unsigned)ISD::PRE_INC;
348 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000349 setIndexedLoadAction(im, MVT::i1, Legal);
350 setIndexedLoadAction(im, MVT::i8, Legal);
351 setIndexedLoadAction(im, MVT::i16, Legal);
352 setIndexedLoadAction(im, MVT::i32, Legal);
353 setIndexedStoreAction(im, MVT::i1, Legal);
354 setIndexedStoreAction(im, MVT::i8, Legal);
355 setIndexedStoreAction(im, MVT::i16, Legal);
356 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000357 }
Evan Chenga8e29892007-01-19 07:51:42 +0000358 }
359
360 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000361 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000362 setOperationAction(ISD::MUL, MVT::i64, Expand);
363 setOperationAction(ISD::MULHU, MVT::i32, Expand);
364 setOperationAction(ISD::MULHS, MVT::i32, Expand);
365 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
366 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000367 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000368 setOperationAction(ISD::MUL, MVT::i64, Expand);
369 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000370 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000372 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000373 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000374 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000375 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000376 setOperationAction(ISD::SRL, MVT::i64, Custom);
377 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000378
379 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000380 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000381 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000383 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000384 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000385
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000386 // Only ARMv6 has BSWAP.
387 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000388 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000389
Evan Chenga8e29892007-01-19 07:51:42 +0000390 // These are expanded into libcalls.
Jim Grosbach29402132010-05-05 23:44:43 +0000391 if (!Subtarget->hasDivide()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000392 // v7M has a hardware divider
393 setOperationAction(ISD::SDIV, MVT::i32, Expand);
394 setOperationAction(ISD::UDIV, MVT::i32, Expand);
395 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000396 setOperationAction(ISD::SREM, MVT::i32, Expand);
397 setOperationAction(ISD::UREM, MVT::i32, Expand);
398 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
399 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000400
Owen Anderson825b72b2009-08-11 20:47:22 +0000401 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
402 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
403 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
404 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000405 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000406
Evan Chengfb3611d2010-05-11 07:26:32 +0000407 setOperationAction(ISD::TRAP, MVT::Other, Legal);
408
Evan Chenga8e29892007-01-19 07:51:42 +0000409 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000410 setOperationAction(ISD::VASTART, MVT::Other, Custom);
411 setOperationAction(ISD::VAARG, MVT::Other, Expand);
412 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
413 setOperationAction(ISD::VAEND, MVT::Other, Expand);
414 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
415 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000416 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
417 // FIXME: Shouldn't need this, since no register is used, but the legalizer
418 // doesn't yet know how to not do that for SjLj.
419 setExceptionSelectorRegister(ARM::R0);
Evan Cheng3a1588a2010-04-15 22:20:34 +0000420 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Jim Grosbach7072cf62010-06-17 02:02:03 +0000421 // Handle atomics directly for ARMv[67] (except for Thumb1), otherwise
422 // use the default expansion.
Jim Grosbach68741be2010-06-18 22:35:32 +0000423 bool canHandleAtomics =
Jim Grosbach7072cf62010-06-17 02:02:03 +0000424 (Subtarget->hasV7Ops() ||
Jim Grosbach68741be2010-06-18 22:35:32 +0000425 (Subtarget->hasV6Ops() && !Subtarget->isThumb1Only()));
426 if (canHandleAtomics) {
427 // membarrier needs custom lowering; the rest are legal and handled
428 // normally.
429 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
430 } else {
431 // Set them all for expansion, which will force libcalls.
432 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
433 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Expand);
434 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Expand);
435 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000436 setOperationAction(ISD::ATOMIC_SWAP, MVT::i8, Expand);
437 setOperationAction(ISD::ATOMIC_SWAP, MVT::i16, Expand);
438 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000439 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i8, Expand);
440 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i16, Expand);
441 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
442 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Expand);
443 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Expand);
444 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
445 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i8, Expand);
446 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i16, Expand);
447 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
448 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i8, Expand);
449 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i16, Expand);
450 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
451 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i8, Expand);
452 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i16, Expand);
453 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
454 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i8, Expand);
455 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i16, Expand);
456 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000457 // Since the libcalls include locking, fold in the fences
458 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000459 }
460 // 64-bit versions are always libcalls (for now)
461 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000462 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000463 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Expand);
464 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Expand);
465 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Expand);
466 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Expand);
467 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Expand);
468 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000469
Eli Friedmana2c6f452010-06-26 04:36:50 +0000470 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
471 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000472 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
473 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000474 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000475 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000476
Nate Begemand1fb5832010-08-03 21:31:55 +0000477 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000478 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
479 // iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000480 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000481 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
482 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000483
484 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000485 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000486 if (Subtarget->isTargetDarwin()) {
487 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
488 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
489 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000490
Owen Anderson825b72b2009-08-11 20:47:22 +0000491 setOperationAction(ISD::SETCC, MVT::i32, Expand);
492 setOperationAction(ISD::SETCC, MVT::f32, Expand);
493 setOperationAction(ISD::SETCC, MVT::f64, Expand);
494 setOperationAction(ISD::SELECT, MVT::i32, Expand);
495 setOperationAction(ISD::SELECT, MVT::f32, Expand);
496 setOperationAction(ISD::SELECT, MVT::f64, Expand);
497 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
498 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
499 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000500
Owen Anderson825b72b2009-08-11 20:47:22 +0000501 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
502 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
503 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
504 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
505 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000506
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000507 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000508 setOperationAction(ISD::FSIN, MVT::f64, Expand);
509 setOperationAction(ISD::FSIN, MVT::f32, Expand);
510 setOperationAction(ISD::FCOS, MVT::f32, Expand);
511 setOperationAction(ISD::FCOS, MVT::f64, Expand);
512 setOperationAction(ISD::FREM, MVT::f64, Expand);
513 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000514 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000515 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
516 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000517 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000518 setOperationAction(ISD::FPOW, MVT::f64, Expand);
519 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000520
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000521 // Various VFP goodness
522 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000523 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
524 if (Subtarget->hasVFP2()) {
525 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
526 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
527 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
528 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
529 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000530 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000531 if (!Subtarget->hasFP16()) {
532 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
533 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000534 }
Evan Cheng110cf482008-04-01 01:50:16 +0000535 }
Evan Chenga8e29892007-01-19 07:51:42 +0000536
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000537 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000538 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000539 setTargetDAGCombine(ISD::ADD);
540 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000541 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000542
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000543 if (Subtarget->hasV6T2Ops())
544 setTargetDAGCombine(ISD::OR);
545
Evan Chenga8e29892007-01-19 07:51:42 +0000546 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000547
Evan Chengf7d87ee2010-05-21 00:43:17 +0000548 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
549 setSchedulingPreference(Sched::RegPressure);
550 else
551 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000552
553 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Evan Chengf6799392010-06-26 01:52:05 +0000554
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000555 // On ARM arguments smaller than 4 bytes are extended, so all arguments
556 // are at least 4 bytes aligned.
557 setMinStackArgumentAlignment(4);
558
Evan Chengf6799392010-06-26 01:52:05 +0000559 if (EnableARMCodePlacement)
560 benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000561}
562
Evan Cheng4f6b4672010-07-21 06:09:07 +0000563std::pair<const TargetRegisterClass*, uint8_t>
564ARMTargetLowering::findRepresentativeClass(EVT VT) const{
565 const TargetRegisterClass *RRC = 0;
566 uint8_t Cost = 1;
567 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000568 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000569 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000570 // Use DPR as representative register class for all floating point
571 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
572 // the cost is 1 for both f32 and f64.
573 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000574 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000575 RRC = ARM::DPRRegisterClass;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000576 break;
577 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
578 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000579 RRC = ARM::DPRRegisterClass;
580 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000581 break;
582 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000583 RRC = ARM::DPRRegisterClass;
584 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000585 break;
586 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000587 RRC = ARM::DPRRegisterClass;
588 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000589 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000590 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000591 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000592}
593
Evan Chenga8e29892007-01-19 07:51:42 +0000594const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
595 switch (Opcode) {
596 default: return 0;
597 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000598 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
599 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000600 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000601 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
602 case ARMISD::tCALL: return "ARMISD::tCALL";
603 case ARMISD::BRCOND: return "ARMISD::BRCOND";
604 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000605 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000606 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
607 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
608 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000609 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000610 case ARMISD::CMPFP: return "ARMISD::CMPFP";
611 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000612 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000613 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
614 case ARMISD::CMOV: return "ARMISD::CMOV";
615 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000616
Jim Grosbach3482c802010-01-18 19:58:49 +0000617 case ARMISD::RBIT: return "ARMISD::RBIT";
618
Bob Wilson76a312b2010-03-19 22:51:32 +0000619 case ARMISD::FTOSI: return "ARMISD::FTOSI";
620 case ARMISD::FTOUI: return "ARMISD::FTOUI";
621 case ARMISD::SITOF: return "ARMISD::SITOF";
622 case ARMISD::UITOF: return "ARMISD::UITOF";
623
Evan Chenga8e29892007-01-19 07:51:42 +0000624 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
625 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
626 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000627
Jim Grosbache5165492009-11-09 00:11:35 +0000628 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
629 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000630
Evan Chengc5942082009-10-28 06:55:03 +0000631 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
632 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
633
Dale Johannesen51e28e62010-06-03 21:09:53 +0000634 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
635
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000636 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000637
Evan Cheng86198642009-08-07 00:34:42 +0000638 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
639
Jim Grosbach3728e962009-12-10 00:11:09 +0000640 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
641 case ARMISD::SYNCBARRIER: return "ARMISD::SYNCBARRIER";
642
Bob Wilson5bafff32009-06-22 23:27:02 +0000643 case ARMISD::VCEQ: return "ARMISD::VCEQ";
644 case ARMISD::VCGE: return "ARMISD::VCGE";
645 case ARMISD::VCGEU: return "ARMISD::VCGEU";
646 case ARMISD::VCGT: return "ARMISD::VCGT";
647 case ARMISD::VCGTU: return "ARMISD::VCGTU";
648 case ARMISD::VTST: return "ARMISD::VTST";
649
650 case ARMISD::VSHL: return "ARMISD::VSHL";
651 case ARMISD::VSHRs: return "ARMISD::VSHRs";
652 case ARMISD::VSHRu: return "ARMISD::VSHRu";
653 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
654 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
655 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
656 case ARMISD::VSHRN: return "ARMISD::VSHRN";
657 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
658 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
659 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
660 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
661 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
662 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
663 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
664 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
665 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
666 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
667 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
668 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
669 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
670 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000671 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000672 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000673 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000674 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000675 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000676 case ARMISD::VREV64: return "ARMISD::VREV64";
677 case ARMISD::VREV32: return "ARMISD::VREV32";
678 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000679 case ARMISD::VZIP: return "ARMISD::VZIP";
680 case ARMISD::VUZP: return "ARMISD::VUZP";
681 case ARMISD::VTRN: return "ARMISD::VTRN";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000682 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000683 case ARMISD::FMAX: return "ARMISD::FMAX";
684 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000685 case ARMISD::BFI: return "ARMISD::BFI";
Evan Chenga8e29892007-01-19 07:51:42 +0000686 }
687}
688
Evan Cheng06b666c2010-05-15 02:18:07 +0000689/// getRegClassFor - Return the register class that should be used for the
690/// specified value type.
691TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
692 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
693 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
694 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000695 if (Subtarget->hasNEON()) {
696 if (VT == MVT::v4i64)
697 return ARM::QQPRRegisterClass;
698 else if (VT == MVT::v8i64)
699 return ARM::QQQQPRRegisterClass;
700 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000701 return TargetLowering::getRegClassFor(VT);
702}
703
Eric Christopherab695882010-07-21 22:26:11 +0000704// Create a fast isel object.
705FastISel *
706ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
707 return ARM::createFastISel(funcInfo);
708}
709
Bill Wendlingb4202b82009-07-01 18:50:55 +0000710/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000711unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Bob Wilsonb5b50572010-07-01 22:26:26 +0000712 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 1 : 2;
Bill Wendling20c568f2009-06-30 22:38:32 +0000713}
714
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000715/// getMaximalGlobalOffset - Returns the maximal possible offset which can
716/// be used for loads / stores from the global.
717unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
718 return (Subtarget->isThumb1Only() ? 127 : 4095);
719}
720
Evan Cheng1cc39842010-05-20 23:26:43 +0000721Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000722 unsigned NumVals = N->getNumValues();
723 if (!NumVals)
724 return Sched::RegPressure;
725
726 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000727 EVT VT = N->getValueType(i);
728 if (VT.isFloatingPoint() || VT.isVector())
729 return Sched::Latency;
730 }
Evan Chengc10f5432010-05-28 23:25:23 +0000731
732 if (!N->isMachineOpcode())
733 return Sched::RegPressure;
734
735 // Load are scheduled for latency even if there instruction itinerary
736 // is not available.
737 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
738 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
739 if (TID.mayLoad())
740 return Sched::Latency;
741
742 const InstrItineraryData &Itins = getTargetMachine().getInstrItineraryData();
743 if (!Itins.isEmpty() && Itins.getStageLatency(TID.getSchedClass()) > 2)
744 return Sched::Latency;
Evan Cheng1cc39842010-05-20 23:26:43 +0000745 return Sched::RegPressure;
746}
747
Evan Cheng31446872010-07-23 22:39:59 +0000748unsigned
749ARMTargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
750 MachineFunction &MF) const {
751 unsigned FPDiff = RegInfo->hasFP(MF) ? 1 : 0;
752 switch (RC->getID()) {
753 default:
754 return 0;
755 case ARM::tGPRRegClassID:
756 return 5 - FPDiff;
757 case ARM::GPRRegClassID:
758 return 10 - FPDiff - (Subtarget->isR9Reserved() ? 1 : 0);
759 case ARM::SPRRegClassID: // Currently not used as 'rep' register class.
760 case ARM::DPRRegClassID:
761 return 32 - 10;
762 }
763}
764
Evan Chenga8e29892007-01-19 07:51:42 +0000765//===----------------------------------------------------------------------===//
766// Lowering Code
767//===----------------------------------------------------------------------===//
768
Evan Chenga8e29892007-01-19 07:51:42 +0000769/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
770static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
771 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000772 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000773 case ISD::SETNE: return ARMCC::NE;
774 case ISD::SETEQ: return ARMCC::EQ;
775 case ISD::SETGT: return ARMCC::GT;
776 case ISD::SETGE: return ARMCC::GE;
777 case ISD::SETLT: return ARMCC::LT;
778 case ISD::SETLE: return ARMCC::LE;
779 case ISD::SETUGT: return ARMCC::HI;
780 case ISD::SETUGE: return ARMCC::HS;
781 case ISD::SETULT: return ARMCC::LO;
782 case ISD::SETULE: return ARMCC::LS;
783 }
784}
785
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000786/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
787static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000788 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000789 CondCode2 = ARMCC::AL;
790 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000791 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000792 case ISD::SETEQ:
793 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
794 case ISD::SETGT:
795 case ISD::SETOGT: CondCode = ARMCC::GT; break;
796 case ISD::SETGE:
797 case ISD::SETOGE: CondCode = ARMCC::GE; break;
798 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000799 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000800 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
801 case ISD::SETO: CondCode = ARMCC::VC; break;
802 case ISD::SETUO: CondCode = ARMCC::VS; break;
803 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
804 case ISD::SETUGT: CondCode = ARMCC::HI; break;
805 case ISD::SETUGE: CondCode = ARMCC::PL; break;
806 case ISD::SETLT:
807 case ISD::SETULT: CondCode = ARMCC::LT; break;
808 case ISD::SETLE:
809 case ISD::SETULE: CondCode = ARMCC::LE; break;
810 case ISD::SETNE:
811 case ISD::SETUNE: CondCode = ARMCC::NE; break;
812 }
Evan Chenga8e29892007-01-19 07:51:42 +0000813}
814
Bob Wilson1f595bb2009-04-17 19:07:39 +0000815//===----------------------------------------------------------------------===//
816// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000817//===----------------------------------------------------------------------===//
818
819#include "ARMGenCallingConv.inc"
820
821// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000822static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000823 CCValAssign::LocInfo &LocInfo,
824 CCState &State, bool CanFail) {
825 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
826
827 // Try to get the first register.
828 if (unsigned Reg = State.AllocateReg(RegList, 4))
829 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
830 else {
831 // For the 2nd half of a v2f64, do not fail.
832 if (CanFail)
833 return false;
834
835 // Put the whole thing on the stack.
836 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
837 State.AllocateStack(8, 4),
838 LocVT, LocInfo));
839 return true;
840 }
841
842 // Try to get the second register.
843 if (unsigned Reg = State.AllocateReg(RegList, 4))
844 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
845 else
846 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
847 State.AllocateStack(4, 4),
848 LocVT, LocInfo));
849 return true;
850}
851
Owen Andersone50ed302009-08-10 22:56:29 +0000852static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000853 CCValAssign::LocInfo &LocInfo,
854 ISD::ArgFlagsTy &ArgFlags,
855 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000856 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
857 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000858 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000859 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
860 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000861 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000862}
863
864// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000865static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000866 CCValAssign::LocInfo &LocInfo,
867 CCState &State, bool CanFail) {
868 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
869 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
Rafael Espindolabc565012010-07-21 11:38:30 +0000870 static const unsigned ShadowRegList[] = { ARM::R0, ARM::R1 };
Bob Wilson5bafff32009-06-22 23:27:02 +0000871
Rafael Espindolabc565012010-07-21 11:38:30 +0000872 unsigned Reg = State.AllocateReg(HiRegList, ShadowRegList, 2);
Bob Wilson5bafff32009-06-22 23:27:02 +0000873 if (Reg == 0) {
874 // For the 2nd half of a v2f64, do not just fail.
875 if (CanFail)
876 return false;
877
878 // Put the whole thing on the stack.
879 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
880 State.AllocateStack(8, 8),
881 LocVT, LocInfo));
882 return true;
883 }
884
885 unsigned i;
886 for (i = 0; i < 2; ++i)
887 if (HiRegList[i] == Reg)
888 break;
889
Rafael Espindolabc565012010-07-21 11:38:30 +0000890 unsigned T = State.AllocateReg(LoRegList[i]);
Chandler Carruth30d35b82010-07-22 08:02:25 +0000891 (void)T;
Rafael Espindolabc565012010-07-21 11:38:30 +0000892 assert(T == LoRegList[i] && "Could not allocate register");
893
Bob Wilson5bafff32009-06-22 23:27:02 +0000894 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
895 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
896 LocVT, LocInfo));
897 return true;
898}
899
Owen Andersone50ed302009-08-10 22:56:29 +0000900static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000901 CCValAssign::LocInfo &LocInfo,
902 ISD::ArgFlagsTy &ArgFlags,
903 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000904 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
905 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000906 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000907 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
908 return false;
909 return true; // we handled it
910}
911
Owen Andersone50ed302009-08-10 22:56:29 +0000912static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000913 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000914 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
915 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
916
Bob Wilsone65586b2009-04-17 20:40:45 +0000917 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
918 if (Reg == 0)
919 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000920
Bob Wilsone65586b2009-04-17 20:40:45 +0000921 unsigned i;
922 for (i = 0; i < 2; ++i)
923 if (HiRegList[i] == Reg)
924 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000925
Bob Wilson5bafff32009-06-22 23:27:02 +0000926 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000927 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000928 LocVT, LocInfo));
929 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000930}
931
Owen Andersone50ed302009-08-10 22:56:29 +0000932static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000933 CCValAssign::LocInfo &LocInfo,
934 ISD::ArgFlagsTy &ArgFlags,
935 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000936 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
937 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000938 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000939 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000940 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000941}
942
Owen Andersone50ed302009-08-10 22:56:29 +0000943static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000944 CCValAssign::LocInfo &LocInfo,
945 ISD::ArgFlagsTy &ArgFlags,
946 CCState &State) {
947 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
948 State);
949}
950
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000951/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
952/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000953CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000954 bool Return,
955 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000956 switch (CC) {
957 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000958 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000959 case CallingConv::C:
960 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000961 // Use target triple & subtarget features to do actual dispatch.
962 if (Subtarget->isAAPCS_ABI()) {
963 if (Subtarget->hasVFP2() &&
964 FloatABIType == FloatABI::Hard && !isVarArg)
965 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
966 else
967 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
968 } else
969 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000970 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000971 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000972 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000973 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000974 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000975 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000976 }
977}
978
Dan Gohman98ca4f22009-08-05 01:29:28 +0000979/// LowerCallResult - Lower the result values of a call into the
980/// appropriate copies out of appropriate physical registers.
981SDValue
982ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000983 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000984 const SmallVectorImpl<ISD::InputArg> &Ins,
985 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000986 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000987
Bob Wilson1f595bb2009-04-17 19:07:39 +0000988 // Assign locations to each value returned by this call.
989 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000990 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000991 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000992 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000993 CCAssignFnForNode(CallConv, /* Return*/ true,
994 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000995
996 // Copy all of the result registers out of their specified physreg.
997 for (unsigned i = 0; i != RVLocs.size(); ++i) {
998 CCValAssign VA = RVLocs[i];
999
Bob Wilson80915242009-04-25 00:33:20 +00001000 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001001 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001002 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001003 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001004 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001005 Chain = Lo.getValue(1);
1006 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001007 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001008 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001009 InFlag);
1010 Chain = Hi.getValue(1);
1011 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001012 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001013
Owen Anderson825b72b2009-08-11 20:47:22 +00001014 if (VA.getLocVT() == MVT::v2f64) {
1015 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1016 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1017 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001018
1019 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001020 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001021 Chain = Lo.getValue(1);
1022 InFlag = Lo.getValue(2);
1023 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001024 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001025 Chain = Hi.getValue(1);
1026 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001027 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001028 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1029 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001030 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001031 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001032 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1033 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001034 Chain = Val.getValue(1);
1035 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001036 }
Bob Wilson80915242009-04-25 00:33:20 +00001037
1038 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001039 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001040 case CCValAssign::Full: break;
1041 case CCValAssign::BCvt:
1042 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
1043 break;
1044 }
1045
Dan Gohman98ca4f22009-08-05 01:29:28 +00001046 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001047 }
1048
Dan Gohman98ca4f22009-08-05 01:29:28 +00001049 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001050}
1051
1052/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1053/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +00001054/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +00001055/// a byval function parameter.
1056/// Sometimes what we are copying is the end of a larger object, the part that
1057/// does not fit in registers.
1058static SDValue
1059CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
1060 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1061 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001062 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001063 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001064 /*isVolatile=*/false, /*AlwaysInline=*/false,
1065 NULL, 0, NULL, 0);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001066}
1067
Bob Wilsondee46d72009-04-17 20:35:10 +00001068/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001069SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001070ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1071 SDValue StackPtr, SDValue Arg,
1072 DebugLoc dl, SelectionDAG &DAG,
1073 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001074 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001075 unsigned LocMemOffset = VA.getLocMemOffset();
1076 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1077 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
1078 if (Flags.isByVal()) {
1079 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
1080 }
1081 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene1b58cab2010-02-15 16:55:24 +00001082 PseudoSourceValue::getStack(), LocMemOffset,
1083 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001084}
1085
Dan Gohman98ca4f22009-08-05 01:29:28 +00001086void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001087 SDValue Chain, SDValue &Arg,
1088 RegsToPassVector &RegsToPass,
1089 CCValAssign &VA, CCValAssign &NextVA,
1090 SDValue &StackPtr,
1091 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001092 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001093
Jim Grosbache5165492009-11-09 00:11:35 +00001094 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001095 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001096 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1097
1098 if (NextVA.isRegLoc())
1099 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1100 else {
1101 assert(NextVA.isMemLoc());
1102 if (StackPtr.getNode() == 0)
1103 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1104
Dan Gohman98ca4f22009-08-05 01:29:28 +00001105 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1106 dl, DAG, NextVA,
1107 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001108 }
1109}
1110
Dan Gohman98ca4f22009-08-05 01:29:28 +00001111/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001112/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1113/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001114SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001115ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001116 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001117 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001118 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001119 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001120 const SmallVectorImpl<ISD::InputArg> &Ins,
1121 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001122 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001123 MachineFunction &MF = DAG.getMachineFunction();
1124 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1125 bool IsSibCall = false;
Dale Johannesen8fa8e7f2010-06-04 18:04:24 +00001126 // Temporarily disable tail calls so things don't break.
1127 if (!EnableARMTailCalls)
1128 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001129 if (isTailCall) {
1130 // Check if it's really possible to do a tail call.
1131 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1132 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001133 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001134 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1135 // detected sibcalls.
1136 if (isTailCall) {
1137 ++NumTailCalls;
1138 IsSibCall = true;
1139 }
1140 }
Evan Chenga8e29892007-01-19 07:51:42 +00001141
Bob Wilson1f595bb2009-04-17 19:07:39 +00001142 // Analyze operands of the call, assigning locations to each operand.
1143 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001144 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1145 *DAG.getContext());
1146 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001147 CCAssignFnForNode(CallConv, /* Return*/ false,
1148 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001149
Bob Wilson1f595bb2009-04-17 19:07:39 +00001150 // Get a count of how many bytes are to be pushed on the stack.
1151 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001152
Dale Johannesen51e28e62010-06-03 21:09:53 +00001153 // For tail calls, memory operands are available in our caller's stack.
1154 if (IsSibCall)
1155 NumBytes = 0;
1156
Evan Chenga8e29892007-01-19 07:51:42 +00001157 // Adjust the stack pointer for the new arguments...
1158 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001159 if (!IsSibCall)
1160 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001161
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001162 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001163
Bob Wilson5bafff32009-06-22 23:27:02 +00001164 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001165 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001166
Bob Wilson1f595bb2009-04-17 19:07:39 +00001167 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001168 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001169 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1170 i != e;
1171 ++i, ++realArgIdx) {
1172 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001173 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001174 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +00001175
Bob Wilson1f595bb2009-04-17 19:07:39 +00001176 // Promote the value if needed.
1177 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001178 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001179 case CCValAssign::Full: break;
1180 case CCValAssign::SExt:
1181 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1182 break;
1183 case CCValAssign::ZExt:
1184 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1185 break;
1186 case CCValAssign::AExt:
1187 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1188 break;
1189 case CCValAssign::BCvt:
1190 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1191 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001192 }
1193
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001194 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001195 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001196 if (VA.getLocVT() == MVT::v2f64) {
1197 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1198 DAG.getConstant(0, MVT::i32));
1199 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1200 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001201
Dan Gohman98ca4f22009-08-05 01:29:28 +00001202 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001203 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1204
1205 VA = ArgLocs[++i]; // skip ahead to next loc
1206 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001207 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001208 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1209 } else {
1210 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001211
Dan Gohman98ca4f22009-08-05 01:29:28 +00001212 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1213 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001214 }
1215 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001216 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001217 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001218 }
1219 } else if (VA.isRegLoc()) {
1220 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001221 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001222 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001223
Dan Gohman98ca4f22009-08-05 01:29:28 +00001224 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1225 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001226 }
Evan Chenga8e29892007-01-19 07:51:42 +00001227 }
1228
1229 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001230 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001231 &MemOpChains[0], MemOpChains.size());
1232
1233 // Build a sequence of copy-to-reg nodes chained together with token chain
1234 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001235 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001236 // Tail call byval lowering might overwrite argument registers so in case of
1237 // tail call optimization the copies to registers are lowered later.
1238 if (!isTailCall)
1239 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1240 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1241 RegsToPass[i].second, InFlag);
1242 InFlag = Chain.getValue(1);
1243 }
Evan Chenga8e29892007-01-19 07:51:42 +00001244
Dale Johannesen51e28e62010-06-03 21:09:53 +00001245 // For tail calls lower the arguments to the 'real' stack slot.
1246 if (isTailCall) {
1247 // Force all the incoming stack arguments to be loaded from the stack
1248 // before any new outgoing arguments are stored to the stack, because the
1249 // outgoing stack slots may alias the incoming argument stack slots, and
1250 // the alias isn't otherwise explicit. This is slightly more conservative
1251 // than necessary, because it means that each store effectively depends
1252 // on every argument instead of just those arguments it would clobber.
1253
1254 // Do not flag preceeding copytoreg stuff together with the following stuff.
1255 InFlag = SDValue();
1256 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1257 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1258 RegsToPass[i].second, InFlag);
1259 InFlag = Chain.getValue(1);
1260 }
1261 InFlag =SDValue();
1262 }
1263
Bill Wendling056292f2008-09-16 21:48:12 +00001264 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1265 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1266 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001267 bool isDirect = false;
1268 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001269 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001270 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001271
1272 if (EnableARMLongCalls) {
1273 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1274 && "long-calls with non-static relocation model!");
1275 // Handle a global address or an external symbol. If it's not one of
1276 // those, the target's already in a register, so we don't need to do
1277 // anything extra.
1278 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001279 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001280 // Create a constant pool entry for the callee address
1281 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1282 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1283 ARMPCLabelIndex,
1284 ARMCP::CPValue, 0);
1285 // Get the address of the callee into a register
1286 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1287 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1288 Callee = DAG.getLoad(getPointerTy(), dl,
1289 DAG.getEntryNode(), CPAddr,
1290 PseudoSourceValue::getConstantPool(), 0,
1291 false, false, 0);
1292 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1293 const char *Sym = S->getSymbol();
1294
1295 // Create a constant pool entry for the callee address
1296 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1297 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1298 Sym, ARMPCLabelIndex, 0);
1299 // Get the address of the callee into a register
1300 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1301 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1302 Callee = DAG.getLoad(getPointerTy(), dl,
1303 DAG.getEntryNode(), CPAddr,
1304 PseudoSourceValue::getConstantPool(), 0,
1305 false, false, 0);
1306 }
1307 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001308 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001309 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001310 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001311 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001312 getTargetMachine().getRelocationModel() != Reloc::Static;
1313 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001314 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001315 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001316 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001317 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001318 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001319 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001320 ARMPCLabelIndex,
1321 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001322 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001323 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001324 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001325 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001326 PseudoSourceValue::getConstantPool(), 0,
1327 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001328 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001329 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001330 getPointerTy(), Callee, PICLabel);
Jim Grosbache7b52522010-04-14 22:28:31 +00001331 } else
Devang Patel0d881da2010-07-06 22:08:15 +00001332 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001333 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001334 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001335 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001336 getTargetMachine().getRelocationModel() != Reloc::Static;
1337 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001338 // tBX takes a register source operand.
1339 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001340 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001341 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001342 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001343 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001344 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001345 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001346 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001347 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001348 PseudoSourceValue::getConstantPool(), 0,
1349 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001350 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001351 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001352 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001353 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001354 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001355 }
1356
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001357 // FIXME: handle tail calls differently.
1358 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001359 if (Subtarget->isThumb()) {
1360 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001361 CallOpc = ARMISD::CALL_NOLINK;
1362 else
1363 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1364 } else {
1365 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001366 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1367 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001368 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001369
Dan Gohman475871a2008-07-27 21:46:04 +00001370 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001371 Ops.push_back(Chain);
1372 Ops.push_back(Callee);
1373
1374 // Add argument registers to the end of the list so that they are known live
1375 // into the call.
1376 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1377 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1378 RegsToPass[i].second.getValueType()));
1379
Gabor Greifba36cb52008-08-28 21:40:38 +00001380 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001381 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001382
1383 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001384 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001385 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001386
Duncan Sands4bdcb612008-07-02 17:40:58 +00001387 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001388 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001389 InFlag = Chain.getValue(1);
1390
Chris Lattnere563bbc2008-10-11 22:08:30 +00001391 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1392 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001393 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001394 InFlag = Chain.getValue(1);
1395
Bob Wilson1f595bb2009-04-17 19:07:39 +00001396 // Handle result values, copying them out of physregs into vregs that we
1397 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001398 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1399 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001400}
1401
Dale Johannesen51e28e62010-06-03 21:09:53 +00001402/// MatchingStackOffset - Return true if the given stack call argument is
1403/// already available in the same position (relatively) of the caller's
1404/// incoming argument stack.
1405static
1406bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1407 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1408 const ARMInstrInfo *TII) {
1409 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1410 int FI = INT_MAX;
1411 if (Arg.getOpcode() == ISD::CopyFromReg) {
1412 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
1413 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
1414 return false;
1415 MachineInstr *Def = MRI->getVRegDef(VR);
1416 if (!Def)
1417 return false;
1418 if (!Flags.isByVal()) {
1419 if (!TII->isLoadFromStackSlot(Def, FI))
1420 return false;
1421 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001422 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001423 }
1424 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1425 if (Flags.isByVal())
1426 // ByVal argument is passed in as a pointer but it's now being
1427 // dereferenced. e.g.
1428 // define @foo(%struct.X* %A) {
1429 // tail call @bar(%struct.X* byval %A)
1430 // }
1431 return false;
1432 SDValue Ptr = Ld->getBasePtr();
1433 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1434 if (!FINode)
1435 return false;
1436 FI = FINode->getIndex();
1437 } else
1438 return false;
1439
1440 assert(FI != INT_MAX);
1441 if (!MFI->isFixedObjectIndex(FI))
1442 return false;
1443 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1444}
1445
1446/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1447/// for tail call optimization. Targets which want to do tail call
1448/// optimization should implement this function.
1449bool
1450ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1451 CallingConv::ID CalleeCC,
1452 bool isVarArg,
1453 bool isCalleeStructRet,
1454 bool isCallerStructRet,
1455 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001456 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001457 const SmallVectorImpl<ISD::InputArg> &Ins,
1458 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001459 const Function *CallerF = DAG.getMachineFunction().getFunction();
1460 CallingConv::ID CallerCC = CallerF->getCallingConv();
1461 bool CCMatch = CallerCC == CalleeCC;
1462
1463 // Look for obvious safe cases to perform tail call optimization that do not
1464 // require ABI changes. This is what gcc calls sibcall.
1465
Jim Grosbach7616b642010-06-16 23:45:49 +00001466 // Do not sibcall optimize vararg calls unless the call site is not passing
1467 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001468 if (isVarArg && !Outs.empty())
1469 return false;
1470
1471 // Also avoid sibcall optimization if either caller or callee uses struct
1472 // return semantics.
1473 if (isCalleeStructRet || isCallerStructRet)
1474 return false;
1475
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001476 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Evan Cheng0110ac62010-06-19 01:01:32 +00001477 // emitEpilogue is not ready for them.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001478 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1479 // LR. This means if we need to reload LR, it takes an extra instructions,
1480 // which outweighs the value of the tail call; but here we don't know yet
1481 // whether LR is going to be used. Probably the right approach is to
1482 // generate the tail call here and turn it back into CALL/RET in
1483 // emitEpilogue if LR is used.
Evan Cheng0110ac62010-06-19 01:01:32 +00001484 if (Subtarget->isThumb1Only())
1485 return false;
1486
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001487 // For the moment, we can only do this to functions defined in this
1488 // compilation, or to indirect calls. A Thumb B to an ARM function,
1489 // or vice versa, is not easily fixed up in the linker unlike BL.
1490 // (We could do this by loading the address of the callee into a register;
1491 // that is an extra instruction over the direct call and burns a register
1492 // as well, so is not likely to be a win.)
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001493
1494 // It might be safe to remove this restriction on non-Darwin.
1495
1496 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1497 // but we need to make sure there are enough registers; the only valid
1498 // registers are the 4 used for parameters. We don't currently do this
1499 // case.
Evan Cheng0110ac62010-06-19 01:01:32 +00001500 if (isa<ExternalSymbolSDNode>(Callee))
1501 return false;
1502
1503 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001504 const GlobalValue *GV = G->getGlobal();
1505 if (GV->isDeclaration() || GV->isWeakForLinker())
Evan Cheng0110ac62010-06-19 01:01:32 +00001506 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001507 }
1508
Dale Johannesen51e28e62010-06-03 21:09:53 +00001509 // If the calling conventions do not match, then we'd better make sure the
1510 // results are returned in the same way as what the caller expects.
1511 if (!CCMatch) {
1512 SmallVector<CCValAssign, 16> RVLocs1;
1513 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
1514 RVLocs1, *DAG.getContext());
1515 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1516
1517 SmallVector<CCValAssign, 16> RVLocs2;
1518 CCState CCInfo2(CallerCC, false, getTargetMachine(),
1519 RVLocs2, *DAG.getContext());
1520 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1521
1522 if (RVLocs1.size() != RVLocs2.size())
1523 return false;
1524 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1525 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1526 return false;
1527 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1528 return false;
1529 if (RVLocs1[i].isRegLoc()) {
1530 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1531 return false;
1532 } else {
1533 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1534 return false;
1535 }
1536 }
1537 }
1538
1539 // If the callee takes no arguments then go on to check the results of the
1540 // call.
1541 if (!Outs.empty()) {
1542 // Check if stack adjustment is needed. For now, do not do this if any
1543 // argument is passed on the stack.
1544 SmallVector<CCValAssign, 16> ArgLocs;
1545 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
1546 ArgLocs, *DAG.getContext());
1547 CCInfo.AnalyzeCallOperands(Outs,
1548 CCAssignFnForNode(CalleeCC, false, isVarArg));
1549 if (CCInfo.getNextStackOffset()) {
1550 MachineFunction &MF = DAG.getMachineFunction();
1551
1552 // Check if the arguments are already laid out in the right way as
1553 // the caller's fixed stack objects.
1554 MachineFrameInfo *MFI = MF.getFrameInfo();
1555 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1556 const ARMInstrInfo *TII =
1557 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001558 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1559 i != e;
1560 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001561 CCValAssign &VA = ArgLocs[i];
1562 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001563 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001564 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001565 if (VA.getLocInfo() == CCValAssign::Indirect)
1566 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001567 if (VA.needsCustom()) {
1568 // f64 and vector types are split into multiple registers or
1569 // register/stack-slot combinations. The types will not match
1570 // the registers; give up on memory f64 refs until we figure
1571 // out what to do about this.
1572 if (!VA.isRegLoc())
1573 return false;
1574 if (!ArgLocs[++i].isRegLoc())
1575 return false;
1576 if (RegVT == MVT::v2f64) {
1577 if (!ArgLocs[++i].isRegLoc())
1578 return false;
1579 if (!ArgLocs[++i].isRegLoc())
1580 return false;
1581 }
1582 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001583 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1584 MFI, MRI, TII))
1585 return false;
1586 }
1587 }
1588 }
1589 }
1590
1591 return true;
1592}
1593
Dan Gohman98ca4f22009-08-05 01:29:28 +00001594SDValue
1595ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001596 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001597 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001598 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001599 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001600
Bob Wilsondee46d72009-04-17 20:35:10 +00001601 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001602 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001603
Bob Wilsondee46d72009-04-17 20:35:10 +00001604 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001605 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1606 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001607
Dan Gohman98ca4f22009-08-05 01:29:28 +00001608 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001609 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1610 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001611
1612 // If this is the first return lowered for this function, add
1613 // the regs to the liveout set for the function.
1614 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1615 for (unsigned i = 0; i != RVLocs.size(); ++i)
1616 if (RVLocs[i].isRegLoc())
1617 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001618 }
1619
Bob Wilson1f595bb2009-04-17 19:07:39 +00001620 SDValue Flag;
1621
1622 // Copy the result values into the output registers.
1623 for (unsigned i = 0, realRVLocIdx = 0;
1624 i != RVLocs.size();
1625 ++i, ++realRVLocIdx) {
1626 CCValAssign &VA = RVLocs[i];
1627 assert(VA.isRegLoc() && "Can only return in registers!");
1628
Dan Gohmanc9403652010-07-07 15:54:55 +00001629 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001630
1631 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001632 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001633 case CCValAssign::Full: break;
1634 case CCValAssign::BCvt:
1635 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1636 break;
1637 }
1638
Bob Wilson1f595bb2009-04-17 19:07:39 +00001639 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001640 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001641 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001642 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1643 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001644 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001645 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001646
1647 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1648 Flag = Chain.getValue(1);
1649 VA = RVLocs[++i]; // skip ahead to next loc
1650 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1651 HalfGPRs.getValue(1), Flag);
1652 Flag = Chain.getValue(1);
1653 VA = RVLocs[++i]; // skip ahead to next loc
1654
1655 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001656 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1657 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001658 }
1659 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1660 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001661 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001662 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001663 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001664 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001665 VA = RVLocs[++i]; // skip ahead to next loc
1666 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1667 Flag);
1668 } else
1669 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1670
Bob Wilsondee46d72009-04-17 20:35:10 +00001671 // Guarantee that all emitted copies are
1672 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001673 Flag = Chain.getValue(1);
1674 }
1675
1676 SDValue result;
1677 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001678 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001679 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001680 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001681
1682 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001683}
1684
Bob Wilsonb62d2572009-11-03 00:02:05 +00001685// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1686// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1687// one of the above mentioned nodes. It has to be wrapped because otherwise
1688// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1689// be used to form addressing mode. These wrapped nodes will be selected
1690// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001691static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001692 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001693 // FIXME there is no actual debug info here
1694 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001695 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001696 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001697 if (CP->isMachineConstantPoolEntry())
1698 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1699 CP->getAlignment());
1700 else
1701 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1702 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001703 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001704}
1705
Jim Grosbache1102ca2010-07-19 17:20:38 +00001706unsigned ARMTargetLowering::getJumpTableEncoding() const {
1707 return MachineJumpTableInfo::EK_Inline;
1708}
1709
Dan Gohmand858e902010-04-17 15:26:15 +00001710SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1711 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001712 MachineFunction &MF = DAG.getMachineFunction();
1713 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1714 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001715 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001716 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001717 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001718 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1719 SDValue CPAddr;
1720 if (RelocM == Reloc::Static) {
1721 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1722 } else {
1723 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001724 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001725 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1726 ARMCP::CPBlockAddress,
1727 PCAdj);
1728 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1729 }
1730 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1731 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001732 PseudoSourceValue::getConstantPool(), 0,
1733 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001734 if (RelocM == Reloc::Static)
1735 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001736 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001737 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001738}
1739
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001740// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001741SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001742ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001743 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001744 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001745 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001746 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001747 MachineFunction &MF = DAG.getMachineFunction();
1748 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1749 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001750 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001751 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001752 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001753 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001754 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001755 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
David Greene1b58cab2010-02-15 16:55:24 +00001756 PseudoSourceValue::getConstantPool(), 0,
1757 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001758 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001759
Evan Chenge7e0d622009-11-06 22:24:13 +00001760 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001761 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001762
1763 // call __tls_get_addr.
1764 ArgListTy Args;
1765 ArgListEntry Entry;
1766 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001767 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001768 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001769 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001770 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001771 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1772 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001773 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001774 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001775 return CallResult.first;
1776}
1777
1778// Lower ISD::GlobalTLSAddress using the "initial exec" or
1779// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001780SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001781ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001782 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001783 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001784 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001785 SDValue Offset;
1786 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001787 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001788 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001789 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001790
Chris Lattner4fb63d02009-07-15 04:12:33 +00001791 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001792 MachineFunction &MF = DAG.getMachineFunction();
1793 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1794 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1795 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001796 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1797 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001798 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001799 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001800 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001801 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001802 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001803 PseudoSourceValue::getConstantPool(), 0,
1804 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001805 Chain = Offset.getValue(1);
1806
Evan Chenge7e0d622009-11-06 22:24:13 +00001807 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001808 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001809
Evan Cheng9eda6892009-10-31 03:39:36 +00001810 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001811 PseudoSourceValue::getConstantPool(), 0,
1812 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001813 } else {
1814 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001815 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001816 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001817 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001818 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001819 PseudoSourceValue::getConstantPool(), 0,
1820 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001821 }
1822
1823 // The address of the thread local variable is the add of the thread
1824 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001825 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001826}
1827
Dan Gohman475871a2008-07-27 21:46:04 +00001828SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001829ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001830 // TODO: implement the "local dynamic" model
1831 assert(Subtarget->isTargetELF() &&
1832 "TLS not implemented for non-ELF targets");
1833 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1834 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1835 // otherwise use the "Local Exec" TLS Model
1836 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1837 return LowerToTLSGeneralDynamicModel(GA, DAG);
1838 else
1839 return LowerToTLSExecModels(GA, DAG);
1840}
1841
Dan Gohman475871a2008-07-27 21:46:04 +00001842SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001843 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001844 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001845 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001846 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001847 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1848 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001849 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001850 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001851 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001852 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001853 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001854 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001855 CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001856 PseudoSourceValue::getConstantPool(), 0,
1857 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001858 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001859 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001860 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001861 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001862 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001863 PseudoSourceValue::getGOT(), 0,
1864 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001865 return Result;
1866 } else {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001867 // If we have T2 ops, we can materialize the address directly via movt/movw
1868 // pair. This is always cheaper.
1869 if (Subtarget->useMovt()) {
1870 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
Devang Patel0d881da2010-07-06 22:08:15 +00001871 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001872 } else {
1873 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1874 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1875 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001876 PseudoSourceValue::getConstantPool(), 0,
1877 false, false, 0);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001878 }
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001879 }
1880}
1881
Dan Gohman475871a2008-07-27 21:46:04 +00001882SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001883 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001884 MachineFunction &MF = DAG.getMachineFunction();
1885 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1886 unsigned ARMPCLabelIndex = 0;
Owen Andersone50ed302009-08-10 22:56:29 +00001887 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001888 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001889 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001890 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001891 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001892 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001893 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001894 else {
Evan Chenge7e0d622009-11-06 22:24:13 +00001895 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001896 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1897 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001898 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001899 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001900 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001901 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001902
Evan Cheng9eda6892009-10-31 03:39:36 +00001903 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001904 PseudoSourceValue::getConstantPool(), 0,
1905 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001906 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001907
1908 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001909 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001910 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001911 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001912
Evan Cheng63476a82009-09-03 07:04:02 +00001913 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Evan Cheng9eda6892009-10-31 03:39:36 +00001914 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001915 PseudoSourceValue::getGOT(), 0,
1916 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001917
1918 return Result;
1919}
1920
Dan Gohman475871a2008-07-27 21:46:04 +00001921SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001922 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001923 assert(Subtarget->isTargetELF() &&
1924 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00001925 MachineFunction &MF = DAG.getMachineFunction();
1926 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1927 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Andersone50ed302009-08-10 22:56:29 +00001928 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001929 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001930 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001931 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1932 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001933 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001934 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001935 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001936 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001937 PseudoSourceValue::getConstantPool(), 0,
1938 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001939 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001940 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001941}
1942
Jim Grosbach0e0da732009-05-12 23:59:14 +00001943SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00001944ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
1945 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00001946 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00001947 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
1948 Op.getOperand(1), Val);
1949}
1950
1951SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00001952ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
1953 DebugLoc dl = Op.getDebugLoc();
1954 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
1955 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
1956}
1957
1958SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00001959ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00001960 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001961 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001962 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001963 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001964 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001965 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001966 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001967 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1968 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001969 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001970 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00001971 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1972 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001973 EVT PtrVT = getPointerTy();
1974 DebugLoc dl = Op.getDebugLoc();
1975 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1976 SDValue CPAddr;
1977 unsigned PCAdj = (RelocM != Reloc::PIC_)
1978 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001979 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001980 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1981 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001982 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001983 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001984 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00001985 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001986 PseudoSourceValue::getConstantPool(), 0,
1987 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001988
1989 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001990 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001991 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1992 }
1993 return Result;
1994 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001995 }
1996}
1997
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001998static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00001999 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002000 DebugLoc dl = Op.getDebugLoc();
2001 SDValue Op5 = Op.getOperand(5);
Jim Grosbach3728e962009-12-10 00:11:09 +00002002 unsigned isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue();
Jim Grosbachc73993b2010-06-17 01:37:00 +00002003 // v6 and v7 can both handle barriers directly, but need handled a bit
2004 // differently. Thumb1 and pre-v6 ARM mode use a libcall instead and should
2005 // never get here.
2006 unsigned Opc = isDeviceBarrier ? ARMISD::SYNCBARRIER : ARMISD::MEMBARRIER;
2007 if (Subtarget->hasV7Ops())
2008 return DAG.getNode(Opc, dl, MVT::Other, Op.getOperand(0));
2009 else if (Subtarget->hasV6Ops() && !Subtarget->isThumb1Only())
2010 return DAG.getNode(Opc, dl, MVT::Other, Op.getOperand(0),
2011 DAG.getConstant(0, MVT::i32));
2012 assert(0 && "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
2013 return SDValue();
Jim Grosbach3728e962009-12-10 00:11:09 +00002014}
2015
Dan Gohman1e93df62010-04-17 14:41:14 +00002016static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2017 MachineFunction &MF = DAG.getMachineFunction();
2018 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2019
Evan Chenga8e29892007-01-19 07:51:42 +00002020 // vastart just stores the address of the VarArgsFrameIndex slot into the
2021 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002022 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002023 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002024 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002025 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
David Greene1b58cab2010-02-15 16:55:24 +00002026 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
2027 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002028}
2029
Dan Gohman475871a2008-07-27 21:46:04 +00002030SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002031ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2032 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002033 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002034 MachineFunction &MF = DAG.getMachineFunction();
2035 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2036
2037 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002038 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002039 RC = ARM::tGPRRegisterClass;
2040 else
2041 RC = ARM::GPRRegisterClass;
2042
2043 // Transform the arguments stored in physical registers into virtual ones.
Evan Cheng2457f2c2010-05-22 01:47:14 +00002044 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002045 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002046
2047 SDValue ArgValue2;
2048 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002049 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002050 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002051
2052 // Create load node to retrieve arguments from the stack.
2053 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002054 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00002055 PseudoSourceValue::getFixedStack(FI), 0,
2056 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002057 } else {
2058 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002059 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002060 }
2061
Jim Grosbache5165492009-11-09 00:11:35 +00002062 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002063}
2064
2065SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002066ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002067 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002068 const SmallVectorImpl<ISD::InputArg>
2069 &Ins,
2070 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002071 SmallVectorImpl<SDValue> &InVals)
2072 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002073
Bob Wilson1f595bb2009-04-17 19:07:39 +00002074 MachineFunction &MF = DAG.getMachineFunction();
2075 MachineFrameInfo *MFI = MF.getFrameInfo();
2076
Bob Wilson1f595bb2009-04-17 19:07:39 +00002077 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2078
2079 // Assign locations to all of the incoming arguments.
2080 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002081 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
2082 *DAG.getContext());
2083 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002084 CCAssignFnForNode(CallConv, /* Return*/ false,
2085 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002086
2087 SmallVector<SDValue, 16> ArgValues;
2088
2089 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2090 CCValAssign &VA = ArgLocs[i];
2091
Bob Wilsondee46d72009-04-17 20:35:10 +00002092 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002093 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002094 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002095
Bob Wilson5bafff32009-06-22 23:27:02 +00002096 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002097 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002098 // f64 and vector types are split up into multiple registers or
2099 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002100 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002101 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002102 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002103 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002104 SDValue ArgValue2;
2105 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002106 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002107 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2108 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
2109 PseudoSourceValue::getFixedStack(FI), 0,
2110 false, false, 0);
2111 } else {
2112 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2113 Chain, DAG, dl);
2114 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002115 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2116 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002117 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002118 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002119 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2120 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002121 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002122
Bob Wilson5bafff32009-06-22 23:27:02 +00002123 } else {
2124 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002125
Owen Anderson825b72b2009-08-11 20:47:22 +00002126 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002127 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002128 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002129 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002130 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002131 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002132 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002133 RC = (AFI->isThumb1OnlyFunction() ?
2134 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002135 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002136 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002137
2138 // Transform the arguments in physical registers into virtual ones.
2139 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002140 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002141 }
2142
2143 // If this is an 8 or 16-bit value, it is really passed promoted
2144 // to 32 bits. Insert an assert[sz]ext to capture this, then
2145 // truncate to the right size.
2146 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002147 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002148 case CCValAssign::Full: break;
2149 case CCValAssign::BCvt:
2150 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
2151 break;
2152 case CCValAssign::SExt:
2153 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2154 DAG.getValueType(VA.getValVT()));
2155 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2156 break;
2157 case CCValAssign::ZExt:
2158 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2159 DAG.getValueType(VA.getValVT()));
2160 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2161 break;
2162 }
2163
Dan Gohman98ca4f22009-08-05 01:29:28 +00002164 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002165
2166 } else { // VA.isRegLoc()
2167
2168 // sanity check
2169 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002170 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002171
2172 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002173 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002174
Bob Wilsondee46d72009-04-17 20:35:10 +00002175 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002176 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002177 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00002178 PseudoSourceValue::getFixedStack(FI), 0,
2179 false, false, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002180 }
2181 }
2182
2183 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00002184 if (isVarArg) {
2185 static const unsigned GPRArgRegs[] = {
2186 ARM::R0, ARM::R1, ARM::R2, ARM::R3
2187 };
2188
Bob Wilsondee46d72009-04-17 20:35:10 +00002189 unsigned NumGPRs = CCInfo.getFirstUnallocated
2190 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002191
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00002192 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
2193 unsigned VARegSize = (4 - NumGPRs) * 4;
2194 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00002195 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00002196 if (VARegSaveSize) {
2197 // If this function is vararg, store any remaining integer argument regs
2198 // to their spots on the stack so that they may be loaded by deferencing
2199 // the result of va_next.
2200 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00002201 AFI->setVarArgsFrameIndex(
2202 MFI->CreateFixedObject(VARegSaveSize,
2203 ArgOffset + VARegSaveSize - VARegSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002204 true));
Dan Gohman1e93df62010-04-17 14:41:14 +00002205 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2206 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00002207
Dan Gohman475871a2008-07-27 21:46:04 +00002208 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00002209 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002210 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002211 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00002212 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002213 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00002214 RC = ARM::GPRRegisterClass;
2215
Bob Wilson998e1252009-04-20 18:36:57 +00002216 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002217 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00002218 SDValue Store =
2219 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Jim Grosbach18f30e62010-06-02 21:53:11 +00002220 PseudoSourceValue::getFixedStack(AFI->getVarArgsFrameIndex()),
2221 0, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002222 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002223 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00002224 DAG.getConstant(4, getPointerTy()));
2225 }
2226 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002227 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002228 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00002229 } else
2230 // This will point to the next argument passed via stack.
Evan Chenged2ae132010-07-03 00:40:23 +00002231 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
Evan Chenga8e29892007-01-19 07:51:42 +00002232 }
2233
Dan Gohman98ca4f22009-08-05 01:29:28 +00002234 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002235}
2236
2237/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002238static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002239 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002240 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002241 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002242 // Maybe this has already been legalized into the constant pool?
2243 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002244 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002245 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002246 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002247 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002248 }
2249 }
2250 return false;
2251}
2252
Evan Chenga8e29892007-01-19 07:51:42 +00002253/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2254/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002255SDValue
2256ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002257 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002258 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002259 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002260 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002261 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002262 // Constant does not fit, try adjusting it by one?
2263 switch (CC) {
2264 default: break;
2265 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002266 case ISD::SETGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00002267 if (isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002268 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002269 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002270 }
2271 break;
2272 case ISD::SETULT:
2273 case ISD::SETUGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00002274 if (C > 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002275 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002276 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002277 }
2278 break;
2279 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002280 case ISD::SETGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00002281 if (isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002282 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002283 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002284 }
2285 break;
2286 case ISD::SETULE:
2287 case ISD::SETUGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00002288 if (C < 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002289 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002290 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002291 }
2292 break;
2293 }
2294 }
2295 }
2296
2297 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002298 ARMISD::NodeType CompareType;
2299 switch (CondCode) {
2300 default:
2301 CompareType = ARMISD::CMP;
2302 break;
2303 case ARMCC::EQ:
2304 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002305 // Uses only Z Flag
2306 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002307 break;
2308 }
Evan Cheng218977b2010-07-13 19:27:42 +00002309 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002310 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002311}
2312
2313/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002314SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002315ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002316 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002317 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002318 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00002319 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002320 else
Owen Anderson825b72b2009-08-11 20:47:22 +00002321 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
2322 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002323}
2324
Dan Gohmand858e902010-04-17 15:26:15 +00002325SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002326 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002327 SDValue LHS = Op.getOperand(0);
2328 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002329 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002330 SDValue TrueVal = Op.getOperand(2);
2331 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002332 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002333
Owen Anderson825b72b2009-08-11 20:47:22 +00002334 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002335 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002336 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002337 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2338 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002339 }
2340
2341 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002342 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002343
Evan Cheng218977b2010-07-13 19:27:42 +00002344 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2345 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002346 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002347 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002348 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002349 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002350 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002351 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002352 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002353 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002354 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002355 }
2356 return Result;
2357}
2358
Evan Cheng218977b2010-07-13 19:27:42 +00002359/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2360/// to morph to an integer compare sequence.
2361static bool canChangeToInt(SDValue Op, bool &SeenZero,
2362 const ARMSubtarget *Subtarget) {
2363 SDNode *N = Op.getNode();
2364 if (!N->hasOneUse())
2365 // Otherwise it requires moving the value from fp to integer registers.
2366 return false;
2367 if (!N->getNumValues())
2368 return false;
2369 EVT VT = Op.getValueType();
2370 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2371 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2372 // vmrs are very slow, e.g. cortex-a8.
2373 return false;
2374
2375 if (isFloatingPointZero(Op)) {
2376 SeenZero = true;
2377 return true;
2378 }
2379 return ISD::isNormalLoad(N);
2380}
2381
2382static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2383 if (isFloatingPointZero(Op))
2384 return DAG.getConstant(0, MVT::i32);
2385
2386 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2387 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2388 Ld->getChain(), Ld->getBasePtr(),
2389 Ld->getSrcValue(), Ld->getSrcValueOffset(),
2390 Ld->isVolatile(), Ld->isNonTemporal(),
2391 Ld->getAlignment());
2392
2393 llvm_unreachable("Unknown VFP cmp argument!");
2394}
2395
2396static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2397 SDValue &RetVal1, SDValue &RetVal2) {
2398 if (isFloatingPointZero(Op)) {
2399 RetVal1 = DAG.getConstant(0, MVT::i32);
2400 RetVal2 = DAG.getConstant(0, MVT::i32);
2401 return;
2402 }
2403
2404 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2405 SDValue Ptr = Ld->getBasePtr();
2406 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2407 Ld->getChain(), Ptr,
2408 Ld->getSrcValue(), Ld->getSrcValueOffset(),
2409 Ld->isVolatile(), Ld->isNonTemporal(),
2410 Ld->getAlignment());
2411
2412 EVT PtrType = Ptr.getValueType();
2413 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2414 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2415 PtrType, Ptr, DAG.getConstant(4, PtrType));
2416 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2417 Ld->getChain(), NewPtr,
2418 Ld->getSrcValue(), Ld->getSrcValueOffset() + 4,
2419 Ld->isVolatile(), Ld->isNonTemporal(),
2420 NewAlign);
2421 return;
2422 }
2423
2424 llvm_unreachable("Unknown VFP cmp argument!");
2425}
2426
2427/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2428/// f32 and even f64 comparisons to integer ones.
2429SDValue
2430ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2431 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002432 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002433 SDValue LHS = Op.getOperand(2);
2434 SDValue RHS = Op.getOperand(3);
2435 SDValue Dest = Op.getOperand(4);
2436 DebugLoc dl = Op.getDebugLoc();
2437
2438 bool SeenZero = false;
2439 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2440 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002441 // If one of the operand is zero, it's safe to ignore the NaN case since
2442 // we only care about equality comparisons.
2443 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Evan Cheng218977b2010-07-13 19:27:42 +00002444 // If unsafe fp math optimization is enabled and there are no othter uses of
2445 // the CMP operands, and the condition code is EQ oe NE, we can optimize it
2446 // to an integer comparison.
2447 if (CC == ISD::SETOEQ)
2448 CC = ISD::SETEQ;
2449 else if (CC == ISD::SETUNE)
2450 CC = ISD::SETNE;
2451
2452 SDValue ARMcc;
2453 if (LHS.getValueType() == MVT::f32) {
2454 LHS = bitcastf32Toi32(LHS, DAG);
2455 RHS = bitcastf32Toi32(RHS, DAG);
2456 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2457 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2458 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2459 Chain, Dest, ARMcc, CCR, Cmp);
2460 }
2461
2462 SDValue LHS1, LHS2;
2463 SDValue RHS1, RHS2;
2464 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2465 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2466 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2467 ARMcc = DAG.getConstant(CondCode, MVT::i32);
2468 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
2469 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2470 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2471 }
2472
2473 return SDValue();
2474}
2475
2476SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2477 SDValue Chain = Op.getOperand(0);
2478 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2479 SDValue LHS = Op.getOperand(2);
2480 SDValue RHS = Op.getOperand(3);
2481 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002482 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002483
Owen Anderson825b72b2009-08-11 20:47:22 +00002484 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002485 SDValue ARMcc;
2486 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002487 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002488 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002489 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002490 }
2491
Owen Anderson825b72b2009-08-11 20:47:22 +00002492 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002493
2494 if (UnsafeFPMath &&
2495 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2496 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2497 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2498 if (Result.getNode())
2499 return Result;
2500 }
2501
Evan Chenga8e29892007-01-19 07:51:42 +00002502 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002503 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002504
Evan Cheng218977b2010-07-13 19:27:42 +00002505 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2506 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002507 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2508 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng218977b2010-07-13 19:27:42 +00002509 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002510 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002511 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002512 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2513 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002514 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002515 }
2516 return Res;
2517}
2518
Dan Gohmand858e902010-04-17 15:26:15 +00002519SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002520 SDValue Chain = Op.getOperand(0);
2521 SDValue Table = Op.getOperand(1);
2522 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002523 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002524
Owen Andersone50ed302009-08-10 22:56:29 +00002525 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002526 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2527 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002528 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002529 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002530 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002531 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2532 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002533 if (Subtarget->isThumb2()) {
2534 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2535 // which does another jump to the destination. This also makes it easier
2536 // to translate it to TBB / TBH later.
2537 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002538 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002539 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002540 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002541 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002542 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002543 PseudoSourceValue::getJumpTable(), 0,
2544 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002545 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002546 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002547 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002548 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002549 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002550 PseudoSourceValue::getJumpTable(), 0, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002551 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002552 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002553 }
Evan Chenga8e29892007-01-19 07:51:42 +00002554}
2555
Bob Wilson76a312b2010-03-19 22:51:32 +00002556static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2557 DebugLoc dl = Op.getDebugLoc();
2558 unsigned Opc;
2559
2560 switch (Op.getOpcode()) {
2561 default:
2562 assert(0 && "Invalid opcode!");
2563 case ISD::FP_TO_SINT:
2564 Opc = ARMISD::FTOSI;
2565 break;
2566 case ISD::FP_TO_UINT:
2567 Opc = ARMISD::FTOUI;
2568 break;
2569 }
2570 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
2571 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
2572}
2573
2574static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2575 EVT VT = Op.getValueType();
2576 DebugLoc dl = Op.getDebugLoc();
2577 unsigned Opc;
2578
2579 switch (Op.getOpcode()) {
2580 default:
2581 assert(0 && "Invalid opcode!");
2582 case ISD::SINT_TO_FP:
2583 Opc = ARMISD::SITOF;
2584 break;
2585 case ISD::UINT_TO_FP:
2586 Opc = ARMISD::UITOF;
2587 break;
2588 }
2589
2590 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
2591 return DAG.getNode(Opc, dl, VT, Op);
2592}
2593
Evan Cheng515fe3a2010-07-08 02:08:50 +00002594SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002595 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002596 SDValue Tmp0 = Op.getOperand(0);
2597 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002598 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002599 EVT VT = Op.getValueType();
2600 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00002601 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
Evan Cheng218977b2010-07-13 19:27:42 +00002602 SDValue ARMcc = DAG.getConstant(ARMCC::LT, MVT::i32);
Evan Cheng515fe3a2010-07-08 02:08:50 +00002603 SDValue FP0 = DAG.getConstantFP(0.0, SrcVT);
Evan Cheng218977b2010-07-13 19:27:42 +00002604 SDValue Cmp = getVFPCmp(Tmp1, FP0, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002605 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002606 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002607}
2608
Evan Cheng2457f2c2010-05-22 01:47:14 +00002609SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
2610 MachineFunction &MF = DAG.getMachineFunction();
2611 MachineFrameInfo *MFI = MF.getFrameInfo();
2612 MFI->setReturnAddressIsTaken(true);
2613
2614 EVT VT = Op.getValueType();
2615 DebugLoc dl = Op.getDebugLoc();
2616 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2617 if (Depth) {
2618 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
2619 SDValue Offset = DAG.getConstant(4, MVT::i32);
2620 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
2621 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
2622 NULL, 0, false, false, 0);
2623 }
2624
2625 // Return LR, which contains the return address. Mark it an implicit live-in.
Jim Grosbachc2723a52010-07-23 23:50:35 +00002626 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00002627 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
2628}
2629
Dan Gohmand858e902010-04-17 15:26:15 +00002630SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002631 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2632 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002633
Owen Andersone50ed302009-08-10 22:56:29 +00002634 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002635 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2636 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002637 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002638 ? ARM::R7 : ARM::R11;
2639 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2640 while (Depth--)
David Greene1b58cab2010-02-15 16:55:24 +00002641 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
2642 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002643 return FrameAddr;
2644}
2645
Bob Wilson9f3f0612010-04-17 05:30:19 +00002646/// ExpandBIT_CONVERT - If the target supports VFP, this function is called to
2647/// expand a bit convert where either the source or destination type is i64 to
2648/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2649/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2650/// vectors), since the legalizer won't know what to do with that.
Duncan Sands1607f052008-12-01 11:39:25 +00002651static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002652 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2653 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002654 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002655
Bob Wilson9f3f0612010-04-17 05:30:19 +00002656 // This function is only supposed to be called for i64 types, either as the
2657 // source or destination of the bit convert.
2658 EVT SrcVT = Op.getValueType();
2659 EVT DstVT = N->getValueType(0);
2660 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
2661 "ExpandBIT_CONVERT called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002662
Bob Wilson9f3f0612010-04-17 05:30:19 +00002663 // Turn i64->f64 into VMOVDRR.
2664 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002665 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2666 DAG.getConstant(0, MVT::i32));
2667 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2668 DAG.getConstant(1, MVT::i32));
Bob Wilson1114f562010-06-11 22:45:25 +00002669 return DAG.getNode(ISD::BIT_CONVERT, dl, DstVT,
2670 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00002671 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002672
Jim Grosbache5165492009-11-09 00:11:35 +00002673 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002674 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2675 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2676 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2677 // Merge the pieces into a single i64 value.
2678 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2679 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002680
Bob Wilson9f3f0612010-04-17 05:30:19 +00002681 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002682}
2683
Bob Wilson5bafff32009-06-22 23:27:02 +00002684/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00002685/// Zero vectors are used to represent vector negation and in those cases
2686/// will be implemented with the NEON VNEG instruction. However, VNEG does
2687/// not support i64 elements, so sometimes the zero vectors will need to be
2688/// explicitly constructed. Regardless, use a canonical VMOV to create the
2689/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002690static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002691 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00002692 // The canonical modified immediate encoding of a zero vector is....0!
2693 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
2694 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
2695 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
2696 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00002697}
2698
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002699/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2700/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002701SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2702 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002703 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2704 EVT VT = Op.getValueType();
2705 unsigned VTBits = VT.getSizeInBits();
2706 DebugLoc dl = Op.getDebugLoc();
2707 SDValue ShOpLo = Op.getOperand(0);
2708 SDValue ShOpHi = Op.getOperand(1);
2709 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00002710 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002711 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002712
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002713 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2714
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002715 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2716 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2717 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2718 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2719 DAG.getConstant(VTBits, MVT::i32));
2720 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2721 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002722 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002723
2724 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2725 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00002726 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002727 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00002728 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002729 CCR, Cmp);
2730
2731 SDValue Ops[2] = { Lo, Hi };
2732 return DAG.getMergeValues(Ops, 2, dl);
2733}
2734
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002735/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2736/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002737SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2738 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002739 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2740 EVT VT = Op.getValueType();
2741 unsigned VTBits = VT.getSizeInBits();
2742 DebugLoc dl = Op.getDebugLoc();
2743 SDValue ShOpLo = Op.getOperand(0);
2744 SDValue ShOpHi = Op.getOperand(1);
2745 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00002746 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002747
2748 assert(Op.getOpcode() == ISD::SHL_PARTS);
2749 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2750 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2751 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2752 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2753 DAG.getConstant(VTBits, MVT::i32));
2754 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2755 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2756
2757 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2758 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2759 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00002760 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002761 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00002762 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002763 CCR, Cmp);
2764
2765 SDValue Ops[2] = { Lo, Hi };
2766 return DAG.getMergeValues(Ops, 2, dl);
2767}
2768
Nate Begemand1fb5832010-08-03 21:31:55 +00002769SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
2770 SelectionDAG &DAG) const {
2771 // The rounding mode is in bits 23:22 of the FPSCR.
2772 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
2773 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
2774 // so that the shift + and get folded into a bitfield extract.
2775 DebugLoc dl = Op.getDebugLoc();
2776 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
2777 DAG.getConstant(Intrinsic::arm_get_fpscr,
2778 MVT::i32));
2779 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
2780 DAG.getConstant(1U << 22, MVT::i32));
2781 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
2782 DAG.getConstant(22, MVT::i32));
2783 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
2784 DAG.getConstant(3, MVT::i32));
2785}
2786
Jim Grosbach3482c802010-01-18 19:58:49 +00002787static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
2788 const ARMSubtarget *ST) {
2789 EVT VT = N->getValueType(0);
2790 DebugLoc dl = N->getDebugLoc();
2791
2792 if (!ST->hasV6T2Ops())
2793 return SDValue();
2794
2795 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
2796 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
2797}
2798
Bob Wilson5bafff32009-06-22 23:27:02 +00002799static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2800 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002801 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002802 DebugLoc dl = N->getDebugLoc();
2803
2804 // Lower vector shifts on NEON to use VSHL.
2805 if (VT.isVector()) {
2806 assert(ST->hasNEON() && "unexpected vector shift");
2807
2808 // Left shifts translate directly to the vshiftu intrinsic.
2809 if (N->getOpcode() == ISD::SHL)
2810 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002811 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002812 N->getOperand(0), N->getOperand(1));
2813
2814 assert((N->getOpcode() == ISD::SRA ||
2815 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2816
2817 // NEON uses the same intrinsics for both left and right shifts. For
2818 // right shifts, the shift amounts are negative, so negate the vector of
2819 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002820 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002821 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2822 getZeroVector(ShiftVT, DAG, dl),
2823 N->getOperand(1));
2824 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2825 Intrinsic::arm_neon_vshifts :
2826 Intrinsic::arm_neon_vshiftu);
2827 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002828 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002829 N->getOperand(0), NegatedCount);
2830 }
2831
Eli Friedmance392eb2009-08-22 03:13:10 +00002832 // We can get here for a node like i32 = ISD::SHL i32, i64
2833 if (VT != MVT::i64)
2834 return SDValue();
2835
2836 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002837 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002838
Chris Lattner27a6c732007-11-24 07:07:01 +00002839 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2840 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002841 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002842 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002843
Chris Lattner27a6c732007-11-24 07:07:01 +00002844 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002845 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002846
Chris Lattner27a6c732007-11-24 07:07:01 +00002847 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002848 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00002849 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00002850 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00002851 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002852
Chris Lattner27a6c732007-11-24 07:07:01 +00002853 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2854 // captures the result into a carry flag.
2855 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002856 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002857
Chris Lattner27a6c732007-11-24 07:07:01 +00002858 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002859 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002860
Chris Lattner27a6c732007-11-24 07:07:01 +00002861 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002862 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002863}
2864
Bob Wilson5bafff32009-06-22 23:27:02 +00002865static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2866 SDValue TmpOp0, TmpOp1;
2867 bool Invert = false;
2868 bool Swap = false;
2869 unsigned Opc = 0;
2870
2871 SDValue Op0 = Op.getOperand(0);
2872 SDValue Op1 = Op.getOperand(1);
2873 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002874 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002875 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2876 DebugLoc dl = Op.getDebugLoc();
2877
2878 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2879 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002880 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002881 case ISD::SETUNE:
2882 case ISD::SETNE: Invert = true; // Fallthrough
2883 case ISD::SETOEQ:
2884 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2885 case ISD::SETOLT:
2886 case ISD::SETLT: Swap = true; // Fallthrough
2887 case ISD::SETOGT:
2888 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2889 case ISD::SETOLE:
2890 case ISD::SETLE: Swap = true; // Fallthrough
2891 case ISD::SETOGE:
2892 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2893 case ISD::SETUGE: Swap = true; // Fallthrough
2894 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2895 case ISD::SETUGT: Swap = true; // Fallthrough
2896 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2897 case ISD::SETUEQ: Invert = true; // Fallthrough
2898 case ISD::SETONE:
2899 // Expand this to (OLT | OGT).
2900 TmpOp0 = Op0;
2901 TmpOp1 = Op1;
2902 Opc = ISD::OR;
2903 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2904 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2905 break;
2906 case ISD::SETUO: Invert = true; // Fallthrough
2907 case ISD::SETO:
2908 // Expand this to (OLT | OGE).
2909 TmpOp0 = Op0;
2910 TmpOp1 = Op1;
2911 Opc = ISD::OR;
2912 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2913 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2914 break;
2915 }
2916 } else {
2917 // Integer comparisons.
2918 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002919 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002920 case ISD::SETNE: Invert = true;
2921 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2922 case ISD::SETLT: Swap = true;
2923 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2924 case ISD::SETLE: Swap = true;
2925 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2926 case ISD::SETULT: Swap = true;
2927 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2928 case ISD::SETULE: Swap = true;
2929 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2930 }
2931
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002932 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002933 if (Opc == ARMISD::VCEQ) {
2934
2935 SDValue AndOp;
2936 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2937 AndOp = Op0;
2938 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2939 AndOp = Op1;
2940
2941 // Ignore bitconvert.
2942 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2943 AndOp = AndOp.getOperand(0);
2944
2945 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2946 Opc = ARMISD::VTST;
2947 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2948 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2949 Invert = !Invert;
2950 }
2951 }
2952 }
2953
2954 if (Swap)
2955 std::swap(Op0, Op1);
2956
2957 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2958
2959 if (Invert)
2960 Result = DAG.getNOT(dl, Result, VT);
2961
2962 return Result;
2963}
2964
Bob Wilsond3c42842010-06-14 22:19:57 +00002965/// isNEONModifiedImm - Check if the specified splat value corresponds to a
2966/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00002967/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00002968static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
2969 unsigned SplatBitSize, SelectionDAG &DAG,
Bob Wilsoncba270d2010-07-13 21:16:48 +00002970 EVT &VT, bool is128Bits, bool isVMOV) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00002971 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002972
Bob Wilson827b2102010-06-15 19:05:35 +00002973 // SplatBitSize is set to the smallest size that splats the vector, so a
2974 // zero vector will always have SplatBitSize == 8. However, NEON modified
2975 // immediate instructions others than VMOV do not support the 8-bit encoding
2976 // of a zero vector, and the default encoding of zero is supposed to be the
2977 // 32-bit version.
2978 if (SplatBits == 0)
2979 SplatBitSize = 32;
2980
Bob Wilson5bafff32009-06-22 23:27:02 +00002981 switch (SplatBitSize) {
2982 case 8:
Bob Wilson7e3f0d22010-07-14 06:31:50 +00002983 if (!isVMOV)
2984 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00002985 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00002986 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00002987 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002988 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00002989 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002990 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002991
2992 case 16:
2993 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00002994 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002995 if ((SplatBits & ~0xff) == 0) {
2996 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00002997 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002998 Imm = SplatBits;
2999 break;
3000 }
3001 if ((SplatBits & ~0xff00) == 0) {
3002 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003003 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003004 Imm = SplatBits >> 8;
3005 break;
3006 }
3007 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003008
3009 case 32:
3010 // NEON's 32-bit VMOV supports splat values where:
3011 // * only one byte is nonzero, or
3012 // * the least significant byte is 0xff and the second byte is nonzero, or
3013 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003014 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003015 if ((SplatBits & ~0xff) == 0) {
3016 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003017 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003018 Imm = SplatBits;
3019 break;
3020 }
3021 if ((SplatBits & ~0xff00) == 0) {
3022 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003023 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003024 Imm = SplatBits >> 8;
3025 break;
3026 }
3027 if ((SplatBits & ~0xff0000) == 0) {
3028 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003029 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003030 Imm = SplatBits >> 16;
3031 break;
3032 }
3033 if ((SplatBits & ~0xff000000) == 0) {
3034 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003035 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003036 Imm = SplatBits >> 24;
3037 break;
3038 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003039
3040 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003041 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3042 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003043 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003044 Imm = SplatBits >> 8;
3045 SplatBits |= 0xff;
3046 break;
3047 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003048
3049 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003050 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3051 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003052 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003053 Imm = SplatBits >> 16;
3054 SplatBits |= 0xffff;
3055 break;
3056 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003057
3058 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3059 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3060 // VMOV.I32. A (very) minor optimization would be to replicate the value
3061 // and fall through here to test for a valid 64-bit splat. But, then the
3062 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003063 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003064
3065 case 64: {
Bob Wilson827b2102010-06-15 19:05:35 +00003066 if (!isVMOV)
3067 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003068 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003069 uint64_t BitMask = 0xff;
3070 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003071 unsigned ImmMask = 1;
3072 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003073 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003074 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003075 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003076 Imm |= ImmMask;
3077 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003078 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003079 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003080 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003081 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003082 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003083 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003084 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003085 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003086 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003087 break;
3088 }
3089
Bob Wilson1a913ed2010-06-11 21:34:50 +00003090 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003091 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003092 return SDValue();
3093 }
3094
Bob Wilsoncba270d2010-07-13 21:16:48 +00003095 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3096 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003097}
3098
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003099static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3100 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003101 unsigned NumElts = VT.getVectorNumElements();
3102 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003103 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003104
3105 // If this is a VEXT shuffle, the immediate value is the index of the first
3106 // element. The other shuffle indices must be the successive elements after
3107 // the first one.
3108 unsigned ExpectedElt = Imm;
3109 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003110 // Increment the expected index. If it wraps around, it may still be
3111 // a VEXT but the source vectors must be swapped.
3112 ExpectedElt += 1;
3113 if (ExpectedElt == NumElts * 2) {
3114 ExpectedElt = 0;
3115 ReverseVEXT = true;
3116 }
3117
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003118 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003119 return false;
3120 }
3121
3122 // Adjust the index value if the source operands will be swapped.
3123 if (ReverseVEXT)
3124 Imm -= NumElts;
3125
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003126 return true;
3127}
3128
Bob Wilson8bb9e482009-07-26 00:39:34 +00003129/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3130/// instruction with the specified blocksize. (The order of the elements
3131/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003132static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3133 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003134 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3135 "Only possible block sizes for VREV are: 16, 32, 64");
3136
Bob Wilson8bb9e482009-07-26 00:39:34 +00003137 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003138 if (EltSz == 64)
3139 return false;
3140
3141 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003142 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003143
3144 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3145 return false;
3146
3147 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003148 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00003149 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
3150 return false;
3151 }
3152
3153 return true;
3154}
3155
Bob Wilsonc692cb72009-08-21 20:54:19 +00003156static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3157 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003158 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3159 if (EltSz == 64)
3160 return false;
3161
Bob Wilsonc692cb72009-08-21 20:54:19 +00003162 unsigned NumElts = VT.getVectorNumElements();
3163 WhichResult = (M[0] == 0 ? 0 : 1);
3164 for (unsigned i = 0; i < NumElts; i += 2) {
3165 if ((unsigned) M[i] != i + WhichResult ||
3166 (unsigned) M[i+1] != i + NumElts + WhichResult)
3167 return false;
3168 }
3169 return true;
3170}
3171
Bob Wilson324f4f12009-12-03 06:40:55 +00003172/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3173/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3174/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3175static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3176 unsigned &WhichResult) {
3177 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3178 if (EltSz == 64)
3179 return false;
3180
3181 unsigned NumElts = VT.getVectorNumElements();
3182 WhichResult = (M[0] == 0 ? 0 : 1);
3183 for (unsigned i = 0; i < NumElts; i += 2) {
3184 if ((unsigned) M[i] != i + WhichResult ||
3185 (unsigned) M[i+1] != i + WhichResult)
3186 return false;
3187 }
3188 return true;
3189}
3190
Bob Wilsonc692cb72009-08-21 20:54:19 +00003191static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3192 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003193 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3194 if (EltSz == 64)
3195 return false;
3196
Bob Wilsonc692cb72009-08-21 20:54:19 +00003197 unsigned NumElts = VT.getVectorNumElements();
3198 WhichResult = (M[0] == 0 ? 0 : 1);
3199 for (unsigned i = 0; i != NumElts; ++i) {
3200 if ((unsigned) M[i] != 2 * i + WhichResult)
3201 return false;
3202 }
3203
3204 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003205 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003206 return false;
3207
3208 return true;
3209}
3210
Bob Wilson324f4f12009-12-03 06:40:55 +00003211/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3212/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3213/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3214static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3215 unsigned &WhichResult) {
3216 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3217 if (EltSz == 64)
3218 return false;
3219
3220 unsigned Half = VT.getVectorNumElements() / 2;
3221 WhichResult = (M[0] == 0 ? 0 : 1);
3222 for (unsigned j = 0; j != 2; ++j) {
3223 unsigned Idx = WhichResult;
3224 for (unsigned i = 0; i != Half; ++i) {
3225 if ((unsigned) M[i + j * Half] != Idx)
3226 return false;
3227 Idx += 2;
3228 }
3229 }
3230
3231 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3232 if (VT.is64BitVector() && EltSz == 32)
3233 return false;
3234
3235 return true;
3236}
3237
Bob Wilsonc692cb72009-08-21 20:54:19 +00003238static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3239 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003240 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3241 if (EltSz == 64)
3242 return false;
3243
Bob Wilsonc692cb72009-08-21 20:54:19 +00003244 unsigned NumElts = VT.getVectorNumElements();
3245 WhichResult = (M[0] == 0 ? 0 : 1);
3246 unsigned Idx = WhichResult * NumElts / 2;
3247 for (unsigned i = 0; i != NumElts; i += 2) {
3248 if ((unsigned) M[i] != Idx ||
3249 (unsigned) M[i+1] != Idx + NumElts)
3250 return false;
3251 Idx += 1;
3252 }
3253
3254 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003255 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003256 return false;
3257
3258 return true;
3259}
3260
Bob Wilson324f4f12009-12-03 06:40:55 +00003261/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3262/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3263/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3264static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3265 unsigned &WhichResult) {
3266 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3267 if (EltSz == 64)
3268 return false;
3269
3270 unsigned NumElts = VT.getVectorNumElements();
3271 WhichResult = (M[0] == 0 ? 0 : 1);
3272 unsigned Idx = WhichResult * NumElts / 2;
3273 for (unsigned i = 0; i != NumElts; i += 2) {
3274 if ((unsigned) M[i] != Idx ||
3275 (unsigned) M[i+1] != Idx)
3276 return false;
3277 Idx += 1;
3278 }
3279
3280 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3281 if (VT.is64BitVector() && EltSz == 32)
3282 return false;
3283
3284 return true;
3285}
3286
Dale Johannesenf630c712010-07-29 20:10:08 +00003287// If N is an integer constant that can be moved into a register in one
3288// instruction, return an SDValue of such a constant (will become a MOV
3289// instruction). Otherwise return null.
3290static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3291 const ARMSubtarget *ST, DebugLoc dl) {
3292 uint64_t Val;
3293 if (!isa<ConstantSDNode>(N))
3294 return SDValue();
3295 Val = cast<ConstantSDNode>(N)->getZExtValue();
3296
3297 if (ST->isThumb1Only()) {
3298 if (Val <= 255 || ~Val <= 255)
3299 return DAG.getConstant(Val, MVT::i32);
3300 } else {
3301 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3302 return DAG.getConstant(Val, MVT::i32);
3303 }
3304 return SDValue();
3305}
3306
Bob Wilson5bafff32009-06-22 23:27:02 +00003307// If this is a case we can't handle, return null and let the default
3308// expansion code take care of it.
Dale Johannesenf630c712010-07-29 20:10:08 +00003309static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3310 const ARMSubtarget *ST) {
Bob Wilsond06791f2009-08-13 01:57:47 +00003311 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003312 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003313 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003314
3315 APInt SplatBits, SplatUndef;
3316 unsigned SplatBitSize;
3317 bool HasAnyUndefs;
3318 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003319 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003320 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003321 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003322 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003323 SplatUndef.getZExtValue(), SplatBitSize,
3324 DAG, VmovVT, VT.is128BitVector(), true);
3325 if (Val.getNode()) {
3326 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
3327 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vmov);
3328 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003329
3330 // Try an immediate VMVN.
3331 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3332 ((1LL << SplatBitSize) - 1));
3333 Val = isNEONModifiedImm(NegatedImm,
3334 SplatUndef.getZExtValue(), SplatBitSize,
3335 DAG, VmovVT, VT.is128BitVector(), false);
3336 if (Val.getNode()) {
3337 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
3338 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vmov);
3339 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003340 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003341 }
3342
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003343 // Scan through the operands to see if only one value is used.
3344 unsigned NumElts = VT.getVectorNumElements();
3345 bool isOnlyLowElement = true;
3346 bool usesOnlyOneValue = true;
3347 bool isConstant = true;
3348 SDValue Value;
3349 for (unsigned i = 0; i < NumElts; ++i) {
3350 SDValue V = Op.getOperand(i);
3351 if (V.getOpcode() == ISD::UNDEF)
3352 continue;
3353 if (i > 0)
3354 isOnlyLowElement = false;
3355 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3356 isConstant = false;
3357
3358 if (!Value.getNode())
3359 Value = V;
3360 else if (V != Value)
3361 usesOnlyOneValue = false;
3362 }
3363
3364 if (!Value.getNode())
3365 return DAG.getUNDEF(VT);
3366
3367 if (isOnlyLowElement)
3368 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3369
Dale Johannesenf630c712010-07-29 20:10:08 +00003370 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3371
3372 if (EnableARMVDUPsplat) {
3373 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3374 // i32 and try again.
3375 if (usesOnlyOneValue && EltSize <= 32) {
3376 if (!isConstant)
3377 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3378 if (VT.getVectorElementType().isFloatingPoint()) {
3379 SmallVector<SDValue, 8> Ops;
3380 for (unsigned i = 0; i < NumElts; ++i)
3381 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
3382 Op.getOperand(i)));
3383 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &Ops[0],
3384 NumElts);
3385 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3386 LowerBUILD_VECTOR(Val, DAG, ST));
3387 }
3388 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3389 if (Val.getNode())
3390 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
3391 }
3392 }
3393
3394 // If all elements are constants and the case above didn't get hit, fall back
3395 // to the default expansion, which will generate a load from the constant
3396 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003397 if (isConstant)
3398 return SDValue();
3399
Dale Johannesenf630c712010-07-29 20:10:08 +00003400 if (!EnableARMVDUPsplat) {
3401 // Use VDUP for non-constant splats.
3402 if (usesOnlyOneValue && EltSize <= 32)
3403 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3404 }
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003405
3406 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003407 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3408 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003409 if (EltSize >= 32) {
3410 // Do the expansion with floating-point types, since that is what the VFP
3411 // registers are defined to use, and since i64 is not legal.
3412 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3413 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003414 SmallVector<SDValue, 8> Ops;
3415 for (unsigned i = 0; i < NumElts; ++i)
3416 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, dl, EltVT, Op.getOperand(i)));
3417 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003418 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003419 }
3420
3421 return SDValue();
3422}
3423
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003424/// isShuffleMaskLegal - Targets can use this to indicate that they only
3425/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3426/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3427/// are assumed to be legal.
3428bool
3429ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
3430 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003431 if (VT.getVectorNumElements() == 4 &&
3432 (VT.is128BitVector() || VT.is64BitVector())) {
3433 unsigned PFIndexes[4];
3434 for (unsigned i = 0; i != 4; ++i) {
3435 if (M[i] < 0)
3436 PFIndexes[i] = 8;
3437 else
3438 PFIndexes[i] = M[i];
3439 }
3440
3441 // Compute the index in the perfect shuffle table.
3442 unsigned PFTableIndex =
3443 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3444 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3445 unsigned Cost = (PFEntry >> 30);
3446
3447 if (Cost <= 4)
3448 return true;
3449 }
3450
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003451 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00003452 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003453
Bob Wilson53dd2452010-06-07 23:53:38 +00003454 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3455 return (EltSize >= 32 ||
3456 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003457 isVREVMask(M, VT, 64) ||
3458 isVREVMask(M, VT, 32) ||
3459 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00003460 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
3461 isVTRNMask(M, VT, WhichResult) ||
3462 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00003463 isVZIPMask(M, VT, WhichResult) ||
3464 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
3465 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
3466 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003467}
3468
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003469/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
3470/// the specified operations to build the shuffle.
3471static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
3472 SDValue RHS, SelectionDAG &DAG,
3473 DebugLoc dl) {
3474 unsigned OpNum = (PFEntry >> 26) & 0x0F;
3475 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
3476 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
3477
3478 enum {
3479 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
3480 OP_VREV,
3481 OP_VDUP0,
3482 OP_VDUP1,
3483 OP_VDUP2,
3484 OP_VDUP3,
3485 OP_VEXT1,
3486 OP_VEXT2,
3487 OP_VEXT3,
3488 OP_VUZPL, // VUZP, left result
3489 OP_VUZPR, // VUZP, right result
3490 OP_VZIPL, // VZIP, left result
3491 OP_VZIPR, // VZIP, right result
3492 OP_VTRNL, // VTRN, left result
3493 OP_VTRNR // VTRN, right result
3494 };
3495
3496 if (OpNum == OP_COPY) {
3497 if (LHSID == (1*9+2)*9+3) return LHS;
3498 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
3499 return RHS;
3500 }
3501
3502 SDValue OpLHS, OpRHS;
3503 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
3504 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
3505 EVT VT = OpLHS.getValueType();
3506
3507 switch (OpNum) {
3508 default: llvm_unreachable("Unknown shuffle opcode!");
3509 case OP_VREV:
3510 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
3511 case OP_VDUP0:
3512 case OP_VDUP1:
3513 case OP_VDUP2:
3514 case OP_VDUP3:
3515 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003516 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003517 case OP_VEXT1:
3518 case OP_VEXT2:
3519 case OP_VEXT3:
3520 return DAG.getNode(ARMISD::VEXT, dl, VT,
3521 OpLHS, OpRHS,
3522 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
3523 case OP_VUZPL:
3524 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003525 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003526 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
3527 case OP_VZIPL:
3528 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003529 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003530 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
3531 case OP_VTRNL:
3532 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003533 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3534 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003535 }
3536}
3537
Bob Wilson5bafff32009-06-22 23:27:02 +00003538static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003539 SDValue V1 = Op.getOperand(0);
3540 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00003541 DebugLoc dl = Op.getDebugLoc();
3542 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003543 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003544 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00003545
Bob Wilson28865062009-08-13 02:13:04 +00003546 // Convert shuffles that are directly supported on NEON to target-specific
3547 // DAG nodes, instead of keeping them as shuffles and matching them again
3548 // during code selection. This is more efficient and avoids the possibility
3549 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00003550 // FIXME: floating-point vectors should be canonicalized to integer vectors
3551 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003552 SVN->getMask(ShuffleMask);
3553
Bob Wilson53dd2452010-06-07 23:53:38 +00003554 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3555 if (EltSize <= 32) {
3556 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
3557 int Lane = SVN->getSplatIndex();
3558 // If this is undef splat, generate it via "just" vdup, if possible.
3559 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00003560
Bob Wilson53dd2452010-06-07 23:53:38 +00003561 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3562 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
3563 }
3564 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
3565 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00003566 }
Bob Wilson53dd2452010-06-07 23:53:38 +00003567
3568 bool ReverseVEXT;
3569 unsigned Imm;
3570 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
3571 if (ReverseVEXT)
3572 std::swap(V1, V2);
3573 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
3574 DAG.getConstant(Imm, MVT::i32));
3575 }
3576
3577 if (isVREVMask(ShuffleMask, VT, 64))
3578 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
3579 if (isVREVMask(ShuffleMask, VT, 32))
3580 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
3581 if (isVREVMask(ShuffleMask, VT, 16))
3582 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
3583
3584 // Check for Neon shuffles that modify both input vectors in place.
3585 // If both results are used, i.e., if there are two shuffles with the same
3586 // source operands and with masks corresponding to both results of one of
3587 // these operations, DAG memoization will ensure that a single node is
3588 // used for both shuffles.
3589 unsigned WhichResult;
3590 if (isVTRNMask(ShuffleMask, VT, WhichResult))
3591 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3592 V1, V2).getValue(WhichResult);
3593 if (isVUZPMask(ShuffleMask, VT, WhichResult))
3594 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3595 V1, V2).getValue(WhichResult);
3596 if (isVZIPMask(ShuffleMask, VT, WhichResult))
3597 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3598 V1, V2).getValue(WhichResult);
3599
3600 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
3601 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3602 V1, V1).getValue(WhichResult);
3603 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3604 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3605 V1, V1).getValue(WhichResult);
3606 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3607 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3608 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00003609 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003610
Bob Wilsonc692cb72009-08-21 20:54:19 +00003611 // If the shuffle is not directly supported and it has 4 elements, use
3612 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003613 unsigned NumElts = VT.getVectorNumElements();
3614 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003615 unsigned PFIndexes[4];
3616 for (unsigned i = 0; i != 4; ++i) {
3617 if (ShuffleMask[i] < 0)
3618 PFIndexes[i] = 8;
3619 else
3620 PFIndexes[i] = ShuffleMask[i];
3621 }
3622
3623 // Compute the index in the perfect shuffle table.
3624 unsigned PFTableIndex =
3625 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003626 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3627 unsigned Cost = (PFEntry >> 30);
3628
3629 if (Cost <= 4)
3630 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
3631 }
Bob Wilsond8e17572009-08-12 22:31:50 +00003632
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003633 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003634 if (EltSize >= 32) {
3635 // Do the expansion with floating-point types, since that is what the VFP
3636 // registers are defined to use, and since i64 is not legal.
3637 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3638 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
3639 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V1);
3640 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003641 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003642 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00003643 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003644 Ops.push_back(DAG.getUNDEF(EltVT));
3645 else
3646 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
3647 ShuffleMask[i] < (int)NumElts ? V1 : V2,
3648 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
3649 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00003650 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003651 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Bob Wilson63b88452010-05-20 18:39:53 +00003652 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
3653 }
3654
Bob Wilson22cac0d2009-08-14 05:16:33 +00003655 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003656}
3657
Bob Wilson5bafff32009-06-22 23:27:02 +00003658static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003659 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003660 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003661 SDValue Vec = Op.getOperand(0);
3662 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00003663 assert(VT == MVT::i32 &&
3664 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
3665 "unexpected type for custom-lowering vector extract");
3666 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00003667}
3668
Bob Wilsona6d65862009-08-03 20:36:38 +00003669static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3670 // The only time a CONCAT_VECTORS operation can have legal types is when
3671 // two 64-bit vectors are concatenated to a 128-bit vector.
3672 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
3673 "unexpected CONCAT_VECTORS");
3674 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00003675 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00003676 SDValue Op0 = Op.getOperand(0);
3677 SDValue Op1 = Op.getOperand(1);
3678 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003679 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3680 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00003681 DAG.getIntPtrConstant(0));
3682 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003683 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3684 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00003685 DAG.getIntPtrConstant(1));
3686 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003687}
3688
Dan Gohmand858e902010-04-17 15:26:15 +00003689SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003690 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003691 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00003692 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00003693 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003694 case ISD::GlobalAddress:
3695 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
3696 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00003697 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00003698 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
3699 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003700 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00003701 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00003702 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00003703 case ISD::SINT_TO_FP:
3704 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
3705 case ISD::FP_TO_SINT:
3706 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003707 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003708 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003709 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003710 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00003711 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00003712 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00003713 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
3714 Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00003715 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003716 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00003717 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00003718 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00003719 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003720 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00003721 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00003722 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003723 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00003724 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003725 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003726 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00003727 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Nate Begemand1fb5832010-08-03 21:31:55 +00003728 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003729 }
Dan Gohman475871a2008-07-27 21:46:04 +00003730 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003731}
3732
Duncan Sands1607f052008-12-01 11:39:25 +00003733/// ReplaceNodeResults - Replace the results of node with an illegal result
3734/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00003735void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
3736 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00003737 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00003738 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00003739 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00003740 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003741 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003742 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003743 case ISD::BIT_CONVERT:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003744 Res = ExpandBIT_CONVERT(N, DAG);
3745 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00003746 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003747 case ISD::SRA:
3748 Res = LowerShift(N, DAG, Subtarget);
3749 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003750 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00003751 if (Res.getNode())
3752 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00003753}
Chris Lattner27a6c732007-11-24 07:07:01 +00003754
Evan Chenga8e29892007-01-19 07:51:42 +00003755//===----------------------------------------------------------------------===//
3756// ARM Scheduler Hooks
3757//===----------------------------------------------------------------------===//
3758
3759MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003760ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
3761 MachineBasicBlock *BB,
3762 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00003763 unsigned dest = MI->getOperand(0).getReg();
3764 unsigned ptr = MI->getOperand(1).getReg();
3765 unsigned oldval = MI->getOperand(2).getReg();
3766 unsigned newval = MI->getOperand(3).getReg();
3767 unsigned scratch = BB->getParent()->getRegInfo()
3768 .createVirtualRegister(ARM::GPRRegisterClass);
3769 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3770 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003771 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00003772
3773 unsigned ldrOpc, strOpc;
3774 switch (Size) {
3775 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003776 case 1:
3777 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
3778 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
3779 break;
3780 case 2:
3781 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3782 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3783 break;
3784 case 4:
3785 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3786 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3787 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00003788 }
3789
3790 MachineFunction *MF = BB->getParent();
3791 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3792 MachineFunction::iterator It = BB;
3793 ++It; // insert the new blocks after the current block
3794
3795 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3796 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3797 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3798 MF->insert(It, loop1MBB);
3799 MF->insert(It, loop2MBB);
3800 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00003801
3802 // Transfer the remainder of BB and its successor edges to exitMBB.
3803 exitMBB->splice(exitMBB->begin(), BB,
3804 llvm::next(MachineBasicBlock::iterator(MI)),
3805 BB->end());
3806 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003807
3808 // thisMBB:
3809 // ...
3810 // fallthrough --> loop1MBB
3811 BB->addSuccessor(loop1MBB);
3812
3813 // loop1MBB:
3814 // ldrex dest, [ptr]
3815 // cmp dest, oldval
3816 // bne exitMBB
3817 BB = loop1MBB;
3818 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003819 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003820 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003821 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3822 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003823 BB->addSuccessor(loop2MBB);
3824 BB->addSuccessor(exitMBB);
3825
3826 // loop2MBB:
3827 // strex scratch, newval, [ptr]
3828 // cmp scratch, #0
3829 // bne loop1MBB
3830 BB = loop2MBB;
3831 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
3832 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003833 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003834 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003835 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3836 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003837 BB->addSuccessor(loop1MBB);
3838 BB->addSuccessor(exitMBB);
3839
3840 // exitMBB:
3841 // ...
3842 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00003843
Dan Gohman14152b42010-07-06 20:24:04 +00003844 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00003845
Jim Grosbach5278eb82009-12-11 01:42:04 +00003846 return BB;
3847}
3848
3849MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003850ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
3851 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00003852 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
3853 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3854
3855 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003856 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003857 MachineFunction::iterator It = BB;
3858 ++It;
3859
3860 unsigned dest = MI->getOperand(0).getReg();
3861 unsigned ptr = MI->getOperand(1).getReg();
3862 unsigned incr = MI->getOperand(2).getReg();
3863 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00003864
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003865 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003866 unsigned ldrOpc, strOpc;
3867 switch (Size) {
3868 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003869 case 1:
3870 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00003871 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003872 break;
3873 case 2:
3874 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3875 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3876 break;
3877 case 4:
3878 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3879 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3880 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00003881 }
3882
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003883 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3884 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3885 MF->insert(It, loopMBB);
3886 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00003887
3888 // Transfer the remainder of BB and its successor edges to exitMBB.
3889 exitMBB->splice(exitMBB->begin(), BB,
3890 llvm::next(MachineBasicBlock::iterator(MI)),
3891 BB->end());
3892 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003893
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003894 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003895 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3896 unsigned scratch2 = (!BinOpcode) ? incr :
3897 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3898
3899 // thisMBB:
3900 // ...
3901 // fallthrough --> loopMBB
3902 BB->addSuccessor(loopMBB);
3903
3904 // loopMBB:
3905 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003906 // <binop> scratch2, dest, incr
3907 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00003908 // cmp scratch, #0
3909 // bne- loopMBB
3910 // fallthrough --> exitMBB
3911 BB = loopMBB;
3912 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00003913 if (BinOpcode) {
3914 // operand order needs to go the other way for NAND
3915 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
3916 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3917 addReg(incr).addReg(dest)).addReg(0);
3918 else
3919 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3920 addReg(dest).addReg(incr)).addReg(0);
3921 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00003922
3923 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
3924 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003925 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00003926 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003927 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3928 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003929
3930 BB->addSuccessor(loopMBB);
3931 BB->addSuccessor(exitMBB);
3932
3933 // exitMBB:
3934 // ...
3935 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00003936
Dan Gohman14152b42010-07-06 20:24:04 +00003937 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00003938
Jim Grosbachc3c23542009-12-14 04:22:04 +00003939 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00003940}
3941
Evan Cheng218977b2010-07-13 19:27:42 +00003942static
3943MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
3944 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
3945 E = MBB->succ_end(); I != E; ++I)
3946 if (*I != Succ)
3947 return *I;
3948 llvm_unreachable("Expecting a BB with two successors!");
3949}
3950
Jim Grosbache801dc42009-12-12 01:40:06 +00003951MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00003952ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003953 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003954 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00003955 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003956 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00003957 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00003958 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00003959 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00003960 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00003961
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003962 case ARM::ATOMIC_LOAD_ADD_I8:
3963 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3964 case ARM::ATOMIC_LOAD_ADD_I16:
3965 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3966 case ARM::ATOMIC_LOAD_ADD_I32:
3967 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003968
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003969 case ARM::ATOMIC_LOAD_AND_I8:
3970 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3971 case ARM::ATOMIC_LOAD_AND_I16:
3972 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3973 case ARM::ATOMIC_LOAD_AND_I32:
3974 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003975
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003976 case ARM::ATOMIC_LOAD_OR_I8:
3977 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3978 case ARM::ATOMIC_LOAD_OR_I16:
3979 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3980 case ARM::ATOMIC_LOAD_OR_I32:
3981 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003982
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003983 case ARM::ATOMIC_LOAD_XOR_I8:
3984 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3985 case ARM::ATOMIC_LOAD_XOR_I16:
3986 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3987 case ARM::ATOMIC_LOAD_XOR_I32:
3988 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003989
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003990 case ARM::ATOMIC_LOAD_NAND_I8:
3991 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3992 case ARM::ATOMIC_LOAD_NAND_I16:
3993 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3994 case ARM::ATOMIC_LOAD_NAND_I32:
3995 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003996
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003997 case ARM::ATOMIC_LOAD_SUB_I8:
3998 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3999 case ARM::ATOMIC_LOAD_SUB_I16:
4000 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
4001 case ARM::ATOMIC_LOAD_SUB_I32:
4002 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004003
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004004 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
4005 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
4006 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00004007
4008 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
4009 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
4010 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004011
Evan Cheng007ea272009-08-12 05:17:19 +00004012 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00004013 // To "insert" a SELECT_CC instruction, we actually have to insert the
4014 // diamond control-flow pattern. The incoming instruction knows the
4015 // destination vreg to set, the condition code register to branch on, the
4016 // true/false values to select between, and a branch opcode to use.
4017 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004018 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00004019 ++It;
4020
4021 // thisMBB:
4022 // ...
4023 // TrueVal = ...
4024 // cmpTY ccX, r1, r2
4025 // bCC copy1MBB
4026 // fallthrough --> copy0MBB
4027 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004028 MachineFunction *F = BB->getParent();
4029 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
4030 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00004031 F->insert(It, copy0MBB);
4032 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004033
4034 // Transfer the remainder of BB and its successor edges to sinkMBB.
4035 sinkMBB->splice(sinkMBB->begin(), BB,
4036 llvm::next(MachineBasicBlock::iterator(MI)),
4037 BB->end());
4038 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
4039
Dan Gohman258c58c2010-07-06 15:49:48 +00004040 BB->addSuccessor(copy0MBB);
4041 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00004042
Dan Gohman14152b42010-07-06 20:24:04 +00004043 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
4044 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
4045
Evan Chenga8e29892007-01-19 07:51:42 +00004046 // copy0MBB:
4047 // %FalseValue = ...
4048 // # fallthrough to sinkMBB
4049 BB = copy0MBB;
4050
4051 // Update machine-CFG edges
4052 BB->addSuccessor(sinkMBB);
4053
4054 // sinkMBB:
4055 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4056 // ...
4057 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00004058 BuildMI(*BB, BB->begin(), dl,
4059 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00004060 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4061 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4062
Dan Gohman14152b42010-07-06 20:24:04 +00004063 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00004064 return BB;
4065 }
Evan Cheng86198642009-08-07 00:34:42 +00004066
Evan Cheng218977b2010-07-13 19:27:42 +00004067 case ARM::BCCi64:
4068 case ARM::BCCZi64: {
4069 // Compare both parts that make up the double comparison separately for
4070 // equality.
4071 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
4072
4073 unsigned LHS1 = MI->getOperand(1).getReg();
4074 unsigned LHS2 = MI->getOperand(2).getReg();
4075 if (RHSisZero) {
4076 AddDefaultPred(BuildMI(BB, dl,
4077 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
4078 .addReg(LHS1).addImm(0));
4079 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
4080 .addReg(LHS2).addImm(0)
4081 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
4082 } else {
4083 unsigned RHS1 = MI->getOperand(3).getReg();
4084 unsigned RHS2 = MI->getOperand(4).getReg();
4085 AddDefaultPred(BuildMI(BB, dl,
4086 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
4087 .addReg(LHS1).addReg(RHS1));
4088 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
4089 .addReg(LHS2).addReg(RHS2)
4090 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
4091 }
4092
4093 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
4094 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
4095 if (MI->getOperand(0).getImm() == ARMCC::NE)
4096 std::swap(destMBB, exitMBB);
4097
4098 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4099 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
4100 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
4101 .addMBB(exitMBB);
4102
4103 MI->eraseFromParent(); // The pseudo instruction is gone now.
4104 return BB;
4105 }
4106
Evan Cheng86198642009-08-07 00:34:42 +00004107 case ARM::tANDsp:
4108 case ARM::tADDspr_:
4109 case ARM::tSUBspi_:
4110 case ARM::t2SUBrSPi_:
4111 case ARM::t2SUBrSPi12_:
4112 case ARM::t2SUBrSPs_: {
4113 MachineFunction *MF = BB->getParent();
4114 unsigned DstReg = MI->getOperand(0).getReg();
4115 unsigned SrcReg = MI->getOperand(1).getReg();
4116 bool DstIsDead = MI->getOperand(0).isDead();
4117 bool SrcIsKill = MI->getOperand(1).isKill();
4118
4119 if (SrcReg != ARM::SP) {
4120 // Copy the source to SP from virtual register.
4121 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
4122 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
4123 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
Dan Gohman14152b42010-07-06 20:24:04 +00004124 BuildMI(*BB, MI, dl, TII->get(CopyOpc), ARM::SP)
Evan Cheng86198642009-08-07 00:34:42 +00004125 .addReg(SrcReg, getKillRegState(SrcIsKill));
4126 }
4127
4128 unsigned OpOpc = 0;
4129 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
4130 switch (MI->getOpcode()) {
4131 default:
4132 llvm_unreachable("Unexpected pseudo instruction!");
4133 case ARM::tANDsp:
4134 OpOpc = ARM::tAND;
4135 NeedPred = true;
4136 break;
4137 case ARM::tADDspr_:
4138 OpOpc = ARM::tADDspr;
4139 break;
4140 case ARM::tSUBspi_:
4141 OpOpc = ARM::tSUBspi;
4142 break;
4143 case ARM::t2SUBrSPi_:
4144 OpOpc = ARM::t2SUBrSPi;
4145 NeedPred = true; NeedCC = true;
4146 break;
4147 case ARM::t2SUBrSPi12_:
4148 OpOpc = ARM::t2SUBrSPi12;
4149 NeedPred = true;
4150 break;
4151 case ARM::t2SUBrSPs_:
4152 OpOpc = ARM::t2SUBrSPs;
4153 NeedPred = true; NeedCC = true; NeedOp3 = true;
4154 break;
4155 }
Dan Gohman14152b42010-07-06 20:24:04 +00004156 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(OpOpc), ARM::SP);
Evan Cheng86198642009-08-07 00:34:42 +00004157 if (OpOpc == ARM::tAND)
4158 AddDefaultT1CC(MIB);
4159 MIB.addReg(ARM::SP);
4160 MIB.addOperand(MI->getOperand(2));
4161 if (NeedOp3)
4162 MIB.addOperand(MI->getOperand(3));
4163 if (NeedPred)
4164 AddDefaultPred(MIB);
4165 if (NeedCC)
4166 AddDefaultCC(MIB);
4167
4168 // Copy the result from SP to virtual register.
4169 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
4170 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
4171 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
Dan Gohman14152b42010-07-06 20:24:04 +00004172 BuildMI(*BB, MI, dl, TII->get(CopyOpc))
Evan Cheng86198642009-08-07 00:34:42 +00004173 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
4174 .addReg(ARM::SP);
Dan Gohman14152b42010-07-06 20:24:04 +00004175 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng86198642009-08-07 00:34:42 +00004176 return BB;
4177 }
Evan Chenga8e29892007-01-19 07:51:42 +00004178 }
4179}
4180
4181//===----------------------------------------------------------------------===//
4182// ARM Optimization Hooks
4183//===----------------------------------------------------------------------===//
4184
Chris Lattnerd1980a52009-03-12 06:52:53 +00004185static
4186SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
4187 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00004188 SelectionDAG &DAG = DCI.DAG;
4189 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004190 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00004191 unsigned Opc = N->getOpcode();
4192 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
4193 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
4194 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
4195 ISD::CondCode CC = ISD::SETCC_INVALID;
4196
4197 if (isSlctCC) {
4198 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
4199 } else {
4200 SDValue CCOp = Slct.getOperand(0);
4201 if (CCOp.getOpcode() == ISD::SETCC)
4202 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
4203 }
4204
4205 bool DoXform = false;
4206 bool InvCC = false;
4207 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
4208 "Bad input!");
4209
4210 if (LHS.getOpcode() == ISD::Constant &&
4211 cast<ConstantSDNode>(LHS)->isNullValue()) {
4212 DoXform = true;
4213 } else if (CC != ISD::SETCC_INVALID &&
4214 RHS.getOpcode() == ISD::Constant &&
4215 cast<ConstantSDNode>(RHS)->isNullValue()) {
4216 std::swap(LHS, RHS);
4217 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004218 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00004219 Op0.getOperand(0).getValueType();
4220 bool isInt = OpVT.isInteger();
4221 CC = ISD::getSetCCInverse(CC, isInt);
4222
4223 if (!TLI.isCondCodeLegal(CC, OpVT))
4224 return SDValue(); // Inverse operator isn't legal.
4225
4226 DoXform = true;
4227 InvCC = true;
4228 }
4229
4230 if (DoXform) {
4231 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
4232 if (isSlctCC)
4233 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
4234 Slct.getOperand(0), Slct.getOperand(1), CC);
4235 SDValue CCOp = Slct.getOperand(0);
4236 if (InvCC)
4237 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
4238 CCOp.getOperand(0), CCOp.getOperand(1), CC);
4239 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
4240 CCOp, OtherOp, Result);
4241 }
4242 return SDValue();
4243}
4244
Bob Wilson3d5792a2010-07-29 20:34:14 +00004245/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
4246/// operands N0 and N1. This is a helper for PerformADDCombine that is
4247/// called with the default operands, and if that fails, with commuted
4248/// operands.
4249static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
4250 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00004251 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
4252 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
4253 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
4254 if (Result.getNode()) return Result;
4255 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00004256
Chris Lattnerd1980a52009-03-12 06:52:53 +00004257 return SDValue();
4258}
4259
Bob Wilson3d5792a2010-07-29 20:34:14 +00004260/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
4261///
4262static SDValue PerformADDCombine(SDNode *N,
4263 TargetLowering::DAGCombinerInfo &DCI) {
4264 SDValue N0 = N->getOperand(0);
4265 SDValue N1 = N->getOperand(1);
4266
4267 // First try with the default operand order.
4268 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI);
4269 if (Result.getNode())
4270 return Result;
4271
4272 // If that didn't work, try again with the operands commuted.
4273 return PerformADDCombineWithOperands(N, N1, N0, DCI);
4274}
4275
Chris Lattnerd1980a52009-03-12 06:52:53 +00004276/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00004277///
Chris Lattnerd1980a52009-03-12 06:52:53 +00004278static SDValue PerformSUBCombine(SDNode *N,
4279 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00004280 SDValue N0 = N->getOperand(0);
4281 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00004282
Chris Lattnerd1980a52009-03-12 06:52:53 +00004283 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
4284 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
4285 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
4286 if (Result.getNode()) return Result;
4287 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00004288
Chris Lattnerd1980a52009-03-12 06:52:53 +00004289 return SDValue();
4290}
4291
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004292static SDValue PerformMULCombine(SDNode *N,
4293 TargetLowering::DAGCombinerInfo &DCI,
4294 const ARMSubtarget *Subtarget) {
4295 SelectionDAG &DAG = DCI.DAG;
4296
4297 if (Subtarget->isThumb1Only())
4298 return SDValue();
4299
4300 if (DAG.getMachineFunction().
4301 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
4302 return SDValue();
4303
4304 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
4305 return SDValue();
4306
4307 EVT VT = N->getValueType(0);
4308 if (VT != MVT::i32)
4309 return SDValue();
4310
4311 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
4312 if (!C)
4313 return SDValue();
4314
4315 uint64_t MulAmt = C->getZExtValue();
4316 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
4317 ShiftAmt = ShiftAmt & (32 - 1);
4318 SDValue V = N->getOperand(0);
4319 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004320
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004321 SDValue Res;
4322 MulAmt >>= ShiftAmt;
4323 if (isPowerOf2_32(MulAmt - 1)) {
4324 // (mul x, 2^N + 1) => (add (shl x, N), x)
4325 Res = DAG.getNode(ISD::ADD, DL, VT,
4326 V, DAG.getNode(ISD::SHL, DL, VT,
4327 V, DAG.getConstant(Log2_32(MulAmt-1),
4328 MVT::i32)));
4329 } else if (isPowerOf2_32(MulAmt + 1)) {
4330 // (mul x, 2^N - 1) => (sub (shl x, N), x)
4331 Res = DAG.getNode(ISD::SUB, DL, VT,
4332 DAG.getNode(ISD::SHL, DL, VT,
4333 V, DAG.getConstant(Log2_32(MulAmt+1),
4334 MVT::i32)),
4335 V);
4336 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004337 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004338
4339 if (ShiftAmt != 0)
4340 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
4341 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004342
4343 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004344 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004345 return SDValue();
4346}
4347
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004348/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
4349static SDValue PerformORCombine(SDNode *N,
4350 TargetLowering::DAGCombinerInfo &DCI,
4351 const ARMSubtarget *Subtarget) {
Jim Grosbach54238562010-07-17 03:30:54 +00004352 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
4353 // reasonable.
4354
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004355 // BFI is only available on V6T2+
4356 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
4357 return SDValue();
4358
4359 SelectionDAG &DAG = DCI.DAG;
4360 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Jim Grosbach54238562010-07-17 03:30:54 +00004361 DebugLoc DL = N->getDebugLoc();
4362 // 1) or (and A, mask), val => ARMbfi A, val, mask
4363 // iff (val & mask) == val
4364 //
4365 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
4366 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
4367 // && CountPopulation_32(mask) == CountPopulation_32(~mask2)
4368 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
4369 // && CountPopulation_32(mask) == CountPopulation_32(~mask2)
4370 // (i.e., copy a bitfield value into another bitfield of the same width)
4371 if (N0.getOpcode() != ISD::AND)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004372 return SDValue();
4373
4374 EVT VT = N->getValueType(0);
4375 if (VT != MVT::i32)
4376 return SDValue();
4377
Jim Grosbach54238562010-07-17 03:30:54 +00004378
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004379 // The value and the mask need to be constants so we can verify this is
4380 // actually a bitfield set. If the mask is 0xffff, we can do better
4381 // via a movt instruction, so don't use BFI in that case.
4382 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
4383 if (!C)
4384 return SDValue();
4385 unsigned Mask = C->getZExtValue();
4386 if (Mask == 0xffff)
4387 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004388 SDValue Res;
4389 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
4390 if ((C = dyn_cast<ConstantSDNode>(N1))) {
4391 unsigned Val = C->getZExtValue();
4392 if (!ARM::isBitFieldInvertedMask(Mask) || (Val & ~Mask) != Val)
4393 return SDValue();
4394 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004395
Jim Grosbach54238562010-07-17 03:30:54 +00004396 Res = DAG.getNode(ARMISD::BFI, DL, VT, N0.getOperand(0),
4397 DAG.getConstant(Val, MVT::i32),
4398 DAG.getConstant(Mask, MVT::i32));
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004399
Jim Grosbach54238562010-07-17 03:30:54 +00004400 // Do not add new nodes to DAG combiner worklist.
4401 DCI.CombineTo(N, Res, false);
4402 } else if (N1.getOpcode() == ISD::AND) {
4403 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
4404 C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
4405 if (!C)
4406 return SDValue();
4407 unsigned Mask2 = C->getZExtValue();
4408
4409 if (ARM::isBitFieldInvertedMask(Mask) &&
4410 ARM::isBitFieldInvertedMask(~Mask2) &&
4411 (CountPopulation_32(Mask) == CountPopulation_32(~Mask2))) {
4412 // The pack halfword instruction works better for masks that fit it,
4413 // so use that when it's available.
4414 if (Subtarget->hasT2ExtractPack() &&
4415 (Mask == 0xffff || Mask == 0xffff0000))
4416 return SDValue();
4417 // 2a
4418 unsigned lsb = CountTrailingZeros_32(Mask2);
4419 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
4420 DAG.getConstant(lsb, MVT::i32));
4421 Res = DAG.getNode(ARMISD::BFI, DL, VT, N0.getOperand(0), Res,
4422 DAG.getConstant(Mask, MVT::i32));
4423 // Do not add new nodes to DAG combiner worklist.
4424 DCI.CombineTo(N, Res, false);
4425 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
4426 ARM::isBitFieldInvertedMask(Mask2) &&
4427 (CountPopulation_32(~Mask) == CountPopulation_32(Mask2))) {
4428 // The pack halfword instruction works better for masks that fit it,
4429 // so use that when it's available.
4430 if (Subtarget->hasT2ExtractPack() &&
4431 (Mask2 == 0xffff || Mask2 == 0xffff0000))
4432 return SDValue();
4433 // 2b
4434 unsigned lsb = CountTrailingZeros_32(Mask);
4435 Res = DAG.getNode(ISD::SRL, DL, VT, N0.getOperand(0),
4436 DAG.getConstant(lsb, MVT::i32));
4437 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
4438 DAG.getConstant(Mask2, MVT::i32));
4439 // Do not add new nodes to DAG combiner worklist.
4440 DCI.CombineTo(N, Res, false);
4441 }
4442 }
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004443
4444 return SDValue();
4445}
4446
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +00004447/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
4448/// ARMISD::VMOVRRD.
Jim Grosbache5165492009-11-09 00:11:35 +00004449static SDValue PerformVMOVRRDCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004450 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004451 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00004452 SDValue InDouble = N->getOperand(0);
Jim Grosbache5165492009-11-09 00:11:35 +00004453 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004454 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004455 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004456}
4457
Bob Wilson9e82bf12010-07-14 01:22:12 +00004458/// PerformVDUPLANECombine - Target-specific dag combine xforms for
4459/// ARMISD::VDUPLANE.
4460static SDValue PerformVDUPLANECombine(SDNode *N,
4461 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004462 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
4463 // redundant.
Bob Wilson9e82bf12010-07-14 01:22:12 +00004464 SDValue Op = N->getOperand(0);
4465 EVT VT = N->getValueType(0);
4466
4467 // Ignore bit_converts.
4468 while (Op.getOpcode() == ISD::BIT_CONVERT)
4469 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004470 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00004471 return SDValue();
4472
4473 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
4474 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
4475 // The canonical VMOV for a zero vector uses a 32-bit element size.
4476 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
4477 unsigned EltBits;
4478 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
4479 EltSize = 8;
4480 if (EltSize > VT.getVectorElementType().getSizeInBits())
4481 return SDValue();
4482
4483 SDValue Res = DCI.DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
4484 return DCI.CombineTo(N, Res, false);
4485}
4486
Bob Wilson5bafff32009-06-22 23:27:02 +00004487/// getVShiftImm - Check if this is a valid build_vector for the immediate
4488/// operand of a vector shift operation, where all the elements of the
4489/// build_vector must have the same constant integer value.
4490static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
4491 // Ignore bit_converts.
4492 while (Op.getOpcode() == ISD::BIT_CONVERT)
4493 Op = Op.getOperand(0);
4494 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
4495 APInt SplatBits, SplatUndef;
4496 unsigned SplatBitSize;
4497 bool HasAnyUndefs;
4498 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
4499 HasAnyUndefs, ElementBits) ||
4500 SplatBitSize > ElementBits)
4501 return false;
4502 Cnt = SplatBits.getSExtValue();
4503 return true;
4504}
4505
4506/// isVShiftLImm - Check if this is a valid build_vector for the immediate
4507/// operand of a vector shift left operation. That value must be in the range:
4508/// 0 <= Value < ElementBits for a left shift; or
4509/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00004510static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00004511 assert(VT.isVector() && "vector shift count is not a vector type");
4512 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
4513 if (! getVShiftImm(Op, ElementBits, Cnt))
4514 return false;
4515 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
4516}
4517
4518/// isVShiftRImm - Check if this is a valid build_vector for the immediate
4519/// operand of a vector shift right operation. For a shift opcode, the value
4520/// is positive, but for an intrinsic the value count must be negative. The
4521/// absolute value must be in the range:
4522/// 1 <= |Value| <= ElementBits for a right shift; or
4523/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00004524static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00004525 int64_t &Cnt) {
4526 assert(VT.isVector() && "vector shift count is not a vector type");
4527 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
4528 if (! getVShiftImm(Op, ElementBits, Cnt))
4529 return false;
4530 if (isIntrinsic)
4531 Cnt = -Cnt;
4532 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
4533}
4534
4535/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
4536static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
4537 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
4538 switch (IntNo) {
4539 default:
4540 // Don't do anything for most intrinsics.
4541 break;
4542
4543 // Vector shifts: check for immediate versions and lower them.
4544 // Note: This is done during DAG combining instead of DAG legalizing because
4545 // the build_vectors for 64-bit vector element shift counts are generally
4546 // not legal, and it is hard to see their values after they get legalized to
4547 // loads from a constant pool.
4548 case Intrinsic::arm_neon_vshifts:
4549 case Intrinsic::arm_neon_vshiftu:
4550 case Intrinsic::arm_neon_vshiftls:
4551 case Intrinsic::arm_neon_vshiftlu:
4552 case Intrinsic::arm_neon_vshiftn:
4553 case Intrinsic::arm_neon_vrshifts:
4554 case Intrinsic::arm_neon_vrshiftu:
4555 case Intrinsic::arm_neon_vrshiftn:
4556 case Intrinsic::arm_neon_vqshifts:
4557 case Intrinsic::arm_neon_vqshiftu:
4558 case Intrinsic::arm_neon_vqshiftsu:
4559 case Intrinsic::arm_neon_vqshiftns:
4560 case Intrinsic::arm_neon_vqshiftnu:
4561 case Intrinsic::arm_neon_vqshiftnsu:
4562 case Intrinsic::arm_neon_vqrshiftns:
4563 case Intrinsic::arm_neon_vqrshiftnu:
4564 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00004565 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004566 int64_t Cnt;
4567 unsigned VShiftOpc = 0;
4568
4569 switch (IntNo) {
4570 case Intrinsic::arm_neon_vshifts:
4571 case Intrinsic::arm_neon_vshiftu:
4572 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
4573 VShiftOpc = ARMISD::VSHL;
4574 break;
4575 }
4576 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
4577 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
4578 ARMISD::VSHRs : ARMISD::VSHRu);
4579 break;
4580 }
4581 return SDValue();
4582
4583 case Intrinsic::arm_neon_vshiftls:
4584 case Intrinsic::arm_neon_vshiftlu:
4585 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
4586 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00004587 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004588
4589 case Intrinsic::arm_neon_vrshifts:
4590 case Intrinsic::arm_neon_vrshiftu:
4591 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
4592 break;
4593 return SDValue();
4594
4595 case Intrinsic::arm_neon_vqshifts:
4596 case Intrinsic::arm_neon_vqshiftu:
4597 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
4598 break;
4599 return SDValue();
4600
4601 case Intrinsic::arm_neon_vqshiftsu:
4602 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
4603 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00004604 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004605
4606 case Intrinsic::arm_neon_vshiftn:
4607 case Intrinsic::arm_neon_vrshiftn:
4608 case Intrinsic::arm_neon_vqshiftns:
4609 case Intrinsic::arm_neon_vqshiftnu:
4610 case Intrinsic::arm_neon_vqshiftnsu:
4611 case Intrinsic::arm_neon_vqrshiftns:
4612 case Intrinsic::arm_neon_vqrshiftnu:
4613 case Intrinsic::arm_neon_vqrshiftnsu:
4614 // Narrowing shifts require an immediate right shift.
4615 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
4616 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00004617 llvm_unreachable("invalid shift count for narrowing vector shift "
4618 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004619
4620 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004621 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00004622 }
4623
4624 switch (IntNo) {
4625 case Intrinsic::arm_neon_vshifts:
4626 case Intrinsic::arm_neon_vshiftu:
4627 // Opcode already set above.
4628 break;
4629 case Intrinsic::arm_neon_vshiftls:
4630 case Intrinsic::arm_neon_vshiftlu:
4631 if (Cnt == VT.getVectorElementType().getSizeInBits())
4632 VShiftOpc = ARMISD::VSHLLi;
4633 else
4634 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
4635 ARMISD::VSHLLs : ARMISD::VSHLLu);
4636 break;
4637 case Intrinsic::arm_neon_vshiftn:
4638 VShiftOpc = ARMISD::VSHRN; break;
4639 case Intrinsic::arm_neon_vrshifts:
4640 VShiftOpc = ARMISD::VRSHRs; break;
4641 case Intrinsic::arm_neon_vrshiftu:
4642 VShiftOpc = ARMISD::VRSHRu; break;
4643 case Intrinsic::arm_neon_vrshiftn:
4644 VShiftOpc = ARMISD::VRSHRN; break;
4645 case Intrinsic::arm_neon_vqshifts:
4646 VShiftOpc = ARMISD::VQSHLs; break;
4647 case Intrinsic::arm_neon_vqshiftu:
4648 VShiftOpc = ARMISD::VQSHLu; break;
4649 case Intrinsic::arm_neon_vqshiftsu:
4650 VShiftOpc = ARMISD::VQSHLsu; break;
4651 case Intrinsic::arm_neon_vqshiftns:
4652 VShiftOpc = ARMISD::VQSHRNs; break;
4653 case Intrinsic::arm_neon_vqshiftnu:
4654 VShiftOpc = ARMISD::VQSHRNu; break;
4655 case Intrinsic::arm_neon_vqshiftnsu:
4656 VShiftOpc = ARMISD::VQSHRNsu; break;
4657 case Intrinsic::arm_neon_vqrshiftns:
4658 VShiftOpc = ARMISD::VQRSHRNs; break;
4659 case Intrinsic::arm_neon_vqrshiftnu:
4660 VShiftOpc = ARMISD::VQRSHRNu; break;
4661 case Intrinsic::arm_neon_vqrshiftnsu:
4662 VShiftOpc = ARMISD::VQRSHRNsu; break;
4663 }
4664
4665 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004666 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004667 }
4668
4669 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00004670 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004671 int64_t Cnt;
4672 unsigned VShiftOpc = 0;
4673
4674 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
4675 VShiftOpc = ARMISD::VSLI;
4676 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
4677 VShiftOpc = ARMISD::VSRI;
4678 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00004679 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004680 }
4681
4682 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
4683 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00004684 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004685 }
4686
4687 case Intrinsic::arm_neon_vqrshifts:
4688 case Intrinsic::arm_neon_vqrshiftu:
4689 // No immediate versions of these to check for.
4690 break;
4691 }
4692
4693 return SDValue();
4694}
4695
4696/// PerformShiftCombine - Checks for immediate versions of vector shifts and
4697/// lowers them. As with the vector shift intrinsics, this is done during DAG
4698/// combining instead of DAG legalizing because the build_vectors for 64-bit
4699/// vector element shift counts are generally not legal, and it is hard to see
4700/// their values after they get legalized to loads from a constant pool.
4701static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
4702 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00004703 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00004704
4705 // Nothing to be done for scalar shifts.
4706 if (! VT.isVector())
4707 return SDValue();
4708
4709 assert(ST->hasNEON() && "unexpected vector shift");
4710 int64_t Cnt;
4711
4712 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004713 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00004714
4715 case ISD::SHL:
4716 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
4717 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004718 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004719 break;
4720
4721 case ISD::SRA:
4722 case ISD::SRL:
4723 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
4724 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
4725 ARMISD::VSHRs : ARMISD::VSHRu);
4726 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004727 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004728 }
4729 }
4730 return SDValue();
4731}
4732
4733/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
4734/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
4735static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
4736 const ARMSubtarget *ST) {
4737 SDValue N0 = N->getOperand(0);
4738
4739 // Check for sign- and zero-extensions of vector extract operations of 8-
4740 // and 16-bit vector elements. NEON supports these directly. They are
4741 // handled during DAG combining because type legalization will promote them
4742 // to 32-bit types and it is messy to recognize the operations after that.
4743 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
4744 SDValue Vec = N0.getOperand(0);
4745 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004746 EVT VT = N->getValueType(0);
4747 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004748 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4749
Owen Anderson825b72b2009-08-11 20:47:22 +00004750 if (VT == MVT::i32 &&
4751 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00004752 TLI.isTypeLegal(Vec.getValueType())) {
4753
4754 unsigned Opc = 0;
4755 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004756 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00004757 case ISD::SIGN_EXTEND:
4758 Opc = ARMISD::VGETLANEs;
4759 break;
4760 case ISD::ZERO_EXTEND:
4761 case ISD::ANY_EXTEND:
4762 Opc = ARMISD::VGETLANEu;
4763 break;
4764 }
4765 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
4766 }
4767 }
4768
4769 return SDValue();
4770}
4771
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004772/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
4773/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
4774static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
4775 const ARMSubtarget *ST) {
4776 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00004777 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004778 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
4779 // a NaN; only do the transformation when it matches that behavior.
4780
4781 // For now only do this when using NEON for FP operations; if using VFP, it
4782 // is not obvious that the benefit outweighs the cost of switching to the
4783 // NEON pipeline.
4784 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
4785 N->getValueType(0) != MVT::f32)
4786 return SDValue();
4787
4788 SDValue CondLHS = N->getOperand(0);
4789 SDValue CondRHS = N->getOperand(1);
4790 SDValue LHS = N->getOperand(2);
4791 SDValue RHS = N->getOperand(3);
4792 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
4793
4794 unsigned Opcode = 0;
4795 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00004796 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004797 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00004798 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004799 IsReversed = true ; // x CC y ? y : x
4800 } else {
4801 return SDValue();
4802 }
4803
Bob Wilsone742bb52010-02-24 22:15:53 +00004804 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004805 switch (CC) {
4806 default: break;
4807 case ISD::SETOLT:
4808 case ISD::SETOLE:
4809 case ISD::SETLT:
4810 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004811 case ISD::SETULT:
4812 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004813 // If LHS is NaN, an ordered comparison will be false and the result will
4814 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
4815 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4816 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
4817 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4818 break;
4819 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
4820 // will return -0, so vmin can only be used for unsafe math or if one of
4821 // the operands is known to be nonzero.
4822 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
4823 !UnsafeFPMath &&
4824 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4825 break;
4826 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004827 break;
4828
4829 case ISD::SETOGT:
4830 case ISD::SETOGE:
4831 case ISD::SETGT:
4832 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004833 case ISD::SETUGT:
4834 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004835 // If LHS is NaN, an ordered comparison will be false and the result will
4836 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
4837 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4838 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
4839 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4840 break;
4841 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
4842 // will return +0, so vmax can only be used for unsafe math or if one of
4843 // the operands is known to be nonzero.
4844 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
4845 !UnsafeFPMath &&
4846 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4847 break;
4848 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004849 break;
4850 }
4851
4852 if (!Opcode)
4853 return SDValue();
4854 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
4855}
4856
Dan Gohman475871a2008-07-27 21:46:04 +00004857SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004858 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004859 switch (N->getOpcode()) {
4860 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004861 case ISD::ADD: return PerformADDCombine(N, DCI);
4862 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004863 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004864 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Jim Grosbache5165492009-11-09 00:11:35 +00004865 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson9e82bf12010-07-14 01:22:12 +00004866 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004867 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004868 case ISD::SHL:
4869 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004870 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004871 case ISD::SIGN_EXTEND:
4872 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004873 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
4874 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004875 }
Dan Gohman475871a2008-07-27 21:46:04 +00004876 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004877}
4878
Bill Wendlingaf566342009-08-15 21:21:19 +00004879bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
4880 if (!Subtarget->hasV6Ops())
4881 // Pre-v6 does not support unaligned mem access.
4882 return false;
Bob Wilson86fe66d2010-06-25 04:12:31 +00004883
4884 // v6+ may or may not support unaligned mem access depending on the system
4885 // configuration.
4886 // FIXME: This is pretty conservative. Should we provide cmdline option to
4887 // control the behaviour?
4888 if (!Subtarget->isTargetDarwin())
4889 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00004890
4891 switch (VT.getSimpleVT().SimpleTy) {
4892 default:
4893 return false;
4894 case MVT::i8:
4895 case MVT::i16:
4896 case MVT::i32:
4897 return true;
4898 // FIXME: VLD1 etc with standard alignment is legal.
4899 }
4900}
4901
Evan Chenge6c835f2009-08-14 20:09:37 +00004902static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
4903 if (V < 0)
4904 return false;
4905
4906 unsigned Scale = 1;
4907 switch (VT.getSimpleVT().SimpleTy) {
4908 default: return false;
4909 case MVT::i1:
4910 case MVT::i8:
4911 // Scale == 1;
4912 break;
4913 case MVT::i16:
4914 // Scale == 2;
4915 Scale = 2;
4916 break;
4917 case MVT::i32:
4918 // Scale == 4;
4919 Scale = 4;
4920 break;
4921 }
4922
4923 if ((V & (Scale - 1)) != 0)
4924 return false;
4925 V /= Scale;
4926 return V == (V & ((1LL << 5) - 1));
4927}
4928
4929static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
4930 const ARMSubtarget *Subtarget) {
4931 bool isNeg = false;
4932 if (V < 0) {
4933 isNeg = true;
4934 V = - V;
4935 }
4936
4937 switch (VT.getSimpleVT().SimpleTy) {
4938 default: return false;
4939 case MVT::i1:
4940 case MVT::i8:
4941 case MVT::i16:
4942 case MVT::i32:
4943 // + imm12 or - imm8
4944 if (isNeg)
4945 return V == (V & ((1LL << 8) - 1));
4946 return V == (V & ((1LL << 12) - 1));
4947 case MVT::f32:
4948 case MVT::f64:
4949 // Same as ARM mode. FIXME: NEON?
4950 if (!Subtarget->hasVFP2())
4951 return false;
4952 if ((V & 3) != 0)
4953 return false;
4954 V >>= 2;
4955 return V == (V & ((1LL << 8) - 1));
4956 }
4957}
4958
Evan Chengb01fad62007-03-12 23:30:29 +00004959/// isLegalAddressImmediate - Return true if the integer value can be used
4960/// as the offset of the target addressing mode for load / store of the
4961/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00004962static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004963 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00004964 if (V == 0)
4965 return true;
4966
Evan Cheng65011532009-03-09 19:15:00 +00004967 if (!VT.isSimple())
4968 return false;
4969
Evan Chenge6c835f2009-08-14 20:09:37 +00004970 if (Subtarget->isThumb1Only())
4971 return isLegalT1AddressImmediate(V, VT);
4972 else if (Subtarget->isThumb2())
4973 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00004974
Evan Chenge6c835f2009-08-14 20:09:37 +00004975 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00004976 if (V < 0)
4977 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00004978 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00004979 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004980 case MVT::i1:
4981 case MVT::i8:
4982 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00004983 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004984 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004985 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00004986 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004987 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004988 case MVT::f32:
4989 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00004990 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00004991 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00004992 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00004993 return false;
4994 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004995 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00004996 }
Evan Chenga8e29892007-01-19 07:51:42 +00004997}
4998
Evan Chenge6c835f2009-08-14 20:09:37 +00004999bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
5000 EVT VT) const {
5001 int Scale = AM.Scale;
5002 if (Scale < 0)
5003 return false;
5004
5005 switch (VT.getSimpleVT().SimpleTy) {
5006 default: return false;
5007 case MVT::i1:
5008 case MVT::i8:
5009 case MVT::i16:
5010 case MVT::i32:
5011 if (Scale == 1)
5012 return true;
5013 // r + r << imm
5014 Scale = Scale & ~1;
5015 return Scale == 2 || Scale == 4 || Scale == 8;
5016 case MVT::i64:
5017 // r + r
5018 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
5019 return true;
5020 return false;
5021 case MVT::isVoid:
5022 // Note, we allow "void" uses (basically, uses that aren't loads or
5023 // stores), because arm allows folding a scale into many arithmetic
5024 // operations. This should be made more precise and revisited later.
5025
5026 // Allow r << imm, but the imm has to be a multiple of two.
5027 if (Scale & 1) return false;
5028 return isPowerOf2_32(Scale);
5029 }
5030}
5031
Chris Lattner37caf8c2007-04-09 23:33:39 +00005032/// isLegalAddressingMode - Return true if the addressing mode represented
5033/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005034bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00005035 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005036 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00005037 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00005038 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005039
Chris Lattner37caf8c2007-04-09 23:33:39 +00005040 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005041 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005042 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005043
Chris Lattner37caf8c2007-04-09 23:33:39 +00005044 switch (AM.Scale) {
5045 case 0: // no scale reg, must be "r+i" or "r", or "i".
5046 break;
5047 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00005048 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00005049 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00005050 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00005051 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00005052 // ARM doesn't support any R+R*scale+imm addr modes.
5053 if (AM.BaseOffs)
5054 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005055
Bob Wilson2c7dab12009-04-08 17:55:28 +00005056 if (!VT.isSimple())
5057 return false;
5058
Evan Chenge6c835f2009-08-14 20:09:37 +00005059 if (Subtarget->isThumb2())
5060 return isLegalT2ScaledAddressingMode(AM, VT);
5061
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005062 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00005063 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00005064 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00005065 case MVT::i1:
5066 case MVT::i8:
5067 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005068 if (Scale < 0) Scale = -Scale;
5069 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005070 return true;
5071 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00005072 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00005073 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00005074 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00005075 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005076 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005077 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00005078 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005079
Owen Anderson825b72b2009-08-11 20:47:22 +00005080 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00005081 // Note, we allow "void" uses (basically, uses that aren't loads or
5082 // stores), because arm allows folding a scale into many arithmetic
5083 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005084
Chris Lattner37caf8c2007-04-09 23:33:39 +00005085 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00005086 if (Scale & 1) return false;
5087 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00005088 }
5089 break;
Evan Chengb01fad62007-03-12 23:30:29 +00005090 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00005091 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00005092}
5093
Evan Cheng77e47512009-11-11 19:05:52 +00005094/// isLegalICmpImmediate - Return true if the specified immediate is legal
5095/// icmp immediate, that is the target has icmp instructions which can compare
5096/// a register against the immediate without having to materialize the
5097/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00005098bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00005099 if (!Subtarget->isThumb())
5100 return ARM_AM::getSOImmVal(Imm) != -1;
5101 if (Subtarget->isThumb2())
5102 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00005103 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00005104}
5105
Owen Andersone50ed302009-08-10 22:56:29 +00005106static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00005107 bool isSEXTLoad, SDValue &Base,
5108 SDValue &Offset, bool &isInc,
5109 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00005110 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
5111 return false;
5112
Owen Anderson825b72b2009-08-11 20:47:22 +00005113 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00005114 // AddressingMode 3
5115 Base = Ptr->getOperand(0);
5116 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005117 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005118 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005119 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00005120 isInc = false;
5121 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5122 return true;
5123 }
5124 }
5125 isInc = (Ptr->getOpcode() == ISD::ADD);
5126 Offset = Ptr->getOperand(1);
5127 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00005128 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00005129 // AddressingMode 2
5130 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005131 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005132 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005133 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00005134 isInc = false;
5135 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5136 Base = Ptr->getOperand(0);
5137 return true;
5138 }
5139 }
5140
5141 if (Ptr->getOpcode() == ISD::ADD) {
5142 isInc = true;
5143 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
5144 if (ShOpcVal != ARM_AM::no_shift) {
5145 Base = Ptr->getOperand(1);
5146 Offset = Ptr->getOperand(0);
5147 } else {
5148 Base = Ptr->getOperand(0);
5149 Offset = Ptr->getOperand(1);
5150 }
5151 return true;
5152 }
5153
5154 isInc = (Ptr->getOpcode() == ISD::ADD);
5155 Base = Ptr->getOperand(0);
5156 Offset = Ptr->getOperand(1);
5157 return true;
5158 }
5159
Jim Grosbache5165492009-11-09 00:11:35 +00005160 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00005161 return false;
5162}
5163
Owen Andersone50ed302009-08-10 22:56:29 +00005164static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00005165 bool isSEXTLoad, SDValue &Base,
5166 SDValue &Offset, bool &isInc,
5167 SelectionDAG &DAG) {
5168 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
5169 return false;
5170
5171 Base = Ptr->getOperand(0);
5172 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
5173 int RHSC = (int)RHS->getZExtValue();
5174 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
5175 assert(Ptr->getOpcode() == ISD::ADD);
5176 isInc = false;
5177 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5178 return true;
5179 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
5180 isInc = Ptr->getOpcode() == ISD::ADD;
5181 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
5182 return true;
5183 }
5184 }
5185
5186 return false;
5187}
5188
Evan Chenga8e29892007-01-19 07:51:42 +00005189/// getPreIndexedAddressParts - returns true by value, base pointer and
5190/// offset pointer and addressing mode by reference if the node's address
5191/// can be legally represented as pre-indexed load / store address.
5192bool
Dan Gohman475871a2008-07-27 21:46:04 +00005193ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
5194 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00005195 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00005196 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005197 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00005198 return false;
5199
Owen Andersone50ed302009-08-10 22:56:29 +00005200 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00005201 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00005202 bool isSEXTLoad = false;
5203 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5204 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005205 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00005206 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
5207 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
5208 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005209 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00005210 } else
5211 return false;
5212
5213 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00005214 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00005215 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00005216 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
5217 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00005218 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00005219 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00005220 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00005221 if (!isLegal)
5222 return false;
5223
5224 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
5225 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00005226}
5227
5228/// getPostIndexedAddressParts - returns true by value, base pointer and
5229/// offset pointer and addressing mode by reference if this node can be
5230/// combined with a load / store to form a post-indexed load / store.
5231bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00005232 SDValue &Base,
5233 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00005234 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00005235 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005236 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00005237 return false;
5238
Owen Andersone50ed302009-08-10 22:56:29 +00005239 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00005240 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00005241 bool isSEXTLoad = false;
5242 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005243 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00005244 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00005245 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
5246 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005247 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00005248 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00005249 } else
5250 return false;
5251
5252 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00005253 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00005254 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00005255 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00005256 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00005257 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00005258 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
5259 isInc, DAG);
5260 if (!isLegal)
5261 return false;
5262
Evan Cheng28dad2a2010-05-18 21:31:17 +00005263 if (Ptr != Base) {
5264 // Swap base ptr and offset to catch more post-index load / store when
5265 // it's legal. In Thumb2 mode, offset must be an immediate.
5266 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
5267 !Subtarget->isThumb2())
5268 std::swap(Base, Offset);
5269
5270 // Post-indexed load / store update the base pointer.
5271 if (Ptr != Base)
5272 return false;
5273 }
5274
Evan Chenge88d5ce2009-07-02 07:28:31 +00005275 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
5276 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00005277}
5278
Dan Gohman475871a2008-07-27 21:46:04 +00005279void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00005280 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00005281 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005282 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00005283 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00005284 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005285 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00005286 switch (Op.getOpcode()) {
5287 default: break;
5288 case ARMISD::CMOV: {
5289 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00005290 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00005291 if (KnownZero == 0 && KnownOne == 0) return;
5292
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005293 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00005294 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
5295 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00005296 KnownZero &= KnownZeroRHS;
5297 KnownOne &= KnownOneRHS;
5298 return;
5299 }
5300 }
5301}
5302
5303//===----------------------------------------------------------------------===//
5304// ARM Inline Assembly Support
5305//===----------------------------------------------------------------------===//
5306
5307/// getConstraintType - Given a constraint letter, return the type of
5308/// constraint it is for this target.
5309ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00005310ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
5311 if (Constraint.size() == 1) {
5312 switch (Constraint[0]) {
5313 default: break;
5314 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005315 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00005316 }
Evan Chenga8e29892007-01-19 07:51:42 +00005317 }
Chris Lattner4234f572007-03-25 02:14:49 +00005318 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00005319}
5320
Bob Wilson2dc4f542009-03-20 22:42:55 +00005321std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00005322ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00005323 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005324 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00005325 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00005326 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005327 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00005328 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00005329 return std::make_pair(0U, ARM::tGPRRegisterClass);
5330 else
5331 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005332 case 'r':
5333 return std::make_pair(0U, ARM::GPRRegisterClass);
5334 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00005335 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005336 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00005337 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005338 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00005339 if (VT.getSizeInBits() == 128)
5340 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005341 break;
Evan Chenga8e29892007-01-19 07:51:42 +00005342 }
5343 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00005344 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00005345 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00005346
Evan Chenga8e29892007-01-19 07:51:42 +00005347 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
5348}
5349
5350std::vector<unsigned> ARMTargetLowering::
5351getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00005352 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005353 if (Constraint.size() != 1)
5354 return std::vector<unsigned>();
5355
5356 switch (Constraint[0]) { // GCC ARM Constraint Letters
5357 default: break;
5358 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00005359 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
5360 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
5361 0);
Evan Chenga8e29892007-01-19 07:51:42 +00005362 case 'r':
5363 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
5364 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
5365 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
5366 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005367 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00005368 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005369 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
5370 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
5371 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
5372 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
5373 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
5374 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
5375 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
5376 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00005377 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005378 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
5379 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
5380 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
5381 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00005382 if (VT.getSizeInBits() == 128)
5383 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
5384 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005385 break;
Evan Chenga8e29892007-01-19 07:51:42 +00005386 }
5387
5388 return std::vector<unsigned>();
5389}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005390
5391/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
5392/// vector. If it is invalid, don't add anything to Ops.
5393void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
5394 char Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005395 std::vector<SDValue>&Ops,
5396 SelectionDAG &DAG) const {
5397 SDValue Result(0, 0);
5398
5399 switch (Constraint) {
5400 default: break;
5401 case 'I': case 'J': case 'K': case 'L':
5402 case 'M': case 'N': case 'O':
5403 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
5404 if (!C)
5405 return;
5406
5407 int64_t CVal64 = C->getSExtValue();
5408 int CVal = (int) CVal64;
5409 // None of these constraints allow values larger than 32 bits. Check
5410 // that the value fits in an int.
5411 if (CVal != CVal64)
5412 return;
5413
5414 switch (Constraint) {
5415 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005416 if (Subtarget->isThumb1Only()) {
5417 // This must be a constant between 0 and 255, for ADD
5418 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005419 if (CVal >= 0 && CVal <= 255)
5420 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005421 } else if (Subtarget->isThumb2()) {
5422 // A constant that can be used as an immediate value in a
5423 // data-processing instruction.
5424 if (ARM_AM::getT2SOImmVal(CVal) != -1)
5425 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005426 } else {
5427 // A constant that can be used as an immediate value in a
5428 // data-processing instruction.
5429 if (ARM_AM::getSOImmVal(CVal) != -1)
5430 break;
5431 }
5432 return;
5433
5434 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005435 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005436 // This must be a constant between -255 and -1, for negated ADD
5437 // immediates. This can be used in GCC with an "n" modifier that
5438 // prints the negated value, for use with SUB instructions. It is
5439 // not useful otherwise but is implemented for compatibility.
5440 if (CVal >= -255 && CVal <= -1)
5441 break;
5442 } else {
5443 // This must be a constant between -4095 and 4095. It is not clear
5444 // what this constraint is intended for. Implemented for
5445 // compatibility with GCC.
5446 if (CVal >= -4095 && CVal <= 4095)
5447 break;
5448 }
5449 return;
5450
5451 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005452 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005453 // A 32-bit value where only one byte has a nonzero value. Exclude
5454 // zero to match GCC. This constraint is used by GCC internally for
5455 // constants that can be loaded with a move/shift combination.
5456 // It is not useful otherwise but is implemented for compatibility.
5457 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
5458 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005459 } else if (Subtarget->isThumb2()) {
5460 // A constant whose bitwise inverse can be used as an immediate
5461 // value in a data-processing instruction. This can be used in GCC
5462 // with a "B" modifier that prints the inverted value, for use with
5463 // BIC and MVN instructions. It is not useful otherwise but is
5464 // implemented for compatibility.
5465 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
5466 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005467 } else {
5468 // A constant whose bitwise inverse can be used as an immediate
5469 // value in a data-processing instruction. This can be used in GCC
5470 // with a "B" modifier that prints the inverted value, for use with
5471 // BIC and MVN instructions. It is not useful otherwise but is
5472 // implemented for compatibility.
5473 if (ARM_AM::getSOImmVal(~CVal) != -1)
5474 break;
5475 }
5476 return;
5477
5478 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005479 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005480 // This must be a constant between -7 and 7,
5481 // for 3-operand ADD/SUB immediate instructions.
5482 if (CVal >= -7 && CVal < 7)
5483 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005484 } else if (Subtarget->isThumb2()) {
5485 // A constant whose negation can be used as an immediate value in a
5486 // data-processing instruction. This can be used in GCC with an "n"
5487 // modifier that prints the negated value, for use with SUB
5488 // instructions. It is not useful otherwise but is implemented for
5489 // compatibility.
5490 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
5491 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005492 } else {
5493 // A constant whose negation can be used as an immediate value in a
5494 // data-processing instruction. This can be used in GCC with an "n"
5495 // modifier that prints the negated value, for use with SUB
5496 // instructions. It is not useful otherwise but is implemented for
5497 // compatibility.
5498 if (ARM_AM::getSOImmVal(-CVal) != -1)
5499 break;
5500 }
5501 return;
5502
5503 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005504 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005505 // This must be a multiple of 4 between 0 and 1020, for
5506 // ADD sp + immediate.
5507 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
5508 break;
5509 } else {
5510 // A power of two or a constant between 0 and 32. This is used in
5511 // GCC for the shift amount on shifted register operands, but it is
5512 // useful in general for any shift amounts.
5513 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
5514 break;
5515 }
5516 return;
5517
5518 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005519 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005520 // This must be a constant between 0 and 31, for shift amounts.
5521 if (CVal >= 0 && CVal <= 31)
5522 break;
5523 }
5524 return;
5525
5526 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005527 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005528 // This must be a multiple of 4 between -508 and 508, for
5529 // ADD/SUB sp = sp + immediate.
5530 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
5531 break;
5532 }
5533 return;
5534 }
5535 Result = DAG.getTargetConstant(CVal, Op.getValueType());
5536 break;
5537 }
5538
5539 if (Result.getNode()) {
5540 Ops.push_back(Result);
5541 return;
5542 }
Dale Johannesen1784d162010-06-25 21:55:36 +00005543 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005544}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00005545
5546bool
5547ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
5548 // The ARM target isn't yet aware of offsets.
5549 return false;
5550}
Evan Cheng39382422009-10-28 01:44:26 +00005551
5552int ARM::getVFPf32Imm(const APFloat &FPImm) {
5553 APInt Imm = FPImm.bitcastToAPInt();
5554 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
5555 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
5556 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
5557
5558 // We can handle 4 bits of mantissa.
5559 // mantissa = (16+UInt(e:f:g:h))/16.
5560 if (Mantissa & 0x7ffff)
5561 return -1;
5562 Mantissa >>= 19;
5563 if ((Mantissa & 0xf) != Mantissa)
5564 return -1;
5565
5566 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
5567 if (Exp < -3 || Exp > 4)
5568 return -1;
5569 Exp = ((Exp+3) & 0x7) ^ 4;
5570
5571 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
5572}
5573
5574int ARM::getVFPf64Imm(const APFloat &FPImm) {
5575 APInt Imm = FPImm.bitcastToAPInt();
5576 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
5577 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
5578 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
5579
5580 // We can handle 4 bits of mantissa.
5581 // mantissa = (16+UInt(e:f:g:h))/16.
5582 if (Mantissa & 0xffffffffffffLL)
5583 return -1;
5584 Mantissa >>= 48;
5585 if ((Mantissa & 0xf) != Mantissa)
5586 return -1;
5587
5588 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
5589 if (Exp < -3 || Exp > 4)
5590 return -1;
5591 Exp = ((Exp+3) & 0x7) ^ 4;
5592
5593 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
5594}
5595
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005596bool ARM::isBitFieldInvertedMask(unsigned v) {
5597 if (v == 0xffffffff)
5598 return 0;
5599 // there can be 1's on either or both "outsides", all the "inside"
5600 // bits must be 0's
5601 unsigned int lsb = 0, msb = 31;
5602 while (v & (1 << msb)) --msb;
5603 while (v & (1 << lsb)) ++lsb;
5604 for (unsigned int i = lsb; i <= msb; ++i) {
5605 if (v & (1 << i))
5606 return 0;
5607 }
5608 return 1;
5609}
5610
Evan Cheng39382422009-10-28 01:44:26 +00005611/// isFPImmLegal - Returns true if the target can instruction select the
5612/// specified FP immediate natively. If false, the legalizer will
5613/// materialize the FP immediate as a load from a constant pool.
5614bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
5615 if (!Subtarget->hasVFP3())
5616 return false;
5617 if (VT == MVT::f32)
5618 return ARM::getVFPf32Imm(Imm) != -1;
5619 if (VT == MVT::f64)
5620 return ARM::getVFPf64Imm(Imm) != -1;
5621 return false;
5622}