blob: 6f2b3b83bc33f3ea6fa604640571f42a190cdbbc [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"
Eric Christopher6f2ccef2010-09-10 22:42:06 +000017#include "ARMCallingConv.h"
Evan Chenga8e29892007-01-19 07:51:42 +000018#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 Chengee04a6d2011-07-20 23:34:39 +000026#include "MCTargetDesc/ARMAddressingModes.h"
Evan Chenga8e29892007-01-19 07:51:42 +000027#include "llvm/CallingConv.h"
28#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000029#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000030#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000031#include "llvm/Instruction.h"
Bob Wilson65ffec42010-09-21 17:56:22 +000032#include "llvm/Instructions.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000033#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000034#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000035#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng55d42002011-01-08 01:24:27 +000036#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Chenga8e29892007-01-19 07:51:42 +000037#include "llvm/CodeGen/MachineBasicBlock.h"
38#include "llvm/CodeGen/MachineFrameInfo.h"
39#include "llvm/CodeGen/MachineFunction.h"
40#include "llvm/CodeGen/MachineInstrBuilder.h"
Bill Wendling2a850152011-10-05 00:02:33 +000041#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000042#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000043#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000044#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000045#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000046#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000047#include "llvm/ADT/VectorExtras.h"
Evan Cheng55d42002011-01-08 01:24:27 +000048#include "llvm/ADT/StringExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000049#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000050#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000051#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000052#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000053#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000054#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000055using namespace llvm;
56
Dale Johannesen51e28e62010-06-03 21:09:53 +000057STATISTIC(NumTailCalls, "Number of tail calls");
Evan Chengfc8475b2011-01-19 02:16:49 +000058STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Dale Johannesen51e28e62010-06-03 21:09:53 +000059
Bob Wilson703af3a2010-08-13 22:43:33 +000060// This option should go away when tail calls fully work.
61static cl::opt<bool>
62EnableARMTailCalls("arm-tail-calls", cl::Hidden,
63 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
64 cl::init(false));
65
Eric Christopher836c6242010-12-15 23:47:29 +000066cl::opt<bool>
Jim Grosbache7b52522010-04-14 22:28:31 +000067EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000068 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbache7b52522010-04-14 22:28:31 +000069 cl::init(false));
70
Evan Cheng46df4eb2010-06-16 07:35:02 +000071static cl::opt<bool>
72ARMInterworking("arm-interworking", cl::Hidden,
73 cl::desc("Enable / disable ARM interworking (for debugging only)"),
74 cl::init(true));
75
Cameron Zwaricha86686e2011-06-10 20:59:24 +000076namespace llvm {
77 class ARMCCState : public CCState {
78 public:
79 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
80 const TargetMachine &TM, SmallVector<CCValAssign, 16> &locs,
81 LLVMContext &C, ParmContext PC)
82 : CCState(CC, isVarArg, MF, TM, locs, C) {
83 assert(((PC == Call) || (PC == Prologue)) &&
84 "ARMCCState users must specify whether their context is call"
85 "or prologue generation.");
86 CallOrPrologue = PC;
87 }
88 };
89}
90
Stuart Hastingsc7315872011-04-20 16:47:52 +000091// The APCS parameter registers.
92static const unsigned GPRArgRegs[] = {
93 ARM::R0, ARM::R1, ARM::R2, ARM::R3
94};
95
Owen Andersone50ed302009-08-10 22:56:29 +000096void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
97 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000098 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000099 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000100 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
101 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000102
Owen Anderson70671842009-08-10 20:18:46 +0000103 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000104 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000105 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000106 }
107
Owen Andersone50ed302009-08-10 22:56:29 +0000108 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000109 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Duncan Sands28b77e92011-09-06 19:07:46 +0000110 setOperationAction(ISD::SETCC, VT.getSimpleVT(), Custom);
Eli Friedman5c89cb82011-10-24 23:08:52 +0000111 setOperationAction(ISD::INSERT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson3468c2e2010-11-03 16:24:50 +0000112 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Eli Friedman14e809c2011-11-09 23:36:02 +0000113 if (ElemTy == MVT::i32) {
114 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Custom);
115 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Custom);
116 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Custom);
117 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Custom);
118 } else {
Bob Wilson0696fdf2009-09-16 20:20:44 +0000119 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
120 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
121 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
122 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
123 }
Owen Anderson70671842009-08-10 20:18:46 +0000124 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
125 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +0000126 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Bob Wilson5e8b8332011-01-07 04:59:04 +0000127 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal);
Bob Wilsond0910c42010-04-06 22:02:24 +0000128 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
129 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000130 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000131 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
132 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
133 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000134 }
135
136 // Promote all bit-wise operations.
137 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000138 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000139 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
140 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000141 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000142 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000143 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000144 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000145 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000146 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000147 }
Bob Wilson16330762009-09-16 00:17:28 +0000148
149 // Neon does not support vector divide/remainder operations.
150 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
151 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
152 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
153 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
154 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
155 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000156}
157
Owen Andersone50ed302009-08-10 22:56:29 +0000158void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000159 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000160 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000161}
162
Owen Andersone50ed302009-08-10 22:56:29 +0000163void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000164 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000166}
167
Chris Lattnerf0144122009-07-28 03:13:23 +0000168static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
169 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000170 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000171
Chris Lattner80ec2792009-08-02 00:34:36 +0000172 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000173}
174
Evan Chenga8e29892007-01-19 07:51:42 +0000175ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000176 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000177 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000178 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000179 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000180
Duncan Sands28b77e92011-09-06 19:07:46 +0000181 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
182
Evan Chengb1df8f22007-04-27 08:15:43 +0000183 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000184 // Uses VFP for Thumb libfuncs if available.
185 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
186 // Single-precision floating-point arithmetic.
187 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
188 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
189 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
190 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000191
Evan Chengb1df8f22007-04-27 08:15:43 +0000192 // Double-precision floating-point arithmetic.
193 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
194 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
195 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
196 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000197
Evan Chengb1df8f22007-04-27 08:15:43 +0000198 // Single-precision comparisons.
199 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
200 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
201 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
202 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
203 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
204 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
205 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
206 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000207
Evan Chengb1df8f22007-04-27 08:15:43 +0000208 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
214 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
215 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000216
Evan Chengb1df8f22007-04-27 08:15:43 +0000217 // Double-precision comparisons.
218 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
219 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
220 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
221 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
222 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
223 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
224 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
225 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000226
Evan Chengb1df8f22007-04-27 08:15:43 +0000227 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
228 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
229 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
230 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
231 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
232 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
233 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
234 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000235
Evan Chengb1df8f22007-04-27 08:15:43 +0000236 // Floating-point to integer conversions.
237 // i64 conversions are done via library routines even when generating VFP
238 // instructions, so use the same ones.
239 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
240 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
241 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
242 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000243
Evan Chengb1df8f22007-04-27 08:15:43 +0000244 // Conversions between floating types.
245 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
246 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
247
248 // Integer to floating-point conversions.
249 // i64 conversions are done via library routines even when generating VFP
250 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000251 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
252 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000253 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
254 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
255 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
256 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
257 }
Evan Chenga8e29892007-01-19 07:51:42 +0000258 }
259
Bob Wilson2f954612009-05-22 17:38:41 +0000260 // These libcalls are not available in 32-bit.
261 setLibcallName(RTLIB::SHL_I128, 0);
262 setLibcallName(RTLIB::SRL_I128, 0);
263 setLibcallName(RTLIB::SRA_I128, 0);
264
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000265 if (Subtarget->isAAPCS_ABI()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000266 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000267 // RTABI chapter 4.1.2, Table 2
268 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
269 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
270 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
271 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
272 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
273 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
274 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
275 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
276
277 // Double-precision floating-point comparison helper functions
278 // RTABI chapter 4.1.2, Table 3
279 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
280 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
281 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
282 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
283 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
284 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
285 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
286 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
287 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
288 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
289 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
290 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
291 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
292 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
293 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
294 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
295 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
296 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
297 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
298 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
299 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
300 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
301 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
302 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
303
304 // Single-precision floating-point arithmetic helper functions
305 // RTABI chapter 4.1.2, Table 4
306 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
307 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
308 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
309 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
310 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
311 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
312 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
313 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
314
315 // Single-precision floating-point comparison helper functions
316 // RTABI chapter 4.1.2, Table 5
317 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
318 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
319 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
320 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
321 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
322 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
323 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
324 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
325 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
326 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
327 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
328 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
329 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
330 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
331 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
332 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
333 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
334 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
335 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
336 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
337 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
338 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
339 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
340 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
341
342 // Floating-point to integer conversions.
343 // RTABI chapter 4.1.2, Table 6
344 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
345 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
346 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
347 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
348 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
349 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
350 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
351 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
352 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
353 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
354 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
355 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
356 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
357 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
358 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
359 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
360
361 // Conversions between floating types.
362 // RTABI chapter 4.1.2, Table 7
363 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
364 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
365 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000366 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000367
368 // Integer to floating-point conversions.
369 // RTABI chapter 4.1.2, Table 8
370 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
371 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
372 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
373 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
374 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
375 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
376 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
377 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
378 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
379 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
380 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
381 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
382 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
383 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
384 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
385 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
386
387 // Long long helper functions
388 // RTABI chapter 4.2, Table 9
389 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
390 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
391 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
392 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
393 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
394 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
395 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
396 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
397 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
398 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
399 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
400 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
401
402 // Integer division functions
403 // RTABI chapter 4.3.1
404 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
405 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
406 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
407 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
408 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
409 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
410 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
411 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
412 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
413 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
414 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000415 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000416
417 // Memory operations
418 // RTABI chapter 4.3.4
419 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
420 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
421 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000422 }
423
Bob Wilson2fef4572011-10-07 16:59:21 +0000424 // Use divmod compiler-rt calls for iOS 5.0 and later.
425 if (Subtarget->getTargetTriple().getOS() == Triple::IOS &&
426 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
427 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
428 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
429 }
430
David Goodwinf1daf7d2009-07-08 23:10:31 +0000431 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000432 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000433 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000434 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000435 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000437 if (!Subtarget->isFPOnlySP())
438 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000439
Owen Anderson825b72b2009-08-11 20:47:22 +0000440 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000441 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000442
Eli Friedman9f1f26a2011-11-08 01:43:53 +0000443 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
444 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
445 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
446 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
447 setTruncStoreAction((MVT::SimpleValueType)VT,
448 (MVT::SimpleValueType)InnerVT, Expand);
449 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
450 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
451 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
452 }
453
Bob Wilson5bafff32009-06-22 23:27:02 +0000454 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000455 addDRTypeForNEON(MVT::v2f32);
456 addDRTypeForNEON(MVT::v8i8);
457 addDRTypeForNEON(MVT::v4i16);
458 addDRTypeForNEON(MVT::v2i32);
459 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000460
Owen Anderson825b72b2009-08-11 20:47:22 +0000461 addQRTypeForNEON(MVT::v4f32);
462 addQRTypeForNEON(MVT::v2f64);
463 addQRTypeForNEON(MVT::v16i8);
464 addQRTypeForNEON(MVT::v8i16);
465 addQRTypeForNEON(MVT::v4i32);
466 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000467
Bob Wilson74dc72e2009-09-15 23:55:57 +0000468 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
469 // neither Neon nor VFP support any arithmetic operations on it.
470 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
471 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
472 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
473 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
474 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
475 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000476 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Bob Wilson74dc72e2009-09-15 23:55:57 +0000477 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
478 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
479 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
480 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
481 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
482 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
483 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
484 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
485 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
486 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
487 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
488 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
489 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
490 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
491 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
492 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
493 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
494
Bob Wilson642b3292009-09-16 00:32:15 +0000495 // Neon does not support some operations on v1i64 and v2i64 types.
496 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000497 // Custom handling for some quad-vector types to detect VMULL.
498 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
499 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
500 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000501 // Custom handling for some vector types to avoid expensive expansions
502 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
503 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
504 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
505 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000506 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
507 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000508 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
509 // a destination type that is wider than the source.
510 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
511 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000512
Bob Wilson1c3ef902011-02-07 17:43:21 +0000513 setTargetDAGCombine(ISD::INTRINSIC_VOID);
514 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000515 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
516 setTargetDAGCombine(ISD::SHL);
517 setTargetDAGCombine(ISD::SRL);
518 setTargetDAGCombine(ISD::SRA);
519 setTargetDAGCombine(ISD::SIGN_EXTEND);
520 setTargetDAGCombine(ISD::ZERO_EXTEND);
521 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000522 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000523 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000524 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000525 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
526 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000527 setTargetDAGCombine(ISD::FP_TO_SINT);
528 setTargetDAGCombine(ISD::FP_TO_UINT);
529 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem004a24b2011-10-15 20:03:12 +0000530
531 setLoadExtAction(ISD::EXTLOAD, MVT::v4i8, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000532 }
533
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000534 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000535
536 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000537 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000538
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000539 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000540 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000541
Evan Chenga8e29892007-01-19 07:51:42 +0000542 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000543 if (!Subtarget->isThumb1Only()) {
544 for (unsigned im = (unsigned)ISD::PRE_INC;
545 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000546 setIndexedLoadAction(im, MVT::i1, Legal);
547 setIndexedLoadAction(im, MVT::i8, Legal);
548 setIndexedLoadAction(im, MVT::i16, Legal);
549 setIndexedLoadAction(im, MVT::i32, Legal);
550 setIndexedStoreAction(im, MVT::i1, Legal);
551 setIndexedStoreAction(im, MVT::i8, Legal);
552 setIndexedStoreAction(im, MVT::i16, Legal);
553 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000554 }
Evan Chenga8e29892007-01-19 07:51:42 +0000555 }
556
557 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000558 setOperationAction(ISD::MUL, MVT::i64, Expand);
559 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000560 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000561 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
562 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000563 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000564 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
565 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000566 setOperationAction(ISD::MULHS, MVT::i32, Expand);
567
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000568 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000569 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000570 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000571 setOperationAction(ISD::SRL, MVT::i64, Custom);
572 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000573
Evan Cheng342e3162011-08-30 01:34:54 +0000574 if (!Subtarget->isThumb1Only()) {
575 // FIXME: We should do this for Thumb1 as well.
576 setOperationAction(ISD::ADDC, MVT::i32, Custom);
577 setOperationAction(ISD::ADDE, MVT::i32, Custom);
578 setOperationAction(ISD::SUBC, MVT::i32, Custom);
579 setOperationAction(ISD::SUBE, MVT::i32, Custom);
580 }
581
Evan Chenga8e29892007-01-19 07:51:42 +0000582 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000583 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000584 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000585 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000586 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000587 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000588
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000589 // Only ARMv6 has BSWAP.
590 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000591 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000592
Evan Chenga8e29892007-01-19 07:51:42 +0000593 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000594 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000595 // v7M has a hardware divider
596 setOperationAction(ISD::SDIV, MVT::i32, Expand);
597 setOperationAction(ISD::UDIV, MVT::i32, Expand);
598 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000599 setOperationAction(ISD::SREM, MVT::i32, Expand);
600 setOperationAction(ISD::UREM, MVT::i32, Expand);
601 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
602 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000603
Owen Anderson825b72b2009-08-11 20:47:22 +0000604 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
605 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
606 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
607 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000608 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000609
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000610 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000611
Evan Chenga8e29892007-01-19 07:51:42 +0000612 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000613 setOperationAction(ISD::VASTART, MVT::Other, Custom);
614 setOperationAction(ISD::VAARG, MVT::Other, Expand);
615 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
616 setOperationAction(ISD::VAEND, MVT::Other, Expand);
617 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
618 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000619 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000620 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
621 setExceptionPointerRegister(ARM::R0);
622 setExceptionSelectorRegister(ARM::R1);
623
Evan Cheng3a1588a2010-04-15 22:20:34 +0000624 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000625 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
626 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000627 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000628 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000629 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000630 // membarrier needs custom lowering; the rest are legal and handled
631 // normally.
632 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000633 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000634 // Custom lowering for 64-bit ops
635 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
636 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
637 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
638 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
639 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
640 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000641 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000642 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
643 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000644 } else {
645 // Set them all for expansion, which will force libcalls.
646 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000647 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000648 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000649 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000650 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000651 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000652 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000653 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000654 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000655 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000656 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000657 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000658 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000659 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000660 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
661 // Unordered/Monotonic case.
662 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
663 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000664 // Since the libcalls include locking, fold in the fences
665 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000666 }
Evan Chenga8e29892007-01-19 07:51:42 +0000667
Evan Cheng416941d2010-11-04 05:19:35 +0000668 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000669
Eli Friedmana2c6f452010-06-26 04:36:50 +0000670 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
671 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
673 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000674 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000675 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000676
Nate Begemand1fb5832010-08-03 21:31:55 +0000677 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000678 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
679 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000680 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000681 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
682 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000683
684 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000686 if (Subtarget->isTargetDarwin()) {
687 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
688 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000689 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000690 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000691 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000692
Owen Anderson825b72b2009-08-11 20:47:22 +0000693 setOperationAction(ISD::SETCC, MVT::i32, Expand);
694 setOperationAction(ISD::SETCC, MVT::f32, Expand);
695 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000696 setOperationAction(ISD::SELECT, MVT::i32, Custom);
697 setOperationAction(ISD::SELECT, MVT::f32, Custom);
698 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000699 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
700 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
701 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000702
Owen Anderson825b72b2009-08-11 20:47:22 +0000703 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
704 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
705 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
706 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
707 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000708
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000709 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000710 setOperationAction(ISD::FSIN, MVT::f64, Expand);
711 setOperationAction(ISD::FSIN, MVT::f32, Expand);
712 setOperationAction(ISD::FCOS, MVT::f32, Expand);
713 setOperationAction(ISD::FCOS, MVT::f64, Expand);
714 setOperationAction(ISD::FREM, MVT::f64, Expand);
715 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000716 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000717 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
718 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000719 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000720 setOperationAction(ISD::FPOW, MVT::f64, Expand);
721 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000722
Cameron Zwarich33390842011-07-08 21:39:21 +0000723 setOperationAction(ISD::FMA, MVT::f64, Expand);
724 setOperationAction(ISD::FMA, MVT::f32, Expand);
725
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000726 // Various VFP goodness
727 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000728 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
729 if (Subtarget->hasVFP2()) {
730 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
731 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
732 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
733 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
734 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000735 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000736 if (!Subtarget->hasFP16()) {
737 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
738 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000739 }
Evan Cheng110cf482008-04-01 01:50:16 +0000740 }
Evan Chenga8e29892007-01-19 07:51:42 +0000741
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000742 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000743 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000744 setTargetDAGCombine(ISD::ADD);
745 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000746 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000747
Owen Anderson080c0922010-11-05 19:27:46 +0000748 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000749 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000750 if (Subtarget->hasNEON())
751 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000752
Evan Chenga8e29892007-01-19 07:51:42 +0000753 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000754
Evan Chengf7d87ee2010-05-21 00:43:17 +0000755 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
756 setSchedulingPreference(Sched::RegPressure);
757 else
758 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000759
Evan Cheng05219282011-01-06 06:52:41 +0000760 //// temporary - rewrite interface to use type
761 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Lang Hames75757f92011-10-26 20:56:52 +0000762 maxStoresPerMemset = 16;
763 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengf6799392010-06-26 01:52:05 +0000764
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000765 // On ARM arguments smaller than 4 bytes are extended, so all arguments
766 // are at least 4 bytes aligned.
767 setMinStackArgumentAlignment(4);
768
Evan Chengfff606d2010-09-24 19:07:23 +0000769 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000770
771 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000772}
773
Andrew Trick32cec0a2011-01-19 02:35:27 +0000774// FIXME: It might make sense to define the representative register class as the
775// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
776// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
777// SPR's representative would be DPR_VFP2. This should work well if register
778// pressure tracking were modified such that a register use would increment the
779// pressure of the register class's representative and all of it's super
780// classes' representatives transitively. We have not implemented this because
781// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000782// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000783// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000784std::pair<const TargetRegisterClass*, uint8_t>
785ARMTargetLowering::findRepresentativeClass(EVT VT) const{
786 const TargetRegisterClass *RRC = 0;
787 uint8_t Cost = 1;
788 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000789 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000790 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000791 // Use DPR as representative register class for all floating point
792 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
793 // the cost is 1 for both f32 and f64.
794 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000795 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000796 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000797 // When NEON is used for SP, only half of the register file is available
798 // because operations that define both SP and DP results will be constrained
799 // to the VFP2 class (D0-D15). We currently model this constraint prior to
800 // coalescing by double-counting the SP regs. See the FIXME above.
801 if (Subtarget->useNEONForSinglePrecisionFP())
802 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000803 break;
804 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
805 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000806 RRC = ARM::DPRRegisterClass;
807 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000808 break;
809 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000810 RRC = ARM::DPRRegisterClass;
811 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000812 break;
813 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000814 RRC = ARM::DPRRegisterClass;
815 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000816 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000817 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000818 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000819}
820
Evan Chenga8e29892007-01-19 07:51:42 +0000821const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
822 switch (Opcode) {
823 default: return 0;
824 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000825 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000826 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000827 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
828 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000829 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000830 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
831 case ARMISD::tCALL: return "ARMISD::tCALL";
832 case ARMISD::BRCOND: return "ARMISD::BRCOND";
833 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000834 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000835 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
836 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
837 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000838 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000839 case ARMISD::CMPFP: return "ARMISD::CMPFP";
840 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000841 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000842 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
843 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000844
Jim Grosbach3482c802010-01-18 19:58:49 +0000845 case ARMISD::RBIT: return "ARMISD::RBIT";
846
Bob Wilson76a312b2010-03-19 22:51:32 +0000847 case ARMISD::FTOSI: return "ARMISD::FTOSI";
848 case ARMISD::FTOUI: return "ARMISD::FTOUI";
849 case ARMISD::SITOF: return "ARMISD::SITOF";
850 case ARMISD::UITOF: return "ARMISD::UITOF";
851
Evan Chenga8e29892007-01-19 07:51:42 +0000852 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
853 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
854 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000855
Evan Cheng342e3162011-08-30 01:34:54 +0000856 case ARMISD::ADDC: return "ARMISD::ADDC";
857 case ARMISD::ADDE: return "ARMISD::ADDE";
858 case ARMISD::SUBC: return "ARMISD::SUBC";
859 case ARMISD::SUBE: return "ARMISD::SUBE";
860
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000861 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
862 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000863
Evan Chengc5942082009-10-28 06:55:03 +0000864 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
865 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000866 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000867
Dale Johannesen51e28e62010-06-03 21:09:53 +0000868 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000869
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000870 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000871
Evan Cheng86198642009-08-07 00:34:42 +0000872 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
873
Jim Grosbach3728e962009-12-10 00:11:09 +0000874 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000875 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000876
Evan Chengdfed19f2010-11-03 06:34:55 +0000877 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
878
Bob Wilson5bafff32009-06-22 23:27:02 +0000879 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000880 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000881 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000882 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
883 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000884 case ARMISD::VCGEU: return "ARMISD::VCGEU";
885 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000886 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
887 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000888 case ARMISD::VCGTU: return "ARMISD::VCGTU";
889 case ARMISD::VTST: return "ARMISD::VTST";
890
891 case ARMISD::VSHL: return "ARMISD::VSHL";
892 case ARMISD::VSHRs: return "ARMISD::VSHRs";
893 case ARMISD::VSHRu: return "ARMISD::VSHRu";
894 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
895 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
896 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
897 case ARMISD::VSHRN: return "ARMISD::VSHRN";
898 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
899 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
900 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
901 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
902 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
903 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
904 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
905 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
906 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
907 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
908 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
909 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
910 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
911 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000912 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000913 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000914 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000915 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000916 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000917 case ARMISD::VREV64: return "ARMISD::VREV64";
918 case ARMISD::VREV32: return "ARMISD::VREV32";
919 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000920 case ARMISD::VZIP: return "ARMISD::VZIP";
921 case ARMISD::VUZP: return "ARMISD::VUZP";
922 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000923 case ARMISD::VTBL1: return "ARMISD::VTBL1";
924 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000925 case ARMISD::VMULLs: return "ARMISD::VMULLs";
926 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000927 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000928 case ARMISD::FMAX: return "ARMISD::FMAX";
929 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000930 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000931 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
932 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000933 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000934 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
935 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
936 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000937 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
938 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
939 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
940 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
941 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
942 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
943 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
944 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
945 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
946 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
947 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
948 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
949 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
950 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
951 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
952 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
953 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000954 }
955}
956
Duncan Sands28b77e92011-09-06 19:07:46 +0000957EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
958 if (!VT.isVector()) return getPointerTy();
959 return VT.changeVectorElementTypeToInteger();
960}
961
Evan Cheng06b666c2010-05-15 02:18:07 +0000962/// getRegClassFor - Return the register class that should be used for the
963/// specified value type.
964TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
965 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
966 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
967 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000968 if (Subtarget->hasNEON()) {
969 if (VT == MVT::v4i64)
970 return ARM::QQPRRegisterClass;
971 else if (VT == MVT::v8i64)
972 return ARM::QQQQPRRegisterClass;
973 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000974 return TargetLowering::getRegClassFor(VT);
975}
976
Eric Christopherab695882010-07-21 22:26:11 +0000977// Create a fast isel object.
978FastISel *
979ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
980 return ARM::createFastISel(funcInfo);
981}
982
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000983/// getMaximalGlobalOffset - Returns the maximal possible offset which can
984/// be used for loads / stores from the global.
985unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
986 return (Subtarget->isThumb1Only() ? 127 : 4095);
987}
988
Evan Cheng1cc39842010-05-20 23:26:43 +0000989Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000990 unsigned NumVals = N->getNumValues();
991 if (!NumVals)
992 return Sched::RegPressure;
993
994 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000995 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000996 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000997 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000998 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +0000999 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001000 }
Evan Chengc10f5432010-05-28 23:25:23 +00001001
1002 if (!N->isMachineOpcode())
1003 return Sched::RegPressure;
1004
1005 // Load are scheduled for latency even if there instruction itinerary
1006 // is not available.
1007 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001008 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001009
Evan Chenge837dea2011-06-28 19:10:37 +00001010 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001011 return Sched::RegPressure;
1012 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001013 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001014 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001015
Evan Cheng1cc39842010-05-20 23:26:43 +00001016 return Sched::RegPressure;
1017}
1018
Evan Chenga8e29892007-01-19 07:51:42 +00001019//===----------------------------------------------------------------------===//
1020// Lowering Code
1021//===----------------------------------------------------------------------===//
1022
Evan Chenga8e29892007-01-19 07:51:42 +00001023/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1024static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1025 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001026 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001027 case ISD::SETNE: return ARMCC::NE;
1028 case ISD::SETEQ: return ARMCC::EQ;
1029 case ISD::SETGT: return ARMCC::GT;
1030 case ISD::SETGE: return ARMCC::GE;
1031 case ISD::SETLT: return ARMCC::LT;
1032 case ISD::SETLE: return ARMCC::LE;
1033 case ISD::SETUGT: return ARMCC::HI;
1034 case ISD::SETUGE: return ARMCC::HS;
1035 case ISD::SETULT: return ARMCC::LO;
1036 case ISD::SETULE: return ARMCC::LS;
1037 }
1038}
1039
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001040/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1041static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001042 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001043 CondCode2 = ARMCC::AL;
1044 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001045 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001046 case ISD::SETEQ:
1047 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1048 case ISD::SETGT:
1049 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1050 case ISD::SETGE:
1051 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1052 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001053 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001054 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1055 case ISD::SETO: CondCode = ARMCC::VC; break;
1056 case ISD::SETUO: CondCode = ARMCC::VS; break;
1057 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1058 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1059 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1060 case ISD::SETLT:
1061 case ISD::SETULT: CondCode = ARMCC::LT; break;
1062 case ISD::SETLE:
1063 case ISD::SETULE: CondCode = ARMCC::LE; break;
1064 case ISD::SETNE:
1065 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1066 }
Evan Chenga8e29892007-01-19 07:51:42 +00001067}
1068
Bob Wilson1f595bb2009-04-17 19:07:39 +00001069//===----------------------------------------------------------------------===//
1070// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001071//===----------------------------------------------------------------------===//
1072
1073#include "ARMGenCallingConv.inc"
1074
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001075/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1076/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001077CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001078 bool Return,
1079 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001080 switch (CC) {
1081 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001082 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001083 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001084 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001085 if (!Subtarget->isAAPCS_ABI())
1086 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1087 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1088 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1089 }
1090 // Fallthrough
1091 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001092 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001093 if (!Subtarget->isAAPCS_ABI())
1094 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1095 else if (Subtarget->hasVFP2() &&
1096 FloatABIType == FloatABI::Hard && !isVarArg)
1097 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1098 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1099 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001100 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001101 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001102 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001103 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001104 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001105 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001106 }
1107}
1108
Dan Gohman98ca4f22009-08-05 01:29:28 +00001109/// LowerCallResult - Lower the result values of a call into the
1110/// appropriate copies out of appropriate physical registers.
1111SDValue
1112ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001113 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001114 const SmallVectorImpl<ISD::InputArg> &Ins,
1115 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001116 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001117
Bob Wilson1f595bb2009-04-17 19:07:39 +00001118 // Assign locations to each value returned by this call.
1119 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001120 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1121 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001122 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001123 CCAssignFnForNode(CallConv, /* Return*/ true,
1124 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001125
1126 // Copy all of the result registers out of their specified physreg.
1127 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1128 CCValAssign VA = RVLocs[i];
1129
Bob Wilson80915242009-04-25 00:33:20 +00001130 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001131 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001132 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001133 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001134 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001135 Chain = Lo.getValue(1);
1136 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001137 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001138 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001139 InFlag);
1140 Chain = Hi.getValue(1);
1141 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001142 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001143
Owen Anderson825b72b2009-08-11 20:47:22 +00001144 if (VA.getLocVT() == MVT::v2f64) {
1145 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1146 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1147 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001148
1149 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001150 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001151 Chain = Lo.getValue(1);
1152 InFlag = Lo.getValue(2);
1153 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001154 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001155 Chain = Hi.getValue(1);
1156 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001157 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001158 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1159 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001160 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001161 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001162 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1163 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001164 Chain = Val.getValue(1);
1165 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001166 }
Bob Wilson80915242009-04-25 00:33:20 +00001167
1168 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001169 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001170 case CCValAssign::Full: break;
1171 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001172 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001173 break;
1174 }
1175
Dan Gohman98ca4f22009-08-05 01:29:28 +00001176 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001177 }
1178
Dan Gohman98ca4f22009-08-05 01:29:28 +00001179 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001180}
1181
Bob Wilsondee46d72009-04-17 20:35:10 +00001182/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001183SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001184ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1185 SDValue StackPtr, SDValue Arg,
1186 DebugLoc dl, SelectionDAG &DAG,
1187 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001188 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001189 unsigned LocMemOffset = VA.getLocMemOffset();
1190 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1191 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001192 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001193 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001194 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001195}
1196
Dan Gohman98ca4f22009-08-05 01:29:28 +00001197void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001198 SDValue Chain, SDValue &Arg,
1199 RegsToPassVector &RegsToPass,
1200 CCValAssign &VA, CCValAssign &NextVA,
1201 SDValue &StackPtr,
1202 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001203 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001204
Jim Grosbache5165492009-11-09 00:11:35 +00001205 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001206 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001207 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1208
1209 if (NextVA.isRegLoc())
1210 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1211 else {
1212 assert(NextVA.isMemLoc());
1213 if (StackPtr.getNode() == 0)
1214 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1215
Dan Gohman98ca4f22009-08-05 01:29:28 +00001216 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1217 dl, DAG, NextVA,
1218 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001219 }
1220}
1221
Dan Gohman98ca4f22009-08-05 01:29:28 +00001222/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001223/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1224/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001225SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001226ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001227 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001228 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001229 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001230 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001231 const SmallVectorImpl<ISD::InputArg> &Ins,
1232 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001233 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001234 MachineFunction &MF = DAG.getMachineFunction();
1235 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1236 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001237 // Disable tail calls if they're not supported.
1238 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001239 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001240 if (isTailCall) {
1241 // Check if it's really possible to do a tail call.
1242 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1243 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001244 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001245 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1246 // detected sibcalls.
1247 if (isTailCall) {
1248 ++NumTailCalls;
1249 IsSibCall = true;
1250 }
1251 }
Evan Chenga8e29892007-01-19 07:51:42 +00001252
Bob Wilson1f595bb2009-04-17 19:07:39 +00001253 // Analyze operands of the call, assigning locations to each operand.
1254 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001255 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1256 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001257 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001258 CCAssignFnForNode(CallConv, /* Return*/ false,
1259 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001260
Bob Wilson1f595bb2009-04-17 19:07:39 +00001261 // Get a count of how many bytes are to be pushed on the stack.
1262 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001263
Dale Johannesen51e28e62010-06-03 21:09:53 +00001264 // For tail calls, memory operands are available in our caller's stack.
1265 if (IsSibCall)
1266 NumBytes = 0;
1267
Evan Chenga8e29892007-01-19 07:51:42 +00001268 // Adjust the stack pointer for the new arguments...
1269 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001270 if (!IsSibCall)
1271 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001272
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001273 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001274
Bob Wilson5bafff32009-06-22 23:27:02 +00001275 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001276 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001277
Bob Wilson1f595bb2009-04-17 19:07:39 +00001278 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001279 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001280 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1281 i != e;
1282 ++i, ++realArgIdx) {
1283 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001284 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001285 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001286 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001287
Bob Wilson1f595bb2009-04-17 19:07:39 +00001288 // Promote the value if needed.
1289 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001290 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001291 case CCValAssign::Full: break;
1292 case CCValAssign::SExt:
1293 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1294 break;
1295 case CCValAssign::ZExt:
1296 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1297 break;
1298 case CCValAssign::AExt:
1299 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1300 break;
1301 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001302 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001303 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001304 }
1305
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001306 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001307 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001308 if (VA.getLocVT() == MVT::v2f64) {
1309 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1310 DAG.getConstant(0, MVT::i32));
1311 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1312 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001313
Dan Gohman98ca4f22009-08-05 01:29:28 +00001314 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001315 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1316
1317 VA = ArgLocs[++i]; // skip ahead to next loc
1318 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001319 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001320 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1321 } else {
1322 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001323
Dan Gohman98ca4f22009-08-05 01:29:28 +00001324 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1325 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001326 }
1327 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001328 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001329 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001330 }
1331 } else if (VA.isRegLoc()) {
1332 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001333 } else if (isByVal) {
1334 assert(VA.isMemLoc());
1335 unsigned offset = 0;
1336
1337 // True if this byval aggregate will be split between registers
1338 // and memory.
1339 if (CCInfo.isFirstByValRegValid()) {
1340 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1341 unsigned int i, j;
1342 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1343 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1344 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1345 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1346 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001347 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001348 MemOpChains.push_back(Load.getValue(1));
1349 RegsToPass.push_back(std::make_pair(j, Load));
1350 }
1351 offset = ARM::R4 - CCInfo.getFirstByValReg();
1352 CCInfo.clearFirstByValReg();
1353 }
1354
1355 unsigned LocMemOffset = VA.getLocMemOffset();
1356 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1357 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1358 StkPtrOff);
1359 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1360 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1361 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1362 MVT::i32);
1363 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1364 Flags.getByValAlign(),
1365 /*isVolatile=*/false,
Dan Gohman65fd6562011-11-03 21:49:52 +00001366 /*AlwaysInline=*/false,
Stuart Hastingsc7315872011-04-20 16:47:52 +00001367 MachinePointerInfo(0),
1368 MachinePointerInfo(0)));
1369
1370 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001371 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001372
Dan Gohman98ca4f22009-08-05 01:29:28 +00001373 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1374 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001375 }
Evan Chenga8e29892007-01-19 07:51:42 +00001376 }
1377
1378 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001379 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001380 &MemOpChains[0], MemOpChains.size());
1381
1382 // Build a sequence of copy-to-reg nodes chained together with token chain
1383 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001384 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001385 // Tail call byval lowering might overwrite argument registers so in case of
1386 // tail call optimization the copies to registers are lowered later.
1387 if (!isTailCall)
1388 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1389 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1390 RegsToPass[i].second, InFlag);
1391 InFlag = Chain.getValue(1);
1392 }
Evan Chenga8e29892007-01-19 07:51:42 +00001393
Dale Johannesen51e28e62010-06-03 21:09:53 +00001394 // For tail calls lower the arguments to the 'real' stack slot.
1395 if (isTailCall) {
1396 // Force all the incoming stack arguments to be loaded from the stack
1397 // before any new outgoing arguments are stored to the stack, because the
1398 // outgoing stack slots may alias the incoming argument stack slots, and
1399 // the alias isn't otherwise explicit. This is slightly more conservative
1400 // than necessary, because it means that each store effectively depends
1401 // on every argument instead of just those arguments it would clobber.
1402
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001403 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001404 InFlag = SDValue();
1405 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1406 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1407 RegsToPass[i].second, InFlag);
1408 InFlag = Chain.getValue(1);
1409 }
1410 InFlag =SDValue();
1411 }
1412
Bill Wendling056292f2008-09-16 21:48:12 +00001413 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1414 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1415 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001416 bool isDirect = false;
1417 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001418 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001419 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001420
1421 if (EnableARMLongCalls) {
1422 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1423 && "long-calls with non-static relocation model!");
1424 // Handle a global address or an external symbol. If it's not one of
1425 // those, the target's already in a register, so we don't need to do
1426 // anything extra.
1427 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001428 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001429 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001430 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001431 ARMConstantPoolValue *CPV =
1432 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1433
Jim Grosbache7b52522010-04-14 22:28:31 +00001434 // Get the address of the callee into a register
1435 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1436 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1437 Callee = DAG.getLoad(getPointerTy(), dl,
1438 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001439 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001440 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001441 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1442 const char *Sym = S->getSymbol();
1443
1444 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001445 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001446 ARMConstantPoolValue *CPV =
1447 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1448 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001449 // Get the address of the callee into a register
1450 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1451 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1452 Callee = DAG.getLoad(getPointerTy(), dl,
1453 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001454 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001455 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001456 }
1457 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001458 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001459 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001460 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001461 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001462 getTargetMachine().getRelocationModel() != Reloc::Static;
1463 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001464 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001465 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001466 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001467 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001468 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001469 ARMConstantPoolValue *CPV =
1470 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001471 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001472 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001473 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001474 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001475 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001476 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001477 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001478 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001479 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001480 } else {
1481 // On ELF targets for PIC code, direct calls should go through the PLT
1482 unsigned OpFlags = 0;
1483 if (Subtarget->isTargetELF() &&
1484 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1485 OpFlags = ARMII::MO_PLT;
1486 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1487 }
Bill Wendling056292f2008-09-16 21:48:12 +00001488 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001489 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001490 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001491 getTargetMachine().getRelocationModel() != Reloc::Static;
1492 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001493 // tBX takes a register source operand.
1494 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001495 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001496 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001497 ARMConstantPoolValue *CPV =
1498 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1499 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001500 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001501 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001502 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001503 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001504 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001505 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001506 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001507 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001508 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001509 } else {
1510 unsigned OpFlags = 0;
1511 // On ELF targets for PIC code, direct calls should go through the PLT
1512 if (Subtarget->isTargetELF() &&
1513 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1514 OpFlags = ARMII::MO_PLT;
1515 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1516 }
Evan Chenga8e29892007-01-19 07:51:42 +00001517 }
1518
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001519 // FIXME: handle tail calls differently.
1520 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001521 if (Subtarget->isThumb()) {
1522 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001523 CallOpc = ARMISD::CALL_NOLINK;
1524 else
1525 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1526 } else {
1527 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001528 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1529 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001530 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001531
Dan Gohman475871a2008-07-27 21:46:04 +00001532 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001533 Ops.push_back(Chain);
1534 Ops.push_back(Callee);
1535
1536 // Add argument registers to the end of the list so that they are known live
1537 // into the call.
1538 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1539 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1540 RegsToPass[i].second.getValueType()));
1541
Gabor Greifba36cb52008-08-28 21:40:38 +00001542 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001543 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001544
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001545 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001546 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001547 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001548
Duncan Sands4bdcb612008-07-02 17:40:58 +00001549 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001550 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001551 InFlag = Chain.getValue(1);
1552
Chris Lattnere563bbc2008-10-11 22:08:30 +00001553 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1554 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001555 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001556 InFlag = Chain.getValue(1);
1557
Bob Wilson1f595bb2009-04-17 19:07:39 +00001558 // Handle result values, copying them out of physregs into vregs that we
1559 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001560 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1561 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001562}
1563
Stuart Hastingsf222e592011-02-28 17:17:53 +00001564/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001565/// on the stack. Remember the next parameter register to allocate,
1566/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001567/// this.
1568void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001569llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1570 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1571 assert((State->getCallOrPrologue() == Prologue ||
1572 State->getCallOrPrologue() == Call) &&
1573 "unhandled ParmContext");
1574 if ((!State->isFirstByValRegValid()) &&
1575 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1576 State->setFirstByValReg(reg);
1577 // At a call site, a byval parameter that is split between
1578 // registers and memory needs its size truncated here. In a
1579 // function prologue, such byval parameters are reassembled in
1580 // memory, and are not truncated.
1581 if (State->getCallOrPrologue() == Call) {
1582 unsigned excess = 4 * (ARM::R4 - reg);
1583 assert(size >= excess && "expected larger existing stack allocation");
1584 size -= excess;
1585 }
1586 }
1587 // Confiscate any remaining parameter registers to preclude their
1588 // assignment to subsequent parameters.
1589 while (State->AllocateReg(GPRArgRegs, 4))
1590 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001591}
1592
Dale Johannesen51e28e62010-06-03 21:09:53 +00001593/// MatchingStackOffset - Return true if the given stack call argument is
1594/// already available in the same position (relatively) of the caller's
1595/// incoming argument stack.
1596static
1597bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1598 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1599 const ARMInstrInfo *TII) {
1600 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1601 int FI = INT_MAX;
1602 if (Arg.getOpcode() == ISD::CopyFromReg) {
1603 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001604 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001605 return false;
1606 MachineInstr *Def = MRI->getVRegDef(VR);
1607 if (!Def)
1608 return false;
1609 if (!Flags.isByVal()) {
1610 if (!TII->isLoadFromStackSlot(Def, FI))
1611 return false;
1612 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001613 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001614 }
1615 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1616 if (Flags.isByVal())
1617 // ByVal argument is passed in as a pointer but it's now being
1618 // dereferenced. e.g.
1619 // define @foo(%struct.X* %A) {
1620 // tail call @bar(%struct.X* byval %A)
1621 // }
1622 return false;
1623 SDValue Ptr = Ld->getBasePtr();
1624 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1625 if (!FINode)
1626 return false;
1627 FI = FINode->getIndex();
1628 } else
1629 return false;
1630
1631 assert(FI != INT_MAX);
1632 if (!MFI->isFixedObjectIndex(FI))
1633 return false;
1634 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1635}
1636
1637/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1638/// for tail call optimization. Targets which want to do tail call
1639/// optimization should implement this function.
1640bool
1641ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1642 CallingConv::ID CalleeCC,
1643 bool isVarArg,
1644 bool isCalleeStructRet,
1645 bool isCallerStructRet,
1646 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001647 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001648 const SmallVectorImpl<ISD::InputArg> &Ins,
1649 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001650 const Function *CallerF = DAG.getMachineFunction().getFunction();
1651 CallingConv::ID CallerCC = CallerF->getCallingConv();
1652 bool CCMatch = CallerCC == CalleeCC;
1653
1654 // Look for obvious safe cases to perform tail call optimization that do not
1655 // require ABI changes. This is what gcc calls sibcall.
1656
Jim Grosbach7616b642010-06-16 23:45:49 +00001657 // Do not sibcall optimize vararg calls unless the call site is not passing
1658 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001659 if (isVarArg && !Outs.empty())
1660 return false;
1661
1662 // Also avoid sibcall optimization if either caller or callee uses struct
1663 // return semantics.
1664 if (isCalleeStructRet || isCallerStructRet)
1665 return false;
1666
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001667 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001668 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1669 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1670 // support in the assembler and linker to be used. This would need to be
1671 // fixed to fully support tail calls in Thumb1.
1672 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001673 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1674 // LR. This means if we need to reload LR, it takes an extra instructions,
1675 // which outweighs the value of the tail call; but here we don't know yet
1676 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001677 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001678 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001679
1680 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1681 // but we need to make sure there are enough registers; the only valid
1682 // registers are the 4 used for parameters. We don't currently do this
1683 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001684 if (Subtarget->isThumb1Only())
1685 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001686
Dale Johannesen51e28e62010-06-03 21:09:53 +00001687 // If the calling conventions do not match, then we'd better make sure the
1688 // results are returned in the same way as what the caller expects.
1689 if (!CCMatch) {
1690 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001691 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1692 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001693 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1694
1695 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001696 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1697 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001698 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1699
1700 if (RVLocs1.size() != RVLocs2.size())
1701 return false;
1702 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1703 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1704 return false;
1705 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1706 return false;
1707 if (RVLocs1[i].isRegLoc()) {
1708 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1709 return false;
1710 } else {
1711 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1712 return false;
1713 }
1714 }
1715 }
1716
1717 // If the callee takes no arguments then go on to check the results of the
1718 // call.
1719 if (!Outs.empty()) {
1720 // Check if stack adjustment is needed. For now, do not do this if any
1721 // argument is passed on the stack.
1722 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001723 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1724 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001725 CCInfo.AnalyzeCallOperands(Outs,
1726 CCAssignFnForNode(CalleeCC, false, isVarArg));
1727 if (CCInfo.getNextStackOffset()) {
1728 MachineFunction &MF = DAG.getMachineFunction();
1729
1730 // Check if the arguments are already laid out in the right way as
1731 // the caller's fixed stack objects.
1732 MachineFrameInfo *MFI = MF.getFrameInfo();
1733 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1734 const ARMInstrInfo *TII =
1735 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001736 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1737 i != e;
1738 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001739 CCValAssign &VA = ArgLocs[i];
1740 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001741 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001742 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001743 if (VA.getLocInfo() == CCValAssign::Indirect)
1744 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001745 if (VA.needsCustom()) {
1746 // f64 and vector types are split into multiple registers or
1747 // register/stack-slot combinations. The types will not match
1748 // the registers; give up on memory f64 refs until we figure
1749 // out what to do about this.
1750 if (!VA.isRegLoc())
1751 return false;
1752 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001753 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001754 if (RegVT == MVT::v2f64) {
1755 if (!ArgLocs[++i].isRegLoc())
1756 return false;
1757 if (!ArgLocs[++i].isRegLoc())
1758 return false;
1759 }
1760 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001761 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1762 MFI, MRI, TII))
1763 return false;
1764 }
1765 }
1766 }
1767 }
1768
1769 return true;
1770}
1771
Dan Gohman98ca4f22009-08-05 01:29:28 +00001772SDValue
1773ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001774 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001775 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001776 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001777 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001778
Bob Wilsondee46d72009-04-17 20:35:10 +00001779 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001780 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001781
Bob Wilsondee46d72009-04-17 20:35:10 +00001782 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001783 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1784 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001785
Dan Gohman98ca4f22009-08-05 01:29:28 +00001786 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001787 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1788 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001789
1790 // If this is the first return lowered for this function, add
1791 // the regs to the liveout set for the function.
1792 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1793 for (unsigned i = 0; i != RVLocs.size(); ++i)
1794 if (RVLocs[i].isRegLoc())
1795 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001796 }
1797
Bob Wilson1f595bb2009-04-17 19:07:39 +00001798 SDValue Flag;
1799
1800 // Copy the result values into the output registers.
1801 for (unsigned i = 0, realRVLocIdx = 0;
1802 i != RVLocs.size();
1803 ++i, ++realRVLocIdx) {
1804 CCValAssign &VA = RVLocs[i];
1805 assert(VA.isRegLoc() && "Can only return in registers!");
1806
Dan Gohmanc9403652010-07-07 15:54:55 +00001807 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001808
1809 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001810 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001811 case CCValAssign::Full: break;
1812 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001813 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001814 break;
1815 }
1816
Bob Wilson1f595bb2009-04-17 19:07:39 +00001817 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001818 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001819 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001820 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1821 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001822 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001823 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001824
1825 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1826 Flag = Chain.getValue(1);
1827 VA = RVLocs[++i]; // skip ahead to next loc
1828 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1829 HalfGPRs.getValue(1), Flag);
1830 Flag = Chain.getValue(1);
1831 VA = RVLocs[++i]; // skip ahead to next loc
1832
1833 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001834 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1835 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001836 }
1837 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1838 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001839 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001840 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001841 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001842 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001843 VA = RVLocs[++i]; // skip ahead to next loc
1844 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1845 Flag);
1846 } else
1847 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1848
Bob Wilsondee46d72009-04-17 20:35:10 +00001849 // Guarantee that all emitted copies are
1850 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001851 Flag = Chain.getValue(1);
1852 }
1853
1854 SDValue result;
1855 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001856 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001857 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001858 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001859
1860 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001861}
1862
Evan Cheng3d2125c2010-11-30 23:55:39 +00001863bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1864 if (N->getNumValues() != 1)
1865 return false;
1866 if (!N->hasNUsesOfValue(1, 0))
1867 return false;
1868
1869 unsigned NumCopies = 0;
1870 SDNode* Copies[2];
1871 SDNode *Use = *N->use_begin();
1872 if (Use->getOpcode() == ISD::CopyToReg) {
1873 Copies[NumCopies++] = Use;
1874 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1875 // f64 returned in a pair of GPRs.
1876 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1877 UI != UE; ++UI) {
1878 if (UI->getOpcode() != ISD::CopyToReg)
1879 return false;
1880 Copies[UI.getUse().getResNo()] = *UI;
1881 ++NumCopies;
1882 }
1883 } else if (Use->getOpcode() == ISD::BITCAST) {
1884 // f32 returned in a single GPR.
1885 if (!Use->hasNUsesOfValue(1, 0))
1886 return false;
1887 Use = *Use->use_begin();
1888 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1889 return false;
1890 Copies[NumCopies++] = Use;
1891 } else {
1892 return false;
1893 }
1894
1895 if (NumCopies != 1 && NumCopies != 2)
1896 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001897
1898 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001899 for (unsigned i = 0; i < NumCopies; ++i) {
1900 SDNode *Copy = Copies[i];
1901 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1902 UI != UE; ++UI) {
1903 if (UI->getOpcode() == ISD::CopyToReg) {
1904 SDNode *Use = *UI;
1905 if (Use == Copies[0] || Use == Copies[1])
1906 continue;
1907 return false;
1908 }
1909 if (UI->getOpcode() != ARMISD::RET_FLAG)
1910 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001911 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001912 }
1913 }
1914
Evan Cheng1bf891a2010-12-01 22:59:46 +00001915 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001916}
1917
Evan Cheng485fafc2011-03-21 01:19:09 +00001918bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1919 if (!EnableARMTailCalls)
1920 return false;
1921
1922 if (!CI->isTailCall())
1923 return false;
1924
1925 return !Subtarget->isThumb1Only();
1926}
1927
Bob Wilsonb62d2572009-11-03 00:02:05 +00001928// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1929// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1930// one of the above mentioned nodes. It has to be wrapped because otherwise
1931// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1932// be used to form addressing mode. These wrapped nodes will be selected
1933// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001934static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001935 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001936 // FIXME there is no actual debug info here
1937 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001938 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001939 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001940 if (CP->isMachineConstantPoolEntry())
1941 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1942 CP->getAlignment());
1943 else
1944 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1945 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001946 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001947}
1948
Jim Grosbache1102ca2010-07-19 17:20:38 +00001949unsigned ARMTargetLowering::getJumpTableEncoding() const {
1950 return MachineJumpTableInfo::EK_Inline;
1951}
1952
Dan Gohmand858e902010-04-17 15:26:15 +00001953SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1954 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001955 MachineFunction &MF = DAG.getMachineFunction();
1956 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1957 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001958 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001959 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001960 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001961 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1962 SDValue CPAddr;
1963 if (RelocM == Reloc::Static) {
1964 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1965 } else {
1966 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001967 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001968 ARMConstantPoolValue *CPV =
1969 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
1970 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00001971 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1972 }
1973 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1974 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001975 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001976 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001977 if (RelocM == Reloc::Static)
1978 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001979 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001980 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001981}
1982
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001983// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001984SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001985ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001986 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001987 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001988 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001989 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001990 MachineFunction &MF = DAG.getMachineFunction();
1991 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001992 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001993 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00001994 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
1995 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001996 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001997 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001998 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001999 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002000 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002001 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002002
Evan Chenge7e0d622009-11-06 22:24:13 +00002003 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002004 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002005
2006 // call __tls_get_addr.
2007 ArgListTy Args;
2008 ArgListEntry Entry;
2009 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002010 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002011 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002012 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00002013 std::pair<SDValue, SDValue> CallResult =
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002014 LowerCallTo(Chain, (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002015 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002016 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002017 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002018 return CallResult.first;
2019}
2020
2021// Lower ISD::GlobalTLSAddress using the "initial exec" or
2022// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002023SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002024ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002025 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002026 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002027 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002028 SDValue Offset;
2029 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002030 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002031 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002032 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002033
Chris Lattner4fb63d02009-07-15 04:12:33 +00002034 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002035 MachineFunction &MF = DAG.getMachineFunction();
2036 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002037 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002038 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002039 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2040 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002041 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2042 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2043 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002044 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002045 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002046 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002047 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002048 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002049 Chain = Offset.getValue(1);
2050
Evan Chenge7e0d622009-11-06 22:24:13 +00002051 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002052 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002053
Evan Cheng9eda6892009-10-31 03:39:36 +00002054 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002055 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002056 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002057 } else {
2058 // local exec model
Bill Wendling5bb77992011-10-01 08:00:54 +00002059 ARMConstantPoolValue *CPV =
2060 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002061 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002062 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002063 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002064 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002065 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002066 }
2067
2068 // The address of the thread local variable is the add of the thread
2069 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002070 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002071}
2072
Dan Gohman475871a2008-07-27 21:46:04 +00002073SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002074ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002075 // TODO: implement the "local dynamic" model
2076 assert(Subtarget->isTargetELF() &&
2077 "TLS not implemented for non-ELF targets");
2078 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2079 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2080 // otherwise use the "Local Exec" TLS Model
2081 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2082 return LowerToTLSGeneralDynamicModel(GA, DAG);
2083 else
2084 return LowerToTLSExecModels(GA, DAG);
2085}
2086
Dan Gohman475871a2008-07-27 21:46:04 +00002087SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002088 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002089 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002090 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002091 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002092 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2093 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002094 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002095 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002096 ARMConstantPoolConstant::Create(GV,
2097 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002098 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002099 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002100 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002101 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002102 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002103 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002104 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002105 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002106 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002107 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002108 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002109 MachinePointerInfo::getGOT(),
2110 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002111 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002112 }
2113
2114 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002115 // pair. This is always cheaper.
2116 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002117 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002118 // FIXME: Once remat is capable of dealing with instructions with register
2119 // operands, expand this into two nodes.
2120 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2121 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002122 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002123 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2124 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2125 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2126 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002127 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002128 }
2129}
2130
Dan Gohman475871a2008-07-27 21:46:04 +00002131SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002132 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002133 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002134 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002135 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002136 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002137 MachineFunction &MF = DAG.getMachineFunction();
2138 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2139
Evan Cheng4abce0c2011-05-27 20:11:27 +00002140 // FIXME: Enable this for static codegen when tool issues are fixed.
Evan Chengf31151f2011-10-26 01:17:44 +00002141 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002142 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002143 // FIXME: Once remat is capable of dealing with instructions with register
2144 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002145 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002146 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2147 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2148
Evan Cheng53519f02011-01-21 18:55:51 +00002149 unsigned Wrapper = (RelocM == Reloc::PIC_)
2150 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2151 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002152 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002153 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2154 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002155 MachinePointerInfo::getGOT(),
2156 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002157 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002158 }
2159
2160 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002161 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002162 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002163 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002164 } else {
2165 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002166 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2167 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002168 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2169 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002170 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002171 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002172 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002173
Evan Cheng9eda6892009-10-31 03:39:36 +00002174 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002175 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002176 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002177 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002178
2179 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002180 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002181 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002182 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002183
Evan Cheng63476a82009-09-03 07:04:02 +00002184 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002185 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002186 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002187
2188 return Result;
2189}
2190
Dan Gohman475871a2008-07-27 21:46:04 +00002191SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002192 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002193 assert(Subtarget->isTargetELF() &&
2194 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002195 MachineFunction &MF = DAG.getMachineFunction();
2196 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002197 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002198 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002199 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002200 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002201 ARMConstantPoolValue *CPV =
2202 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2203 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002204 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002205 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002206 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002207 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002208 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002209 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002210 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002211}
2212
Jim Grosbach0e0da732009-05-12 23:59:14 +00002213SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002214ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2215 const {
2216 DebugLoc dl = Op.getDebugLoc();
2217 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
Bill Wendling61512ba2011-05-11 01:11:55 +00002218 Op.getOperand(0), Op.getOperand(1));
Jim Grosbache4ad3872010-10-19 23:27:08 +00002219}
2220
2221SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002222ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2223 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002224 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002225 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2226 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002227 Op.getOperand(1), Val);
2228}
2229
2230SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002231ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2232 DebugLoc dl = Op.getDebugLoc();
2233 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2234 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2235}
2236
2237SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002238ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002239 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002240 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002241 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002242 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002243 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002244 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002245 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002246 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2247 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002248 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002249 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002250 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002251 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002252 EVT PtrVT = getPointerTy();
2253 DebugLoc dl = Op.getDebugLoc();
2254 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2255 SDValue CPAddr;
2256 unsigned PCAdj = (RelocM != Reloc::PIC_)
2257 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002258 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002259 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2260 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002261 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002262 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002263 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002264 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002265 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002266 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002267
2268 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002269 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002270 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2271 }
2272 return Result;
2273 }
Evan Cheng92e39162011-03-29 23:06:19 +00002274 case Intrinsic::arm_neon_vmulls:
2275 case Intrinsic::arm_neon_vmullu: {
2276 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2277 ? ARMISD::VMULLs : ARMISD::VMULLu;
2278 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2279 Op.getOperand(1), Op.getOperand(2));
2280 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002281 }
2282}
2283
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002284static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002285 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002286 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002287 if (!Subtarget->hasDataBarrier()) {
2288 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2289 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2290 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002291 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002292 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002293 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002294 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002295 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002296
2297 SDValue Op5 = Op.getOperand(5);
2298 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2299 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2300 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2301 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2302
2303 ARM_MB::MemBOpt DMBOpt;
2304 if (isDeviceBarrier)
2305 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2306 else
2307 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2308 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2309 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002310}
2311
Eli Friedman26689ac2011-08-03 21:06:02 +00002312
2313static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2314 const ARMSubtarget *Subtarget) {
2315 // FIXME: handle "fence singlethread" more efficiently.
2316 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002317 if (!Subtarget->hasDataBarrier()) {
2318 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2319 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2320 // here.
2321 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2322 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002323 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002324 DAG.getConstant(0, MVT::i32));
2325 }
2326
Eli Friedman26689ac2011-08-03 21:06:02 +00002327 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002328 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002329}
2330
Evan Chengdfed19f2010-11-03 06:34:55 +00002331static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2332 const ARMSubtarget *Subtarget) {
2333 // ARM pre v5TE and Thumb1 does not have preload instructions.
2334 if (!(Subtarget->isThumb2() ||
2335 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2336 // Just preserve the chain.
2337 return Op.getOperand(0);
2338
2339 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002340 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2341 if (!isRead &&
2342 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2343 // ARMv7 with MP extension has PLDW.
2344 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002345
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002346 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2347 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002348 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002349 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002350 isData = ~isData & 1;
2351 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002352
2353 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002354 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2355 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002356}
2357
Dan Gohman1e93df62010-04-17 14:41:14 +00002358static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2359 MachineFunction &MF = DAG.getMachineFunction();
2360 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2361
Evan Chenga8e29892007-01-19 07:51:42 +00002362 // vastart just stores the address of the VarArgsFrameIndex slot into the
2363 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002364 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002365 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002366 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002367 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002368 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2369 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002370}
2371
Dan Gohman475871a2008-07-27 21:46:04 +00002372SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002373ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2374 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002375 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002376 MachineFunction &MF = DAG.getMachineFunction();
2377 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2378
2379 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002380 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002381 RC = ARM::tGPRRegisterClass;
2382 else
2383 RC = ARM::GPRRegisterClass;
2384
2385 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002386 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002387 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002388
2389 SDValue ArgValue2;
2390 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002391 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002392 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002393
2394 // Create load node to retrieve arguments from the stack.
2395 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002396 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002397 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002398 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002399 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002400 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002401 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002402 }
2403
Jim Grosbache5165492009-11-09 00:11:35 +00002404 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002405}
2406
Stuart Hastingsc7315872011-04-20 16:47:52 +00002407void
2408ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2409 unsigned &VARegSize, unsigned &VARegSaveSize)
2410 const {
2411 unsigned NumGPRs;
2412 if (CCInfo.isFirstByValRegValid())
2413 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2414 else {
2415 unsigned int firstUnalloced;
2416 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2417 sizeof(GPRArgRegs) /
2418 sizeof(GPRArgRegs[0]));
2419 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2420 }
2421
2422 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2423 VARegSize = NumGPRs * 4;
2424 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2425}
2426
2427// The remaining GPRs hold either the beginning of variable-argument
2428// data, or the beginning of an aggregate passed by value (usuall
2429// byval). Either way, we allocate stack slots adjacent to the data
2430// provided by our caller, and store the unallocated registers there.
2431// If this is a variadic function, the va_list pointer will begin with
2432// these values; otherwise, this reassembles a (byval) structure that
2433// was split between registers and memory.
2434void
2435ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2436 DebugLoc dl, SDValue &Chain,
2437 unsigned ArgOffset) const {
2438 MachineFunction &MF = DAG.getMachineFunction();
2439 MachineFrameInfo *MFI = MF.getFrameInfo();
2440 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2441 unsigned firstRegToSaveIndex;
2442 if (CCInfo.isFirstByValRegValid())
2443 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2444 else {
2445 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2446 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2447 }
2448
2449 unsigned VARegSize, VARegSaveSize;
2450 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2451 if (VARegSaveSize) {
2452 // If this function is vararg, store any remaining integer argument regs
2453 // to their spots on the stack so that they may be loaded by deferencing
2454 // the result of va_next.
2455 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002456 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2457 ArgOffset + VARegSaveSize
2458 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002459 false));
2460 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2461 getPointerTy());
2462
2463 SmallVector<SDValue, 4> MemOps;
2464 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2465 TargetRegisterClass *RC;
2466 if (AFI->isThumb1OnlyFunction())
2467 RC = ARM::tGPRRegisterClass;
2468 else
2469 RC = ARM::GPRRegisterClass;
2470
2471 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2472 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2473 SDValue Store =
2474 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002475 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002476 false, false, 0);
2477 MemOps.push_back(Store);
2478 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2479 DAG.getConstant(4, getPointerTy()));
2480 }
2481 if (!MemOps.empty())
2482 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2483 &MemOps[0], MemOps.size());
2484 } else
2485 // This will point to the next argument passed via stack.
2486 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2487}
2488
Bob Wilson5bafff32009-06-22 23:27:02 +00002489SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002490ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002491 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002492 const SmallVectorImpl<ISD::InputArg>
2493 &Ins,
2494 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002495 SmallVectorImpl<SDValue> &InVals)
2496 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002497 MachineFunction &MF = DAG.getMachineFunction();
2498 MachineFrameInfo *MFI = MF.getFrameInfo();
2499
Bob Wilson1f595bb2009-04-17 19:07:39 +00002500 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2501
2502 // Assign locations to all of the incoming arguments.
2503 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002504 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2505 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002506 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002507 CCAssignFnForNode(CallConv, /* Return*/ false,
2508 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002509
2510 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002511 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002512
Stuart Hastingsf222e592011-02-28 17:17:53 +00002513 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002514 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2515 CCValAssign &VA = ArgLocs[i];
2516
Bob Wilsondee46d72009-04-17 20:35:10 +00002517 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002518 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002519 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002520
Bob Wilson1f595bb2009-04-17 19:07:39 +00002521 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002522 // f64 and vector types are split up into multiple registers or
2523 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002524 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002525 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002526 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002527 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002528 SDValue ArgValue2;
2529 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002530 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002531 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2532 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002533 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002534 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002535 } else {
2536 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2537 Chain, DAG, dl);
2538 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002539 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2540 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002541 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002542 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002543 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2544 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002545 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002546
Bob Wilson5bafff32009-06-22 23:27:02 +00002547 } else {
2548 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002549
Owen Anderson825b72b2009-08-11 20:47:22 +00002550 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002551 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002552 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002553 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002554 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002555 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002556 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002557 RC = (AFI->isThumb1OnlyFunction() ?
2558 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002559 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002560 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002561
2562 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002563 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002564 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002565 }
2566
2567 // If this is an 8 or 16-bit value, it is really passed promoted
2568 // to 32 bits. Insert an assert[sz]ext to capture this, then
2569 // truncate to the right size.
2570 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002571 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002572 case CCValAssign::Full: break;
2573 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002574 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002575 break;
2576 case CCValAssign::SExt:
2577 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2578 DAG.getValueType(VA.getValVT()));
2579 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2580 break;
2581 case CCValAssign::ZExt:
2582 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2583 DAG.getValueType(VA.getValVT()));
2584 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2585 break;
2586 }
2587
Dan Gohman98ca4f22009-08-05 01:29:28 +00002588 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002589
2590 } else { // VA.isRegLoc()
2591
2592 // sanity check
2593 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002594 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002595
Stuart Hastingsf222e592011-02-28 17:17:53 +00002596 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002597
Stuart Hastingsf222e592011-02-28 17:17:53 +00002598 // Some Ins[] entries become multiple ArgLoc[] entries.
2599 // Process them only once.
2600 if (index != lastInsIndex)
2601 {
2602 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002603 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002604 // This can be changed with more analysis.
2605 // In case of tail call optimization mark all arguments mutable.
2606 // Since they could be overwritten by lowering of arguments in case of
2607 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002608 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002609 unsigned VARegSize, VARegSaveSize;
2610 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2611 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2612 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002613 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002614 int FI = MFI->CreateFixedObject(Bytes,
2615 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002616 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2617 } else {
2618 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2619 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002620
Stuart Hastingsf222e592011-02-28 17:17:53 +00002621 // Create load nodes to retrieve arguments from the stack.
2622 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2623 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2624 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002625 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002626 }
2627 lastInsIndex = index;
2628 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002629 }
2630 }
2631
2632 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002633 if (isVarArg)
2634 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002635
Dan Gohman98ca4f22009-08-05 01:29:28 +00002636 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002637}
2638
2639/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002640static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002641 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002642 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002643 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002644 // Maybe this has already been legalized into the constant pool?
2645 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002646 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002647 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002648 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002649 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002650 }
2651 }
2652 return false;
2653}
2654
Evan Chenga8e29892007-01-19 07:51:42 +00002655/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2656/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002657SDValue
2658ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002659 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002660 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002661 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002662 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002663 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002664 // Constant does not fit, try adjusting it by one?
2665 switch (CC) {
2666 default: break;
2667 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002668 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002669 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002670 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002671 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002672 }
2673 break;
2674 case ISD::SETULT:
2675 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002676 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002677 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002678 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002679 }
2680 break;
2681 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002682 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002683 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002684 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002685 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002686 }
2687 break;
2688 case ISD::SETULE:
2689 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002690 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002691 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002692 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002693 }
2694 break;
2695 }
2696 }
2697 }
2698
2699 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002700 ARMISD::NodeType CompareType;
2701 switch (CondCode) {
2702 default:
2703 CompareType = ARMISD::CMP;
2704 break;
2705 case ARMCC::EQ:
2706 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002707 // Uses only Z Flag
2708 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002709 break;
2710 }
Evan Cheng218977b2010-07-13 19:27:42 +00002711 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002712 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002713}
2714
2715/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002716SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002717ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002718 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002719 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002720 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002721 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002722 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002723 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2724 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002725}
2726
Bob Wilson79f56c92011-03-08 01:17:20 +00002727/// duplicateCmp - Glue values can have only one use, so this function
2728/// duplicates a comparison node.
2729SDValue
2730ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2731 unsigned Opc = Cmp.getOpcode();
2732 DebugLoc DL = Cmp.getDebugLoc();
2733 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2734 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2735
2736 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2737 Cmp = Cmp.getOperand(0);
2738 Opc = Cmp.getOpcode();
2739 if (Opc == ARMISD::CMPFP)
2740 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2741 else {
2742 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2743 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2744 }
2745 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2746}
2747
Bill Wendlingde2b1512010-08-11 08:43:16 +00002748SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2749 SDValue Cond = Op.getOperand(0);
2750 SDValue SelectTrue = Op.getOperand(1);
2751 SDValue SelectFalse = Op.getOperand(2);
2752 DebugLoc dl = Op.getDebugLoc();
2753
2754 // Convert:
2755 //
2756 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2757 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2758 //
2759 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2760 const ConstantSDNode *CMOVTrue =
2761 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2762 const ConstantSDNode *CMOVFalse =
2763 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2764
2765 if (CMOVTrue && CMOVFalse) {
2766 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2767 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2768
2769 SDValue True;
2770 SDValue False;
2771 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2772 True = SelectTrue;
2773 False = SelectFalse;
2774 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2775 True = SelectFalse;
2776 False = SelectTrue;
2777 }
2778
2779 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002780 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002781 SDValue ARMcc = Cond.getOperand(2);
2782 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002783 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002784 assert(True.getValueType() == VT);
2785 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002786 }
2787 }
2788 }
2789
2790 return DAG.getSelectCC(dl, Cond,
2791 DAG.getConstant(0, Cond.getValueType()),
2792 SelectTrue, SelectFalse, ISD::SETNE);
2793}
2794
Dan Gohmand858e902010-04-17 15:26:15 +00002795SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002796 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002797 SDValue LHS = Op.getOperand(0);
2798 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002799 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002800 SDValue TrueVal = Op.getOperand(2);
2801 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002802 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002803
Owen Anderson825b72b2009-08-11 20:47:22 +00002804 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002805 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002806 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002807 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002808 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002809 }
2810
2811 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002812 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002813
Evan Cheng218977b2010-07-13 19:27:42 +00002814 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2815 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002816 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002817 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002818 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002819 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002820 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002821 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002822 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002823 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002824 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002825 }
2826 return Result;
2827}
2828
Evan Cheng218977b2010-07-13 19:27:42 +00002829/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2830/// to morph to an integer compare sequence.
2831static bool canChangeToInt(SDValue Op, bool &SeenZero,
2832 const ARMSubtarget *Subtarget) {
2833 SDNode *N = Op.getNode();
2834 if (!N->hasOneUse())
2835 // Otherwise it requires moving the value from fp to integer registers.
2836 return false;
2837 if (!N->getNumValues())
2838 return false;
2839 EVT VT = Op.getValueType();
2840 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2841 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2842 // vmrs are very slow, e.g. cortex-a8.
2843 return false;
2844
2845 if (isFloatingPointZero(Op)) {
2846 SeenZero = true;
2847 return true;
2848 }
2849 return ISD::isNormalLoad(N);
2850}
2851
2852static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2853 if (isFloatingPointZero(Op))
2854 return DAG.getConstant(0, MVT::i32);
2855
2856 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2857 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002858 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002859 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002860 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002861
2862 llvm_unreachable("Unknown VFP cmp argument!");
2863}
2864
2865static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2866 SDValue &RetVal1, SDValue &RetVal2) {
2867 if (isFloatingPointZero(Op)) {
2868 RetVal1 = DAG.getConstant(0, MVT::i32);
2869 RetVal2 = DAG.getConstant(0, MVT::i32);
2870 return;
2871 }
2872
2873 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2874 SDValue Ptr = Ld->getBasePtr();
2875 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2876 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002877 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002878 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002879 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002880
2881 EVT PtrType = Ptr.getValueType();
2882 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2883 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2884 PtrType, Ptr, DAG.getConstant(4, PtrType));
2885 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2886 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002887 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002888 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002889 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00002890 return;
2891 }
2892
2893 llvm_unreachable("Unknown VFP cmp argument!");
2894}
2895
2896/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2897/// f32 and even f64 comparisons to integer ones.
2898SDValue
2899ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2900 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002901 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002902 SDValue LHS = Op.getOperand(2);
2903 SDValue RHS = Op.getOperand(3);
2904 SDValue Dest = Op.getOperand(4);
2905 DebugLoc dl = Op.getDebugLoc();
2906
2907 bool SeenZero = false;
2908 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2909 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002910 // If one of the operand is zero, it's safe to ignore the NaN case since
2911 // we only care about equality comparisons.
2912 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002913 // If unsafe fp math optimization is enabled and there are no other uses of
2914 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002915 // to an integer comparison.
2916 if (CC == ISD::SETOEQ)
2917 CC = ISD::SETEQ;
2918 else if (CC == ISD::SETUNE)
2919 CC = ISD::SETNE;
2920
2921 SDValue ARMcc;
2922 if (LHS.getValueType() == MVT::f32) {
2923 LHS = bitcastf32Toi32(LHS, DAG);
2924 RHS = bitcastf32Toi32(RHS, DAG);
2925 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2926 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2927 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2928 Chain, Dest, ARMcc, CCR, Cmp);
2929 }
2930
2931 SDValue LHS1, LHS2;
2932 SDValue RHS1, RHS2;
2933 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2934 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2935 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2936 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002937 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002938 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2939 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2940 }
2941
2942 return SDValue();
2943}
2944
2945SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2946 SDValue Chain = Op.getOperand(0);
2947 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2948 SDValue LHS = Op.getOperand(2);
2949 SDValue RHS = Op.getOperand(3);
2950 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002951 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002952
Owen Anderson825b72b2009-08-11 20:47:22 +00002953 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002954 SDValue ARMcc;
2955 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002956 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002957 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002958 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002959 }
2960
Owen Anderson825b72b2009-08-11 20:47:22 +00002961 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002962
2963 if (UnsafeFPMath &&
2964 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2965 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2966 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2967 if (Result.getNode())
2968 return Result;
2969 }
2970
Evan Chenga8e29892007-01-19 07:51:42 +00002971 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002972 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002973
Evan Cheng218977b2010-07-13 19:27:42 +00002974 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2975 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002976 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002977 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002978 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002979 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002980 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002981 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2982 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002983 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002984 }
2985 return Res;
2986}
2987
Dan Gohmand858e902010-04-17 15:26:15 +00002988SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002989 SDValue Chain = Op.getOperand(0);
2990 SDValue Table = Op.getOperand(1);
2991 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002992 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002993
Owen Andersone50ed302009-08-10 22:56:29 +00002994 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002995 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2996 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002997 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002998 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002999 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003000 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3001 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003002 if (Subtarget->isThumb2()) {
3003 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3004 // which does another jump to the destination. This also makes it easier
3005 // to translate it to TBB / TBH later.
3006 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003007 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003008 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003009 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003010 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003011 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003012 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003013 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003014 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003015 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003016 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003017 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003018 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003019 MachinePointerInfo::getJumpTable(),
3020 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003021 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003022 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003023 }
Evan Chenga8e29892007-01-19 07:51:42 +00003024}
3025
Eli Friedman14e809c2011-11-09 23:36:02 +00003026static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
3027 EVT VT = Op.getValueType();
3028 assert(VT.getVectorElementType() == MVT::i32 && "Unexpected custom lowering");
3029
3030 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3031 return Op;
3032 return DAG.UnrollVectorOp(Op.getNode());
3033}
3034
Bob Wilson76a312b2010-03-19 22:51:32 +00003035static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003036 EVT VT = Op.getValueType();
3037 if (VT.isVector())
3038 return LowerVectorFP_TO_INT(Op, DAG);
3039
Bob Wilson76a312b2010-03-19 22:51:32 +00003040 DebugLoc dl = Op.getDebugLoc();
3041 unsigned Opc;
3042
3043 switch (Op.getOpcode()) {
3044 default:
3045 assert(0 && "Invalid opcode!");
3046 case ISD::FP_TO_SINT:
3047 Opc = ARMISD::FTOSI;
3048 break;
3049 case ISD::FP_TO_UINT:
3050 Opc = ARMISD::FTOUI;
3051 break;
3052 }
3053 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003054 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003055}
3056
Cameron Zwarich3007d332011-03-29 21:41:55 +00003057static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3058 EVT VT = Op.getValueType();
3059 DebugLoc dl = Op.getDebugLoc();
3060
Eli Friedman14e809c2011-11-09 23:36:02 +00003061 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3062 if (VT.getVectorElementType() == MVT::f32)
3063 return Op;
3064 return DAG.UnrollVectorOp(Op.getNode());
3065 }
3066
Duncan Sands1f6a3292011-08-12 14:54:45 +00003067 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3068 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003069 if (VT != MVT::v4f32)
3070 return DAG.UnrollVectorOp(Op.getNode());
3071
3072 unsigned CastOpc;
3073 unsigned Opc;
3074 switch (Op.getOpcode()) {
3075 default:
3076 assert(0 && "Invalid opcode!");
3077 case ISD::SINT_TO_FP:
3078 CastOpc = ISD::SIGN_EXTEND;
3079 Opc = ISD::SINT_TO_FP;
3080 break;
3081 case ISD::UINT_TO_FP:
3082 CastOpc = ISD::ZERO_EXTEND;
3083 Opc = ISD::UINT_TO_FP;
3084 break;
3085 }
3086
3087 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3088 return DAG.getNode(Opc, dl, VT, Op);
3089}
3090
Bob Wilson76a312b2010-03-19 22:51:32 +00003091static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3092 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003093 if (VT.isVector())
3094 return LowerVectorINT_TO_FP(Op, DAG);
3095
Bob Wilson76a312b2010-03-19 22:51:32 +00003096 DebugLoc dl = Op.getDebugLoc();
3097 unsigned Opc;
3098
3099 switch (Op.getOpcode()) {
3100 default:
3101 assert(0 && "Invalid opcode!");
3102 case ISD::SINT_TO_FP:
3103 Opc = ARMISD::SITOF;
3104 break;
3105 case ISD::UINT_TO_FP:
3106 Opc = ARMISD::UITOF;
3107 break;
3108 }
3109
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003110 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003111 return DAG.getNode(Opc, dl, VT, Op);
3112}
3113
Evan Cheng515fe3a2010-07-08 02:08:50 +00003114SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003115 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003116 SDValue Tmp0 = Op.getOperand(0);
3117 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003118 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003119 EVT VT = Op.getValueType();
3120 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003121 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3122 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3123 bool UseNEON = !InGPR && Subtarget->hasNEON();
3124
3125 if (UseNEON) {
3126 // Use VBSL to copy the sign bit.
3127 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3128 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3129 DAG.getTargetConstant(EncodedVal, MVT::i32));
3130 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3131 if (VT == MVT::f64)
3132 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3133 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3134 DAG.getConstant(32, MVT::i32));
3135 else /*if (VT == MVT::f32)*/
3136 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3137 if (SrcVT == MVT::f32) {
3138 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3139 if (VT == MVT::f64)
3140 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3141 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3142 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003143 } else if (VT == MVT::f32)
3144 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3145 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3146 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003147 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3148 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3149
3150 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3151 MVT::i32);
3152 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3153 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3154 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003155
Evan Chenge573fb32011-02-23 02:24:55 +00003156 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3157 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3158 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003159 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003160 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3161 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3162 DAG.getConstant(0, MVT::i32));
3163 } else {
3164 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3165 }
3166
3167 return Res;
3168 }
Evan Chengc143dd42011-02-11 02:28:55 +00003169
3170 // Bitcast operand 1 to i32.
3171 if (SrcVT == MVT::f64)
3172 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3173 &Tmp1, 1).getValue(1);
3174 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3175
Evan Chenge573fb32011-02-23 02:24:55 +00003176 // Or in the signbit with integer operations.
3177 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3178 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3179 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3180 if (VT == MVT::f32) {
3181 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3182 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3183 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3184 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003185 }
3186
Evan Chenge573fb32011-02-23 02:24:55 +00003187 // f64: Or the high part with signbit and then combine two parts.
3188 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3189 &Tmp0, 1);
3190 SDValue Lo = Tmp0.getValue(0);
3191 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3192 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3193 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003194}
3195
Evan Cheng2457f2c2010-05-22 01:47:14 +00003196SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3197 MachineFunction &MF = DAG.getMachineFunction();
3198 MachineFrameInfo *MFI = MF.getFrameInfo();
3199 MFI->setReturnAddressIsTaken(true);
3200
3201 EVT VT = Op.getValueType();
3202 DebugLoc dl = Op.getDebugLoc();
3203 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3204 if (Depth) {
3205 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3206 SDValue Offset = DAG.getConstant(4, MVT::i32);
3207 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3208 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003209 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003210 }
3211
3212 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003213 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003214 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3215}
3216
Dan Gohmand858e902010-04-17 15:26:15 +00003217SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003218 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3219 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003220
Owen Andersone50ed302009-08-10 22:56:29 +00003221 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003222 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3223 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003224 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003225 ? ARM::R7 : ARM::R11;
3226 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3227 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003228 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3229 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003230 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003231 return FrameAddr;
3232}
3233
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003234/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003235/// expand a bit convert where either the source or destination type is i64 to
3236/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3237/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3238/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003239static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003240 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3241 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003242 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003243
Bob Wilson9f3f0612010-04-17 05:30:19 +00003244 // This function is only supposed to be called for i64 types, either as the
3245 // source or destination of the bit convert.
3246 EVT SrcVT = Op.getValueType();
3247 EVT DstVT = N->getValueType(0);
3248 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003249 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003250
Bob Wilson9f3f0612010-04-17 05:30:19 +00003251 // Turn i64->f64 into VMOVDRR.
3252 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003253 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3254 DAG.getConstant(0, MVT::i32));
3255 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3256 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003257 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003258 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003259 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003260
Jim Grosbache5165492009-11-09 00:11:35 +00003261 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003262 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3263 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3264 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3265 // Merge the pieces into a single i64 value.
3266 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3267 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003268
Bob Wilson9f3f0612010-04-17 05:30:19 +00003269 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003270}
3271
Bob Wilson5bafff32009-06-22 23:27:02 +00003272/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003273/// Zero vectors are used to represent vector negation and in those cases
3274/// will be implemented with the NEON VNEG instruction. However, VNEG does
3275/// not support i64 elements, so sometimes the zero vectors will need to be
3276/// explicitly constructed. Regardless, use a canonical VMOV to create the
3277/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003278static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003279 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003280 // The canonical modified immediate encoding of a zero vector is....0!
3281 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3282 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3283 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003284 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003285}
3286
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003287/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3288/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003289SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3290 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003291 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3292 EVT VT = Op.getValueType();
3293 unsigned VTBits = VT.getSizeInBits();
3294 DebugLoc dl = Op.getDebugLoc();
3295 SDValue ShOpLo = Op.getOperand(0);
3296 SDValue ShOpHi = Op.getOperand(1);
3297 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003298 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003299 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003300
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003301 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3302
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003303 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3304 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3305 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3306 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3307 DAG.getConstant(VTBits, MVT::i32));
3308 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3309 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003310 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003311
3312 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3313 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003314 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003315 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003316 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003317 CCR, Cmp);
3318
3319 SDValue Ops[2] = { Lo, Hi };
3320 return DAG.getMergeValues(Ops, 2, dl);
3321}
3322
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003323/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3324/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003325SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3326 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003327 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3328 EVT VT = Op.getValueType();
3329 unsigned VTBits = VT.getSizeInBits();
3330 DebugLoc dl = Op.getDebugLoc();
3331 SDValue ShOpLo = Op.getOperand(0);
3332 SDValue ShOpHi = Op.getOperand(1);
3333 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003334 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003335
3336 assert(Op.getOpcode() == ISD::SHL_PARTS);
3337 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3338 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3339 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3340 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3341 DAG.getConstant(VTBits, MVT::i32));
3342 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3343 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3344
3345 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3346 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3347 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003348 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003349 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003350 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003351 CCR, Cmp);
3352
3353 SDValue Ops[2] = { Lo, Hi };
3354 return DAG.getMergeValues(Ops, 2, dl);
3355}
3356
Jim Grosbach4725ca72010-09-08 03:54:02 +00003357SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003358 SelectionDAG &DAG) const {
3359 // The rounding mode is in bits 23:22 of the FPSCR.
3360 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3361 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3362 // so that the shift + and get folded into a bitfield extract.
3363 DebugLoc dl = Op.getDebugLoc();
3364 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3365 DAG.getConstant(Intrinsic::arm_get_fpscr,
3366 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003367 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003368 DAG.getConstant(1U << 22, MVT::i32));
3369 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3370 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003371 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003372 DAG.getConstant(3, MVT::i32));
3373}
3374
Jim Grosbach3482c802010-01-18 19:58:49 +00003375static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3376 const ARMSubtarget *ST) {
3377 EVT VT = N->getValueType(0);
3378 DebugLoc dl = N->getDebugLoc();
3379
3380 if (!ST->hasV6T2Ops())
3381 return SDValue();
3382
3383 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3384 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3385}
3386
Bob Wilson5bafff32009-06-22 23:27:02 +00003387static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3388 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003389 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003390 DebugLoc dl = N->getDebugLoc();
3391
Bob Wilsond5448bb2010-11-18 21:16:28 +00003392 if (!VT.isVector())
3393 return SDValue();
3394
Bob Wilson5bafff32009-06-22 23:27:02 +00003395 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003396 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003397
Bob Wilsond5448bb2010-11-18 21:16:28 +00003398 // Left shifts translate directly to the vshiftu intrinsic.
3399 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003400 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003401 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3402 N->getOperand(0), N->getOperand(1));
3403
3404 assert((N->getOpcode() == ISD::SRA ||
3405 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3406
3407 // NEON uses the same intrinsics for both left and right shifts. For
3408 // right shifts, the shift amounts are negative, so negate the vector of
3409 // shift amounts.
3410 EVT ShiftVT = N->getOperand(1).getValueType();
3411 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3412 getZeroVector(ShiftVT, DAG, dl),
3413 N->getOperand(1));
3414 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3415 Intrinsic::arm_neon_vshifts :
3416 Intrinsic::arm_neon_vshiftu);
3417 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3418 DAG.getConstant(vshiftInt, MVT::i32),
3419 N->getOperand(0), NegatedCount);
3420}
3421
3422static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3423 const ARMSubtarget *ST) {
3424 EVT VT = N->getValueType(0);
3425 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003426
Eli Friedmance392eb2009-08-22 03:13:10 +00003427 // We can get here for a node like i32 = ISD::SHL i32, i64
3428 if (VT != MVT::i64)
3429 return SDValue();
3430
3431 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003432 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003433
Chris Lattner27a6c732007-11-24 07:07:01 +00003434 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3435 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003436 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003437 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003438
Chris Lattner27a6c732007-11-24 07:07:01 +00003439 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003440 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003441
Chris Lattner27a6c732007-11-24 07:07:01 +00003442 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003443 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003444 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003445 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003446 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003447
Chris Lattner27a6c732007-11-24 07:07:01 +00003448 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3449 // captures the result into a carry flag.
3450 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003451 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003452
Chris Lattner27a6c732007-11-24 07:07:01 +00003453 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003454 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003455
Chris Lattner27a6c732007-11-24 07:07:01 +00003456 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003457 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003458}
3459
Bob Wilson5bafff32009-06-22 23:27:02 +00003460static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3461 SDValue TmpOp0, TmpOp1;
3462 bool Invert = false;
3463 bool Swap = false;
3464 unsigned Opc = 0;
3465
3466 SDValue Op0 = Op.getOperand(0);
3467 SDValue Op1 = Op.getOperand(1);
3468 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003469 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003470 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3471 DebugLoc dl = Op.getDebugLoc();
3472
3473 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3474 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003475 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003476 case ISD::SETUNE:
3477 case ISD::SETNE: Invert = true; // Fallthrough
3478 case ISD::SETOEQ:
3479 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3480 case ISD::SETOLT:
3481 case ISD::SETLT: Swap = true; // Fallthrough
3482 case ISD::SETOGT:
3483 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3484 case ISD::SETOLE:
3485 case ISD::SETLE: Swap = true; // Fallthrough
3486 case ISD::SETOGE:
3487 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3488 case ISD::SETUGE: Swap = true; // Fallthrough
3489 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3490 case ISD::SETUGT: Swap = true; // Fallthrough
3491 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3492 case ISD::SETUEQ: Invert = true; // Fallthrough
3493 case ISD::SETONE:
3494 // Expand this to (OLT | OGT).
3495 TmpOp0 = Op0;
3496 TmpOp1 = Op1;
3497 Opc = ISD::OR;
3498 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3499 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3500 break;
3501 case ISD::SETUO: Invert = true; // Fallthrough
3502 case ISD::SETO:
3503 // Expand this to (OLT | OGE).
3504 TmpOp0 = Op0;
3505 TmpOp1 = Op1;
3506 Opc = ISD::OR;
3507 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3508 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3509 break;
3510 }
3511 } else {
3512 // Integer comparisons.
3513 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003514 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003515 case ISD::SETNE: Invert = true;
3516 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3517 case ISD::SETLT: Swap = true;
3518 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3519 case ISD::SETLE: Swap = true;
3520 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3521 case ISD::SETULT: Swap = true;
3522 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3523 case ISD::SETULE: Swap = true;
3524 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3525 }
3526
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003527 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003528 if (Opc == ARMISD::VCEQ) {
3529
3530 SDValue AndOp;
3531 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3532 AndOp = Op0;
3533 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3534 AndOp = Op1;
3535
3536 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003537 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003538 AndOp = AndOp.getOperand(0);
3539
3540 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3541 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003542 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3543 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003544 Invert = !Invert;
3545 }
3546 }
3547 }
3548
3549 if (Swap)
3550 std::swap(Op0, Op1);
3551
Owen Andersonc24cb352010-11-08 23:21:22 +00003552 // If one of the operands is a constant vector zero, attempt to fold the
3553 // comparison to a specialized compare-against-zero form.
3554 SDValue SingleOp;
3555 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3556 SingleOp = Op0;
3557 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3558 if (Opc == ARMISD::VCGE)
3559 Opc = ARMISD::VCLEZ;
3560 else if (Opc == ARMISD::VCGT)
3561 Opc = ARMISD::VCLTZ;
3562 SingleOp = Op1;
3563 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003564
Owen Andersonc24cb352010-11-08 23:21:22 +00003565 SDValue Result;
3566 if (SingleOp.getNode()) {
3567 switch (Opc) {
3568 case ARMISD::VCEQ:
3569 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3570 case ARMISD::VCGE:
3571 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3572 case ARMISD::VCLEZ:
3573 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3574 case ARMISD::VCGT:
3575 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3576 case ARMISD::VCLTZ:
3577 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3578 default:
3579 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3580 }
3581 } else {
3582 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3583 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003584
3585 if (Invert)
3586 Result = DAG.getNOT(dl, Result, VT);
3587
3588 return Result;
3589}
3590
Bob Wilsond3c42842010-06-14 22:19:57 +00003591/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3592/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003593/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003594static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3595 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003596 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003597 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003598
Bob Wilson827b2102010-06-15 19:05:35 +00003599 // SplatBitSize is set to the smallest size that splats the vector, so a
3600 // zero vector will always have SplatBitSize == 8. However, NEON modified
3601 // immediate instructions others than VMOV do not support the 8-bit encoding
3602 // of a zero vector, and the default encoding of zero is supposed to be the
3603 // 32-bit version.
3604 if (SplatBits == 0)
3605 SplatBitSize = 32;
3606
Bob Wilson5bafff32009-06-22 23:27:02 +00003607 switch (SplatBitSize) {
3608 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003609 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003610 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003611 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003612 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003613 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003614 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003615 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003616 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003617
3618 case 16:
3619 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003620 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003621 if ((SplatBits & ~0xff) == 0) {
3622 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003623 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003624 Imm = SplatBits;
3625 break;
3626 }
3627 if ((SplatBits & ~0xff00) == 0) {
3628 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003629 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003630 Imm = SplatBits >> 8;
3631 break;
3632 }
3633 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003634
3635 case 32:
3636 // NEON's 32-bit VMOV supports splat values where:
3637 // * only one byte is nonzero, or
3638 // * the least significant byte is 0xff and the second byte is nonzero, or
3639 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003640 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003641 if ((SplatBits & ~0xff) == 0) {
3642 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003643 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003644 Imm = SplatBits;
3645 break;
3646 }
3647 if ((SplatBits & ~0xff00) == 0) {
3648 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003649 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003650 Imm = SplatBits >> 8;
3651 break;
3652 }
3653 if ((SplatBits & ~0xff0000) == 0) {
3654 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003655 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003656 Imm = SplatBits >> 16;
3657 break;
3658 }
3659 if ((SplatBits & ~0xff000000) == 0) {
3660 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003661 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003662 Imm = SplatBits >> 24;
3663 break;
3664 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003665
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003666 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3667 if (type == OtherModImm) return SDValue();
3668
Bob Wilson5bafff32009-06-22 23:27:02 +00003669 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003670 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3671 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003672 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003673 Imm = SplatBits >> 8;
3674 SplatBits |= 0xff;
3675 break;
3676 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003677
3678 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003679 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3680 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003681 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003682 Imm = SplatBits >> 16;
3683 SplatBits |= 0xffff;
3684 break;
3685 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003686
3687 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3688 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3689 // VMOV.I32. A (very) minor optimization would be to replicate the value
3690 // and fall through here to test for a valid 64-bit splat. But, then the
3691 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003692 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003693
3694 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003695 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003696 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003697 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003698 uint64_t BitMask = 0xff;
3699 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003700 unsigned ImmMask = 1;
3701 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003702 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003703 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003704 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003705 Imm |= ImmMask;
3706 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003707 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003708 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003709 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003710 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003711 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003712 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003713 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003714 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003715 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003716 break;
3717 }
3718
Bob Wilson1a913ed2010-06-11 21:34:50 +00003719 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003720 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003721 return SDValue();
3722 }
3723
Bob Wilsoncba270d2010-07-13 21:16:48 +00003724 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3725 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003726}
3727
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003728static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3729 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003730 unsigned NumElts = VT.getVectorNumElements();
3731 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003732
3733 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3734 if (M[0] < 0)
3735 return false;
3736
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003737 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003738
3739 // If this is a VEXT shuffle, the immediate value is the index of the first
3740 // element. The other shuffle indices must be the successive elements after
3741 // the first one.
3742 unsigned ExpectedElt = Imm;
3743 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003744 // Increment the expected index. If it wraps around, it may still be
3745 // a VEXT but the source vectors must be swapped.
3746 ExpectedElt += 1;
3747 if (ExpectedElt == NumElts * 2) {
3748 ExpectedElt = 0;
3749 ReverseVEXT = true;
3750 }
3751
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003752 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003753 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003754 return false;
3755 }
3756
3757 // Adjust the index value if the source operands will be swapped.
3758 if (ReverseVEXT)
3759 Imm -= NumElts;
3760
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003761 return true;
3762}
3763
Bob Wilson8bb9e482009-07-26 00:39:34 +00003764/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3765/// instruction with the specified blocksize. (The order of the elements
3766/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003767static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3768 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003769 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3770 "Only possible block sizes for VREV are: 16, 32, 64");
3771
Bob Wilson8bb9e482009-07-26 00:39:34 +00003772 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003773 if (EltSz == 64)
3774 return false;
3775
3776 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003777 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003778 // If the first shuffle index is UNDEF, be optimistic.
3779 if (M[0] < 0)
3780 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003781
3782 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3783 return false;
3784
3785 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003786 if (M[i] < 0) continue; // ignore UNDEF indices
3787 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003788 return false;
3789 }
3790
3791 return true;
3792}
3793
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003794static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3795 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3796 // range, then 0 is placed into the resulting vector. So pretty much any mask
3797 // of 8 elements can work here.
3798 return VT == MVT::v8i8 && M.size() == 8;
3799}
3800
Bob Wilsonc692cb72009-08-21 20:54:19 +00003801static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3802 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003803 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3804 if (EltSz == 64)
3805 return false;
3806
Bob Wilsonc692cb72009-08-21 20:54:19 +00003807 unsigned NumElts = VT.getVectorNumElements();
3808 WhichResult = (M[0] == 0 ? 0 : 1);
3809 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003810 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3811 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003812 return false;
3813 }
3814 return true;
3815}
3816
Bob Wilson324f4f12009-12-03 06:40:55 +00003817/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3818/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3819/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3820static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3821 unsigned &WhichResult) {
3822 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3823 if (EltSz == 64)
3824 return false;
3825
3826 unsigned NumElts = VT.getVectorNumElements();
3827 WhichResult = (M[0] == 0 ? 0 : 1);
3828 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003829 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3830 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003831 return false;
3832 }
3833 return true;
3834}
3835
Bob Wilsonc692cb72009-08-21 20:54:19 +00003836static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3837 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003838 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3839 if (EltSz == 64)
3840 return false;
3841
Bob Wilsonc692cb72009-08-21 20:54:19 +00003842 unsigned NumElts = VT.getVectorNumElements();
3843 WhichResult = (M[0] == 0 ? 0 : 1);
3844 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003845 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003846 if ((unsigned) M[i] != 2 * i + WhichResult)
3847 return false;
3848 }
3849
3850 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003851 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003852 return false;
3853
3854 return true;
3855}
3856
Bob Wilson324f4f12009-12-03 06:40:55 +00003857/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3858/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3859/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3860static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3861 unsigned &WhichResult) {
3862 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3863 if (EltSz == 64)
3864 return false;
3865
3866 unsigned Half = VT.getVectorNumElements() / 2;
3867 WhichResult = (M[0] == 0 ? 0 : 1);
3868 for (unsigned j = 0; j != 2; ++j) {
3869 unsigned Idx = WhichResult;
3870 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003871 int MIdx = M[i + j * Half];
3872 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003873 return false;
3874 Idx += 2;
3875 }
3876 }
3877
3878 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3879 if (VT.is64BitVector() && EltSz == 32)
3880 return false;
3881
3882 return true;
3883}
3884
Bob Wilsonc692cb72009-08-21 20:54:19 +00003885static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3886 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003887 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3888 if (EltSz == 64)
3889 return false;
3890
Bob Wilsonc692cb72009-08-21 20:54:19 +00003891 unsigned NumElts = VT.getVectorNumElements();
3892 WhichResult = (M[0] == 0 ? 0 : 1);
3893 unsigned Idx = WhichResult * NumElts / 2;
3894 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003895 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3896 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003897 return false;
3898 Idx += 1;
3899 }
3900
3901 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003902 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003903 return false;
3904
3905 return true;
3906}
3907
Bob Wilson324f4f12009-12-03 06:40:55 +00003908/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3909/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3910/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3911static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3912 unsigned &WhichResult) {
3913 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3914 if (EltSz == 64)
3915 return false;
3916
3917 unsigned NumElts = VT.getVectorNumElements();
3918 WhichResult = (M[0] == 0 ? 0 : 1);
3919 unsigned Idx = WhichResult * NumElts / 2;
3920 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003921 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3922 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003923 return false;
3924 Idx += 1;
3925 }
3926
3927 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3928 if (VT.is64BitVector() && EltSz == 32)
3929 return false;
3930
3931 return true;
3932}
3933
Dale Johannesenf630c712010-07-29 20:10:08 +00003934// If N is an integer constant that can be moved into a register in one
3935// instruction, return an SDValue of such a constant (will become a MOV
3936// instruction). Otherwise return null.
3937static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3938 const ARMSubtarget *ST, DebugLoc dl) {
3939 uint64_t Val;
3940 if (!isa<ConstantSDNode>(N))
3941 return SDValue();
3942 Val = cast<ConstantSDNode>(N)->getZExtValue();
3943
3944 if (ST->isThumb1Only()) {
3945 if (Val <= 255 || ~Val <= 255)
3946 return DAG.getConstant(Val, MVT::i32);
3947 } else {
3948 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3949 return DAG.getConstant(Val, MVT::i32);
3950 }
3951 return SDValue();
3952}
3953
Bob Wilson5bafff32009-06-22 23:27:02 +00003954// If this is a case we can't handle, return null and let the default
3955// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003956SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3957 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003958 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003959 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003960 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003961
3962 APInt SplatBits, SplatUndef;
3963 unsigned SplatBitSize;
3964 bool HasAnyUndefs;
3965 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003966 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003967 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003968 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003969 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003970 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003971 DAG, VmovVT, VT.is128BitVector(),
3972 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003973 if (Val.getNode()) {
3974 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003975 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003976 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003977
3978 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00003979 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003980 Val = isNEONModifiedImm(NegatedImm,
3981 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003982 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003983 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003984 if (Val.getNode()) {
3985 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003986 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003987 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003988 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003989 }
3990
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003991 // Scan through the operands to see if only one value is used.
3992 unsigned NumElts = VT.getVectorNumElements();
3993 bool isOnlyLowElement = true;
3994 bool usesOnlyOneValue = true;
3995 bool isConstant = true;
3996 SDValue Value;
3997 for (unsigned i = 0; i < NumElts; ++i) {
3998 SDValue V = Op.getOperand(i);
3999 if (V.getOpcode() == ISD::UNDEF)
4000 continue;
4001 if (i > 0)
4002 isOnlyLowElement = false;
4003 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4004 isConstant = false;
4005
4006 if (!Value.getNode())
4007 Value = V;
4008 else if (V != Value)
4009 usesOnlyOneValue = false;
4010 }
4011
4012 if (!Value.getNode())
4013 return DAG.getUNDEF(VT);
4014
4015 if (isOnlyLowElement)
4016 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4017
Dale Johannesenf630c712010-07-29 20:10:08 +00004018 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4019
Dale Johannesen575cd142010-10-19 20:00:17 +00004020 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4021 // i32 and try again.
4022 if (usesOnlyOneValue && EltSize <= 32) {
4023 if (!isConstant)
4024 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4025 if (VT.getVectorElementType().isFloatingPoint()) {
4026 SmallVector<SDValue, 8> Ops;
4027 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004028 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004029 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004030 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4031 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004032 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4033 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004034 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004035 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004036 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4037 if (Val.getNode())
4038 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004039 }
4040
4041 // If all elements are constants and the case above didn't get hit, fall back
4042 // to the default expansion, which will generate a load from the constant
4043 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004044 if (isConstant)
4045 return SDValue();
4046
Bob Wilson11a1dff2011-01-07 21:37:30 +00004047 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4048 if (NumElts >= 4) {
4049 SDValue shuffle = ReconstructShuffle(Op, DAG);
4050 if (shuffle != SDValue())
4051 return shuffle;
4052 }
4053
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004054 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004055 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4056 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004057 if (EltSize >= 32) {
4058 // Do the expansion with floating-point types, since that is what the VFP
4059 // registers are defined to use, and since i64 is not legal.
4060 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4061 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004062 SmallVector<SDValue, 8> Ops;
4063 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004064 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004065 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004066 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004067 }
4068
4069 return SDValue();
4070}
4071
Bob Wilson11a1dff2011-01-07 21:37:30 +00004072// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004073// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004074SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4075 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004076 DebugLoc dl = Op.getDebugLoc();
4077 EVT VT = Op.getValueType();
4078 unsigned NumElts = VT.getVectorNumElements();
4079
4080 SmallVector<SDValue, 2> SourceVecs;
4081 SmallVector<unsigned, 2> MinElts;
4082 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004083
Bob Wilson11a1dff2011-01-07 21:37:30 +00004084 for (unsigned i = 0; i < NumElts; ++i) {
4085 SDValue V = Op.getOperand(i);
4086 if (V.getOpcode() == ISD::UNDEF)
4087 continue;
4088 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4089 // A shuffle can only come from building a vector from various
4090 // elements of other vectors.
4091 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004092 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4093 VT.getVectorElementType()) {
4094 // This code doesn't know how to handle shuffles where the vector
4095 // element types do not match (this happens because type legalization
4096 // promotes the return type of EXTRACT_VECTOR_ELT).
4097 // FIXME: It might be appropriate to extend this code to handle
4098 // mismatched types.
4099 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004100 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004101
Bob Wilson11a1dff2011-01-07 21:37:30 +00004102 // Record this extraction against the appropriate vector if possible...
4103 SDValue SourceVec = V.getOperand(0);
4104 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4105 bool FoundSource = false;
4106 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4107 if (SourceVecs[j] == SourceVec) {
4108 if (MinElts[j] > EltNo)
4109 MinElts[j] = EltNo;
4110 if (MaxElts[j] < EltNo)
4111 MaxElts[j] = EltNo;
4112 FoundSource = true;
4113 break;
4114 }
4115 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004116
Bob Wilson11a1dff2011-01-07 21:37:30 +00004117 // Or record a new source if not...
4118 if (!FoundSource) {
4119 SourceVecs.push_back(SourceVec);
4120 MinElts.push_back(EltNo);
4121 MaxElts.push_back(EltNo);
4122 }
4123 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004124
Bob Wilson11a1dff2011-01-07 21:37:30 +00004125 // Currently only do something sane when at most two source vectors
4126 // involved.
4127 if (SourceVecs.size() > 2)
4128 return SDValue();
4129
4130 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4131 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004132
Bob Wilson11a1dff2011-01-07 21:37:30 +00004133 // This loop extracts the usage patterns of the source vectors
4134 // and prepares appropriate SDValues for a shuffle if possible.
4135 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4136 if (SourceVecs[i].getValueType() == VT) {
4137 // No VEXT necessary
4138 ShuffleSrcs[i] = SourceVecs[i];
4139 VEXTOffsets[i] = 0;
4140 continue;
4141 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4142 // It probably isn't worth padding out a smaller vector just to
4143 // break it down again in a shuffle.
4144 return SDValue();
4145 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004146
Bob Wilson11a1dff2011-01-07 21:37:30 +00004147 // Since only 64-bit and 128-bit vectors are legal on ARM and
4148 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004149 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4150 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004151
Bob Wilson11a1dff2011-01-07 21:37:30 +00004152 if (MaxElts[i] - MinElts[i] >= NumElts) {
4153 // Span too large for a VEXT to cope
4154 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004155 }
4156
Bob Wilson11a1dff2011-01-07 21:37:30 +00004157 if (MinElts[i] >= NumElts) {
4158 // The extraction can just take the second half
4159 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004160 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4161 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004162 DAG.getIntPtrConstant(NumElts));
4163 } else if (MaxElts[i] < NumElts) {
4164 // The extraction can just take the first half
4165 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004166 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4167 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004168 DAG.getIntPtrConstant(0));
4169 } else {
4170 // An actual VEXT is needed
4171 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004172 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4173 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004174 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004175 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4176 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004177 DAG.getIntPtrConstant(NumElts));
4178 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4179 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4180 }
4181 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004182
Bob Wilson11a1dff2011-01-07 21:37:30 +00004183 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004184
Bob Wilson11a1dff2011-01-07 21:37:30 +00004185 for (unsigned i = 0; i < NumElts; ++i) {
4186 SDValue Entry = Op.getOperand(i);
4187 if (Entry.getOpcode() == ISD::UNDEF) {
4188 Mask.push_back(-1);
4189 continue;
4190 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004191
Bob Wilson11a1dff2011-01-07 21:37:30 +00004192 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004193 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4194 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004195 if (ExtractVec == SourceVecs[0]) {
4196 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4197 } else {
4198 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4199 }
4200 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004201
Bob Wilson11a1dff2011-01-07 21:37:30 +00004202 // Final check before we try to produce nonsense...
4203 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004204 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4205 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004206
Bob Wilson11a1dff2011-01-07 21:37:30 +00004207 return SDValue();
4208}
4209
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004210/// isShuffleMaskLegal - Targets can use this to indicate that they only
4211/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4212/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4213/// are assumed to be legal.
4214bool
4215ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4216 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004217 if (VT.getVectorNumElements() == 4 &&
4218 (VT.is128BitVector() || VT.is64BitVector())) {
4219 unsigned PFIndexes[4];
4220 for (unsigned i = 0; i != 4; ++i) {
4221 if (M[i] < 0)
4222 PFIndexes[i] = 8;
4223 else
4224 PFIndexes[i] = M[i];
4225 }
4226
4227 // Compute the index in the perfect shuffle table.
4228 unsigned PFTableIndex =
4229 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4230 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4231 unsigned Cost = (PFEntry >> 30);
4232
4233 if (Cost <= 4)
4234 return true;
4235 }
4236
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004237 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004238 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004239
Bob Wilson53dd2452010-06-07 23:53:38 +00004240 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4241 return (EltSize >= 32 ||
4242 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004243 isVREVMask(M, VT, 64) ||
4244 isVREVMask(M, VT, 32) ||
4245 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004246 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004247 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004248 isVTRNMask(M, VT, WhichResult) ||
4249 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004250 isVZIPMask(M, VT, WhichResult) ||
4251 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4252 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4253 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004254}
4255
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004256/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4257/// the specified operations to build the shuffle.
4258static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4259 SDValue RHS, SelectionDAG &DAG,
4260 DebugLoc dl) {
4261 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4262 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4263 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4264
4265 enum {
4266 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4267 OP_VREV,
4268 OP_VDUP0,
4269 OP_VDUP1,
4270 OP_VDUP2,
4271 OP_VDUP3,
4272 OP_VEXT1,
4273 OP_VEXT2,
4274 OP_VEXT3,
4275 OP_VUZPL, // VUZP, left result
4276 OP_VUZPR, // VUZP, right result
4277 OP_VZIPL, // VZIP, left result
4278 OP_VZIPR, // VZIP, right result
4279 OP_VTRNL, // VTRN, left result
4280 OP_VTRNR // VTRN, right result
4281 };
4282
4283 if (OpNum == OP_COPY) {
4284 if (LHSID == (1*9+2)*9+3) return LHS;
4285 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4286 return RHS;
4287 }
4288
4289 SDValue OpLHS, OpRHS;
4290 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4291 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4292 EVT VT = OpLHS.getValueType();
4293
4294 switch (OpNum) {
4295 default: llvm_unreachable("Unknown shuffle opcode!");
4296 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004297 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004298 if (VT.getVectorElementType() == MVT::i32 ||
4299 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004300 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4301 // vrev <4 x i16> -> VREV32
4302 if (VT.getVectorElementType() == MVT::i16)
4303 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4304 // vrev <4 x i8> -> VREV16
4305 assert(VT.getVectorElementType() == MVT::i8);
4306 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004307 case OP_VDUP0:
4308 case OP_VDUP1:
4309 case OP_VDUP2:
4310 case OP_VDUP3:
4311 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004312 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004313 case OP_VEXT1:
4314 case OP_VEXT2:
4315 case OP_VEXT3:
4316 return DAG.getNode(ARMISD::VEXT, dl, VT,
4317 OpLHS, OpRHS,
4318 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4319 case OP_VUZPL:
4320 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004321 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004322 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4323 case OP_VZIPL:
4324 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004325 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004326 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4327 case OP_VTRNL:
4328 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004329 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4330 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004331 }
4332}
4333
Bill Wendling69a05a72011-03-14 23:02:38 +00004334static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4335 SmallVectorImpl<int> &ShuffleMask,
4336 SelectionDAG &DAG) {
4337 // Check to see if we can use the VTBL instruction.
4338 SDValue V1 = Op.getOperand(0);
4339 SDValue V2 = Op.getOperand(1);
4340 DebugLoc DL = Op.getDebugLoc();
4341
4342 SmallVector<SDValue, 8> VTBLMask;
4343 for (SmallVectorImpl<int>::iterator
4344 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4345 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4346
4347 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4348 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4349 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4350 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004351
Owen Anderson76706012011-04-05 21:48:57 +00004352 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004353 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4354 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004355}
4356
Bob Wilson5bafff32009-06-22 23:27:02 +00004357static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004358 SDValue V1 = Op.getOperand(0);
4359 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004360 DebugLoc dl = Op.getDebugLoc();
4361 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004362 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004363 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004364
Bob Wilson28865062009-08-13 02:13:04 +00004365 // Convert shuffles that are directly supported on NEON to target-specific
4366 // DAG nodes, instead of keeping them as shuffles and matching them again
4367 // during code selection. This is more efficient and avoids the possibility
4368 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004369 // FIXME: floating-point vectors should be canonicalized to integer vectors
4370 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004371 SVN->getMask(ShuffleMask);
4372
Bob Wilson53dd2452010-06-07 23:53:38 +00004373 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4374 if (EltSize <= 32) {
4375 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4376 int Lane = SVN->getSplatIndex();
4377 // If this is undef splat, generate it via "just" vdup, if possible.
4378 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004379
Dan Gohman65fd6562011-11-03 21:49:52 +00004380 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004381 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4382 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4383 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004384 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4385 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4386 // reaches it).
4387 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4388 !isa<ConstantSDNode>(V1.getOperand(0))) {
4389 bool IsScalarToVector = true;
4390 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4391 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4392 IsScalarToVector = false;
4393 break;
4394 }
4395 if (IsScalarToVector)
4396 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4397 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004398 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4399 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004400 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004401
4402 bool ReverseVEXT;
4403 unsigned Imm;
4404 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4405 if (ReverseVEXT)
4406 std::swap(V1, V2);
4407 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4408 DAG.getConstant(Imm, MVT::i32));
4409 }
4410
4411 if (isVREVMask(ShuffleMask, VT, 64))
4412 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4413 if (isVREVMask(ShuffleMask, VT, 32))
4414 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4415 if (isVREVMask(ShuffleMask, VT, 16))
4416 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4417
4418 // Check for Neon shuffles that modify both input vectors in place.
4419 // If both results are used, i.e., if there are two shuffles with the same
4420 // source operands and with masks corresponding to both results of one of
4421 // these operations, DAG memoization will ensure that a single node is
4422 // used for both shuffles.
4423 unsigned WhichResult;
4424 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4425 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4426 V1, V2).getValue(WhichResult);
4427 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4428 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4429 V1, V2).getValue(WhichResult);
4430 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4431 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4432 V1, V2).getValue(WhichResult);
4433
4434 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4435 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4436 V1, V1).getValue(WhichResult);
4437 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4438 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4439 V1, V1).getValue(WhichResult);
4440 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4441 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4442 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004443 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004444
Bob Wilsonc692cb72009-08-21 20:54:19 +00004445 // If the shuffle is not directly supported and it has 4 elements, use
4446 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004447 unsigned NumElts = VT.getVectorNumElements();
4448 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004449 unsigned PFIndexes[4];
4450 for (unsigned i = 0; i != 4; ++i) {
4451 if (ShuffleMask[i] < 0)
4452 PFIndexes[i] = 8;
4453 else
4454 PFIndexes[i] = ShuffleMask[i];
4455 }
4456
4457 // Compute the index in the perfect shuffle table.
4458 unsigned PFTableIndex =
4459 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004460 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4461 unsigned Cost = (PFEntry >> 30);
4462
4463 if (Cost <= 4)
4464 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4465 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004466
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004467 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004468 if (EltSize >= 32) {
4469 // Do the expansion with floating-point types, since that is what the VFP
4470 // registers are defined to use, and since i64 is not legal.
4471 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4472 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004473 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4474 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004475 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004476 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004477 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004478 Ops.push_back(DAG.getUNDEF(EltVT));
4479 else
4480 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4481 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4482 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4483 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004484 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004485 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004486 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004487 }
4488
Bill Wendling69a05a72011-03-14 23:02:38 +00004489 if (VT == MVT::v8i8) {
4490 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4491 if (NewOp.getNode())
4492 return NewOp;
4493 }
4494
Bob Wilson22cac0d2009-08-14 05:16:33 +00004495 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004496}
4497
Eli Friedman5c89cb82011-10-24 23:08:52 +00004498static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4499 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4500 SDValue Lane = Op.getOperand(2);
4501 if (!isa<ConstantSDNode>(Lane))
4502 return SDValue();
4503
4504 return Op;
4505}
4506
Bob Wilson5bafff32009-06-22 23:27:02 +00004507static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004508 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004509 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004510 if (!isa<ConstantSDNode>(Lane))
4511 return SDValue();
4512
4513 SDValue Vec = Op.getOperand(0);
4514 if (Op.getValueType() == MVT::i32 &&
4515 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4516 DebugLoc dl = Op.getDebugLoc();
4517 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4518 }
4519
4520 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004521}
4522
Bob Wilsona6d65862009-08-03 20:36:38 +00004523static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4524 // The only time a CONCAT_VECTORS operation can have legal types is when
4525 // two 64-bit vectors are concatenated to a 128-bit vector.
4526 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4527 "unexpected CONCAT_VECTORS");
4528 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004529 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004530 SDValue Op0 = Op.getOperand(0);
4531 SDValue Op1 = Op.getOperand(1);
4532 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004533 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004534 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004535 DAG.getIntPtrConstant(0));
4536 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004537 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004538 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004539 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004540 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004541}
4542
Bob Wilson626613d2010-11-23 19:38:38 +00004543/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4544/// element has been zero/sign-extended, depending on the isSigned parameter,
4545/// from an integer type half its size.
4546static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4547 bool isSigned) {
4548 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4549 EVT VT = N->getValueType(0);
4550 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4551 SDNode *BVN = N->getOperand(0).getNode();
4552 if (BVN->getValueType(0) != MVT::v4i32 ||
4553 BVN->getOpcode() != ISD::BUILD_VECTOR)
4554 return false;
4555 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4556 unsigned HiElt = 1 - LoElt;
4557 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4558 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4559 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4560 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4561 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4562 return false;
4563 if (isSigned) {
4564 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4565 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4566 return true;
4567 } else {
4568 if (Hi0->isNullValue() && Hi1->isNullValue())
4569 return true;
4570 }
4571 return false;
4572 }
4573
4574 if (N->getOpcode() != ISD::BUILD_VECTOR)
4575 return false;
4576
4577 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4578 SDNode *Elt = N->getOperand(i).getNode();
4579 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4580 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4581 unsigned HalfSize = EltSize / 2;
4582 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004583 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004584 return false;
4585 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004586 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004587 return false;
4588 }
4589 continue;
4590 }
4591 return false;
4592 }
4593
4594 return true;
4595}
4596
4597/// isSignExtended - Check if a node is a vector value that is sign-extended
4598/// or a constant BUILD_VECTOR with sign-extended elements.
4599static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4600 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4601 return true;
4602 if (isExtendedBUILD_VECTOR(N, DAG, true))
4603 return true;
4604 return false;
4605}
4606
4607/// isZeroExtended - Check if a node is a vector value that is zero-extended
4608/// or a constant BUILD_VECTOR with zero-extended elements.
4609static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4610 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4611 return true;
4612 if (isExtendedBUILD_VECTOR(N, DAG, false))
4613 return true;
4614 return false;
4615}
4616
4617/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4618/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004619static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4620 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4621 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004622 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4623 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4624 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004625 LD->isNonTemporal(), LD->isInvariant(),
4626 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004627 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4628 // have been legalized as a BITCAST from v4i32.
4629 if (N->getOpcode() == ISD::BITCAST) {
4630 SDNode *BVN = N->getOperand(0).getNode();
4631 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4632 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4633 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4634 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4635 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4636 }
4637 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4638 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4639 EVT VT = N->getValueType(0);
4640 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4641 unsigned NumElts = VT.getVectorNumElements();
4642 MVT TruncVT = MVT::getIntegerVT(EltSize);
4643 SmallVector<SDValue, 8> Ops;
4644 for (unsigned i = 0; i != NumElts; ++i) {
4645 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4646 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004647 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004648 }
4649 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4650 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004651}
4652
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004653static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4654 unsigned Opcode = N->getOpcode();
4655 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4656 SDNode *N0 = N->getOperand(0).getNode();
4657 SDNode *N1 = N->getOperand(1).getNode();
4658 return N0->hasOneUse() && N1->hasOneUse() &&
4659 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4660 }
4661 return false;
4662}
4663
4664static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4665 unsigned Opcode = N->getOpcode();
4666 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4667 SDNode *N0 = N->getOperand(0).getNode();
4668 SDNode *N1 = N->getOperand(1).getNode();
4669 return N0->hasOneUse() && N1->hasOneUse() &&
4670 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4671 }
4672 return false;
4673}
4674
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004675static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4676 // Multiplications are only custom-lowered for 128-bit vectors so that
4677 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4678 EVT VT = Op.getValueType();
4679 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4680 SDNode *N0 = Op.getOperand(0).getNode();
4681 SDNode *N1 = Op.getOperand(1).getNode();
4682 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004683 bool isMLA = false;
4684 bool isN0SExt = isSignExtended(N0, DAG);
4685 bool isN1SExt = isSignExtended(N1, DAG);
4686 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004687 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004688 else {
4689 bool isN0ZExt = isZeroExtended(N0, DAG);
4690 bool isN1ZExt = isZeroExtended(N1, DAG);
4691 if (isN0ZExt && isN1ZExt)
4692 NewOpc = ARMISD::VMULLu;
4693 else if (isN1SExt || isN1ZExt) {
4694 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4695 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4696 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4697 NewOpc = ARMISD::VMULLs;
4698 isMLA = true;
4699 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4700 NewOpc = ARMISD::VMULLu;
4701 isMLA = true;
4702 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4703 std::swap(N0, N1);
4704 NewOpc = ARMISD::VMULLu;
4705 isMLA = true;
4706 }
4707 }
4708
4709 if (!NewOpc) {
4710 if (VT == MVT::v2i64)
4711 // Fall through to expand this. It is not legal.
4712 return SDValue();
4713 else
4714 // Other vector multiplications are legal.
4715 return Op;
4716 }
4717 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004718
4719 // Legalize to a VMULL instruction.
4720 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004721 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004722 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004723 if (!isMLA) {
4724 Op0 = SkipExtension(N0, DAG);
4725 assert(Op0.getValueType().is64BitVector() &&
4726 Op1.getValueType().is64BitVector() &&
4727 "unexpected types for extended operands to VMULL");
4728 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4729 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004730
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004731 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4732 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4733 // vmull q0, d4, d6
4734 // vmlal q0, d5, d6
4735 // is faster than
4736 // vaddl q0, d4, d5
4737 // vmovl q1, d6
4738 // vmul q0, q0, q1
4739 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4740 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4741 EVT Op1VT = Op1.getValueType();
4742 return DAG.getNode(N0->getOpcode(), DL, VT,
4743 DAG.getNode(NewOpc, DL, VT,
4744 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4745 DAG.getNode(NewOpc, DL, VT,
4746 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004747}
4748
Owen Anderson76706012011-04-05 21:48:57 +00004749static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004750LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4751 // Convert to float
4752 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4753 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4754 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4755 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4756 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4757 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4758 // Get reciprocal estimate.
4759 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004760 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004761 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4762 // Because char has a smaller range than uchar, we can actually get away
4763 // without any newton steps. This requires that we use a weird bias
4764 // of 0xb000, however (again, this has been exhaustively tested).
4765 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4766 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4767 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4768 Y = DAG.getConstant(0xb000, MVT::i32);
4769 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4770 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4771 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4772 // Convert back to short.
4773 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4774 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4775 return X;
4776}
4777
Owen Anderson76706012011-04-05 21:48:57 +00004778static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004779LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4780 SDValue N2;
4781 // Convert to float.
4782 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4783 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4784 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4785 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4786 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4787 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004788
Nate Begeman7973f352011-02-11 20:53:29 +00004789 // Use reciprocal estimate and one refinement step.
4790 // float4 recip = vrecpeq_f32(yf);
4791 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004792 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004793 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004794 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004795 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4796 N1, N2);
4797 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4798 // Because short has a smaller range than ushort, we can actually get away
4799 // with only a single newton step. This requires that we use a weird bias
4800 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004801 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004802 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4803 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004804 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004805 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4806 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4807 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4808 // Convert back to integer and return.
4809 // return vmovn_s32(vcvt_s32_f32(result));
4810 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4811 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4812 return N0;
4813}
4814
4815static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4816 EVT VT = Op.getValueType();
4817 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4818 "unexpected type for custom-lowering ISD::SDIV");
4819
4820 DebugLoc dl = Op.getDebugLoc();
4821 SDValue N0 = Op.getOperand(0);
4822 SDValue N1 = Op.getOperand(1);
4823 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004824
Nate Begeman7973f352011-02-11 20:53:29 +00004825 if (VT == MVT::v8i8) {
4826 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4827 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004828
Nate Begeman7973f352011-02-11 20:53:29 +00004829 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4830 DAG.getIntPtrConstant(4));
4831 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004832 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004833 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4834 DAG.getIntPtrConstant(0));
4835 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4836 DAG.getIntPtrConstant(0));
4837
4838 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4839 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4840
4841 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4842 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004843
Nate Begeman7973f352011-02-11 20:53:29 +00004844 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4845 return N0;
4846 }
4847 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4848}
4849
4850static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4851 EVT VT = Op.getValueType();
4852 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4853 "unexpected type for custom-lowering ISD::UDIV");
4854
4855 DebugLoc dl = Op.getDebugLoc();
4856 SDValue N0 = Op.getOperand(0);
4857 SDValue N1 = Op.getOperand(1);
4858 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004859
Nate Begeman7973f352011-02-11 20:53:29 +00004860 if (VT == MVT::v8i8) {
4861 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4862 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004863
Nate Begeman7973f352011-02-11 20:53:29 +00004864 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4865 DAG.getIntPtrConstant(4));
4866 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004867 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004868 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4869 DAG.getIntPtrConstant(0));
4870 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4871 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004872
Nate Begeman7973f352011-02-11 20:53:29 +00004873 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4874 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004875
Nate Begeman7973f352011-02-11 20:53:29 +00004876 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4877 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004878
4879 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004880 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4881 N0);
4882 return N0;
4883 }
Owen Anderson76706012011-04-05 21:48:57 +00004884
Nate Begeman7973f352011-02-11 20:53:29 +00004885 // v4i16 sdiv ... Convert to float.
4886 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4887 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4888 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4889 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4890 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004891 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00004892
4893 // Use reciprocal estimate and two refinement steps.
4894 // float4 recip = vrecpeq_f32(yf);
4895 // recip *= vrecpsq_f32(yf, recip);
4896 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004897 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004898 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00004899 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004900 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004901 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004902 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004903 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004904 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004905 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004906 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4907 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4908 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4909 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004910 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00004911 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4912 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4913 N1 = DAG.getConstant(2, MVT::i32);
4914 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4915 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4916 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4917 // Convert back to integer and return.
4918 // return vmovn_u32(vcvt_s32_f32(result));
4919 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4920 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4921 return N0;
4922}
4923
Evan Cheng342e3162011-08-30 01:34:54 +00004924static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
4925 EVT VT = Op.getNode()->getValueType(0);
4926 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
4927
4928 unsigned Opc;
4929 bool ExtraOp = false;
4930 switch (Op.getOpcode()) {
4931 default: assert(0 && "Invalid code");
4932 case ISD::ADDC: Opc = ARMISD::ADDC; break;
4933 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
4934 case ISD::SUBC: Opc = ARMISD::SUBC; break;
4935 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
4936 }
4937
4938 if (!ExtraOp)
4939 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4940 Op.getOperand(1));
4941 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4942 Op.getOperand(1), Op.getOperand(2));
4943}
4944
Eli Friedman74bf18c2011-09-15 22:26:18 +00004945static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00004946 // Monotonic load/store is legal for all targets
4947 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
4948 return Op;
4949
4950 // Aquire/Release load/store is not legal for targets without a
4951 // dmb or equivalent available.
4952 return SDValue();
4953}
4954
4955
Eli Friedman2bdffe42011-08-31 00:31:29 +00004956static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00004957ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
4958 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00004959 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00004960 assert (Node->getValueType(0) == MVT::i64 &&
4961 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00004962
Eli Friedman4d3f3292011-08-31 17:52:22 +00004963 SmallVector<SDValue, 6> Ops;
4964 Ops.push_back(Node->getOperand(0)); // Chain
4965 Ops.push_back(Node->getOperand(1)); // Ptr
4966 // Low part of Val1
4967 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4968 Node->getOperand(2), DAG.getIntPtrConstant(0)));
4969 // High part of Val1
4970 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4971 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00004972 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00004973 // High part of Val1
4974 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4975 Node->getOperand(3), DAG.getIntPtrConstant(0)));
4976 // High part of Val2
4977 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4978 Node->getOperand(3), DAG.getIntPtrConstant(1)));
4979 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00004980 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
4981 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00004982 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00004983 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00004984 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00004985 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
4986 Results.push_back(Result.getValue(2));
4987}
4988
Dan Gohmand858e902010-04-17 15:26:15 +00004989SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004990 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004991 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004992 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004993 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004994 case ISD::GlobalAddress:
4995 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4996 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00004997 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004998 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004999 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5000 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005001 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005002 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005003 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005004 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005005 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005006 case ISD::SINT_TO_FP:
5007 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5008 case ISD::FP_TO_SINT:
5009 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005010 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005011 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005012 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005013 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005014 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005015 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00005016 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005017 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5018 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005019 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005020 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005021 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005022 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005023 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005024 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005025 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005026 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005027 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00005028 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005029 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005030 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005031 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005032 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005033 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005034 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005035 case ISD::SDIV: return LowerSDIV(Op, DAG);
5036 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005037 case ISD::ADDC:
5038 case ISD::ADDE:
5039 case ISD::SUBC:
5040 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005041 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005042 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005043 }
Dan Gohman475871a2008-07-27 21:46:04 +00005044 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005045}
5046
Duncan Sands1607f052008-12-01 11:39:25 +00005047/// ReplaceNodeResults - Replace the results of node with an illegal result
5048/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005049void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5050 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005051 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005052 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005053 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005054 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005055 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00005056 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005057 case ISD::BITCAST:
5058 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005059 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005060 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005061 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005062 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005063 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005064 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005065 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005066 return;
5067 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005068 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005069 return;
5070 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005071 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005072 return;
5073 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005074 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005075 return;
5076 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005077 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005078 return;
5079 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005080 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005081 return;
5082 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005083 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005084 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005085 case ISD::ATOMIC_CMP_SWAP:
5086 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5087 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005088 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005089 if (Res.getNode())
5090 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005091}
Chris Lattner27a6c732007-11-24 07:07:01 +00005092
Evan Chenga8e29892007-01-19 07:51:42 +00005093//===----------------------------------------------------------------------===//
5094// ARM Scheduler Hooks
5095//===----------------------------------------------------------------------===//
5096
5097MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005098ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5099 MachineBasicBlock *BB,
5100 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005101 unsigned dest = MI->getOperand(0).getReg();
5102 unsigned ptr = MI->getOperand(1).getReg();
5103 unsigned oldval = MI->getOperand(2).getReg();
5104 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005105 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5106 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005107 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005108
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005109 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5110 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00005111 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005112 : ARM::GPRRegisterClass);
5113
5114 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00005115 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5116 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
5117 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005118 }
5119
Jim Grosbach5278eb82009-12-11 01:42:04 +00005120 unsigned ldrOpc, strOpc;
5121 switch (Size) {
5122 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005123 case 1:
5124 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005125 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005126 break;
5127 case 2:
5128 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5129 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5130 break;
5131 case 4:
5132 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5133 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5134 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005135 }
5136
5137 MachineFunction *MF = BB->getParent();
5138 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5139 MachineFunction::iterator It = BB;
5140 ++It; // insert the new blocks after the current block
5141
5142 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5143 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5144 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5145 MF->insert(It, loop1MBB);
5146 MF->insert(It, loop2MBB);
5147 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005148
5149 // Transfer the remainder of BB and its successor edges to exitMBB.
5150 exitMBB->splice(exitMBB->begin(), BB,
5151 llvm::next(MachineBasicBlock::iterator(MI)),
5152 BB->end());
5153 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005154
5155 // thisMBB:
5156 // ...
5157 // fallthrough --> loop1MBB
5158 BB->addSuccessor(loop1MBB);
5159
5160 // loop1MBB:
5161 // ldrex dest, [ptr]
5162 // cmp dest, oldval
5163 // bne exitMBB
5164 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005165 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5166 if (ldrOpc == ARM::t2LDREX)
5167 MIB.addImm(0);
5168 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005169 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005170 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005171 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5172 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005173 BB->addSuccessor(loop2MBB);
5174 BB->addSuccessor(exitMBB);
5175
5176 // loop2MBB:
5177 // strex scratch, newval, [ptr]
5178 // cmp scratch, #0
5179 // bne loop1MBB
5180 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005181 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5182 if (strOpc == ARM::t2STREX)
5183 MIB.addImm(0);
5184 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005185 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005186 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005187 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5188 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005189 BB->addSuccessor(loop1MBB);
5190 BB->addSuccessor(exitMBB);
5191
5192 // exitMBB:
5193 // ...
5194 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005195
Dan Gohman14152b42010-07-06 20:24:04 +00005196 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005197
Jim Grosbach5278eb82009-12-11 01:42:04 +00005198 return BB;
5199}
5200
5201MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005202ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5203 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005204 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5205 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5206
5207 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005208 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005209 MachineFunction::iterator It = BB;
5210 ++It;
5211
5212 unsigned dest = MI->getOperand(0).getReg();
5213 unsigned ptr = MI->getOperand(1).getReg();
5214 unsigned incr = MI->getOperand(2).getReg();
5215 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005216 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005217
5218 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5219 if (isThumb2) {
5220 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5221 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5222 }
5223
Jim Grosbachc3c23542009-12-14 04:22:04 +00005224 unsigned ldrOpc, strOpc;
5225 switch (Size) {
5226 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005227 case 1:
5228 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005229 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005230 break;
5231 case 2:
5232 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5233 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5234 break;
5235 case 4:
5236 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5237 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5238 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005239 }
5240
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005241 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5242 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5243 MF->insert(It, loopMBB);
5244 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005245
5246 // Transfer the remainder of BB and its successor edges to exitMBB.
5247 exitMBB->splice(exitMBB->begin(), BB,
5248 llvm::next(MachineBasicBlock::iterator(MI)),
5249 BB->end());
5250 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005251
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005252 TargetRegisterClass *TRC =
5253 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5254 unsigned scratch = MRI.createVirtualRegister(TRC);
5255 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005256
5257 // thisMBB:
5258 // ...
5259 // fallthrough --> loopMBB
5260 BB->addSuccessor(loopMBB);
5261
5262 // loopMBB:
5263 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005264 // <binop> scratch2, dest, incr
5265 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005266 // cmp scratch, #0
5267 // bne- loopMBB
5268 // fallthrough --> exitMBB
5269 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005270 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5271 if (ldrOpc == ARM::t2LDREX)
5272 MIB.addImm(0);
5273 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005274 if (BinOpcode) {
5275 // operand order needs to go the other way for NAND
5276 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5277 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5278 addReg(incr).addReg(dest)).addReg(0);
5279 else
5280 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5281 addReg(dest).addReg(incr)).addReg(0);
5282 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005283
Jim Grosbachb6aed502011-09-09 18:37:27 +00005284 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5285 if (strOpc == ARM::t2STREX)
5286 MIB.addImm(0);
5287 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005288 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005289 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005290 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5291 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005292
5293 BB->addSuccessor(loopMBB);
5294 BB->addSuccessor(exitMBB);
5295
5296 // exitMBB:
5297 // ...
5298 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005299
Dan Gohman14152b42010-07-06 20:24:04 +00005300 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005301
Jim Grosbachc3c23542009-12-14 04:22:04 +00005302 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005303}
5304
Jim Grosbachf7da8822011-04-26 19:44:18 +00005305MachineBasicBlock *
5306ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5307 MachineBasicBlock *BB,
5308 unsigned Size,
5309 bool signExtend,
5310 ARMCC::CondCodes Cond) const {
5311 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5312
5313 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5314 MachineFunction *MF = BB->getParent();
5315 MachineFunction::iterator It = BB;
5316 ++It;
5317
5318 unsigned dest = MI->getOperand(0).getReg();
5319 unsigned ptr = MI->getOperand(1).getReg();
5320 unsigned incr = MI->getOperand(2).getReg();
5321 unsigned oldval = dest;
5322 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005323 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005324
5325 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5326 if (isThumb2) {
5327 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5328 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5329 }
5330
Jim Grosbachf7da8822011-04-26 19:44:18 +00005331 unsigned ldrOpc, strOpc, extendOpc;
5332 switch (Size) {
5333 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5334 case 1:
5335 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5336 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005337 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005338 break;
5339 case 2:
5340 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5341 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005342 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005343 break;
5344 case 4:
5345 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5346 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5347 extendOpc = 0;
5348 break;
5349 }
5350
5351 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5352 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5353 MF->insert(It, loopMBB);
5354 MF->insert(It, exitMBB);
5355
5356 // Transfer the remainder of BB and its successor edges to exitMBB.
5357 exitMBB->splice(exitMBB->begin(), BB,
5358 llvm::next(MachineBasicBlock::iterator(MI)),
5359 BB->end());
5360 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5361
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005362 TargetRegisterClass *TRC =
5363 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5364 unsigned scratch = MRI.createVirtualRegister(TRC);
5365 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005366
5367 // thisMBB:
5368 // ...
5369 // fallthrough --> loopMBB
5370 BB->addSuccessor(loopMBB);
5371
5372 // loopMBB:
5373 // ldrex dest, ptr
5374 // (sign extend dest, if required)
5375 // cmp dest, incr
5376 // cmov.cond scratch2, dest, incr
5377 // strex scratch, scratch2, ptr
5378 // cmp scratch, #0
5379 // bne- loopMBB
5380 // fallthrough --> exitMBB
5381 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005382 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5383 if (ldrOpc == ARM::t2LDREX)
5384 MIB.addImm(0);
5385 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005386
5387 // Sign extend the value, if necessary.
5388 if (signExtend && extendOpc) {
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005389 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005390 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5391 .addReg(dest)
5392 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005393 }
5394
5395 // Build compare and cmov instructions.
5396 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5397 .addReg(oldval).addReg(incr));
5398 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5399 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5400
Jim Grosbachb6aed502011-09-09 18:37:27 +00005401 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5402 if (strOpc == ARM::t2STREX)
5403 MIB.addImm(0);
5404 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005405 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5406 .addReg(scratch).addImm(0));
5407 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5408 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5409
5410 BB->addSuccessor(loopMBB);
5411 BB->addSuccessor(exitMBB);
5412
5413 // exitMBB:
5414 // ...
5415 BB = exitMBB;
5416
5417 MI->eraseFromParent(); // The instruction is gone now.
5418
5419 return BB;
5420}
5421
Eli Friedman2bdffe42011-08-31 00:31:29 +00005422MachineBasicBlock *
5423ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5424 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005425 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005426 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5427 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5428
5429 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5430 MachineFunction *MF = BB->getParent();
5431 MachineFunction::iterator It = BB;
5432 ++It;
5433
5434 unsigned destlo = MI->getOperand(0).getReg();
5435 unsigned desthi = MI->getOperand(1).getReg();
5436 unsigned ptr = MI->getOperand(2).getReg();
5437 unsigned vallo = MI->getOperand(3).getReg();
5438 unsigned valhi = MI->getOperand(4).getReg();
5439 DebugLoc dl = MI->getDebugLoc();
5440 bool isThumb2 = Subtarget->isThumb2();
5441
5442 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5443 if (isThumb2) {
5444 MRI.constrainRegClass(destlo, ARM::rGPRRegisterClass);
5445 MRI.constrainRegClass(desthi, ARM::rGPRRegisterClass);
5446 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5447 }
5448
5449 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5450 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5451
5452 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005453 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005454 if (IsCmpxchg) {
5455 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5456 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5457 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005458 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5459 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005460 if (IsCmpxchg) {
5461 MF->insert(It, contBB);
5462 MF->insert(It, cont2BB);
5463 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005464 MF->insert(It, exitMBB);
5465
5466 // Transfer the remainder of BB and its successor edges to exitMBB.
5467 exitMBB->splice(exitMBB->begin(), BB,
5468 llvm::next(MachineBasicBlock::iterator(MI)),
5469 BB->end());
5470 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5471
5472 TargetRegisterClass *TRC =
5473 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5474 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5475
5476 // thisMBB:
5477 // ...
5478 // fallthrough --> loopMBB
5479 BB->addSuccessor(loopMBB);
5480
5481 // loopMBB:
5482 // ldrexd r2, r3, ptr
5483 // <binopa> r0, r2, incr
5484 // <binopb> r1, r3, incr
5485 // strexd storesuccess, r0, r1, ptr
5486 // cmp storesuccess, #0
5487 // bne- loopMBB
5488 // fallthrough --> exitMBB
5489 //
5490 // Note that the registers are explicitly specified because there is not any
5491 // way to force the register allocator to allocate a register pair.
5492 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005493 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005494 // need to properly enforce the restriction that the two output registers
5495 // for ldrexd must be different.
5496 BB = loopMBB;
5497 // Load
5498 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5499 .addReg(ARM::R2, RegState::Define)
5500 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5501 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5502 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5503 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005504
5505 if (IsCmpxchg) {
5506 // Add early exit
5507 for (unsigned i = 0; i < 2; i++) {
5508 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5509 ARM::CMPrr))
5510 .addReg(i == 0 ? destlo : desthi)
5511 .addReg(i == 0 ? vallo : valhi));
5512 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5513 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5514 BB->addSuccessor(exitMBB);
5515 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5516 BB = (i == 0 ? contBB : cont2BB);
5517 }
5518
5519 // Copy to physregs for strexd
5520 unsigned setlo = MI->getOperand(5).getReg();
5521 unsigned sethi = MI->getOperand(6).getReg();
5522 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5523 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5524 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005525 // Perform binary operation
5526 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5527 .addReg(destlo).addReg(vallo))
5528 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5529 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5530 .addReg(desthi).addReg(valhi)).addReg(0);
5531 } else {
5532 // Copy to physregs for strexd
5533 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5534 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5535 }
5536
5537 // Store
5538 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5539 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5540 // Cmp+jump
5541 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5542 .addReg(storesuccess).addImm(0));
5543 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5544 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5545
5546 BB->addSuccessor(loopMBB);
5547 BB->addSuccessor(exitMBB);
5548
5549 // exitMBB:
5550 // ...
5551 BB = exitMBB;
5552
5553 MI->eraseFromParent(); // The instruction is gone now.
5554
5555 return BB;
5556}
5557
Bill Wendlingf1083d42011-10-07 22:08:37 +00005558/// EmitBasePointerRecalculation - For functions using a base pointer, we
5559/// rematerialize it (via the frame pointer).
5560void ARMTargetLowering::
5561EmitBasePointerRecalculation(MachineInstr *MI, MachineBasicBlock *MBB,
5562 MachineBasicBlock *DispatchBB) const {
5563 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5564 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
5565 MachineFunction &MF = *MI->getParent()->getParent();
5566 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
5567 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
5568
5569 if (!RI.hasBasePointer(MF)) return;
5570
5571 MachineBasicBlock::iterator MBBI = MI;
5572
5573 int32_t NumBytes = AFI->getFramePtrSpillOffset();
5574 unsigned FramePtr = RI.getFrameRegister(MF);
5575 assert(MF.getTarget().getFrameLowering()->hasFP(MF) &&
5576 "Base pointer without frame pointer?");
5577
5578 if (AFI->isThumb2Function())
5579 llvm::emitT2RegPlusImmediate(*MBB, MBBI, MI->getDebugLoc(), ARM::R6,
5580 FramePtr, -NumBytes, ARMCC::AL, 0, *AII);
5581 else if (AFI->isThumbFunction())
5582 llvm::emitThumbRegPlusImmediate(*MBB, MBBI, MI->getDebugLoc(), ARM::R6,
5583 FramePtr, -NumBytes, *AII, RI);
5584 else
5585 llvm::emitARMRegPlusImmediate(*MBB, MBBI, MI->getDebugLoc(), ARM::R6,
5586 FramePtr, -NumBytes, ARMCC::AL, 0, *AII);
5587
5588 if (!RI.needsStackRealignment(MF)) return;
5589
5590 // If there's dynamic realignment, adjust for it.
5591 MachineFrameInfo *MFI = MF.getFrameInfo();
5592 unsigned MaxAlign = MFI->getMaxAlignment();
5593 assert(!AFI->isThumb1OnlyFunction());
5594
5595 // Emit bic r6, r6, MaxAlign
5596 unsigned bicOpc = AFI->isThumbFunction() ? ARM::t2BICri : ARM::BICri;
5597 AddDefaultCC(
5598 AddDefaultPred(
5599 BuildMI(*MBB, MBBI, MI->getDebugLoc(), TII->get(bicOpc), ARM::R6)
5600 .addReg(ARM::R6, RegState::Kill)
5601 .addImm(MaxAlign - 1)));
5602}
5603
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005604/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5605/// registers the function context.
5606void ARMTargetLowering::
5607SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5608 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005609 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5610 DebugLoc dl = MI->getDebugLoc();
5611 MachineFunction *MF = MBB->getParent();
5612 MachineRegisterInfo *MRI = &MF->getRegInfo();
5613 MachineConstantPool *MCP = MF->getConstantPool();
5614 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5615 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005616
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005617 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005618 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005619
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005620 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005621 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005622 ARMConstantPoolValue *CPV =
5623 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5624 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5625
5626 const TargetRegisterClass *TRC =
5627 isThumb ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5628
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005629 // Grab constant pool and fixed stack memory operands.
5630 MachineMemOperand *CPMMO =
5631 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5632 MachineMemOperand::MOLoad, 4, 4);
5633
5634 MachineMemOperand *FIMMOSt =
5635 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5636 MachineMemOperand::MOStore, 4, 4);
5637
Bill Wendlingf1083d42011-10-07 22:08:37 +00005638 EmitBasePointerRecalculation(MI, MBB, DispatchBB);
5639
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005640 // Load the address of the dispatch MBB into the jump buffer.
5641 if (isThumb2) {
5642 // Incoming value: jbuf
5643 // ldr.n r5, LCPI1_1
5644 // orr r5, r5, #1
5645 // add r5, pc
5646 // str r5, [$jbuf, #+4] ; &jbuf[1]
5647 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5648 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
5649 .addConstantPoolIndex(CPI)
5650 .addMemOperand(CPMMO));
5651 // Set the low bit because of thumb mode.
5652 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5653 AddDefaultCC(
5654 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
5655 .addReg(NewVReg1, RegState::Kill)
5656 .addImm(0x01)));
5657 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5658 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
5659 .addReg(NewVReg2, RegState::Kill)
5660 .addImm(PCLabelId);
5661 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
5662 .addReg(NewVReg3, RegState::Kill)
5663 .addFrameIndex(FI)
5664 .addImm(36) // &jbuf[1] :: pc
5665 .addMemOperand(FIMMOSt));
5666 } else if (isThumb) {
5667 // Incoming value: jbuf
5668 // ldr.n r1, LCPI1_4
5669 // add r1, pc
5670 // mov r2, #1
5671 // orrs r1, r2
5672 // add r2, $jbuf, #+4 ; &jbuf[1]
5673 // str r1, [r2]
5674 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5675 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
5676 .addConstantPoolIndex(CPI)
5677 .addMemOperand(CPMMO));
5678 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5679 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
5680 .addReg(NewVReg1, RegState::Kill)
5681 .addImm(PCLabelId);
5682 // Set the low bit because of thumb mode.
5683 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5684 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
5685 .addReg(ARM::CPSR, RegState::Define)
5686 .addImm(1));
5687 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5688 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
5689 .addReg(ARM::CPSR, RegState::Define)
5690 .addReg(NewVReg2, RegState::Kill)
5691 .addReg(NewVReg3, RegState::Kill));
5692 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5693 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
5694 .addFrameIndex(FI)
5695 .addImm(36)); // &jbuf[1] :: pc
5696 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
5697 .addReg(NewVReg4, RegState::Kill)
5698 .addReg(NewVReg5, RegState::Kill)
5699 .addImm(0)
5700 .addMemOperand(FIMMOSt));
5701 } else {
5702 // Incoming value: jbuf
5703 // ldr r1, LCPI1_1
5704 // add r1, pc, r1
5705 // str r1, [$jbuf, #+4] ; &jbuf[1]
5706 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5707 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
5708 .addConstantPoolIndex(CPI)
5709 .addImm(0)
5710 .addMemOperand(CPMMO));
5711 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5712 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
5713 .addReg(NewVReg1, RegState::Kill)
5714 .addImm(PCLabelId));
5715 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
5716 .addReg(NewVReg2, RegState::Kill)
5717 .addFrameIndex(FI)
5718 .addImm(36) // &jbuf[1] :: pc
5719 .addMemOperand(FIMMOSt));
5720 }
5721}
5722
5723MachineBasicBlock *ARMTargetLowering::
5724EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
5725 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5726 DebugLoc dl = MI->getDebugLoc();
5727 MachineFunction *MF = MBB->getParent();
5728 MachineRegisterInfo *MRI = &MF->getRegInfo();
5729 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5730 MachineFrameInfo *MFI = MF->getFrameInfo();
5731 int FI = MFI->getFunctionContextIndex();
5732
5733 const TargetRegisterClass *TRC =
5734 Subtarget->isThumb() ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5735
Bill Wendling04f15b42011-10-06 21:29:56 +00005736 // Get a mapping of the call site numbers to all of the landing pads they're
5737 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00005738 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
5739 unsigned MaxCSNum = 0;
5740 MachineModuleInfo &MMI = MF->getMMI();
5741 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E; ++BB) {
5742 if (!BB->isLandingPad()) continue;
5743
5744 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
5745 // pad.
5746 for (MachineBasicBlock::iterator
5747 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
5748 if (!II->isEHLabel()) continue;
5749
5750 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00005751 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00005752
Bill Wendling5cbef192011-10-05 23:28:57 +00005753 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
5754 for (SmallVectorImpl<unsigned>::iterator
5755 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
5756 CSI != CSE; ++CSI) {
5757 CallSiteNumToLPad[*CSI].push_back(BB);
5758 MaxCSNum = std::max(MaxCSNum, *CSI);
5759 }
Bill Wendling2a850152011-10-05 00:02:33 +00005760 break;
5761 }
5762 }
5763
5764 // Get an ordered list of the machine basic blocks for the jump table.
5765 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00005766 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00005767 LPadList.reserve(CallSiteNumToLPad.size());
5768 for (unsigned I = 1; I <= MaxCSNum; ++I) {
5769 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
5770 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005771 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00005772 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00005773 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
5774 }
Bill Wendling2a850152011-10-05 00:02:33 +00005775 }
5776
Bill Wendling5cbef192011-10-05 23:28:57 +00005777 assert(!LPadList.empty() &&
5778 "No landing pad destinations for the dispatch jump table!");
5779
Bill Wendling04f15b42011-10-06 21:29:56 +00005780 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00005781 MachineJumpTableInfo *JTI =
5782 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
5783 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
5784 unsigned UId = AFI->createJumpTableUId();
5785
Bill Wendling04f15b42011-10-06 21:29:56 +00005786 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005787
5788 // Shove the dispatch's address into the return slot in the function context.
5789 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
5790 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005791
Bill Wendlingbb734682011-10-05 00:39:32 +00005792 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00005793 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00005794 DispatchBB->addSuccessor(TrapBB);
5795
5796 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
5797 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00005798
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00005799 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00005800 MF->insert(MF->end(), DispatchBB);
5801 MF->insert(MF->end(), DispContBB);
5802 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00005803
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005804 // Insert code into the entry block that creates and registers the function
5805 // context.
5806 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
5807
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005808 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00005809 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00005810 MachineMemOperand::MOLoad |
5811 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00005812
Bill Wendling952cb502011-10-18 22:49:07 +00005813 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00005814 if (Subtarget->isThumb2()) {
5815 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5816 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
5817 .addFrameIndex(FI)
5818 .addImm(4)
5819 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005820
Bill Wendling952cb502011-10-18 22:49:07 +00005821 if (NumLPads < 256) {
5822 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
5823 .addReg(NewVReg1)
5824 .addImm(LPadList.size()));
5825 } else {
5826 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5827 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005828 .addImm(NumLPads & 0xFFFF));
5829
5830 unsigned VReg2 = VReg1;
5831 if ((NumLPads & 0xFFFF0000) != 0) {
5832 VReg2 = MRI->createVirtualRegister(TRC);
5833 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
5834 .addReg(VReg1)
5835 .addImm(NumLPads >> 16));
5836 }
5837
Bill Wendling952cb502011-10-18 22:49:07 +00005838 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
5839 .addReg(NewVReg1)
5840 .addReg(VReg2));
5841 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005842
Bill Wendling95ce2e92011-10-06 22:53:00 +00005843 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
5844 .addMBB(TrapBB)
5845 .addImm(ARMCC::HI)
5846 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00005847
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005848 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5849 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005850 .addJumpTableIndex(MJTI)
5851 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00005852
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005853 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005854 AddDefaultCC(
5855 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005856 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
5857 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005858 .addReg(NewVReg1)
5859 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
5860
5861 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005862 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00005863 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005864 .addJumpTableIndex(MJTI)
5865 .addImm(UId);
5866 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00005867 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5868 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
5869 .addFrameIndex(FI)
5870 .addImm(1)
5871 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00005872
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005873 if (NumLPads < 256) {
5874 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
5875 .addReg(NewVReg1)
5876 .addImm(NumLPads));
5877 } else {
5878 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00005879 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
5880 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
5881
5882 // MachineConstantPool wants an explicit alignment.
5883 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
5884 if (Align == 0)
5885 Align = getTargetData()->getTypeAllocSize(C->getType());
5886 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005887
5888 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5889 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
5890 .addReg(VReg1, RegState::Define)
5891 .addConstantPoolIndex(Idx));
5892 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
5893 .addReg(NewVReg1)
5894 .addReg(VReg1));
5895 }
5896
Bill Wendling083a8eb2011-10-06 23:37:36 +00005897 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
5898 .addMBB(TrapBB)
5899 .addImm(ARMCC::HI)
5900 .addReg(ARM::CPSR);
5901
5902 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5903 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
5904 .addReg(ARM::CPSR, RegState::Define)
5905 .addReg(NewVReg1)
5906 .addImm(2));
5907
5908 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00005909 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00005910 .addJumpTableIndex(MJTI)
5911 .addImm(UId));
5912
5913 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5914 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
5915 .addReg(ARM::CPSR, RegState::Define)
5916 .addReg(NewVReg2, RegState::Kill)
5917 .addReg(NewVReg3));
5918
5919 MachineMemOperand *JTMMOLd =
5920 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
5921 MachineMemOperand::MOLoad, 4, 4);
5922
5923 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5924 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
5925 .addReg(NewVReg4, RegState::Kill)
5926 .addImm(0)
5927 .addMemOperand(JTMMOLd));
5928
5929 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
5930 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
5931 .addReg(ARM::CPSR, RegState::Define)
5932 .addReg(NewVReg5, RegState::Kill)
5933 .addReg(NewVReg3));
5934
5935 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
5936 .addReg(NewVReg6, RegState::Kill)
5937 .addJumpTableIndex(MJTI)
5938 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005939 } else {
5940 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5941 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
5942 .addFrameIndex(FI)
5943 .addImm(4)
5944 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00005945
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005946 if (NumLPads < 256) {
5947 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
5948 .addReg(NewVReg1)
5949 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00005950 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005951 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5952 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005953 .addImm(NumLPads & 0xFFFF));
5954
5955 unsigned VReg2 = VReg1;
5956 if ((NumLPads & 0xFFFF0000) != 0) {
5957 VReg2 = MRI->createVirtualRegister(TRC);
5958 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
5959 .addReg(VReg1)
5960 .addImm(NumLPads >> 16));
5961 }
5962
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005963 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
5964 .addReg(NewVReg1)
5965 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00005966 } else {
5967 MachineConstantPool *ConstantPool = MF->getConstantPool();
5968 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
5969 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
5970
5971 // MachineConstantPool wants an explicit alignment.
5972 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
5973 if (Align == 0)
5974 Align = getTargetData()->getTypeAllocSize(C->getType());
5975 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
5976
5977 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5978 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
5979 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00005980 .addConstantPoolIndex(Idx)
5981 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00005982 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
5983 .addReg(NewVReg1)
5984 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005985 }
5986
Bill Wendling95ce2e92011-10-06 22:53:00 +00005987 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
5988 .addMBB(TrapBB)
5989 .addImm(ARMCC::HI)
5990 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00005991
Bill Wendling564392b2011-10-18 22:11:18 +00005992 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005993 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00005994 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005995 .addReg(NewVReg1)
5996 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00005997 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5998 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005999 .addJumpTableIndex(MJTI)
6000 .addImm(UId));
6001
6002 MachineMemOperand *JTMMOLd =
6003 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6004 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00006005 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006006 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00006007 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6008 .addReg(NewVReg3, RegState::Kill)
6009 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006010 .addImm(0)
6011 .addMemOperand(JTMMOLd));
6012
6013 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00006014 .addReg(NewVReg5, RegState::Kill)
6015 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006016 .addJumpTableIndex(MJTI)
6017 .addImm(UId);
6018 }
Bill Wendling2a850152011-10-05 00:02:33 +00006019
Bill Wendlingbb734682011-10-05 00:39:32 +00006020 // Add the jump table entries as successors to the MBB.
Bill Wendling2acf6382011-10-07 23:18:02 +00006021 MachineBasicBlock *PrevMBB = 0;
Bill Wendlingbb734682011-10-05 00:39:32 +00006022 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006023 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6024 MachineBasicBlock *CurMBB = *I;
6025 if (PrevMBB != CurMBB)
6026 DispContBB->addSuccessor(CurMBB);
6027 PrevMBB = CurMBB;
6028 }
6029
Bill Wendling24bb9252011-10-17 05:25:09 +00006030 // N.B. the order the invoke BBs are processed in doesn't matter here.
Bill Wendling969c9ef2011-10-14 23:34:37 +00006031 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6032 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6033 const unsigned *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006034 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006035 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6036 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6037 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006038
6039 // Remove the landing pad successor from the invoke block and replace it
6040 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006041 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6042 BB->succ_end());
6043 while (!Successors.empty()) {
6044 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006045 if (SMBB->isLandingPad()) {
6046 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006047 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006048 }
6049 }
6050
6051 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006052
6053 // Find the invoke call and mark all of the callee-saved registers as
6054 // 'implicit defined' so that they're spilled. This prevents code from
6055 // moving instructions to before the EH block, where they will never be
6056 // executed.
6057 for (MachineBasicBlock::reverse_iterator
6058 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
6059 if (!II->getDesc().isCall()) continue;
6060
6061 DenseMap<unsigned, bool> DefRegs;
6062 for (MachineInstr::mop_iterator
6063 OI = II->operands_begin(), OE = II->operands_end();
6064 OI != OE; ++OI) {
6065 if (!OI->isReg()) continue;
6066 DefRegs[OI->getReg()] = true;
6067 }
6068
6069 MachineInstrBuilder MIB(&*II);
6070
Bill Wendling5d798592011-10-14 23:55:44 +00006071 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006072 unsigned Reg = SavedRegs[i];
6073 if (Subtarget->isThumb2() &&
6074 !ARM::tGPRRegisterClass->contains(Reg) &&
6075 !ARM::hGPRRegisterClass->contains(Reg))
6076 continue;
6077 else if (Subtarget->isThumb1Only() &&
6078 !ARM::tGPRRegisterClass->contains(Reg))
6079 continue;
6080 else if (!Subtarget->isThumb() &&
6081 !ARM::GPRRegisterClass->contains(Reg))
6082 continue;
6083 if (!DefRegs[Reg])
6084 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006085 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006086
6087 break;
6088 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006089 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006090
Bill Wendlingf7b02072011-10-18 18:30:49 +00006091 // Mark all former landing pads as non-landing pads. The dispatch is the only
6092 // landing pad now.
6093 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6094 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6095 (*I)->setIsLandingPad(false);
6096
Bill Wendlingbb734682011-10-05 00:39:32 +00006097 // The instruction is gone now.
6098 MI->eraseFromParent();
6099
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006100 return MBB;
6101}
6102
Evan Cheng218977b2010-07-13 19:27:42 +00006103static
6104MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6105 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6106 E = MBB->succ_end(); I != E; ++I)
6107 if (*I != Succ)
6108 return *I;
6109 llvm_unreachable("Expecting a BB with two successors!");
6110}
6111
Jim Grosbache801dc42009-12-12 01:40:06 +00006112MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006113ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006114 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006115 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006116 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006117 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006118 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006119 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006120 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006121 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006122 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006123 // The Thumb2 pre-indexed stores have the same MI operands, they just
6124 // define them differently in the .td files from the isel patterns, so
6125 // they need pseudos.
6126 case ARM::t2STR_preidx:
6127 MI->setDesc(TII->get(ARM::t2STR_PRE));
6128 return BB;
6129 case ARM::t2STRB_preidx:
6130 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6131 return BB;
6132 case ARM::t2STRH_preidx:
6133 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6134 return BB;
6135
Jim Grosbach19dec202011-08-05 20:35:44 +00006136 case ARM::STRi_preidx:
6137 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006138 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006139 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6140 // Decode the offset.
6141 unsigned Offset = MI->getOperand(4).getImm();
6142 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6143 Offset = ARM_AM::getAM2Offset(Offset);
6144 if (isSub)
6145 Offset = -Offset;
6146
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006147 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006148 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006149 .addOperand(MI->getOperand(0)) // Rn_wb
6150 .addOperand(MI->getOperand(1)) // Rt
6151 .addOperand(MI->getOperand(2)) // Rn
6152 .addImm(Offset) // offset (skip GPR==zero_reg)
6153 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006154 .addOperand(MI->getOperand(6))
6155 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006156 MI->eraseFromParent();
6157 return BB;
6158 }
6159 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006160 case ARM::STRBr_preidx:
6161 case ARM::STRH_preidx: {
6162 unsigned NewOpc;
6163 switch (MI->getOpcode()) {
6164 default: llvm_unreachable("unexpected opcode!");
6165 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6166 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6167 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6168 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006169 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6170 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6171 MIB.addOperand(MI->getOperand(i));
6172 MI->eraseFromParent();
6173 return BB;
6174 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006175 case ARM::ATOMIC_LOAD_ADD_I8:
6176 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6177 case ARM::ATOMIC_LOAD_ADD_I16:
6178 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6179 case ARM::ATOMIC_LOAD_ADD_I32:
6180 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006181
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006182 case ARM::ATOMIC_LOAD_AND_I8:
6183 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6184 case ARM::ATOMIC_LOAD_AND_I16:
6185 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6186 case ARM::ATOMIC_LOAD_AND_I32:
6187 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006188
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006189 case ARM::ATOMIC_LOAD_OR_I8:
6190 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6191 case ARM::ATOMIC_LOAD_OR_I16:
6192 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6193 case ARM::ATOMIC_LOAD_OR_I32:
6194 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006195
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006196 case ARM::ATOMIC_LOAD_XOR_I8:
6197 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6198 case ARM::ATOMIC_LOAD_XOR_I16:
6199 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6200 case ARM::ATOMIC_LOAD_XOR_I32:
6201 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006202
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006203 case ARM::ATOMIC_LOAD_NAND_I8:
6204 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6205 case ARM::ATOMIC_LOAD_NAND_I16:
6206 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6207 case ARM::ATOMIC_LOAD_NAND_I32:
6208 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006209
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006210 case ARM::ATOMIC_LOAD_SUB_I8:
6211 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6212 case ARM::ATOMIC_LOAD_SUB_I16:
6213 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6214 case ARM::ATOMIC_LOAD_SUB_I32:
6215 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006216
Jim Grosbachf7da8822011-04-26 19:44:18 +00006217 case ARM::ATOMIC_LOAD_MIN_I8:
6218 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6219 case ARM::ATOMIC_LOAD_MIN_I16:
6220 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6221 case ARM::ATOMIC_LOAD_MIN_I32:
6222 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6223
6224 case ARM::ATOMIC_LOAD_MAX_I8:
6225 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6226 case ARM::ATOMIC_LOAD_MAX_I16:
6227 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6228 case ARM::ATOMIC_LOAD_MAX_I32:
6229 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6230
6231 case ARM::ATOMIC_LOAD_UMIN_I8:
6232 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6233 case ARM::ATOMIC_LOAD_UMIN_I16:
6234 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6235 case ARM::ATOMIC_LOAD_UMIN_I32:
6236 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6237
6238 case ARM::ATOMIC_LOAD_UMAX_I8:
6239 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6240 case ARM::ATOMIC_LOAD_UMAX_I16:
6241 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6242 case ARM::ATOMIC_LOAD_UMAX_I32:
6243 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6244
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006245 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6246 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6247 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006248
6249 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6250 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6251 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006252
Eli Friedman2bdffe42011-08-31 00:31:29 +00006253
6254 case ARM::ATOMADD6432:
6255 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006256 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6257 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006258 case ARM::ATOMSUB6432:
6259 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006260 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6261 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006262 case ARM::ATOMOR6432:
6263 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006264 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006265 case ARM::ATOMXOR6432:
6266 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006267 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006268 case ARM::ATOMAND6432:
6269 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006270 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006271 case ARM::ATOMSWAP6432:
6272 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006273 case ARM::ATOMCMPXCHG6432:
6274 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6275 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6276 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006277
Evan Cheng007ea272009-08-12 05:17:19 +00006278 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006279 // To "insert" a SELECT_CC instruction, we actually have to insert the
6280 // diamond control-flow pattern. The incoming instruction knows the
6281 // destination vreg to set, the condition code register to branch on, the
6282 // true/false values to select between, and a branch opcode to use.
6283 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006284 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006285 ++It;
6286
6287 // thisMBB:
6288 // ...
6289 // TrueVal = ...
6290 // cmpTY ccX, r1, r2
6291 // bCC copy1MBB
6292 // fallthrough --> copy0MBB
6293 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006294 MachineFunction *F = BB->getParent();
6295 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6296 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006297 F->insert(It, copy0MBB);
6298 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006299
6300 // Transfer the remainder of BB and its successor edges to sinkMBB.
6301 sinkMBB->splice(sinkMBB->begin(), BB,
6302 llvm::next(MachineBasicBlock::iterator(MI)),
6303 BB->end());
6304 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6305
Dan Gohman258c58c2010-07-06 15:49:48 +00006306 BB->addSuccessor(copy0MBB);
6307 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006308
Dan Gohman14152b42010-07-06 20:24:04 +00006309 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6310 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6311
Evan Chenga8e29892007-01-19 07:51:42 +00006312 // copy0MBB:
6313 // %FalseValue = ...
6314 // # fallthrough to sinkMBB
6315 BB = copy0MBB;
6316
6317 // Update machine-CFG edges
6318 BB->addSuccessor(sinkMBB);
6319
6320 // sinkMBB:
6321 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6322 // ...
6323 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006324 BuildMI(*BB, BB->begin(), dl,
6325 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00006326 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6327 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6328
Dan Gohman14152b42010-07-06 20:24:04 +00006329 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00006330 return BB;
6331 }
Evan Cheng86198642009-08-07 00:34:42 +00006332
Evan Cheng218977b2010-07-13 19:27:42 +00006333 case ARM::BCCi64:
6334 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00006335 // If there is an unconditional branch to the other successor, remove it.
6336 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00006337
Evan Cheng218977b2010-07-13 19:27:42 +00006338 // Compare both parts that make up the double comparison separately for
6339 // equality.
6340 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
6341
6342 unsigned LHS1 = MI->getOperand(1).getReg();
6343 unsigned LHS2 = MI->getOperand(2).getReg();
6344 if (RHSisZero) {
6345 AddDefaultPred(BuildMI(BB, dl,
6346 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6347 .addReg(LHS1).addImm(0));
6348 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6349 .addReg(LHS2).addImm(0)
6350 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6351 } else {
6352 unsigned RHS1 = MI->getOperand(3).getReg();
6353 unsigned RHS2 = MI->getOperand(4).getReg();
6354 AddDefaultPred(BuildMI(BB, dl,
6355 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6356 .addReg(LHS1).addReg(RHS1));
6357 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6358 .addReg(LHS2).addReg(RHS2)
6359 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6360 }
6361
6362 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
6363 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
6364 if (MI->getOperand(0).getImm() == ARMCC::NE)
6365 std::swap(destMBB, exitMBB);
6366
6367 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6368 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00006369 if (isThumb2)
6370 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
6371 else
6372 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00006373
6374 MI->eraseFromParent(); // The pseudo instruction is gone now.
6375 return BB;
6376 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006377
Bill Wendling5bc85282011-10-17 20:37:20 +00006378 case ARM::Int_eh_sjlj_setjmp:
6379 case ARM::Int_eh_sjlj_setjmp_nofp:
6380 case ARM::tInt_eh_sjlj_setjmp:
6381 case ARM::t2Int_eh_sjlj_setjmp:
6382 case ARM::t2Int_eh_sjlj_setjmp_nofp:
6383 EmitSjLjDispatchBlock(MI, BB);
6384 return BB;
6385
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006386 case ARM::ABS:
6387 case ARM::t2ABS: {
6388 // To insert an ABS instruction, we have to insert the
6389 // diamond control-flow pattern. The incoming instruction knows the
6390 // source vreg to test against 0, the destination vreg to set,
6391 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006392 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006393 // It transforms
6394 // V1 = ABS V0
6395 // into
6396 // V2 = MOVS V0
6397 // BCC (branch to SinkBB if V0 >= 0)
6398 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006399 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006400 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6401 MachineFunction::iterator BBI = BB;
6402 ++BBI;
6403 MachineFunction *Fn = BB->getParent();
6404 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6405 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6406 Fn->insert(BBI, RSBBB);
6407 Fn->insert(BBI, SinkBB);
6408
6409 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
6410 unsigned int ABSDstReg = MI->getOperand(0).getReg();
6411 bool isThumb2 = Subtarget->isThumb2();
6412 MachineRegisterInfo &MRI = Fn->getRegInfo();
6413 // In Thumb mode S must not be specified if source register is the SP or
6414 // PC and if destination register is the SP, so restrict register class
6415 unsigned NewMovDstReg = MRI.createVirtualRegister(
6416 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6417 unsigned NewRsbDstReg = MRI.createVirtualRegister(
6418 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6419
6420 // Transfer the remainder of BB and its successor edges to sinkMBB.
6421 SinkBB->splice(SinkBB->begin(), BB,
6422 llvm::next(MachineBasicBlock::iterator(MI)),
6423 BB->end());
6424 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
6425
6426 BB->addSuccessor(RSBBB);
6427 BB->addSuccessor(SinkBB);
6428
6429 // fall through to SinkMBB
6430 RSBBB->addSuccessor(SinkBB);
6431
6432 // insert a movs at the end of BB
6433 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVr : ARM::MOVr),
6434 NewMovDstReg)
6435 .addReg(ABSSrcReg, RegState::Kill)
6436 .addImm((unsigned)ARMCC::AL).addReg(0)
6437 .addReg(ARM::CPSR, RegState::Define);
6438
6439 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006440 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006441 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
6442 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
6443
6444 // insert rsbri in RSBBB
6445 // Note: BCC and rsbri will be converted into predicated rsbmi
6446 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006447 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006448 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
6449 .addReg(NewMovDstReg, RegState::Kill)
6450 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
6451
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006452 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006453 // reuse ABSDstReg to not change uses of ABS instruction
6454 BuildMI(*SinkBB, SinkBB->begin(), dl,
6455 TII->get(ARM::PHI), ABSDstReg)
6456 .addReg(NewRsbDstReg).addMBB(RSBBB)
6457 .addReg(NewMovDstReg).addMBB(BB);
6458
6459 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006460 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006461
6462 // return last added BB
6463 return SinkBB;
6464 }
Evan Chenga8e29892007-01-19 07:51:42 +00006465 }
6466}
6467
Evan Cheng37fefc22011-08-30 19:09:48 +00006468void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
6469 SDNode *Node) const {
Andrew Trick90b7b122011-10-18 19:18:52 +00006470 const MCInstrDesc *MCID = &MI->getDesc();
6471 if (!MCID->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006472 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
6473 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
6474 return;
6475 }
6476
Andrew Trick4815d562011-09-20 03:17:40 +00006477 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
6478 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
6479 // operand is still set to noreg. If needed, set the optional operand's
6480 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00006481 //
Andrew Trick90b7b122011-10-18 19:18:52 +00006482 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00006483
Andrew Trick3be654f2011-09-21 02:20:46 +00006484 // Rename pseudo opcodes.
6485 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
6486 if (NewOpc) {
6487 const ARMBaseInstrInfo *TII =
6488 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00006489 MCID = &TII->get(NewOpc);
6490
6491 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
6492 "converted opcode should be the same except for cc_out");
6493
6494 MI->setDesc(*MCID);
6495
6496 // Add the optional cc_out operand
6497 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00006498 }
Andrew Trick90b7b122011-10-18 19:18:52 +00006499 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00006500
6501 // Any ARM instruction that sets the 's' bit should specify an optional
6502 // "cc_out" operand in the last operand position.
Andrew Trick90b7b122011-10-18 19:18:52 +00006503 if (!MCID->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006504 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006505 return;
6506 }
Andrew Trick3be654f2011-09-21 02:20:46 +00006507 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
6508 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006509 bool definesCPSR = false;
6510 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00006511 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00006512 i != e; ++i) {
6513 const MachineOperand &MO = MI->getOperand(i);
6514 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
6515 definesCPSR = true;
6516 if (MO.isDead())
6517 deadCPSR = true;
6518 MI->RemoveOperand(i);
6519 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00006520 }
6521 }
Andrew Trick4815d562011-09-20 03:17:40 +00006522 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006523 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006524 return;
6525 }
6526 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00006527 if (deadCPSR) {
6528 assert(!MI->getOperand(ccOutIdx).getReg() &&
6529 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00006530 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00006531 }
Andrew Trick4815d562011-09-20 03:17:40 +00006532
Andrew Trick3be654f2011-09-21 02:20:46 +00006533 // If this instruction was defined with an optional CPSR def and its dag node
6534 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006535 MachineOperand &MO = MI->getOperand(ccOutIdx);
6536 MO.setReg(ARM::CPSR);
6537 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00006538}
6539
Evan Chenga8e29892007-01-19 07:51:42 +00006540//===----------------------------------------------------------------------===//
6541// ARM Optimization Hooks
6542//===----------------------------------------------------------------------===//
6543
Chris Lattnerd1980a52009-03-12 06:52:53 +00006544static
6545SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
6546 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00006547 SelectionDAG &DAG = DCI.DAG;
6548 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00006549 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00006550 unsigned Opc = N->getOpcode();
6551 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
6552 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
6553 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
6554 ISD::CondCode CC = ISD::SETCC_INVALID;
6555
6556 if (isSlctCC) {
6557 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
6558 } else {
6559 SDValue CCOp = Slct.getOperand(0);
6560 if (CCOp.getOpcode() == ISD::SETCC)
6561 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
6562 }
6563
6564 bool DoXform = false;
6565 bool InvCC = false;
6566 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
6567 "Bad input!");
6568
6569 if (LHS.getOpcode() == ISD::Constant &&
6570 cast<ConstantSDNode>(LHS)->isNullValue()) {
6571 DoXform = true;
6572 } else if (CC != ISD::SETCC_INVALID &&
6573 RHS.getOpcode() == ISD::Constant &&
6574 cast<ConstantSDNode>(RHS)->isNullValue()) {
6575 std::swap(LHS, RHS);
6576 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006577 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00006578 Op0.getOperand(0).getValueType();
6579 bool isInt = OpVT.isInteger();
6580 CC = ISD::getSetCCInverse(CC, isInt);
6581
6582 if (!TLI.isCondCodeLegal(CC, OpVT))
6583 return SDValue(); // Inverse operator isn't legal.
6584
6585 DoXform = true;
6586 InvCC = true;
6587 }
6588
6589 if (DoXform) {
6590 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
6591 if (isSlctCC)
6592 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
6593 Slct.getOperand(0), Slct.getOperand(1), CC);
6594 SDValue CCOp = Slct.getOperand(0);
6595 if (InvCC)
6596 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
6597 CCOp.getOperand(0), CCOp.getOperand(1), CC);
6598 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
6599 CCOp, OtherOp, Result);
6600 }
6601 return SDValue();
6602}
6603
Eric Christopherfa6f5912011-06-29 21:10:36 +00006604// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00006605// (only after legalization).
6606static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
6607 TargetLowering::DAGCombinerInfo &DCI,
6608 const ARMSubtarget *Subtarget) {
6609
6610 // Only perform optimization if after legalize, and if NEON is available. We
6611 // also expected both operands to be BUILD_VECTORs.
6612 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
6613 || N0.getOpcode() != ISD::BUILD_VECTOR
6614 || N1.getOpcode() != ISD::BUILD_VECTOR)
6615 return SDValue();
6616
6617 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
6618 EVT VT = N->getValueType(0);
6619 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
6620 return SDValue();
6621
6622 // Check that the vector operands are of the right form.
6623 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
6624 // operands, where N is the size of the formed vector.
6625 // Each EXTRACT_VECTOR should have the same input vector and odd or even
6626 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00006627
6628 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00006629 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00006630 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00006631 SDValue Vec = N0->getOperand(0)->getOperand(0);
6632 SDNode *V = Vec.getNode();
6633 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00006634
Eric Christopherfa6f5912011-06-29 21:10:36 +00006635 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00006636 // check to see if each of their operands are an EXTRACT_VECTOR with
6637 // the same vector and appropriate index.
6638 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
6639 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
6640 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00006641
Tanya Lattner189531f2011-06-14 23:48:48 +00006642 SDValue ExtVec0 = N0->getOperand(i);
6643 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006644
Tanya Lattner189531f2011-06-14 23:48:48 +00006645 // First operand is the vector, verify its the same.
6646 if (V != ExtVec0->getOperand(0).getNode() ||
6647 V != ExtVec1->getOperand(0).getNode())
6648 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00006649
Tanya Lattner189531f2011-06-14 23:48:48 +00006650 // Second is the constant, verify its correct.
6651 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
6652 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00006653
Tanya Lattner189531f2011-06-14 23:48:48 +00006654 // For the constant, we want to see all the even or all the odd.
6655 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
6656 || C1->getZExtValue() != nextIndex+1)
6657 return SDValue();
6658
6659 // Increment index.
6660 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006661 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00006662 return SDValue();
6663 }
6664
6665 // Create VPADDL node.
6666 SelectionDAG &DAG = DCI.DAG;
6667 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00006668
6669 // Build operand list.
6670 SmallVector<SDValue, 8> Ops;
6671 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
6672 TLI.getPointerTy()));
6673
6674 // Input is the vector.
6675 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006676
Tanya Lattner189531f2011-06-14 23:48:48 +00006677 // Get widened type and narrowed type.
6678 MVT widenType;
6679 unsigned numElem = VT.getVectorNumElements();
6680 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
6681 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
6682 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
6683 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
6684 default:
6685 assert(0 && "Invalid vector element type for padd optimization.");
6686 }
6687
6688 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6689 widenType, &Ops[0], Ops.size());
6690 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
6691}
6692
Bob Wilson3d5792a2010-07-29 20:34:14 +00006693/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
6694/// operands N0 and N1. This is a helper for PerformADDCombine that is
6695/// called with the default operands, and if that fails, with commuted
6696/// operands.
6697static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00006698 TargetLowering::DAGCombinerInfo &DCI,
6699 const ARMSubtarget *Subtarget){
6700
6701 // Attempt to create vpaddl for this add.
6702 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
6703 if (Result.getNode())
6704 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006705
Chris Lattnerd1980a52009-03-12 06:52:53 +00006706 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
6707 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
6708 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
6709 if (Result.getNode()) return Result;
6710 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00006711 return SDValue();
6712}
6713
Bob Wilson3d5792a2010-07-29 20:34:14 +00006714/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
6715///
6716static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00006717 TargetLowering::DAGCombinerInfo &DCI,
6718 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006719 SDValue N0 = N->getOperand(0);
6720 SDValue N1 = N->getOperand(1);
6721
6722 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00006723 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006724 if (Result.getNode())
6725 return Result;
6726
6727 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00006728 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006729}
6730
Chris Lattnerd1980a52009-03-12 06:52:53 +00006731/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00006732///
Chris Lattnerd1980a52009-03-12 06:52:53 +00006733static SDValue PerformSUBCombine(SDNode *N,
6734 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006735 SDValue N0 = N->getOperand(0);
6736 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00006737
Chris Lattnerd1980a52009-03-12 06:52:53 +00006738 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
6739 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
6740 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
6741 if (Result.getNode()) return Result;
6742 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00006743
Chris Lattnerd1980a52009-03-12 06:52:53 +00006744 return SDValue();
6745}
6746
Evan Cheng463d3582011-03-31 19:38:48 +00006747/// PerformVMULCombine
6748/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
6749/// special multiplier accumulator forwarding.
6750/// vmul d3, d0, d2
6751/// vmla d3, d1, d2
6752/// is faster than
6753/// vadd d3, d0, d1
6754/// vmul d3, d3, d2
6755static SDValue PerformVMULCombine(SDNode *N,
6756 TargetLowering::DAGCombinerInfo &DCI,
6757 const ARMSubtarget *Subtarget) {
6758 if (!Subtarget->hasVMLxForwarding())
6759 return SDValue();
6760
6761 SelectionDAG &DAG = DCI.DAG;
6762 SDValue N0 = N->getOperand(0);
6763 SDValue N1 = N->getOperand(1);
6764 unsigned Opcode = N0.getOpcode();
6765 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6766 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00006767 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00006768 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6769 Opcode != ISD::FADD && Opcode != ISD::FSUB)
6770 return SDValue();
6771 std::swap(N0, N1);
6772 }
6773
6774 EVT VT = N->getValueType(0);
6775 DebugLoc DL = N->getDebugLoc();
6776 SDValue N00 = N0->getOperand(0);
6777 SDValue N01 = N0->getOperand(1);
6778 return DAG.getNode(Opcode, DL, VT,
6779 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
6780 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
6781}
6782
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006783static SDValue PerformMULCombine(SDNode *N,
6784 TargetLowering::DAGCombinerInfo &DCI,
6785 const ARMSubtarget *Subtarget) {
6786 SelectionDAG &DAG = DCI.DAG;
6787
6788 if (Subtarget->isThumb1Only())
6789 return SDValue();
6790
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006791 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6792 return SDValue();
6793
6794 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00006795 if (VT.is64BitVector() || VT.is128BitVector())
6796 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006797 if (VT != MVT::i32)
6798 return SDValue();
6799
6800 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
6801 if (!C)
6802 return SDValue();
6803
6804 uint64_t MulAmt = C->getZExtValue();
6805 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
6806 ShiftAmt = ShiftAmt & (32 - 1);
6807 SDValue V = N->getOperand(0);
6808 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006809
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006810 SDValue Res;
6811 MulAmt >>= ShiftAmt;
6812 if (isPowerOf2_32(MulAmt - 1)) {
6813 // (mul x, 2^N + 1) => (add (shl x, N), x)
6814 Res = DAG.getNode(ISD::ADD, DL, VT,
6815 V, DAG.getNode(ISD::SHL, DL, VT,
6816 V, DAG.getConstant(Log2_32(MulAmt-1),
6817 MVT::i32)));
6818 } else if (isPowerOf2_32(MulAmt + 1)) {
6819 // (mul x, 2^N - 1) => (sub (shl x, N), x)
6820 Res = DAG.getNode(ISD::SUB, DL, VT,
6821 DAG.getNode(ISD::SHL, DL, VT,
6822 V, DAG.getConstant(Log2_32(MulAmt+1),
6823 MVT::i32)),
6824 V);
6825 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006826 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006827
6828 if (ShiftAmt != 0)
6829 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
6830 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006831
6832 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006833 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006834 return SDValue();
6835}
6836
Owen Anderson080c0922010-11-05 19:27:46 +00006837static SDValue PerformANDCombine(SDNode *N,
6838 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00006839
Owen Anderson080c0922010-11-05 19:27:46 +00006840 // Attempt to use immediate-form VBIC
6841 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6842 DebugLoc dl = N->getDebugLoc();
6843 EVT VT = N->getValueType(0);
6844 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006845
Tanya Lattner0433b212011-04-07 15:24:20 +00006846 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6847 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006848
Owen Anderson080c0922010-11-05 19:27:46 +00006849 APInt SplatBits, SplatUndef;
6850 unsigned SplatBitSize;
6851 bool HasAnyUndefs;
6852 if (BVN &&
6853 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6854 if (SplatBitSize <= 64) {
6855 EVT VbicVT;
6856 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
6857 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006858 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006859 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00006860 if (Val.getNode()) {
6861 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006862 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00006863 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006864 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00006865 }
6866 }
6867 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006868
Owen Anderson080c0922010-11-05 19:27:46 +00006869 return SDValue();
6870}
6871
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006872/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
6873static SDValue PerformORCombine(SDNode *N,
6874 TargetLowering::DAGCombinerInfo &DCI,
6875 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00006876 // Attempt to use immediate-form VORR
6877 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6878 DebugLoc dl = N->getDebugLoc();
6879 EVT VT = N->getValueType(0);
6880 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006881
Tanya Lattner0433b212011-04-07 15:24:20 +00006882 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6883 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006884
Owen Anderson60f48702010-11-03 23:15:26 +00006885 APInt SplatBits, SplatUndef;
6886 unsigned SplatBitSize;
6887 bool HasAnyUndefs;
6888 if (BVN && Subtarget->hasNEON() &&
6889 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6890 if (SplatBitSize <= 64) {
6891 EVT VorrVT;
6892 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
6893 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006894 DAG, VorrVT, VT.is128BitVector(),
6895 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00006896 if (Val.getNode()) {
6897 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006898 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00006899 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006900 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00006901 }
6902 }
6903 }
6904
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006905 SDValue N0 = N->getOperand(0);
6906 if (N0.getOpcode() != ISD::AND)
6907 return SDValue();
6908 SDValue N1 = N->getOperand(1);
6909
6910 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
6911 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
6912 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
6913 APInt SplatUndef;
6914 unsigned SplatBitSize;
6915 bool HasAnyUndefs;
6916
6917 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
6918 APInt SplatBits0;
6919 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
6920 HasAnyUndefs) && !HasAnyUndefs) {
6921 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
6922 APInt SplatBits1;
6923 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
6924 HasAnyUndefs) && !HasAnyUndefs &&
6925 SplatBits0 == ~SplatBits1) {
6926 // Canonicalize the vector type to make instruction selection simpler.
6927 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
6928 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
6929 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00006930 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006931 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
6932 }
6933 }
6934 }
6935
Jim Grosbach54238562010-07-17 03:30:54 +00006936 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
6937 // reasonable.
6938
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006939 // BFI is only available on V6T2+
6940 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
6941 return SDValue();
6942
Jim Grosbach54238562010-07-17 03:30:54 +00006943 DebugLoc DL = N->getDebugLoc();
6944 // 1) or (and A, mask), val => ARMbfi A, val, mask
6945 // iff (val & mask) == val
6946 //
6947 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
6948 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00006949 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00006950 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00006951 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00006952 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006953
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006954 if (VT != MVT::i32)
6955 return SDValue();
6956
Evan Cheng30fb13f2010-12-13 20:32:54 +00006957 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00006958
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006959 // The value and the mask need to be constants so we can verify this is
6960 // actually a bitfield set. If the mask is 0xffff, we can do better
6961 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00006962 SDValue MaskOp = N0.getOperand(1);
6963 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
6964 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006965 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00006966 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006967 if (Mask == 0xffff)
6968 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006969 SDValue Res;
6970 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00006971 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
6972 if (N1C) {
6973 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00006974 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00006975 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006976
Evan Chenga9688c42010-12-11 04:11:38 +00006977 if (ARM::isBitFieldInvertedMask(Mask)) {
6978 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006979
Evan Cheng30fb13f2010-12-13 20:32:54 +00006980 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00006981 DAG.getConstant(Val, MVT::i32),
6982 DAG.getConstant(Mask, MVT::i32));
6983
6984 // Do not add new nodes to DAG combiner worklist.
6985 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006986 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00006987 }
Jim Grosbach54238562010-07-17 03:30:54 +00006988 } else if (N1.getOpcode() == ISD::AND) {
6989 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00006990 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
6991 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00006992 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00006993 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006994
Eric Christopher29aeed12011-03-26 01:21:03 +00006995 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
6996 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00006997 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00006998 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00006999 // The pack halfword instruction works better for masks that fit it,
7000 // so use that when it's available.
7001 if (Subtarget->hasT2ExtractPack() &&
7002 (Mask == 0xffff || Mask == 0xffff0000))
7003 return SDValue();
7004 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00007005 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00007006 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00007007 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00007008 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00007009 DAG.getConstant(Mask, MVT::i32));
7010 // Do not add new nodes to DAG combiner worklist.
7011 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007012 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007013 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007014 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007015 // The pack halfword instruction works better for masks that fit it,
7016 // so use that when it's available.
7017 if (Subtarget->hasT2ExtractPack() &&
7018 (Mask2 == 0xffff || Mask2 == 0xffff0000))
7019 return SDValue();
7020 // 2b
7021 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007022 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00007023 DAG.getConstant(lsb, MVT::i32));
7024 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00007025 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00007026 // Do not add new nodes to DAG combiner worklist.
7027 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007028 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007029 }
7030 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007031
Evan Cheng30fb13f2010-12-13 20:32:54 +00007032 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
7033 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
7034 ARM::isBitFieldInvertedMask(~Mask)) {
7035 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
7036 // where lsb(mask) == #shamt and masked bits of B are known zero.
7037 SDValue ShAmt = N00.getOperand(1);
7038 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
7039 unsigned LSB = CountTrailingZeros_32(Mask);
7040 if (ShAmtC != LSB)
7041 return SDValue();
7042
7043 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
7044 DAG.getConstant(~Mask, MVT::i32));
7045
7046 // Do not add new nodes to DAG combiner worklist.
7047 DCI.CombineTo(N, Res, false);
7048 }
7049
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007050 return SDValue();
7051}
7052
Evan Chengbf188ae2011-06-15 01:12:31 +00007053/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
7054/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00007055static SDValue PerformBFICombine(SDNode *N,
7056 TargetLowering::DAGCombinerInfo &DCI) {
7057 SDValue N1 = N->getOperand(1);
7058 if (N1.getOpcode() == ISD::AND) {
7059 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7060 if (!N11C)
7061 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007062 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
7063 unsigned LSB = CountTrailingZeros_32(~InvMask);
7064 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
7065 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00007066 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007067 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00007068 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
7069 N->getOperand(0), N1.getOperand(0),
7070 N->getOperand(2));
7071 }
7072 return SDValue();
7073}
7074
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007075/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
7076/// ARMISD::VMOVRRD.
7077static SDValue PerformVMOVRRDCombine(SDNode *N,
7078 TargetLowering::DAGCombinerInfo &DCI) {
7079 // vmovrrd(vmovdrr x, y) -> x,y
7080 SDValue InDouble = N->getOperand(0);
7081 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
7082 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00007083
7084 // vmovrrd(load f64) -> (load i32), (load i32)
7085 SDNode *InNode = InDouble.getNode();
7086 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
7087 InNode->getValueType(0) == MVT::f64 &&
7088 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
7089 !cast<LoadSDNode>(InNode)->isVolatile()) {
7090 // TODO: Should this be done for non-FrameIndex operands?
7091 LoadSDNode *LD = cast<LoadSDNode>(InNode);
7092
7093 SelectionDAG &DAG = DCI.DAG;
7094 DebugLoc DL = LD->getDebugLoc();
7095 SDValue BasePtr = LD->getBasePtr();
7096 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
7097 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007098 LD->isNonTemporal(), LD->isInvariant(),
7099 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00007100
7101 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7102 DAG.getConstant(4, MVT::i32));
7103 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
7104 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007105 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00007106 std::min(4U, LD->getAlignment() / 2));
7107
7108 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
7109 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
7110 DCI.RemoveFromWorklist(LD);
7111 DAG.DeleteNode(LD);
7112 return Result;
7113 }
7114
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007115 return SDValue();
7116}
7117
7118/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
7119/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
7120static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
7121 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
7122 SDValue Op0 = N->getOperand(0);
7123 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007124 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007125 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007126 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007127 Op1 = Op1.getOperand(0);
7128 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
7129 Op0.getNode() == Op1.getNode() &&
7130 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007131 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007132 N->getValueType(0), Op0.getOperand(0));
7133 return SDValue();
7134}
7135
Bob Wilson31600902010-12-21 06:43:19 +00007136/// PerformSTORECombine - Target-specific dag combine xforms for
7137/// ISD::STORE.
7138static SDValue PerformSTORECombine(SDNode *N,
7139 TargetLowering::DAGCombinerInfo &DCI) {
7140 // Bitcast an i64 store extracted from a vector to f64.
7141 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7142 StoreSDNode *St = cast<StoreSDNode>(N);
7143 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007144 if (!ISD::isNormalStore(St) || St->isVolatile())
7145 return SDValue();
7146
7147 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
7148 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
7149 SelectionDAG &DAG = DCI.DAG;
7150 DebugLoc DL = St->getDebugLoc();
7151 SDValue BasePtr = St->getBasePtr();
7152 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
7153 StVal.getNode()->getOperand(0), BasePtr,
7154 St->getPointerInfo(), St->isVolatile(),
7155 St->isNonTemporal(), St->getAlignment());
7156
7157 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7158 DAG.getConstant(4, MVT::i32));
7159 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
7160 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
7161 St->isNonTemporal(),
7162 std::min(4U, St->getAlignment() / 2));
7163 }
7164
7165 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00007166 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
7167 return SDValue();
7168
7169 SelectionDAG &DAG = DCI.DAG;
7170 DebugLoc dl = StVal.getDebugLoc();
7171 SDValue IntVec = StVal.getOperand(0);
7172 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7173 IntVec.getValueType().getVectorNumElements());
7174 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
7175 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
7176 Vec, StVal.getOperand(1));
7177 dl = N->getDebugLoc();
7178 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
7179 // Make the DAGCombiner fold the bitcasts.
7180 DCI.AddToWorklist(Vec.getNode());
7181 DCI.AddToWorklist(ExtElt.getNode());
7182 DCI.AddToWorklist(V.getNode());
7183 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
7184 St->getPointerInfo(), St->isVolatile(),
7185 St->isNonTemporal(), St->getAlignment(),
7186 St->getTBAAInfo());
7187}
7188
7189/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
7190/// are normal, non-volatile loads. If so, it is profitable to bitcast an
7191/// i64 vector to have f64 elements, since the value can then be loaded
7192/// directly into a VFP register.
7193static bool hasNormalLoadOperand(SDNode *N) {
7194 unsigned NumElts = N->getValueType(0).getVectorNumElements();
7195 for (unsigned i = 0; i < NumElts; ++i) {
7196 SDNode *Elt = N->getOperand(i).getNode();
7197 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
7198 return true;
7199 }
7200 return false;
7201}
7202
Bob Wilson75f02882010-09-17 22:59:05 +00007203/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
7204/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00007205static SDValue PerformBUILD_VECTORCombine(SDNode *N,
7206 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00007207 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
7208 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
7209 // into a pair of GPRs, which is fine when the value is used as a scalar,
7210 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00007211 SelectionDAG &DAG = DCI.DAG;
7212 if (N->getNumOperands() == 2) {
7213 SDValue RV = PerformVMOVDRRCombine(N, DAG);
7214 if (RV.getNode())
7215 return RV;
7216 }
Bob Wilson75f02882010-09-17 22:59:05 +00007217
Bob Wilson31600902010-12-21 06:43:19 +00007218 // Load i64 elements as f64 values so that type legalization does not split
7219 // them up into i32 values.
7220 EVT VT = N->getValueType(0);
7221 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
7222 return SDValue();
7223 DebugLoc dl = N->getDebugLoc();
7224 SmallVector<SDValue, 8> Ops;
7225 unsigned NumElts = VT.getVectorNumElements();
7226 for (unsigned i = 0; i < NumElts; ++i) {
7227 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
7228 Ops.push_back(V);
7229 // Make the DAGCombiner fold the bitcast.
7230 DCI.AddToWorklist(V.getNode());
7231 }
7232 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
7233 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
7234 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
7235}
7236
7237/// PerformInsertEltCombine - Target-specific dag combine xforms for
7238/// ISD::INSERT_VECTOR_ELT.
7239static SDValue PerformInsertEltCombine(SDNode *N,
7240 TargetLowering::DAGCombinerInfo &DCI) {
7241 // Bitcast an i64 load inserted into a vector to f64.
7242 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7243 EVT VT = N->getValueType(0);
7244 SDNode *Elt = N->getOperand(1).getNode();
7245 if (VT.getVectorElementType() != MVT::i64 ||
7246 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
7247 return SDValue();
7248
7249 SelectionDAG &DAG = DCI.DAG;
7250 DebugLoc dl = N->getDebugLoc();
7251 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7252 VT.getVectorNumElements());
7253 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
7254 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
7255 // Make the DAGCombiner fold the bitcasts.
7256 DCI.AddToWorklist(Vec.getNode());
7257 DCI.AddToWorklist(V.getNode());
7258 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
7259 Vec, V, N->getOperand(2));
7260 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00007261}
7262
Bob Wilsonf20700c2010-10-27 20:38:28 +00007263/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
7264/// ISD::VECTOR_SHUFFLE.
7265static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
7266 // The LLVM shufflevector instruction does not require the shuffle mask
7267 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
7268 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
7269 // operands do not match the mask length, they are extended by concatenating
7270 // them with undef vectors. That is probably the right thing for other
7271 // targets, but for NEON it is better to concatenate two double-register
7272 // size vector operands into a single quad-register size vector. Do that
7273 // transformation here:
7274 // shuffle(concat(v1, undef), concat(v2, undef)) ->
7275 // shuffle(concat(v1, v2), undef)
7276 SDValue Op0 = N->getOperand(0);
7277 SDValue Op1 = N->getOperand(1);
7278 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
7279 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
7280 Op0.getNumOperands() != 2 ||
7281 Op1.getNumOperands() != 2)
7282 return SDValue();
7283 SDValue Concat0Op1 = Op0.getOperand(1);
7284 SDValue Concat1Op1 = Op1.getOperand(1);
7285 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
7286 Concat1Op1.getOpcode() != ISD::UNDEF)
7287 return SDValue();
7288 // Skip the transformation if any of the types are illegal.
7289 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7290 EVT VT = N->getValueType(0);
7291 if (!TLI.isTypeLegal(VT) ||
7292 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
7293 !TLI.isTypeLegal(Concat1Op1.getValueType()))
7294 return SDValue();
7295
7296 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
7297 Op0.getOperand(0), Op1.getOperand(0));
7298 // Translate the shuffle mask.
7299 SmallVector<int, 16> NewMask;
7300 unsigned NumElts = VT.getVectorNumElements();
7301 unsigned HalfElts = NumElts/2;
7302 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7303 for (unsigned n = 0; n < NumElts; ++n) {
7304 int MaskElt = SVN->getMaskElt(n);
7305 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007306 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00007307 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007308 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00007309 NewElt = HalfElts + MaskElt - NumElts;
7310 NewMask.push_back(NewElt);
7311 }
7312 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
7313 DAG.getUNDEF(VT), NewMask.data());
7314}
7315
Bob Wilson1c3ef902011-02-07 17:43:21 +00007316/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
7317/// NEON load/store intrinsics to merge base address updates.
7318static SDValue CombineBaseUpdate(SDNode *N,
7319 TargetLowering::DAGCombinerInfo &DCI) {
7320 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7321 return SDValue();
7322
7323 SelectionDAG &DAG = DCI.DAG;
7324 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
7325 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
7326 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
7327 SDValue Addr = N->getOperand(AddrOpIdx);
7328
7329 // Search for a use of the address operand that is an increment.
7330 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
7331 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
7332 SDNode *User = *UI;
7333 if (User->getOpcode() != ISD::ADD ||
7334 UI.getUse().getResNo() != Addr.getResNo())
7335 continue;
7336
7337 // Check that the add is independent of the load/store. Otherwise, folding
7338 // it would create a cycle.
7339 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
7340 continue;
7341
7342 // Find the new opcode for the updating load/store.
7343 bool isLoad = true;
7344 bool isLaneOp = false;
7345 unsigned NewOpc = 0;
7346 unsigned NumVecs = 0;
7347 if (isIntrinsic) {
7348 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
7349 switch (IntNo) {
7350 default: assert(0 && "unexpected intrinsic for Neon base update");
7351 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
7352 NumVecs = 1; break;
7353 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
7354 NumVecs = 2; break;
7355 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
7356 NumVecs = 3; break;
7357 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
7358 NumVecs = 4; break;
7359 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
7360 NumVecs = 2; isLaneOp = true; break;
7361 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
7362 NumVecs = 3; isLaneOp = true; break;
7363 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
7364 NumVecs = 4; isLaneOp = true; break;
7365 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
7366 NumVecs = 1; isLoad = false; break;
7367 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
7368 NumVecs = 2; isLoad = false; break;
7369 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
7370 NumVecs = 3; isLoad = false; break;
7371 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
7372 NumVecs = 4; isLoad = false; break;
7373 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
7374 NumVecs = 2; isLoad = false; isLaneOp = true; break;
7375 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
7376 NumVecs = 3; isLoad = false; isLaneOp = true; break;
7377 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
7378 NumVecs = 4; isLoad = false; isLaneOp = true; break;
7379 }
7380 } else {
7381 isLaneOp = true;
7382 switch (N->getOpcode()) {
7383 default: assert(0 && "unexpected opcode for Neon base update");
7384 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
7385 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
7386 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
7387 }
7388 }
7389
7390 // Find the size of memory referenced by the load/store.
7391 EVT VecTy;
7392 if (isLoad)
7393 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00007394 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00007395 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
7396 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
7397 if (isLaneOp)
7398 NumBytes /= VecTy.getVectorNumElements();
7399
7400 // If the increment is a constant, it must match the memory ref size.
7401 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
7402 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
7403 uint64_t IncVal = CInc->getZExtValue();
7404 if (IncVal != NumBytes)
7405 continue;
7406 } else if (NumBytes >= 3 * 16) {
7407 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
7408 // separate instructions that make it harder to use a non-constant update.
7409 continue;
7410 }
7411
7412 // Create the new updating load/store node.
7413 EVT Tys[6];
7414 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
7415 unsigned n;
7416 for (n = 0; n < NumResultVecs; ++n)
7417 Tys[n] = VecTy;
7418 Tys[n++] = MVT::i32;
7419 Tys[n] = MVT::Other;
7420 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
7421 SmallVector<SDValue, 8> Ops;
7422 Ops.push_back(N->getOperand(0)); // incoming chain
7423 Ops.push_back(N->getOperand(AddrOpIdx));
7424 Ops.push_back(Inc);
7425 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
7426 Ops.push_back(N->getOperand(i));
7427 }
7428 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
7429 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
7430 Ops.data(), Ops.size(),
7431 MemInt->getMemoryVT(),
7432 MemInt->getMemOperand());
7433
7434 // Update the uses.
7435 std::vector<SDValue> NewResults;
7436 for (unsigned i = 0; i < NumResultVecs; ++i) {
7437 NewResults.push_back(SDValue(UpdN.getNode(), i));
7438 }
7439 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
7440 DCI.CombineTo(N, NewResults);
7441 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
7442
7443 break;
Owen Anderson76706012011-04-05 21:48:57 +00007444 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00007445 return SDValue();
7446}
7447
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007448/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
7449/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
7450/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
7451/// return true.
7452static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
7453 SelectionDAG &DAG = DCI.DAG;
7454 EVT VT = N->getValueType(0);
7455 // vldN-dup instructions only support 64-bit vectors for N > 1.
7456 if (!VT.is64BitVector())
7457 return false;
7458
7459 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
7460 SDNode *VLD = N->getOperand(0).getNode();
7461 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
7462 return false;
7463 unsigned NumVecs = 0;
7464 unsigned NewOpc = 0;
7465 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
7466 if (IntNo == Intrinsic::arm_neon_vld2lane) {
7467 NumVecs = 2;
7468 NewOpc = ARMISD::VLD2DUP;
7469 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
7470 NumVecs = 3;
7471 NewOpc = ARMISD::VLD3DUP;
7472 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
7473 NumVecs = 4;
7474 NewOpc = ARMISD::VLD4DUP;
7475 } else {
7476 return false;
7477 }
7478
7479 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
7480 // numbers match the load.
7481 unsigned VLDLaneNo =
7482 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
7483 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7484 UI != UE; ++UI) {
7485 // Ignore uses of the chain result.
7486 if (UI.getUse().getResNo() == NumVecs)
7487 continue;
7488 SDNode *User = *UI;
7489 if (User->getOpcode() != ARMISD::VDUPLANE ||
7490 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
7491 return false;
7492 }
7493
7494 // Create the vldN-dup node.
7495 EVT Tys[5];
7496 unsigned n;
7497 for (n = 0; n < NumVecs; ++n)
7498 Tys[n] = VT;
7499 Tys[n] = MVT::Other;
7500 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
7501 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
7502 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
7503 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
7504 Ops, 2, VLDMemInt->getMemoryVT(),
7505 VLDMemInt->getMemOperand());
7506
7507 // Update the uses.
7508 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7509 UI != UE; ++UI) {
7510 unsigned ResNo = UI.getUse().getResNo();
7511 // Ignore uses of the chain result.
7512 if (ResNo == NumVecs)
7513 continue;
7514 SDNode *User = *UI;
7515 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
7516 }
7517
7518 // Now the vldN-lane intrinsic is dead except for its chain result.
7519 // Update uses of the chain.
7520 std::vector<SDValue> VLDDupResults;
7521 for (unsigned n = 0; n < NumVecs; ++n)
7522 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
7523 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
7524 DCI.CombineTo(VLD, VLDDupResults);
7525
7526 return true;
7527}
7528
Bob Wilson9e82bf12010-07-14 01:22:12 +00007529/// PerformVDUPLANECombine - Target-specific dag combine xforms for
7530/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007531static SDValue PerformVDUPLANECombine(SDNode *N,
7532 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00007533 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007534
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007535 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
7536 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
7537 if (CombineVLDDUP(N, DCI))
7538 return SDValue(N, 0);
7539
7540 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
7541 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007542 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007543 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00007544 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007545 return SDValue();
7546
7547 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
7548 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
7549 // The canonical VMOV for a zero vector uses a 32-bit element size.
7550 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7551 unsigned EltBits;
7552 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
7553 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007554 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007555 if (EltSize > VT.getVectorElementType().getSizeInBits())
7556 return SDValue();
7557
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007558 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007559}
7560
Eric Christopherfa6f5912011-06-29 21:10:36 +00007561// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00007562// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
7563static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
7564{
Chad Rosier118c9a02011-06-28 17:26:57 +00007565 integerPart cN;
7566 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00007567 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
7568 I != E; I++) {
7569 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
7570 if (!C)
7571 return false;
7572
Eric Christopherfa6f5912011-06-29 21:10:36 +00007573 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00007574 APFloat APF = C->getValueAPF();
7575 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
7576 != APFloat::opOK || !isExact)
7577 return false;
7578
7579 c0 = (I == 0) ? cN : c0;
7580 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
7581 return false;
7582 }
7583 C = c0;
7584 return true;
7585}
7586
7587/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
7588/// can replace combinations of VMUL and VCVT (floating-point to integer)
7589/// when the VMUL has a constant operand that is a power of 2.
7590///
7591/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7592/// vmul.f32 d16, d17, d16
7593/// vcvt.s32.f32 d16, d16
7594/// becomes:
7595/// vcvt.s32.f32 d16, d16, #3
7596static SDValue PerformVCVTCombine(SDNode *N,
7597 TargetLowering::DAGCombinerInfo &DCI,
7598 const ARMSubtarget *Subtarget) {
7599 SelectionDAG &DAG = DCI.DAG;
7600 SDValue Op = N->getOperand(0);
7601
7602 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
7603 Op.getOpcode() != ISD::FMUL)
7604 return SDValue();
7605
7606 uint64_t C;
7607 SDValue N0 = Op->getOperand(0);
7608 SDValue ConstVec = Op->getOperand(1);
7609 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
7610
Eric Christopherfa6f5912011-06-29 21:10:36 +00007611 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00007612 !isConstVecPow2(ConstVec, isSigned, C))
7613 return SDValue();
7614
7615 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
7616 Intrinsic::arm_neon_vcvtfp2fxu;
7617 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7618 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007619 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00007620 DAG.getConstant(Log2_64(C), MVT::i32));
7621}
7622
7623/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
7624/// can replace combinations of VCVT (integer to floating-point) and VDIV
7625/// when the VDIV has a constant operand that is a power of 2.
7626///
7627/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7628/// vcvt.f32.s32 d16, d16
7629/// vdiv.f32 d16, d17, d16
7630/// becomes:
7631/// vcvt.f32.s32 d16, d16, #3
7632static SDValue PerformVDIVCombine(SDNode *N,
7633 TargetLowering::DAGCombinerInfo &DCI,
7634 const ARMSubtarget *Subtarget) {
7635 SelectionDAG &DAG = DCI.DAG;
7636 SDValue Op = N->getOperand(0);
7637 unsigned OpOpcode = Op.getNode()->getOpcode();
7638
7639 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
7640 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
7641 return SDValue();
7642
7643 uint64_t C;
7644 SDValue ConstVec = N->getOperand(1);
7645 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
7646
7647 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
7648 !isConstVecPow2(ConstVec, isSigned, C))
7649 return SDValue();
7650
Eric Christopherfa6f5912011-06-29 21:10:36 +00007651 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00007652 Intrinsic::arm_neon_vcvtfxu2fp;
7653 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7654 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007655 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00007656 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
7657}
7658
7659/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00007660/// operand of a vector shift operation, where all the elements of the
7661/// build_vector must have the same constant integer value.
7662static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
7663 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007664 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00007665 Op = Op.getOperand(0);
7666 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
7667 APInt SplatBits, SplatUndef;
7668 unsigned SplatBitSize;
7669 bool HasAnyUndefs;
7670 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
7671 HasAnyUndefs, ElementBits) ||
7672 SplatBitSize > ElementBits)
7673 return false;
7674 Cnt = SplatBits.getSExtValue();
7675 return true;
7676}
7677
7678/// isVShiftLImm - Check if this is a valid build_vector for the immediate
7679/// operand of a vector shift left operation. That value must be in the range:
7680/// 0 <= Value < ElementBits for a left shift; or
7681/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007682static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007683 assert(VT.isVector() && "vector shift count is not a vector type");
7684 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7685 if (! getVShiftImm(Op, ElementBits, Cnt))
7686 return false;
7687 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
7688}
7689
7690/// isVShiftRImm - Check if this is a valid build_vector for the immediate
7691/// operand of a vector shift right operation. For a shift opcode, the value
7692/// is positive, but for an intrinsic the value count must be negative. The
7693/// absolute value must be in the range:
7694/// 1 <= |Value| <= ElementBits for a right shift; or
7695/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007696static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00007697 int64_t &Cnt) {
7698 assert(VT.isVector() && "vector shift count is not a vector type");
7699 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7700 if (! getVShiftImm(Op, ElementBits, Cnt))
7701 return false;
7702 if (isIntrinsic)
7703 Cnt = -Cnt;
7704 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
7705}
7706
7707/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
7708static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
7709 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
7710 switch (IntNo) {
7711 default:
7712 // Don't do anything for most intrinsics.
7713 break;
7714
7715 // Vector shifts: check for immediate versions and lower them.
7716 // Note: This is done during DAG combining instead of DAG legalizing because
7717 // the build_vectors for 64-bit vector element shift counts are generally
7718 // not legal, and it is hard to see their values after they get legalized to
7719 // loads from a constant pool.
7720 case Intrinsic::arm_neon_vshifts:
7721 case Intrinsic::arm_neon_vshiftu:
7722 case Intrinsic::arm_neon_vshiftls:
7723 case Intrinsic::arm_neon_vshiftlu:
7724 case Intrinsic::arm_neon_vshiftn:
7725 case Intrinsic::arm_neon_vrshifts:
7726 case Intrinsic::arm_neon_vrshiftu:
7727 case Intrinsic::arm_neon_vrshiftn:
7728 case Intrinsic::arm_neon_vqshifts:
7729 case Intrinsic::arm_neon_vqshiftu:
7730 case Intrinsic::arm_neon_vqshiftsu:
7731 case Intrinsic::arm_neon_vqshiftns:
7732 case Intrinsic::arm_neon_vqshiftnu:
7733 case Intrinsic::arm_neon_vqshiftnsu:
7734 case Intrinsic::arm_neon_vqrshiftns:
7735 case Intrinsic::arm_neon_vqrshiftnu:
7736 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00007737 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007738 int64_t Cnt;
7739 unsigned VShiftOpc = 0;
7740
7741 switch (IntNo) {
7742 case Intrinsic::arm_neon_vshifts:
7743 case Intrinsic::arm_neon_vshiftu:
7744 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
7745 VShiftOpc = ARMISD::VSHL;
7746 break;
7747 }
7748 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
7749 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
7750 ARMISD::VSHRs : ARMISD::VSHRu);
7751 break;
7752 }
7753 return SDValue();
7754
7755 case Intrinsic::arm_neon_vshiftls:
7756 case Intrinsic::arm_neon_vshiftlu:
7757 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
7758 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007759 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007760
7761 case Intrinsic::arm_neon_vrshifts:
7762 case Intrinsic::arm_neon_vrshiftu:
7763 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
7764 break;
7765 return SDValue();
7766
7767 case Intrinsic::arm_neon_vqshifts:
7768 case Intrinsic::arm_neon_vqshiftu:
7769 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7770 break;
7771 return SDValue();
7772
7773 case Intrinsic::arm_neon_vqshiftsu:
7774 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7775 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007776 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007777
7778 case Intrinsic::arm_neon_vshiftn:
7779 case Intrinsic::arm_neon_vrshiftn:
7780 case Intrinsic::arm_neon_vqshiftns:
7781 case Intrinsic::arm_neon_vqshiftnu:
7782 case Intrinsic::arm_neon_vqshiftnsu:
7783 case Intrinsic::arm_neon_vqrshiftns:
7784 case Intrinsic::arm_neon_vqrshiftnu:
7785 case Intrinsic::arm_neon_vqrshiftnsu:
7786 // Narrowing shifts require an immediate right shift.
7787 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
7788 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00007789 llvm_unreachable("invalid shift count for narrowing vector shift "
7790 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007791
7792 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007793 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00007794 }
7795
7796 switch (IntNo) {
7797 case Intrinsic::arm_neon_vshifts:
7798 case Intrinsic::arm_neon_vshiftu:
7799 // Opcode already set above.
7800 break;
7801 case Intrinsic::arm_neon_vshiftls:
7802 case Intrinsic::arm_neon_vshiftlu:
7803 if (Cnt == VT.getVectorElementType().getSizeInBits())
7804 VShiftOpc = ARMISD::VSHLLi;
7805 else
7806 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
7807 ARMISD::VSHLLs : ARMISD::VSHLLu);
7808 break;
7809 case Intrinsic::arm_neon_vshiftn:
7810 VShiftOpc = ARMISD::VSHRN; break;
7811 case Intrinsic::arm_neon_vrshifts:
7812 VShiftOpc = ARMISD::VRSHRs; break;
7813 case Intrinsic::arm_neon_vrshiftu:
7814 VShiftOpc = ARMISD::VRSHRu; break;
7815 case Intrinsic::arm_neon_vrshiftn:
7816 VShiftOpc = ARMISD::VRSHRN; break;
7817 case Intrinsic::arm_neon_vqshifts:
7818 VShiftOpc = ARMISD::VQSHLs; break;
7819 case Intrinsic::arm_neon_vqshiftu:
7820 VShiftOpc = ARMISD::VQSHLu; break;
7821 case Intrinsic::arm_neon_vqshiftsu:
7822 VShiftOpc = ARMISD::VQSHLsu; break;
7823 case Intrinsic::arm_neon_vqshiftns:
7824 VShiftOpc = ARMISD::VQSHRNs; break;
7825 case Intrinsic::arm_neon_vqshiftnu:
7826 VShiftOpc = ARMISD::VQSHRNu; break;
7827 case Intrinsic::arm_neon_vqshiftnsu:
7828 VShiftOpc = ARMISD::VQSHRNsu; break;
7829 case Intrinsic::arm_neon_vqrshiftns:
7830 VShiftOpc = ARMISD::VQRSHRNs; break;
7831 case Intrinsic::arm_neon_vqrshiftnu:
7832 VShiftOpc = ARMISD::VQRSHRNu; break;
7833 case Intrinsic::arm_neon_vqrshiftnsu:
7834 VShiftOpc = ARMISD::VQRSHRNsu; break;
7835 }
7836
7837 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007838 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007839 }
7840
7841 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00007842 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007843 int64_t Cnt;
7844 unsigned VShiftOpc = 0;
7845
7846 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
7847 VShiftOpc = ARMISD::VSLI;
7848 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
7849 VShiftOpc = ARMISD::VSRI;
7850 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00007851 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007852 }
7853
7854 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
7855 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00007856 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007857 }
7858
7859 case Intrinsic::arm_neon_vqrshifts:
7860 case Intrinsic::arm_neon_vqrshiftu:
7861 // No immediate versions of these to check for.
7862 break;
7863 }
7864
7865 return SDValue();
7866}
7867
7868/// PerformShiftCombine - Checks for immediate versions of vector shifts and
7869/// lowers them. As with the vector shift intrinsics, this is done during DAG
7870/// combining instead of DAG legalizing because the build_vectors for 64-bit
7871/// vector element shift counts are generally not legal, and it is hard to see
7872/// their values after they get legalized to loads from a constant pool.
7873static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
7874 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00007875 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00007876
7877 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00007878 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7879 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00007880 return SDValue();
7881
7882 assert(ST->hasNEON() && "unexpected vector shift");
7883 int64_t Cnt;
7884
7885 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007886 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007887
7888 case ISD::SHL:
7889 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
7890 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007891 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007892 break;
7893
7894 case ISD::SRA:
7895 case ISD::SRL:
7896 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
7897 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
7898 ARMISD::VSHRs : ARMISD::VSHRu);
7899 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007900 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007901 }
7902 }
7903 return SDValue();
7904}
7905
7906/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
7907/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
7908static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
7909 const ARMSubtarget *ST) {
7910 SDValue N0 = N->getOperand(0);
7911
7912 // Check for sign- and zero-extensions of vector extract operations of 8-
7913 // and 16-bit vector elements. NEON supports these directly. They are
7914 // handled during DAG combining because type legalization will promote them
7915 // to 32-bit types and it is messy to recognize the operations after that.
7916 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
7917 SDValue Vec = N0.getOperand(0);
7918 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00007919 EVT VT = N->getValueType(0);
7920 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007921 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7922
Owen Anderson825b72b2009-08-11 20:47:22 +00007923 if (VT == MVT::i32 &&
7924 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00007925 TLI.isTypeLegal(Vec.getValueType()) &&
7926 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007927
7928 unsigned Opc = 0;
7929 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007930 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007931 case ISD::SIGN_EXTEND:
7932 Opc = ARMISD::VGETLANEs;
7933 break;
7934 case ISD::ZERO_EXTEND:
7935 case ISD::ANY_EXTEND:
7936 Opc = ARMISD::VGETLANEu;
7937 break;
7938 }
7939 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
7940 }
7941 }
7942
7943 return SDValue();
7944}
7945
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007946/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
7947/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
7948static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
7949 const ARMSubtarget *ST) {
7950 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00007951 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007952 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
7953 // a NaN; only do the transformation when it matches that behavior.
7954
7955 // For now only do this when using NEON for FP operations; if using VFP, it
7956 // is not obvious that the benefit outweighs the cost of switching to the
7957 // NEON pipeline.
7958 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
7959 N->getValueType(0) != MVT::f32)
7960 return SDValue();
7961
7962 SDValue CondLHS = N->getOperand(0);
7963 SDValue CondRHS = N->getOperand(1);
7964 SDValue LHS = N->getOperand(2);
7965 SDValue RHS = N->getOperand(3);
7966 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
7967
7968 unsigned Opcode = 0;
7969 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00007970 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007971 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00007972 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007973 IsReversed = true ; // x CC y ? y : x
7974 } else {
7975 return SDValue();
7976 }
7977
Bob Wilsone742bb52010-02-24 22:15:53 +00007978 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007979 switch (CC) {
7980 default: break;
7981 case ISD::SETOLT:
7982 case ISD::SETOLE:
7983 case ISD::SETLT:
7984 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007985 case ISD::SETULT:
7986 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00007987 // If LHS is NaN, an ordered comparison will be false and the result will
7988 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
7989 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
7990 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
7991 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
7992 break;
7993 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
7994 // will return -0, so vmin can only be used for unsafe math or if one of
7995 // the operands is known to be nonzero.
7996 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
7997 !UnsafeFPMath &&
7998 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
7999 break;
8000 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008001 break;
8002
8003 case ISD::SETOGT:
8004 case ISD::SETOGE:
8005 case ISD::SETGT:
8006 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008007 case ISD::SETUGT:
8008 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008009 // If LHS is NaN, an ordered comparison will be false and the result will
8010 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
8011 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8012 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
8013 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8014 break;
8015 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
8016 // will return +0, so vmax can only be used for unsafe math or if one of
8017 // the operands is known to be nonzero.
8018 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
8019 !UnsafeFPMath &&
8020 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8021 break;
8022 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008023 break;
8024 }
8025
8026 if (!Opcode)
8027 return SDValue();
8028 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
8029}
8030
Evan Chenge721f5c2011-07-13 00:42:17 +00008031/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
8032SDValue
8033ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
8034 SDValue Cmp = N->getOperand(4);
8035 if (Cmp.getOpcode() != ARMISD::CMPZ)
8036 // Only looking at EQ and NE cases.
8037 return SDValue();
8038
8039 EVT VT = N->getValueType(0);
8040 DebugLoc dl = N->getDebugLoc();
8041 SDValue LHS = Cmp.getOperand(0);
8042 SDValue RHS = Cmp.getOperand(1);
8043 SDValue FalseVal = N->getOperand(0);
8044 SDValue TrueVal = N->getOperand(1);
8045 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00008046 ARMCC::CondCodes CC =
8047 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00008048
8049 // Simplify
8050 // mov r1, r0
8051 // cmp r1, x
8052 // mov r0, y
8053 // moveq r0, x
8054 // to
8055 // cmp r0, x
8056 // movne r0, y
8057 //
8058 // mov r1, r0
8059 // cmp r1, x
8060 // mov r0, x
8061 // movne r0, y
8062 // to
8063 // cmp r0, x
8064 // movne r0, y
8065 /// FIXME: Turn this into a target neutral optimization?
8066 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00008067 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00008068 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
8069 N->getOperand(3), Cmp);
8070 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
8071 SDValue ARMcc;
8072 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
8073 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
8074 N->getOperand(3), NewCmp);
8075 }
8076
8077 if (Res.getNode()) {
8078 APInt KnownZero, KnownOne;
8079 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
8080 DAG.ComputeMaskedBits(SDValue(N,0), Mask, KnownZero, KnownOne);
8081 // Capture demanded bits information that would be otherwise lost.
8082 if (KnownZero == 0xfffffffe)
8083 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8084 DAG.getValueType(MVT::i1));
8085 else if (KnownZero == 0xffffff00)
8086 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8087 DAG.getValueType(MVT::i8));
8088 else if (KnownZero == 0xffff0000)
8089 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8090 DAG.getValueType(MVT::i16));
8091 }
8092
8093 return Res;
8094}
8095
Dan Gohman475871a2008-07-27 21:46:04 +00008096SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008097 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008098 switch (N->getOpcode()) {
8099 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00008100 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008101 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008102 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008103 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00008104 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00008105 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00008106 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008107 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00008108 case ISD::STORE: return PerformSTORECombine(N, DCI);
8109 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
8110 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00008111 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008112 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00008113 case ISD::FP_TO_SINT:
8114 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
8115 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008116 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00008117 case ISD::SHL:
8118 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008119 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00008120 case ISD::SIGN_EXTEND:
8121 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008122 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
8123 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00008124 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00008125 case ARMISD::VLD2DUP:
8126 case ARMISD::VLD3DUP:
8127 case ARMISD::VLD4DUP:
8128 return CombineBaseUpdate(N, DCI);
8129 case ISD::INTRINSIC_VOID:
8130 case ISD::INTRINSIC_W_CHAIN:
8131 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8132 case Intrinsic::arm_neon_vld1:
8133 case Intrinsic::arm_neon_vld2:
8134 case Intrinsic::arm_neon_vld3:
8135 case Intrinsic::arm_neon_vld4:
8136 case Intrinsic::arm_neon_vld2lane:
8137 case Intrinsic::arm_neon_vld3lane:
8138 case Intrinsic::arm_neon_vld4lane:
8139 case Intrinsic::arm_neon_vst1:
8140 case Intrinsic::arm_neon_vst2:
8141 case Intrinsic::arm_neon_vst3:
8142 case Intrinsic::arm_neon_vst4:
8143 case Intrinsic::arm_neon_vst2lane:
8144 case Intrinsic::arm_neon_vst3lane:
8145 case Intrinsic::arm_neon_vst4lane:
8146 return CombineBaseUpdate(N, DCI);
8147 default: break;
8148 }
8149 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008150 }
Dan Gohman475871a2008-07-27 21:46:04 +00008151 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008152}
8153
Evan Cheng31959b12011-02-02 01:06:55 +00008154bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
8155 EVT VT) const {
8156 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
8157}
8158
Bill Wendlingaf566342009-08-15 21:21:19 +00008159bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00008160 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00008161 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00008162
8163 switch (VT.getSimpleVT().SimpleTy) {
8164 default:
8165 return false;
8166 case MVT::i8:
8167 case MVT::i16:
8168 case MVT::i32:
8169 return true;
8170 // FIXME: VLD1 etc with standard alignment is legal.
8171 }
8172}
8173
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008174static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
8175 unsigned AlignCheck) {
8176 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
8177 (DstAlign == 0 || DstAlign % AlignCheck == 0));
8178}
8179
8180EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
8181 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00008182 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008183 bool MemcpyStrSrc,
8184 MachineFunction &MF) const {
8185 const Function *F = MF.getFunction();
8186
8187 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00008188 if (IsZeroVal &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008189 !F->hasFnAttr(Attribute::NoImplicitFloat) &&
8190 Subtarget->hasNEON()) {
8191 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
8192 return MVT::v4i32;
8193 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
8194 return MVT::v2i32;
8195 }
8196 }
8197
Lang Hames5207bf22011-11-08 18:56:23 +00008198 // Lowering to i32/i16 if the size permits.
8199 if (Size >= 4) {
8200 return MVT::i32;
8201 } else if (Size >= 2) {
8202 return MVT::i16;
8203 }
8204
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008205 // Let the target-independent logic figure it out.
8206 return MVT::Other;
8207}
8208
Evan Chenge6c835f2009-08-14 20:09:37 +00008209static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
8210 if (V < 0)
8211 return false;
8212
8213 unsigned Scale = 1;
8214 switch (VT.getSimpleVT().SimpleTy) {
8215 default: return false;
8216 case MVT::i1:
8217 case MVT::i8:
8218 // Scale == 1;
8219 break;
8220 case MVT::i16:
8221 // Scale == 2;
8222 Scale = 2;
8223 break;
8224 case MVT::i32:
8225 // Scale == 4;
8226 Scale = 4;
8227 break;
8228 }
8229
8230 if ((V & (Scale - 1)) != 0)
8231 return false;
8232 V /= Scale;
8233 return V == (V & ((1LL << 5) - 1));
8234}
8235
8236static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
8237 const ARMSubtarget *Subtarget) {
8238 bool isNeg = false;
8239 if (V < 0) {
8240 isNeg = true;
8241 V = - V;
8242 }
8243
8244 switch (VT.getSimpleVT().SimpleTy) {
8245 default: return false;
8246 case MVT::i1:
8247 case MVT::i8:
8248 case MVT::i16:
8249 case MVT::i32:
8250 // + imm12 or - imm8
8251 if (isNeg)
8252 return V == (V & ((1LL << 8) - 1));
8253 return V == (V & ((1LL << 12) - 1));
8254 case MVT::f32:
8255 case MVT::f64:
8256 // Same as ARM mode. FIXME: NEON?
8257 if (!Subtarget->hasVFP2())
8258 return false;
8259 if ((V & 3) != 0)
8260 return false;
8261 V >>= 2;
8262 return V == (V & ((1LL << 8) - 1));
8263 }
8264}
8265
Evan Chengb01fad62007-03-12 23:30:29 +00008266/// isLegalAddressImmediate - Return true if the integer value can be used
8267/// as the offset of the target addressing mode for load / store of the
8268/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00008269static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00008270 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00008271 if (V == 0)
8272 return true;
8273
Evan Cheng65011532009-03-09 19:15:00 +00008274 if (!VT.isSimple())
8275 return false;
8276
Evan Chenge6c835f2009-08-14 20:09:37 +00008277 if (Subtarget->isThumb1Only())
8278 return isLegalT1AddressImmediate(V, VT);
8279 else if (Subtarget->isThumb2())
8280 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00008281
Evan Chenge6c835f2009-08-14 20:09:37 +00008282 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00008283 if (V < 0)
8284 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00008285 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00008286 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008287 case MVT::i1:
8288 case MVT::i8:
8289 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00008290 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008291 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008292 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00008293 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008294 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008295 case MVT::f32:
8296 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00008297 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00008298 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00008299 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00008300 return false;
8301 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008302 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00008303 }
Evan Chenga8e29892007-01-19 07:51:42 +00008304}
8305
Evan Chenge6c835f2009-08-14 20:09:37 +00008306bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
8307 EVT VT) const {
8308 int Scale = AM.Scale;
8309 if (Scale < 0)
8310 return false;
8311
8312 switch (VT.getSimpleVT().SimpleTy) {
8313 default: return false;
8314 case MVT::i1:
8315 case MVT::i8:
8316 case MVT::i16:
8317 case MVT::i32:
8318 if (Scale == 1)
8319 return true;
8320 // r + r << imm
8321 Scale = Scale & ~1;
8322 return Scale == 2 || Scale == 4 || Scale == 8;
8323 case MVT::i64:
8324 // r + r
8325 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
8326 return true;
8327 return false;
8328 case MVT::isVoid:
8329 // Note, we allow "void" uses (basically, uses that aren't loads or
8330 // stores), because arm allows folding a scale into many arithmetic
8331 // operations. This should be made more precise and revisited later.
8332
8333 // Allow r << imm, but the imm has to be a multiple of two.
8334 if (Scale & 1) return false;
8335 return isPowerOf2_32(Scale);
8336 }
8337}
8338
Chris Lattner37caf8c2007-04-09 23:33:39 +00008339/// isLegalAddressingMode - Return true if the addressing mode represented
8340/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008341bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008342 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008343 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00008344 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00008345 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008346
Chris Lattner37caf8c2007-04-09 23:33:39 +00008347 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008348 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008349 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008350
Chris Lattner37caf8c2007-04-09 23:33:39 +00008351 switch (AM.Scale) {
8352 case 0: // no scale reg, must be "r+i" or "r", or "i".
8353 break;
8354 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00008355 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00008356 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008357 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00008358 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008359 // ARM doesn't support any R+R*scale+imm addr modes.
8360 if (AM.BaseOffs)
8361 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008362
Bob Wilson2c7dab12009-04-08 17:55:28 +00008363 if (!VT.isSimple())
8364 return false;
8365
Evan Chenge6c835f2009-08-14 20:09:37 +00008366 if (Subtarget->isThumb2())
8367 return isLegalT2ScaledAddressingMode(AM, VT);
8368
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008369 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00008370 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00008371 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008372 case MVT::i1:
8373 case MVT::i8:
8374 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008375 if (Scale < 0) Scale = -Scale;
8376 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008377 return true;
8378 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00008379 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008380 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00008381 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008382 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008383 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008384 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00008385 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008386
Owen Anderson825b72b2009-08-11 20:47:22 +00008387 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008388 // Note, we allow "void" uses (basically, uses that aren't loads or
8389 // stores), because arm allows folding a scale into many arithmetic
8390 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008391
Chris Lattner37caf8c2007-04-09 23:33:39 +00008392 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00008393 if (Scale & 1) return false;
8394 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00008395 }
8396 break;
Evan Chengb01fad62007-03-12 23:30:29 +00008397 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00008398 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00008399}
8400
Evan Cheng77e47512009-11-11 19:05:52 +00008401/// isLegalICmpImmediate - Return true if the specified immediate is legal
8402/// icmp immediate, that is the target has icmp instructions which can compare
8403/// a register against the immediate without having to materialize the
8404/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00008405bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00008406 if (!Subtarget->isThumb())
8407 return ARM_AM::getSOImmVal(Imm) != -1;
8408 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00008409 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00008410 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00008411}
8412
Dan Gohmancca82142011-05-03 00:46:49 +00008413/// isLegalAddImmediate - Return true if the specified immediate is legal
8414/// add immediate, that is the target has add instructions which can add
8415/// a register with the immediate without having to materialize the
8416/// immediate into a register.
8417bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
8418 return ARM_AM::getSOImmVal(Imm) != -1;
8419}
8420
Owen Andersone50ed302009-08-10 22:56:29 +00008421static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008422 bool isSEXTLoad, SDValue &Base,
8423 SDValue &Offset, bool &isInc,
8424 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00008425 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8426 return false;
8427
Owen Anderson825b72b2009-08-11 20:47:22 +00008428 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00008429 // AddressingMode 3
8430 Base = Ptr->getOperand(0);
8431 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008432 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008433 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008434 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008435 isInc = false;
8436 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8437 return true;
8438 }
8439 }
8440 isInc = (Ptr->getOpcode() == ISD::ADD);
8441 Offset = Ptr->getOperand(1);
8442 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00008443 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00008444 // AddressingMode 2
8445 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008446 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008447 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008448 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008449 isInc = false;
8450 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8451 Base = Ptr->getOperand(0);
8452 return true;
8453 }
8454 }
8455
8456 if (Ptr->getOpcode() == ISD::ADD) {
8457 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00008458 ARM_AM::ShiftOpc ShOpcVal=
8459 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00008460 if (ShOpcVal != ARM_AM::no_shift) {
8461 Base = Ptr->getOperand(1);
8462 Offset = Ptr->getOperand(0);
8463 } else {
8464 Base = Ptr->getOperand(0);
8465 Offset = Ptr->getOperand(1);
8466 }
8467 return true;
8468 }
8469
8470 isInc = (Ptr->getOpcode() == ISD::ADD);
8471 Base = Ptr->getOperand(0);
8472 Offset = Ptr->getOperand(1);
8473 return true;
8474 }
8475
Jim Grosbache5165492009-11-09 00:11:35 +00008476 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00008477 return false;
8478}
8479
Owen Andersone50ed302009-08-10 22:56:29 +00008480static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008481 bool isSEXTLoad, SDValue &Base,
8482 SDValue &Offset, bool &isInc,
8483 SelectionDAG &DAG) {
8484 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8485 return false;
8486
8487 Base = Ptr->getOperand(0);
8488 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
8489 int RHSC = (int)RHS->getZExtValue();
8490 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
8491 assert(Ptr->getOpcode() == ISD::ADD);
8492 isInc = false;
8493 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8494 return true;
8495 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
8496 isInc = Ptr->getOpcode() == ISD::ADD;
8497 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
8498 return true;
8499 }
8500 }
8501
8502 return false;
8503}
8504
Evan Chenga8e29892007-01-19 07:51:42 +00008505/// getPreIndexedAddressParts - returns true by value, base pointer and
8506/// offset pointer and addressing mode by reference if the node's address
8507/// can be legally represented as pre-indexed load / store address.
8508bool
Dan Gohman475871a2008-07-27 21:46:04 +00008509ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
8510 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008511 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008512 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008513 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008514 return false;
8515
Owen Andersone50ed302009-08-10 22:56:29 +00008516 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008517 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008518 bool isSEXTLoad = false;
8519 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
8520 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008521 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008522 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8523 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
8524 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008525 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008526 } else
8527 return false;
8528
8529 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008530 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008531 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008532 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
8533 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008534 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008535 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00008536 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00008537 if (!isLegal)
8538 return false;
8539
8540 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
8541 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008542}
8543
8544/// getPostIndexedAddressParts - returns true by value, base pointer and
8545/// offset pointer and addressing mode by reference if this node can be
8546/// combined with a load / store to form a post-indexed load / store.
8547bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00008548 SDValue &Base,
8549 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008550 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008551 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008552 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008553 return false;
8554
Owen Andersone50ed302009-08-10 22:56:29 +00008555 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008556 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008557 bool isSEXTLoad = false;
8558 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008559 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008560 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008561 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8562 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008563 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008564 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008565 } else
8566 return false;
8567
8568 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008569 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008570 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008571 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00008572 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008573 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008574 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
8575 isInc, DAG);
8576 if (!isLegal)
8577 return false;
8578
Evan Cheng28dad2a2010-05-18 21:31:17 +00008579 if (Ptr != Base) {
8580 // Swap base ptr and offset to catch more post-index load / store when
8581 // it's legal. In Thumb2 mode, offset must be an immediate.
8582 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
8583 !Subtarget->isThumb2())
8584 std::swap(Base, Offset);
8585
8586 // Post-indexed load / store update the base pointer.
8587 if (Ptr != Base)
8588 return false;
8589 }
8590
Evan Chenge88d5ce2009-07-02 07:28:31 +00008591 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
8592 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008593}
8594
Dan Gohman475871a2008-07-27 21:46:04 +00008595void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008596 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008597 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008598 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008599 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00008600 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008601 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00008602 switch (Op.getOpcode()) {
8603 default: break;
8604 case ARMISD::CMOV: {
8605 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00008606 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008607 if (KnownZero == 0 && KnownOne == 0) return;
8608
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008609 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00008610 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
8611 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008612 KnownZero &= KnownZeroRHS;
8613 KnownOne &= KnownOneRHS;
8614 return;
8615 }
8616 }
8617}
8618
8619//===----------------------------------------------------------------------===//
8620// ARM Inline Assembly Support
8621//===----------------------------------------------------------------------===//
8622
Evan Cheng55d42002011-01-08 01:24:27 +00008623bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
8624 // Looking for "rev" which is V6+.
8625 if (!Subtarget->hasV6Ops())
8626 return false;
8627
8628 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
8629 std::string AsmStr = IA->getAsmString();
8630 SmallVector<StringRef, 4> AsmPieces;
8631 SplitString(AsmStr, AsmPieces, ";\n");
8632
8633 switch (AsmPieces.size()) {
8634 default: return false;
8635 case 1:
8636 AsmStr = AsmPieces[0];
8637 AsmPieces.clear();
8638 SplitString(AsmStr, AsmPieces, " \t,");
8639
8640 // rev $0, $1
8641 if (AsmPieces.size() == 3 &&
8642 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
8643 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008644 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00008645 if (Ty && Ty->getBitWidth() == 32)
8646 return IntrinsicLowering::LowerToByteSwap(CI);
8647 }
8648 break;
8649 }
8650
8651 return false;
8652}
8653
Evan Chenga8e29892007-01-19 07:51:42 +00008654/// getConstraintType - Given a constraint letter, return the type of
8655/// constraint it is for this target.
8656ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008657ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
8658 if (Constraint.size() == 1) {
8659 switch (Constraint[0]) {
8660 default: break;
8661 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008662 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00008663 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008664 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008665 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00008666 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00008667 // An address with a single base register. Due to the way we
8668 // currently handle addresses it is the same as an 'r' memory constraint.
8669 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00008670 }
Eric Christopher1312ca82011-06-21 22:10:57 +00008671 } else if (Constraint.size() == 2) {
8672 switch (Constraint[0]) {
8673 default: break;
8674 // All 'U+' constraints are addresses.
8675 case 'U': return C_Memory;
8676 }
Evan Chenga8e29892007-01-19 07:51:42 +00008677 }
Chris Lattner4234f572007-03-25 02:14:49 +00008678 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00008679}
8680
John Thompson44ab89e2010-10-29 17:29:13 +00008681/// Examine constraint type and operand type and determine a weight value.
8682/// This object must already have been set up with the operand type
8683/// and the current alternative constraint selected.
8684TargetLowering::ConstraintWeight
8685ARMTargetLowering::getSingleConstraintMatchWeight(
8686 AsmOperandInfo &info, const char *constraint) const {
8687 ConstraintWeight weight = CW_Invalid;
8688 Value *CallOperandVal = info.CallOperandVal;
8689 // If we don't have a value, we can't do a match,
8690 // but allow it at the lowest weight.
8691 if (CallOperandVal == NULL)
8692 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008693 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00008694 // Look at the constraint type.
8695 switch (*constraint) {
8696 default:
8697 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
8698 break;
8699 case 'l':
8700 if (type->isIntegerTy()) {
8701 if (Subtarget->isThumb())
8702 weight = CW_SpecificReg;
8703 else
8704 weight = CW_Register;
8705 }
8706 break;
8707 case 'w':
8708 if (type->isFloatingPointTy())
8709 weight = CW_Register;
8710 break;
8711 }
8712 return weight;
8713}
8714
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008715typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
8716RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00008717ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00008718 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00008719 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00008720 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00008721 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00008722 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00008723 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008724 return RCPair(0U, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +00008725 else
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008726 return RCPair(0U, ARM::GPRRegisterClass);
Eric Christopher73744df2011-06-30 23:23:01 +00008727 case 'h': // High regs or no regs.
8728 if (Subtarget->isThumb())
Andrew Trick3af7a672011-09-20 03:06:13 +00008729 return RCPair(0U, ARM::hGPRRegisterClass);
Eric Christopher1070f822011-07-01 00:19:27 +00008730 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008731 case 'r':
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008732 return RCPair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008733 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00008734 if (VT == MVT::f32)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008735 return RCPair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00008736 if (VT.getSizeInBits() == 64)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008737 return RCPair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00008738 if (VT.getSizeInBits() == 128)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008739 return RCPair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008740 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008741 case 'x':
8742 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00008743 return RCPair(0U, ARM::SPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008744 if (VT.getSizeInBits() == 64)
Andrew Trick3af7a672011-09-20 03:06:13 +00008745 return RCPair(0U, ARM::DPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008746 if (VT.getSizeInBits() == 128)
Andrew Trick3af7a672011-09-20 03:06:13 +00008747 return RCPair(0U, ARM::QPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008748 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008749 case 't':
8750 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00008751 return RCPair(0U, ARM::SPRRegisterClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008752 break;
Evan Chenga8e29892007-01-19 07:51:42 +00008753 }
8754 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00008755 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00008756 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00008757
Evan Chenga8e29892007-01-19 07:51:42 +00008758 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8759}
8760
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008761/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8762/// vector. If it is invalid, don't add anything to Ops.
8763void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00008764 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008765 std::vector<SDValue>&Ops,
8766 SelectionDAG &DAG) const {
8767 SDValue Result(0, 0);
8768
Eric Christopher100c8332011-06-02 23:16:42 +00008769 // Currently only support length 1 constraints.
8770 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00008771
Eric Christopher100c8332011-06-02 23:16:42 +00008772 char ConstraintLetter = Constraint[0];
8773 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008774 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00008775 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008776 case 'I': case 'J': case 'K': case 'L':
8777 case 'M': case 'N': case 'O':
8778 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
8779 if (!C)
8780 return;
8781
8782 int64_t CVal64 = C->getSExtValue();
8783 int CVal = (int) CVal64;
8784 // None of these constraints allow values larger than 32 bits. Check
8785 // that the value fits in an int.
8786 if (CVal != CVal64)
8787 return;
8788
Eric Christopher100c8332011-06-02 23:16:42 +00008789 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00008790 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00008791 // Constant suitable for movw, must be between 0 and
8792 // 65535.
8793 if (Subtarget->hasV6T2Ops())
8794 if (CVal >= 0 && CVal <= 65535)
8795 break;
8796 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008797 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008798 if (Subtarget->isThumb1Only()) {
8799 // This must be a constant between 0 and 255, for ADD
8800 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008801 if (CVal >= 0 && CVal <= 255)
8802 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008803 } else if (Subtarget->isThumb2()) {
8804 // A constant that can be used as an immediate value in a
8805 // data-processing instruction.
8806 if (ARM_AM::getT2SOImmVal(CVal) != -1)
8807 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008808 } else {
8809 // A constant that can be used as an immediate value in a
8810 // data-processing instruction.
8811 if (ARM_AM::getSOImmVal(CVal) != -1)
8812 break;
8813 }
8814 return;
8815
8816 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008817 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008818 // This must be a constant between -255 and -1, for negated ADD
8819 // immediates. This can be used in GCC with an "n" modifier that
8820 // prints the negated value, for use with SUB instructions. It is
8821 // not useful otherwise but is implemented for compatibility.
8822 if (CVal >= -255 && CVal <= -1)
8823 break;
8824 } else {
8825 // This must be a constant between -4095 and 4095. It is not clear
8826 // what this constraint is intended for. Implemented for
8827 // compatibility with GCC.
8828 if (CVal >= -4095 && CVal <= 4095)
8829 break;
8830 }
8831 return;
8832
8833 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008834 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008835 // A 32-bit value where only one byte has a nonzero value. Exclude
8836 // zero to match GCC. This constraint is used by GCC internally for
8837 // constants that can be loaded with a move/shift combination.
8838 // It is not useful otherwise but is implemented for compatibility.
8839 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
8840 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008841 } else if (Subtarget->isThumb2()) {
8842 // A constant whose bitwise inverse can be used as an immediate
8843 // value in a data-processing instruction. This can be used in GCC
8844 // with a "B" modifier that prints the inverted value, for use with
8845 // BIC and MVN instructions. It is not useful otherwise but is
8846 // implemented for compatibility.
8847 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
8848 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008849 } else {
8850 // A constant whose bitwise inverse can be used as an immediate
8851 // value in a data-processing instruction. This can be used in GCC
8852 // with a "B" modifier that prints the inverted value, for use with
8853 // BIC and MVN instructions. It is not useful otherwise but is
8854 // implemented for compatibility.
8855 if (ARM_AM::getSOImmVal(~CVal) != -1)
8856 break;
8857 }
8858 return;
8859
8860 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008861 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008862 // This must be a constant between -7 and 7,
8863 // for 3-operand ADD/SUB immediate instructions.
8864 if (CVal >= -7 && CVal < 7)
8865 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008866 } else if (Subtarget->isThumb2()) {
8867 // A constant whose negation can be used as an immediate value in a
8868 // data-processing instruction. This can be used in GCC with an "n"
8869 // modifier that prints the negated value, for use with SUB
8870 // instructions. It is not useful otherwise but is implemented for
8871 // compatibility.
8872 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
8873 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008874 } else {
8875 // A constant whose negation can be used as an immediate value in a
8876 // data-processing instruction. This can be used in GCC with an "n"
8877 // modifier that prints the negated value, for use with SUB
8878 // instructions. It is not useful otherwise but is implemented for
8879 // compatibility.
8880 if (ARM_AM::getSOImmVal(-CVal) != -1)
8881 break;
8882 }
8883 return;
8884
8885 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008886 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008887 // This must be a multiple of 4 between 0 and 1020, for
8888 // ADD sp + immediate.
8889 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
8890 break;
8891 } else {
8892 // A power of two or a constant between 0 and 32. This is used in
8893 // GCC for the shift amount on shifted register operands, but it is
8894 // useful in general for any shift amounts.
8895 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
8896 break;
8897 }
8898 return;
8899
8900 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008901 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008902 // This must be a constant between 0 and 31, for shift amounts.
8903 if (CVal >= 0 && CVal <= 31)
8904 break;
8905 }
8906 return;
8907
8908 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008909 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008910 // This must be a multiple of 4 between -508 and 508, for
8911 // ADD/SUB sp = sp + immediate.
8912 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
8913 break;
8914 }
8915 return;
8916 }
8917 Result = DAG.getTargetConstant(CVal, Op.getValueType());
8918 break;
8919 }
8920
8921 if (Result.getNode()) {
8922 Ops.push_back(Result);
8923 return;
8924 }
Dale Johannesen1784d162010-06-25 21:55:36 +00008925 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008926}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00008927
8928bool
8929ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
8930 // The ARM target isn't yet aware of offsets.
8931 return false;
8932}
Evan Cheng39382422009-10-28 01:44:26 +00008933
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008934bool ARM::isBitFieldInvertedMask(unsigned v) {
8935 if (v == 0xffffffff)
8936 return 0;
8937 // there can be 1's on either or both "outsides", all the "inside"
8938 // bits must be 0's
8939 unsigned int lsb = 0, msb = 31;
8940 while (v & (1 << msb)) --msb;
8941 while (v & (1 << lsb)) ++lsb;
8942 for (unsigned int i = lsb; i <= msb; ++i) {
8943 if (v & (1 << i))
8944 return 0;
8945 }
8946 return 1;
8947}
8948
Evan Cheng39382422009-10-28 01:44:26 +00008949/// isFPImmLegal - Returns true if the target can instruction select the
8950/// specified FP immediate natively. If false, the legalizer will
8951/// materialize the FP immediate as a load from a constant pool.
8952bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
8953 if (!Subtarget->hasVFP3())
8954 return false;
8955 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00008956 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00008957 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00008958 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00008959 return false;
8960}
Bob Wilson65ffec42010-09-21 17:56:22 +00008961
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008962/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00008963/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
8964/// specified in the intrinsic calls.
8965bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
8966 const CallInst &I,
8967 unsigned Intrinsic) const {
8968 switch (Intrinsic) {
8969 case Intrinsic::arm_neon_vld1:
8970 case Intrinsic::arm_neon_vld2:
8971 case Intrinsic::arm_neon_vld3:
8972 case Intrinsic::arm_neon_vld4:
8973 case Intrinsic::arm_neon_vld2lane:
8974 case Intrinsic::arm_neon_vld3lane:
8975 case Intrinsic::arm_neon_vld4lane: {
8976 Info.opc = ISD::INTRINSIC_W_CHAIN;
8977 // Conservatively set memVT to the entire set of vectors loaded.
8978 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
8979 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
8980 Info.ptrVal = I.getArgOperand(0);
8981 Info.offset = 0;
8982 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
8983 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
8984 Info.vol = false; // volatile loads with NEON intrinsics not supported
8985 Info.readMem = true;
8986 Info.writeMem = false;
8987 return true;
8988 }
8989 case Intrinsic::arm_neon_vst1:
8990 case Intrinsic::arm_neon_vst2:
8991 case Intrinsic::arm_neon_vst3:
8992 case Intrinsic::arm_neon_vst4:
8993 case Intrinsic::arm_neon_vst2lane:
8994 case Intrinsic::arm_neon_vst3lane:
8995 case Intrinsic::arm_neon_vst4lane: {
8996 Info.opc = ISD::INTRINSIC_VOID;
8997 // Conservatively set memVT to the entire set of vectors stored.
8998 unsigned NumElts = 0;
8999 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009000 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00009001 if (!ArgTy->isVectorTy())
9002 break;
9003 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
9004 }
9005 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9006 Info.ptrVal = I.getArgOperand(0);
9007 Info.offset = 0;
9008 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9009 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9010 Info.vol = false; // volatile stores with NEON intrinsics not supported
9011 Info.readMem = false;
9012 Info.writeMem = true;
9013 return true;
9014 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009015 case Intrinsic::arm_strexd: {
9016 Info.opc = ISD::INTRINSIC_W_CHAIN;
9017 Info.memVT = MVT::i64;
9018 Info.ptrVal = I.getArgOperand(2);
9019 Info.offset = 0;
9020 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009021 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009022 Info.readMem = false;
9023 Info.writeMem = true;
9024 return true;
9025 }
9026 case Intrinsic::arm_ldrexd: {
9027 Info.opc = ISD::INTRINSIC_W_CHAIN;
9028 Info.memVT = MVT::i64;
9029 Info.ptrVal = I.getArgOperand(0);
9030 Info.offset = 0;
9031 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009032 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009033 Info.readMem = true;
9034 Info.writeMem = false;
9035 return true;
9036 }
Bob Wilson65ffec42010-09-21 17:56:22 +00009037 default:
9038 break;
9039 }
9040
9041 return false;
9042}