blob: 778f6c0221800ad6daa4b56606df36781d88680d [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Dale Johannesen51e28e62010-06-03 21:09:53 +000015#define DEBUG_TYPE "arm-isel"
Evan Chenga8e29892007-01-19 07:51:42 +000016#include "ARM.h"
17#include "ARMAddressingModes.h"
18#include "ARMConstantPoolValue.h"
19#include "ARMISelLowering.h"
20#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000021#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000022#include "ARMRegisterInfo.h"
23#include "ARMSubtarget.h"
24#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000025#include "ARMTargetObjectFile.h"
Evan Chenga8e29892007-01-19 07:51:42 +000026#include "llvm/CallingConv.h"
27#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000028#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000029#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000030#include "llvm/Instruction.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000031#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000032#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000033#include "llvm/CodeGen/CallingConvLower.h"
Evan Chenga8e29892007-01-19 07:51:42 +000034#include "llvm/CodeGen/MachineBasicBlock.h"
35#include "llvm/CodeGen/MachineFrameInfo.h"
36#include "llvm/CodeGen/MachineFunction.h"
37#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000038#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000039#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000040#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000041#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000042#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000043#include "llvm/ADT/VectorExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000044#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000045#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000046#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000047#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000048#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000049#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000050using namespace llvm;
51
Dale Johannesen51e28e62010-06-03 21:09:53 +000052STATISTIC(NumTailCalls, "Number of tail calls");
53
54// This option should go away when tail calls fully work.
55static cl::opt<bool>
56EnableARMTailCalls("arm-tail-calls", cl::Hidden,
57 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
Dale Johannesenc66cdf72010-06-18 19:00:18 +000058 cl::init(true));
Dale Johannesen51e28e62010-06-03 21:09:53 +000059
Jim Grosbache7b52522010-04-14 22:28:31 +000060static cl::opt<bool>
61EnableARMLongCalls("arm-long-calls", cl::Hidden,
62 cl::desc("Generate calls via indirect call instructions."),
63 cl::init(false));
64
Evan Cheng46df4eb2010-06-16 07:35:02 +000065static cl::opt<bool>
66ARMInterworking("arm-interworking", cl::Hidden,
67 cl::desc("Enable / disable ARM interworking (for debugging only)"),
68 cl::init(true));
69
Owen Andersone50ed302009-08-10 22:56:29 +000070static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000071 CCValAssign::LocInfo &LocInfo,
72 ISD::ArgFlagsTy &ArgFlags,
73 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000074static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000075 CCValAssign::LocInfo &LocInfo,
76 ISD::ArgFlagsTy &ArgFlags,
77 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000078static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000079 CCValAssign::LocInfo &LocInfo,
80 ISD::ArgFlagsTy &ArgFlags,
81 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000082static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000083 CCValAssign::LocInfo &LocInfo,
84 ISD::ArgFlagsTy &ArgFlags,
85 CCState &State);
86
Owen Andersone50ed302009-08-10 22:56:29 +000087void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
88 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000089 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000090 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000091 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
92 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000093
Owen Anderson70671842009-08-10 20:18:46 +000094 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000095 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000096 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000097 }
98
Owen Andersone50ed302009-08-10 22:56:29 +000099 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000100 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +0000101 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000102 if (ElemTy == MVT::i8 || ElemTy == MVT::i16)
Owen Anderson70671842009-08-10 20:18:46 +0000103 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +0000104 if (ElemTy != MVT::i32) {
105 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
106 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
107 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
108 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
109 }
Owen Anderson70671842009-08-10 20:18:46 +0000110 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
111 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +0000112 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Anton Korobeynikov8e6c2b92009-08-21 12:40:35 +0000113 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
Bob Wilsond0910c42010-04-06 22:02:24 +0000114 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
115 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000116 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000117 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
118 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
119 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000120 }
121
122 // Promote all bit-wise operations.
123 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000124 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000125 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
126 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000127 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000128 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000129 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000130 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000131 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000132 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000133 }
Bob Wilson16330762009-09-16 00:17:28 +0000134
135 // Neon does not support vector divide/remainder operations.
136 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
137 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
138 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
139 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
140 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
141 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000142}
143
Owen Andersone50ed302009-08-10 22:56:29 +0000144void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000145 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000146 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000147}
148
Owen Andersone50ed302009-08-10 22:56:29 +0000149void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000150 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000151 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000152}
153
Chris Lattnerf0144122009-07-28 03:13:23 +0000154static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
155 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000156 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000157
Chris Lattner80ec2792009-08-02 00:34:36 +0000158 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000159}
160
Evan Chenga8e29892007-01-19 07:51:42 +0000161ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000162 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000163 Subtarget = &TM.getSubtarget<ARMSubtarget>();
164
Evan Chengb1df8f22007-04-27 08:15:43 +0000165 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000166 // Uses VFP for Thumb libfuncs if available.
167 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
168 // Single-precision floating-point arithmetic.
169 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
170 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
171 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
172 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000173
Evan Chengb1df8f22007-04-27 08:15:43 +0000174 // Double-precision floating-point arithmetic.
175 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
176 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
177 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
178 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000179
Evan Chengb1df8f22007-04-27 08:15:43 +0000180 // Single-precision comparisons.
181 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
182 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
183 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
184 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
185 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
186 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
187 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
188 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000189
Evan Chengb1df8f22007-04-27 08:15:43 +0000190 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
191 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
192 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
193 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
194 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
195 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
196 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
197 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000198
Evan Chengb1df8f22007-04-27 08:15:43 +0000199 // Double-precision comparisons.
200 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
201 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
202 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
203 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
204 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
205 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
206 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
207 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000208
Evan Chengb1df8f22007-04-27 08:15:43 +0000209 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
214 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
215 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
216 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000217
Evan Chengb1df8f22007-04-27 08:15:43 +0000218 // Floating-point to integer conversions.
219 // i64 conversions are done via library routines even when generating VFP
220 // instructions, so use the same ones.
221 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
222 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
223 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
224 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000225
Evan Chengb1df8f22007-04-27 08:15:43 +0000226 // Conversions between floating types.
227 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
228 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
229
230 // Integer to floating-point conversions.
231 // i64 conversions are done via library routines even when generating VFP
232 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000233 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
234 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000235 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
236 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
237 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
238 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
239 }
Evan Chenga8e29892007-01-19 07:51:42 +0000240 }
241
Bob Wilson2f954612009-05-22 17:38:41 +0000242 // These libcalls are not available in 32-bit.
243 setLibcallName(RTLIB::SHL_I128, 0);
244 setLibcallName(RTLIB::SRL_I128, 0);
245 setLibcallName(RTLIB::SRA_I128, 0);
246
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000247 // Libcalls should use the AAPCS base standard ABI, even if hard float
248 // is in effect, as per the ARM RTABI specification, section 4.1.2.
249 if (Subtarget->isAAPCS_ABI()) {
250 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
251 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
252 CallingConv::ARM_AAPCS);
253 }
254 }
255
David Goodwinf1daf7d2009-07-08 23:10:31 +0000256 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000258 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000259 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000260 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
262 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000263
Owen Anderson825b72b2009-08-11 20:47:22 +0000264 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000265 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000266
267 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 addDRTypeForNEON(MVT::v2f32);
269 addDRTypeForNEON(MVT::v8i8);
270 addDRTypeForNEON(MVT::v4i16);
271 addDRTypeForNEON(MVT::v2i32);
272 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000273
Owen Anderson825b72b2009-08-11 20:47:22 +0000274 addQRTypeForNEON(MVT::v4f32);
275 addQRTypeForNEON(MVT::v2f64);
276 addQRTypeForNEON(MVT::v16i8);
277 addQRTypeForNEON(MVT::v8i16);
278 addQRTypeForNEON(MVT::v4i32);
279 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000280
Bob Wilson74dc72e2009-09-15 23:55:57 +0000281 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
282 // neither Neon nor VFP support any arithmetic operations on it.
283 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
284 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
285 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
286 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
287 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
288 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
289 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
290 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
291 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
292 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
293 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
294 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
295 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
296 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
297 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
298 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
299 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
300 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
301 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
302 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
303 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
304 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
305 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
306 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
307
Bob Wilson642b3292009-09-16 00:32:15 +0000308 // Neon does not support some operations on v1i64 and v2i64 types.
309 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
310 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
311 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
312 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
313
Bob Wilson5bafff32009-06-22 23:27:02 +0000314 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
315 setTargetDAGCombine(ISD::SHL);
316 setTargetDAGCombine(ISD::SRL);
317 setTargetDAGCombine(ISD::SRA);
318 setTargetDAGCombine(ISD::SIGN_EXTEND);
319 setTargetDAGCombine(ISD::ZERO_EXTEND);
320 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000321 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson5bafff32009-06-22 23:27:02 +0000322 }
323
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000324 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000325
326 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000327 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000328
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000329 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000330 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000331
Evan Chenga8e29892007-01-19 07:51:42 +0000332 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000333 if (!Subtarget->isThumb1Only()) {
334 for (unsigned im = (unsigned)ISD::PRE_INC;
335 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000336 setIndexedLoadAction(im, MVT::i1, Legal);
337 setIndexedLoadAction(im, MVT::i8, Legal);
338 setIndexedLoadAction(im, MVT::i16, Legal);
339 setIndexedLoadAction(im, MVT::i32, Legal);
340 setIndexedStoreAction(im, MVT::i1, Legal);
341 setIndexedStoreAction(im, MVT::i8, Legal);
342 setIndexedStoreAction(im, MVT::i16, Legal);
343 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000344 }
Evan Chenga8e29892007-01-19 07:51:42 +0000345 }
346
347 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000348 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000349 setOperationAction(ISD::MUL, MVT::i64, Expand);
350 setOperationAction(ISD::MULHU, MVT::i32, Expand);
351 setOperationAction(ISD::MULHS, MVT::i32, Expand);
352 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
353 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000354 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 setOperationAction(ISD::MUL, MVT::i64, Expand);
356 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000357 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000358 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000359 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000360 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000361 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000362 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000363 setOperationAction(ISD::SRL, MVT::i64, Custom);
364 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000365
366 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000368 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000369 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000370 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000372
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000373 // Only ARMv6 has BSWAP.
374 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000375 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000376
Evan Chenga8e29892007-01-19 07:51:42 +0000377 // These are expanded into libcalls.
Jim Grosbach29402132010-05-05 23:44:43 +0000378 if (!Subtarget->hasDivide()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000379 // v7M has a hardware divider
380 setOperationAction(ISD::SDIV, MVT::i32, Expand);
381 setOperationAction(ISD::UDIV, MVT::i32, Expand);
382 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000383 setOperationAction(ISD::SREM, MVT::i32, Expand);
384 setOperationAction(ISD::UREM, MVT::i32, Expand);
385 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
386 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000387
Owen Anderson825b72b2009-08-11 20:47:22 +0000388 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
389 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
390 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
391 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000392 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000393
Evan Chengfb3611d2010-05-11 07:26:32 +0000394 setOperationAction(ISD::TRAP, MVT::Other, Legal);
395
Evan Chenga8e29892007-01-19 07:51:42 +0000396 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000397 setOperationAction(ISD::VASTART, MVT::Other, Custom);
398 setOperationAction(ISD::VAARG, MVT::Other, Expand);
399 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
400 setOperationAction(ISD::VAEND, MVT::Other, Expand);
401 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
402 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000403 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
404 // FIXME: Shouldn't need this, since no register is used, but the legalizer
405 // doesn't yet know how to not do that for SjLj.
406 setExceptionSelectorRegister(ARM::R0);
Evan Cheng3a1588a2010-04-15 22:20:34 +0000407 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Jim Grosbach7072cf62010-06-17 02:02:03 +0000408 // Handle atomics directly for ARMv[67] (except for Thumb1), otherwise
409 // use the default expansion.
Jim Grosbach68741be2010-06-18 22:35:32 +0000410 bool canHandleAtomics =
Jim Grosbach7072cf62010-06-17 02:02:03 +0000411 (Subtarget->hasV7Ops() ||
Jim Grosbach68741be2010-06-18 22:35:32 +0000412 (Subtarget->hasV6Ops() && !Subtarget->isThumb1Only()));
413 if (canHandleAtomics) {
414 // membarrier needs custom lowering; the rest are legal and handled
415 // normally.
416 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
417 } else {
418 // Set them all for expansion, which will force libcalls.
419 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
420 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Expand);
421 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Expand);
422 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
423 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i8, Expand);
424 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i16, Expand);
425 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
426 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Expand);
427 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Expand);
428 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
429 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i8, Expand);
430 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i16, Expand);
431 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
432 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i8, Expand);
433 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i16, Expand);
434 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
435 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i8, Expand);
436 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i16, Expand);
437 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
438 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i8, Expand);
439 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i16, Expand);
440 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
441 }
442 // 64-bit versions are always libcalls (for now)
443 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Expand);
444 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Expand);
445 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Expand);
446 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Expand);
447 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Expand);
448 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Expand);
449 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000450
Jim Grosbach4b77f6a2010-05-07 18:34:55 +0000451 // If the subtarget does not have extract instructions, sign_extend_inreg
452 // needs to be expanded. Extract is available in ARM mode on v6 and up,
453 // and on most Thumb2 implementations.
454 if ((!Subtarget->isThumb() && !Subtarget->hasV6Ops())
455 || (Subtarget->isThumb2() && !Subtarget->hasT2ExtractPack())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
457 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000458 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000459 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000460
David Goodwinf1daf7d2009-07-08 23:10:31 +0000461 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000462 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
463 // iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000464 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000465
466 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000468
Owen Anderson825b72b2009-08-11 20:47:22 +0000469 setOperationAction(ISD::SETCC, MVT::i32, Expand);
470 setOperationAction(ISD::SETCC, MVT::f32, Expand);
471 setOperationAction(ISD::SETCC, MVT::f64, Expand);
472 setOperationAction(ISD::SELECT, MVT::i32, Expand);
473 setOperationAction(ISD::SELECT, MVT::f32, Expand);
474 setOperationAction(ISD::SELECT, MVT::f64, Expand);
475 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
476 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
477 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000478
Owen Anderson825b72b2009-08-11 20:47:22 +0000479 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
480 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
481 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
482 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
483 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000484
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000485 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000486 setOperationAction(ISD::FSIN, MVT::f64, Expand);
487 setOperationAction(ISD::FSIN, MVT::f32, Expand);
488 setOperationAction(ISD::FCOS, MVT::f32, Expand);
489 setOperationAction(ISD::FCOS, MVT::f64, Expand);
490 setOperationAction(ISD::FREM, MVT::f64, Expand);
491 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000492 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000493 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
494 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000495 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000496 setOperationAction(ISD::FPOW, MVT::f64, Expand);
497 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000498
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000499 // Various VFP goodness
500 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000501 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
502 if (Subtarget->hasVFP2()) {
503 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
504 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
505 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
506 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
507 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000508 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000509 if (!Subtarget->hasFP16()) {
510 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
511 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000512 }
Evan Cheng110cf482008-04-01 01:50:16 +0000513 }
Evan Chenga8e29892007-01-19 07:51:42 +0000514
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000515 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000516 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000517 setTargetDAGCombine(ISD::ADD);
518 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000519 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000520
Evan Chenga8e29892007-01-19 07:51:42 +0000521 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000522
Evan Chengf7d87ee2010-05-21 00:43:17 +0000523 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
524 setSchedulingPreference(Sched::RegPressure);
525 else
526 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000527
Evan Chengbc9b7542009-08-15 07:59:10 +0000528 // FIXME: If-converter should use instruction latency to determine
529 // profitability rather than relying on fixed limits.
530 if (Subtarget->getCPUString() == "generic") {
531 // Generic (and overly aggressive) if-conversion limits.
532 setIfCvtBlockSizeLimit(10);
533 setIfCvtDupBlockSizeLimit(2);
Jim Grosbach35075a72010-03-24 16:15:14 +0000534 } else if (Subtarget->hasV7Ops()) {
Jim Grosbachfceabef2010-03-24 00:03:13 +0000535 setIfCvtBlockSizeLimit(3);
536 setIfCvtDupBlockSizeLimit(1);
Evan Chengbc9b7542009-08-15 07:59:10 +0000537 } else if (Subtarget->hasV6Ops()) {
538 setIfCvtBlockSizeLimit(2);
539 setIfCvtDupBlockSizeLimit(1);
540 } else {
541 setIfCvtBlockSizeLimit(3);
542 setIfCvtDupBlockSizeLimit(2);
Evan Cheng8557c2b2009-06-19 01:51:50 +0000543 }
544
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000545 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Bob Wilsone6abdff2009-05-18 20:55:32 +0000546 // Do not enable CodePlacementOpt for now: it currently runs after the
547 // ARMConstantIslandPass and messes up branch relaxation and placement
548 // of constant islands.
549 // benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000550}
551
Evan Chenga8e29892007-01-19 07:51:42 +0000552const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
553 switch (Opcode) {
554 default: return 0;
555 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000556 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
557 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000558 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000559 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
560 case ARMISD::tCALL: return "ARMISD::tCALL";
561 case ARMISD::BRCOND: return "ARMISD::BRCOND";
562 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000563 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000564 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
565 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
566 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000567 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000568 case ARMISD::CMPFP: return "ARMISD::CMPFP";
569 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
570 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
571 case ARMISD::CMOV: return "ARMISD::CMOV";
572 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000573
Jim Grosbach3482c802010-01-18 19:58:49 +0000574 case ARMISD::RBIT: return "ARMISD::RBIT";
575
Bob Wilson76a312b2010-03-19 22:51:32 +0000576 case ARMISD::FTOSI: return "ARMISD::FTOSI";
577 case ARMISD::FTOUI: return "ARMISD::FTOUI";
578 case ARMISD::SITOF: return "ARMISD::SITOF";
579 case ARMISD::UITOF: return "ARMISD::UITOF";
580
Evan Chenga8e29892007-01-19 07:51:42 +0000581 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
582 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
583 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000584
Jim Grosbache5165492009-11-09 00:11:35 +0000585 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
586 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000587
Evan Chengc5942082009-10-28 06:55:03 +0000588 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
589 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
590
Dale Johannesen51e28e62010-06-03 21:09:53 +0000591 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
592
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000593 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000594
Evan Cheng86198642009-08-07 00:34:42 +0000595 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
596
Jim Grosbach3728e962009-12-10 00:11:09 +0000597 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
598 case ARMISD::SYNCBARRIER: return "ARMISD::SYNCBARRIER";
599
Bob Wilson5bafff32009-06-22 23:27:02 +0000600 case ARMISD::VCEQ: return "ARMISD::VCEQ";
601 case ARMISD::VCGE: return "ARMISD::VCGE";
602 case ARMISD::VCGEU: return "ARMISD::VCGEU";
603 case ARMISD::VCGT: return "ARMISD::VCGT";
604 case ARMISD::VCGTU: return "ARMISD::VCGTU";
605 case ARMISD::VTST: return "ARMISD::VTST";
606
607 case ARMISD::VSHL: return "ARMISD::VSHL";
608 case ARMISD::VSHRs: return "ARMISD::VSHRs";
609 case ARMISD::VSHRu: return "ARMISD::VSHRu";
610 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
611 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
612 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
613 case ARMISD::VSHRN: return "ARMISD::VSHRN";
614 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
615 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
616 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
617 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
618 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
619 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
620 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
621 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
622 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
623 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
624 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
625 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
626 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
627 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000628 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000629 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000630 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000631 case ARMISD::VREV64: return "ARMISD::VREV64";
632 case ARMISD::VREV32: return "ARMISD::VREV32";
633 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000634 case ARMISD::VZIP: return "ARMISD::VZIP";
635 case ARMISD::VUZP: return "ARMISD::VUZP";
636 case ARMISD::VTRN: return "ARMISD::VTRN";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000637 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000638 case ARMISD::FMAX: return "ARMISD::FMAX";
639 case ARMISD::FMIN: return "ARMISD::FMIN";
Evan Chenga8e29892007-01-19 07:51:42 +0000640 }
641}
642
Evan Cheng06b666c2010-05-15 02:18:07 +0000643/// getRegClassFor - Return the register class that should be used for the
644/// specified value type.
645TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
646 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
647 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
648 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000649 if (Subtarget->hasNEON()) {
650 if (VT == MVT::v4i64)
651 return ARM::QQPRRegisterClass;
652 else if (VT == MVT::v8i64)
653 return ARM::QQQQPRRegisterClass;
654 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000655 return TargetLowering::getRegClassFor(VT);
656}
657
Bill Wendlingb4202b82009-07-01 18:50:55 +0000658/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000659unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Evan Cheng048e36f2009-10-02 06:57:25 +0000660 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 0 : 1;
Bill Wendling20c568f2009-06-30 22:38:32 +0000661}
662
Evan Cheng1cc39842010-05-20 23:26:43 +0000663Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000664 unsigned NumVals = N->getNumValues();
665 if (!NumVals)
666 return Sched::RegPressure;
667
668 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000669 EVT VT = N->getValueType(i);
670 if (VT.isFloatingPoint() || VT.isVector())
671 return Sched::Latency;
672 }
Evan Chengc10f5432010-05-28 23:25:23 +0000673
674 if (!N->isMachineOpcode())
675 return Sched::RegPressure;
676
677 // Load are scheduled for latency even if there instruction itinerary
678 // is not available.
679 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
680 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
681 if (TID.mayLoad())
682 return Sched::Latency;
683
684 const InstrItineraryData &Itins = getTargetMachine().getInstrItineraryData();
685 if (!Itins.isEmpty() && Itins.getStageLatency(TID.getSchedClass()) > 2)
686 return Sched::Latency;
Evan Cheng1cc39842010-05-20 23:26:43 +0000687 return Sched::RegPressure;
688}
689
Evan Chenga8e29892007-01-19 07:51:42 +0000690//===----------------------------------------------------------------------===//
691// Lowering Code
692//===----------------------------------------------------------------------===//
693
Evan Chenga8e29892007-01-19 07:51:42 +0000694/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
695static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
696 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000697 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000698 case ISD::SETNE: return ARMCC::NE;
699 case ISD::SETEQ: return ARMCC::EQ;
700 case ISD::SETGT: return ARMCC::GT;
701 case ISD::SETGE: return ARMCC::GE;
702 case ISD::SETLT: return ARMCC::LT;
703 case ISD::SETLE: return ARMCC::LE;
704 case ISD::SETUGT: return ARMCC::HI;
705 case ISD::SETUGE: return ARMCC::HS;
706 case ISD::SETULT: return ARMCC::LO;
707 case ISD::SETULE: return ARMCC::LS;
708 }
709}
710
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000711/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
712static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000713 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000714 CondCode2 = ARMCC::AL;
715 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000716 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000717 case ISD::SETEQ:
718 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
719 case ISD::SETGT:
720 case ISD::SETOGT: CondCode = ARMCC::GT; break;
721 case ISD::SETGE:
722 case ISD::SETOGE: CondCode = ARMCC::GE; break;
723 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000724 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000725 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
726 case ISD::SETO: CondCode = ARMCC::VC; break;
727 case ISD::SETUO: CondCode = ARMCC::VS; break;
728 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
729 case ISD::SETUGT: CondCode = ARMCC::HI; break;
730 case ISD::SETUGE: CondCode = ARMCC::PL; break;
731 case ISD::SETLT:
732 case ISD::SETULT: CondCode = ARMCC::LT; break;
733 case ISD::SETLE:
734 case ISD::SETULE: CondCode = ARMCC::LE; break;
735 case ISD::SETNE:
736 case ISD::SETUNE: CondCode = ARMCC::NE; break;
737 }
Evan Chenga8e29892007-01-19 07:51:42 +0000738}
739
Bob Wilson1f595bb2009-04-17 19:07:39 +0000740//===----------------------------------------------------------------------===//
741// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000742//===----------------------------------------------------------------------===//
743
744#include "ARMGenCallingConv.inc"
745
746// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000747static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000748 CCValAssign::LocInfo &LocInfo,
749 CCState &State, bool CanFail) {
750 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
751
752 // Try to get the first register.
753 if (unsigned Reg = State.AllocateReg(RegList, 4))
754 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
755 else {
756 // For the 2nd half of a v2f64, do not fail.
757 if (CanFail)
758 return false;
759
760 // Put the whole thing on the stack.
761 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
762 State.AllocateStack(8, 4),
763 LocVT, LocInfo));
764 return true;
765 }
766
767 // Try to get the second register.
768 if (unsigned Reg = State.AllocateReg(RegList, 4))
769 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
770 else
771 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
772 State.AllocateStack(4, 4),
773 LocVT, LocInfo));
774 return true;
775}
776
Owen Andersone50ed302009-08-10 22:56:29 +0000777static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000778 CCValAssign::LocInfo &LocInfo,
779 ISD::ArgFlagsTy &ArgFlags,
780 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000781 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
782 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000783 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000784 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
785 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000786 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000787}
788
789// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000790static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000791 CCValAssign::LocInfo &LocInfo,
792 CCState &State, bool CanFail) {
793 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
794 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
795
796 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
797 if (Reg == 0) {
798 // For the 2nd half of a v2f64, do not just fail.
799 if (CanFail)
800 return false;
801
802 // Put the whole thing on the stack.
803 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
804 State.AllocateStack(8, 8),
805 LocVT, LocInfo));
806 return true;
807 }
808
809 unsigned i;
810 for (i = 0; i < 2; ++i)
811 if (HiRegList[i] == Reg)
812 break;
813
814 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
815 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
816 LocVT, LocInfo));
817 return true;
818}
819
Owen Andersone50ed302009-08-10 22:56:29 +0000820static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000821 CCValAssign::LocInfo &LocInfo,
822 ISD::ArgFlagsTy &ArgFlags,
823 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000824 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
825 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000826 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000827 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
828 return false;
829 return true; // we handled it
830}
831
Owen Andersone50ed302009-08-10 22:56:29 +0000832static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000833 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000834 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
835 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
836
Bob Wilsone65586b2009-04-17 20:40:45 +0000837 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
838 if (Reg == 0)
839 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000840
Bob Wilsone65586b2009-04-17 20:40:45 +0000841 unsigned i;
842 for (i = 0; i < 2; ++i)
843 if (HiRegList[i] == Reg)
844 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000845
Bob Wilson5bafff32009-06-22 23:27:02 +0000846 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000847 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000848 LocVT, LocInfo));
849 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000850}
851
Owen Andersone50ed302009-08-10 22:56:29 +0000852static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000853 CCValAssign::LocInfo &LocInfo,
854 ISD::ArgFlagsTy &ArgFlags,
855 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000856 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
857 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000858 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000859 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000860 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000861}
862
Owen Andersone50ed302009-08-10 22:56:29 +0000863static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000864 CCValAssign::LocInfo &LocInfo,
865 ISD::ArgFlagsTy &ArgFlags,
866 CCState &State) {
867 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
868 State);
869}
870
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000871/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
872/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000873CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000874 bool Return,
875 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000876 switch (CC) {
877 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000878 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000879 case CallingConv::C:
880 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000881 // Use target triple & subtarget features to do actual dispatch.
882 if (Subtarget->isAAPCS_ABI()) {
883 if (Subtarget->hasVFP2() &&
884 FloatABIType == FloatABI::Hard && !isVarArg)
885 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
886 else
887 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
888 } else
889 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000890 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000891 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000892 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000893 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000894 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000895 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000896 }
897}
898
Dan Gohman98ca4f22009-08-05 01:29:28 +0000899/// LowerCallResult - Lower the result values of a call into the
900/// appropriate copies out of appropriate physical registers.
901SDValue
902ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000903 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000904 const SmallVectorImpl<ISD::InputArg> &Ins,
905 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000906 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000907
Bob Wilson1f595bb2009-04-17 19:07:39 +0000908 // Assign locations to each value returned by this call.
909 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000910 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000911 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000912 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000913 CCAssignFnForNode(CallConv, /* Return*/ true,
914 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000915
916 // Copy all of the result registers out of their specified physreg.
917 for (unsigned i = 0; i != RVLocs.size(); ++i) {
918 CCValAssign VA = RVLocs[i];
919
Bob Wilson80915242009-04-25 00:33:20 +0000920 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000921 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000922 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000923 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000924 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000925 Chain = Lo.getValue(1);
926 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000927 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000928 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000929 InFlag);
930 Chain = Hi.getValue(1);
931 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000932 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000933
Owen Anderson825b72b2009-08-11 20:47:22 +0000934 if (VA.getLocVT() == MVT::v2f64) {
935 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
936 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
937 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000938
939 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000940 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000941 Chain = Lo.getValue(1);
942 InFlag = Lo.getValue(2);
943 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000944 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000945 Chain = Hi.getValue(1);
946 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000947 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +0000948 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
949 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000950 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000951 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000952 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
953 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000954 Chain = Val.getValue(1);
955 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000956 }
Bob Wilson80915242009-04-25 00:33:20 +0000957
958 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000959 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000960 case CCValAssign::Full: break;
961 case CCValAssign::BCvt:
962 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
963 break;
964 }
965
Dan Gohman98ca4f22009-08-05 01:29:28 +0000966 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000967 }
968
Dan Gohman98ca4f22009-08-05 01:29:28 +0000969 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000970}
971
972/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
973/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000974/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000975/// a byval function parameter.
976/// Sometimes what we are copying is the end of a larger object, the part that
977/// does not fit in registers.
978static SDValue
979CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
980 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
981 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000982 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000983 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +0000984 /*isVolatile=*/false, /*AlwaysInline=*/false,
985 NULL, 0, NULL, 0);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000986}
987
Bob Wilsondee46d72009-04-17 20:35:10 +0000988/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000989SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000990ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
991 SDValue StackPtr, SDValue Arg,
992 DebugLoc dl, SelectionDAG &DAG,
993 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +0000994 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000995 unsigned LocMemOffset = VA.getLocMemOffset();
996 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
997 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
998 if (Flags.isByVal()) {
999 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
1000 }
1001 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene1b58cab2010-02-15 16:55:24 +00001002 PseudoSourceValue::getStack(), LocMemOffset,
1003 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001004}
1005
Dan Gohman98ca4f22009-08-05 01:29:28 +00001006void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001007 SDValue Chain, SDValue &Arg,
1008 RegsToPassVector &RegsToPass,
1009 CCValAssign &VA, CCValAssign &NextVA,
1010 SDValue &StackPtr,
1011 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001012 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001013
Jim Grosbache5165492009-11-09 00:11:35 +00001014 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001015 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001016 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1017
1018 if (NextVA.isRegLoc())
1019 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1020 else {
1021 assert(NextVA.isMemLoc());
1022 if (StackPtr.getNode() == 0)
1023 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1024
Dan Gohman98ca4f22009-08-05 01:29:28 +00001025 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1026 dl, DAG, NextVA,
1027 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001028 }
1029}
1030
Dan Gohman98ca4f22009-08-05 01:29:28 +00001031/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001032/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1033/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001034SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001035ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001036 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001037 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001038 const SmallVectorImpl<ISD::OutputArg> &Outs,
1039 const SmallVectorImpl<ISD::InputArg> &Ins,
1040 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001041 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001042 MachineFunction &MF = DAG.getMachineFunction();
1043 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1044 bool IsSibCall = false;
Dale Johannesen8fa8e7f2010-06-04 18:04:24 +00001045 // Temporarily disable tail calls so things don't break.
1046 if (!EnableARMTailCalls)
1047 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001048 if (isTailCall) {
1049 // Check if it's really possible to do a tail call.
1050 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1051 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
1052 Outs, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001053 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1054 // detected sibcalls.
1055 if (isTailCall) {
1056 ++NumTailCalls;
1057 IsSibCall = true;
1058 }
1059 }
Evan Chenga8e29892007-01-19 07:51:42 +00001060
Bob Wilson1f595bb2009-04-17 19:07:39 +00001061 // Analyze operands of the call, assigning locations to each operand.
1062 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001063 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1064 *DAG.getContext());
1065 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001066 CCAssignFnForNode(CallConv, /* Return*/ false,
1067 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001068
Bob Wilson1f595bb2009-04-17 19:07:39 +00001069 // Get a count of how many bytes are to be pushed on the stack.
1070 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001071
Dale Johannesen51e28e62010-06-03 21:09:53 +00001072 // For tail calls, memory operands are available in our caller's stack.
1073 if (IsSibCall)
1074 NumBytes = 0;
1075
Evan Chenga8e29892007-01-19 07:51:42 +00001076 // Adjust the stack pointer for the new arguments...
1077 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001078 if (!IsSibCall)
1079 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001080
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001081 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001082
Bob Wilson5bafff32009-06-22 23:27:02 +00001083 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001084 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001085
Bob Wilson1f595bb2009-04-17 19:07:39 +00001086 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001087 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001088 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1089 i != e;
1090 ++i, ++realArgIdx) {
1091 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001092 SDValue Arg = Outs[realArgIdx].Val;
1093 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +00001094
Bob Wilson1f595bb2009-04-17 19:07:39 +00001095 // Promote the value if needed.
1096 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001097 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001098 case CCValAssign::Full: break;
1099 case CCValAssign::SExt:
1100 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1101 break;
1102 case CCValAssign::ZExt:
1103 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1104 break;
1105 case CCValAssign::AExt:
1106 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1107 break;
1108 case CCValAssign::BCvt:
1109 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1110 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001111 }
1112
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001113 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001114 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001115 if (VA.getLocVT() == MVT::v2f64) {
1116 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1117 DAG.getConstant(0, MVT::i32));
1118 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1119 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001120
Dan Gohman98ca4f22009-08-05 01:29:28 +00001121 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001122 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1123
1124 VA = ArgLocs[++i]; // skip ahead to next loc
1125 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001126 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001127 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1128 } else {
1129 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001130
Dan Gohman98ca4f22009-08-05 01:29:28 +00001131 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1132 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001133 }
1134 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001135 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001136 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001137 }
1138 } else if (VA.isRegLoc()) {
1139 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001140 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001141 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001142
Dan Gohman98ca4f22009-08-05 01:29:28 +00001143 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1144 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001145 }
Evan Chenga8e29892007-01-19 07:51:42 +00001146 }
1147
1148 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001149 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001150 &MemOpChains[0], MemOpChains.size());
1151
1152 // Build a sequence of copy-to-reg nodes chained together with token chain
1153 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001154 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001155 // Tail call byval lowering might overwrite argument registers so in case of
1156 // tail call optimization the copies to registers are lowered later.
1157 if (!isTailCall)
1158 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1159 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1160 RegsToPass[i].second, InFlag);
1161 InFlag = Chain.getValue(1);
1162 }
Evan Chenga8e29892007-01-19 07:51:42 +00001163
Dale Johannesen51e28e62010-06-03 21:09:53 +00001164 // For tail calls lower the arguments to the 'real' stack slot.
1165 if (isTailCall) {
1166 // Force all the incoming stack arguments to be loaded from the stack
1167 // before any new outgoing arguments are stored to the stack, because the
1168 // outgoing stack slots may alias the incoming argument stack slots, and
1169 // the alias isn't otherwise explicit. This is slightly more conservative
1170 // than necessary, because it means that each store effectively depends
1171 // on every argument instead of just those arguments it would clobber.
1172
1173 // Do not flag preceeding copytoreg stuff together with the following stuff.
1174 InFlag = SDValue();
1175 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1176 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1177 RegsToPass[i].second, InFlag);
1178 InFlag = Chain.getValue(1);
1179 }
1180 InFlag =SDValue();
1181 }
1182
Bill Wendling056292f2008-09-16 21:48:12 +00001183 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1184 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1185 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001186 bool isDirect = false;
1187 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001188 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001189 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001190
1191 if (EnableARMLongCalls) {
1192 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1193 && "long-calls with non-static relocation model!");
1194 // Handle a global address or an external symbol. If it's not one of
1195 // those, the target's already in a register, so we don't need to do
1196 // anything extra.
1197 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001198 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001199 // Create a constant pool entry for the callee address
1200 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1201 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1202 ARMPCLabelIndex,
1203 ARMCP::CPValue, 0);
1204 // Get the address of the callee into a register
1205 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1206 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1207 Callee = DAG.getLoad(getPointerTy(), dl,
1208 DAG.getEntryNode(), CPAddr,
1209 PseudoSourceValue::getConstantPool(), 0,
1210 false, false, 0);
1211 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1212 const char *Sym = S->getSymbol();
1213
1214 // Create a constant pool entry for the callee address
1215 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1216 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1217 Sym, ARMPCLabelIndex, 0);
1218 // Get the address of the callee into a register
1219 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1220 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1221 Callee = DAG.getLoad(getPointerTy(), dl,
1222 DAG.getEntryNode(), CPAddr,
1223 PseudoSourceValue::getConstantPool(), 0,
1224 false, false, 0);
1225 }
1226 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001227 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001228 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001229 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001230 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001231 getTargetMachine().getRelocationModel() != Reloc::Static;
1232 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001233 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001234 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001235 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001236 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001237 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001238 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001239 ARMPCLabelIndex,
1240 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001241 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001242 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001243 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001244 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001245 PseudoSourceValue::getConstantPool(), 0,
1246 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001247 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001248 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001249 getPointerTy(), Callee, PICLabel);
Jim Grosbache7b52522010-04-14 22:28:31 +00001250 } else
Evan Chengc60e76d2007-01-30 20:37:08 +00001251 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001252 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001253 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001254 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001255 getTargetMachine().getRelocationModel() != Reloc::Static;
1256 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001257 // tBX takes a register source operand.
1258 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001259 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001260 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001261 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001262 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001263 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001264 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001265 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001266 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001267 PseudoSourceValue::getConstantPool(), 0,
1268 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001269 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001270 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001271 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001272 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001273 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001274 }
1275
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001276 // FIXME: handle tail calls differently.
1277 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001278 if (Subtarget->isThumb()) {
1279 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001280 CallOpc = ARMISD::CALL_NOLINK;
1281 else
1282 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1283 } else {
1284 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001285 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1286 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001287 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001288 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001289 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001290 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001291 InFlag = Chain.getValue(1);
1292 }
1293
Dan Gohman475871a2008-07-27 21:46:04 +00001294 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001295 Ops.push_back(Chain);
1296 Ops.push_back(Callee);
1297
1298 // Add argument registers to the end of the list so that they are known live
1299 // into the call.
1300 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1301 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1302 RegsToPass[i].second.getValueType()));
1303
Gabor Greifba36cb52008-08-28 21:40:38 +00001304 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001305 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001306
1307 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001308 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001309 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001310
Duncan Sands4bdcb612008-07-02 17:40:58 +00001311 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001312 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001313 InFlag = Chain.getValue(1);
1314
Chris Lattnere563bbc2008-10-11 22:08:30 +00001315 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1316 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001317 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001318 InFlag = Chain.getValue(1);
1319
Bob Wilson1f595bb2009-04-17 19:07:39 +00001320 // Handle result values, copying them out of physregs into vregs that we
1321 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001322 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1323 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001324}
1325
Dale Johannesen51e28e62010-06-03 21:09:53 +00001326/// MatchingStackOffset - Return true if the given stack call argument is
1327/// already available in the same position (relatively) of the caller's
1328/// incoming argument stack.
1329static
1330bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1331 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1332 const ARMInstrInfo *TII) {
1333 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1334 int FI = INT_MAX;
1335 if (Arg.getOpcode() == ISD::CopyFromReg) {
1336 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
1337 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
1338 return false;
1339 MachineInstr *Def = MRI->getVRegDef(VR);
1340 if (!Def)
1341 return false;
1342 if (!Flags.isByVal()) {
1343 if (!TII->isLoadFromStackSlot(Def, FI))
1344 return false;
1345 } else {
1346// unsigned Opcode = Def->getOpcode();
1347// if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
1348// Def->getOperand(1).isFI()) {
1349// FI = Def->getOperand(1).getIndex();
1350// Bytes = Flags.getByValSize();
1351// } else
1352 return false;
1353 }
1354 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1355 if (Flags.isByVal())
1356 // ByVal argument is passed in as a pointer but it's now being
1357 // dereferenced. e.g.
1358 // define @foo(%struct.X* %A) {
1359 // tail call @bar(%struct.X* byval %A)
1360 // }
1361 return false;
1362 SDValue Ptr = Ld->getBasePtr();
1363 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1364 if (!FINode)
1365 return false;
1366 FI = FINode->getIndex();
1367 } else
1368 return false;
1369
1370 assert(FI != INT_MAX);
1371 if (!MFI->isFixedObjectIndex(FI))
1372 return false;
1373 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1374}
1375
1376/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1377/// for tail call optimization. Targets which want to do tail call
1378/// optimization should implement this function.
1379bool
1380ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1381 CallingConv::ID CalleeCC,
1382 bool isVarArg,
1383 bool isCalleeStructRet,
1384 bool isCallerStructRet,
1385 const SmallVectorImpl<ISD::OutputArg> &Outs,
1386 const SmallVectorImpl<ISD::InputArg> &Ins,
1387 SelectionDAG& DAG) const {
1388
Dale Johannesen51e28e62010-06-03 21:09:53 +00001389 const Function *CallerF = DAG.getMachineFunction().getFunction();
1390 CallingConv::ID CallerCC = CallerF->getCallingConv();
1391 bool CCMatch = CallerCC == CalleeCC;
1392
1393 // Look for obvious safe cases to perform tail call optimization that do not
1394 // require ABI changes. This is what gcc calls sibcall.
1395
Jim Grosbach7616b642010-06-16 23:45:49 +00001396 // Do not sibcall optimize vararg calls unless the call site is not passing
1397 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001398 if (isVarArg && !Outs.empty())
1399 return false;
1400
1401 // Also avoid sibcall optimization if either caller or callee uses struct
1402 // return semantics.
1403 if (isCalleeStructRet || isCallerStructRet)
1404 return false;
1405
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001406 // On Thumb, for the moment, we can only do this to functions defined in this
1407 // compilation, or to indirect calls. A Thumb B to an ARM function is not
1408 // easily fixed up in the linker, unlike BL.
1409 if (Subtarget->isThumb()) {
1410 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1411 const GlobalValue *GV = G->getGlobal();
1412 if (GV->isDeclaration() || GV->isWeakForLinker())
1413 return false;
1414 } else if (isa<ExternalSymbolSDNode>(Callee)) {
1415 return false;
1416 }
1417 }
1418
Dale Johannesen51e28e62010-06-03 21:09:53 +00001419 // If the calling conventions do not match, then we'd better make sure the
1420 // results are returned in the same way as what the caller expects.
1421 if (!CCMatch) {
1422 SmallVector<CCValAssign, 16> RVLocs1;
1423 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
1424 RVLocs1, *DAG.getContext());
1425 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1426
1427 SmallVector<CCValAssign, 16> RVLocs2;
1428 CCState CCInfo2(CallerCC, false, getTargetMachine(),
1429 RVLocs2, *DAG.getContext());
1430 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1431
1432 if (RVLocs1.size() != RVLocs2.size())
1433 return false;
1434 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1435 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1436 return false;
1437 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1438 return false;
1439 if (RVLocs1[i].isRegLoc()) {
1440 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1441 return false;
1442 } else {
1443 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1444 return false;
1445 }
1446 }
1447 }
1448
1449 // If the callee takes no arguments then go on to check the results of the
1450 // call.
1451 if (!Outs.empty()) {
1452 // Check if stack adjustment is needed. For now, do not do this if any
1453 // argument is passed on the stack.
1454 SmallVector<CCValAssign, 16> ArgLocs;
1455 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
1456 ArgLocs, *DAG.getContext());
1457 CCInfo.AnalyzeCallOperands(Outs,
1458 CCAssignFnForNode(CalleeCC, false, isVarArg));
1459 if (CCInfo.getNextStackOffset()) {
1460 MachineFunction &MF = DAG.getMachineFunction();
1461
1462 // Check if the arguments are already laid out in the right way as
1463 // the caller's fixed stack objects.
1464 MachineFrameInfo *MFI = MF.getFrameInfo();
1465 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1466 const ARMInstrInfo *TII =
1467 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001468 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1469 i != e;
1470 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001471 CCValAssign &VA = ArgLocs[i];
1472 EVT RegVT = VA.getLocVT();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001473 SDValue Arg = Outs[realArgIdx].Val;
1474 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001475 if (VA.getLocInfo() == CCValAssign::Indirect)
1476 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001477 if (VA.needsCustom()) {
1478 // f64 and vector types are split into multiple registers or
1479 // register/stack-slot combinations. The types will not match
1480 // the registers; give up on memory f64 refs until we figure
1481 // out what to do about this.
1482 if (!VA.isRegLoc())
1483 return false;
1484 if (!ArgLocs[++i].isRegLoc())
1485 return false;
1486 if (RegVT == MVT::v2f64) {
1487 if (!ArgLocs[++i].isRegLoc())
1488 return false;
1489 if (!ArgLocs[++i].isRegLoc())
1490 return false;
1491 }
1492 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001493 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1494 MFI, MRI, TII))
1495 return false;
1496 }
1497 }
1498 }
1499 }
1500
1501 return true;
1502}
1503
Dan Gohman98ca4f22009-08-05 01:29:28 +00001504SDValue
1505ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001506 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001507 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmand858e902010-04-17 15:26:15 +00001508 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001509
Bob Wilsondee46d72009-04-17 20:35:10 +00001510 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001511 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001512
Bob Wilsondee46d72009-04-17 20:35:10 +00001513 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001514 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1515 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001516
Dan Gohman98ca4f22009-08-05 01:29:28 +00001517 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001518 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1519 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001520
1521 // If this is the first return lowered for this function, add
1522 // the regs to the liveout set for the function.
1523 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1524 for (unsigned i = 0; i != RVLocs.size(); ++i)
1525 if (RVLocs[i].isRegLoc())
1526 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001527 }
1528
Bob Wilson1f595bb2009-04-17 19:07:39 +00001529 SDValue Flag;
1530
1531 // Copy the result values into the output registers.
1532 for (unsigned i = 0, realRVLocIdx = 0;
1533 i != RVLocs.size();
1534 ++i, ++realRVLocIdx) {
1535 CCValAssign &VA = RVLocs[i];
1536 assert(VA.isRegLoc() && "Can only return in registers!");
1537
Dan Gohman98ca4f22009-08-05 01:29:28 +00001538 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001539
1540 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001541 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001542 case CCValAssign::Full: break;
1543 case CCValAssign::BCvt:
1544 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1545 break;
1546 }
1547
Bob Wilson1f595bb2009-04-17 19:07:39 +00001548 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001549 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001550 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001551 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1552 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001553 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001554 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001555
1556 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1557 Flag = Chain.getValue(1);
1558 VA = RVLocs[++i]; // skip ahead to next loc
1559 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1560 HalfGPRs.getValue(1), Flag);
1561 Flag = Chain.getValue(1);
1562 VA = RVLocs[++i]; // skip ahead to next loc
1563
1564 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001565 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1566 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001567 }
1568 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1569 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001570 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001571 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001572 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001573 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001574 VA = RVLocs[++i]; // skip ahead to next loc
1575 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1576 Flag);
1577 } else
1578 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1579
Bob Wilsondee46d72009-04-17 20:35:10 +00001580 // Guarantee that all emitted copies are
1581 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001582 Flag = Chain.getValue(1);
1583 }
1584
1585 SDValue result;
1586 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001587 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001588 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001589 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001590
1591 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001592}
1593
Bob Wilsonb62d2572009-11-03 00:02:05 +00001594// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1595// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1596// one of the above mentioned nodes. It has to be wrapped because otherwise
1597// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1598// be used to form addressing mode. These wrapped nodes will be selected
1599// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001600static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001601 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001602 // FIXME there is no actual debug info here
1603 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001604 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001605 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001606 if (CP->isMachineConstantPoolEntry())
1607 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1608 CP->getAlignment());
1609 else
1610 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1611 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001612 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001613}
1614
Dan Gohmand858e902010-04-17 15:26:15 +00001615SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1616 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001617 MachineFunction &MF = DAG.getMachineFunction();
1618 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1619 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001620 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001621 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001622 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001623 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1624 SDValue CPAddr;
1625 if (RelocM == Reloc::Static) {
1626 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1627 } else {
1628 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001629 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001630 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1631 ARMCP::CPBlockAddress,
1632 PCAdj);
1633 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1634 }
1635 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1636 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001637 PseudoSourceValue::getConstantPool(), 0,
1638 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001639 if (RelocM == Reloc::Static)
1640 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001641 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001642 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001643}
1644
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001645// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001646SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001647ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001648 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001649 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001650 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001651 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001652 MachineFunction &MF = DAG.getMachineFunction();
1653 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1654 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001655 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001656 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001657 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001658 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001659 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001660 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
David Greene1b58cab2010-02-15 16:55:24 +00001661 PseudoSourceValue::getConstantPool(), 0,
1662 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001663 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001664
Evan Chenge7e0d622009-11-06 22:24:13 +00001665 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001666 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001667
1668 // call __tls_get_addr.
1669 ArgListTy Args;
1670 ArgListEntry Entry;
1671 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001672 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001673 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001674 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001675 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001676 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1677 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001678 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001679 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001680 return CallResult.first;
1681}
1682
1683// Lower ISD::GlobalTLSAddress using the "initial exec" or
1684// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001685SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001686ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001687 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001688 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001689 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001690 SDValue Offset;
1691 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001692 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001693 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001694 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001695
Chris Lattner4fb63d02009-07-15 04:12:33 +00001696 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001697 MachineFunction &MF = DAG.getMachineFunction();
1698 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1699 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1700 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001701 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1702 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001703 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001704 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001705 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001706 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001707 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001708 PseudoSourceValue::getConstantPool(), 0,
1709 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001710 Chain = Offset.getValue(1);
1711
Evan Chenge7e0d622009-11-06 22:24:13 +00001712 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001713 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001714
Evan Cheng9eda6892009-10-31 03:39:36 +00001715 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001716 PseudoSourceValue::getConstantPool(), 0,
1717 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001718 } else {
1719 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001720 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001721 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001722 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001723 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001724 PseudoSourceValue::getConstantPool(), 0,
1725 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001726 }
1727
1728 // The address of the thread local variable is the add of the thread
1729 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001730 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001731}
1732
Dan Gohman475871a2008-07-27 21:46:04 +00001733SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001734ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001735 // TODO: implement the "local dynamic" model
1736 assert(Subtarget->isTargetELF() &&
1737 "TLS not implemented for non-ELF targets");
1738 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1739 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1740 // otherwise use the "Local Exec" TLS Model
1741 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1742 return LowerToTLSGeneralDynamicModel(GA, DAG);
1743 else
1744 return LowerToTLSExecModels(GA, DAG);
1745}
1746
Dan Gohman475871a2008-07-27 21:46:04 +00001747SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001748 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001749 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001750 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001751 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001752 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1753 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001754 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001755 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001756 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001757 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001758 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001759 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001760 CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001761 PseudoSourceValue::getConstantPool(), 0,
1762 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001763 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001764 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001765 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001766 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001767 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001768 PseudoSourceValue::getGOT(), 0,
1769 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001770 return Result;
1771 } else {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001772 // If we have T2 ops, we can materialize the address directly via movt/movw
1773 // pair. This is always cheaper.
1774 if (Subtarget->useMovt()) {
1775 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
1776 DAG.getTargetGlobalAddress(GV, PtrVT));
1777 } else {
1778 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1779 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1780 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001781 PseudoSourceValue::getConstantPool(), 0,
1782 false, false, 0);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001783 }
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001784 }
1785}
1786
Dan Gohman475871a2008-07-27 21:46:04 +00001787SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001788 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001789 MachineFunction &MF = DAG.getMachineFunction();
1790 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1791 unsigned ARMPCLabelIndex = 0;
Owen Andersone50ed302009-08-10 22:56:29 +00001792 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001793 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001794 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001795 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001796 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001797 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001798 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001799 else {
Evan Chenge7e0d622009-11-06 22:24:13 +00001800 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001801 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1802 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001803 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001804 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001805 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001806 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001807
Evan Cheng9eda6892009-10-31 03:39:36 +00001808 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001809 PseudoSourceValue::getConstantPool(), 0,
1810 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001811 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001812
1813 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001814 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001815 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001816 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001817
Evan Cheng63476a82009-09-03 07:04:02 +00001818 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Evan Cheng9eda6892009-10-31 03:39:36 +00001819 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001820 PseudoSourceValue::getGOT(), 0,
1821 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001822
1823 return Result;
1824}
1825
Dan Gohman475871a2008-07-27 21:46:04 +00001826SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001827 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001828 assert(Subtarget->isTargetELF() &&
1829 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00001830 MachineFunction &MF = DAG.getMachineFunction();
1831 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1832 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Andersone50ed302009-08-10 22:56:29 +00001833 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001834 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001835 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001836 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1837 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001838 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001839 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001840 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001841 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001842 PseudoSourceValue::getConstantPool(), 0,
1843 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001844 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001845 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001846}
1847
Jim Grosbach0e0da732009-05-12 23:59:14 +00001848SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00001849ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
1850 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00001851 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00001852 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
1853 Op.getOperand(1), Val);
1854}
1855
1856SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00001857ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
1858 DebugLoc dl = Op.getDebugLoc();
1859 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
1860 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
1861}
1862
1863SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00001864ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00001865 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001866 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001867 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001868 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001869 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001870 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001871 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001872 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1873 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001874 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001875 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00001876 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1877 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001878 EVT PtrVT = getPointerTy();
1879 DebugLoc dl = Op.getDebugLoc();
1880 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1881 SDValue CPAddr;
1882 unsigned PCAdj = (RelocM != Reloc::PIC_)
1883 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001884 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001885 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1886 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001887 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001888 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001889 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00001890 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001891 PseudoSourceValue::getConstantPool(), 0,
1892 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001893 SDValue Chain = Result.getValue(1);
1894
1895 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001896 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001897 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1898 }
1899 return Result;
1900 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001901 }
1902}
1903
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001904static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00001905 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00001906 DebugLoc dl = Op.getDebugLoc();
1907 SDValue Op5 = Op.getOperand(5);
Jim Grosbach3728e962009-12-10 00:11:09 +00001908 unsigned isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue();
Jim Grosbachc73993b2010-06-17 01:37:00 +00001909 // v6 and v7 can both handle barriers directly, but need handled a bit
1910 // differently. Thumb1 and pre-v6 ARM mode use a libcall instead and should
1911 // never get here.
1912 unsigned Opc = isDeviceBarrier ? ARMISD::SYNCBARRIER : ARMISD::MEMBARRIER;
1913 if (Subtarget->hasV7Ops())
1914 return DAG.getNode(Opc, dl, MVT::Other, Op.getOperand(0));
1915 else if (Subtarget->hasV6Ops() && !Subtarget->isThumb1Only())
1916 return DAG.getNode(Opc, dl, MVT::Other, Op.getOperand(0),
1917 DAG.getConstant(0, MVT::i32));
1918 assert(0 && "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
1919 return SDValue();
Jim Grosbach3728e962009-12-10 00:11:09 +00001920}
1921
Dan Gohman1e93df62010-04-17 14:41:14 +00001922static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
1923 MachineFunction &MF = DAG.getMachineFunction();
1924 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
1925
Evan Chenga8e29892007-01-19 07:51:42 +00001926 // vastart just stores the address of the VarArgsFrameIndex slot into the
1927 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001928 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001929 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00001930 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001931 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
David Greene1b58cab2010-02-15 16:55:24 +00001932 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
1933 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001934}
1935
Dan Gohman475871a2008-07-27 21:46:04 +00001936SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001937ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1938 SelectionDAG &DAG) const {
Evan Cheng86198642009-08-07 00:34:42 +00001939 SDNode *Node = Op.getNode();
1940 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001941 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001942 SDValue Chain = Op.getOperand(0);
1943 SDValue Size = Op.getOperand(1);
1944 SDValue Align = Op.getOperand(2);
1945
1946 // Chain the dynamic stack allocation so that it doesn't modify the stack
1947 // pointer when other instructions are using the stack.
1948 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1949
1950 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1951 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1952 if (AlignVal > StackAlign)
1953 // Do this now since selection pass cannot introduce new target
1954 // independent node.
1955 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1956
1957 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1958 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1959 // do even more horrible hack later.
1960 MachineFunction &MF = DAG.getMachineFunction();
1961 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1962 if (AFI->isThumb1OnlyFunction()) {
1963 bool Negate = true;
1964 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1965 if (C) {
1966 uint32_t Val = C->getZExtValue();
1967 if (Val <= 508 && ((Val & 3) == 0))
1968 Negate = false;
1969 }
1970 if (Negate)
1971 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1972 }
1973
Owen Anderson825b72b2009-08-11 20:47:22 +00001974 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001975 SDValue Ops1[] = { Chain, Size, Align };
1976 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1977 Chain = Res.getValue(1);
1978 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1979 DAG.getIntPtrConstant(0, true), SDValue());
1980 SDValue Ops2[] = { Res, Chain };
1981 return DAG.getMergeValues(Ops2, 2, dl);
1982}
1983
1984SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001985ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1986 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001987 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001988 MachineFunction &MF = DAG.getMachineFunction();
1989 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1990
1991 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001992 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001993 RC = ARM::tGPRRegisterClass;
1994 else
1995 RC = ARM::GPRRegisterClass;
1996
1997 // Transform the arguments stored in physical registers into virtual ones.
Evan Cheng2457f2c2010-05-22 01:47:14 +00001998 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001999 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002000
2001 SDValue ArgValue2;
2002 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002003 MachineFrameInfo *MFI = MF.getFrameInfo();
Bob Wilson6a234f02010-04-13 22:03:22 +00002004 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true, false);
Bob Wilson5bafff32009-06-22 23:27:02 +00002005
2006 // Create load node to retrieve arguments from the stack.
2007 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002008 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00002009 PseudoSourceValue::getFixedStack(FI), 0,
2010 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002011 } else {
2012 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002013 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002014 }
2015
Jim Grosbache5165492009-11-09 00:11:35 +00002016 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002017}
2018
2019SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002020ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002021 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002022 const SmallVectorImpl<ISD::InputArg>
2023 &Ins,
2024 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002025 SmallVectorImpl<SDValue> &InVals)
2026 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002027
Bob Wilson1f595bb2009-04-17 19:07:39 +00002028 MachineFunction &MF = DAG.getMachineFunction();
2029 MachineFrameInfo *MFI = MF.getFrameInfo();
2030
Bob Wilson1f595bb2009-04-17 19:07:39 +00002031 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2032
2033 // Assign locations to all of the incoming arguments.
2034 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002035 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
2036 *DAG.getContext());
2037 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002038 CCAssignFnForNode(CallConv, /* Return*/ false,
2039 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002040
2041 SmallVector<SDValue, 16> ArgValues;
2042
2043 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2044 CCValAssign &VA = ArgLocs[i];
2045
Bob Wilsondee46d72009-04-17 20:35:10 +00002046 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002047 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002048 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002049
Bob Wilson5bafff32009-06-22 23:27:02 +00002050 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002051 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002052 // f64 and vector types are split up into multiple registers or
2053 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002054 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002055 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002056 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002057 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002058 SDValue ArgValue2;
2059 if (VA.isMemLoc()) {
2060 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(),
2061 true, false);
2062 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2063 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
2064 PseudoSourceValue::getFixedStack(FI), 0,
2065 false, false, 0);
2066 } else {
2067 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2068 Chain, DAG, dl);
2069 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002070 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2071 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002072 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002073 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002074 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2075 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002076 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002077
Bob Wilson5bafff32009-06-22 23:27:02 +00002078 } else {
2079 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002080
Owen Anderson825b72b2009-08-11 20:47:22 +00002081 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002082 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002083 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002084 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002085 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002086 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002087 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002088 RC = (AFI->isThumb1OnlyFunction() ?
2089 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002090 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002091 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002092
2093 // Transform the arguments in physical registers into virtual ones.
2094 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002095 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002096 }
2097
2098 // If this is an 8 or 16-bit value, it is really passed promoted
2099 // to 32 bits. Insert an assert[sz]ext to capture this, then
2100 // truncate to the right size.
2101 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002102 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002103 case CCValAssign::Full: break;
2104 case CCValAssign::BCvt:
2105 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
2106 break;
2107 case CCValAssign::SExt:
2108 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2109 DAG.getValueType(VA.getValVT()));
2110 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2111 break;
2112 case CCValAssign::ZExt:
2113 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2114 DAG.getValueType(VA.getValVT()));
2115 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2116 break;
2117 }
2118
Dan Gohman98ca4f22009-08-05 01:29:28 +00002119 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002120
2121 } else { // VA.isRegLoc()
2122
2123 // sanity check
2124 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002125 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002126
2127 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00002128 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
2129 true, false);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002130
Bob Wilsondee46d72009-04-17 20:35:10 +00002131 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002132 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002133 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00002134 PseudoSourceValue::getFixedStack(FI), 0,
2135 false, false, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002136 }
2137 }
2138
2139 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00002140 if (isVarArg) {
2141 static const unsigned GPRArgRegs[] = {
2142 ARM::R0, ARM::R1, ARM::R2, ARM::R3
2143 };
2144
Bob Wilsondee46d72009-04-17 20:35:10 +00002145 unsigned NumGPRs = CCInfo.getFirstUnallocated
2146 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002147
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00002148 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
2149 unsigned VARegSize = (4 - NumGPRs) * 4;
2150 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00002151 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00002152 if (VARegSaveSize) {
2153 // If this function is vararg, store any remaining integer argument regs
2154 // to their spots on the stack so that they may be loaded by deferencing
2155 // the result of va_next.
2156 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00002157 AFI->setVarArgsFrameIndex(
2158 MFI->CreateFixedObject(VARegSaveSize,
2159 ArgOffset + VARegSaveSize - VARegSize,
2160 true, false));
2161 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2162 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00002163
Dan Gohman475871a2008-07-27 21:46:04 +00002164 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00002165 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002166 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002167 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00002168 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002169 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00002170 RC = ARM::GPRRegisterClass;
2171
Bob Wilson998e1252009-04-20 18:36:57 +00002172 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002173 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00002174 SDValue Store =
2175 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Jim Grosbach18f30e62010-06-02 21:53:11 +00002176 PseudoSourceValue::getFixedStack(AFI->getVarArgsFrameIndex()),
2177 0, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002178 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002179 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00002180 DAG.getConstant(4, getPointerTy()));
2181 }
2182 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002183 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002184 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00002185 } else
2186 // This will point to the next argument passed via stack.
Dan Gohman1e93df62010-04-17 14:41:14 +00002187 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset,
2188 true, false));
Evan Chenga8e29892007-01-19 07:51:42 +00002189 }
2190
Dan Gohman98ca4f22009-08-05 01:29:28 +00002191 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002192}
2193
2194/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002195static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002196 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002197 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002198 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002199 // Maybe this has already been legalized into the constant pool?
2200 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002201 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002202 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002203 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002204 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002205 }
2206 }
2207 return false;
2208}
2209
Evan Chenga8e29892007-01-19 07:51:42 +00002210/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2211/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002212SDValue
2213ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Dan Gohmand858e902010-04-17 15:26:15 +00002214 SDValue &ARMCC, SelectionDAG &DAG,
2215 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002216 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002217 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002218 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002219 // Constant does not fit, try adjusting it by one?
2220 switch (CC) {
2221 default: break;
2222 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002223 case ISD::SETGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00002224 if (isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002225 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002226 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002227 }
2228 break;
2229 case ISD::SETULT:
2230 case ISD::SETUGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00002231 if (C > 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002232 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002233 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002234 }
2235 break;
2236 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002237 case ISD::SETGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00002238 if (isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002239 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002240 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002241 }
2242 break;
2243 case ISD::SETULE:
2244 case ISD::SETUGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00002245 if (C < 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002246 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002247 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002248 }
2249 break;
2250 }
2251 }
2252 }
2253
2254 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002255 ARMISD::NodeType CompareType;
2256 switch (CondCode) {
2257 default:
2258 CompareType = ARMISD::CMP;
2259 break;
2260 case ARMCC::EQ:
2261 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002262 // Uses only Z Flag
2263 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002264 break;
2265 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002266 ARMCC = DAG.getConstant(CondCode, MVT::i32);
2267 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002268}
2269
2270/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00002271static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00002272 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00002273 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002274 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00002275 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002276 else
Owen Anderson825b72b2009-08-11 20:47:22 +00002277 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
2278 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002279}
2280
Dan Gohmand858e902010-04-17 15:26:15 +00002281SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002282 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002283 SDValue LHS = Op.getOperand(0);
2284 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002285 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002286 SDValue TrueVal = Op.getOperand(2);
2287 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002288 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002289
Owen Anderson825b72b2009-08-11 20:47:22 +00002290 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00002291 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002292 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00002293 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Dale Johannesende064702009-02-06 21:50:26 +00002294 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002295 }
2296
2297 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002298 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002299
Owen Anderson825b72b2009-08-11 20:47:22 +00002300 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
2301 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002302 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
2303 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00002304 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002305 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002306 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002307 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00002308 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002309 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00002310 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002311 }
2312 return Result;
2313}
2314
Dan Gohmand858e902010-04-17 15:26:15 +00002315SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002316 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002317 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002318 SDValue LHS = Op.getOperand(2);
2319 SDValue RHS = Op.getOperand(3);
2320 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002321 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002322
Owen Anderson825b72b2009-08-11 20:47:22 +00002323 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00002324 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002325 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00002326 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002327 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00002328 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002329 }
2330
Owen Anderson825b72b2009-08-11 20:47:22 +00002331 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00002332 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002333 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002334
Dale Johannesende064702009-02-06 21:50:26 +00002335 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002336 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
2337 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2338 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002339 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002340 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002341 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002342 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00002343 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002344 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002345 }
2346 return Res;
2347}
2348
Dan Gohmand858e902010-04-17 15:26:15 +00002349SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002350 SDValue Chain = Op.getOperand(0);
2351 SDValue Table = Op.getOperand(1);
2352 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002353 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002354
Owen Andersone50ed302009-08-10 22:56:29 +00002355 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002356 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2357 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002358 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002359 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002360 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002361 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2362 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002363 if (Subtarget->isThumb2()) {
2364 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2365 // which does another jump to the destination. This also makes it easier
2366 // to translate it to TBB / TBH later.
2367 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002368 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002369 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002370 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002371 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002372 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002373 PseudoSourceValue::getJumpTable(), 0,
2374 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002375 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002376 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002377 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002378 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002379 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002380 PseudoSourceValue::getJumpTable(), 0, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002381 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002382 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002383 }
Evan Chenga8e29892007-01-19 07:51:42 +00002384}
2385
Bob Wilson76a312b2010-03-19 22:51:32 +00002386static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2387 DebugLoc dl = Op.getDebugLoc();
2388 unsigned Opc;
2389
2390 switch (Op.getOpcode()) {
2391 default:
2392 assert(0 && "Invalid opcode!");
2393 case ISD::FP_TO_SINT:
2394 Opc = ARMISD::FTOSI;
2395 break;
2396 case ISD::FP_TO_UINT:
2397 Opc = ARMISD::FTOUI;
2398 break;
2399 }
2400 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
2401 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
2402}
2403
2404static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2405 EVT VT = Op.getValueType();
2406 DebugLoc dl = Op.getDebugLoc();
2407 unsigned Opc;
2408
2409 switch (Op.getOpcode()) {
2410 default:
2411 assert(0 && "Invalid opcode!");
2412 case ISD::SINT_TO_FP:
2413 Opc = ARMISD::SITOF;
2414 break;
2415 case ISD::UINT_TO_FP:
2416 Opc = ARMISD::UITOF;
2417 break;
2418 }
2419
2420 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
2421 return DAG.getNode(Opc, dl, VT, Op);
2422}
2423
Dan Gohman475871a2008-07-27 21:46:04 +00002424static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002425 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002426 SDValue Tmp0 = Op.getOperand(0);
2427 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002428 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002429 EVT VT = Op.getValueType();
2430 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00002431 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
2432 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002433 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
2434 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002435 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002436}
2437
Evan Cheng2457f2c2010-05-22 01:47:14 +00002438SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
2439 MachineFunction &MF = DAG.getMachineFunction();
2440 MachineFrameInfo *MFI = MF.getFrameInfo();
2441 MFI->setReturnAddressIsTaken(true);
2442
2443 EVT VT = Op.getValueType();
2444 DebugLoc dl = Op.getDebugLoc();
2445 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2446 if (Depth) {
2447 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
2448 SDValue Offset = DAG.getConstant(4, MVT::i32);
2449 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
2450 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
2451 NULL, 0, false, false, 0);
2452 }
2453
2454 // Return LR, which contains the return address. Mark it an implicit live-in.
Evan Chengc7cf10c2010-05-24 18:00:18 +00002455 unsigned Reg = MF.addLiveIn(ARM::LR, ARM::GPRRegisterClass);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002456 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
2457}
2458
Dan Gohmand858e902010-04-17 15:26:15 +00002459SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002460 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2461 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002462
Owen Andersone50ed302009-08-10 22:56:29 +00002463 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002464 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2465 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002466 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002467 ? ARM::R7 : ARM::R11;
2468 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2469 while (Depth--)
David Greene1b58cab2010-02-15 16:55:24 +00002470 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
2471 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002472 return FrameAddr;
2473}
2474
Bob Wilson9f3f0612010-04-17 05:30:19 +00002475/// ExpandBIT_CONVERT - If the target supports VFP, this function is called to
2476/// expand a bit convert where either the source or destination type is i64 to
2477/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2478/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2479/// vectors), since the legalizer won't know what to do with that.
Duncan Sands1607f052008-12-01 11:39:25 +00002480static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002481 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2482 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002483 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002484
Bob Wilson9f3f0612010-04-17 05:30:19 +00002485 // This function is only supposed to be called for i64 types, either as the
2486 // source or destination of the bit convert.
2487 EVT SrcVT = Op.getValueType();
2488 EVT DstVT = N->getValueType(0);
2489 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
2490 "ExpandBIT_CONVERT called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002491
Bob Wilson9f3f0612010-04-17 05:30:19 +00002492 // Turn i64->f64 into VMOVDRR.
2493 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002494 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2495 DAG.getConstant(0, MVT::i32));
2496 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2497 DAG.getConstant(1, MVT::i32));
Bob Wilson1114f562010-06-11 22:45:25 +00002498 return DAG.getNode(ISD::BIT_CONVERT, dl, DstVT,
2499 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00002500 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002501
Jim Grosbache5165492009-11-09 00:11:35 +00002502 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002503 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2504 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2505 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2506 // Merge the pieces into a single i64 value.
2507 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2508 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002509
Bob Wilson9f3f0612010-04-17 05:30:19 +00002510 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002511}
2512
Bob Wilson5bafff32009-06-22 23:27:02 +00002513/// getZeroVector - Returns a vector of specified type with all zero elements.
2514///
Owen Andersone50ed302009-08-10 22:56:29 +00002515static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002516 assert(VT.isVector() && "Expected a vector type");
2517
2518 // Zero vectors are used to represent vector negation and in those cases
2519 // will be implemented with the NEON VNEG instruction. However, VNEG does
2520 // not support i64 elements, so sometimes the zero vectors will need to be
2521 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002522 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilson5bafff32009-06-22 23:27:02 +00002523 // to their dest type. This ensures they get CSE'd.
2524 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002525 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2526 SmallVector<SDValue, 8> Ops;
2527 MVT TVT;
2528
2529 if (VT.getSizeInBits() == 64) {
2530 Ops.assign(8, Cst); TVT = MVT::v8i8;
2531 } else {
2532 Ops.assign(16, Cst); TVT = MVT::v16i8;
2533 }
2534 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002535
2536 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2537}
2538
2539/// getOnesVector - Returns a vector of specified type with all bits set.
2540///
Owen Andersone50ed302009-08-10 22:56:29 +00002541static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002542 assert(VT.isVector() && "Expected a vector type");
2543
Bob Wilson929ffa22009-10-30 20:13:25 +00002544 // Always build ones vectors as <16 x i8> or <8 x i8> bitcasted to their
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002545 // dest type. This ensures they get CSE'd.
Bob Wilson5bafff32009-06-22 23:27:02 +00002546 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002547 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2548 SmallVector<SDValue, 8> Ops;
2549 MVT TVT;
2550
2551 if (VT.getSizeInBits() == 64) {
2552 Ops.assign(8, Cst); TVT = MVT::v8i8;
2553 } else {
2554 Ops.assign(16, Cst); TVT = MVT::v16i8;
2555 }
2556 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002557
2558 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2559}
2560
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002561/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2562/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002563SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2564 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002565 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2566 EVT VT = Op.getValueType();
2567 unsigned VTBits = VT.getSizeInBits();
2568 DebugLoc dl = Op.getDebugLoc();
2569 SDValue ShOpLo = Op.getOperand(0);
2570 SDValue ShOpHi = Op.getOperand(1);
2571 SDValue ShAmt = Op.getOperand(2);
2572 SDValue ARMCC;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002573 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002574
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002575 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2576
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002577 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2578 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2579 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2580 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2581 DAG.getConstant(VTBits, MVT::i32));
2582 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2583 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002584 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002585
2586 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2587 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002588 ARMCC, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002589 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002590 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC,
2591 CCR, Cmp);
2592
2593 SDValue Ops[2] = { Lo, Hi };
2594 return DAG.getMergeValues(Ops, 2, dl);
2595}
2596
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002597/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2598/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002599SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2600 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002601 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2602 EVT VT = Op.getValueType();
2603 unsigned VTBits = VT.getSizeInBits();
2604 DebugLoc dl = Op.getDebugLoc();
2605 SDValue ShOpLo = Op.getOperand(0);
2606 SDValue ShOpHi = Op.getOperand(1);
2607 SDValue ShAmt = Op.getOperand(2);
2608 SDValue ARMCC;
2609
2610 assert(Op.getOpcode() == ISD::SHL_PARTS);
2611 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2612 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2613 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2614 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2615 DAG.getConstant(VTBits, MVT::i32));
2616 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2617 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2618
2619 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2620 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2621 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002622 ARMCC, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002623 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
2624 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMCC,
2625 CCR, Cmp);
2626
2627 SDValue Ops[2] = { Lo, Hi };
2628 return DAG.getMergeValues(Ops, 2, dl);
2629}
2630
Jim Grosbach3482c802010-01-18 19:58:49 +00002631static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
2632 const ARMSubtarget *ST) {
2633 EVT VT = N->getValueType(0);
2634 DebugLoc dl = N->getDebugLoc();
2635
2636 if (!ST->hasV6T2Ops())
2637 return SDValue();
2638
2639 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
2640 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
2641}
2642
Bob Wilson5bafff32009-06-22 23:27:02 +00002643static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2644 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002645 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002646 DebugLoc dl = N->getDebugLoc();
2647
2648 // Lower vector shifts on NEON to use VSHL.
2649 if (VT.isVector()) {
2650 assert(ST->hasNEON() && "unexpected vector shift");
2651
2652 // Left shifts translate directly to the vshiftu intrinsic.
2653 if (N->getOpcode() == ISD::SHL)
2654 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002655 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002656 N->getOperand(0), N->getOperand(1));
2657
2658 assert((N->getOpcode() == ISD::SRA ||
2659 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2660
2661 // NEON uses the same intrinsics for both left and right shifts. For
2662 // right shifts, the shift amounts are negative, so negate the vector of
2663 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002664 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002665 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2666 getZeroVector(ShiftVT, DAG, dl),
2667 N->getOperand(1));
2668 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2669 Intrinsic::arm_neon_vshifts :
2670 Intrinsic::arm_neon_vshiftu);
2671 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002672 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002673 N->getOperand(0), NegatedCount);
2674 }
2675
Eli Friedmance392eb2009-08-22 03:13:10 +00002676 // We can get here for a node like i32 = ISD::SHL i32, i64
2677 if (VT != MVT::i64)
2678 return SDValue();
2679
2680 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002681 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002682
Chris Lattner27a6c732007-11-24 07:07:01 +00002683 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2684 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002685 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002686 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002687
Chris Lattner27a6c732007-11-24 07:07:01 +00002688 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002689 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002690
Chris Lattner27a6c732007-11-24 07:07:01 +00002691 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002692 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00002693 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00002694 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00002695 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002696
Chris Lattner27a6c732007-11-24 07:07:01 +00002697 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2698 // captures the result into a carry flag.
2699 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002700 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002701
Chris Lattner27a6c732007-11-24 07:07:01 +00002702 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002703 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002704
Chris Lattner27a6c732007-11-24 07:07:01 +00002705 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002706 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002707}
2708
Bob Wilson5bafff32009-06-22 23:27:02 +00002709static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2710 SDValue TmpOp0, TmpOp1;
2711 bool Invert = false;
2712 bool Swap = false;
2713 unsigned Opc = 0;
2714
2715 SDValue Op0 = Op.getOperand(0);
2716 SDValue Op1 = Op.getOperand(1);
2717 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002718 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002719 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2720 DebugLoc dl = Op.getDebugLoc();
2721
2722 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2723 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002724 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002725 case ISD::SETUNE:
2726 case ISD::SETNE: Invert = true; // Fallthrough
2727 case ISD::SETOEQ:
2728 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2729 case ISD::SETOLT:
2730 case ISD::SETLT: Swap = true; // Fallthrough
2731 case ISD::SETOGT:
2732 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2733 case ISD::SETOLE:
2734 case ISD::SETLE: Swap = true; // Fallthrough
2735 case ISD::SETOGE:
2736 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2737 case ISD::SETUGE: Swap = true; // Fallthrough
2738 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2739 case ISD::SETUGT: Swap = true; // Fallthrough
2740 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2741 case ISD::SETUEQ: Invert = true; // Fallthrough
2742 case ISD::SETONE:
2743 // Expand this to (OLT | OGT).
2744 TmpOp0 = Op0;
2745 TmpOp1 = Op1;
2746 Opc = ISD::OR;
2747 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2748 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2749 break;
2750 case ISD::SETUO: Invert = true; // Fallthrough
2751 case ISD::SETO:
2752 // Expand this to (OLT | OGE).
2753 TmpOp0 = Op0;
2754 TmpOp1 = Op1;
2755 Opc = ISD::OR;
2756 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2757 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2758 break;
2759 }
2760 } else {
2761 // Integer comparisons.
2762 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002763 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002764 case ISD::SETNE: Invert = true;
2765 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2766 case ISD::SETLT: Swap = true;
2767 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2768 case ISD::SETLE: Swap = true;
2769 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2770 case ISD::SETULT: Swap = true;
2771 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2772 case ISD::SETULE: Swap = true;
2773 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2774 }
2775
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002776 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002777 if (Opc == ARMISD::VCEQ) {
2778
2779 SDValue AndOp;
2780 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2781 AndOp = Op0;
2782 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2783 AndOp = Op1;
2784
2785 // Ignore bitconvert.
2786 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2787 AndOp = AndOp.getOperand(0);
2788
2789 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2790 Opc = ARMISD::VTST;
2791 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2792 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2793 Invert = !Invert;
2794 }
2795 }
2796 }
2797
2798 if (Swap)
2799 std::swap(Op0, Op1);
2800
2801 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2802
2803 if (Invert)
2804 Result = DAG.getNOT(dl, Result, VT);
2805
2806 return Result;
2807}
2808
Bob Wilsond3c42842010-06-14 22:19:57 +00002809/// isNEONModifiedImm - Check if the specified splat value corresponds to a
2810/// valid vector constant for a NEON instruction with a "modified immediate"
2811/// operand (e.g., VMOV). If so, return either the constant being
2812/// splatted or the encoded value, depending on the DoEncode parameter. The
2813/// format of the encoded value is: bit12=Op, bits11-8=Cmode,
2814/// bits7-0=Immediate.
2815static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
2816 unsigned SplatBitSize, SelectionDAG &DAG,
Bob Wilson827b2102010-06-15 19:05:35 +00002817 bool isVMOV, bool DoEncode) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00002818 unsigned Op, Cmode, Imm;
2819 EVT VT;
2820
Bob Wilson827b2102010-06-15 19:05:35 +00002821 // SplatBitSize is set to the smallest size that splats the vector, so a
2822 // zero vector will always have SplatBitSize == 8. However, NEON modified
2823 // immediate instructions others than VMOV do not support the 8-bit encoding
2824 // of a zero vector, and the default encoding of zero is supposed to be the
2825 // 32-bit version.
2826 if (SplatBits == 0)
2827 SplatBitSize = 32;
2828
Bob Wilson1a913ed2010-06-11 21:34:50 +00002829 Op = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00002830 switch (SplatBitSize) {
2831 case 8:
Bob Wilson1a913ed2010-06-11 21:34:50 +00002832 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00002833 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson1a913ed2010-06-11 21:34:50 +00002834 Cmode = 0xe;
2835 Imm = SplatBits;
2836 VT = MVT::i8;
2837 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002838
2839 case 16:
2840 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilson1a913ed2010-06-11 21:34:50 +00002841 VT = MVT::i16;
2842 if ((SplatBits & ~0xff) == 0) {
2843 // Value = 0x00nn: Op=x, Cmode=100x.
2844 Cmode = 0x8;
2845 Imm = SplatBits;
2846 break;
2847 }
2848 if ((SplatBits & ~0xff00) == 0) {
2849 // Value = 0xnn00: Op=x, Cmode=101x.
2850 Cmode = 0xa;
2851 Imm = SplatBits >> 8;
2852 break;
2853 }
2854 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002855
2856 case 32:
2857 // NEON's 32-bit VMOV supports splat values where:
2858 // * only one byte is nonzero, or
2859 // * the least significant byte is 0xff and the second byte is nonzero, or
2860 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilson1a913ed2010-06-11 21:34:50 +00002861 VT = MVT::i32;
2862 if ((SplatBits & ~0xff) == 0) {
2863 // Value = 0x000000nn: Op=x, Cmode=000x.
2864 Cmode = 0;
2865 Imm = SplatBits;
2866 break;
2867 }
2868 if ((SplatBits & ~0xff00) == 0) {
2869 // Value = 0x0000nn00: Op=x, Cmode=001x.
2870 Cmode = 0x2;
2871 Imm = SplatBits >> 8;
2872 break;
2873 }
2874 if ((SplatBits & ~0xff0000) == 0) {
2875 // Value = 0x00nn0000: Op=x, Cmode=010x.
2876 Cmode = 0x4;
2877 Imm = SplatBits >> 16;
2878 break;
2879 }
2880 if ((SplatBits & ~0xff000000) == 0) {
2881 // Value = 0xnn000000: Op=x, Cmode=011x.
2882 Cmode = 0x6;
2883 Imm = SplatBits >> 24;
2884 break;
2885 }
Bob Wilson5bafff32009-06-22 23:27:02 +00002886
2887 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00002888 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
2889 // Value = 0x0000nnff: Op=x, Cmode=1100.
2890 Cmode = 0xc;
2891 Imm = SplatBits >> 8;
2892 SplatBits |= 0xff;
2893 break;
2894 }
Bob Wilson5bafff32009-06-22 23:27:02 +00002895
2896 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00002897 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
2898 // Value = 0x00nnffff: Op=x, Cmode=1101.
2899 Cmode = 0xd;
2900 Imm = SplatBits >> 16;
2901 SplatBits |= 0xffff;
2902 break;
2903 }
Bob Wilson5bafff32009-06-22 23:27:02 +00002904
2905 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2906 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2907 // VMOV.I32. A (very) minor optimization would be to replicate the value
2908 // and fall through here to test for a valid 64-bit splat. But, then the
2909 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00002910 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002911
2912 case 64: {
2913 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson827b2102010-06-15 19:05:35 +00002914 if (!isVMOV)
2915 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002916 uint64_t BitMask = 0xff;
2917 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002918 unsigned ImmMask = 1;
2919 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00002920 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00002921 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002922 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002923 Imm |= ImmMask;
2924 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002925 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00002926 }
Bob Wilson5bafff32009-06-22 23:27:02 +00002927 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00002928 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00002929 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00002930 // Op=1, Cmode=1110.
2931 Op = 1;
2932 Cmode = 0xe;
2933 SplatBits = Val;
2934 VT = MVT::i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00002935 break;
2936 }
2937
Bob Wilson1a913ed2010-06-11 21:34:50 +00002938 default:
2939 llvm_unreachable("unexpected size for EncodeNEONModImm");
2940 return SDValue();
2941 }
2942
2943 if (DoEncode)
2944 return DAG.getTargetConstant((Op << 12) | (Cmode << 8) | Imm, MVT::i32);
2945 return DAG.getTargetConstant(SplatBits, VT);
Bob Wilson5bafff32009-06-22 23:27:02 +00002946}
2947
Bob Wilsond3c42842010-06-14 22:19:57 +00002948
2949/// getNEONModImm - If this is a valid vector constant for a NEON instruction
2950/// with a "modified immediate" operand (e.g., VMOV) of the specified element
2951/// size, return the encoded value for that immediate. The ByteSize field
2952/// indicates the number of bytes of each element [1248].
Bob Wilson827b2102010-06-15 19:05:35 +00002953SDValue ARM::getNEONModImm(SDNode *N, unsigned ByteSize, bool isVMOV,
2954 SelectionDAG &DAG) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002955 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2956 APInt SplatBits, SplatUndef;
2957 unsigned SplatBitSize;
2958 bool HasAnyUndefs;
2959 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2960 HasAnyUndefs, ByteSize * 8))
2961 return SDValue();
2962
2963 if (SplatBitSize > ByteSize * 8)
2964 return SDValue();
2965
Bob Wilsond3c42842010-06-14 22:19:57 +00002966 return isNEONModifiedImm(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
Bob Wilson827b2102010-06-15 19:05:35 +00002967 SplatBitSize, DAG, isVMOV, true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002968}
2969
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002970static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2971 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002972 unsigned NumElts = VT.getVectorNumElements();
2973 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002974 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002975
2976 // If this is a VEXT shuffle, the immediate value is the index of the first
2977 // element. The other shuffle indices must be the successive elements after
2978 // the first one.
2979 unsigned ExpectedElt = Imm;
2980 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002981 // Increment the expected index. If it wraps around, it may still be
2982 // a VEXT but the source vectors must be swapped.
2983 ExpectedElt += 1;
2984 if (ExpectedElt == NumElts * 2) {
2985 ExpectedElt = 0;
2986 ReverseVEXT = true;
2987 }
2988
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002989 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002990 return false;
2991 }
2992
2993 // Adjust the index value if the source operands will be swapped.
2994 if (ReverseVEXT)
2995 Imm -= NumElts;
2996
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002997 return true;
2998}
2999
Bob Wilson8bb9e482009-07-26 00:39:34 +00003000/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3001/// instruction with the specified blocksize. (The order of the elements
3002/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003003static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3004 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003005 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3006 "Only possible block sizes for VREV are: 16, 32, 64");
3007
Bob Wilson8bb9e482009-07-26 00:39:34 +00003008 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003009 if (EltSz == 64)
3010 return false;
3011
3012 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003013 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003014
3015 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3016 return false;
3017
3018 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003019 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00003020 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
3021 return false;
3022 }
3023
3024 return true;
3025}
3026
Bob Wilsonc692cb72009-08-21 20:54:19 +00003027static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3028 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003029 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3030 if (EltSz == 64)
3031 return false;
3032
Bob Wilsonc692cb72009-08-21 20:54:19 +00003033 unsigned NumElts = VT.getVectorNumElements();
3034 WhichResult = (M[0] == 0 ? 0 : 1);
3035 for (unsigned i = 0; i < NumElts; i += 2) {
3036 if ((unsigned) M[i] != i + WhichResult ||
3037 (unsigned) M[i+1] != i + NumElts + WhichResult)
3038 return false;
3039 }
3040 return true;
3041}
3042
Bob Wilson324f4f12009-12-03 06:40:55 +00003043/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3044/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3045/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3046static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3047 unsigned &WhichResult) {
3048 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3049 if (EltSz == 64)
3050 return false;
3051
3052 unsigned NumElts = VT.getVectorNumElements();
3053 WhichResult = (M[0] == 0 ? 0 : 1);
3054 for (unsigned i = 0; i < NumElts; i += 2) {
3055 if ((unsigned) M[i] != i + WhichResult ||
3056 (unsigned) M[i+1] != i + WhichResult)
3057 return false;
3058 }
3059 return true;
3060}
3061
Bob Wilsonc692cb72009-08-21 20:54:19 +00003062static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3063 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003064 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3065 if (EltSz == 64)
3066 return false;
3067
Bob Wilsonc692cb72009-08-21 20:54:19 +00003068 unsigned NumElts = VT.getVectorNumElements();
3069 WhichResult = (M[0] == 0 ? 0 : 1);
3070 for (unsigned i = 0; i != NumElts; ++i) {
3071 if ((unsigned) M[i] != 2 * i + WhichResult)
3072 return false;
3073 }
3074
3075 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003076 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003077 return false;
3078
3079 return true;
3080}
3081
Bob Wilson324f4f12009-12-03 06:40:55 +00003082/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3083/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3084/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3085static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3086 unsigned &WhichResult) {
3087 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3088 if (EltSz == 64)
3089 return false;
3090
3091 unsigned Half = VT.getVectorNumElements() / 2;
3092 WhichResult = (M[0] == 0 ? 0 : 1);
3093 for (unsigned j = 0; j != 2; ++j) {
3094 unsigned Idx = WhichResult;
3095 for (unsigned i = 0; i != Half; ++i) {
3096 if ((unsigned) M[i + j * Half] != Idx)
3097 return false;
3098 Idx += 2;
3099 }
3100 }
3101
3102 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3103 if (VT.is64BitVector() && EltSz == 32)
3104 return false;
3105
3106 return true;
3107}
3108
Bob Wilsonc692cb72009-08-21 20:54:19 +00003109static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3110 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003111 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3112 if (EltSz == 64)
3113 return false;
3114
Bob Wilsonc692cb72009-08-21 20:54:19 +00003115 unsigned NumElts = VT.getVectorNumElements();
3116 WhichResult = (M[0] == 0 ? 0 : 1);
3117 unsigned Idx = WhichResult * NumElts / 2;
3118 for (unsigned i = 0; i != NumElts; i += 2) {
3119 if ((unsigned) M[i] != Idx ||
3120 (unsigned) M[i+1] != Idx + NumElts)
3121 return false;
3122 Idx += 1;
3123 }
3124
3125 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003126 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003127 return false;
3128
3129 return true;
3130}
3131
Bob Wilson324f4f12009-12-03 06:40:55 +00003132/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3133/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3134/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3135static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3136 unsigned &WhichResult) {
3137 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3138 if (EltSz == 64)
3139 return false;
3140
3141 unsigned NumElts = VT.getVectorNumElements();
3142 WhichResult = (M[0] == 0 ? 0 : 1);
3143 unsigned Idx = WhichResult * NumElts / 2;
3144 for (unsigned i = 0; i != NumElts; i += 2) {
3145 if ((unsigned) M[i] != Idx ||
3146 (unsigned) M[i+1] != Idx)
3147 return false;
3148 Idx += 1;
3149 }
3150
3151 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3152 if (VT.is64BitVector() && EltSz == 32)
3153 return false;
3154
3155 return true;
3156}
3157
3158
Owen Andersone50ed302009-08-10 22:56:29 +00003159static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003160 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00003161 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003162 if (ConstVal->isNullValue())
3163 return getZeroVector(VT, DAG, dl);
3164 if (ConstVal->isAllOnesValue())
3165 return getOnesVector(VT, DAG, dl);
3166
Owen Andersone50ed302009-08-10 22:56:29 +00003167 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00003168 if (VT.is64BitVector()) {
3169 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003170 case 8: CanonicalVT = MVT::v8i8; break;
3171 case 16: CanonicalVT = MVT::v4i16; break;
3172 case 32: CanonicalVT = MVT::v2i32; break;
3173 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003174 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003175 }
3176 } else {
3177 assert(VT.is128BitVector() && "unknown splat vector size");
3178 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003179 case 8: CanonicalVT = MVT::v16i8; break;
3180 case 16: CanonicalVT = MVT::v8i16; break;
3181 case 32: CanonicalVT = MVT::v4i32; break;
3182 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003183 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003184 }
3185 }
3186
3187 // Build a canonical splat for this value.
3188 SmallVector<SDValue, 8> Ops;
3189 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
3190 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
3191 Ops.size());
3192 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
3193}
3194
3195// If this is a case we can't handle, return null and let the default
3196// expansion code take care of it.
3197static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00003198 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003199 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003200 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003201
3202 APInt SplatBits, SplatUndef;
3203 unsigned SplatBitSize;
3204 bool HasAnyUndefs;
3205 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003206 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003207 // Check if an immediate VMOV works.
3208 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
3209 SplatUndef.getZExtValue(),
Bob Wilson827b2102010-06-15 19:05:35 +00003210 SplatBitSize, DAG, true, false);
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003211 if (Val.getNode())
3212 return BuildSplat(Val, VT, DAG, dl);
3213 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003214 }
3215
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003216 // Scan through the operands to see if only one value is used.
3217 unsigned NumElts = VT.getVectorNumElements();
3218 bool isOnlyLowElement = true;
3219 bool usesOnlyOneValue = true;
3220 bool isConstant = true;
3221 SDValue Value;
3222 for (unsigned i = 0; i < NumElts; ++i) {
3223 SDValue V = Op.getOperand(i);
3224 if (V.getOpcode() == ISD::UNDEF)
3225 continue;
3226 if (i > 0)
3227 isOnlyLowElement = false;
3228 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3229 isConstant = false;
3230
3231 if (!Value.getNode())
3232 Value = V;
3233 else if (V != Value)
3234 usesOnlyOneValue = false;
3235 }
3236
3237 if (!Value.getNode())
3238 return DAG.getUNDEF(VT);
3239
3240 if (isOnlyLowElement)
3241 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3242
3243 // If all elements are constants, fall back to the default expansion, which
3244 // will generate a load from the constant pool.
3245 if (isConstant)
3246 return SDValue();
3247
3248 // Use VDUP for non-constant splats.
Bob Wilson069e4342010-05-23 05:42:31 +00003249 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3250 if (usesOnlyOneValue && EltSize <= 32)
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003251 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3252
3253 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003254 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3255 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003256 if (EltSize >= 32) {
3257 // Do the expansion with floating-point types, since that is what the VFP
3258 // registers are defined to use, and since i64 is not legal.
3259 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3260 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003261 SmallVector<SDValue, 8> Ops;
3262 for (unsigned i = 0; i < NumElts; ++i)
3263 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, dl, EltVT, Op.getOperand(i)));
3264 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003265 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003266 }
3267
3268 return SDValue();
3269}
3270
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003271/// isShuffleMaskLegal - Targets can use this to indicate that they only
3272/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3273/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3274/// are assumed to be legal.
3275bool
3276ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
3277 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003278 if (VT.getVectorNumElements() == 4 &&
3279 (VT.is128BitVector() || VT.is64BitVector())) {
3280 unsigned PFIndexes[4];
3281 for (unsigned i = 0; i != 4; ++i) {
3282 if (M[i] < 0)
3283 PFIndexes[i] = 8;
3284 else
3285 PFIndexes[i] = M[i];
3286 }
3287
3288 // Compute the index in the perfect shuffle table.
3289 unsigned PFTableIndex =
3290 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3291 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3292 unsigned Cost = (PFEntry >> 30);
3293
3294 if (Cost <= 4)
3295 return true;
3296 }
3297
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003298 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00003299 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003300
Bob Wilson53dd2452010-06-07 23:53:38 +00003301 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3302 return (EltSize >= 32 ||
3303 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003304 isVREVMask(M, VT, 64) ||
3305 isVREVMask(M, VT, 32) ||
3306 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00003307 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
3308 isVTRNMask(M, VT, WhichResult) ||
3309 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00003310 isVZIPMask(M, VT, WhichResult) ||
3311 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
3312 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
3313 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003314}
3315
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003316/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
3317/// the specified operations to build the shuffle.
3318static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
3319 SDValue RHS, SelectionDAG &DAG,
3320 DebugLoc dl) {
3321 unsigned OpNum = (PFEntry >> 26) & 0x0F;
3322 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
3323 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
3324
3325 enum {
3326 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
3327 OP_VREV,
3328 OP_VDUP0,
3329 OP_VDUP1,
3330 OP_VDUP2,
3331 OP_VDUP3,
3332 OP_VEXT1,
3333 OP_VEXT2,
3334 OP_VEXT3,
3335 OP_VUZPL, // VUZP, left result
3336 OP_VUZPR, // VUZP, right result
3337 OP_VZIPL, // VZIP, left result
3338 OP_VZIPR, // VZIP, right result
3339 OP_VTRNL, // VTRN, left result
3340 OP_VTRNR // VTRN, right result
3341 };
3342
3343 if (OpNum == OP_COPY) {
3344 if (LHSID == (1*9+2)*9+3) return LHS;
3345 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
3346 return RHS;
3347 }
3348
3349 SDValue OpLHS, OpRHS;
3350 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
3351 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
3352 EVT VT = OpLHS.getValueType();
3353
3354 switch (OpNum) {
3355 default: llvm_unreachable("Unknown shuffle opcode!");
3356 case OP_VREV:
3357 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
3358 case OP_VDUP0:
3359 case OP_VDUP1:
3360 case OP_VDUP2:
3361 case OP_VDUP3:
3362 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003363 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003364 case OP_VEXT1:
3365 case OP_VEXT2:
3366 case OP_VEXT3:
3367 return DAG.getNode(ARMISD::VEXT, dl, VT,
3368 OpLHS, OpRHS,
3369 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
3370 case OP_VUZPL:
3371 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003372 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003373 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
3374 case OP_VZIPL:
3375 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003376 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003377 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
3378 case OP_VTRNL:
3379 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003380 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3381 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003382 }
3383}
3384
Bob Wilson5bafff32009-06-22 23:27:02 +00003385static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003386 SDValue V1 = Op.getOperand(0);
3387 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00003388 DebugLoc dl = Op.getDebugLoc();
3389 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003390 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003391 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00003392
Bob Wilson28865062009-08-13 02:13:04 +00003393 // Convert shuffles that are directly supported on NEON to target-specific
3394 // DAG nodes, instead of keeping them as shuffles and matching them again
3395 // during code selection. This is more efficient and avoids the possibility
3396 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00003397 // FIXME: floating-point vectors should be canonicalized to integer vectors
3398 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003399 SVN->getMask(ShuffleMask);
3400
Bob Wilson53dd2452010-06-07 23:53:38 +00003401 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3402 if (EltSize <= 32) {
3403 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
3404 int Lane = SVN->getSplatIndex();
3405 // If this is undef splat, generate it via "just" vdup, if possible.
3406 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00003407
Bob Wilson53dd2452010-06-07 23:53:38 +00003408 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3409 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
3410 }
3411 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
3412 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00003413 }
Bob Wilson53dd2452010-06-07 23:53:38 +00003414
3415 bool ReverseVEXT;
3416 unsigned Imm;
3417 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
3418 if (ReverseVEXT)
3419 std::swap(V1, V2);
3420 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
3421 DAG.getConstant(Imm, MVT::i32));
3422 }
3423
3424 if (isVREVMask(ShuffleMask, VT, 64))
3425 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
3426 if (isVREVMask(ShuffleMask, VT, 32))
3427 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
3428 if (isVREVMask(ShuffleMask, VT, 16))
3429 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
3430
3431 // Check for Neon shuffles that modify both input vectors in place.
3432 // If both results are used, i.e., if there are two shuffles with the same
3433 // source operands and with masks corresponding to both results of one of
3434 // these operations, DAG memoization will ensure that a single node is
3435 // used for both shuffles.
3436 unsigned WhichResult;
3437 if (isVTRNMask(ShuffleMask, VT, WhichResult))
3438 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3439 V1, V2).getValue(WhichResult);
3440 if (isVUZPMask(ShuffleMask, VT, WhichResult))
3441 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3442 V1, V2).getValue(WhichResult);
3443 if (isVZIPMask(ShuffleMask, VT, WhichResult))
3444 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3445 V1, V2).getValue(WhichResult);
3446
3447 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
3448 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3449 V1, V1).getValue(WhichResult);
3450 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3451 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3452 V1, V1).getValue(WhichResult);
3453 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3454 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3455 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00003456 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003457
Bob Wilsonc692cb72009-08-21 20:54:19 +00003458 // If the shuffle is not directly supported and it has 4 elements, use
3459 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003460 unsigned NumElts = VT.getVectorNumElements();
3461 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003462 unsigned PFIndexes[4];
3463 for (unsigned i = 0; i != 4; ++i) {
3464 if (ShuffleMask[i] < 0)
3465 PFIndexes[i] = 8;
3466 else
3467 PFIndexes[i] = ShuffleMask[i];
3468 }
3469
3470 // Compute the index in the perfect shuffle table.
3471 unsigned PFTableIndex =
3472 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003473 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3474 unsigned Cost = (PFEntry >> 30);
3475
3476 if (Cost <= 4)
3477 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
3478 }
Bob Wilsond8e17572009-08-12 22:31:50 +00003479
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003480 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003481 if (EltSize >= 32) {
3482 // Do the expansion with floating-point types, since that is what the VFP
3483 // registers are defined to use, and since i64 is not legal.
3484 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3485 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
3486 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V1);
3487 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003488 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003489 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00003490 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003491 Ops.push_back(DAG.getUNDEF(EltVT));
3492 else
3493 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
3494 ShuffleMask[i] < (int)NumElts ? V1 : V2,
3495 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
3496 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00003497 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003498 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Bob Wilson63b88452010-05-20 18:39:53 +00003499 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
3500 }
3501
Bob Wilson22cac0d2009-08-14 05:16:33 +00003502 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003503}
3504
Bob Wilson5bafff32009-06-22 23:27:02 +00003505static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003506 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003507 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003508 SDValue Vec = Op.getOperand(0);
3509 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00003510 assert(VT == MVT::i32 &&
3511 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
3512 "unexpected type for custom-lowering vector extract");
3513 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00003514}
3515
Bob Wilsona6d65862009-08-03 20:36:38 +00003516static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3517 // The only time a CONCAT_VECTORS operation can have legal types is when
3518 // two 64-bit vectors are concatenated to a 128-bit vector.
3519 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
3520 "unexpected CONCAT_VECTORS");
3521 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00003522 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00003523 SDValue Op0 = Op.getOperand(0);
3524 SDValue Op1 = Op.getOperand(1);
3525 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003526 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3527 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00003528 DAG.getIntPtrConstant(0));
3529 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003530 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3531 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00003532 DAG.getIntPtrConstant(1));
3533 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003534}
3535
Dan Gohmand858e902010-04-17 15:26:15 +00003536SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003537 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003538 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00003539 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00003540 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003541 case ISD::GlobalAddress:
3542 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
3543 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00003544 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00003545 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
3546 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003547 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00003548 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00003549 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00003550 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00003551 case ISD::SINT_TO_FP:
3552 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
3553 case ISD::FP_TO_SINT:
3554 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003555 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003556 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003557 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003558 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00003559 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00003560 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00003561 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
3562 Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00003563 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003564 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00003565 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00003566 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00003567 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003568 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00003569 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00003570 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003571 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
3572 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3573 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003574 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00003575 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003576 }
Dan Gohman475871a2008-07-27 21:46:04 +00003577 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003578}
3579
Duncan Sands1607f052008-12-01 11:39:25 +00003580/// ReplaceNodeResults - Replace the results of node with an illegal result
3581/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00003582void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
3583 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00003584 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00003585 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00003586 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00003587 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003588 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003589 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003590 case ISD::BIT_CONVERT:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003591 Res = ExpandBIT_CONVERT(N, DAG);
3592 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00003593 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003594 case ISD::SRA:
3595 Res = LowerShift(N, DAG, Subtarget);
3596 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003597 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00003598 if (Res.getNode())
3599 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00003600}
Chris Lattner27a6c732007-11-24 07:07:01 +00003601
Evan Chenga8e29892007-01-19 07:51:42 +00003602//===----------------------------------------------------------------------===//
3603// ARM Scheduler Hooks
3604//===----------------------------------------------------------------------===//
3605
3606MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003607ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
3608 MachineBasicBlock *BB,
3609 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00003610 unsigned dest = MI->getOperand(0).getReg();
3611 unsigned ptr = MI->getOperand(1).getReg();
3612 unsigned oldval = MI->getOperand(2).getReg();
3613 unsigned newval = MI->getOperand(3).getReg();
3614 unsigned scratch = BB->getParent()->getRegInfo()
3615 .createVirtualRegister(ARM::GPRRegisterClass);
3616 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3617 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003618 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00003619
3620 unsigned ldrOpc, strOpc;
3621 switch (Size) {
3622 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003623 case 1:
3624 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
3625 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
3626 break;
3627 case 2:
3628 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3629 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3630 break;
3631 case 4:
3632 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3633 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3634 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00003635 }
3636
3637 MachineFunction *MF = BB->getParent();
3638 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3639 MachineFunction::iterator It = BB;
3640 ++It; // insert the new blocks after the current block
3641
3642 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3643 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3644 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3645 MF->insert(It, loop1MBB);
3646 MF->insert(It, loop2MBB);
3647 MF->insert(It, exitMBB);
3648 exitMBB->transferSuccessors(BB);
3649
3650 // thisMBB:
3651 // ...
3652 // fallthrough --> loop1MBB
3653 BB->addSuccessor(loop1MBB);
3654
3655 // loop1MBB:
3656 // ldrex dest, [ptr]
3657 // cmp dest, oldval
3658 // bne exitMBB
3659 BB = loop1MBB;
3660 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003661 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003662 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003663 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3664 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003665 BB->addSuccessor(loop2MBB);
3666 BB->addSuccessor(exitMBB);
3667
3668 // loop2MBB:
3669 // strex scratch, newval, [ptr]
3670 // cmp scratch, #0
3671 // bne loop1MBB
3672 BB = loop2MBB;
3673 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
3674 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003675 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003676 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003677 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3678 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003679 BB->addSuccessor(loop1MBB);
3680 BB->addSuccessor(exitMBB);
3681
3682 // exitMBB:
3683 // ...
3684 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00003685
3686 MF->DeleteMachineInstr(MI); // The instruction is gone now.
3687
Jim Grosbach5278eb82009-12-11 01:42:04 +00003688 return BB;
3689}
3690
3691MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003692ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
3693 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00003694 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
3695 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3696
3697 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003698 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003699 MachineFunction::iterator It = BB;
3700 ++It;
3701
3702 unsigned dest = MI->getOperand(0).getReg();
3703 unsigned ptr = MI->getOperand(1).getReg();
3704 unsigned incr = MI->getOperand(2).getReg();
3705 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00003706
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003707 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003708 unsigned ldrOpc, strOpc;
3709 switch (Size) {
3710 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003711 case 1:
3712 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00003713 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003714 break;
3715 case 2:
3716 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3717 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3718 break;
3719 case 4:
3720 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3721 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3722 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00003723 }
3724
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003725 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3726 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3727 MF->insert(It, loopMBB);
3728 MF->insert(It, exitMBB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003729 exitMBB->transferSuccessors(BB);
3730
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003731 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003732 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3733 unsigned scratch2 = (!BinOpcode) ? incr :
3734 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3735
3736 // thisMBB:
3737 // ...
3738 // fallthrough --> loopMBB
3739 BB->addSuccessor(loopMBB);
3740
3741 // loopMBB:
3742 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003743 // <binop> scratch2, dest, incr
3744 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00003745 // cmp scratch, #0
3746 // bne- loopMBB
3747 // fallthrough --> exitMBB
3748 BB = loopMBB;
3749 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00003750 if (BinOpcode) {
3751 // operand order needs to go the other way for NAND
3752 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
3753 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3754 addReg(incr).addReg(dest)).addReg(0);
3755 else
3756 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3757 addReg(dest).addReg(incr)).addReg(0);
3758 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00003759
3760 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
3761 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003762 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00003763 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003764 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3765 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003766
3767 BB->addSuccessor(loopMBB);
3768 BB->addSuccessor(exitMBB);
3769
3770 // exitMBB:
3771 // ...
3772 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00003773
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003774 MF->DeleteMachineInstr(MI); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00003775
Jim Grosbachc3c23542009-12-14 04:22:04 +00003776 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00003777}
3778
3779MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00003780ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003781 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003782 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00003783 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003784 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00003785 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00003786 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00003787 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00003788 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00003789
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003790 case ARM::ATOMIC_LOAD_ADD_I8:
3791 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3792 case ARM::ATOMIC_LOAD_ADD_I16:
3793 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3794 case ARM::ATOMIC_LOAD_ADD_I32:
3795 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003796
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003797 case ARM::ATOMIC_LOAD_AND_I8:
3798 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3799 case ARM::ATOMIC_LOAD_AND_I16:
3800 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3801 case ARM::ATOMIC_LOAD_AND_I32:
3802 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003803
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003804 case ARM::ATOMIC_LOAD_OR_I8:
3805 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3806 case ARM::ATOMIC_LOAD_OR_I16:
3807 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3808 case ARM::ATOMIC_LOAD_OR_I32:
3809 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003810
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003811 case ARM::ATOMIC_LOAD_XOR_I8:
3812 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3813 case ARM::ATOMIC_LOAD_XOR_I16:
3814 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3815 case ARM::ATOMIC_LOAD_XOR_I32:
3816 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003817
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003818 case ARM::ATOMIC_LOAD_NAND_I8:
3819 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3820 case ARM::ATOMIC_LOAD_NAND_I16:
3821 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3822 case ARM::ATOMIC_LOAD_NAND_I32:
3823 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003824
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003825 case ARM::ATOMIC_LOAD_SUB_I8:
3826 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3827 case ARM::ATOMIC_LOAD_SUB_I16:
3828 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3829 case ARM::ATOMIC_LOAD_SUB_I32:
3830 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003831
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003832 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
3833 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
3834 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00003835
3836 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
3837 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
3838 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003839
Evan Cheng007ea272009-08-12 05:17:19 +00003840 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00003841 // To "insert" a SELECT_CC instruction, we actually have to insert the
3842 // diamond control-flow pattern. The incoming instruction knows the
3843 // destination vreg to set, the condition code register to branch on, the
3844 // true/false values to select between, and a branch opcode to use.
3845 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003846 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00003847 ++It;
3848
3849 // thisMBB:
3850 // ...
3851 // TrueVal = ...
3852 // cmpTY ccX, r1, r2
3853 // bCC copy1MBB
3854 // fallthrough --> copy0MBB
3855 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003856 MachineFunction *F = BB->getParent();
3857 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3858 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00003859 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00003860 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003861 F->insert(It, copy0MBB);
3862 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00003863 // Update machine-CFG edges by first adding all successors of the current
3864 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00003865 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003866 E = BB->succ_end(); I != E; ++I)
Evan Chengce319102009-09-19 09:51:03 +00003867 sinkMBB->addSuccessor(*I);
Evan Chenga8e29892007-01-19 07:51:42 +00003868 // Next, remove all successors of the current block, and add the true
3869 // and fallthrough blocks as its successors.
Evan Chengce319102009-09-19 09:51:03 +00003870 while (!BB->succ_empty())
Evan Chenga8e29892007-01-19 07:51:42 +00003871 BB->removeSuccessor(BB->succ_begin());
3872 BB->addSuccessor(copy0MBB);
3873 BB->addSuccessor(sinkMBB);
3874
3875 // copy0MBB:
3876 // %FalseValue = ...
3877 // # fallthrough to sinkMBB
3878 BB = copy0MBB;
3879
3880 // Update machine-CFG edges
3881 BB->addSuccessor(sinkMBB);
3882
3883 // sinkMBB:
3884 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3885 // ...
3886 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00003887 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00003888 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
3889 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
3890
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003891 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00003892 return BB;
3893 }
Evan Cheng86198642009-08-07 00:34:42 +00003894
3895 case ARM::tANDsp:
3896 case ARM::tADDspr_:
3897 case ARM::tSUBspi_:
3898 case ARM::t2SUBrSPi_:
3899 case ARM::t2SUBrSPi12_:
3900 case ARM::t2SUBrSPs_: {
3901 MachineFunction *MF = BB->getParent();
3902 unsigned DstReg = MI->getOperand(0).getReg();
3903 unsigned SrcReg = MI->getOperand(1).getReg();
3904 bool DstIsDead = MI->getOperand(0).isDead();
3905 bool SrcIsKill = MI->getOperand(1).isKill();
3906
3907 if (SrcReg != ARM::SP) {
3908 // Copy the source to SP from virtual register.
3909 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
3910 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3911 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
3912 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
3913 .addReg(SrcReg, getKillRegState(SrcIsKill));
3914 }
3915
3916 unsigned OpOpc = 0;
3917 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
3918 switch (MI->getOpcode()) {
3919 default:
3920 llvm_unreachable("Unexpected pseudo instruction!");
3921 case ARM::tANDsp:
3922 OpOpc = ARM::tAND;
3923 NeedPred = true;
3924 break;
3925 case ARM::tADDspr_:
3926 OpOpc = ARM::tADDspr;
3927 break;
3928 case ARM::tSUBspi_:
3929 OpOpc = ARM::tSUBspi;
3930 break;
3931 case ARM::t2SUBrSPi_:
3932 OpOpc = ARM::t2SUBrSPi;
3933 NeedPred = true; NeedCC = true;
3934 break;
3935 case ARM::t2SUBrSPi12_:
3936 OpOpc = ARM::t2SUBrSPi12;
3937 NeedPred = true;
3938 break;
3939 case ARM::t2SUBrSPs_:
3940 OpOpc = ARM::t2SUBrSPs;
3941 NeedPred = true; NeedCC = true; NeedOp3 = true;
3942 break;
3943 }
3944 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
3945 if (OpOpc == ARM::tAND)
3946 AddDefaultT1CC(MIB);
3947 MIB.addReg(ARM::SP);
3948 MIB.addOperand(MI->getOperand(2));
3949 if (NeedOp3)
3950 MIB.addOperand(MI->getOperand(3));
3951 if (NeedPred)
3952 AddDefaultPred(MIB);
3953 if (NeedCC)
3954 AddDefaultCC(MIB);
3955
3956 // Copy the result from SP to virtual register.
3957 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
3958 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3959 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
3960 BuildMI(BB, dl, TII->get(CopyOpc))
3961 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
3962 .addReg(ARM::SP);
3963 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3964 return BB;
3965 }
Evan Chenga8e29892007-01-19 07:51:42 +00003966 }
3967}
3968
3969//===----------------------------------------------------------------------===//
3970// ARM Optimization Hooks
3971//===----------------------------------------------------------------------===//
3972
Chris Lattnerd1980a52009-03-12 06:52:53 +00003973static
3974SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3975 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00003976 SelectionDAG &DAG = DCI.DAG;
3977 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003978 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00003979 unsigned Opc = N->getOpcode();
3980 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3981 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3982 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3983 ISD::CondCode CC = ISD::SETCC_INVALID;
3984
3985 if (isSlctCC) {
3986 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
3987 } else {
3988 SDValue CCOp = Slct.getOperand(0);
3989 if (CCOp.getOpcode() == ISD::SETCC)
3990 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
3991 }
3992
3993 bool DoXform = false;
3994 bool InvCC = false;
3995 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
3996 "Bad input!");
3997
3998 if (LHS.getOpcode() == ISD::Constant &&
3999 cast<ConstantSDNode>(LHS)->isNullValue()) {
4000 DoXform = true;
4001 } else if (CC != ISD::SETCC_INVALID &&
4002 RHS.getOpcode() == ISD::Constant &&
4003 cast<ConstantSDNode>(RHS)->isNullValue()) {
4004 std::swap(LHS, RHS);
4005 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004006 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00004007 Op0.getOperand(0).getValueType();
4008 bool isInt = OpVT.isInteger();
4009 CC = ISD::getSetCCInverse(CC, isInt);
4010
4011 if (!TLI.isCondCodeLegal(CC, OpVT))
4012 return SDValue(); // Inverse operator isn't legal.
4013
4014 DoXform = true;
4015 InvCC = true;
4016 }
4017
4018 if (DoXform) {
4019 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
4020 if (isSlctCC)
4021 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
4022 Slct.getOperand(0), Slct.getOperand(1), CC);
4023 SDValue CCOp = Slct.getOperand(0);
4024 if (InvCC)
4025 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
4026 CCOp.getOperand(0), CCOp.getOperand(1), CC);
4027 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
4028 CCOp, OtherOp, Result);
4029 }
4030 return SDValue();
4031}
4032
4033/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
4034static SDValue PerformADDCombine(SDNode *N,
4035 TargetLowering::DAGCombinerInfo &DCI) {
4036 // added by evan in r37685 with no testcase.
4037 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00004038
Chris Lattnerd1980a52009-03-12 06:52:53 +00004039 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
4040 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
4041 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
4042 if (Result.getNode()) return Result;
4043 }
4044 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
4045 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
4046 if (Result.getNode()) return Result;
4047 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00004048
Chris Lattnerd1980a52009-03-12 06:52:53 +00004049 return SDValue();
4050}
4051
4052/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
4053static SDValue PerformSUBCombine(SDNode *N,
4054 TargetLowering::DAGCombinerInfo &DCI) {
4055 // added by evan in r37685 with no testcase.
4056 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00004057
Chris Lattnerd1980a52009-03-12 06:52:53 +00004058 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
4059 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
4060 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
4061 if (Result.getNode()) return Result;
4062 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00004063
Chris Lattnerd1980a52009-03-12 06:52:53 +00004064 return SDValue();
4065}
4066
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004067static SDValue PerformMULCombine(SDNode *N,
4068 TargetLowering::DAGCombinerInfo &DCI,
4069 const ARMSubtarget *Subtarget) {
4070 SelectionDAG &DAG = DCI.DAG;
4071
4072 if (Subtarget->isThumb1Only())
4073 return SDValue();
4074
4075 if (DAG.getMachineFunction().
4076 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
4077 return SDValue();
4078
4079 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
4080 return SDValue();
4081
4082 EVT VT = N->getValueType(0);
4083 if (VT != MVT::i32)
4084 return SDValue();
4085
4086 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
4087 if (!C)
4088 return SDValue();
4089
4090 uint64_t MulAmt = C->getZExtValue();
4091 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
4092 ShiftAmt = ShiftAmt & (32 - 1);
4093 SDValue V = N->getOperand(0);
4094 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004095
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004096 SDValue Res;
4097 MulAmt >>= ShiftAmt;
4098 if (isPowerOf2_32(MulAmt - 1)) {
4099 // (mul x, 2^N + 1) => (add (shl x, N), x)
4100 Res = DAG.getNode(ISD::ADD, DL, VT,
4101 V, DAG.getNode(ISD::SHL, DL, VT,
4102 V, DAG.getConstant(Log2_32(MulAmt-1),
4103 MVT::i32)));
4104 } else if (isPowerOf2_32(MulAmt + 1)) {
4105 // (mul x, 2^N - 1) => (sub (shl x, N), x)
4106 Res = DAG.getNode(ISD::SUB, DL, VT,
4107 DAG.getNode(ISD::SHL, DL, VT,
4108 V, DAG.getConstant(Log2_32(MulAmt+1),
4109 MVT::i32)),
4110 V);
4111 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004112 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004113
4114 if (ShiftAmt != 0)
4115 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
4116 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004117
4118 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004119 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004120 return SDValue();
4121}
4122
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +00004123/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
4124/// ARMISD::VMOVRRD.
Jim Grosbache5165492009-11-09 00:11:35 +00004125static SDValue PerformVMOVRRDCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004126 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004127 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00004128 SDValue InDouble = N->getOperand(0);
Jim Grosbache5165492009-11-09 00:11:35 +00004129 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004130 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004131 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004132}
4133
Bob Wilson5bafff32009-06-22 23:27:02 +00004134/// getVShiftImm - Check if this is a valid build_vector for the immediate
4135/// operand of a vector shift operation, where all the elements of the
4136/// build_vector must have the same constant integer value.
4137static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
4138 // Ignore bit_converts.
4139 while (Op.getOpcode() == ISD::BIT_CONVERT)
4140 Op = Op.getOperand(0);
4141 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
4142 APInt SplatBits, SplatUndef;
4143 unsigned SplatBitSize;
4144 bool HasAnyUndefs;
4145 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
4146 HasAnyUndefs, ElementBits) ||
4147 SplatBitSize > ElementBits)
4148 return false;
4149 Cnt = SplatBits.getSExtValue();
4150 return true;
4151}
4152
4153/// isVShiftLImm - Check if this is a valid build_vector for the immediate
4154/// operand of a vector shift left operation. That value must be in the range:
4155/// 0 <= Value < ElementBits for a left shift; or
4156/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00004157static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00004158 assert(VT.isVector() && "vector shift count is not a vector type");
4159 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
4160 if (! getVShiftImm(Op, ElementBits, Cnt))
4161 return false;
4162 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
4163}
4164
4165/// isVShiftRImm - Check if this is a valid build_vector for the immediate
4166/// operand of a vector shift right operation. For a shift opcode, the value
4167/// is positive, but for an intrinsic the value count must be negative. The
4168/// absolute value must be in the range:
4169/// 1 <= |Value| <= ElementBits for a right shift; or
4170/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00004171static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00004172 int64_t &Cnt) {
4173 assert(VT.isVector() && "vector shift count is not a vector type");
4174 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
4175 if (! getVShiftImm(Op, ElementBits, Cnt))
4176 return false;
4177 if (isIntrinsic)
4178 Cnt = -Cnt;
4179 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
4180}
4181
4182/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
4183static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
4184 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
4185 switch (IntNo) {
4186 default:
4187 // Don't do anything for most intrinsics.
4188 break;
4189
4190 // Vector shifts: check for immediate versions and lower them.
4191 // Note: This is done during DAG combining instead of DAG legalizing because
4192 // the build_vectors for 64-bit vector element shift counts are generally
4193 // not legal, and it is hard to see their values after they get legalized to
4194 // loads from a constant pool.
4195 case Intrinsic::arm_neon_vshifts:
4196 case Intrinsic::arm_neon_vshiftu:
4197 case Intrinsic::arm_neon_vshiftls:
4198 case Intrinsic::arm_neon_vshiftlu:
4199 case Intrinsic::arm_neon_vshiftn:
4200 case Intrinsic::arm_neon_vrshifts:
4201 case Intrinsic::arm_neon_vrshiftu:
4202 case Intrinsic::arm_neon_vrshiftn:
4203 case Intrinsic::arm_neon_vqshifts:
4204 case Intrinsic::arm_neon_vqshiftu:
4205 case Intrinsic::arm_neon_vqshiftsu:
4206 case Intrinsic::arm_neon_vqshiftns:
4207 case Intrinsic::arm_neon_vqshiftnu:
4208 case Intrinsic::arm_neon_vqshiftnsu:
4209 case Intrinsic::arm_neon_vqrshiftns:
4210 case Intrinsic::arm_neon_vqrshiftnu:
4211 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00004212 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004213 int64_t Cnt;
4214 unsigned VShiftOpc = 0;
4215
4216 switch (IntNo) {
4217 case Intrinsic::arm_neon_vshifts:
4218 case Intrinsic::arm_neon_vshiftu:
4219 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
4220 VShiftOpc = ARMISD::VSHL;
4221 break;
4222 }
4223 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
4224 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
4225 ARMISD::VSHRs : ARMISD::VSHRu);
4226 break;
4227 }
4228 return SDValue();
4229
4230 case Intrinsic::arm_neon_vshiftls:
4231 case Intrinsic::arm_neon_vshiftlu:
4232 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
4233 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00004234 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004235
4236 case Intrinsic::arm_neon_vrshifts:
4237 case Intrinsic::arm_neon_vrshiftu:
4238 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
4239 break;
4240 return SDValue();
4241
4242 case Intrinsic::arm_neon_vqshifts:
4243 case Intrinsic::arm_neon_vqshiftu:
4244 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
4245 break;
4246 return SDValue();
4247
4248 case Intrinsic::arm_neon_vqshiftsu:
4249 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
4250 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00004251 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004252
4253 case Intrinsic::arm_neon_vshiftn:
4254 case Intrinsic::arm_neon_vrshiftn:
4255 case Intrinsic::arm_neon_vqshiftns:
4256 case Intrinsic::arm_neon_vqshiftnu:
4257 case Intrinsic::arm_neon_vqshiftnsu:
4258 case Intrinsic::arm_neon_vqrshiftns:
4259 case Intrinsic::arm_neon_vqrshiftnu:
4260 case Intrinsic::arm_neon_vqrshiftnsu:
4261 // Narrowing shifts require an immediate right shift.
4262 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
4263 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00004264 llvm_unreachable("invalid shift count for narrowing vector shift "
4265 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004266
4267 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004268 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00004269 }
4270
4271 switch (IntNo) {
4272 case Intrinsic::arm_neon_vshifts:
4273 case Intrinsic::arm_neon_vshiftu:
4274 // Opcode already set above.
4275 break;
4276 case Intrinsic::arm_neon_vshiftls:
4277 case Intrinsic::arm_neon_vshiftlu:
4278 if (Cnt == VT.getVectorElementType().getSizeInBits())
4279 VShiftOpc = ARMISD::VSHLLi;
4280 else
4281 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
4282 ARMISD::VSHLLs : ARMISD::VSHLLu);
4283 break;
4284 case Intrinsic::arm_neon_vshiftn:
4285 VShiftOpc = ARMISD::VSHRN; break;
4286 case Intrinsic::arm_neon_vrshifts:
4287 VShiftOpc = ARMISD::VRSHRs; break;
4288 case Intrinsic::arm_neon_vrshiftu:
4289 VShiftOpc = ARMISD::VRSHRu; break;
4290 case Intrinsic::arm_neon_vrshiftn:
4291 VShiftOpc = ARMISD::VRSHRN; break;
4292 case Intrinsic::arm_neon_vqshifts:
4293 VShiftOpc = ARMISD::VQSHLs; break;
4294 case Intrinsic::arm_neon_vqshiftu:
4295 VShiftOpc = ARMISD::VQSHLu; break;
4296 case Intrinsic::arm_neon_vqshiftsu:
4297 VShiftOpc = ARMISD::VQSHLsu; break;
4298 case Intrinsic::arm_neon_vqshiftns:
4299 VShiftOpc = ARMISD::VQSHRNs; break;
4300 case Intrinsic::arm_neon_vqshiftnu:
4301 VShiftOpc = ARMISD::VQSHRNu; break;
4302 case Intrinsic::arm_neon_vqshiftnsu:
4303 VShiftOpc = ARMISD::VQSHRNsu; break;
4304 case Intrinsic::arm_neon_vqrshiftns:
4305 VShiftOpc = ARMISD::VQRSHRNs; break;
4306 case Intrinsic::arm_neon_vqrshiftnu:
4307 VShiftOpc = ARMISD::VQRSHRNu; break;
4308 case Intrinsic::arm_neon_vqrshiftnsu:
4309 VShiftOpc = ARMISD::VQRSHRNsu; break;
4310 }
4311
4312 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004313 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004314 }
4315
4316 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00004317 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004318 int64_t Cnt;
4319 unsigned VShiftOpc = 0;
4320
4321 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
4322 VShiftOpc = ARMISD::VSLI;
4323 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
4324 VShiftOpc = ARMISD::VSRI;
4325 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00004326 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00004327 }
4328
4329 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
4330 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00004331 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004332 }
4333
4334 case Intrinsic::arm_neon_vqrshifts:
4335 case Intrinsic::arm_neon_vqrshiftu:
4336 // No immediate versions of these to check for.
4337 break;
4338 }
4339
4340 return SDValue();
4341}
4342
4343/// PerformShiftCombine - Checks for immediate versions of vector shifts and
4344/// lowers them. As with the vector shift intrinsics, this is done during DAG
4345/// combining instead of DAG legalizing because the build_vectors for 64-bit
4346/// vector element shift counts are generally not legal, and it is hard to see
4347/// their values after they get legalized to loads from a constant pool.
4348static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
4349 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00004350 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00004351
4352 // Nothing to be done for scalar shifts.
4353 if (! VT.isVector())
4354 return SDValue();
4355
4356 assert(ST->hasNEON() && "unexpected vector shift");
4357 int64_t Cnt;
4358
4359 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004360 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00004361
4362 case ISD::SHL:
4363 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
4364 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004365 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004366 break;
4367
4368 case ISD::SRA:
4369 case ISD::SRL:
4370 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
4371 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
4372 ARMISD::VSHRs : ARMISD::VSHRu);
4373 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00004374 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00004375 }
4376 }
4377 return SDValue();
4378}
4379
4380/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
4381/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
4382static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
4383 const ARMSubtarget *ST) {
4384 SDValue N0 = N->getOperand(0);
4385
4386 // Check for sign- and zero-extensions of vector extract operations of 8-
4387 // and 16-bit vector elements. NEON supports these directly. They are
4388 // handled during DAG combining because type legalization will promote them
4389 // to 32-bit types and it is messy to recognize the operations after that.
4390 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
4391 SDValue Vec = N0.getOperand(0);
4392 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004393 EVT VT = N->getValueType(0);
4394 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004395 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4396
Owen Anderson825b72b2009-08-11 20:47:22 +00004397 if (VT == MVT::i32 &&
4398 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00004399 TLI.isTypeLegal(Vec.getValueType())) {
4400
4401 unsigned Opc = 0;
4402 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004403 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00004404 case ISD::SIGN_EXTEND:
4405 Opc = ARMISD::VGETLANEs;
4406 break;
4407 case ISD::ZERO_EXTEND:
4408 case ISD::ANY_EXTEND:
4409 Opc = ARMISD::VGETLANEu;
4410 break;
4411 }
4412 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
4413 }
4414 }
4415
4416 return SDValue();
4417}
4418
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004419/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
4420/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
4421static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
4422 const ARMSubtarget *ST) {
4423 // If the target supports NEON, try to use vmax/vmin instructions for f32
4424 // selects like "x < y ? x : y". Unless the FiniteOnlyFPMath option is set,
4425 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
4426 // a NaN; only do the transformation when it matches that behavior.
4427
4428 // For now only do this when using NEON for FP operations; if using VFP, it
4429 // is not obvious that the benefit outweighs the cost of switching to the
4430 // NEON pipeline.
4431 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
4432 N->getValueType(0) != MVT::f32)
4433 return SDValue();
4434
4435 SDValue CondLHS = N->getOperand(0);
4436 SDValue CondRHS = N->getOperand(1);
4437 SDValue LHS = N->getOperand(2);
4438 SDValue RHS = N->getOperand(3);
4439 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
4440
4441 unsigned Opcode = 0;
4442 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00004443 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004444 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00004445 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004446 IsReversed = true ; // x CC y ? y : x
4447 } else {
4448 return SDValue();
4449 }
4450
Bob Wilsone742bb52010-02-24 22:15:53 +00004451 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004452 switch (CC) {
4453 default: break;
4454 case ISD::SETOLT:
4455 case ISD::SETOLE:
4456 case ISD::SETLT:
4457 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004458 case ISD::SETULT:
4459 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004460 // If LHS is NaN, an ordered comparison will be false and the result will
4461 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
4462 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4463 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
4464 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4465 break;
4466 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
4467 // will return -0, so vmin can only be used for unsafe math or if one of
4468 // the operands is known to be nonzero.
4469 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
4470 !UnsafeFPMath &&
4471 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4472 break;
4473 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004474 break;
4475
4476 case ISD::SETOGT:
4477 case ISD::SETOGE:
4478 case ISD::SETGT:
4479 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004480 case ISD::SETUGT:
4481 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004482 // If LHS is NaN, an ordered comparison will be false and the result will
4483 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
4484 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4485 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
4486 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4487 break;
4488 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
4489 // will return +0, so vmax can only be used for unsafe math or if one of
4490 // the operands is known to be nonzero.
4491 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
4492 !UnsafeFPMath &&
4493 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4494 break;
4495 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004496 break;
4497 }
4498
4499 if (!Opcode)
4500 return SDValue();
4501 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
4502}
4503
Dan Gohman475871a2008-07-27 21:46:04 +00004504SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004505 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004506 switch (N->getOpcode()) {
4507 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004508 case ISD::ADD: return PerformADDCombine(N, DCI);
4509 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004510 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbache5165492009-11-09 00:11:35 +00004511 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004512 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004513 case ISD::SHL:
4514 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004515 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004516 case ISD::SIGN_EXTEND:
4517 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004518 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
4519 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004520 }
Dan Gohman475871a2008-07-27 21:46:04 +00004521 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004522}
4523
Bill Wendlingaf566342009-08-15 21:21:19 +00004524bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
4525 if (!Subtarget->hasV6Ops())
4526 // Pre-v6 does not support unaligned mem access.
4527 return false;
Anton Korobeynikov90cfc132010-01-30 14:08:12 +00004528 else {
4529 // v6+ may or may not support unaligned mem access depending on the system
4530 // configuration.
4531 // FIXME: This is pretty conservative. Should we provide cmdline option to
4532 // control the behaviour?
Bill Wendlingaf566342009-08-15 21:21:19 +00004533 if (!Subtarget->isTargetDarwin())
4534 return false;
4535 }
4536
4537 switch (VT.getSimpleVT().SimpleTy) {
4538 default:
4539 return false;
4540 case MVT::i8:
4541 case MVT::i16:
4542 case MVT::i32:
4543 return true;
4544 // FIXME: VLD1 etc with standard alignment is legal.
4545 }
4546}
4547
Evan Chenge6c835f2009-08-14 20:09:37 +00004548static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
4549 if (V < 0)
4550 return false;
4551
4552 unsigned Scale = 1;
4553 switch (VT.getSimpleVT().SimpleTy) {
4554 default: return false;
4555 case MVT::i1:
4556 case MVT::i8:
4557 // Scale == 1;
4558 break;
4559 case MVT::i16:
4560 // Scale == 2;
4561 Scale = 2;
4562 break;
4563 case MVT::i32:
4564 // Scale == 4;
4565 Scale = 4;
4566 break;
4567 }
4568
4569 if ((V & (Scale - 1)) != 0)
4570 return false;
4571 V /= Scale;
4572 return V == (V & ((1LL << 5) - 1));
4573}
4574
4575static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
4576 const ARMSubtarget *Subtarget) {
4577 bool isNeg = false;
4578 if (V < 0) {
4579 isNeg = true;
4580 V = - V;
4581 }
4582
4583 switch (VT.getSimpleVT().SimpleTy) {
4584 default: return false;
4585 case MVT::i1:
4586 case MVT::i8:
4587 case MVT::i16:
4588 case MVT::i32:
4589 // + imm12 or - imm8
4590 if (isNeg)
4591 return V == (V & ((1LL << 8) - 1));
4592 return V == (V & ((1LL << 12) - 1));
4593 case MVT::f32:
4594 case MVT::f64:
4595 // Same as ARM mode. FIXME: NEON?
4596 if (!Subtarget->hasVFP2())
4597 return false;
4598 if ((V & 3) != 0)
4599 return false;
4600 V >>= 2;
4601 return V == (V & ((1LL << 8) - 1));
4602 }
4603}
4604
Evan Chengb01fad62007-03-12 23:30:29 +00004605/// isLegalAddressImmediate - Return true if the integer value can be used
4606/// as the offset of the target addressing mode for load / store of the
4607/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00004608static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004609 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00004610 if (V == 0)
4611 return true;
4612
Evan Cheng65011532009-03-09 19:15:00 +00004613 if (!VT.isSimple())
4614 return false;
4615
Evan Chenge6c835f2009-08-14 20:09:37 +00004616 if (Subtarget->isThumb1Only())
4617 return isLegalT1AddressImmediate(V, VT);
4618 else if (Subtarget->isThumb2())
4619 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00004620
Evan Chenge6c835f2009-08-14 20:09:37 +00004621 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00004622 if (V < 0)
4623 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00004624 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00004625 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004626 case MVT::i1:
4627 case MVT::i8:
4628 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00004629 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004630 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004631 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00004632 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004633 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004634 case MVT::f32:
4635 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00004636 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00004637 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00004638 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00004639 return false;
4640 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004641 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00004642 }
Evan Chenga8e29892007-01-19 07:51:42 +00004643}
4644
Evan Chenge6c835f2009-08-14 20:09:37 +00004645bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
4646 EVT VT) const {
4647 int Scale = AM.Scale;
4648 if (Scale < 0)
4649 return false;
4650
4651 switch (VT.getSimpleVT().SimpleTy) {
4652 default: return false;
4653 case MVT::i1:
4654 case MVT::i8:
4655 case MVT::i16:
4656 case MVT::i32:
4657 if (Scale == 1)
4658 return true;
4659 // r + r << imm
4660 Scale = Scale & ~1;
4661 return Scale == 2 || Scale == 4 || Scale == 8;
4662 case MVT::i64:
4663 // r + r
4664 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
4665 return true;
4666 return false;
4667 case MVT::isVoid:
4668 // Note, we allow "void" uses (basically, uses that aren't loads or
4669 // stores), because arm allows folding a scale into many arithmetic
4670 // operations. This should be made more precise and revisited later.
4671
4672 // Allow r << imm, but the imm has to be a multiple of two.
4673 if (Scale & 1) return false;
4674 return isPowerOf2_32(Scale);
4675 }
4676}
4677
Chris Lattner37caf8c2007-04-09 23:33:39 +00004678/// isLegalAddressingMode - Return true if the addressing mode represented
4679/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004680bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004681 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004682 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00004683 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00004684 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004685
Chris Lattner37caf8c2007-04-09 23:33:39 +00004686 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004687 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004688 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004689
Chris Lattner37caf8c2007-04-09 23:33:39 +00004690 switch (AM.Scale) {
4691 case 0: // no scale reg, must be "r+i" or "r", or "i".
4692 break;
4693 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00004694 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00004695 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004696 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00004697 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004698 // ARM doesn't support any R+R*scale+imm addr modes.
4699 if (AM.BaseOffs)
4700 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004701
Bob Wilson2c7dab12009-04-08 17:55:28 +00004702 if (!VT.isSimple())
4703 return false;
4704
Evan Chenge6c835f2009-08-14 20:09:37 +00004705 if (Subtarget->isThumb2())
4706 return isLegalT2ScaledAddressingMode(AM, VT);
4707
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004708 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00004709 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00004710 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004711 case MVT::i1:
4712 case MVT::i8:
4713 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004714 if (Scale < 0) Scale = -Scale;
4715 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004716 return true;
4717 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00004718 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00004719 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00004720 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004721 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004722 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004723 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00004724 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004725
Owen Anderson825b72b2009-08-11 20:47:22 +00004726 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004727 // Note, we allow "void" uses (basically, uses that aren't loads or
4728 // stores), because arm allows folding a scale into many arithmetic
4729 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004730
Chris Lattner37caf8c2007-04-09 23:33:39 +00004731 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00004732 if (Scale & 1) return false;
4733 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00004734 }
4735 break;
Evan Chengb01fad62007-03-12 23:30:29 +00004736 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00004737 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00004738}
4739
Evan Cheng77e47512009-11-11 19:05:52 +00004740/// isLegalICmpImmediate - Return true if the specified immediate is legal
4741/// icmp immediate, that is the target has icmp instructions which can compare
4742/// a register against the immediate without having to materialize the
4743/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00004744bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00004745 if (!Subtarget->isThumb())
4746 return ARM_AM::getSOImmVal(Imm) != -1;
4747 if (Subtarget->isThumb2())
4748 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00004749 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00004750}
4751
Owen Andersone50ed302009-08-10 22:56:29 +00004752static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004753 bool isSEXTLoad, SDValue &Base,
4754 SDValue &Offset, bool &isInc,
4755 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00004756 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4757 return false;
4758
Owen Anderson825b72b2009-08-11 20:47:22 +00004759 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00004760 // AddressingMode 3
4761 Base = Ptr->getOperand(0);
4762 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004763 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004764 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004765 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004766 isInc = false;
4767 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4768 return true;
4769 }
4770 }
4771 isInc = (Ptr->getOpcode() == ISD::ADD);
4772 Offset = Ptr->getOperand(1);
4773 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004774 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00004775 // AddressingMode 2
4776 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004777 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004778 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004779 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004780 isInc = false;
4781 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4782 Base = Ptr->getOperand(0);
4783 return true;
4784 }
4785 }
4786
4787 if (Ptr->getOpcode() == ISD::ADD) {
4788 isInc = true;
4789 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
4790 if (ShOpcVal != ARM_AM::no_shift) {
4791 Base = Ptr->getOperand(1);
4792 Offset = Ptr->getOperand(0);
4793 } else {
4794 Base = Ptr->getOperand(0);
4795 Offset = Ptr->getOperand(1);
4796 }
4797 return true;
4798 }
4799
4800 isInc = (Ptr->getOpcode() == ISD::ADD);
4801 Base = Ptr->getOperand(0);
4802 Offset = Ptr->getOperand(1);
4803 return true;
4804 }
4805
Jim Grosbache5165492009-11-09 00:11:35 +00004806 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00004807 return false;
4808}
4809
Owen Andersone50ed302009-08-10 22:56:29 +00004810static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004811 bool isSEXTLoad, SDValue &Base,
4812 SDValue &Offset, bool &isInc,
4813 SelectionDAG &DAG) {
4814 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4815 return false;
4816
4817 Base = Ptr->getOperand(0);
4818 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
4819 int RHSC = (int)RHS->getZExtValue();
4820 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
4821 assert(Ptr->getOpcode() == ISD::ADD);
4822 isInc = false;
4823 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4824 return true;
4825 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
4826 isInc = Ptr->getOpcode() == ISD::ADD;
4827 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
4828 return true;
4829 }
4830 }
4831
4832 return false;
4833}
4834
Evan Chenga8e29892007-01-19 07:51:42 +00004835/// getPreIndexedAddressParts - returns true by value, base pointer and
4836/// offset pointer and addressing mode by reference if the node's address
4837/// can be legally represented as pre-indexed load / store address.
4838bool
Dan Gohman475871a2008-07-27 21:46:04 +00004839ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
4840 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004841 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004842 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004843 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004844 return false;
4845
Owen Andersone50ed302009-08-10 22:56:29 +00004846 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004847 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004848 bool isSEXTLoad = false;
4849 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4850 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004851 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004852 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4853 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
4854 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004855 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004856 } else
4857 return false;
4858
4859 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004860 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004861 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004862 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
4863 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004864 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004865 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00004866 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00004867 if (!isLegal)
4868 return false;
4869
4870 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
4871 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004872}
4873
4874/// getPostIndexedAddressParts - returns true by value, base pointer and
4875/// offset pointer and addressing mode by reference if this node can be
4876/// combined with a load / store to form a post-indexed load / store.
4877bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00004878 SDValue &Base,
4879 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004880 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004881 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004882 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004883 return false;
4884
Owen Andersone50ed302009-08-10 22:56:29 +00004885 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004886 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004887 bool isSEXTLoad = false;
4888 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004889 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00004890 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00004891 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4892 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004893 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00004894 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00004895 } else
4896 return false;
4897
4898 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004899 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004900 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004901 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00004902 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004903 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004904 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
4905 isInc, DAG);
4906 if (!isLegal)
4907 return false;
4908
Evan Cheng28dad2a2010-05-18 21:31:17 +00004909 if (Ptr != Base) {
4910 // Swap base ptr and offset to catch more post-index load / store when
4911 // it's legal. In Thumb2 mode, offset must be an immediate.
4912 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
4913 !Subtarget->isThumb2())
4914 std::swap(Base, Offset);
4915
4916 // Post-indexed load / store update the base pointer.
4917 if (Ptr != Base)
4918 return false;
4919 }
4920
Evan Chenge88d5ce2009-07-02 07:28:31 +00004921 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
4922 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004923}
4924
Dan Gohman475871a2008-07-27 21:46:04 +00004925void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00004926 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004927 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004928 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00004929 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00004930 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004931 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004932 switch (Op.getOpcode()) {
4933 default: break;
4934 case ARMISD::CMOV: {
4935 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00004936 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004937 if (KnownZero == 0 && KnownOne == 0) return;
4938
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004939 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00004940 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
4941 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004942 KnownZero &= KnownZeroRHS;
4943 KnownOne &= KnownOneRHS;
4944 return;
4945 }
4946 }
4947}
4948
4949//===----------------------------------------------------------------------===//
4950// ARM Inline Assembly Support
4951//===----------------------------------------------------------------------===//
4952
4953/// getConstraintType - Given a constraint letter, return the type of
4954/// constraint it is for this target.
4955ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00004956ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
4957 if (Constraint.size() == 1) {
4958 switch (Constraint[0]) {
4959 default: break;
4960 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004961 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00004962 }
Evan Chenga8e29892007-01-19 07:51:42 +00004963 }
Chris Lattner4234f572007-03-25 02:14:49 +00004964 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00004965}
4966
Bob Wilson2dc4f542009-03-20 22:42:55 +00004967std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00004968ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004969 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004970 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004971 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00004972 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004973 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004974 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004975 return std::make_pair(0U, ARM::tGPRRegisterClass);
4976 else
4977 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004978 case 'r':
4979 return std::make_pair(0U, ARM::GPRRegisterClass);
4980 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004981 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004982 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00004983 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004984 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00004985 if (VT.getSizeInBits() == 128)
4986 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004987 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004988 }
4989 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00004990 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00004991 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00004992
Evan Chenga8e29892007-01-19 07:51:42 +00004993 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
4994}
4995
4996std::vector<unsigned> ARMTargetLowering::
4997getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004998 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004999 if (Constraint.size() != 1)
5000 return std::vector<unsigned>();
5001
5002 switch (Constraint[0]) { // GCC ARM Constraint Letters
5003 default: break;
5004 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00005005 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
5006 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
5007 0);
Evan Chenga8e29892007-01-19 07:51:42 +00005008 case 'r':
5009 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
5010 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
5011 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
5012 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005013 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00005014 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005015 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
5016 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
5017 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
5018 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
5019 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
5020 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
5021 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
5022 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00005023 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005024 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
5025 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
5026 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
5027 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00005028 if (VT.getSizeInBits() == 128)
5029 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
5030 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00005031 break;
Evan Chenga8e29892007-01-19 07:51:42 +00005032 }
5033
5034 return std::vector<unsigned>();
5035}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005036
5037/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
5038/// vector. If it is invalid, don't add anything to Ops.
5039void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
5040 char Constraint,
5041 bool hasMemory,
5042 std::vector<SDValue>&Ops,
5043 SelectionDAG &DAG) const {
5044 SDValue Result(0, 0);
5045
5046 switch (Constraint) {
5047 default: break;
5048 case 'I': case 'J': case 'K': case 'L':
5049 case 'M': case 'N': case 'O':
5050 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
5051 if (!C)
5052 return;
5053
5054 int64_t CVal64 = C->getSExtValue();
5055 int CVal = (int) CVal64;
5056 // None of these constraints allow values larger than 32 bits. Check
5057 // that the value fits in an int.
5058 if (CVal != CVal64)
5059 return;
5060
5061 switch (Constraint) {
5062 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005063 if (Subtarget->isThumb1Only()) {
5064 // This must be a constant between 0 and 255, for ADD
5065 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005066 if (CVal >= 0 && CVal <= 255)
5067 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005068 } else if (Subtarget->isThumb2()) {
5069 // A constant that can be used as an immediate value in a
5070 // data-processing instruction.
5071 if (ARM_AM::getT2SOImmVal(CVal) != -1)
5072 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005073 } else {
5074 // A constant that can be used as an immediate value in a
5075 // data-processing instruction.
5076 if (ARM_AM::getSOImmVal(CVal) != -1)
5077 break;
5078 }
5079 return;
5080
5081 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005082 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005083 // This must be a constant between -255 and -1, for negated ADD
5084 // immediates. This can be used in GCC with an "n" modifier that
5085 // prints the negated value, for use with SUB instructions. It is
5086 // not useful otherwise but is implemented for compatibility.
5087 if (CVal >= -255 && CVal <= -1)
5088 break;
5089 } else {
5090 // This must be a constant between -4095 and 4095. It is not clear
5091 // what this constraint is intended for. Implemented for
5092 // compatibility with GCC.
5093 if (CVal >= -4095 && CVal <= 4095)
5094 break;
5095 }
5096 return;
5097
5098 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005099 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005100 // A 32-bit value where only one byte has a nonzero value. Exclude
5101 // zero to match GCC. This constraint is used by GCC internally for
5102 // constants that can be loaded with a move/shift combination.
5103 // It is not useful otherwise but is implemented for compatibility.
5104 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
5105 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005106 } else if (Subtarget->isThumb2()) {
5107 // A constant whose bitwise inverse can be used as an immediate
5108 // value in a data-processing instruction. This can be used in GCC
5109 // with a "B" modifier that prints the inverted value, for use with
5110 // BIC and MVN instructions. It is not useful otherwise but is
5111 // implemented for compatibility.
5112 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
5113 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005114 } else {
5115 // A constant whose bitwise inverse can be used as an immediate
5116 // value in a data-processing instruction. This can be used in GCC
5117 // with a "B" modifier that prints the inverted value, for use with
5118 // BIC and MVN instructions. It is not useful otherwise but is
5119 // implemented for compatibility.
5120 if (ARM_AM::getSOImmVal(~CVal) != -1)
5121 break;
5122 }
5123 return;
5124
5125 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005126 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005127 // This must be a constant between -7 and 7,
5128 // for 3-operand ADD/SUB immediate instructions.
5129 if (CVal >= -7 && CVal < 7)
5130 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00005131 } else if (Subtarget->isThumb2()) {
5132 // A constant whose negation can be used as an immediate value in a
5133 // data-processing instruction. This can be used in GCC with an "n"
5134 // modifier that prints the negated value, for use with SUB
5135 // instructions. It is not useful otherwise but is implemented for
5136 // compatibility.
5137 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
5138 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005139 } else {
5140 // A constant whose negation can be used as an immediate value in a
5141 // data-processing instruction. This can be used in GCC with an "n"
5142 // modifier that prints the negated value, for use with SUB
5143 // instructions. It is not useful otherwise but is implemented for
5144 // compatibility.
5145 if (ARM_AM::getSOImmVal(-CVal) != -1)
5146 break;
5147 }
5148 return;
5149
5150 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005151 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005152 // This must be a multiple of 4 between 0 and 1020, for
5153 // ADD sp + immediate.
5154 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
5155 break;
5156 } else {
5157 // A power of two or a constant between 0 and 32. This is used in
5158 // GCC for the shift amount on shifted register operands, but it is
5159 // useful in general for any shift amounts.
5160 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
5161 break;
5162 }
5163 return;
5164
5165 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005166 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005167 // This must be a constant between 0 and 31, for shift amounts.
5168 if (CVal >= 0 && CVal <= 31)
5169 break;
5170 }
5171 return;
5172
5173 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00005174 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00005175 // This must be a multiple of 4 between -508 and 508, for
5176 // ADD/SUB sp = sp + immediate.
5177 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
5178 break;
5179 }
5180 return;
5181 }
5182 Result = DAG.getTargetConstant(CVal, Op.getValueType());
5183 break;
5184 }
5185
5186 if (Result.getNode()) {
5187 Ops.push_back(Result);
5188 return;
5189 }
5190 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
5191 Ops, DAG);
5192}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00005193
5194bool
5195ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
5196 // The ARM target isn't yet aware of offsets.
5197 return false;
5198}
Evan Cheng39382422009-10-28 01:44:26 +00005199
5200int ARM::getVFPf32Imm(const APFloat &FPImm) {
5201 APInt Imm = FPImm.bitcastToAPInt();
5202 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
5203 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
5204 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
5205
5206 // We can handle 4 bits of mantissa.
5207 // mantissa = (16+UInt(e:f:g:h))/16.
5208 if (Mantissa & 0x7ffff)
5209 return -1;
5210 Mantissa >>= 19;
5211 if ((Mantissa & 0xf) != Mantissa)
5212 return -1;
5213
5214 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
5215 if (Exp < -3 || Exp > 4)
5216 return -1;
5217 Exp = ((Exp+3) & 0x7) ^ 4;
5218
5219 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
5220}
5221
5222int ARM::getVFPf64Imm(const APFloat &FPImm) {
5223 APInt Imm = FPImm.bitcastToAPInt();
5224 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
5225 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
5226 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
5227
5228 // We can handle 4 bits of mantissa.
5229 // mantissa = (16+UInt(e:f:g:h))/16.
5230 if (Mantissa & 0xffffffffffffLL)
5231 return -1;
5232 Mantissa >>= 48;
5233 if ((Mantissa & 0xf) != Mantissa)
5234 return -1;
5235
5236 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
5237 if (Exp < -3 || Exp > 4)
5238 return -1;
5239 Exp = ((Exp+3) & 0x7) ^ 4;
5240
5241 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
5242}
5243
5244/// isFPImmLegal - Returns true if the target can instruction select the
5245/// specified FP immediate natively. If false, the legalizer will
5246/// materialize the FP immediate as a load from a constant pool.
5247bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
5248 if (!Subtarget->hasVFP3())
5249 return false;
5250 if (VT == MVT::f32)
5251 return ARM::getVFPf32Imm(Imm) != -1;
5252 if (VT == MVT::f64)
5253 return ARM::getVFPf64Imm(Imm) != -1;
5254 return false;
5255}