blob: 5a03a07dccac1b72d4d39d111ea2c7d3e1e1579f [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Dale Johannesen51e28e62010-06-03 21:09:53 +000015#define DEBUG_TYPE "arm-isel"
Evan Chenga8e29892007-01-19 07:51:42 +000016#include "ARM.h"
17#include "ARMAddressingModes.h"
18#include "ARMConstantPoolValue.h"
19#include "ARMISelLowering.h"
20#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000021#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000022#include "ARMRegisterInfo.h"
23#include "ARMSubtarget.h"
24#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000025#include "ARMTargetObjectFile.h"
Evan Chenga8e29892007-01-19 07:51:42 +000026#include "llvm/CallingConv.h"
27#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000028#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000029#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000030#include "llvm/Instruction.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000031#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000032#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000033#include "llvm/CodeGen/CallingConvLower.h"
Evan Chenga8e29892007-01-19 07:51:42 +000034#include "llvm/CodeGen/MachineBasicBlock.h"
35#include "llvm/CodeGen/MachineFrameInfo.h"
36#include "llvm/CodeGen/MachineFunction.h"
37#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000038#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000039#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000040#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000041#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000042#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000043#include "llvm/ADT/VectorExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000044#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000045#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000046#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000047#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000048#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000049#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000050using namespace llvm;
51
Dale Johannesen51e28e62010-06-03 21:09:53 +000052STATISTIC(NumTailCalls, "Number of tail calls");
53
Bob Wilson703af3a2010-08-13 22:43:33 +000054// This option should go away when tail calls fully work.
55static cl::opt<bool>
56EnableARMTailCalls("arm-tail-calls", cl::Hidden,
57 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
58 cl::init(false));
59
Dale Johannesenf630c712010-07-29 20:10:08 +000060// This option should go away when Machine LICM is smart enough to hoist a
61// reg-to-reg VDUP.
62static cl::opt<bool>
63EnableARMVDUPsplat("arm-vdup-splat", cl::Hidden,
64 cl::desc("Generate VDUP for integer constant splats (TEMPORARY OPTION)."),
65 cl::init(false));
66
Jim Grosbache7b52522010-04-14 22:28:31 +000067static cl::opt<bool>
68EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000069 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbache7b52522010-04-14 22:28:31 +000070 cl::init(false));
71
Evan Cheng46df4eb2010-06-16 07:35:02 +000072static cl::opt<bool>
73ARMInterworking("arm-interworking", cl::Hidden,
74 cl::desc("Enable / disable ARM interworking (for debugging only)"),
75 cl::init(true));
76
Evan Chengf6799392010-06-26 01:52:05 +000077static cl::opt<bool>
78EnableARMCodePlacement("arm-code-placement", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000079 cl::desc("Enable code placement pass for ARM"),
Evan Chengf6799392010-06-26 01:52:05 +000080 cl::init(false));
81
Owen Andersone50ed302009-08-10 22:56:29 +000082static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000083 CCValAssign::LocInfo &LocInfo,
84 ISD::ArgFlagsTy &ArgFlags,
85 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000086static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000087 CCValAssign::LocInfo &LocInfo,
88 ISD::ArgFlagsTy &ArgFlags,
89 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000090static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000091 CCValAssign::LocInfo &LocInfo,
92 ISD::ArgFlagsTy &ArgFlags,
93 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000094static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000095 CCValAssign::LocInfo &LocInfo,
96 ISD::ArgFlagsTy &ArgFlags,
97 CCState &State);
98
Owen Andersone50ed302009-08-10 22:56:29 +000099void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
100 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000101 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000102 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000103 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
104 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000105
Owen Anderson70671842009-08-10 20:18:46 +0000106 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000107 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000108 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000109 }
110
Owen Andersone50ed302009-08-10 22:56:29 +0000111 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000112 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +0000113 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000114 if (ElemTy == MVT::i8 || ElemTy == MVT::i16)
Owen Anderson70671842009-08-10 20:18:46 +0000115 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +0000116 if (ElemTy != MVT::i32) {
117 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
118 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
119 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
120 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
121 }
Owen Anderson70671842009-08-10 20:18:46 +0000122 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
123 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +0000124 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Anton Korobeynikov8e6c2b92009-08-21 12:40:35 +0000125 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
Bob Wilsond0910c42010-04-06 22:02:24 +0000126 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
127 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000128 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000129 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
130 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
131 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob 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);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000275 if (!Subtarget->isFPOnlySP())
276 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000277
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000279 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000280
281 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000282 addDRTypeForNEON(MVT::v2f32);
283 addDRTypeForNEON(MVT::v8i8);
284 addDRTypeForNEON(MVT::v4i16);
285 addDRTypeForNEON(MVT::v2i32);
286 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000287
Owen Anderson825b72b2009-08-11 20:47:22 +0000288 addQRTypeForNEON(MVT::v4f32);
289 addQRTypeForNEON(MVT::v2f64);
290 addQRTypeForNEON(MVT::v16i8);
291 addQRTypeForNEON(MVT::v8i16);
292 addQRTypeForNEON(MVT::v4i32);
293 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000294
Bob Wilson74dc72e2009-09-15 23:55:57 +0000295 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
296 // neither Neon nor VFP support any arithmetic operations on it.
297 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
298 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
299 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
300 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
301 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
302 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
303 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
304 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
305 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
306 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
307 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
308 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
309 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
310 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
311 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
312 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
313 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
314 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
315 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
316 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
317 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
318 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
319 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
320 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
321
Bob Wilson642b3292009-09-16 00:32:15 +0000322 // Neon does not support some operations on v1i64 and v2i64 types.
323 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
324 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
325 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
326 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
327
Bob Wilson5bafff32009-06-22 23:27:02 +0000328 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
329 setTargetDAGCombine(ISD::SHL);
330 setTargetDAGCombine(ISD::SRL);
331 setTargetDAGCombine(ISD::SRA);
332 setTargetDAGCombine(ISD::SIGN_EXTEND);
333 setTargetDAGCombine(ISD::ZERO_EXTEND);
334 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000335 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson5bafff32009-06-22 23:27:02 +0000336 }
337
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000338 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000339
340 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000341 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000342
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000343 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000344 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000345
Evan Chenga8e29892007-01-19 07:51:42 +0000346 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000347 if (!Subtarget->isThumb1Only()) {
348 for (unsigned im = (unsigned)ISD::PRE_INC;
349 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000350 setIndexedLoadAction(im, MVT::i1, Legal);
351 setIndexedLoadAction(im, MVT::i8, Legal);
352 setIndexedLoadAction(im, MVT::i16, Legal);
353 setIndexedLoadAction(im, MVT::i32, Legal);
354 setIndexedStoreAction(im, MVT::i1, Legal);
355 setIndexedStoreAction(im, MVT::i8, Legal);
356 setIndexedStoreAction(im, MVT::i16, Legal);
357 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000358 }
Evan Chenga8e29892007-01-19 07:51:42 +0000359 }
360
361 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000362 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000363 setOperationAction(ISD::MUL, MVT::i64, Expand);
364 setOperationAction(ISD::MULHU, MVT::i32, Expand);
365 setOperationAction(ISD::MULHS, MVT::i32, Expand);
366 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
367 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000368 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000369 setOperationAction(ISD::MUL, MVT::i64, Expand);
370 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000371 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000373 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000374 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000375 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000376 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000377 setOperationAction(ISD::SRL, MVT::i64, Custom);
378 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000379
380 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000381 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000382 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000383 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000384 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000386
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000387 // Only ARMv6 has BSWAP.
388 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000389 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000390
Evan Chenga8e29892007-01-19 07:51:42 +0000391 // These are expanded into libcalls.
Jim Grosbach29402132010-05-05 23:44:43 +0000392 if (!Subtarget->hasDivide()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000393 // v7M has a hardware divider
394 setOperationAction(ISD::SDIV, MVT::i32, Expand);
395 setOperationAction(ISD::UDIV, MVT::i32, Expand);
396 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000397 setOperationAction(ISD::SREM, MVT::i32, Expand);
398 setOperationAction(ISD::UREM, MVT::i32, Expand);
399 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
400 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000401
Owen Anderson825b72b2009-08-11 20:47:22 +0000402 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
403 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
404 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
405 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000406 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000407
Evan Chengfb3611d2010-05-11 07:26:32 +0000408 setOperationAction(ISD::TRAP, MVT::Other, Legal);
409
Evan Chenga8e29892007-01-19 07:51:42 +0000410 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::VASTART, MVT::Other, Custom);
412 setOperationAction(ISD::VAARG, MVT::Other, Expand);
413 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
414 setOperationAction(ISD::VAEND, MVT::Other, Expand);
415 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
416 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000417 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
418 // FIXME: Shouldn't need this, since no register is used, but the legalizer
419 // doesn't yet know how to not do that for SjLj.
420 setExceptionSelectorRegister(ARM::R0);
Evan Cheng3a1588a2010-04-15 22:20:34 +0000421 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000422 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
423 // the default expansion.
424 if (Subtarget->hasDataBarrier() ||
425 (Subtarget->hasV6Ops() && !Subtarget->isThumb1Only())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000426 // membarrier needs custom lowering; the rest are legal and handled
427 // normally.
428 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
429 } else {
430 // Set them all for expansion, which will force libcalls.
431 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
432 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Expand);
433 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Expand);
434 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000435 setOperationAction(ISD::ATOMIC_SWAP, MVT::i8, Expand);
436 setOperationAction(ISD::ATOMIC_SWAP, MVT::i16, Expand);
437 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000438 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i8, Expand);
439 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i16, Expand);
440 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
441 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Expand);
442 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Expand);
443 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
444 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i8, Expand);
445 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i16, Expand);
446 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
447 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i8, Expand);
448 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i16, Expand);
449 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
450 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i8, Expand);
451 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i16, Expand);
452 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
453 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i8, Expand);
454 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i16, Expand);
455 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000456 // Since the libcalls include locking, fold in the fences
457 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000458 }
459 // 64-bit versions are always libcalls (for now)
460 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000461 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000462 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Expand);
463 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Expand);
464 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Expand);
465 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Expand);
466 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Expand);
467 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000468
Eli Friedmana2c6f452010-06-26 04:36:50 +0000469 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
470 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000471 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
472 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000473 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000474 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000475
Nate Begemand1fb5832010-08-03 21:31:55 +0000476 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000477 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
478 // iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000479 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000480 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
481 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000482
483 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000484 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000485 if (Subtarget->isTargetDarwin()) {
486 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
487 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
488 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000489
Owen Anderson825b72b2009-08-11 20:47:22 +0000490 setOperationAction(ISD::SETCC, MVT::i32, Expand);
491 setOperationAction(ISD::SETCC, MVT::f32, Expand);
492 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000493 setOperationAction(ISD::SELECT, MVT::i32, Custom);
494 setOperationAction(ISD::SELECT, MVT::f32, Custom);
495 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000496 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
497 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
498 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000499
Owen Anderson825b72b2009-08-11 20:47:22 +0000500 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
501 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
502 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
503 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
504 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000505
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000506 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000507 setOperationAction(ISD::FSIN, MVT::f64, Expand);
508 setOperationAction(ISD::FSIN, MVT::f32, Expand);
509 setOperationAction(ISD::FCOS, MVT::f32, Expand);
510 setOperationAction(ISD::FCOS, MVT::f64, Expand);
511 setOperationAction(ISD::FREM, MVT::f64, Expand);
512 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000513 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000514 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
515 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000516 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 setOperationAction(ISD::FPOW, MVT::f64, Expand);
518 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000519
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000520 // Various VFP goodness
521 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000522 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
523 if (Subtarget->hasVFP2()) {
524 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
525 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
526 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
527 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
528 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000529 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000530 if (!Subtarget->hasFP16()) {
531 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
532 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000533 }
Evan Cheng110cf482008-04-01 01:50:16 +0000534 }
Evan Chenga8e29892007-01-19 07:51:42 +0000535
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000536 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000537 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000538 setTargetDAGCombine(ISD::ADD);
539 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000540 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000541
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000542 if (Subtarget->hasV6T2Ops())
543 setTargetDAGCombine(ISD::OR);
544
Evan Chenga8e29892007-01-19 07:51:42 +0000545 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000546
Evan Chengf7d87ee2010-05-21 00:43:17 +0000547 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
548 setSchedulingPreference(Sched::RegPressure);
549 else
550 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000551
552 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Evan Chengf6799392010-06-26 01:52:05 +0000553
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000554 // On ARM arguments smaller than 4 bytes are extended, so all arguments
555 // are at least 4 bytes aligned.
556 setMinStackArgumentAlignment(4);
557
Evan Chengf6799392010-06-26 01:52:05 +0000558 if (EnableARMCodePlacement)
559 benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000560}
561
Evan Cheng4f6b4672010-07-21 06:09:07 +0000562std::pair<const TargetRegisterClass*, uint8_t>
563ARMTargetLowering::findRepresentativeClass(EVT VT) const{
564 const TargetRegisterClass *RRC = 0;
565 uint8_t Cost = 1;
566 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000567 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000568 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000569 // Use DPR as representative register class for all floating point
570 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
571 // the cost is 1 for both f32 and f64.
572 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000573 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000574 RRC = ARM::DPRRegisterClass;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000575 break;
576 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
577 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000578 RRC = ARM::DPRRegisterClass;
579 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000580 break;
581 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000582 RRC = ARM::DPRRegisterClass;
583 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000584 break;
585 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000586 RRC = ARM::DPRRegisterClass;
587 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000588 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000589 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000590 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000591}
592
Evan Chenga8e29892007-01-19 07:51:42 +0000593const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
594 switch (Opcode) {
595 default: return 0;
596 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000597 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
598 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000599 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000600 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
601 case ARMISD::tCALL: return "ARMISD::tCALL";
602 case ARMISD::BRCOND: return "ARMISD::BRCOND";
603 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000604 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000605 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
606 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
607 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000608 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000609 case ARMISD::CMPFP: return "ARMISD::CMPFP";
610 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000611 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000612 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
613 case ARMISD::CMOV: return "ARMISD::CMOV";
614 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000615
Jim Grosbach3482c802010-01-18 19:58:49 +0000616 case ARMISD::RBIT: return "ARMISD::RBIT";
617
Bob Wilson76a312b2010-03-19 22:51:32 +0000618 case ARMISD::FTOSI: return "ARMISD::FTOSI";
619 case ARMISD::FTOUI: return "ARMISD::FTOUI";
620 case ARMISD::SITOF: return "ARMISD::SITOF";
621 case ARMISD::UITOF: return "ARMISD::UITOF";
622
Evan Chenga8e29892007-01-19 07:51:42 +0000623 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
624 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
625 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000626
Jim Grosbache5165492009-11-09 00:11:35 +0000627 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
628 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000629
Evan Chengc5942082009-10-28 06:55:03 +0000630 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
631 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
632
Dale Johannesen51e28e62010-06-03 21:09:53 +0000633 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
634
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000635 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000636
Evan Cheng86198642009-08-07 00:34:42 +0000637 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
638
Jim Grosbach3728e962009-12-10 00:11:09 +0000639 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
640 case ARMISD::SYNCBARRIER: return "ARMISD::SYNCBARRIER";
641
Bob Wilson5bafff32009-06-22 23:27:02 +0000642 case ARMISD::VCEQ: return "ARMISD::VCEQ";
643 case ARMISD::VCGE: return "ARMISD::VCGE";
644 case ARMISD::VCGEU: return "ARMISD::VCGEU";
645 case ARMISD::VCGT: return "ARMISD::VCGT";
646 case ARMISD::VCGTU: return "ARMISD::VCGTU";
647 case ARMISD::VTST: return "ARMISD::VTST";
648
649 case ARMISD::VSHL: return "ARMISD::VSHL";
650 case ARMISD::VSHRs: return "ARMISD::VSHRs";
651 case ARMISD::VSHRu: return "ARMISD::VSHRu";
652 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
653 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
654 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
655 case ARMISD::VSHRN: return "ARMISD::VSHRN";
656 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
657 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
658 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
659 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
660 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
661 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
662 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
663 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
664 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
665 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
666 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
667 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
668 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
669 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000670 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000671 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000672 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000673 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000674 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000675 case ARMISD::VREV64: return "ARMISD::VREV64";
676 case ARMISD::VREV32: return "ARMISD::VREV32";
677 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000678 case ARMISD::VZIP: return "ARMISD::VZIP";
679 case ARMISD::VUZP: return "ARMISD::VUZP";
680 case ARMISD::VTRN: return "ARMISD::VTRN";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000681 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000682 case ARMISD::FMAX: return "ARMISD::FMAX";
683 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000684 case ARMISD::BFI: return "ARMISD::BFI";
Evan Chenga8e29892007-01-19 07:51:42 +0000685 }
686}
687
Evan Cheng06b666c2010-05-15 02:18:07 +0000688/// getRegClassFor - Return the register class that should be used for the
689/// specified value type.
690TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
691 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
692 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
693 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000694 if (Subtarget->hasNEON()) {
695 if (VT == MVT::v4i64)
696 return ARM::QQPRRegisterClass;
697 else if (VT == MVT::v8i64)
698 return ARM::QQQQPRRegisterClass;
699 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000700 return TargetLowering::getRegClassFor(VT);
701}
702
Eric Christopherab695882010-07-21 22:26:11 +0000703// Create a fast isel object.
704FastISel *
705ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
706 return ARM::createFastISel(funcInfo);
707}
708
Bill Wendlingb4202b82009-07-01 18:50:55 +0000709/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000710unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Bob Wilsonb5b50572010-07-01 22:26:26 +0000711 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 1 : 2;
Bill Wendling20c568f2009-06-30 22:38:32 +0000712}
713
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000714/// getMaximalGlobalOffset - Returns the maximal possible offset which can
715/// be used for loads / stores from the global.
716unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
717 return (Subtarget->isThumb1Only() ? 127 : 4095);
718}
719
Evan Cheng1cc39842010-05-20 23:26:43 +0000720Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000721 unsigned NumVals = N->getNumValues();
722 if (!NumVals)
723 return Sched::RegPressure;
724
725 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000726 EVT VT = N->getValueType(i);
727 if (VT.isFloatingPoint() || VT.isVector())
728 return Sched::Latency;
729 }
Evan Chengc10f5432010-05-28 23:25:23 +0000730
731 if (!N->isMachineOpcode())
732 return Sched::RegPressure;
733
734 // Load are scheduled for latency even if there instruction itinerary
735 // is not available.
736 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
737 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
738 if (TID.mayLoad())
739 return Sched::Latency;
740
741 const InstrItineraryData &Itins = getTargetMachine().getInstrItineraryData();
742 if (!Itins.isEmpty() && Itins.getStageLatency(TID.getSchedClass()) > 2)
743 return Sched::Latency;
Evan Cheng1cc39842010-05-20 23:26:43 +0000744 return Sched::RegPressure;
745}
746
Evan Cheng31446872010-07-23 22:39:59 +0000747unsigned
748ARMTargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
749 MachineFunction &MF) const {
Evan Cheng31446872010-07-23 22:39:59 +0000750 switch (RC->getID()) {
751 default:
752 return 0;
753 case ARM::tGPRRegClassID:
Evan Chengac096802010-08-10 19:30:19 +0000754 return RegInfo->hasFP(MF) ? 4 : 5;
755 case ARM::GPRRegClassID: {
756 unsigned FP = RegInfo->hasFP(MF) ? 1 : 0;
757 return 10 - FP - (Subtarget->isR9Reserved() ? 1 : 0);
758 }
Evan Cheng31446872010-07-23 22:39:59 +0000759 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;
Bob Wilson703af3a2010-08-13 22:43:33 +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();
Evan Cheng11db0682010-08-11 06:22:01 +00002003 // Some subtargets which have dmb and dsb instructions can handle barriers
2004 // directly. Some ARMv6 cpus can support them with the help of mcr
2005 // instruction. Thumb1 and pre-v6 ARM mode use a libcall instead and should
Jim Grosbachc73993b2010-06-17 01:37:00 +00002006 // never get here.
2007 unsigned Opc = isDeviceBarrier ? ARMISD::SYNCBARRIER : ARMISD::MEMBARRIER;
Evan Cheng11db0682010-08-11 06:22:01 +00002008 if (Subtarget->hasDataBarrier())
Jim Grosbachc73993b2010-06-17 01:37:00 +00002009 return DAG.getNode(Opc, dl, MVT::Other, Op.getOperand(0));
Evan Cheng11db0682010-08-11 06:22:01 +00002010 else {
2011 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb1Only() &&
2012 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Jim Grosbachc73993b2010-06-17 01:37:00 +00002013 return DAG.getNode(Opc, dl, MVT::Other, Op.getOperand(0),
2014 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002015 }
Jim Grosbach3728e962009-12-10 00:11:09 +00002016}
2017
Dan Gohman1e93df62010-04-17 14:41:14 +00002018static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2019 MachineFunction &MF = DAG.getMachineFunction();
2020 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2021
Evan Chenga8e29892007-01-19 07:51:42 +00002022 // vastart just stores the address of the VarArgsFrameIndex slot into the
2023 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002024 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002025 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002026 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002027 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
David Greene1b58cab2010-02-15 16:55:24 +00002028 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
2029 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002030}
2031
Dan Gohman475871a2008-07-27 21:46:04 +00002032SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002033ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2034 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002035 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002036 MachineFunction &MF = DAG.getMachineFunction();
2037 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2038
2039 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002040 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002041 RC = ARM::tGPRRegisterClass;
2042 else
2043 RC = ARM::GPRRegisterClass;
2044
2045 // Transform the arguments stored in physical registers into virtual ones.
Evan Cheng2457f2c2010-05-22 01:47:14 +00002046 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002047 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002048
2049 SDValue ArgValue2;
2050 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002051 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002052 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002053
2054 // Create load node to retrieve arguments from the stack.
2055 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002056 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00002057 PseudoSourceValue::getFixedStack(FI), 0,
2058 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002059 } else {
2060 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002061 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002062 }
2063
Jim Grosbache5165492009-11-09 00:11:35 +00002064 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002065}
2066
2067SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002068ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002069 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002070 const SmallVectorImpl<ISD::InputArg>
2071 &Ins,
2072 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002073 SmallVectorImpl<SDValue> &InVals)
2074 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002075
Bob Wilson1f595bb2009-04-17 19:07:39 +00002076 MachineFunction &MF = DAG.getMachineFunction();
2077 MachineFrameInfo *MFI = MF.getFrameInfo();
2078
Bob Wilson1f595bb2009-04-17 19:07:39 +00002079 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2080
2081 // Assign locations to all of the incoming arguments.
2082 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002083 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
2084 *DAG.getContext());
2085 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002086 CCAssignFnForNode(CallConv, /* Return*/ false,
2087 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002088
2089 SmallVector<SDValue, 16> ArgValues;
2090
2091 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2092 CCValAssign &VA = ArgLocs[i];
2093
Bob Wilsondee46d72009-04-17 20:35:10 +00002094 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002095 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002096 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002097
Bob Wilson5bafff32009-06-22 23:27:02 +00002098 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002099 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002100 // f64 and vector types are split up into multiple registers or
2101 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002102 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002103 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002104 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002105 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002106 SDValue ArgValue2;
2107 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002108 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002109 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2110 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
2111 PseudoSourceValue::getFixedStack(FI), 0,
2112 false, false, 0);
2113 } else {
2114 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2115 Chain, DAG, dl);
2116 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002117 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2118 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002119 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002120 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002121 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2122 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002123 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002124
Bob Wilson5bafff32009-06-22 23:27:02 +00002125 } else {
2126 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002127
Owen Anderson825b72b2009-08-11 20:47:22 +00002128 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002129 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002130 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002131 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002132 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002133 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002134 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002135 RC = (AFI->isThumb1OnlyFunction() ?
2136 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002137 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002138 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002139
2140 // Transform the arguments in physical registers into virtual ones.
2141 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002142 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002143 }
2144
2145 // If this is an 8 or 16-bit value, it is really passed promoted
2146 // to 32 bits. Insert an assert[sz]ext to capture this, then
2147 // truncate to the right size.
2148 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002149 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002150 case CCValAssign::Full: break;
2151 case CCValAssign::BCvt:
2152 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
2153 break;
2154 case CCValAssign::SExt:
2155 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2156 DAG.getValueType(VA.getValVT()));
2157 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2158 break;
2159 case CCValAssign::ZExt:
2160 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2161 DAG.getValueType(VA.getValVT()));
2162 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2163 break;
2164 }
2165
Dan Gohman98ca4f22009-08-05 01:29:28 +00002166 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002167
2168 } else { // VA.isRegLoc()
2169
2170 // sanity check
2171 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002172 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002173
2174 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002175 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002176
Bob Wilsondee46d72009-04-17 20:35:10 +00002177 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002178 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002179 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00002180 PseudoSourceValue::getFixedStack(FI), 0,
2181 false, false, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002182 }
2183 }
2184
2185 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00002186 if (isVarArg) {
2187 static const unsigned GPRArgRegs[] = {
2188 ARM::R0, ARM::R1, ARM::R2, ARM::R3
2189 };
2190
Bob Wilsondee46d72009-04-17 20:35:10 +00002191 unsigned NumGPRs = CCInfo.getFirstUnallocated
2192 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002193
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00002194 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
2195 unsigned VARegSize = (4 - NumGPRs) * 4;
2196 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00002197 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00002198 if (VARegSaveSize) {
2199 // If this function is vararg, store any remaining integer argument regs
2200 // to their spots on the stack so that they may be loaded by deferencing
2201 // the result of va_next.
2202 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00002203 AFI->setVarArgsFrameIndex(
2204 MFI->CreateFixedObject(VARegSaveSize,
2205 ArgOffset + VARegSaveSize - VARegSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002206 true));
Dan Gohman1e93df62010-04-17 14:41:14 +00002207 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2208 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00002209
Dan Gohman475871a2008-07-27 21:46:04 +00002210 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00002211 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002212 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002213 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00002214 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002215 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00002216 RC = ARM::GPRRegisterClass;
2217
Bob Wilson998e1252009-04-20 18:36:57 +00002218 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002219 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00002220 SDValue Store =
2221 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Jim Grosbach18f30e62010-06-02 21:53:11 +00002222 PseudoSourceValue::getFixedStack(AFI->getVarArgsFrameIndex()),
2223 0, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002224 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002225 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00002226 DAG.getConstant(4, getPointerTy()));
2227 }
2228 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002229 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002230 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00002231 } else
2232 // This will point to the next argument passed via stack.
Evan Chenged2ae132010-07-03 00:40:23 +00002233 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
Evan Chenga8e29892007-01-19 07:51:42 +00002234 }
2235
Dan Gohman98ca4f22009-08-05 01:29:28 +00002236 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002237}
2238
2239/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002240static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002241 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002242 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002243 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002244 // Maybe this has already been legalized into the constant pool?
2245 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002246 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002247 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002248 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002249 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002250 }
2251 }
2252 return false;
2253}
2254
Evan Chenga8e29892007-01-19 07:51:42 +00002255/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2256/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002257SDValue
2258ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002259 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002260 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002261 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002262 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002263 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002264 // Constant does not fit, try adjusting it by one?
2265 switch (CC) {
2266 default: break;
2267 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002268 case ISD::SETGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00002269 if (isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002270 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002271 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002272 }
2273 break;
2274 case ISD::SETULT:
2275 case ISD::SETUGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00002276 if (C > 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002277 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002278 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002279 }
2280 break;
2281 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002282 case ISD::SETGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00002283 if (isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002284 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002285 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002286 }
2287 break;
2288 case ISD::SETULE:
2289 case ISD::SETUGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00002290 if (C < 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002291 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002292 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002293 }
2294 break;
2295 }
2296 }
2297 }
2298
2299 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002300 ARMISD::NodeType CompareType;
2301 switch (CondCode) {
2302 default:
2303 CompareType = ARMISD::CMP;
2304 break;
2305 case ARMCC::EQ:
2306 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002307 // Uses only Z Flag
2308 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002309 break;
2310 }
Evan Cheng218977b2010-07-13 19:27:42 +00002311 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002312 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002313}
2314
2315/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002316SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002317ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002318 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002319 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002320 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00002321 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002322 else
Owen Anderson825b72b2009-08-11 20:47:22 +00002323 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
2324 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002325}
2326
Bill Wendlingde2b1512010-08-11 08:43:16 +00002327SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2328 SDValue Cond = Op.getOperand(0);
2329 SDValue SelectTrue = Op.getOperand(1);
2330 SDValue SelectFalse = Op.getOperand(2);
2331 DebugLoc dl = Op.getDebugLoc();
2332
2333 // Convert:
2334 //
2335 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2336 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2337 //
2338 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2339 const ConstantSDNode *CMOVTrue =
2340 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2341 const ConstantSDNode *CMOVFalse =
2342 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2343
2344 if (CMOVTrue && CMOVFalse) {
2345 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2346 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2347
2348 SDValue True;
2349 SDValue False;
2350 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2351 True = SelectTrue;
2352 False = SelectFalse;
2353 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2354 True = SelectFalse;
2355 False = SelectTrue;
2356 }
2357
2358 if (True.getNode() && False.getNode()) {
2359 EVT VT = Cond.getValueType();
2360 SDValue ARMcc = Cond.getOperand(2);
2361 SDValue CCR = Cond.getOperand(3);
2362 SDValue Cmp = Cond.getOperand(4);
2363 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
2364 }
2365 }
2366 }
2367
2368 return DAG.getSelectCC(dl, Cond,
2369 DAG.getConstant(0, Cond.getValueType()),
2370 SelectTrue, SelectFalse, ISD::SETNE);
2371}
2372
Dan Gohmand858e902010-04-17 15:26:15 +00002373SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002374 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002375 SDValue LHS = Op.getOperand(0);
2376 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002377 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002378 SDValue TrueVal = Op.getOperand(2);
2379 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002380 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002381
Owen Anderson825b72b2009-08-11 20:47:22 +00002382 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002383 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002384 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002385 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2386 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002387 }
2388
2389 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002390 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002391
Evan Cheng218977b2010-07-13 19:27:42 +00002392 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2393 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002394 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002395 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002396 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002397 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002398 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002399 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002400 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002401 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002402 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002403 }
2404 return Result;
2405}
2406
Evan Cheng218977b2010-07-13 19:27:42 +00002407/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2408/// to morph to an integer compare sequence.
2409static bool canChangeToInt(SDValue Op, bool &SeenZero,
2410 const ARMSubtarget *Subtarget) {
2411 SDNode *N = Op.getNode();
2412 if (!N->hasOneUse())
2413 // Otherwise it requires moving the value from fp to integer registers.
2414 return false;
2415 if (!N->getNumValues())
2416 return false;
2417 EVT VT = Op.getValueType();
2418 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2419 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2420 // vmrs are very slow, e.g. cortex-a8.
2421 return false;
2422
2423 if (isFloatingPointZero(Op)) {
2424 SeenZero = true;
2425 return true;
2426 }
2427 return ISD::isNormalLoad(N);
2428}
2429
2430static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2431 if (isFloatingPointZero(Op))
2432 return DAG.getConstant(0, MVT::i32);
2433
2434 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2435 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2436 Ld->getChain(), Ld->getBasePtr(),
2437 Ld->getSrcValue(), Ld->getSrcValueOffset(),
2438 Ld->isVolatile(), Ld->isNonTemporal(),
2439 Ld->getAlignment());
2440
2441 llvm_unreachable("Unknown VFP cmp argument!");
2442}
2443
2444static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2445 SDValue &RetVal1, SDValue &RetVal2) {
2446 if (isFloatingPointZero(Op)) {
2447 RetVal1 = DAG.getConstant(0, MVT::i32);
2448 RetVal2 = DAG.getConstant(0, MVT::i32);
2449 return;
2450 }
2451
2452 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2453 SDValue Ptr = Ld->getBasePtr();
2454 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2455 Ld->getChain(), Ptr,
2456 Ld->getSrcValue(), Ld->getSrcValueOffset(),
2457 Ld->isVolatile(), Ld->isNonTemporal(),
2458 Ld->getAlignment());
2459
2460 EVT PtrType = Ptr.getValueType();
2461 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2462 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2463 PtrType, Ptr, DAG.getConstant(4, PtrType));
2464 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2465 Ld->getChain(), NewPtr,
2466 Ld->getSrcValue(), Ld->getSrcValueOffset() + 4,
2467 Ld->isVolatile(), Ld->isNonTemporal(),
2468 NewAlign);
2469 return;
2470 }
2471
2472 llvm_unreachable("Unknown VFP cmp argument!");
2473}
2474
2475/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2476/// f32 and even f64 comparisons to integer ones.
2477SDValue
2478ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2479 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002480 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002481 SDValue LHS = Op.getOperand(2);
2482 SDValue RHS = Op.getOperand(3);
2483 SDValue Dest = Op.getOperand(4);
2484 DebugLoc dl = Op.getDebugLoc();
2485
2486 bool SeenZero = false;
2487 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2488 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002489 // If one of the operand is zero, it's safe to ignore the NaN case since
2490 // we only care about equality comparisons.
2491 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Evan Cheng218977b2010-07-13 19:27:42 +00002492 // If unsafe fp math optimization is enabled and there are no othter uses of
2493 // the CMP operands, and the condition code is EQ oe NE, we can optimize it
2494 // to an integer comparison.
2495 if (CC == ISD::SETOEQ)
2496 CC = ISD::SETEQ;
2497 else if (CC == ISD::SETUNE)
2498 CC = ISD::SETNE;
2499
2500 SDValue ARMcc;
2501 if (LHS.getValueType() == MVT::f32) {
2502 LHS = bitcastf32Toi32(LHS, DAG);
2503 RHS = bitcastf32Toi32(RHS, DAG);
2504 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2505 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2506 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2507 Chain, Dest, ARMcc, CCR, Cmp);
2508 }
2509
2510 SDValue LHS1, LHS2;
2511 SDValue RHS1, RHS2;
2512 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2513 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2514 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2515 ARMcc = DAG.getConstant(CondCode, MVT::i32);
2516 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
2517 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2518 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2519 }
2520
2521 return SDValue();
2522}
2523
2524SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2525 SDValue Chain = Op.getOperand(0);
2526 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2527 SDValue LHS = Op.getOperand(2);
2528 SDValue RHS = Op.getOperand(3);
2529 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002530 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002531
Owen Anderson825b72b2009-08-11 20:47:22 +00002532 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002533 SDValue ARMcc;
2534 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002535 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002536 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002537 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002538 }
2539
Owen Anderson825b72b2009-08-11 20:47:22 +00002540 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002541
2542 if (UnsafeFPMath &&
2543 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2544 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2545 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2546 if (Result.getNode())
2547 return Result;
2548 }
2549
Evan Chenga8e29892007-01-19 07:51:42 +00002550 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002551 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002552
Evan Cheng218977b2010-07-13 19:27:42 +00002553 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2554 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002555 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2556 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng218977b2010-07-13 19:27:42 +00002557 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002558 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002559 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002560 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2561 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002562 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002563 }
2564 return Res;
2565}
2566
Dan Gohmand858e902010-04-17 15:26:15 +00002567SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002568 SDValue Chain = Op.getOperand(0);
2569 SDValue Table = Op.getOperand(1);
2570 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002571 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002572
Owen Andersone50ed302009-08-10 22:56:29 +00002573 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002574 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2575 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002576 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002577 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002578 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002579 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2580 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002581 if (Subtarget->isThumb2()) {
2582 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2583 // which does another jump to the destination. This also makes it easier
2584 // to translate it to TBB / TBH later.
2585 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002586 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002587 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002588 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002589 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002590 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002591 PseudoSourceValue::getJumpTable(), 0,
2592 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002593 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002594 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002595 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002596 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002597 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002598 PseudoSourceValue::getJumpTable(), 0, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002599 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002600 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002601 }
Evan Chenga8e29892007-01-19 07:51:42 +00002602}
2603
Bob Wilson76a312b2010-03-19 22:51:32 +00002604static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2605 DebugLoc dl = Op.getDebugLoc();
2606 unsigned Opc;
2607
2608 switch (Op.getOpcode()) {
2609 default:
2610 assert(0 && "Invalid opcode!");
2611 case ISD::FP_TO_SINT:
2612 Opc = ARMISD::FTOSI;
2613 break;
2614 case ISD::FP_TO_UINT:
2615 Opc = ARMISD::FTOUI;
2616 break;
2617 }
2618 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
2619 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
2620}
2621
2622static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2623 EVT VT = Op.getValueType();
2624 DebugLoc dl = Op.getDebugLoc();
2625 unsigned Opc;
2626
2627 switch (Op.getOpcode()) {
2628 default:
2629 assert(0 && "Invalid opcode!");
2630 case ISD::SINT_TO_FP:
2631 Opc = ARMISD::SITOF;
2632 break;
2633 case ISD::UINT_TO_FP:
2634 Opc = ARMISD::UITOF;
2635 break;
2636 }
2637
2638 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
2639 return DAG.getNode(Opc, dl, VT, Op);
2640}
2641
Evan Cheng515fe3a2010-07-08 02:08:50 +00002642SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002643 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002644 SDValue Tmp0 = Op.getOperand(0);
2645 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002646 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002647 EVT VT = Op.getValueType();
2648 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00002649 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
Evan Cheng218977b2010-07-13 19:27:42 +00002650 SDValue ARMcc = DAG.getConstant(ARMCC::LT, MVT::i32);
Evan Cheng515fe3a2010-07-08 02:08:50 +00002651 SDValue FP0 = DAG.getConstantFP(0.0, SrcVT);
Evan Cheng218977b2010-07-13 19:27:42 +00002652 SDValue Cmp = getVFPCmp(Tmp1, FP0, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002653 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002654 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002655}
2656
Evan Cheng2457f2c2010-05-22 01:47:14 +00002657SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
2658 MachineFunction &MF = DAG.getMachineFunction();
2659 MachineFrameInfo *MFI = MF.getFrameInfo();
2660 MFI->setReturnAddressIsTaken(true);
2661
2662 EVT VT = Op.getValueType();
2663 DebugLoc dl = Op.getDebugLoc();
2664 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2665 if (Depth) {
2666 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
2667 SDValue Offset = DAG.getConstant(4, MVT::i32);
2668 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
2669 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
2670 NULL, 0, false, false, 0);
2671 }
2672
2673 // Return LR, which contains the return address. Mark it an implicit live-in.
Jim Grosbachc2723a52010-07-23 23:50:35 +00002674 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00002675 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
2676}
2677
Dan Gohmand858e902010-04-17 15:26:15 +00002678SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002679 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2680 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002681
Owen Andersone50ed302009-08-10 22:56:29 +00002682 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002683 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2684 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002685 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002686 ? ARM::R7 : ARM::R11;
2687 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2688 while (Depth--)
David Greene1b58cab2010-02-15 16:55:24 +00002689 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
2690 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002691 return FrameAddr;
2692}
2693
Bob Wilson9f3f0612010-04-17 05:30:19 +00002694/// ExpandBIT_CONVERT - If the target supports VFP, this function is called to
2695/// expand a bit convert where either the source or destination type is i64 to
2696/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2697/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2698/// vectors), since the legalizer won't know what to do with that.
Duncan Sands1607f052008-12-01 11:39:25 +00002699static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002700 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2701 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002702 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002703
Bob Wilson9f3f0612010-04-17 05:30:19 +00002704 // This function is only supposed to be called for i64 types, either as the
2705 // source or destination of the bit convert.
2706 EVT SrcVT = Op.getValueType();
2707 EVT DstVT = N->getValueType(0);
2708 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
2709 "ExpandBIT_CONVERT called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002710
Bob Wilson9f3f0612010-04-17 05:30:19 +00002711 // Turn i64->f64 into VMOVDRR.
2712 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002713 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2714 DAG.getConstant(0, MVT::i32));
2715 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2716 DAG.getConstant(1, MVT::i32));
Bob Wilson1114f562010-06-11 22:45:25 +00002717 return DAG.getNode(ISD::BIT_CONVERT, dl, DstVT,
2718 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00002719 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002720
Jim Grosbache5165492009-11-09 00:11:35 +00002721 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002722 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2723 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2724 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2725 // Merge the pieces into a single i64 value.
2726 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2727 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002728
Bob Wilson9f3f0612010-04-17 05:30:19 +00002729 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002730}
2731
Bob Wilson5bafff32009-06-22 23:27:02 +00002732/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00002733/// Zero vectors are used to represent vector negation and in those cases
2734/// will be implemented with the NEON VNEG instruction. However, VNEG does
2735/// not support i64 elements, so sometimes the zero vectors will need to be
2736/// explicitly constructed. Regardless, use a canonical VMOV to create the
2737/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002738static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002739 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00002740 // The canonical modified immediate encoding of a zero vector is....0!
2741 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
2742 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
2743 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
2744 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00002745}
2746
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002747/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2748/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002749SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2750 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002751 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2752 EVT VT = Op.getValueType();
2753 unsigned VTBits = VT.getSizeInBits();
2754 DebugLoc dl = Op.getDebugLoc();
2755 SDValue ShOpLo = Op.getOperand(0);
2756 SDValue ShOpHi = Op.getOperand(1);
2757 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00002758 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002759 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002760
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002761 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2762
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002763 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2764 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2765 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2766 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2767 DAG.getConstant(VTBits, MVT::i32));
2768 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2769 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002770 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002771
2772 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2773 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00002774 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002775 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00002776 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002777 CCR, Cmp);
2778
2779 SDValue Ops[2] = { Lo, Hi };
2780 return DAG.getMergeValues(Ops, 2, dl);
2781}
2782
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002783/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2784/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002785SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2786 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002787 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2788 EVT VT = Op.getValueType();
2789 unsigned VTBits = VT.getSizeInBits();
2790 DebugLoc dl = Op.getDebugLoc();
2791 SDValue ShOpLo = Op.getOperand(0);
2792 SDValue ShOpHi = Op.getOperand(1);
2793 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00002794 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002795
2796 assert(Op.getOpcode() == ISD::SHL_PARTS);
2797 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2798 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2799 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2800 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2801 DAG.getConstant(VTBits, MVT::i32));
2802 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2803 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2804
2805 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2806 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2807 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00002808 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002809 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00002810 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002811 CCR, Cmp);
2812
2813 SDValue Ops[2] = { Lo, Hi };
2814 return DAG.getMergeValues(Ops, 2, dl);
2815}
2816
Nate Begemand1fb5832010-08-03 21:31:55 +00002817SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
2818 SelectionDAG &DAG) const {
2819 // The rounding mode is in bits 23:22 of the FPSCR.
2820 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
2821 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
2822 // so that the shift + and get folded into a bitfield extract.
2823 DebugLoc dl = Op.getDebugLoc();
2824 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
2825 DAG.getConstant(Intrinsic::arm_get_fpscr,
2826 MVT::i32));
2827 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
2828 DAG.getConstant(1U << 22, MVT::i32));
2829 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
2830 DAG.getConstant(22, MVT::i32));
2831 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
2832 DAG.getConstant(3, MVT::i32));
2833}
2834
Jim Grosbach3482c802010-01-18 19:58:49 +00002835static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
2836 const ARMSubtarget *ST) {
2837 EVT VT = N->getValueType(0);
2838 DebugLoc dl = N->getDebugLoc();
2839
2840 if (!ST->hasV6T2Ops())
2841 return SDValue();
2842
2843 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
2844 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
2845}
2846
Bob Wilson5bafff32009-06-22 23:27:02 +00002847static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2848 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002849 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002850 DebugLoc dl = N->getDebugLoc();
2851
2852 // Lower vector shifts on NEON to use VSHL.
2853 if (VT.isVector()) {
2854 assert(ST->hasNEON() && "unexpected vector shift");
2855
2856 // Left shifts translate directly to the vshiftu intrinsic.
2857 if (N->getOpcode() == ISD::SHL)
2858 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002859 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002860 N->getOperand(0), N->getOperand(1));
2861
2862 assert((N->getOpcode() == ISD::SRA ||
2863 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2864
2865 // NEON uses the same intrinsics for both left and right shifts. For
2866 // right shifts, the shift amounts are negative, so negate the vector of
2867 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002868 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002869 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2870 getZeroVector(ShiftVT, DAG, dl),
2871 N->getOperand(1));
2872 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2873 Intrinsic::arm_neon_vshifts :
2874 Intrinsic::arm_neon_vshiftu);
2875 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002876 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002877 N->getOperand(0), NegatedCount);
2878 }
2879
Eli Friedmance392eb2009-08-22 03:13:10 +00002880 // We can get here for a node like i32 = ISD::SHL i32, i64
2881 if (VT != MVT::i64)
2882 return SDValue();
2883
2884 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002885 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002886
Chris Lattner27a6c732007-11-24 07:07:01 +00002887 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2888 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002889 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002890 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002891
Chris Lattner27a6c732007-11-24 07:07:01 +00002892 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002893 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002894
Chris Lattner27a6c732007-11-24 07:07:01 +00002895 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002896 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00002897 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00002898 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00002899 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002900
Chris Lattner27a6c732007-11-24 07:07:01 +00002901 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2902 // captures the result into a carry flag.
2903 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002904 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002905
Chris Lattner27a6c732007-11-24 07:07:01 +00002906 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002907 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002908
Chris Lattner27a6c732007-11-24 07:07:01 +00002909 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002910 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002911}
2912
Bob Wilson5bafff32009-06-22 23:27:02 +00002913static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2914 SDValue TmpOp0, TmpOp1;
2915 bool Invert = false;
2916 bool Swap = false;
2917 unsigned Opc = 0;
2918
2919 SDValue Op0 = Op.getOperand(0);
2920 SDValue Op1 = Op.getOperand(1);
2921 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002922 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002923 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2924 DebugLoc dl = Op.getDebugLoc();
2925
2926 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2927 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002928 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002929 case ISD::SETUNE:
2930 case ISD::SETNE: Invert = true; // Fallthrough
2931 case ISD::SETOEQ:
2932 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2933 case ISD::SETOLT:
2934 case ISD::SETLT: Swap = true; // Fallthrough
2935 case ISD::SETOGT:
2936 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2937 case ISD::SETOLE:
2938 case ISD::SETLE: Swap = true; // Fallthrough
2939 case ISD::SETOGE:
2940 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2941 case ISD::SETUGE: Swap = true; // Fallthrough
2942 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2943 case ISD::SETUGT: Swap = true; // Fallthrough
2944 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2945 case ISD::SETUEQ: Invert = true; // Fallthrough
2946 case ISD::SETONE:
2947 // Expand this to (OLT | OGT).
2948 TmpOp0 = Op0;
2949 TmpOp1 = Op1;
2950 Opc = ISD::OR;
2951 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2952 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2953 break;
2954 case ISD::SETUO: Invert = true; // Fallthrough
2955 case ISD::SETO:
2956 // Expand this to (OLT | OGE).
2957 TmpOp0 = Op0;
2958 TmpOp1 = Op1;
2959 Opc = ISD::OR;
2960 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2961 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2962 break;
2963 }
2964 } else {
2965 // Integer comparisons.
2966 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002967 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002968 case ISD::SETNE: Invert = true;
2969 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2970 case ISD::SETLT: Swap = true;
2971 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2972 case ISD::SETLE: Swap = true;
2973 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2974 case ISD::SETULT: Swap = true;
2975 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2976 case ISD::SETULE: Swap = true;
2977 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2978 }
2979
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002980 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002981 if (Opc == ARMISD::VCEQ) {
2982
2983 SDValue AndOp;
2984 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2985 AndOp = Op0;
2986 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2987 AndOp = Op1;
2988
2989 // Ignore bitconvert.
2990 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2991 AndOp = AndOp.getOperand(0);
2992
2993 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2994 Opc = ARMISD::VTST;
2995 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2996 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2997 Invert = !Invert;
2998 }
2999 }
3000 }
3001
3002 if (Swap)
3003 std::swap(Op0, Op1);
3004
3005 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3006
3007 if (Invert)
3008 Result = DAG.getNOT(dl, Result, VT);
3009
3010 return Result;
3011}
3012
Bob Wilsond3c42842010-06-14 22:19:57 +00003013/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3014/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003015/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003016static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3017 unsigned SplatBitSize, SelectionDAG &DAG,
Bob Wilsoncba270d2010-07-13 21:16:48 +00003018 EVT &VT, bool is128Bits, bool isVMOV) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003019 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003020
Bob Wilson827b2102010-06-15 19:05:35 +00003021 // SplatBitSize is set to the smallest size that splats the vector, so a
3022 // zero vector will always have SplatBitSize == 8. However, NEON modified
3023 // immediate instructions others than VMOV do not support the 8-bit encoding
3024 // of a zero vector, and the default encoding of zero is supposed to be the
3025 // 32-bit version.
3026 if (SplatBits == 0)
3027 SplatBitSize = 32;
3028
Bob Wilson5bafff32009-06-22 23:27:02 +00003029 switch (SplatBitSize) {
3030 case 8:
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003031 if (!isVMOV)
3032 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003033 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003034 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003035 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003036 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003037 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003038 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003039
3040 case 16:
3041 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003042 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003043 if ((SplatBits & ~0xff) == 0) {
3044 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003045 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003046 Imm = SplatBits;
3047 break;
3048 }
3049 if ((SplatBits & ~0xff00) == 0) {
3050 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003051 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003052 Imm = SplatBits >> 8;
3053 break;
3054 }
3055 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003056
3057 case 32:
3058 // NEON's 32-bit VMOV supports splat values where:
3059 // * only one byte is nonzero, or
3060 // * the least significant byte is 0xff and the second byte is nonzero, or
3061 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003062 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003063 if ((SplatBits & ~0xff) == 0) {
3064 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003065 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003066 Imm = SplatBits;
3067 break;
3068 }
3069 if ((SplatBits & ~0xff00) == 0) {
3070 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003071 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003072 Imm = SplatBits >> 8;
3073 break;
3074 }
3075 if ((SplatBits & ~0xff0000) == 0) {
3076 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003077 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003078 Imm = SplatBits >> 16;
3079 break;
3080 }
3081 if ((SplatBits & ~0xff000000) == 0) {
3082 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003083 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003084 Imm = SplatBits >> 24;
3085 break;
3086 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003087
3088 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003089 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3090 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003091 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003092 Imm = SplatBits >> 8;
3093 SplatBits |= 0xff;
3094 break;
3095 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003096
3097 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003098 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3099 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003100 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003101 Imm = SplatBits >> 16;
3102 SplatBits |= 0xffff;
3103 break;
3104 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003105
3106 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3107 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3108 // VMOV.I32. A (very) minor optimization would be to replicate the value
3109 // and fall through here to test for a valid 64-bit splat. But, then the
3110 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003111 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003112
3113 case 64: {
Bob Wilson827b2102010-06-15 19:05:35 +00003114 if (!isVMOV)
3115 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003116 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003117 uint64_t BitMask = 0xff;
3118 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003119 unsigned ImmMask = 1;
3120 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003121 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003122 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003123 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003124 Imm |= ImmMask;
3125 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003126 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003127 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003128 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003129 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003130 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003131 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003132 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003133 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003134 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003135 break;
3136 }
3137
Bob Wilson1a913ed2010-06-11 21:34:50 +00003138 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003139 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003140 return SDValue();
3141 }
3142
Bob Wilsoncba270d2010-07-13 21:16:48 +00003143 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3144 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003145}
3146
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003147static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3148 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003149 unsigned NumElts = VT.getVectorNumElements();
3150 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003151 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003152
3153 // If this is a VEXT shuffle, the immediate value is the index of the first
3154 // element. The other shuffle indices must be the successive elements after
3155 // the first one.
3156 unsigned ExpectedElt = Imm;
3157 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003158 // Increment the expected index. If it wraps around, it may still be
3159 // a VEXT but the source vectors must be swapped.
3160 ExpectedElt += 1;
3161 if (ExpectedElt == NumElts * 2) {
3162 ExpectedElt = 0;
3163 ReverseVEXT = true;
3164 }
3165
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003166 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003167 return false;
3168 }
3169
3170 // Adjust the index value if the source operands will be swapped.
3171 if (ReverseVEXT)
3172 Imm -= NumElts;
3173
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003174 return true;
3175}
3176
Bob Wilson8bb9e482009-07-26 00:39:34 +00003177/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3178/// instruction with the specified blocksize. (The order of the elements
3179/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003180static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3181 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003182 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3183 "Only possible block sizes for VREV are: 16, 32, 64");
3184
Bob Wilson8bb9e482009-07-26 00:39:34 +00003185 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003186 if (EltSz == 64)
3187 return false;
3188
3189 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003190 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003191
3192 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3193 return false;
3194
3195 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003196 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00003197 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
3198 return false;
3199 }
3200
3201 return true;
3202}
3203
Bob Wilsonc692cb72009-08-21 20:54:19 +00003204static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3205 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003206 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3207 if (EltSz == 64)
3208 return false;
3209
Bob Wilsonc692cb72009-08-21 20:54:19 +00003210 unsigned NumElts = VT.getVectorNumElements();
3211 WhichResult = (M[0] == 0 ? 0 : 1);
3212 for (unsigned i = 0; i < NumElts; i += 2) {
3213 if ((unsigned) M[i] != i + WhichResult ||
3214 (unsigned) M[i+1] != i + NumElts + WhichResult)
3215 return false;
3216 }
3217 return true;
3218}
3219
Bob Wilson324f4f12009-12-03 06:40:55 +00003220/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3221/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3222/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3223static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3224 unsigned &WhichResult) {
3225 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3226 if (EltSz == 64)
3227 return false;
3228
3229 unsigned NumElts = VT.getVectorNumElements();
3230 WhichResult = (M[0] == 0 ? 0 : 1);
3231 for (unsigned i = 0; i < NumElts; i += 2) {
3232 if ((unsigned) M[i] != i + WhichResult ||
3233 (unsigned) M[i+1] != i + WhichResult)
3234 return false;
3235 }
3236 return true;
3237}
3238
Bob Wilsonc692cb72009-08-21 20:54:19 +00003239static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3240 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003241 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3242 if (EltSz == 64)
3243 return false;
3244
Bob Wilsonc692cb72009-08-21 20:54:19 +00003245 unsigned NumElts = VT.getVectorNumElements();
3246 WhichResult = (M[0] == 0 ? 0 : 1);
3247 for (unsigned i = 0; i != NumElts; ++i) {
3248 if ((unsigned) M[i] != 2 * i + WhichResult)
3249 return false;
3250 }
3251
3252 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003253 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003254 return false;
3255
3256 return true;
3257}
3258
Bob Wilson324f4f12009-12-03 06:40:55 +00003259/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3260/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3261/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3262static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3263 unsigned &WhichResult) {
3264 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3265 if (EltSz == 64)
3266 return false;
3267
3268 unsigned Half = VT.getVectorNumElements() / 2;
3269 WhichResult = (M[0] == 0 ? 0 : 1);
3270 for (unsigned j = 0; j != 2; ++j) {
3271 unsigned Idx = WhichResult;
3272 for (unsigned i = 0; i != Half; ++i) {
3273 if ((unsigned) M[i + j * Half] != Idx)
3274 return false;
3275 Idx += 2;
3276 }
3277 }
3278
3279 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3280 if (VT.is64BitVector() && EltSz == 32)
3281 return false;
3282
3283 return true;
3284}
3285
Bob Wilsonc692cb72009-08-21 20:54:19 +00003286static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3287 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003288 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3289 if (EltSz == 64)
3290 return false;
3291
Bob Wilsonc692cb72009-08-21 20:54:19 +00003292 unsigned NumElts = VT.getVectorNumElements();
3293 WhichResult = (M[0] == 0 ? 0 : 1);
3294 unsigned Idx = WhichResult * NumElts / 2;
3295 for (unsigned i = 0; i != NumElts; i += 2) {
3296 if ((unsigned) M[i] != Idx ||
3297 (unsigned) M[i+1] != Idx + NumElts)
3298 return false;
3299 Idx += 1;
3300 }
3301
3302 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003303 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003304 return false;
3305
3306 return true;
3307}
3308
Bob Wilson324f4f12009-12-03 06:40:55 +00003309/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3310/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3311/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3312static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3313 unsigned &WhichResult) {
3314 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3315 if (EltSz == 64)
3316 return false;
3317
3318 unsigned NumElts = VT.getVectorNumElements();
3319 WhichResult = (M[0] == 0 ? 0 : 1);
3320 unsigned Idx = WhichResult * NumElts / 2;
3321 for (unsigned i = 0; i != NumElts; i += 2) {
3322 if ((unsigned) M[i] != Idx ||
3323 (unsigned) M[i+1] != Idx)
3324 return false;
3325 Idx += 1;
3326 }
3327
3328 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3329 if (VT.is64BitVector() && EltSz == 32)
3330 return false;
3331
3332 return true;
3333}
3334
Dale Johannesenf630c712010-07-29 20:10:08 +00003335// If N is an integer constant that can be moved into a register in one
3336// instruction, return an SDValue of such a constant (will become a MOV
3337// instruction). Otherwise return null.
3338static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3339 const ARMSubtarget *ST, DebugLoc dl) {
3340 uint64_t Val;
3341 if (!isa<ConstantSDNode>(N))
3342 return SDValue();
3343 Val = cast<ConstantSDNode>(N)->getZExtValue();
3344
3345 if (ST->isThumb1Only()) {
3346 if (Val <= 255 || ~Val <= 255)
3347 return DAG.getConstant(Val, MVT::i32);
3348 } else {
3349 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3350 return DAG.getConstant(Val, MVT::i32);
3351 }
3352 return SDValue();
3353}
3354
Bob Wilson5bafff32009-06-22 23:27:02 +00003355// If this is a case we can't handle, return null and let the default
3356// expansion code take care of it.
Dale Johannesenf630c712010-07-29 20:10:08 +00003357static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3358 const ARMSubtarget *ST) {
Bob Wilsond06791f2009-08-13 01:57:47 +00003359 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003360 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003361 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003362
3363 APInt SplatBits, SplatUndef;
3364 unsigned SplatBitSize;
3365 bool HasAnyUndefs;
3366 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003367 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003368 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003369 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003370 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003371 SplatUndef.getZExtValue(), SplatBitSize,
3372 DAG, VmovVT, VT.is128BitVector(), true);
3373 if (Val.getNode()) {
3374 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
3375 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vmov);
3376 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003377
3378 // Try an immediate VMVN.
3379 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3380 ((1LL << SplatBitSize) - 1));
3381 Val = isNEONModifiedImm(NegatedImm,
3382 SplatUndef.getZExtValue(), SplatBitSize,
3383 DAG, VmovVT, VT.is128BitVector(), false);
3384 if (Val.getNode()) {
3385 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
3386 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vmov);
3387 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003388 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003389 }
3390
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003391 // Scan through the operands to see if only one value is used.
3392 unsigned NumElts = VT.getVectorNumElements();
3393 bool isOnlyLowElement = true;
3394 bool usesOnlyOneValue = true;
3395 bool isConstant = true;
3396 SDValue Value;
3397 for (unsigned i = 0; i < NumElts; ++i) {
3398 SDValue V = Op.getOperand(i);
3399 if (V.getOpcode() == ISD::UNDEF)
3400 continue;
3401 if (i > 0)
3402 isOnlyLowElement = false;
3403 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3404 isConstant = false;
3405
3406 if (!Value.getNode())
3407 Value = V;
3408 else if (V != Value)
3409 usesOnlyOneValue = false;
3410 }
3411
3412 if (!Value.getNode())
3413 return DAG.getUNDEF(VT);
3414
3415 if (isOnlyLowElement)
3416 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3417
Dale Johannesenf630c712010-07-29 20:10:08 +00003418 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3419
3420 if (EnableARMVDUPsplat) {
3421 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3422 // i32 and try again.
3423 if (usesOnlyOneValue && EltSize <= 32) {
3424 if (!isConstant)
3425 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3426 if (VT.getVectorElementType().isFloatingPoint()) {
3427 SmallVector<SDValue, 8> Ops;
3428 for (unsigned i = 0; i < NumElts; ++i)
3429 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
3430 Op.getOperand(i)));
3431 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &Ops[0],
3432 NumElts);
3433 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3434 LowerBUILD_VECTOR(Val, DAG, ST));
3435 }
3436 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3437 if (Val.getNode())
3438 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
3439 }
3440 }
3441
3442 // If all elements are constants and the case above didn't get hit, fall back
3443 // to the default expansion, which will generate a load from the constant
3444 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003445 if (isConstant)
3446 return SDValue();
3447
Dale Johannesenf630c712010-07-29 20:10:08 +00003448 if (!EnableARMVDUPsplat) {
3449 // Use VDUP for non-constant splats.
3450 if (usesOnlyOneValue && EltSize <= 32)
3451 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3452 }
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003453
3454 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003455 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3456 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003457 if (EltSize >= 32) {
3458 // Do the expansion with floating-point types, since that is what the VFP
3459 // registers are defined to use, and since i64 is not legal.
3460 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3461 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003462 SmallVector<SDValue, 8> Ops;
3463 for (unsigned i = 0; i < NumElts; ++i)
3464 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, dl, EltVT, Op.getOperand(i)));
3465 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003466 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003467 }
3468
3469 return SDValue();
3470}
3471
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003472/// isShuffleMaskLegal - Targets can use this to indicate that they only
3473/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3474/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3475/// are assumed to be legal.
3476bool
3477ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
3478 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003479 if (VT.getVectorNumElements() == 4 &&
3480 (VT.is128BitVector() || VT.is64BitVector())) {
3481 unsigned PFIndexes[4];
3482 for (unsigned i = 0; i != 4; ++i) {
3483 if (M[i] < 0)
3484 PFIndexes[i] = 8;
3485 else
3486 PFIndexes[i] = M[i];
3487 }
3488
3489 // Compute the index in the perfect shuffle table.
3490 unsigned PFTableIndex =
3491 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3492 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3493 unsigned Cost = (PFEntry >> 30);
3494
3495 if (Cost <= 4)
3496 return true;
3497 }
3498
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003499 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00003500 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003501
Bob Wilson53dd2452010-06-07 23:53:38 +00003502 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3503 return (EltSize >= 32 ||
3504 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003505 isVREVMask(M, VT, 64) ||
3506 isVREVMask(M, VT, 32) ||
3507 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00003508 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
3509 isVTRNMask(M, VT, WhichResult) ||
3510 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00003511 isVZIPMask(M, VT, WhichResult) ||
3512 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
3513 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
3514 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003515}
3516
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003517/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
3518/// the specified operations to build the shuffle.
3519static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
3520 SDValue RHS, SelectionDAG &DAG,
3521 DebugLoc dl) {
3522 unsigned OpNum = (PFEntry >> 26) & 0x0F;
3523 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
3524 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
3525
3526 enum {
3527 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
3528 OP_VREV,
3529 OP_VDUP0,
3530 OP_VDUP1,
3531 OP_VDUP2,
3532 OP_VDUP3,
3533 OP_VEXT1,
3534 OP_VEXT2,
3535 OP_VEXT3,
3536 OP_VUZPL, // VUZP, left result
3537 OP_VUZPR, // VUZP, right result
3538 OP_VZIPL, // VZIP, left result
3539 OP_VZIPR, // VZIP, right result
3540 OP_VTRNL, // VTRN, left result
3541 OP_VTRNR // VTRN, right result
3542 };
3543
3544 if (OpNum == OP_COPY) {
3545 if (LHSID == (1*9+2)*9+3) return LHS;
3546 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
3547 return RHS;
3548 }
3549
3550 SDValue OpLHS, OpRHS;
3551 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
3552 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
3553 EVT VT = OpLHS.getValueType();
3554
3555 switch (OpNum) {
3556 default: llvm_unreachable("Unknown shuffle opcode!");
3557 case OP_VREV:
3558 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
3559 case OP_VDUP0:
3560 case OP_VDUP1:
3561 case OP_VDUP2:
3562 case OP_VDUP3:
3563 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003564 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003565 case OP_VEXT1:
3566 case OP_VEXT2:
3567 case OP_VEXT3:
3568 return DAG.getNode(ARMISD::VEXT, dl, VT,
3569 OpLHS, OpRHS,
3570 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
3571 case OP_VUZPL:
3572 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003573 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003574 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
3575 case OP_VZIPL:
3576 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003577 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003578 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
3579 case OP_VTRNL:
3580 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003581 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3582 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003583 }
3584}
3585
Bob Wilson5bafff32009-06-22 23:27:02 +00003586static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003587 SDValue V1 = Op.getOperand(0);
3588 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00003589 DebugLoc dl = Op.getDebugLoc();
3590 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003591 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003592 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00003593
Bob Wilson28865062009-08-13 02:13:04 +00003594 // Convert shuffles that are directly supported on NEON to target-specific
3595 // DAG nodes, instead of keeping them as shuffles and matching them again
3596 // during code selection. This is more efficient and avoids the possibility
3597 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00003598 // FIXME: floating-point vectors should be canonicalized to integer vectors
3599 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003600 SVN->getMask(ShuffleMask);
3601
Bob Wilson53dd2452010-06-07 23:53:38 +00003602 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3603 if (EltSize <= 32) {
3604 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
3605 int Lane = SVN->getSplatIndex();
3606 // If this is undef splat, generate it via "just" vdup, if possible.
3607 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00003608
Bob Wilson53dd2452010-06-07 23:53:38 +00003609 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3610 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
3611 }
3612 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
3613 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00003614 }
Bob Wilson53dd2452010-06-07 23:53:38 +00003615
3616 bool ReverseVEXT;
3617 unsigned Imm;
3618 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
3619 if (ReverseVEXT)
3620 std::swap(V1, V2);
3621 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
3622 DAG.getConstant(Imm, MVT::i32));
3623 }
3624
3625 if (isVREVMask(ShuffleMask, VT, 64))
3626 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
3627 if (isVREVMask(ShuffleMask, VT, 32))
3628 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
3629 if (isVREVMask(ShuffleMask, VT, 16))
3630 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
3631
3632 // Check for Neon shuffles that modify both input vectors in place.
3633 // If both results are used, i.e., if there are two shuffles with the same
3634 // source operands and with masks corresponding to both results of one of
3635 // these operations, DAG memoization will ensure that a single node is
3636 // used for both shuffles.
3637 unsigned WhichResult;
3638 if (isVTRNMask(ShuffleMask, VT, WhichResult))
3639 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3640 V1, V2).getValue(WhichResult);
3641 if (isVUZPMask(ShuffleMask, VT, WhichResult))
3642 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3643 V1, V2).getValue(WhichResult);
3644 if (isVZIPMask(ShuffleMask, VT, WhichResult))
3645 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3646 V1, V2).getValue(WhichResult);
3647
3648 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
3649 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3650 V1, V1).getValue(WhichResult);
3651 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3652 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3653 V1, V1).getValue(WhichResult);
3654 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3655 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3656 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00003657 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003658
Bob Wilsonc692cb72009-08-21 20:54:19 +00003659 // If the shuffle is not directly supported and it has 4 elements, use
3660 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003661 unsigned NumElts = VT.getVectorNumElements();
3662 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003663 unsigned PFIndexes[4];
3664 for (unsigned i = 0; i != 4; ++i) {
3665 if (ShuffleMask[i] < 0)
3666 PFIndexes[i] = 8;
3667 else
3668 PFIndexes[i] = ShuffleMask[i];
3669 }
3670
3671 // Compute the index in the perfect shuffle table.
3672 unsigned PFTableIndex =
3673 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003674 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3675 unsigned Cost = (PFEntry >> 30);
3676
3677 if (Cost <= 4)
3678 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
3679 }
Bob Wilsond8e17572009-08-12 22:31:50 +00003680
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003681 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003682 if (EltSize >= 32) {
3683 // Do the expansion with floating-point types, since that is what the VFP
3684 // registers are defined to use, and since i64 is not legal.
3685 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3686 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
3687 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V1);
3688 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003689 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003690 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00003691 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003692 Ops.push_back(DAG.getUNDEF(EltVT));
3693 else
3694 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
3695 ShuffleMask[i] < (int)NumElts ? V1 : V2,
3696 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
3697 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00003698 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003699 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Bob Wilson63b88452010-05-20 18:39:53 +00003700 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
3701 }
3702
Bob Wilson22cac0d2009-08-14 05:16:33 +00003703 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003704}
3705
Bob Wilson5bafff32009-06-22 23:27:02 +00003706static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003707 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003708 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003709 SDValue Vec = Op.getOperand(0);
3710 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00003711 assert(VT == MVT::i32 &&
3712 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
3713 "unexpected type for custom-lowering vector extract");
3714 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00003715}
3716
Bob Wilsona6d65862009-08-03 20:36:38 +00003717static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3718 // The only time a CONCAT_VECTORS operation can have legal types is when
3719 // two 64-bit vectors are concatenated to a 128-bit vector.
3720 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
3721 "unexpected CONCAT_VECTORS");
3722 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00003723 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00003724 SDValue Op0 = Op.getOperand(0);
3725 SDValue Op1 = Op.getOperand(1);
3726 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003727 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3728 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00003729 DAG.getIntPtrConstant(0));
3730 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003731 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3732 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00003733 DAG.getIntPtrConstant(1));
3734 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003735}
3736
Dan Gohmand858e902010-04-17 15:26:15 +00003737SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003738 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003739 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00003740 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00003741 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003742 case ISD::GlobalAddress:
3743 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
3744 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00003745 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00003746 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00003747 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
3748 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003749 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00003750 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00003751 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00003752 case ISD::SINT_TO_FP:
3753 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
3754 case ISD::FP_TO_SINT:
3755 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003756 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003757 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003758 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003759 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00003760 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00003761 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00003762 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
3763 Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00003764 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003765 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00003766 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00003767 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00003768 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003769 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00003770 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00003771 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003772 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00003773 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003774 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003775 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00003776 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Nate Begemand1fb5832010-08-03 21:31:55 +00003777 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003778 }
Dan Gohman475871a2008-07-27 21:46:04 +00003779 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003780}
3781
Duncan Sands1607f052008-12-01 11:39:25 +00003782/// ReplaceNodeResults - Replace the results of node with an illegal result
3783/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00003784void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
3785 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00003786 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00003787 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00003788 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00003789 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003790 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003791 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003792 case ISD::BIT_CONVERT:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003793 Res = ExpandBIT_CONVERT(N, DAG);
3794 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00003795 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003796 case ISD::SRA:
3797 Res = LowerShift(N, DAG, Subtarget);
3798 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003799 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00003800 if (Res.getNode())
3801 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00003802}
Chris Lattner27a6c732007-11-24 07:07:01 +00003803
Evan Chenga8e29892007-01-19 07:51:42 +00003804//===----------------------------------------------------------------------===//
3805// ARM Scheduler Hooks
3806//===----------------------------------------------------------------------===//
3807
3808MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003809ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
3810 MachineBasicBlock *BB,
3811 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00003812 unsigned dest = MI->getOperand(0).getReg();
3813 unsigned ptr = MI->getOperand(1).getReg();
3814 unsigned oldval = MI->getOperand(2).getReg();
3815 unsigned newval = MI->getOperand(3).getReg();
3816 unsigned scratch = BB->getParent()->getRegInfo()
3817 .createVirtualRegister(ARM::GPRRegisterClass);
3818 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3819 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003820 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00003821
3822 unsigned ldrOpc, strOpc;
3823 switch (Size) {
3824 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003825 case 1:
3826 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
3827 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
3828 break;
3829 case 2:
3830 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3831 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3832 break;
3833 case 4:
3834 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3835 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3836 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00003837 }
3838
3839 MachineFunction *MF = BB->getParent();
3840 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3841 MachineFunction::iterator It = BB;
3842 ++It; // insert the new blocks after the current block
3843
3844 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3845 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3846 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3847 MF->insert(It, loop1MBB);
3848 MF->insert(It, loop2MBB);
3849 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00003850
3851 // Transfer the remainder of BB and its successor edges to exitMBB.
3852 exitMBB->splice(exitMBB->begin(), BB,
3853 llvm::next(MachineBasicBlock::iterator(MI)),
3854 BB->end());
3855 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003856
3857 // thisMBB:
3858 // ...
3859 // fallthrough --> loop1MBB
3860 BB->addSuccessor(loop1MBB);
3861
3862 // loop1MBB:
3863 // ldrex dest, [ptr]
3864 // cmp dest, oldval
3865 // bne exitMBB
3866 BB = loop1MBB;
3867 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003868 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003869 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003870 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3871 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003872 BB->addSuccessor(loop2MBB);
3873 BB->addSuccessor(exitMBB);
3874
3875 // loop2MBB:
3876 // strex scratch, newval, [ptr]
3877 // cmp scratch, #0
3878 // bne loop1MBB
3879 BB = loop2MBB;
3880 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
3881 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003882 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003883 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003884 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3885 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003886 BB->addSuccessor(loop1MBB);
3887 BB->addSuccessor(exitMBB);
3888
3889 // exitMBB:
3890 // ...
3891 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00003892
Dan Gohman14152b42010-07-06 20:24:04 +00003893 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00003894
Jim Grosbach5278eb82009-12-11 01:42:04 +00003895 return BB;
3896}
3897
3898MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003899ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
3900 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00003901 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
3902 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3903
3904 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003905 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003906 MachineFunction::iterator It = BB;
3907 ++It;
3908
3909 unsigned dest = MI->getOperand(0).getReg();
3910 unsigned ptr = MI->getOperand(1).getReg();
3911 unsigned incr = MI->getOperand(2).getReg();
3912 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00003913
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003914 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003915 unsigned ldrOpc, strOpc;
3916 switch (Size) {
3917 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003918 case 1:
3919 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00003920 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003921 break;
3922 case 2:
3923 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3924 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3925 break;
3926 case 4:
3927 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3928 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3929 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00003930 }
3931
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003932 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3933 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3934 MF->insert(It, loopMBB);
3935 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00003936
3937 // Transfer the remainder of BB and its successor edges to exitMBB.
3938 exitMBB->splice(exitMBB->begin(), BB,
3939 llvm::next(MachineBasicBlock::iterator(MI)),
3940 BB->end());
3941 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003942
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003943 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003944 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3945 unsigned scratch2 = (!BinOpcode) ? incr :
3946 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3947
3948 // thisMBB:
3949 // ...
3950 // fallthrough --> loopMBB
3951 BB->addSuccessor(loopMBB);
3952
3953 // loopMBB:
3954 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003955 // <binop> scratch2, dest, incr
3956 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00003957 // cmp scratch, #0
3958 // bne- loopMBB
3959 // fallthrough --> exitMBB
3960 BB = loopMBB;
3961 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00003962 if (BinOpcode) {
3963 // operand order needs to go the other way for NAND
3964 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
3965 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3966 addReg(incr).addReg(dest)).addReg(0);
3967 else
3968 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3969 addReg(dest).addReg(incr)).addReg(0);
3970 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00003971
3972 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
3973 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003974 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00003975 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003976 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3977 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003978
3979 BB->addSuccessor(loopMBB);
3980 BB->addSuccessor(exitMBB);
3981
3982 // exitMBB:
3983 // ...
3984 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00003985
Dan Gohman14152b42010-07-06 20:24:04 +00003986 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00003987
Jim Grosbachc3c23542009-12-14 04:22:04 +00003988 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00003989}
3990
Evan Cheng218977b2010-07-13 19:27:42 +00003991static
3992MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
3993 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
3994 E = MBB->succ_end(); I != E; ++I)
3995 if (*I != Succ)
3996 return *I;
3997 llvm_unreachable("Expecting a BB with two successors!");
3998}
3999
Jim Grosbache801dc42009-12-12 01:40:06 +00004000MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00004001ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00004002 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004003 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00004004 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004005 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00004006 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00004007 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00004008 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00004009 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00004010
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004011 case ARM::ATOMIC_LOAD_ADD_I8:
4012 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4013 case ARM::ATOMIC_LOAD_ADD_I16:
4014 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4015 case ARM::ATOMIC_LOAD_ADD_I32:
4016 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004017
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004018 case ARM::ATOMIC_LOAD_AND_I8:
4019 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4020 case ARM::ATOMIC_LOAD_AND_I16:
4021 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4022 case ARM::ATOMIC_LOAD_AND_I32:
4023 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004024
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004025 case ARM::ATOMIC_LOAD_OR_I8:
4026 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4027 case ARM::ATOMIC_LOAD_OR_I16:
4028 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4029 case ARM::ATOMIC_LOAD_OR_I32:
4030 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004031
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004032 case ARM::ATOMIC_LOAD_XOR_I8:
4033 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
4034 case ARM::ATOMIC_LOAD_XOR_I16:
4035 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
4036 case ARM::ATOMIC_LOAD_XOR_I32:
4037 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004038
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004039 case ARM::ATOMIC_LOAD_NAND_I8:
4040 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
4041 case ARM::ATOMIC_LOAD_NAND_I16:
4042 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
4043 case ARM::ATOMIC_LOAD_NAND_I32:
4044 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004045
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004046 case ARM::ATOMIC_LOAD_SUB_I8:
4047 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
4048 case ARM::ATOMIC_LOAD_SUB_I16:
4049 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
4050 case ARM::ATOMIC_LOAD_SUB_I32:
4051 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004052
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004053 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
4054 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
4055 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00004056
4057 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
4058 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
4059 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004060
Evan Cheng007ea272009-08-12 05:17:19 +00004061 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00004062 // To "insert" a SELECT_CC instruction, we actually have to insert the
4063 // diamond control-flow pattern. The incoming instruction knows the
4064 // destination vreg to set, the condition code register to branch on, the
4065 // true/false values to select between, and a branch opcode to use.
4066 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004067 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00004068 ++It;
4069
4070 // thisMBB:
4071 // ...
4072 // TrueVal = ...
4073 // cmpTY ccX, r1, r2
4074 // bCC copy1MBB
4075 // fallthrough --> copy0MBB
4076 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004077 MachineFunction *F = BB->getParent();
4078 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
4079 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00004080 F->insert(It, copy0MBB);
4081 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004082
4083 // Transfer the remainder of BB and its successor edges to sinkMBB.
4084 sinkMBB->splice(sinkMBB->begin(), BB,
4085 llvm::next(MachineBasicBlock::iterator(MI)),
4086 BB->end());
4087 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
4088
Dan Gohman258c58c2010-07-06 15:49:48 +00004089 BB->addSuccessor(copy0MBB);
4090 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00004091
Dan Gohman14152b42010-07-06 20:24:04 +00004092 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
4093 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
4094
Evan Chenga8e29892007-01-19 07:51:42 +00004095 // copy0MBB:
4096 // %FalseValue = ...
4097 // # fallthrough to sinkMBB
4098 BB = copy0MBB;
4099
4100 // Update machine-CFG edges
4101 BB->addSuccessor(sinkMBB);
4102
4103 // sinkMBB:
4104 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4105 // ...
4106 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00004107 BuildMI(*BB, BB->begin(), dl,
4108 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00004109 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4110 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4111
Dan Gohman14152b42010-07-06 20:24:04 +00004112 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00004113 return BB;
4114 }
Evan Cheng86198642009-08-07 00:34:42 +00004115
Evan Cheng218977b2010-07-13 19:27:42 +00004116 case ARM::BCCi64:
4117 case ARM::BCCZi64: {
4118 // Compare both parts that make up the double comparison separately for
4119 // equality.
4120 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
4121
4122 unsigned LHS1 = MI->getOperand(1).getReg();
4123 unsigned LHS2 = MI->getOperand(2).getReg();
4124 if (RHSisZero) {
4125 AddDefaultPred(BuildMI(BB, dl,
4126 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
4127 .addReg(LHS1).addImm(0));
4128 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
4129 .addReg(LHS2).addImm(0)
4130 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
4131 } else {
4132 unsigned RHS1 = MI->getOperand(3).getReg();
4133 unsigned RHS2 = MI->getOperand(4).getReg();
4134 AddDefaultPred(BuildMI(BB, dl,
4135 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
4136 .addReg(LHS1).addReg(RHS1));
4137 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
4138 .addReg(LHS2).addReg(RHS2)
4139 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
4140 }
4141
4142 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
4143 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
4144 if (MI->getOperand(0).getImm() == ARMCC::NE)
4145 std::swap(destMBB, exitMBB);
4146
4147 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4148 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
4149 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
4150 .addMBB(exitMBB);
4151
4152 MI->eraseFromParent(); // The pseudo instruction is gone now.
4153 return BB;
4154 }
Evan Chenga8e29892007-01-19 07:51:42 +00004155 }
4156}
4157
4158//===----------------------------------------------------------------------===//
4159// ARM Optimization Hooks
4160//===----------------------------------------------------------------------===//
4161
Chris Lattnerd1980a52009-03-12 06:52:53 +00004162static
4163SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
4164 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00004165 SelectionDAG &DAG = DCI.DAG;
4166 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004167 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00004168 unsigned Opc = N->getOpcode();
4169 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
4170 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
4171 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
4172 ISD::CondCode CC = ISD::SETCC_INVALID;
4173
4174 if (isSlctCC) {
4175 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
4176 } else {
4177 SDValue CCOp = Slct.getOperand(0);
4178 if (CCOp.getOpcode() == ISD::SETCC)
4179 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
4180 }
4181
4182 bool DoXform = false;
4183 bool InvCC = false;
4184 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
4185 "Bad input!");
4186
4187 if (LHS.getOpcode() == ISD::Constant &&
4188 cast<ConstantSDNode>(LHS)->isNullValue()) {
4189 DoXform = true;
4190 } else if (CC != ISD::SETCC_INVALID &&
4191 RHS.getOpcode() == ISD::Constant &&
4192 cast<ConstantSDNode>(RHS)->isNullValue()) {
4193 std::swap(LHS, RHS);
4194 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004195 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00004196 Op0.getOperand(0).getValueType();
4197 bool isInt = OpVT.isInteger();
4198 CC = ISD::getSetCCInverse(CC, isInt);
4199
4200 if (!TLI.isCondCodeLegal(CC, OpVT))
4201 return SDValue(); // Inverse operator isn't legal.
4202
4203 DoXform = true;
4204 InvCC = true;
4205 }
4206
4207 if (DoXform) {
4208 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
4209 if (isSlctCC)
4210 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
4211 Slct.getOperand(0), Slct.getOperand(1), CC);
4212 SDValue CCOp = Slct.getOperand(0);
4213 if (InvCC)
4214 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
4215 CCOp.getOperand(0), CCOp.getOperand(1), CC);
4216 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
4217 CCOp, OtherOp, Result);
4218 }
4219 return SDValue();
4220}
4221
Bob Wilson3d5792a2010-07-29 20:34:14 +00004222/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
4223/// operands N0 and N1. This is a helper for PerformADDCombine that is
4224/// called with the default operands, and if that fails, with commuted
4225/// operands.
4226static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
4227 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson67b453b2010-08-04 00:12:08 +00004228 SelectionDAG &DAG = DCI.DAG;
4229
Chris Lattnerd1980a52009-03-12 06:52:53 +00004230 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
4231 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
4232 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
4233 if (Result.getNode()) return Result;
4234 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00004235
Bob Wilson67b453b2010-08-04 00:12:08 +00004236 // fold (add (arm_neon_vabd a, b) c) -> (arm_neon_vaba c, a, b)
4237 EVT VT = N->getValueType(0);
4238 if (N0.getOpcode() == ISD::INTRINSIC_WO_CHAIN && VT.isInteger()) {
4239 unsigned IntNo = cast<ConstantSDNode>(N0.getOperand(0))->getZExtValue();
4240 if (IntNo == Intrinsic::arm_neon_vabds)
4241 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(), VT,
4242 DAG.getConstant(Intrinsic::arm_neon_vabas, MVT::i32),
4243 N1, N0.getOperand(1), N0.getOperand(2));
4244 if (IntNo == Intrinsic::arm_neon_vabdu)
4245 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(), VT,
4246 DAG.getConstant(Intrinsic::arm_neon_vabau, MVT::i32),
4247 N1, N0.getOperand(1), N0.getOperand(2));
4248 }
4249
Chris Lattnerd1980a52009-03-12 06:52:53 +00004250 return SDValue();
4251}
4252
Bob Wilson3d5792a2010-07-29 20:34:14 +00004253/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
4254///
4255static SDValue PerformADDCombine(SDNode *N,
4256 TargetLowering::DAGCombinerInfo &DCI) {
4257 SDValue N0 = N->getOperand(0);
4258 SDValue N1 = N->getOperand(1);
4259
4260 // First try with the default operand order.
4261 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI);
4262 if (Result.getNode())
4263 return Result;
4264
4265 // If that didn't work, try again with the operands commuted.
4266 return PerformADDCombineWithOperands(N, N1, N0, DCI);
4267}
4268
Chris Lattnerd1980a52009-03-12 06:52:53 +00004269/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00004270///
Chris Lattnerd1980a52009-03-12 06:52:53 +00004271static SDValue PerformSUBCombine(SDNode *N,
4272 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00004273 SDValue N0 = N->getOperand(0);
4274 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00004275
Chris Lattnerd1980a52009-03-12 06:52:53 +00004276 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
4277 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
4278 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
4279 if (Result.getNode()) return Result;
4280 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00004281
Chris Lattnerd1980a52009-03-12 06:52:53 +00004282 return SDValue();
4283}
4284
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004285static SDValue PerformMULCombine(SDNode *N,
4286 TargetLowering::DAGCombinerInfo &DCI,
4287 const ARMSubtarget *Subtarget) {
4288 SelectionDAG &DAG = DCI.DAG;
4289
4290 if (Subtarget->isThumb1Only())
4291 return SDValue();
4292
4293 if (DAG.getMachineFunction().
4294 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
4295 return SDValue();
4296
4297 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
4298 return SDValue();
4299
4300 EVT VT = N->getValueType(0);
4301 if (VT != MVT::i32)
4302 return SDValue();
4303
4304 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
4305 if (!C)
4306 return SDValue();
4307
4308 uint64_t MulAmt = C->getZExtValue();
4309 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
4310 ShiftAmt = ShiftAmt & (32 - 1);
4311 SDValue V = N->getOperand(0);
4312 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004313
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004314 SDValue Res;
4315 MulAmt >>= ShiftAmt;
4316 if (isPowerOf2_32(MulAmt - 1)) {
4317 // (mul x, 2^N + 1) => (add (shl x, N), x)
4318 Res = DAG.getNode(ISD::ADD, DL, VT,
4319 V, DAG.getNode(ISD::SHL, DL, VT,
4320 V, DAG.getConstant(Log2_32(MulAmt-1),
4321 MVT::i32)));
4322 } else if (isPowerOf2_32(MulAmt + 1)) {
4323 // (mul x, 2^N - 1) => (sub (shl x, N), x)
4324 Res = DAG.getNode(ISD::SUB, DL, VT,
4325 DAG.getNode(ISD::SHL, DL, VT,
4326 V, DAG.getConstant(Log2_32(MulAmt+1),
4327 MVT::i32)),
4328 V);
4329 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004330 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004331
4332 if (ShiftAmt != 0)
4333 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
4334 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004335
4336 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004337 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004338 return SDValue();
4339}
4340
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004341/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
4342static SDValue PerformORCombine(SDNode *N,
4343 TargetLowering::DAGCombinerInfo &DCI,
4344 const ARMSubtarget *Subtarget) {
Jim Grosbach54238562010-07-17 03:30:54 +00004345 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
4346 // reasonable.
4347
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004348 // BFI is only available on V6T2+
4349 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
4350 return SDValue();
4351
4352 SelectionDAG &DAG = DCI.DAG;
4353 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Jim Grosbach54238562010-07-17 03:30:54 +00004354 DebugLoc DL = N->getDebugLoc();
4355 // 1) or (and A, mask), val => ARMbfi A, val, mask
4356 // iff (val & mask) == val
4357 //
4358 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
4359 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
4360 // && CountPopulation_32(mask) == CountPopulation_32(~mask2)
4361 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
4362 // && CountPopulation_32(mask) == CountPopulation_32(~mask2)
4363 // (i.e., copy a bitfield value into another bitfield of the same width)
4364 if (N0.getOpcode() != ISD::AND)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004365 return SDValue();
4366
4367 EVT VT = N->getValueType(0);
4368 if (VT != MVT::i32)
4369 return SDValue();
4370
Jim Grosbach54238562010-07-17 03:30:54 +00004371
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004372 // The value and the mask need to be constants so we can verify this is
4373 // actually a bitfield set. If the mask is 0xffff, we can do better
4374 // via a movt instruction, so don't use BFI in that case.
4375 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
4376 if (!C)
4377 return SDValue();
4378 unsigned Mask = C->getZExtValue();
4379 if (Mask == 0xffff)
4380 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004381 SDValue Res;
4382 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
4383 if ((C = dyn_cast<ConstantSDNode>(N1))) {
4384 unsigned Val = C->getZExtValue();
4385 if (!ARM::isBitFieldInvertedMask(Mask) || (Val & ~Mask) != Val)
4386 return SDValue();
4387 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004388
Jim Grosbach54238562010-07-17 03:30:54 +00004389 Res = DAG.getNode(ARMISD::BFI, DL, VT, N0.getOperand(0),
4390 DAG.getConstant(Val, MVT::i32),
4391 DAG.getConstant(Mask, MVT::i32));
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004392
Jim Grosbach54238562010-07-17 03:30:54 +00004393 // Do not add new nodes to DAG combiner worklist.
4394 DCI.CombineTo(N, Res, false);
4395 } else if (N1.getOpcode() == ISD::AND) {
4396 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
4397 C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
4398 if (!C)
4399 return SDValue();
4400 unsigned Mask2 = C->getZExtValue();
4401
4402 if (ARM::isBitFieldInvertedMask(Mask) &&
4403 ARM::isBitFieldInvertedMask(~Mask2) &&
4404 (CountPopulation_32(Mask) == CountPopulation_32(~Mask2))) {
4405 // The pack halfword instruction works better for masks that fit it,
4406 // so use that when it's available.
4407 if (Subtarget->hasT2ExtractPack() &&
4408 (Mask == 0xffff || Mask == 0xffff0000))
4409 return SDValue();
4410 // 2a
4411 unsigned lsb = CountTrailingZeros_32(Mask2);
4412 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
4413 DAG.getConstant(lsb, MVT::i32));
4414 Res = DAG.getNode(ARMISD::BFI, DL, VT, N0.getOperand(0), Res,
4415 DAG.getConstant(Mask, MVT::i32));
4416 // Do not add new nodes to DAG combiner worklist.
4417 DCI.CombineTo(N, Res, false);
4418 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
4419 ARM::isBitFieldInvertedMask(Mask2) &&
4420 (CountPopulation_32(~Mask) == CountPopulation_32(Mask2))) {
4421 // The pack halfword instruction works better for masks that fit it,
4422 // so use that when it's available.
4423 if (Subtarget->hasT2ExtractPack() &&
4424 (Mask2 == 0xffff || Mask2 == 0xffff0000))
4425 return SDValue();
4426 // 2b
4427 unsigned lsb = CountTrailingZeros_32(Mask);
4428 Res = DAG.getNode(ISD::SRL, DL, VT, N0.getOperand(0),
4429 DAG.getConstant(lsb, MVT::i32));
4430 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
4431 DAG.getConstant(Mask2, MVT::i32));
4432 // Do not add new nodes to DAG combiner worklist.
4433 DCI.CombineTo(N, Res, false);
4434 }
4435 }
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004436
4437 return SDValue();
4438}
4439
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +00004440/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
4441/// ARMISD::VMOVRRD.
Jim Grosbache5165492009-11-09 00:11:35 +00004442static SDValue PerformVMOVRRDCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004443 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004444 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00004445 SDValue InDouble = N->getOperand(0);
Jim Grosbache5165492009-11-09 00:11:35 +00004446 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004447 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004448 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004449}
4450
Bob Wilson9e82bf12010-07-14 01:22:12 +00004451/// PerformVDUPLANECombine - Target-specific dag combine xforms for
4452/// ARMISD::VDUPLANE.
4453static SDValue PerformVDUPLANECombine(SDNode *N,
4454 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004455 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
4456 // redundant.
Bob Wilson9e82bf12010-07-14 01:22:12 +00004457 SDValue Op = N->getOperand(0);
4458 EVT VT = N->getValueType(0);
4459
4460 // Ignore bit_converts.
4461 while (Op.getOpcode() == ISD::BIT_CONVERT)
4462 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004463 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00004464 return SDValue();
4465
4466 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
4467 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
4468 // The canonical VMOV for a zero vector uses a 32-bit element size.
4469 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
4470 unsigned EltBits;
4471 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
4472 EltSize = 8;
4473 if (EltSize > VT.getVectorElementType().getSizeInBits())
4474 return SDValue();
4475
4476 SDValue Res = DCI.DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
4477 return DCI.CombineTo(N, Res, false);
4478}
4479
Bob Wilson5bafff32009-06-22 23:27:02 +00004480/// getVShiftImm - Check if this is a valid build_vector for the immediate
4481/// operand of a vector shift operation, where all the elements of the
4482/// build_vector must have the same constant integer value.
4483static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
4484 // Ignore bit_converts.
4485 while (Op.getOpcode() == ISD::BIT_CONVERT)
4486 Op = Op.getOperand(0);
4487 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
4488 APInt SplatBits, SplatUndef;
4489 unsigned SplatBitSize;
4490 bool HasAnyUndefs;
4491 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
4492 HasAnyUndefs, ElementBits) ||
4493 SplatBitSize > ElementBits)
4494 return false;
4495 Cnt = SplatBits.getSExtValue();
4496 return true;
4497}
4498
4499/// isVShiftLImm - Check if this is a valid build_vector for the immediate
4500/// operand of a vector shift left operation. That value must be in the range:
4501/// 0 <= Value < ElementBits for a left shift; or
4502/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00004503static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00004504 assert(VT.isVector() && "vector shift count is not a vector type");
4505 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
4506 if (! getVShiftImm(Op, ElementBits, Cnt))
4507 return false;
4508 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
4509}
4510
4511/// isVShiftRImm - Check if this is a valid build_vector for the immediate
4512/// operand of a vector shift right operation. For a shift opcode, the value
4513/// is positive, but for an intrinsic the value count must be negative. The
4514/// absolute value must be in the range:
4515/// 1 <= |Value| <= ElementBits for a right shift; or
4516/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00004517static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00004518 int64_t &Cnt) {
4519 assert(VT.isVector() && "vector shift count is not a vector type");
4520 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
4521 if (! getVShiftImm(Op, ElementBits, Cnt))
4522 return false;
4523 if (isIntrinsic)
4524 Cnt = -Cnt;
4525 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
4526}
4527
4528/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
4529static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
4530 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
4531 switch (IntNo) {
4532 default:
4533 // Don't do anything for most intrinsics.
4534 break;
4535
4536 // Vector shifts: check for immediate versions and lower them.
4537 // Note: This is done during DAG combining instead of DAG legalizing because
4538 // the build_vectors for 64-bit vector element shift counts are generally
4539 // not legal, and it is hard to see their values after they get legalized to
4540 // loads from a constant pool.
4541 case Intrinsic::arm_neon_vshifts:
4542 case Intrinsic::arm_neon_vshiftu:
4543 case Intrinsic::arm_neon_vshiftls:
4544 case Intrinsic::arm_neon_vshiftlu:
4545 case Intrinsic::arm_neon_vshiftn:
4546 case Intrinsic::arm_neon_vrshifts:
4547 case Intrinsic::arm_neon_vrshiftu:
4548 case Intrinsic::arm_neon_vrshiftn:
4549 case Intrinsic::arm_neon_vqshifts:
4550 case Intrinsic::arm_neon_vqshiftu:
4551 case Intrinsic::arm_neon_vqshiftsu:
4552 case Intrinsic::arm_neon_vqshiftns:
4553 case Intrinsic::arm_neon_vqshiftnu:
4554 case Intrinsic::arm_neon_vqshiftnsu:
4555 case Intrinsic::arm_neon_vqrshiftns:
4556 case Intrinsic::arm_neon_vqrshiftnu:
4557 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00004558 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004559 int64_t Cnt;
4560 unsigned VShiftOpc = 0;
4561
4562 switch (IntNo) {
4563 case Intrinsic::arm_neon_vshifts:
4564 case Intrinsic::arm_neon_vshiftu:
4565 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
4566 VShiftOpc = ARMISD::VSHL;
4567 break;
4568 }
4569 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
4570 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
4571 ARMISD::VSHRs : ARMISD::VSHRu);
4572 break;
4573 }
4574 return SDValue();
4575
4576 case Intrinsic::arm_neon_vshiftls:
4577 case Intrinsic::arm_neon_vshiftlu:
4578 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
4579 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00004580 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004581
4582 case Intrinsic::arm_neon_vrshifts:
4583 case Intrinsic::arm_neon_vrshiftu:
4584 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
4585 break;
4586 return SDValue();
4587
4588 case Intrinsic::arm_neon_vqshifts:
4589 case Intrinsic::arm_neon_vqshiftu:
4590 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
4591 break;
4592 return SDValue();
4593
4594 case Intrinsic::arm_neon_vqshiftsu:
4595 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
4596 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00004597 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004598
4599 case Intrinsic::arm_neon_vshiftn:
4600 case Intrinsic::arm_neon_vrshiftn:
4601 case Intrinsic::arm_neon_vqshiftns:
4602 case Intrinsic::arm_neon_vqshiftnu:
4603 case Intrinsic::arm_neon_vqshiftnsu:
4604 case Intrinsic::arm_neon_vqrshiftns:
4605 case Intrinsic::arm_neon_vqrshiftnu:
4606 case Intrinsic::arm_neon_vqrshiftnsu:
4607 // Narrowing shifts require an immediate right shift.
4608 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
4609 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00004610 llvm_unreachable("invalid shift count for narrowing vector shift "
4611 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004612
4613 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004614 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00004615 }
4616
4617 switch (IntNo) {
4618 case Intrinsic::arm_neon_vshifts:
4619 case Intrinsic::arm_neon_vshiftu:
4620 // Opcode already set above.
4621 break;
4622 case Intrinsic::arm_neon_vshiftls:
4623 case Intrinsic::arm_neon_vshiftlu:
4624 if (Cnt == VT.getVectorElementType().getSizeInBits())
4625 VShiftOpc = ARMISD::VSHLLi;
4626 else
4627 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
4628 ARMISD::VSHLLs : ARMISD::VSHLLu);
4629 break;
4630 case Intrinsic::arm_neon_vshiftn:
4631 VShiftOpc = ARMISD::VSHRN; break;
4632 case Intrinsic::arm_neon_vrshifts:
4633 VShiftOpc = ARMISD::VRSHRs; break;
4634 case Intrinsic::arm_neon_vrshiftu:
4635 VShiftOpc = ARMISD::VRSHRu; break;
4636 case Intrinsic::arm_neon_vrshiftn:
4637 VShiftOpc = ARMISD::VRSHRN; break;
4638 case Intrinsic::arm_neon_vqshifts:
4639 VShiftOpc = ARMISD::VQSHLs; break;
4640 case Intrinsic::arm_neon_vqshiftu:
4641 VShiftOpc = ARMISD::VQSHLu; break;
4642 case Intrinsic::arm_neon_vqshiftsu:
4643 VShiftOpc = ARMISD::VQSHLsu; break;
4644 case Intrinsic::arm_neon_vqshiftns:
4645 VShiftOpc = ARMISD::VQSHRNs; break;
4646 case Intrinsic::arm_neon_vqshiftnu:
4647 VShiftOpc = ARMISD::VQSHRNu; break;
4648 case Intrinsic::arm_neon_vqshiftnsu:
4649 VShiftOpc = ARMISD::VQSHRNsu; break;
4650 case Intrinsic::arm_neon_vqrshiftns:
4651 VShiftOpc = ARMISD::VQRSHRNs; break;
4652 case Intrinsic::arm_neon_vqrshiftnu:
4653 VShiftOpc = ARMISD::VQRSHRNu; break;
4654 case Intrinsic::arm_neon_vqrshiftnsu:
4655 VShiftOpc = ARMISD::VQRSHRNsu; break;
4656 }
4657
4658 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004659 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004660 }
4661
4662 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00004663 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004664 int64_t Cnt;
4665 unsigned VShiftOpc = 0;
4666
4667 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
4668 VShiftOpc = ARMISD::VSLI;
4669 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
4670 VShiftOpc = ARMISD::VSRI;
4671 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00004672 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004673 }
4674
4675 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
4676 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00004677 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004678 }
4679
4680 case Intrinsic::arm_neon_vqrshifts:
4681 case Intrinsic::arm_neon_vqrshiftu:
4682 // No immediate versions of these to check for.
4683 break;
4684 }
4685
4686 return SDValue();
4687}
4688
4689/// PerformShiftCombine - Checks for immediate versions of vector shifts and
4690/// lowers them. As with the vector shift intrinsics, this is done during DAG
4691/// combining instead of DAG legalizing because the build_vectors for 64-bit
4692/// vector element shift counts are generally not legal, and it is hard to see
4693/// their values after they get legalized to loads from a constant pool.
4694static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
4695 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00004696 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00004697
4698 // Nothing to be done for scalar shifts.
4699 if (! VT.isVector())
4700 return SDValue();
4701
4702 assert(ST->hasNEON() && "unexpected vector shift");
4703 int64_t Cnt;
4704
4705 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004706 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00004707
4708 case ISD::SHL:
4709 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
4710 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004711 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004712 break;
4713
4714 case ISD::SRA:
4715 case ISD::SRL:
4716 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
4717 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
4718 ARMISD::VSHRs : ARMISD::VSHRu);
4719 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004720 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004721 }
4722 }
4723 return SDValue();
4724}
4725
4726/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
4727/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
4728static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
4729 const ARMSubtarget *ST) {
4730 SDValue N0 = N->getOperand(0);
4731
4732 // Check for sign- and zero-extensions of vector extract operations of 8-
4733 // and 16-bit vector elements. NEON supports these directly. They are
4734 // handled during DAG combining because type legalization will promote them
4735 // to 32-bit types and it is messy to recognize the operations after that.
4736 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
4737 SDValue Vec = N0.getOperand(0);
4738 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004739 EVT VT = N->getValueType(0);
4740 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004741 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4742
Owen Anderson825b72b2009-08-11 20:47:22 +00004743 if (VT == MVT::i32 &&
4744 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00004745 TLI.isTypeLegal(Vec.getValueType())) {
4746
4747 unsigned Opc = 0;
4748 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004749 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00004750 case ISD::SIGN_EXTEND:
4751 Opc = ARMISD::VGETLANEs;
4752 break;
4753 case ISD::ZERO_EXTEND:
4754 case ISD::ANY_EXTEND:
4755 Opc = ARMISD::VGETLANEu;
4756 break;
4757 }
4758 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
4759 }
4760 }
4761
4762 return SDValue();
4763}
4764
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004765/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
4766/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
4767static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
4768 const ARMSubtarget *ST) {
4769 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00004770 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004771 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
4772 // a NaN; only do the transformation when it matches that behavior.
4773
4774 // For now only do this when using NEON for FP operations; if using VFP, it
4775 // is not obvious that the benefit outweighs the cost of switching to the
4776 // NEON pipeline.
4777 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
4778 N->getValueType(0) != MVT::f32)
4779 return SDValue();
4780
4781 SDValue CondLHS = N->getOperand(0);
4782 SDValue CondRHS = N->getOperand(1);
4783 SDValue LHS = N->getOperand(2);
4784 SDValue RHS = N->getOperand(3);
4785 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
4786
4787 unsigned Opcode = 0;
4788 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00004789 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004790 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00004791 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004792 IsReversed = true ; // x CC y ? y : x
4793 } else {
4794 return SDValue();
4795 }
4796
Bob Wilsone742bb52010-02-24 22:15:53 +00004797 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004798 switch (CC) {
4799 default: break;
4800 case ISD::SETOLT:
4801 case ISD::SETOLE:
4802 case ISD::SETLT:
4803 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004804 case ISD::SETULT:
4805 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004806 // If LHS is NaN, an ordered comparison will be false and the result will
4807 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
4808 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4809 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
4810 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4811 break;
4812 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
4813 // will return -0, so vmin can only be used for unsafe math or if one of
4814 // the operands is known to be nonzero.
4815 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
4816 !UnsafeFPMath &&
4817 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4818 break;
4819 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004820 break;
4821
4822 case ISD::SETOGT:
4823 case ISD::SETOGE:
4824 case ISD::SETGT:
4825 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004826 case ISD::SETUGT:
4827 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004828 // If LHS is NaN, an ordered comparison will be false and the result will
4829 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
4830 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4831 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
4832 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4833 break;
4834 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
4835 // will return +0, so vmax can only be used for unsafe math or if one of
4836 // the operands is known to be nonzero.
4837 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
4838 !UnsafeFPMath &&
4839 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4840 break;
4841 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004842 break;
4843 }
4844
4845 if (!Opcode)
4846 return SDValue();
4847 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
4848}
4849
Dan Gohman475871a2008-07-27 21:46:04 +00004850SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004851 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004852 switch (N->getOpcode()) {
4853 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004854 case ISD::ADD: return PerformADDCombine(N, DCI);
4855 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004856 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004857 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Jim Grosbache5165492009-11-09 00:11:35 +00004858 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson9e82bf12010-07-14 01:22:12 +00004859 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004860 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004861 case ISD::SHL:
4862 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004863 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004864 case ISD::SIGN_EXTEND:
4865 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004866 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
4867 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004868 }
Dan Gohman475871a2008-07-27 21:46:04 +00004869 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004870}
4871
Bill Wendlingaf566342009-08-15 21:21:19 +00004872bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
4873 if (!Subtarget->hasV6Ops())
4874 // Pre-v6 does not support unaligned mem access.
4875 return false;
Bob Wilson86fe66d2010-06-25 04:12:31 +00004876
4877 // v6+ may or may not support unaligned mem access depending on the system
4878 // configuration.
4879 // FIXME: This is pretty conservative. Should we provide cmdline option to
4880 // control the behaviour?
4881 if (!Subtarget->isTargetDarwin())
4882 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00004883
4884 switch (VT.getSimpleVT().SimpleTy) {
4885 default:
4886 return false;
4887 case MVT::i8:
4888 case MVT::i16:
4889 case MVT::i32:
4890 return true;
4891 // FIXME: VLD1 etc with standard alignment is legal.
4892 }
4893}
4894
Evan Chenge6c835f2009-08-14 20:09:37 +00004895static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
4896 if (V < 0)
4897 return false;
4898
4899 unsigned Scale = 1;
4900 switch (VT.getSimpleVT().SimpleTy) {
4901 default: return false;
4902 case MVT::i1:
4903 case MVT::i8:
4904 // Scale == 1;
4905 break;
4906 case MVT::i16:
4907 // Scale == 2;
4908 Scale = 2;
4909 break;
4910 case MVT::i32:
4911 // Scale == 4;
4912 Scale = 4;
4913 break;
4914 }
4915
4916 if ((V & (Scale - 1)) != 0)
4917 return false;
4918 V /= Scale;
4919 return V == (V & ((1LL << 5) - 1));
4920}
4921
4922static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
4923 const ARMSubtarget *Subtarget) {
4924 bool isNeg = false;
4925 if (V < 0) {
4926 isNeg = true;
4927 V = - V;
4928 }
4929
4930 switch (VT.getSimpleVT().SimpleTy) {
4931 default: return false;
4932 case MVT::i1:
4933 case MVT::i8:
4934 case MVT::i16:
4935 case MVT::i32:
4936 // + imm12 or - imm8
4937 if (isNeg)
4938 return V == (V & ((1LL << 8) - 1));
4939 return V == (V & ((1LL << 12) - 1));
4940 case MVT::f32:
4941 case MVT::f64:
4942 // Same as ARM mode. FIXME: NEON?
4943 if (!Subtarget->hasVFP2())
4944 return false;
4945 if ((V & 3) != 0)
4946 return false;
4947 V >>= 2;
4948 return V == (V & ((1LL << 8) - 1));
4949 }
4950}
4951
Evan Chengb01fad62007-03-12 23:30:29 +00004952/// isLegalAddressImmediate - Return true if the integer value can be used
4953/// as the offset of the target addressing mode for load / store of the
4954/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00004955static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004956 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00004957 if (V == 0)
4958 return true;
4959
Evan Cheng65011532009-03-09 19:15:00 +00004960 if (!VT.isSimple())
4961 return false;
4962
Evan Chenge6c835f2009-08-14 20:09:37 +00004963 if (Subtarget->isThumb1Only())
4964 return isLegalT1AddressImmediate(V, VT);
4965 else if (Subtarget->isThumb2())
4966 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00004967
Evan Chenge6c835f2009-08-14 20:09:37 +00004968 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00004969 if (V < 0)
4970 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00004971 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00004972 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004973 case MVT::i1:
4974 case MVT::i8:
4975 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00004976 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004977 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004978 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00004979 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004980 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004981 case MVT::f32:
4982 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00004983 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00004984 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00004985 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00004986 return false;
4987 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004988 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00004989 }
Evan Chenga8e29892007-01-19 07:51:42 +00004990}
4991
Evan Chenge6c835f2009-08-14 20:09:37 +00004992bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
4993 EVT VT) const {
4994 int Scale = AM.Scale;
4995 if (Scale < 0)
4996 return false;
4997
4998 switch (VT.getSimpleVT().SimpleTy) {
4999 default: return false;
5000 case MVT::i1:
5001 case MVT::i8:
5002 case MVT::i16:
5003 case MVT::i32:
5004 if (Scale == 1)
5005 return true;
5006 // r + r << imm
5007 Scale = Scale & ~1;
5008 return Scale == 2 || Scale == 4 || Scale == 8;
5009 case MVT::i64:
5010 // r + r
5011 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
5012 return true;
5013 return false;
5014 case MVT::isVoid:
5015 // Note, we allow "void" uses (basically, uses that aren't loads or
5016 // stores), because arm allows folding a scale into many arithmetic
5017 // operations. This should be made more precise and revisited later.
5018
5019 // Allow r << imm, but the imm has to be a multiple of two.
5020 if (Scale & 1) return false;
5021 return isPowerOf2_32(Scale);
5022 }
5023}
5024
Chris Lattner37caf8c2007-04-09 23:33:39 +00005025/// isLegalAddressingMode - Return true if the addressing mode represented
5026/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005027bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00005028 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005029 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00005030 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00005031 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005032
Chris Lattner37caf8c2007-04-09 23:33:39 +00005033 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005034 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005035 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005036
Chris Lattner37caf8c2007-04-09 23:33:39 +00005037 switch (AM.Scale) {
5038 case 0: // no scale reg, must be "r+i" or "r", or "i".
5039 break;
5040 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00005041 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00005042 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00005043 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00005044 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00005045 // ARM doesn't support any R+R*scale+imm addr modes.
5046 if (AM.BaseOffs)
5047 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005048
Bob Wilson2c7dab12009-04-08 17:55:28 +00005049 if (!VT.isSimple())
5050 return false;
5051
Evan Chenge6c835f2009-08-14 20:09:37 +00005052 if (Subtarget->isThumb2())
5053 return isLegalT2ScaledAddressingMode(AM, VT);
5054
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005055 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00005056 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00005057 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00005058 case MVT::i1:
5059 case MVT::i8:
5060 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005061 if (Scale < 0) Scale = -Scale;
5062 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005063 return true;
5064 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00005065 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00005066 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00005067 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00005068 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005069 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005070 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00005071 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005072
Owen Anderson825b72b2009-08-11 20:47:22 +00005073 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00005074 // Note, we allow "void" uses (basically, uses that aren't loads or
5075 // stores), because arm allows folding a scale into many arithmetic
5076 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005077
Chris Lattner37caf8c2007-04-09 23:33:39 +00005078 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00005079 if (Scale & 1) return false;
5080 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00005081 }
5082 break;
Evan Chengb01fad62007-03-12 23:30:29 +00005083 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00005084 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00005085}
5086
Evan Cheng77e47512009-11-11 19:05:52 +00005087/// isLegalICmpImmediate - Return true if the specified immediate is legal
5088/// icmp immediate, that is the target has icmp instructions which can compare
5089/// a register against the immediate without having to materialize the
5090/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00005091bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00005092 if (!Subtarget->isThumb())
5093 return ARM_AM::getSOImmVal(Imm) != -1;
5094 if (Subtarget->isThumb2())
5095 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00005096 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00005097}
5098
Owen Andersone50ed302009-08-10 22:56:29 +00005099static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00005100 bool isSEXTLoad, SDValue &Base,
5101 SDValue &Offset, bool &isInc,
5102 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00005103 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
5104 return false;
5105
Owen Anderson825b72b2009-08-11 20:47:22 +00005106 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00005107 // AddressingMode 3
5108 Base = Ptr->getOperand(0);
5109 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005110 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005111 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005112 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00005113 isInc = false;
5114 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5115 return true;
5116 }
5117 }
5118 isInc = (Ptr->getOpcode() == ISD::ADD);
5119 Offset = Ptr->getOperand(1);
5120 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00005121 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00005122 // AddressingMode 2
5123 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005124 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005125 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005126 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00005127 isInc = false;
5128 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5129 Base = Ptr->getOperand(0);
5130 return true;
5131 }
5132 }
5133
5134 if (Ptr->getOpcode() == ISD::ADD) {
5135 isInc = true;
5136 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
5137 if (ShOpcVal != ARM_AM::no_shift) {
5138 Base = Ptr->getOperand(1);
5139 Offset = Ptr->getOperand(0);
5140 } else {
5141 Base = Ptr->getOperand(0);
5142 Offset = Ptr->getOperand(1);
5143 }
5144 return true;
5145 }
5146
5147 isInc = (Ptr->getOpcode() == ISD::ADD);
5148 Base = Ptr->getOperand(0);
5149 Offset = Ptr->getOperand(1);
5150 return true;
5151 }
5152
Jim Grosbache5165492009-11-09 00:11:35 +00005153 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00005154 return false;
5155}
5156
Owen Andersone50ed302009-08-10 22:56:29 +00005157static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00005158 bool isSEXTLoad, SDValue &Base,
5159 SDValue &Offset, bool &isInc,
5160 SelectionDAG &DAG) {
5161 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
5162 return false;
5163
5164 Base = Ptr->getOperand(0);
5165 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
5166 int RHSC = (int)RHS->getZExtValue();
5167 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
5168 assert(Ptr->getOpcode() == ISD::ADD);
5169 isInc = false;
5170 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5171 return true;
5172 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
5173 isInc = Ptr->getOpcode() == ISD::ADD;
5174 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
5175 return true;
5176 }
5177 }
5178
5179 return false;
5180}
5181
Evan Chenga8e29892007-01-19 07:51:42 +00005182/// getPreIndexedAddressParts - returns true by value, base pointer and
5183/// offset pointer and addressing mode by reference if the node's address
5184/// can be legally represented as pre-indexed load / store address.
5185bool
Dan Gohman475871a2008-07-27 21:46:04 +00005186ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
5187 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00005188 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00005189 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005190 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00005191 return false;
5192
Owen Andersone50ed302009-08-10 22:56:29 +00005193 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00005194 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00005195 bool isSEXTLoad = false;
5196 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5197 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005198 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00005199 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
5200 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
5201 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005202 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00005203 } else
5204 return false;
5205
5206 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00005207 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00005208 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00005209 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
5210 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00005211 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00005212 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00005213 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00005214 if (!isLegal)
5215 return false;
5216
5217 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
5218 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00005219}
5220
5221/// getPostIndexedAddressParts - returns true by value, base pointer and
5222/// offset pointer and addressing mode by reference if this node can be
5223/// combined with a load / store to form a post-indexed load / store.
5224bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00005225 SDValue &Base,
5226 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00005227 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00005228 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005229 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00005230 return false;
5231
Owen Andersone50ed302009-08-10 22:56:29 +00005232 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00005233 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00005234 bool isSEXTLoad = false;
5235 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005236 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00005237 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00005238 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
5239 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005240 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00005241 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00005242 } else
5243 return false;
5244
5245 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00005246 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00005247 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00005248 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00005249 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00005250 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00005251 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
5252 isInc, DAG);
5253 if (!isLegal)
5254 return false;
5255
Evan Cheng28dad2a2010-05-18 21:31:17 +00005256 if (Ptr != Base) {
5257 // Swap base ptr and offset to catch more post-index load / store when
5258 // it's legal. In Thumb2 mode, offset must be an immediate.
5259 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
5260 !Subtarget->isThumb2())
5261 std::swap(Base, Offset);
5262
5263 // Post-indexed load / store update the base pointer.
5264 if (Ptr != Base)
5265 return false;
5266 }
5267
Evan Chenge88d5ce2009-07-02 07:28:31 +00005268 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
5269 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00005270}
5271
Dan Gohman475871a2008-07-27 21:46:04 +00005272void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00005273 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00005274 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005275 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00005276 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00005277 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005278 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00005279 switch (Op.getOpcode()) {
5280 default: break;
5281 case ARMISD::CMOV: {
5282 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00005283 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00005284 if (KnownZero == 0 && KnownOne == 0) return;
5285
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005286 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00005287 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
5288 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00005289 KnownZero &= KnownZeroRHS;
5290 KnownOne &= KnownOneRHS;
5291 return;
5292 }
5293 }
5294}
5295
5296//===----------------------------------------------------------------------===//
5297// ARM Inline Assembly Support
5298//===----------------------------------------------------------------------===//
5299
5300/// getConstraintType - Given a constraint letter, return the type of
5301/// constraint it is for this target.
5302ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00005303ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
5304 if (Constraint.size() == 1) {
5305 switch (Constraint[0]) {
5306 default: break;
5307 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005308 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00005309 }
Evan Chenga8e29892007-01-19 07:51:42 +00005310 }
Chris Lattner4234f572007-03-25 02:14:49 +00005311 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00005312}
5313
Bob Wilson2dc4f542009-03-20 22:42:55 +00005314std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00005315ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00005316 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005317 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00005318 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00005319 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005320 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00005321 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00005322 return std::make_pair(0U, ARM::tGPRRegisterClass);
5323 else
5324 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005325 case 'r':
5326 return std::make_pair(0U, ARM::GPRRegisterClass);
5327 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00005328 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005329 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00005330 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005331 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00005332 if (VT.getSizeInBits() == 128)
5333 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005334 break;
Evan Chenga8e29892007-01-19 07:51:42 +00005335 }
5336 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00005337 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00005338 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00005339
Evan Chenga8e29892007-01-19 07:51:42 +00005340 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
5341}
5342
5343std::vector<unsigned> ARMTargetLowering::
5344getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00005345 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005346 if (Constraint.size() != 1)
5347 return std::vector<unsigned>();
5348
5349 switch (Constraint[0]) { // GCC ARM Constraint Letters
5350 default: break;
5351 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00005352 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
5353 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
5354 0);
Evan Chenga8e29892007-01-19 07:51:42 +00005355 case 'r':
5356 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
5357 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
5358 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
5359 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005360 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00005361 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005362 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
5363 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
5364 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
5365 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
5366 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
5367 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
5368 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
5369 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00005370 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005371 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
5372 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
5373 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
5374 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00005375 if (VT.getSizeInBits() == 128)
5376 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
5377 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005378 break;
Evan Chenga8e29892007-01-19 07:51:42 +00005379 }
5380
5381 return std::vector<unsigned>();
5382}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005383
5384/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
5385/// vector. If it is invalid, don't add anything to Ops.
5386void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
5387 char Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005388 std::vector<SDValue>&Ops,
5389 SelectionDAG &DAG) const {
5390 SDValue Result(0, 0);
5391
5392 switch (Constraint) {
5393 default: break;
5394 case 'I': case 'J': case 'K': case 'L':
5395 case 'M': case 'N': case 'O':
5396 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
5397 if (!C)
5398 return;
5399
5400 int64_t CVal64 = C->getSExtValue();
5401 int CVal = (int) CVal64;
5402 // None of these constraints allow values larger than 32 bits. Check
5403 // that the value fits in an int.
5404 if (CVal != CVal64)
5405 return;
5406
5407 switch (Constraint) {
5408 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005409 if (Subtarget->isThumb1Only()) {
5410 // This must be a constant between 0 and 255, for ADD
5411 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005412 if (CVal >= 0 && CVal <= 255)
5413 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005414 } else if (Subtarget->isThumb2()) {
5415 // A constant that can be used as an immediate value in a
5416 // data-processing instruction.
5417 if (ARM_AM::getT2SOImmVal(CVal) != -1)
5418 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005419 } else {
5420 // A constant that can be used as an immediate value in a
5421 // data-processing instruction.
5422 if (ARM_AM::getSOImmVal(CVal) != -1)
5423 break;
5424 }
5425 return;
5426
5427 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005428 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005429 // This must be a constant between -255 and -1, for negated ADD
5430 // immediates. This can be used in GCC with an "n" modifier that
5431 // prints the negated value, for use with SUB instructions. It is
5432 // not useful otherwise but is implemented for compatibility.
5433 if (CVal >= -255 && CVal <= -1)
5434 break;
5435 } else {
5436 // This must be a constant between -4095 and 4095. It is not clear
5437 // what this constraint is intended for. Implemented for
5438 // compatibility with GCC.
5439 if (CVal >= -4095 && CVal <= 4095)
5440 break;
5441 }
5442 return;
5443
5444 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005445 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005446 // A 32-bit value where only one byte has a nonzero value. Exclude
5447 // zero to match GCC. This constraint is used by GCC internally for
5448 // constants that can be loaded with a move/shift combination.
5449 // It is not useful otherwise but is implemented for compatibility.
5450 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
5451 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005452 } else if (Subtarget->isThumb2()) {
5453 // A constant whose bitwise inverse can be used as an immediate
5454 // value in a data-processing instruction. This can be used in GCC
5455 // with a "B" modifier that prints the inverted value, for use with
5456 // BIC and MVN instructions. It is not useful otherwise but is
5457 // implemented for compatibility.
5458 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
5459 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005460 } else {
5461 // A constant whose bitwise inverse can be used as an immediate
5462 // value in a data-processing instruction. This can be used in GCC
5463 // with a "B" modifier that prints the inverted value, for use with
5464 // BIC and MVN instructions. It is not useful otherwise but is
5465 // implemented for compatibility.
5466 if (ARM_AM::getSOImmVal(~CVal) != -1)
5467 break;
5468 }
5469 return;
5470
5471 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005472 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005473 // This must be a constant between -7 and 7,
5474 // for 3-operand ADD/SUB immediate instructions.
5475 if (CVal >= -7 && CVal < 7)
5476 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005477 } else if (Subtarget->isThumb2()) {
5478 // A constant whose negation can be used as an immediate value in a
5479 // data-processing instruction. This can be used in GCC with an "n"
5480 // modifier that prints the negated value, for use with SUB
5481 // instructions. It is not useful otherwise but is implemented for
5482 // compatibility.
5483 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
5484 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005485 } else {
5486 // A constant whose negation can be used as an immediate value in a
5487 // data-processing instruction. This can be used in GCC with an "n"
5488 // modifier that prints the negated value, for use with SUB
5489 // instructions. It is not useful otherwise but is implemented for
5490 // compatibility.
5491 if (ARM_AM::getSOImmVal(-CVal) != -1)
5492 break;
5493 }
5494 return;
5495
5496 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005497 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005498 // This must be a multiple of 4 between 0 and 1020, for
5499 // ADD sp + immediate.
5500 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
5501 break;
5502 } else {
5503 // A power of two or a constant between 0 and 32. This is used in
5504 // GCC for the shift amount on shifted register operands, but it is
5505 // useful in general for any shift amounts.
5506 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
5507 break;
5508 }
5509 return;
5510
5511 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005512 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005513 // This must be a constant between 0 and 31, for shift amounts.
5514 if (CVal >= 0 && CVal <= 31)
5515 break;
5516 }
5517 return;
5518
5519 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005520 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005521 // This must be a multiple of 4 between -508 and 508, for
5522 // ADD/SUB sp = sp + immediate.
5523 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
5524 break;
5525 }
5526 return;
5527 }
5528 Result = DAG.getTargetConstant(CVal, Op.getValueType());
5529 break;
5530 }
5531
5532 if (Result.getNode()) {
5533 Ops.push_back(Result);
5534 return;
5535 }
Dale Johannesen1784d162010-06-25 21:55:36 +00005536 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005537}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00005538
5539bool
5540ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
5541 // The ARM target isn't yet aware of offsets.
5542 return false;
5543}
Evan Cheng39382422009-10-28 01:44:26 +00005544
5545int ARM::getVFPf32Imm(const APFloat &FPImm) {
5546 APInt Imm = FPImm.bitcastToAPInt();
5547 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
5548 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
5549 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
5550
5551 // We can handle 4 bits of mantissa.
5552 // mantissa = (16+UInt(e:f:g:h))/16.
5553 if (Mantissa & 0x7ffff)
5554 return -1;
5555 Mantissa >>= 19;
5556 if ((Mantissa & 0xf) != Mantissa)
5557 return -1;
5558
5559 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
5560 if (Exp < -3 || Exp > 4)
5561 return -1;
5562 Exp = ((Exp+3) & 0x7) ^ 4;
5563
5564 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
5565}
5566
5567int ARM::getVFPf64Imm(const APFloat &FPImm) {
5568 APInt Imm = FPImm.bitcastToAPInt();
5569 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
5570 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
5571 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
5572
5573 // We can handle 4 bits of mantissa.
5574 // mantissa = (16+UInt(e:f:g:h))/16.
5575 if (Mantissa & 0xffffffffffffLL)
5576 return -1;
5577 Mantissa >>= 48;
5578 if ((Mantissa & 0xf) != Mantissa)
5579 return -1;
5580
5581 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
5582 if (Exp < -3 || Exp > 4)
5583 return -1;
5584 Exp = ((Exp+3) & 0x7) ^ 4;
5585
5586 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
5587}
5588
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005589bool ARM::isBitFieldInvertedMask(unsigned v) {
5590 if (v == 0xffffffff)
5591 return 0;
5592 // there can be 1's on either or both "outsides", all the "inside"
5593 // bits must be 0's
5594 unsigned int lsb = 0, msb = 31;
5595 while (v & (1 << msb)) --msb;
5596 while (v & (1 << lsb)) ++lsb;
5597 for (unsigned int i = lsb; i <= msb; ++i) {
5598 if (v & (1 << i))
5599 return 0;
5600 }
5601 return 1;
5602}
5603
Evan Cheng39382422009-10-28 01:44:26 +00005604/// isFPImmLegal - Returns true if the target can instruction select the
5605/// specified FP immediate natively. If false, the legalizer will
5606/// materialize the FP immediate as a load from a constant pool.
5607bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
5608 if (!Subtarget->hasVFP3())
5609 return false;
5610 if (VT == MVT::f32)
5611 return ARM::getVFPf32Imm(Imm) != -1;
5612 if (VT == MVT::f64)
5613 return ARM::getVFPf64Imm(Imm) != -1;
5614 return false;
5615}