blob: 529f7115f5b19216d005cad70cc9bd34d5bf2c56 [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
15#include "ARM.h"
16#include "ARMAddressingModes.h"
17#include "ARMConstantPoolValue.h"
18#include "ARMISelLowering.h"
19#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000020#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000021#include "ARMRegisterInfo.h"
22#include "ARMSubtarget.h"
23#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000024#include "ARMTargetObjectFile.h"
Evan Chenga8e29892007-01-19 07:51:42 +000025#include "llvm/CallingConv.h"
26#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000027#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000028#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000029#include "llvm/Instruction.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000030#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000031#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000032#include "llvm/CodeGen/CallingConvLower.h"
Evan Chenga8e29892007-01-19 07:51:42 +000033#include "llvm/CodeGen/MachineBasicBlock.h"
34#include "llvm/CodeGen/MachineFrameInfo.h"
35#include "llvm/CodeGen/MachineFunction.h"
36#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000039#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000040#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000041#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000042#include "llvm/ADT/VectorExtras.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000043#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000044#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000045#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000046#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000047#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000048using namespace llvm;
49
Jim Grosbache7b52522010-04-14 22:28:31 +000050static cl::opt<bool>
51EnableARMLongCalls("arm-long-calls", cl::Hidden,
52 cl::desc("Generate calls via indirect call instructions."),
53 cl::init(false));
54
Owen Andersone50ed302009-08-10 22:56:29 +000055static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000056 CCValAssign::LocInfo &LocInfo,
57 ISD::ArgFlagsTy &ArgFlags,
58 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000059static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000060 CCValAssign::LocInfo &LocInfo,
61 ISD::ArgFlagsTy &ArgFlags,
62 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000063static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000064 CCValAssign::LocInfo &LocInfo,
65 ISD::ArgFlagsTy &ArgFlags,
66 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000067static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000068 CCValAssign::LocInfo &LocInfo,
69 ISD::ArgFlagsTy &ArgFlags,
70 CCState &State);
71
Owen Andersone50ed302009-08-10 22:56:29 +000072void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
73 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000074 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000075 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000076 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
77 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000078
Owen Anderson70671842009-08-10 20:18:46 +000079 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000080 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000081 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000082 }
83
Owen Andersone50ed302009-08-10 22:56:29 +000084 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +000085 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +000086 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +000087 if (ElemTy == MVT::i8 || ElemTy == MVT::i16)
Owen Anderson70671842009-08-10 20:18:46 +000088 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +000089 if (ElemTy != MVT::i32) {
90 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
91 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
92 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
93 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
94 }
Owen Anderson70671842009-08-10 20:18:46 +000095 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
96 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Evan Chengde8aa4e2010-05-05 18:28:36 +000097 if (llvm::ModelWithRegSequence())
98 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
99 else
100 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Custom);
Anton Korobeynikov8e6c2b92009-08-21 12:40:35 +0000101 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
Bob Wilsond0910c42010-04-06 22:02:24 +0000102 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
103 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000104 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000105 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
106 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
107 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000108 }
109
110 // Promote all bit-wise operations.
111 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000112 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000113 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
114 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000115 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000116 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000117 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000118 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000119 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000120 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000121 }
Bob Wilson16330762009-09-16 00:17:28 +0000122
123 // Neon does not support vector divide/remainder operations.
124 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
125 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
126 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
127 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
128 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
129 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000130}
131
Owen Andersone50ed302009-08-10 22:56:29 +0000132void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000133 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000135}
136
Owen Andersone50ed302009-08-10 22:56:29 +0000137void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000138 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000140}
141
Chris Lattnerf0144122009-07-28 03:13:23 +0000142static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
143 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000144 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000145
Chris Lattner80ec2792009-08-02 00:34:36 +0000146 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000147}
148
Evan Chenga8e29892007-01-19 07:51:42 +0000149ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000150 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000151 Subtarget = &TM.getSubtarget<ARMSubtarget>();
152
Evan Chengb1df8f22007-04-27 08:15:43 +0000153 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000154 // Uses VFP for Thumb libfuncs if available.
155 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
156 // Single-precision floating-point arithmetic.
157 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
158 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
159 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
160 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000161
Evan Chengb1df8f22007-04-27 08:15:43 +0000162 // Double-precision floating-point arithmetic.
163 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
164 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
165 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
166 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000167
Evan Chengb1df8f22007-04-27 08:15:43 +0000168 // Single-precision comparisons.
169 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
170 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
171 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
172 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
173 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
174 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
175 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
176 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000177
Evan Chengb1df8f22007-04-27 08:15:43 +0000178 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
179 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
180 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
181 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
182 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
183 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
184 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
185 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000186
Evan Chengb1df8f22007-04-27 08:15:43 +0000187 // Double-precision comparisons.
188 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
189 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
190 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
191 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
192 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
193 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
194 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
195 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000196
Evan Chengb1df8f22007-04-27 08:15:43 +0000197 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
198 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
199 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
200 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
201 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
202 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
203 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
204 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000205
Evan Chengb1df8f22007-04-27 08:15:43 +0000206 // Floating-point to integer conversions.
207 // i64 conversions are done via library routines even when generating VFP
208 // instructions, so use the same ones.
209 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
210 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
211 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
212 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000213
Evan Chengb1df8f22007-04-27 08:15:43 +0000214 // Conversions between floating types.
215 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
216 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
217
218 // Integer to floating-point conversions.
219 // i64 conversions are done via library routines even when generating VFP
220 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000221 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
222 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000223 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
224 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
225 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
226 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
227 }
Evan Chenga8e29892007-01-19 07:51:42 +0000228 }
229
Bob Wilson2f954612009-05-22 17:38:41 +0000230 // These libcalls are not available in 32-bit.
231 setLibcallName(RTLIB::SHL_I128, 0);
232 setLibcallName(RTLIB::SRL_I128, 0);
233 setLibcallName(RTLIB::SRA_I128, 0);
234
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000235 // Libcalls should use the AAPCS base standard ABI, even if hard float
236 // is in effect, as per the ARM RTABI specification, section 4.1.2.
237 if (Subtarget->isAAPCS_ABI()) {
238 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
239 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
240 CallingConv::ARM_AAPCS);
241 }
242 }
243
David Goodwinf1daf7d2009-07-08 23:10:31 +0000244 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000245 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000246 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000247 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000248 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
250 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000251
Owen Anderson825b72b2009-08-11 20:47:22 +0000252 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000253 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000254
255 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000256 addDRTypeForNEON(MVT::v2f32);
257 addDRTypeForNEON(MVT::v8i8);
258 addDRTypeForNEON(MVT::v4i16);
259 addDRTypeForNEON(MVT::v2i32);
260 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000261
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 addQRTypeForNEON(MVT::v4f32);
263 addQRTypeForNEON(MVT::v2f64);
264 addQRTypeForNEON(MVT::v16i8);
265 addQRTypeForNEON(MVT::v8i16);
266 addQRTypeForNEON(MVT::v4i32);
267 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000268
Evan Cheng603afbf2010-05-10 17:34:18 +0000269 // Map v4i64 to QQ registers but do not make the type legal for any
Evan Cheng22c687b2010-05-14 02:13:41 +0000270 // operations. Similarly map v8i64 to QQQQ registers. v4i64 and v8i64 are
271 // only used for REG_SEQUENCE to load / store 4 to 8 consecutive
Evan Cheng603afbf2010-05-10 17:34:18 +0000272 // D registers.
273 addRegisterClass(MVT::v4i64, ARM::QQPRRegisterClass);
Evan Cheng22c687b2010-05-14 02:13:41 +0000274 addRegisterClass(MVT::v8i64, ARM::QQQQPRRegisterClass);
Evan Cheng603afbf2010-05-10 17:34:18 +0000275
Bob Wilson74dc72e2009-09-15 23:55:57 +0000276 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
277 // neither Neon nor VFP support any arithmetic operations on it.
278 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
279 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
280 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
281 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
282 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
283 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
284 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
285 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
286 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
287 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
288 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
289 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
290 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
291 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
292 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
293 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
294 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
295 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
296 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
297 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
298 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
299 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
300 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
301 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
302
Bob Wilson642b3292009-09-16 00:32:15 +0000303 // Neon does not support some operations on v1i64 and v2i64 types.
304 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
305 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
306 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
307 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
308
Bob Wilson5bafff32009-06-22 23:27:02 +0000309 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
310 setTargetDAGCombine(ISD::SHL);
311 setTargetDAGCombine(ISD::SRL);
312 setTargetDAGCombine(ISD::SRA);
313 setTargetDAGCombine(ISD::SIGN_EXTEND);
314 setTargetDAGCombine(ISD::ZERO_EXTEND);
315 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000316 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson5bafff32009-06-22 23:27:02 +0000317 }
318
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000319 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000320
321 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000323
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000324 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000325 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000326
Evan Chenga8e29892007-01-19 07:51:42 +0000327 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000328 if (!Subtarget->isThumb1Only()) {
329 for (unsigned im = (unsigned)ISD::PRE_INC;
330 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000331 setIndexedLoadAction(im, MVT::i1, Legal);
332 setIndexedLoadAction(im, MVT::i8, Legal);
333 setIndexedLoadAction(im, MVT::i16, Legal);
334 setIndexedLoadAction(im, MVT::i32, Legal);
335 setIndexedStoreAction(im, MVT::i1, Legal);
336 setIndexedStoreAction(im, MVT::i8, Legal);
337 setIndexedStoreAction(im, MVT::i16, Legal);
338 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000339 }
Evan Chenga8e29892007-01-19 07:51:42 +0000340 }
341
342 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000343 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000344 setOperationAction(ISD::MUL, MVT::i64, Expand);
345 setOperationAction(ISD::MULHU, MVT::i32, Expand);
346 setOperationAction(ISD::MULHS, MVT::i32, Expand);
347 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
348 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000349 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000350 setOperationAction(ISD::MUL, MVT::i64, Expand);
351 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000352 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000353 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000354 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000355 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000356 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000357 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000358 setOperationAction(ISD::SRL, MVT::i64, Custom);
359 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000360
361 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000362 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000363 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000364 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000365 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000366 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000367
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000368 // Only ARMv6 has BSWAP.
369 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000370 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000371
Evan Chenga8e29892007-01-19 07:51:42 +0000372 // These are expanded into libcalls.
Jim Grosbach29402132010-05-05 23:44:43 +0000373 if (!Subtarget->hasDivide()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000374 // v7M has a hardware divider
375 setOperationAction(ISD::SDIV, MVT::i32, Expand);
376 setOperationAction(ISD::UDIV, MVT::i32, Expand);
377 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000378 setOperationAction(ISD::SREM, MVT::i32, Expand);
379 setOperationAction(ISD::UREM, MVT::i32, Expand);
380 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
381 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000382
Owen Anderson825b72b2009-08-11 20:47:22 +0000383 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
384 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
385 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
386 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000387 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000388
Evan Chengfb3611d2010-05-11 07:26:32 +0000389 setOperationAction(ISD::TRAP, MVT::Other, Legal);
390
Evan Chenga8e29892007-01-19 07:51:42 +0000391 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000392 setOperationAction(ISD::VASTART, MVT::Other, Custom);
393 setOperationAction(ISD::VAARG, MVT::Other, Expand);
394 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
395 setOperationAction(ISD::VAEND, MVT::Other, Expand);
396 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
397 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000398 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
399 // FIXME: Shouldn't need this, since no register is used, but the legalizer
400 // doesn't yet know how to not do that for SjLj.
401 setExceptionSelectorRegister(ARM::R0);
Evan Cheng3a1588a2010-04-15 22:20:34 +0000402 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Jim Grosbach3728e962009-12-10 00:11:09 +0000403 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000404
Jim Grosbach4b77f6a2010-05-07 18:34:55 +0000405 // If the subtarget does not have extract instructions, sign_extend_inreg
406 // needs to be expanded. Extract is available in ARM mode on v6 and up,
407 // and on most Thumb2 implementations.
408 if ((!Subtarget->isThumb() && !Subtarget->hasV6Ops())
409 || (Subtarget->isThumb2() && !Subtarget->hasT2ExtractPack())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000410 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
411 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000412 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000413 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000414
David Goodwinf1daf7d2009-07-08 23:10:31 +0000415 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000416 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
417 // iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000418 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000419
420 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000421 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000422
Owen Anderson825b72b2009-08-11 20:47:22 +0000423 setOperationAction(ISD::SETCC, MVT::i32, Expand);
424 setOperationAction(ISD::SETCC, MVT::f32, Expand);
425 setOperationAction(ISD::SETCC, MVT::f64, Expand);
426 setOperationAction(ISD::SELECT, MVT::i32, Expand);
427 setOperationAction(ISD::SELECT, MVT::f32, Expand);
428 setOperationAction(ISD::SELECT, MVT::f64, Expand);
429 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
430 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
431 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000432
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
434 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
435 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
436 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
437 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000438
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000439 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000440 setOperationAction(ISD::FSIN, MVT::f64, Expand);
441 setOperationAction(ISD::FSIN, MVT::f32, Expand);
442 setOperationAction(ISD::FCOS, MVT::f32, Expand);
443 setOperationAction(ISD::FCOS, MVT::f64, Expand);
444 setOperationAction(ISD::FREM, MVT::f64, Expand);
445 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000446 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000447 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
448 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000449 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000450 setOperationAction(ISD::FPOW, MVT::f64, Expand);
451 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000452
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000453 // Various VFP goodness
454 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000455 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
456 if (Subtarget->hasVFP2()) {
457 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
458 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
459 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
460 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
461 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000462 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000463 if (!Subtarget->hasFP16()) {
464 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
465 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000466 }
Evan Cheng110cf482008-04-01 01:50:16 +0000467 }
Evan Chenga8e29892007-01-19 07:51:42 +0000468
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000469 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000470 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000471 setTargetDAGCombine(ISD::ADD);
472 setTargetDAGCombine(ISD::SUB);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000473
Evan Chenga8e29892007-01-19 07:51:42 +0000474 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Chenga8e29892007-01-19 07:51:42 +0000475 setSchedulingPreference(SchedulingForRegPressure);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000476
Evan Chengbc9b7542009-08-15 07:59:10 +0000477 // FIXME: If-converter should use instruction latency to determine
478 // profitability rather than relying on fixed limits.
479 if (Subtarget->getCPUString() == "generic") {
480 // Generic (and overly aggressive) if-conversion limits.
481 setIfCvtBlockSizeLimit(10);
482 setIfCvtDupBlockSizeLimit(2);
Jim Grosbach35075a72010-03-24 16:15:14 +0000483 } else if (Subtarget->hasV7Ops()) {
Jim Grosbachfceabef2010-03-24 00:03:13 +0000484 setIfCvtBlockSizeLimit(3);
485 setIfCvtDupBlockSizeLimit(1);
Evan Chengbc9b7542009-08-15 07:59:10 +0000486 } else if (Subtarget->hasV6Ops()) {
487 setIfCvtBlockSizeLimit(2);
488 setIfCvtDupBlockSizeLimit(1);
489 } else {
490 setIfCvtBlockSizeLimit(3);
491 setIfCvtDupBlockSizeLimit(2);
Evan Cheng8557c2b2009-06-19 01:51:50 +0000492 }
493
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000494 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Bob Wilsone6abdff2009-05-18 20:55:32 +0000495 // Do not enable CodePlacementOpt for now: it currently runs after the
496 // ARMConstantIslandPass and messes up branch relaxation and placement
497 // of constant islands.
498 // benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000499}
500
Evan Chenga8e29892007-01-19 07:51:42 +0000501const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
502 switch (Opcode) {
503 default: return 0;
504 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000505 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
506 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000507 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000508 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
509 case ARMISD::tCALL: return "ARMISD::tCALL";
510 case ARMISD::BRCOND: return "ARMISD::BRCOND";
511 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000512 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000513 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
514 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
515 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000516 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000517 case ARMISD::CMPFP: return "ARMISD::CMPFP";
518 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
519 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
520 case ARMISD::CMOV: return "ARMISD::CMOV";
521 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000522
Jim Grosbach3482c802010-01-18 19:58:49 +0000523 case ARMISD::RBIT: return "ARMISD::RBIT";
524
Bob Wilson76a312b2010-03-19 22:51:32 +0000525 case ARMISD::FTOSI: return "ARMISD::FTOSI";
526 case ARMISD::FTOUI: return "ARMISD::FTOUI";
527 case ARMISD::SITOF: return "ARMISD::SITOF";
528 case ARMISD::UITOF: return "ARMISD::UITOF";
529
Evan Chenga8e29892007-01-19 07:51:42 +0000530 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
531 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
532 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000533
Jim Grosbache5165492009-11-09 00:11:35 +0000534 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
535 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000536
Evan Chengc5942082009-10-28 06:55:03 +0000537 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
538 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
539
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000540 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000541
Evan Cheng86198642009-08-07 00:34:42 +0000542 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
543
Jim Grosbach3728e962009-12-10 00:11:09 +0000544 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
545 case ARMISD::SYNCBARRIER: return "ARMISD::SYNCBARRIER";
546
Bob Wilson5bafff32009-06-22 23:27:02 +0000547 case ARMISD::VCEQ: return "ARMISD::VCEQ";
548 case ARMISD::VCGE: return "ARMISD::VCGE";
549 case ARMISD::VCGEU: return "ARMISD::VCGEU";
550 case ARMISD::VCGT: return "ARMISD::VCGT";
551 case ARMISD::VCGTU: return "ARMISD::VCGTU";
552 case ARMISD::VTST: return "ARMISD::VTST";
553
554 case ARMISD::VSHL: return "ARMISD::VSHL";
555 case ARMISD::VSHRs: return "ARMISD::VSHRs";
556 case ARMISD::VSHRu: return "ARMISD::VSHRu";
557 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
558 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
559 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
560 case ARMISD::VSHRN: return "ARMISD::VSHRN";
561 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
562 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
563 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
564 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
565 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
566 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
567 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
568 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
569 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
570 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
571 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
572 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
573 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
574 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000575 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000576 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000577 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000578 case ARMISD::VREV64: return "ARMISD::VREV64";
579 case ARMISD::VREV32: return "ARMISD::VREV32";
580 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000581 case ARMISD::VZIP: return "ARMISD::VZIP";
582 case ARMISD::VUZP: return "ARMISD::VUZP";
583 case ARMISD::VTRN: return "ARMISD::VTRN";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000584 case ARMISD::FMAX: return "ARMISD::FMAX";
585 case ARMISD::FMIN: return "ARMISD::FMIN";
Evan Chenga8e29892007-01-19 07:51:42 +0000586 }
587}
588
Bill Wendlingb4202b82009-07-01 18:50:55 +0000589/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000590unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Evan Cheng048e36f2009-10-02 06:57:25 +0000591 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 0 : 1;
Bill Wendling20c568f2009-06-30 22:38:32 +0000592}
593
Evan Chenga8e29892007-01-19 07:51:42 +0000594//===----------------------------------------------------------------------===//
595// Lowering Code
596//===----------------------------------------------------------------------===//
597
Evan Chenga8e29892007-01-19 07:51:42 +0000598/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
599static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
600 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000601 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000602 case ISD::SETNE: return ARMCC::NE;
603 case ISD::SETEQ: return ARMCC::EQ;
604 case ISD::SETGT: return ARMCC::GT;
605 case ISD::SETGE: return ARMCC::GE;
606 case ISD::SETLT: return ARMCC::LT;
607 case ISD::SETLE: return ARMCC::LE;
608 case ISD::SETUGT: return ARMCC::HI;
609 case ISD::SETUGE: return ARMCC::HS;
610 case ISD::SETULT: return ARMCC::LO;
611 case ISD::SETULE: return ARMCC::LS;
612 }
613}
614
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000615/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
616static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000617 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000618 CondCode2 = ARMCC::AL;
619 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000620 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000621 case ISD::SETEQ:
622 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
623 case ISD::SETGT:
624 case ISD::SETOGT: CondCode = ARMCC::GT; break;
625 case ISD::SETGE:
626 case ISD::SETOGE: CondCode = ARMCC::GE; break;
627 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000628 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000629 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
630 case ISD::SETO: CondCode = ARMCC::VC; break;
631 case ISD::SETUO: CondCode = ARMCC::VS; break;
632 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
633 case ISD::SETUGT: CondCode = ARMCC::HI; break;
634 case ISD::SETUGE: CondCode = ARMCC::PL; break;
635 case ISD::SETLT:
636 case ISD::SETULT: CondCode = ARMCC::LT; break;
637 case ISD::SETLE:
638 case ISD::SETULE: CondCode = ARMCC::LE; break;
639 case ISD::SETNE:
640 case ISD::SETUNE: CondCode = ARMCC::NE; break;
641 }
Evan Chenga8e29892007-01-19 07:51:42 +0000642}
643
Bob Wilson1f595bb2009-04-17 19:07:39 +0000644//===----------------------------------------------------------------------===//
645// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000646//===----------------------------------------------------------------------===//
647
648#include "ARMGenCallingConv.inc"
649
650// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000651static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000652 CCValAssign::LocInfo &LocInfo,
653 CCState &State, bool CanFail) {
654 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
655
656 // Try to get the first register.
657 if (unsigned Reg = State.AllocateReg(RegList, 4))
658 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
659 else {
660 // For the 2nd half of a v2f64, do not fail.
661 if (CanFail)
662 return false;
663
664 // Put the whole thing on the stack.
665 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
666 State.AllocateStack(8, 4),
667 LocVT, LocInfo));
668 return true;
669 }
670
671 // Try to get the second register.
672 if (unsigned Reg = State.AllocateReg(RegList, 4))
673 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
674 else
675 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
676 State.AllocateStack(4, 4),
677 LocVT, LocInfo));
678 return true;
679}
680
Owen Andersone50ed302009-08-10 22:56:29 +0000681static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000682 CCValAssign::LocInfo &LocInfo,
683 ISD::ArgFlagsTy &ArgFlags,
684 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000685 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
686 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000687 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000688 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
689 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000690 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000691}
692
693// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000694static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000695 CCValAssign::LocInfo &LocInfo,
696 CCState &State, bool CanFail) {
697 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
698 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
699
700 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
701 if (Reg == 0) {
702 // For the 2nd half of a v2f64, do not just fail.
703 if (CanFail)
704 return false;
705
706 // Put the whole thing on the stack.
707 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
708 State.AllocateStack(8, 8),
709 LocVT, LocInfo));
710 return true;
711 }
712
713 unsigned i;
714 for (i = 0; i < 2; ++i)
715 if (HiRegList[i] == Reg)
716 break;
717
718 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
719 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
720 LocVT, LocInfo));
721 return true;
722}
723
Owen Andersone50ed302009-08-10 22:56:29 +0000724static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000725 CCValAssign::LocInfo &LocInfo,
726 ISD::ArgFlagsTy &ArgFlags,
727 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000728 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
729 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000730 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000731 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
732 return false;
733 return true; // we handled it
734}
735
Owen Andersone50ed302009-08-10 22:56:29 +0000736static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000737 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000738 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
739 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
740
Bob Wilsone65586b2009-04-17 20:40:45 +0000741 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
742 if (Reg == 0)
743 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000744
Bob Wilsone65586b2009-04-17 20:40:45 +0000745 unsigned i;
746 for (i = 0; i < 2; ++i)
747 if (HiRegList[i] == Reg)
748 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000749
Bob Wilson5bafff32009-06-22 23:27:02 +0000750 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000751 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000752 LocVT, LocInfo));
753 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000754}
755
Owen Andersone50ed302009-08-10 22:56:29 +0000756static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000757 CCValAssign::LocInfo &LocInfo,
758 ISD::ArgFlagsTy &ArgFlags,
759 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000760 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
761 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000763 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000764 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000765}
766
Owen Andersone50ed302009-08-10 22:56:29 +0000767static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000768 CCValAssign::LocInfo &LocInfo,
769 ISD::ArgFlagsTy &ArgFlags,
770 CCState &State) {
771 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
772 State);
773}
774
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000775/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
776/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000777CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000778 bool Return,
779 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000780 switch (CC) {
781 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000782 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000783 case CallingConv::C:
784 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000785 // Use target triple & subtarget features to do actual dispatch.
786 if (Subtarget->isAAPCS_ABI()) {
787 if (Subtarget->hasVFP2() &&
788 FloatABIType == FloatABI::Hard && !isVarArg)
789 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
790 else
791 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
792 } else
793 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000794 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000795 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000796 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000797 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000798 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000799 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000800 }
801}
802
Dan Gohman98ca4f22009-08-05 01:29:28 +0000803/// LowerCallResult - Lower the result values of a call into the
804/// appropriate copies out of appropriate physical registers.
805SDValue
806ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000807 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000808 const SmallVectorImpl<ISD::InputArg> &Ins,
809 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000810 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000811
Bob Wilson1f595bb2009-04-17 19:07:39 +0000812 // Assign locations to each value returned by this call.
813 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000814 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000815 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000816 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000817 CCAssignFnForNode(CallConv, /* Return*/ true,
818 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000819
820 // Copy all of the result registers out of their specified physreg.
821 for (unsigned i = 0; i != RVLocs.size(); ++i) {
822 CCValAssign VA = RVLocs[i];
823
Bob Wilson80915242009-04-25 00:33:20 +0000824 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000825 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000826 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000827 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000828 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000829 Chain = Lo.getValue(1);
830 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000831 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000832 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000833 InFlag);
834 Chain = Hi.getValue(1);
835 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000836 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000837
Owen Anderson825b72b2009-08-11 20:47:22 +0000838 if (VA.getLocVT() == MVT::v2f64) {
839 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
840 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
841 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000842
843 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000844 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000845 Chain = Lo.getValue(1);
846 InFlag = Lo.getValue(2);
847 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000848 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000849 Chain = Hi.getValue(1);
850 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000851 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +0000852 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
853 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000854 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000855 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000856 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
857 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000858 Chain = Val.getValue(1);
859 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000860 }
Bob Wilson80915242009-04-25 00:33:20 +0000861
862 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000863 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000864 case CCValAssign::Full: break;
865 case CCValAssign::BCvt:
866 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
867 break;
868 }
869
Dan Gohman98ca4f22009-08-05 01:29:28 +0000870 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000871 }
872
Dan Gohman98ca4f22009-08-05 01:29:28 +0000873 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000874}
875
876/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
877/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000878/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000879/// a byval function parameter.
880/// Sometimes what we are copying is the end of a larger object, the part that
881/// does not fit in registers.
882static SDValue
883CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
884 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
885 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000886 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000887 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +0000888 /*isVolatile=*/false, /*AlwaysInline=*/false,
889 NULL, 0, NULL, 0);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000890}
891
Bob Wilsondee46d72009-04-17 20:35:10 +0000892/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000893SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000894ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
895 SDValue StackPtr, SDValue Arg,
896 DebugLoc dl, SelectionDAG &DAG,
897 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +0000898 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000899 unsigned LocMemOffset = VA.getLocMemOffset();
900 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
901 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
902 if (Flags.isByVal()) {
903 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
904 }
905 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene1b58cab2010-02-15 16:55:24 +0000906 PseudoSourceValue::getStack(), LocMemOffset,
907 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +0000908}
909
Dan Gohman98ca4f22009-08-05 01:29:28 +0000910void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000911 SDValue Chain, SDValue &Arg,
912 RegsToPassVector &RegsToPass,
913 CCValAssign &VA, CCValAssign &NextVA,
914 SDValue &StackPtr,
915 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +0000916 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +0000917
Jim Grosbache5165492009-11-09 00:11:35 +0000918 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000919 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000920 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
921
922 if (NextVA.isRegLoc())
923 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
924 else {
925 assert(NextVA.isMemLoc());
926 if (StackPtr.getNode() == 0)
927 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
928
Dan Gohman98ca4f22009-08-05 01:29:28 +0000929 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
930 dl, DAG, NextVA,
931 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000932 }
933}
934
Dan Gohman98ca4f22009-08-05 01:29:28 +0000935/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000936/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
937/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000938SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000939ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000940 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000941 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000942 const SmallVectorImpl<ISD::OutputArg> &Outs,
943 const SmallVectorImpl<ISD::InputArg> &Ins,
944 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000945 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +0000946 // ARM target does not yet support tail call optimization.
947 isTailCall = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000948
Bob Wilson1f595bb2009-04-17 19:07:39 +0000949 // Analyze operands of the call, assigning locations to each operand.
950 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000951 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
952 *DAG.getContext());
953 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000954 CCAssignFnForNode(CallConv, /* Return*/ false,
955 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +0000956
Bob Wilson1f595bb2009-04-17 19:07:39 +0000957 // Get a count of how many bytes are to be pushed on the stack.
958 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000959
960 // Adjust the stack pointer for the new arguments...
961 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000962 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000963
Jim Grosbachf9a4b762010-02-24 01:43:03 +0000964 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000965
Bob Wilson5bafff32009-06-22 23:27:02 +0000966 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000967 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000968
Bob Wilson1f595bb2009-04-17 19:07:39 +0000969 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +0000970 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000971 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
972 i != e;
973 ++i, ++realArgIdx) {
974 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000975 SDValue Arg = Outs[realArgIdx].Val;
976 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +0000977
Bob Wilson1f595bb2009-04-17 19:07:39 +0000978 // Promote the value if needed.
979 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000980 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +0000981 case CCValAssign::Full: break;
982 case CCValAssign::SExt:
983 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
984 break;
985 case CCValAssign::ZExt:
986 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
987 break;
988 case CCValAssign::AExt:
989 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
990 break;
991 case CCValAssign::BCvt:
992 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
993 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000994 }
995
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000996 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +0000997 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000998 if (VA.getLocVT() == MVT::v2f64) {
999 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1000 DAG.getConstant(0, MVT::i32));
1001 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1002 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001003
Dan Gohman98ca4f22009-08-05 01:29:28 +00001004 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001005 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1006
1007 VA = ArgLocs[++i]; // skip ahead to next loc
1008 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001009 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001010 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1011 } else {
1012 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001013
Dan Gohman98ca4f22009-08-05 01:29:28 +00001014 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1015 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001016 }
1017 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001018 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001019 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001020 }
1021 } else if (VA.isRegLoc()) {
1022 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1023 } else {
1024 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001025
Dan Gohman98ca4f22009-08-05 01:29:28 +00001026 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1027 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001028 }
Evan Chenga8e29892007-01-19 07:51:42 +00001029 }
1030
1031 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001032 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001033 &MemOpChains[0], MemOpChains.size());
1034
1035 // Build a sequence of copy-to-reg nodes chained together with token chain
1036 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001037 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +00001038 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001039 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001040 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +00001041 InFlag = Chain.getValue(1);
1042 }
1043
Bill Wendling056292f2008-09-16 21:48:12 +00001044 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1045 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1046 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001047 bool isDirect = false;
1048 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001049 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001050 MachineFunction &MF = DAG.getMachineFunction();
1051 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001052
1053 if (EnableARMLongCalls) {
1054 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1055 && "long-calls with non-static relocation model!");
1056 // Handle a global address or an external symbol. If it's not one of
1057 // those, the target's already in a register, so we don't need to do
1058 // anything extra.
1059 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001060 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001061 // Create a constant pool entry for the callee address
1062 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1063 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1064 ARMPCLabelIndex,
1065 ARMCP::CPValue, 0);
1066 // Get the address of the callee into a register
1067 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1068 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1069 Callee = DAG.getLoad(getPointerTy(), dl,
1070 DAG.getEntryNode(), CPAddr,
1071 PseudoSourceValue::getConstantPool(), 0,
1072 false, false, 0);
1073 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1074 const char *Sym = S->getSymbol();
1075
1076 // Create a constant pool entry for the callee address
1077 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1078 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1079 Sym, ARMPCLabelIndex, 0);
1080 // Get the address of the callee into a register
1081 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1082 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1083 Callee = DAG.getLoad(getPointerTy(), dl,
1084 DAG.getEntryNode(), CPAddr,
1085 PseudoSourceValue::getConstantPool(), 0,
1086 false, false, 0);
1087 }
1088 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001089 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001090 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001091 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001092 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001093 getTargetMachine().getRelocationModel() != Reloc::Static;
1094 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001095 // ARM call to a local ARM function is predicable.
1096 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +00001097 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001098 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001099 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001100 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001101 ARMPCLabelIndex,
1102 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001103 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001104 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001105 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001106 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001107 PseudoSourceValue::getConstantPool(), 0,
1108 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001109 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001110 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001111 getPointerTy(), Callee, PICLabel);
Jim Grosbache7b52522010-04-14 22:28:31 +00001112 } else
Evan Chengc60e76d2007-01-30 20:37:08 +00001113 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001114 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001115 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001116 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001117 getTargetMachine().getRelocationModel() != Reloc::Static;
1118 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001119 // tBX takes a register source operand.
1120 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001121 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001122 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001123 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001124 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001125 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001126 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001127 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001128 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001129 PseudoSourceValue::getConstantPool(), 0,
1130 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001131 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001132 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001133 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001134 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001135 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001136 }
1137
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001138 // FIXME: handle tail calls differently.
1139 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001140 if (Subtarget->isThumb()) {
1141 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001142 CallOpc = ARMISD::CALL_NOLINK;
1143 else
1144 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1145 } else {
1146 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001147 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1148 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001149 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001150 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001151 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001152 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001153 InFlag = Chain.getValue(1);
1154 }
1155
Dan Gohman475871a2008-07-27 21:46:04 +00001156 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001157 Ops.push_back(Chain);
1158 Ops.push_back(Callee);
1159
1160 // Add argument registers to the end of the list so that they are known live
1161 // into the call.
1162 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1163 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1164 RegsToPass[i].second.getValueType()));
1165
Gabor Greifba36cb52008-08-28 21:40:38 +00001166 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001167 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +00001168 // Returns a chain and a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00001169 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +00001170 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001171 InFlag = Chain.getValue(1);
1172
Chris Lattnere563bbc2008-10-11 22:08:30 +00001173 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1174 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001175 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001176 InFlag = Chain.getValue(1);
1177
Bob Wilson1f595bb2009-04-17 19:07:39 +00001178 // Handle result values, copying them out of physregs into vregs that we
1179 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001180 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1181 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001182}
1183
Dan Gohman98ca4f22009-08-05 01:29:28 +00001184SDValue
1185ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001186 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001187 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmand858e902010-04-17 15:26:15 +00001188 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001189
Bob Wilsondee46d72009-04-17 20:35:10 +00001190 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001191 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001192
Bob Wilsondee46d72009-04-17 20:35:10 +00001193 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001194 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1195 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001196
Dan Gohman98ca4f22009-08-05 01:29:28 +00001197 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001198 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1199 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001200
1201 // If this is the first return lowered for this function, add
1202 // the regs to the liveout set for the function.
1203 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1204 for (unsigned i = 0; i != RVLocs.size(); ++i)
1205 if (RVLocs[i].isRegLoc())
1206 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001207 }
1208
Bob Wilson1f595bb2009-04-17 19:07:39 +00001209 SDValue Flag;
1210
1211 // Copy the result values into the output registers.
1212 for (unsigned i = 0, realRVLocIdx = 0;
1213 i != RVLocs.size();
1214 ++i, ++realRVLocIdx) {
1215 CCValAssign &VA = RVLocs[i];
1216 assert(VA.isRegLoc() && "Can only return in registers!");
1217
Dan Gohman98ca4f22009-08-05 01:29:28 +00001218 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001219
1220 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001221 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001222 case CCValAssign::Full: break;
1223 case CCValAssign::BCvt:
1224 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1225 break;
1226 }
1227
Bob Wilson1f595bb2009-04-17 19:07:39 +00001228 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001229 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001230 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001231 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1232 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001233 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001234 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001235
1236 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1237 Flag = Chain.getValue(1);
1238 VA = RVLocs[++i]; // skip ahead to next loc
1239 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1240 HalfGPRs.getValue(1), Flag);
1241 Flag = Chain.getValue(1);
1242 VA = RVLocs[++i]; // skip ahead to next loc
1243
1244 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001245 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1246 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001247 }
1248 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1249 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001250 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001251 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001252 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001253 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001254 VA = RVLocs[++i]; // skip ahead to next loc
1255 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1256 Flag);
1257 } else
1258 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1259
Bob Wilsondee46d72009-04-17 20:35:10 +00001260 // Guarantee that all emitted copies are
1261 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001262 Flag = Chain.getValue(1);
1263 }
1264
1265 SDValue result;
1266 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001267 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001268 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001269 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001270
1271 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001272}
1273
Bob Wilsonb62d2572009-11-03 00:02:05 +00001274// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1275// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1276// one of the above mentioned nodes. It has to be wrapped because otherwise
1277// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1278// be used to form addressing mode. These wrapped nodes will be selected
1279// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001280static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001281 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001282 // FIXME there is no actual debug info here
1283 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001284 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001285 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001286 if (CP->isMachineConstantPoolEntry())
1287 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1288 CP->getAlignment());
1289 else
1290 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1291 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001292 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001293}
1294
Dan Gohmand858e902010-04-17 15:26:15 +00001295SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1296 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001297 MachineFunction &MF = DAG.getMachineFunction();
1298 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1299 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001300 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001301 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001302 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001303 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1304 SDValue CPAddr;
1305 if (RelocM == Reloc::Static) {
1306 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1307 } else {
1308 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001309 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001310 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1311 ARMCP::CPBlockAddress,
1312 PCAdj);
1313 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1314 }
1315 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1316 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001317 PseudoSourceValue::getConstantPool(), 0,
1318 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001319 if (RelocM == Reloc::Static)
1320 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001321 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001322 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001323}
1324
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001325// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001326SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001327ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001328 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001329 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001330 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001331 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001332 MachineFunction &MF = DAG.getMachineFunction();
1333 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1334 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001335 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001336 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001337 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001338 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001339 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001340 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
David Greene1b58cab2010-02-15 16:55:24 +00001341 PseudoSourceValue::getConstantPool(), 0,
1342 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001343 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001344
Evan Chenge7e0d622009-11-06 22:24:13 +00001345 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001346 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001347
1348 // call __tls_get_addr.
1349 ArgListTy Args;
1350 ArgListEntry Entry;
1351 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001352 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001353 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001354 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001355 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001356 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1357 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001358 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001359 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001360 return CallResult.first;
1361}
1362
1363// Lower ISD::GlobalTLSAddress using the "initial exec" or
1364// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001365SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001366ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001367 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001368 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001369 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001370 SDValue Offset;
1371 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001372 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001373 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001374 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001375
Chris Lattner4fb63d02009-07-15 04:12:33 +00001376 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001377 MachineFunction &MF = DAG.getMachineFunction();
1378 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1379 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1380 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001381 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1382 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001383 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001384 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001385 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001386 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001387 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001388 PseudoSourceValue::getConstantPool(), 0,
1389 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001390 Chain = Offset.getValue(1);
1391
Evan Chenge7e0d622009-11-06 22:24:13 +00001392 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001393 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001394
Evan Cheng9eda6892009-10-31 03:39:36 +00001395 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001396 PseudoSourceValue::getConstantPool(), 0,
1397 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001398 } else {
1399 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001400 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001401 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001402 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001403 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001404 PseudoSourceValue::getConstantPool(), 0,
1405 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001406 }
1407
1408 // The address of the thread local variable is the add of the thread
1409 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001410 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001411}
1412
Dan Gohman475871a2008-07-27 21:46:04 +00001413SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001414ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001415 // TODO: implement the "local dynamic" model
1416 assert(Subtarget->isTargetELF() &&
1417 "TLS not implemented for non-ELF targets");
1418 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1419 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1420 // otherwise use the "Local Exec" TLS Model
1421 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1422 return LowerToTLSGeneralDynamicModel(GA, DAG);
1423 else
1424 return LowerToTLSExecModels(GA, DAG);
1425}
1426
Dan Gohman475871a2008-07-27 21:46:04 +00001427SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001428 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001429 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001430 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001431 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001432 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1433 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001434 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001435 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001436 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001437 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001438 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001439 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001440 CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001441 PseudoSourceValue::getConstantPool(), 0,
1442 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001443 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001444 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001445 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001446 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001447 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001448 PseudoSourceValue::getGOT(), 0,
1449 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001450 return Result;
1451 } else {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001452 // If we have T2 ops, we can materialize the address directly via movt/movw
1453 // pair. This is always cheaper.
1454 if (Subtarget->useMovt()) {
1455 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
1456 DAG.getTargetGlobalAddress(GV, PtrVT));
1457 } else {
1458 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1459 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1460 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001461 PseudoSourceValue::getConstantPool(), 0,
1462 false, false, 0);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001463 }
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001464 }
1465}
1466
Dan Gohman475871a2008-07-27 21:46:04 +00001467SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001468 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001469 MachineFunction &MF = DAG.getMachineFunction();
1470 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1471 unsigned ARMPCLabelIndex = 0;
Owen Andersone50ed302009-08-10 22:56:29 +00001472 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001473 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001474 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001475 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001476 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001477 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001478 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001479 else {
Evan Chenge7e0d622009-11-06 22:24:13 +00001480 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001481 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1482 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001483 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001484 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001485 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001486 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001487
Evan Cheng9eda6892009-10-31 03:39:36 +00001488 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001489 PseudoSourceValue::getConstantPool(), 0,
1490 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001491 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001492
1493 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001494 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001495 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001496 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001497
Evan Cheng63476a82009-09-03 07:04:02 +00001498 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Evan Cheng9eda6892009-10-31 03:39:36 +00001499 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001500 PseudoSourceValue::getGOT(), 0,
1501 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001502
1503 return Result;
1504}
1505
Dan Gohman475871a2008-07-27 21:46:04 +00001506SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001507 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001508 assert(Subtarget->isTargetELF() &&
1509 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00001510 MachineFunction &MF = DAG.getMachineFunction();
1511 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1512 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Andersone50ed302009-08-10 22:56:29 +00001513 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001514 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001515 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001516 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1517 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001518 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001519 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001520 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001521 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001522 PseudoSourceValue::getConstantPool(), 0,
1523 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001524 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001525 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001526}
1527
Jim Grosbach0e0da732009-05-12 23:59:14 +00001528SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00001529ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001530 const ARMSubtarget *Subtarget)
1531 const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001532 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001533 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001534 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001535 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001536 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001537 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001538 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1539 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001540 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001541 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00001542 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1543 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001544 EVT PtrVT = getPointerTy();
1545 DebugLoc dl = Op.getDebugLoc();
1546 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1547 SDValue CPAddr;
1548 unsigned PCAdj = (RelocM != Reloc::PIC_)
1549 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001550 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001551 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1552 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001553 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001554 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001555 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00001556 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001557 PseudoSourceValue::getConstantPool(), 0,
1558 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001559 SDValue Chain = Result.getValue(1);
1560
1561 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001562 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001563 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1564 }
1565 return Result;
1566 }
Jim Grosbachf9570122009-05-14 00:46:35 +00001567 case Intrinsic::eh_sjlj_setjmp:
Jim Grosbacha87ded22010-02-08 23:22:00 +00001568 SDValue Val = Subtarget->isThumb() ?
1569 DAG.getCopyFromReg(DAG.getEntryNode(), dl, ARM::SP, MVT::i32) :
1570 DAG.getConstant(0, MVT::i32);
1571 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1),
1572 Val);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001573 }
1574}
1575
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001576static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
1577 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00001578 DebugLoc dl = Op.getDebugLoc();
1579 SDValue Op5 = Op.getOperand(5);
1580 SDValue Res;
1581 unsigned isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue();
1582 if (isDeviceBarrier) {
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001583 if (Subtarget->hasV7Ops())
1584 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0));
1585 else
1586 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0),
1587 DAG.getConstant(0, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00001588 } else {
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001589 if (Subtarget->hasV7Ops())
1590 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
1591 else
1592 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
1593 DAG.getConstant(0, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00001594 }
1595 return Res;
1596}
1597
Dan Gohman1e93df62010-04-17 14:41:14 +00001598static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
1599 MachineFunction &MF = DAG.getMachineFunction();
1600 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
1601
Evan Chenga8e29892007-01-19 07:51:42 +00001602 // vastart just stores the address of the VarArgsFrameIndex slot into the
1603 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001604 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001605 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00001606 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001607 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
David Greene1b58cab2010-02-15 16:55:24 +00001608 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
1609 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001610}
1611
Dan Gohman475871a2008-07-27 21:46:04 +00001612SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001613ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1614 SelectionDAG &DAG) const {
Evan Cheng86198642009-08-07 00:34:42 +00001615 SDNode *Node = Op.getNode();
1616 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001617 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001618 SDValue Chain = Op.getOperand(0);
1619 SDValue Size = Op.getOperand(1);
1620 SDValue Align = Op.getOperand(2);
1621
1622 // Chain the dynamic stack allocation so that it doesn't modify the stack
1623 // pointer when other instructions are using the stack.
1624 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1625
1626 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1627 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1628 if (AlignVal > StackAlign)
1629 // Do this now since selection pass cannot introduce new target
1630 // independent node.
1631 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1632
1633 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1634 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1635 // do even more horrible hack later.
1636 MachineFunction &MF = DAG.getMachineFunction();
1637 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1638 if (AFI->isThumb1OnlyFunction()) {
1639 bool Negate = true;
1640 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1641 if (C) {
1642 uint32_t Val = C->getZExtValue();
1643 if (Val <= 508 && ((Val & 3) == 0))
1644 Negate = false;
1645 }
1646 if (Negate)
1647 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1648 }
1649
Owen Anderson825b72b2009-08-11 20:47:22 +00001650 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001651 SDValue Ops1[] = { Chain, Size, Align };
1652 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1653 Chain = Res.getValue(1);
1654 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1655 DAG.getIntPtrConstant(0, true), SDValue());
1656 SDValue Ops2[] = { Res, Chain };
1657 return DAG.getMergeValues(Ops2, 2, dl);
1658}
1659
1660SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001661ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1662 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001663 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001664 MachineFunction &MF = DAG.getMachineFunction();
1665 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1666
1667 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001668 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001669 RC = ARM::tGPRRegisterClass;
1670 else
1671 RC = ARM::GPRRegisterClass;
1672
1673 // Transform the arguments stored in physical registers into virtual ones.
1674 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001675 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001676
1677 SDValue ArgValue2;
1678 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001679 MachineFrameInfo *MFI = MF.getFrameInfo();
Bob Wilson6a234f02010-04-13 22:03:22 +00001680 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true, false);
Bob Wilson5bafff32009-06-22 23:27:02 +00001681
1682 // Create load node to retrieve arguments from the stack.
1683 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001684 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00001685 PseudoSourceValue::getFixedStack(FI), 0,
1686 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001687 } else {
1688 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001689 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001690 }
1691
Jim Grosbache5165492009-11-09 00:11:35 +00001692 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001693}
1694
1695SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001696ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001697 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001698 const SmallVectorImpl<ISD::InputArg>
1699 &Ins,
1700 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001701 SmallVectorImpl<SDValue> &InVals)
1702 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001703
Bob Wilson1f595bb2009-04-17 19:07:39 +00001704 MachineFunction &MF = DAG.getMachineFunction();
1705 MachineFrameInfo *MFI = MF.getFrameInfo();
1706
Bob Wilson1f595bb2009-04-17 19:07:39 +00001707 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1708
1709 // Assign locations to all of the incoming arguments.
1710 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001711 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1712 *DAG.getContext());
1713 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001714 CCAssignFnForNode(CallConv, /* Return*/ false,
1715 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001716
1717 SmallVector<SDValue, 16> ArgValues;
1718
1719 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1720 CCValAssign &VA = ArgLocs[i];
1721
Bob Wilsondee46d72009-04-17 20:35:10 +00001722 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001723 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001724 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00001725
Bob Wilson5bafff32009-06-22 23:27:02 +00001726 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001727 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001728 // f64 and vector types are split up into multiple registers or
1729 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00001730 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001731 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001732 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00001733 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00001734 SDValue ArgValue2;
1735 if (VA.isMemLoc()) {
1736 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(),
1737 true, false);
1738 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1739 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
1740 PseudoSourceValue::getFixedStack(FI), 0,
1741 false, false, 0);
1742 } else {
1743 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
1744 Chain, DAG, dl);
1745 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001746 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1747 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001748 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00001749 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001750 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1751 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001752 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001753
Bob Wilson5bafff32009-06-22 23:27:02 +00001754 } else {
1755 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001756
Owen Anderson825b72b2009-08-11 20:47:22 +00001757 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00001758 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001759 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00001760 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001761 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001762 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001763 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001764 RC = (AFI->isThumb1OnlyFunction() ?
1765 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00001766 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001767 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00001768
1769 // Transform the arguments in physical registers into virtual ones.
1770 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001771 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001772 }
1773
1774 // If this is an 8 or 16-bit value, it is really passed promoted
1775 // to 32 bits. Insert an assert[sz]ext to capture this, then
1776 // truncate to the right size.
1777 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001778 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001779 case CCValAssign::Full: break;
1780 case CCValAssign::BCvt:
1781 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1782 break;
1783 case CCValAssign::SExt:
1784 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1785 DAG.getValueType(VA.getValVT()));
1786 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1787 break;
1788 case CCValAssign::ZExt:
1789 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1790 DAG.getValueType(VA.getValVT()));
1791 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1792 break;
1793 }
1794
Dan Gohman98ca4f22009-08-05 01:29:28 +00001795 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001796
1797 } else { // VA.isRegLoc()
1798
1799 // sanity check
1800 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00001801 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001802
1803 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001804 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
1805 true, false);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001806
Bob Wilsondee46d72009-04-17 20:35:10 +00001807 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001808 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001809 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00001810 PseudoSourceValue::getFixedStack(FI), 0,
1811 false, false, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001812 }
1813 }
1814
1815 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001816 if (isVarArg) {
1817 static const unsigned GPRArgRegs[] = {
1818 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1819 };
1820
Bob Wilsondee46d72009-04-17 20:35:10 +00001821 unsigned NumGPRs = CCInfo.getFirstUnallocated
1822 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001823
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001824 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1825 unsigned VARegSize = (4 - NumGPRs) * 4;
1826 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00001827 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001828 if (VARegSaveSize) {
1829 // If this function is vararg, store any remaining integer argument regs
1830 // to their spots on the stack so that they may be loaded by deferencing
1831 // the result of va_next.
1832 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00001833 AFI->setVarArgsFrameIndex(
1834 MFI->CreateFixedObject(VARegSaveSize,
1835 ArgOffset + VARegSaveSize - VARegSize,
1836 true, false));
1837 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
1838 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001839
Dan Gohman475871a2008-07-27 21:46:04 +00001840 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001841 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001842 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001843 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001844 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001845 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001846 RC = ARM::GPRRegisterClass;
1847
Bob Wilson998e1252009-04-20 18:36:57 +00001848 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001849 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00001850 SDValue Store =
1851 DAG.getStore(Val.getValue(1), dl, Val, FIN,
1852 PseudoSourceValue::getFixedStack(AFI->getVarArgsFrameIndex()), 0,
1853 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001854 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001855 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001856 DAG.getConstant(4, getPointerTy()));
1857 }
1858 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001859 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001860 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001861 } else
1862 // This will point to the next argument passed via stack.
Dan Gohman1e93df62010-04-17 14:41:14 +00001863 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset,
1864 true, false));
Evan Chenga8e29892007-01-19 07:51:42 +00001865 }
1866
Dan Gohman98ca4f22009-08-05 01:29:28 +00001867 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00001868}
1869
1870/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001871static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001872 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001873 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001874 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001875 // Maybe this has already been legalized into the constant pool?
1876 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001877 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001878 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00001879 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001880 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001881 }
1882 }
1883 return false;
1884}
1885
Evan Chenga8e29892007-01-19 07:51:42 +00001886/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1887/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00001888SDValue
1889ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Dan Gohmand858e902010-04-17 15:26:15 +00001890 SDValue &ARMCC, SelectionDAG &DAG,
1891 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00001892 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001893 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00001894 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001895 // Constant does not fit, try adjusting it by one?
1896 switch (CC) {
1897 default: break;
1898 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001899 case ISD::SETGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00001900 if (isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001901 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001902 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001903 }
1904 break;
1905 case ISD::SETULT:
1906 case ISD::SETUGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00001907 if (C > 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001908 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001909 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001910 }
1911 break;
1912 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001913 case ISD::SETGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00001914 if (isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001915 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001916 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001917 }
1918 break;
1919 case ISD::SETULE:
1920 case ISD::SETUGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00001921 if (C < 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001922 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001923 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001924 }
1925 break;
1926 }
1927 }
1928 }
1929
1930 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001931 ARMISD::NodeType CompareType;
1932 switch (CondCode) {
1933 default:
1934 CompareType = ARMISD::CMP;
1935 break;
1936 case ARMCC::EQ:
1937 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00001938 // Uses only Z Flag
1939 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001940 break;
1941 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001942 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1943 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001944}
1945
1946/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001947static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001948 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001949 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001950 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00001951 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001952 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001953 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1954 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001955}
1956
Dan Gohmand858e902010-04-17 15:26:15 +00001957SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001958 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001959 SDValue LHS = Op.getOperand(0);
1960 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001961 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001962 SDValue TrueVal = Op.getOperand(2);
1963 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001964 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001965
Owen Anderson825b72b2009-08-11 20:47:22 +00001966 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001967 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001968 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00001969 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Dale Johannesende064702009-02-06 21:50:26 +00001970 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001971 }
1972
1973 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001974 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00001975
Owen Anderson825b72b2009-08-11 20:47:22 +00001976 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1977 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001978 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1979 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001980 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001981 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001982 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001983 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00001984 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001985 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00001986 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001987 }
1988 return Result;
1989}
1990
Dan Gohmand858e902010-04-17 15:26:15 +00001991SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00001992 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001993 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001994 SDValue LHS = Op.getOperand(2);
1995 SDValue RHS = Op.getOperand(3);
1996 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00001997 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001998
Owen Anderson825b72b2009-08-11 20:47:22 +00001999 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00002000 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002001 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00002002 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002003 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00002004 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002005 }
2006
Owen Anderson825b72b2009-08-11 20:47:22 +00002007 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00002008 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002009 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002010
Dale Johannesende064702009-02-06 21:50:26 +00002011 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002012 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
2013 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2014 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002015 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002016 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002017 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002018 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00002019 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002020 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002021 }
2022 return Res;
2023}
2024
Dan Gohmand858e902010-04-17 15:26:15 +00002025SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002026 SDValue Chain = Op.getOperand(0);
2027 SDValue Table = Op.getOperand(1);
2028 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002029 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002030
Owen Andersone50ed302009-08-10 22:56:29 +00002031 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002032 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2033 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002034 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002035 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002036 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002037 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2038 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002039 if (Subtarget->isThumb2()) {
2040 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2041 // which does another jump to the destination. This also makes it easier
2042 // to translate it to TBB / TBH later.
2043 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002044 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002045 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002046 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002047 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002048 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002049 PseudoSourceValue::getJumpTable(), 0,
2050 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002051 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002052 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002053 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002054 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002055 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002056 PseudoSourceValue::getJumpTable(), 0, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002057 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002058 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002059 }
Evan Chenga8e29892007-01-19 07:51:42 +00002060}
2061
Bob Wilson76a312b2010-03-19 22:51:32 +00002062static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2063 DebugLoc dl = Op.getDebugLoc();
2064 unsigned Opc;
2065
2066 switch (Op.getOpcode()) {
2067 default:
2068 assert(0 && "Invalid opcode!");
2069 case ISD::FP_TO_SINT:
2070 Opc = ARMISD::FTOSI;
2071 break;
2072 case ISD::FP_TO_UINT:
2073 Opc = ARMISD::FTOUI;
2074 break;
2075 }
2076 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
2077 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
2078}
2079
2080static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2081 EVT VT = Op.getValueType();
2082 DebugLoc dl = Op.getDebugLoc();
2083 unsigned Opc;
2084
2085 switch (Op.getOpcode()) {
2086 default:
2087 assert(0 && "Invalid opcode!");
2088 case ISD::SINT_TO_FP:
2089 Opc = ARMISD::SITOF;
2090 break;
2091 case ISD::UINT_TO_FP:
2092 Opc = ARMISD::UITOF;
2093 break;
2094 }
2095
2096 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
2097 return DAG.getNode(Opc, dl, VT, Op);
2098}
2099
Dan Gohman475871a2008-07-27 21:46:04 +00002100static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002101 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002102 SDValue Tmp0 = Op.getOperand(0);
2103 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002104 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002105 EVT VT = Op.getValueType();
2106 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00002107 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
2108 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002109 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
2110 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002111 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002112}
2113
Dan Gohmand858e902010-04-17 15:26:15 +00002114SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002115 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2116 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00002117 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002118 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2119 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002120 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002121 ? ARM::R7 : ARM::R11;
2122 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2123 while (Depth--)
David Greene1b58cab2010-02-15 16:55:24 +00002124 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
2125 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002126 return FrameAddr;
2127}
2128
Bob Wilson9f3f0612010-04-17 05:30:19 +00002129/// ExpandBIT_CONVERT - If the target supports VFP, this function is called to
2130/// expand a bit convert where either the source or destination type is i64 to
2131/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2132/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2133/// vectors), since the legalizer won't know what to do with that.
Duncan Sands1607f052008-12-01 11:39:25 +00002134static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002135 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2136 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002137 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002138
Bob Wilson9f3f0612010-04-17 05:30:19 +00002139 // This function is only supposed to be called for i64 types, either as the
2140 // source or destination of the bit convert.
2141 EVT SrcVT = Op.getValueType();
2142 EVT DstVT = N->getValueType(0);
2143 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
2144 "ExpandBIT_CONVERT called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002145
Bob Wilson9f3f0612010-04-17 05:30:19 +00002146 // Turn i64->f64 into VMOVDRR.
2147 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002148 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2149 DAG.getConstant(0, MVT::i32));
2150 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2151 DAG.getConstant(1, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00002152 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002153 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002154
Jim Grosbache5165492009-11-09 00:11:35 +00002155 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002156 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2157 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2158 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2159 // Merge the pieces into a single i64 value.
2160 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2161 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002162
Bob Wilson9f3f0612010-04-17 05:30:19 +00002163 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002164}
2165
Bob Wilson5bafff32009-06-22 23:27:02 +00002166/// getZeroVector - Returns a vector of specified type with all zero elements.
2167///
Owen Andersone50ed302009-08-10 22:56:29 +00002168static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002169 assert(VT.isVector() && "Expected a vector type");
2170
2171 // Zero vectors are used to represent vector negation and in those cases
2172 // will be implemented with the NEON VNEG instruction. However, VNEG does
2173 // not support i64 elements, so sometimes the zero vectors will need to be
2174 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002175 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilson5bafff32009-06-22 23:27:02 +00002176 // to their dest type. This ensures they get CSE'd.
2177 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002178 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2179 SmallVector<SDValue, 8> Ops;
2180 MVT TVT;
2181
2182 if (VT.getSizeInBits() == 64) {
2183 Ops.assign(8, Cst); TVT = MVT::v8i8;
2184 } else {
2185 Ops.assign(16, Cst); TVT = MVT::v16i8;
2186 }
2187 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002188
2189 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2190}
2191
2192/// getOnesVector - Returns a vector of specified type with all bits set.
2193///
Owen Andersone50ed302009-08-10 22:56:29 +00002194static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002195 assert(VT.isVector() && "Expected a vector type");
2196
Bob Wilson929ffa22009-10-30 20:13:25 +00002197 // Always build ones vectors as <16 x i8> or <8 x i8> bitcasted to their
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002198 // dest type. This ensures they get CSE'd.
Bob Wilson5bafff32009-06-22 23:27:02 +00002199 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002200 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2201 SmallVector<SDValue, 8> Ops;
2202 MVT TVT;
2203
2204 if (VT.getSizeInBits() == 64) {
2205 Ops.assign(8, Cst); TVT = MVT::v8i8;
2206 } else {
2207 Ops.assign(16, Cst); TVT = MVT::v16i8;
2208 }
2209 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002210
2211 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2212}
2213
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002214/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2215/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002216SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2217 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002218 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2219 EVT VT = Op.getValueType();
2220 unsigned VTBits = VT.getSizeInBits();
2221 DebugLoc dl = Op.getDebugLoc();
2222 SDValue ShOpLo = Op.getOperand(0);
2223 SDValue ShOpHi = Op.getOperand(1);
2224 SDValue ShAmt = Op.getOperand(2);
2225 SDValue ARMCC;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002226 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002227
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002228 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2229
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002230 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2231 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2232 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2233 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2234 DAG.getConstant(VTBits, MVT::i32));
2235 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2236 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002237 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002238
2239 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2240 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002241 ARMCC, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002242 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002243 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC,
2244 CCR, Cmp);
2245
2246 SDValue Ops[2] = { Lo, Hi };
2247 return DAG.getMergeValues(Ops, 2, dl);
2248}
2249
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002250/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2251/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002252SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2253 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002254 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2255 EVT VT = Op.getValueType();
2256 unsigned VTBits = VT.getSizeInBits();
2257 DebugLoc dl = Op.getDebugLoc();
2258 SDValue ShOpLo = Op.getOperand(0);
2259 SDValue ShOpHi = Op.getOperand(1);
2260 SDValue ShAmt = Op.getOperand(2);
2261 SDValue ARMCC;
2262
2263 assert(Op.getOpcode() == ISD::SHL_PARTS);
2264 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2265 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2266 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2267 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2268 DAG.getConstant(VTBits, MVT::i32));
2269 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2270 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2271
2272 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2273 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2274 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002275 ARMCC, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002276 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
2277 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMCC,
2278 CCR, Cmp);
2279
2280 SDValue Ops[2] = { Lo, Hi };
2281 return DAG.getMergeValues(Ops, 2, dl);
2282}
2283
Jim Grosbach3482c802010-01-18 19:58:49 +00002284static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
2285 const ARMSubtarget *ST) {
2286 EVT VT = N->getValueType(0);
2287 DebugLoc dl = N->getDebugLoc();
2288
2289 if (!ST->hasV6T2Ops())
2290 return SDValue();
2291
2292 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
2293 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
2294}
2295
Bob Wilson5bafff32009-06-22 23:27:02 +00002296static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2297 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002298 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002299 DebugLoc dl = N->getDebugLoc();
2300
2301 // Lower vector shifts on NEON to use VSHL.
2302 if (VT.isVector()) {
2303 assert(ST->hasNEON() && "unexpected vector shift");
2304
2305 // Left shifts translate directly to the vshiftu intrinsic.
2306 if (N->getOpcode() == ISD::SHL)
2307 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002308 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002309 N->getOperand(0), N->getOperand(1));
2310
2311 assert((N->getOpcode() == ISD::SRA ||
2312 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2313
2314 // NEON uses the same intrinsics for both left and right shifts. For
2315 // right shifts, the shift amounts are negative, so negate the vector of
2316 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002317 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002318 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2319 getZeroVector(ShiftVT, DAG, dl),
2320 N->getOperand(1));
2321 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2322 Intrinsic::arm_neon_vshifts :
2323 Intrinsic::arm_neon_vshiftu);
2324 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002325 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002326 N->getOperand(0), NegatedCount);
2327 }
2328
Eli Friedmance392eb2009-08-22 03:13:10 +00002329 // We can get here for a node like i32 = ISD::SHL i32, i64
2330 if (VT != MVT::i64)
2331 return SDValue();
2332
2333 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002334 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002335
Chris Lattner27a6c732007-11-24 07:07:01 +00002336 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2337 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002338 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002339 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002340
Chris Lattner27a6c732007-11-24 07:07:01 +00002341 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002342 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002343
Chris Lattner27a6c732007-11-24 07:07:01 +00002344 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002345 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2346 DAG.getConstant(0, MVT::i32));
2347 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2348 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002349
Chris Lattner27a6c732007-11-24 07:07:01 +00002350 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2351 // captures the result into a carry flag.
2352 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002353 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002354
Chris Lattner27a6c732007-11-24 07:07:01 +00002355 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002356 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002357
Chris Lattner27a6c732007-11-24 07:07:01 +00002358 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002359 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002360}
2361
Bob Wilson5bafff32009-06-22 23:27:02 +00002362static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2363 SDValue TmpOp0, TmpOp1;
2364 bool Invert = false;
2365 bool Swap = false;
2366 unsigned Opc = 0;
2367
2368 SDValue Op0 = Op.getOperand(0);
2369 SDValue Op1 = Op.getOperand(1);
2370 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002371 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002372 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2373 DebugLoc dl = Op.getDebugLoc();
2374
2375 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2376 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002377 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002378 case ISD::SETUNE:
2379 case ISD::SETNE: Invert = true; // Fallthrough
2380 case ISD::SETOEQ:
2381 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2382 case ISD::SETOLT:
2383 case ISD::SETLT: Swap = true; // Fallthrough
2384 case ISD::SETOGT:
2385 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2386 case ISD::SETOLE:
2387 case ISD::SETLE: Swap = true; // Fallthrough
2388 case ISD::SETOGE:
2389 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2390 case ISD::SETUGE: Swap = true; // Fallthrough
2391 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2392 case ISD::SETUGT: Swap = true; // Fallthrough
2393 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2394 case ISD::SETUEQ: Invert = true; // Fallthrough
2395 case ISD::SETONE:
2396 // Expand this to (OLT | OGT).
2397 TmpOp0 = Op0;
2398 TmpOp1 = Op1;
2399 Opc = ISD::OR;
2400 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2401 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2402 break;
2403 case ISD::SETUO: Invert = true; // Fallthrough
2404 case ISD::SETO:
2405 // Expand this to (OLT | OGE).
2406 TmpOp0 = Op0;
2407 TmpOp1 = Op1;
2408 Opc = ISD::OR;
2409 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2410 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2411 break;
2412 }
2413 } else {
2414 // Integer comparisons.
2415 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002416 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002417 case ISD::SETNE: Invert = true;
2418 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2419 case ISD::SETLT: Swap = true;
2420 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2421 case ISD::SETLE: Swap = true;
2422 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2423 case ISD::SETULT: Swap = true;
2424 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2425 case ISD::SETULE: Swap = true;
2426 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2427 }
2428
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002429 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002430 if (Opc == ARMISD::VCEQ) {
2431
2432 SDValue AndOp;
2433 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2434 AndOp = Op0;
2435 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2436 AndOp = Op1;
2437
2438 // Ignore bitconvert.
2439 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2440 AndOp = AndOp.getOperand(0);
2441
2442 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2443 Opc = ARMISD::VTST;
2444 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2445 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2446 Invert = !Invert;
2447 }
2448 }
2449 }
2450
2451 if (Swap)
2452 std::swap(Op0, Op1);
2453
2454 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2455
2456 if (Invert)
2457 Result = DAG.getNOT(dl, Result, VT);
2458
2459 return Result;
2460}
2461
2462/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2463/// VMOV instruction, and if so, return the constant being splatted.
2464static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2465 unsigned SplatBitSize, SelectionDAG &DAG) {
2466 switch (SplatBitSize) {
2467 case 8:
2468 // Any 1-byte value is OK.
2469 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson825b72b2009-08-11 20:47:22 +00002470 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilson5bafff32009-06-22 23:27:02 +00002471
2472 case 16:
2473 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2474 if ((SplatBits & ~0xff) == 0 ||
2475 (SplatBits & ~0xff00) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002476 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilson5bafff32009-06-22 23:27:02 +00002477 break;
2478
2479 case 32:
2480 // NEON's 32-bit VMOV supports splat values where:
2481 // * only one byte is nonzero, or
2482 // * the least significant byte is 0xff and the second byte is nonzero, or
2483 // * the least significant 2 bytes are 0xff and the third is nonzero.
2484 if ((SplatBits & ~0xff) == 0 ||
2485 (SplatBits & ~0xff00) == 0 ||
2486 (SplatBits & ~0xff0000) == 0 ||
2487 (SplatBits & ~0xff000000) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002488 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002489
2490 if ((SplatBits & ~0xffff) == 0 &&
2491 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002492 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002493
2494 if ((SplatBits & ~0xffffff) == 0 &&
2495 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002496 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002497
2498 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2499 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2500 // VMOV.I32. A (very) minor optimization would be to replicate the value
2501 // and fall through here to test for a valid 64-bit splat. But, then the
2502 // caller would also need to check and handle the change in size.
2503 break;
2504
2505 case 64: {
2506 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2507 uint64_t BitMask = 0xff;
2508 uint64_t Val = 0;
2509 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2510 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2511 Val |= BitMask;
2512 else if ((SplatBits & BitMask) != 0)
2513 return SDValue();
2514 BitMask <<= 8;
2515 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002516 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilson5bafff32009-06-22 23:27:02 +00002517 }
2518
2519 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002520 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilson5bafff32009-06-22 23:27:02 +00002521 break;
2522 }
2523
2524 return SDValue();
2525}
2526
2527/// getVMOVImm - If this is a build_vector of constants which can be
2528/// formed by using a VMOV instruction of the specified element size,
2529/// return the constant being splatted. The ByteSize field indicates the
2530/// number of bytes of each element [1248].
2531SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2532 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2533 APInt SplatBits, SplatUndef;
2534 unsigned SplatBitSize;
2535 bool HasAnyUndefs;
2536 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2537 HasAnyUndefs, ByteSize * 8))
2538 return SDValue();
2539
2540 if (SplatBitSize > ByteSize * 8)
2541 return SDValue();
2542
2543 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2544 SplatBitSize, DAG);
2545}
2546
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002547static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2548 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002549 unsigned NumElts = VT.getVectorNumElements();
2550 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002551 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002552
2553 // If this is a VEXT shuffle, the immediate value is the index of the first
2554 // element. The other shuffle indices must be the successive elements after
2555 // the first one.
2556 unsigned ExpectedElt = Imm;
2557 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002558 // Increment the expected index. If it wraps around, it may still be
2559 // a VEXT but the source vectors must be swapped.
2560 ExpectedElt += 1;
2561 if (ExpectedElt == NumElts * 2) {
2562 ExpectedElt = 0;
2563 ReverseVEXT = true;
2564 }
2565
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002566 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002567 return false;
2568 }
2569
2570 // Adjust the index value if the source operands will be swapped.
2571 if (ReverseVEXT)
2572 Imm -= NumElts;
2573
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002574 return true;
2575}
2576
Bob Wilson8bb9e482009-07-26 00:39:34 +00002577/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2578/// instruction with the specified blocksize. (The order of the elements
2579/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002580static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2581 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002582 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2583 "Only possible block sizes for VREV are: 16, 32, 64");
2584
Bob Wilson8bb9e482009-07-26 00:39:34 +00002585 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00002586 if (EltSz == 64)
2587 return false;
2588
2589 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002590 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002591
2592 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2593 return false;
2594
2595 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002596 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002597 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2598 return false;
2599 }
2600
2601 return true;
2602}
2603
Bob Wilsonc692cb72009-08-21 20:54:19 +00002604static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2605 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002606 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2607 if (EltSz == 64)
2608 return false;
2609
Bob Wilsonc692cb72009-08-21 20:54:19 +00002610 unsigned NumElts = VT.getVectorNumElements();
2611 WhichResult = (M[0] == 0 ? 0 : 1);
2612 for (unsigned i = 0; i < NumElts; i += 2) {
2613 if ((unsigned) M[i] != i + WhichResult ||
2614 (unsigned) M[i+1] != i + NumElts + WhichResult)
2615 return false;
2616 }
2617 return true;
2618}
2619
Bob Wilson324f4f12009-12-03 06:40:55 +00002620/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
2621/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2622/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
2623static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2624 unsigned &WhichResult) {
2625 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2626 if (EltSz == 64)
2627 return false;
2628
2629 unsigned NumElts = VT.getVectorNumElements();
2630 WhichResult = (M[0] == 0 ? 0 : 1);
2631 for (unsigned i = 0; i < NumElts; i += 2) {
2632 if ((unsigned) M[i] != i + WhichResult ||
2633 (unsigned) M[i+1] != i + WhichResult)
2634 return false;
2635 }
2636 return true;
2637}
2638
Bob Wilsonc692cb72009-08-21 20:54:19 +00002639static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2640 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002641 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2642 if (EltSz == 64)
2643 return false;
2644
Bob Wilsonc692cb72009-08-21 20:54:19 +00002645 unsigned NumElts = VT.getVectorNumElements();
2646 WhichResult = (M[0] == 0 ? 0 : 1);
2647 for (unsigned i = 0; i != NumElts; ++i) {
2648 if ((unsigned) M[i] != 2 * i + WhichResult)
2649 return false;
2650 }
2651
2652 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002653 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002654 return false;
2655
2656 return true;
2657}
2658
Bob Wilson324f4f12009-12-03 06:40:55 +00002659/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
2660/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2661/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
2662static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2663 unsigned &WhichResult) {
2664 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2665 if (EltSz == 64)
2666 return false;
2667
2668 unsigned Half = VT.getVectorNumElements() / 2;
2669 WhichResult = (M[0] == 0 ? 0 : 1);
2670 for (unsigned j = 0; j != 2; ++j) {
2671 unsigned Idx = WhichResult;
2672 for (unsigned i = 0; i != Half; ++i) {
2673 if ((unsigned) M[i + j * Half] != Idx)
2674 return false;
2675 Idx += 2;
2676 }
2677 }
2678
2679 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2680 if (VT.is64BitVector() && EltSz == 32)
2681 return false;
2682
2683 return true;
2684}
2685
Bob Wilsonc692cb72009-08-21 20:54:19 +00002686static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2687 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002688 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2689 if (EltSz == 64)
2690 return false;
2691
Bob Wilsonc692cb72009-08-21 20:54:19 +00002692 unsigned NumElts = VT.getVectorNumElements();
2693 WhichResult = (M[0] == 0 ? 0 : 1);
2694 unsigned Idx = WhichResult * NumElts / 2;
2695 for (unsigned i = 0; i != NumElts; i += 2) {
2696 if ((unsigned) M[i] != Idx ||
2697 (unsigned) M[i+1] != Idx + NumElts)
2698 return false;
2699 Idx += 1;
2700 }
2701
2702 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002703 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002704 return false;
2705
2706 return true;
2707}
2708
Bob Wilson324f4f12009-12-03 06:40:55 +00002709/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
2710/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2711/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
2712static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2713 unsigned &WhichResult) {
2714 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2715 if (EltSz == 64)
2716 return false;
2717
2718 unsigned NumElts = VT.getVectorNumElements();
2719 WhichResult = (M[0] == 0 ? 0 : 1);
2720 unsigned Idx = WhichResult * NumElts / 2;
2721 for (unsigned i = 0; i != NumElts; i += 2) {
2722 if ((unsigned) M[i] != Idx ||
2723 (unsigned) M[i+1] != Idx)
2724 return false;
2725 Idx += 1;
2726 }
2727
2728 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2729 if (VT.is64BitVector() && EltSz == 32)
2730 return false;
2731
2732 return true;
2733}
2734
2735
Owen Andersone50ed302009-08-10 22:56:29 +00002736static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002737 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00002738 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002739 if (ConstVal->isNullValue())
2740 return getZeroVector(VT, DAG, dl);
2741 if (ConstVal->isAllOnesValue())
2742 return getOnesVector(VT, DAG, dl);
2743
Owen Andersone50ed302009-08-10 22:56:29 +00002744 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00002745 if (VT.is64BitVector()) {
2746 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002747 case 8: CanonicalVT = MVT::v8i8; break;
2748 case 16: CanonicalVT = MVT::v4i16; break;
2749 case 32: CanonicalVT = MVT::v2i32; break;
2750 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002751 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002752 }
2753 } else {
2754 assert(VT.is128BitVector() && "unknown splat vector size");
2755 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002756 case 8: CanonicalVT = MVT::v16i8; break;
2757 case 16: CanonicalVT = MVT::v8i16; break;
2758 case 32: CanonicalVT = MVT::v4i32; break;
2759 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002760 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002761 }
2762 }
2763
2764 // Build a canonical splat for this value.
2765 SmallVector<SDValue, 8> Ops;
2766 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2767 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2768 Ops.size());
2769 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2770}
2771
2772// If this is a case we can't handle, return null and let the default
2773// expansion code take care of it.
2774static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00002775 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002776 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002777 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002778
2779 APInt SplatBits, SplatUndef;
2780 unsigned SplatBitSize;
2781 bool HasAnyUndefs;
2782 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00002783 if (SplatBitSize <= 64) {
2784 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2785 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2786 if (Val.getNode())
2787 return BuildSplat(Val, VT, DAG, dl);
2788 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00002789 }
2790
2791 // If there are only 2 elements in a 128-bit vector, insert them into an
2792 // undef vector. This handles the common case for 128-bit vector argument
2793 // passing, where the insertions should be translated to subreg accesses
2794 // with no real instructions.
2795 if (VT.is128BitVector() && Op.getNumOperands() == 2) {
2796 SDValue Val = DAG.getUNDEF(VT);
2797 SDValue Op0 = Op.getOperand(0);
2798 SDValue Op1 = Op.getOperand(1);
2799 if (Op0.getOpcode() != ISD::UNDEF)
2800 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op0,
2801 DAG.getIntPtrConstant(0));
2802 if (Op1.getOpcode() != ISD::UNDEF)
2803 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op1,
2804 DAG.getIntPtrConstant(1));
2805 return Val;
Bob Wilson5bafff32009-06-22 23:27:02 +00002806 }
2807
2808 return SDValue();
2809}
2810
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002811/// isShuffleMaskLegal - Targets can use this to indicate that they only
2812/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2813/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2814/// are assumed to be legal.
2815bool
2816ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2817 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002818 if (VT.getVectorNumElements() == 4 &&
2819 (VT.is128BitVector() || VT.is64BitVector())) {
2820 unsigned PFIndexes[4];
2821 for (unsigned i = 0; i != 4; ++i) {
2822 if (M[i] < 0)
2823 PFIndexes[i] = 8;
2824 else
2825 PFIndexes[i] = M[i];
2826 }
2827
2828 // Compute the index in the perfect shuffle table.
2829 unsigned PFTableIndex =
2830 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2831 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2832 unsigned Cost = (PFEntry >> 30);
2833
2834 if (Cost <= 4)
2835 return true;
2836 }
2837
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002838 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00002839 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002840
2841 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2842 isVREVMask(M, VT, 64) ||
2843 isVREVMask(M, VT, 32) ||
2844 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00002845 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2846 isVTRNMask(M, VT, WhichResult) ||
2847 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00002848 isVZIPMask(M, VT, WhichResult) ||
2849 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
2850 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
2851 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002852}
2853
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002854/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2855/// the specified operations to build the shuffle.
2856static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2857 SDValue RHS, SelectionDAG &DAG,
2858 DebugLoc dl) {
2859 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2860 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2861 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2862
2863 enum {
2864 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2865 OP_VREV,
2866 OP_VDUP0,
2867 OP_VDUP1,
2868 OP_VDUP2,
2869 OP_VDUP3,
2870 OP_VEXT1,
2871 OP_VEXT2,
2872 OP_VEXT3,
2873 OP_VUZPL, // VUZP, left result
2874 OP_VUZPR, // VUZP, right result
2875 OP_VZIPL, // VZIP, left result
2876 OP_VZIPR, // VZIP, right result
2877 OP_VTRNL, // VTRN, left result
2878 OP_VTRNR // VTRN, right result
2879 };
2880
2881 if (OpNum == OP_COPY) {
2882 if (LHSID == (1*9+2)*9+3) return LHS;
2883 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2884 return RHS;
2885 }
2886
2887 SDValue OpLHS, OpRHS;
2888 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2889 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2890 EVT VT = OpLHS.getValueType();
2891
2892 switch (OpNum) {
2893 default: llvm_unreachable("Unknown shuffle opcode!");
2894 case OP_VREV:
2895 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2896 case OP_VDUP0:
2897 case OP_VDUP1:
2898 case OP_VDUP2:
2899 case OP_VDUP3:
2900 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002901 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002902 case OP_VEXT1:
2903 case OP_VEXT2:
2904 case OP_VEXT3:
2905 return DAG.getNode(ARMISD::VEXT, dl, VT,
2906 OpLHS, OpRHS,
2907 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2908 case OP_VUZPL:
2909 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002910 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002911 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
2912 case OP_VZIPL:
2913 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002914 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002915 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
2916 case OP_VTRNL:
2917 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002918 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2919 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002920 }
2921}
2922
Bob Wilson5bafff32009-06-22 23:27:02 +00002923static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002924 SDValue V1 = Op.getOperand(0);
2925 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00002926 DebugLoc dl = Op.getDebugLoc();
2927 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002928 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002929 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00002930
Bob Wilson28865062009-08-13 02:13:04 +00002931 // Convert shuffles that are directly supported on NEON to target-specific
2932 // DAG nodes, instead of keeping them as shuffles and matching them again
2933 // during code selection. This is more efficient and avoids the possibility
2934 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00002935 // FIXME: floating-point vectors should be canonicalized to integer vectors
2936 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002937 SVN->getMask(ShuffleMask);
2938
2939 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson0ce37102009-08-14 05:08:32 +00002940 int Lane = SVN->getSplatIndex();
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00002941 // If this is undef splat, generate it via "just" vdup, if possible.
2942 if (Lane == -1) Lane = 0;
2943
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002944 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
2945 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00002946 }
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002947 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002948 DAG.getConstant(Lane, MVT::i32));
Bob Wilson0ce37102009-08-14 05:08:32 +00002949 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002950
2951 bool ReverseVEXT;
2952 unsigned Imm;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002953 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002954 if (ReverseVEXT)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002955 std::swap(V1, V2);
2956 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002957 DAG.getConstant(Imm, MVT::i32));
2958 }
2959
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002960 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002961 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002962 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002963 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002964 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002965 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
2966
Bob Wilsonc692cb72009-08-21 20:54:19 +00002967 // Check for Neon shuffles that modify both input vectors in place.
2968 // If both results are used, i.e., if there are two shuffles with the same
2969 // source operands and with masks corresponding to both results of one of
2970 // these operations, DAG memoization will ensure that a single node is
2971 // used for both shuffles.
2972 unsigned WhichResult;
2973 if (isVTRNMask(ShuffleMask, VT, WhichResult))
2974 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2975 V1, V2).getValue(WhichResult);
2976 if (isVUZPMask(ShuffleMask, VT, WhichResult))
2977 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2978 V1, V2).getValue(WhichResult);
2979 if (isVZIPMask(ShuffleMask, VT, WhichResult))
2980 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2981 V1, V2).getValue(WhichResult);
2982
Bob Wilson324f4f12009-12-03 06:40:55 +00002983 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
2984 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2985 V1, V1).getValue(WhichResult);
2986 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
2987 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2988 V1, V1).getValue(WhichResult);
2989 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
2990 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2991 V1, V1).getValue(WhichResult);
2992
Bob Wilsonc692cb72009-08-21 20:54:19 +00002993 // If the shuffle is not directly supported and it has 4 elements, use
2994 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002995 if (VT.getVectorNumElements() == 4 &&
2996 (VT.is128BitVector() || VT.is64BitVector())) {
2997 unsigned PFIndexes[4];
2998 for (unsigned i = 0; i != 4; ++i) {
2999 if (ShuffleMask[i] < 0)
3000 PFIndexes[i] = 8;
3001 else
3002 PFIndexes[i] = ShuffleMask[i];
3003 }
3004
3005 // Compute the index in the perfect shuffle table.
3006 unsigned PFTableIndex =
3007 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3008
3009 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3010 unsigned Cost = (PFEntry >> 30);
3011
3012 if (Cost <= 4)
3013 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
3014 }
Bob Wilsond8e17572009-08-12 22:31:50 +00003015
Bob Wilson22cac0d2009-08-14 05:16:33 +00003016 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003017}
3018
Bob Wilson5bafff32009-06-22 23:27:02 +00003019static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003020 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003021 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003022 SDValue Vec = Op.getOperand(0);
3023 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00003024 assert(VT == MVT::i32 &&
3025 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
3026 "unexpected type for custom-lowering vector extract");
3027 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00003028}
3029
Bob Wilsona6d65862009-08-03 20:36:38 +00003030static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3031 // The only time a CONCAT_VECTORS operation can have legal types is when
3032 // two 64-bit vectors are concatenated to a 128-bit vector.
3033 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
3034 "unexpected CONCAT_VECTORS");
3035 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00003036 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00003037 SDValue Op0 = Op.getOperand(0);
3038 SDValue Op1 = Op.getOperand(1);
3039 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003040 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3041 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00003042 DAG.getIntPtrConstant(0));
3043 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003044 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3045 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00003046 DAG.getIntPtrConstant(1));
3047 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003048}
3049
Dan Gohmand858e902010-04-17 15:26:15 +00003050SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003051 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003052 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00003053 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00003054 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003055 case ISD::GlobalAddress:
3056 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
3057 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00003058 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00003059 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
3060 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003061 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00003062 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00003063 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00003064 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00003065 case ISD::SINT_TO_FP:
3066 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
3067 case ISD::FP_TO_SINT:
3068 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003069 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00003070 case ISD::RETURNADDR: break;
Jim Grosbach0e0da732009-05-12 23:59:14 +00003071 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003072 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00003073 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
3074 Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00003075 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003076 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00003077 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00003078 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00003079 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003080 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00003081 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00003082 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003083 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
3084 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3085 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003086 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00003087 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003088 }
Dan Gohman475871a2008-07-27 21:46:04 +00003089 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003090}
3091
Duncan Sands1607f052008-12-01 11:39:25 +00003092/// ReplaceNodeResults - Replace the results of node with an illegal result
3093/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00003094void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
3095 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00003096 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00003097 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00003098 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00003099 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003100 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003101 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003102 case ISD::BIT_CONVERT:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003103 Res = ExpandBIT_CONVERT(N, DAG);
3104 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00003105 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003106 case ISD::SRA:
3107 Res = LowerShift(N, DAG, Subtarget);
3108 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003109 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00003110 if (Res.getNode())
3111 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00003112}
Chris Lattner27a6c732007-11-24 07:07:01 +00003113
Evan Chenga8e29892007-01-19 07:51:42 +00003114//===----------------------------------------------------------------------===//
3115// ARM Scheduler Hooks
3116//===----------------------------------------------------------------------===//
3117
3118MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003119ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
3120 MachineBasicBlock *BB,
3121 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00003122 unsigned dest = MI->getOperand(0).getReg();
3123 unsigned ptr = MI->getOperand(1).getReg();
3124 unsigned oldval = MI->getOperand(2).getReg();
3125 unsigned newval = MI->getOperand(3).getReg();
3126 unsigned scratch = BB->getParent()->getRegInfo()
3127 .createVirtualRegister(ARM::GPRRegisterClass);
3128 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3129 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003130 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00003131
3132 unsigned ldrOpc, strOpc;
3133 switch (Size) {
3134 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003135 case 1:
3136 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
3137 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
3138 break;
3139 case 2:
3140 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3141 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3142 break;
3143 case 4:
3144 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3145 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3146 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00003147 }
3148
3149 MachineFunction *MF = BB->getParent();
3150 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3151 MachineFunction::iterator It = BB;
3152 ++It; // insert the new blocks after the current block
3153
3154 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3155 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3156 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3157 MF->insert(It, loop1MBB);
3158 MF->insert(It, loop2MBB);
3159 MF->insert(It, exitMBB);
3160 exitMBB->transferSuccessors(BB);
3161
3162 // thisMBB:
3163 // ...
3164 // fallthrough --> loop1MBB
3165 BB->addSuccessor(loop1MBB);
3166
3167 // loop1MBB:
3168 // ldrex dest, [ptr]
3169 // cmp dest, oldval
3170 // bne exitMBB
3171 BB = loop1MBB;
3172 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003173 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003174 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003175 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3176 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003177 BB->addSuccessor(loop2MBB);
3178 BB->addSuccessor(exitMBB);
3179
3180 // loop2MBB:
3181 // strex scratch, newval, [ptr]
3182 // cmp scratch, #0
3183 // bne loop1MBB
3184 BB = loop2MBB;
3185 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
3186 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003187 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003188 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003189 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3190 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003191 BB->addSuccessor(loop1MBB);
3192 BB->addSuccessor(exitMBB);
3193
3194 // exitMBB:
3195 // ...
3196 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00003197
3198 MF->DeleteMachineInstr(MI); // The instruction is gone now.
3199
Jim Grosbach5278eb82009-12-11 01:42:04 +00003200 return BB;
3201}
3202
3203MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003204ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
3205 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00003206 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
3207 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3208
3209 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003210 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003211 MachineFunction::iterator It = BB;
3212 ++It;
3213
3214 unsigned dest = MI->getOperand(0).getReg();
3215 unsigned ptr = MI->getOperand(1).getReg();
3216 unsigned incr = MI->getOperand(2).getReg();
3217 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00003218
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003219 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003220 unsigned ldrOpc, strOpc;
3221 switch (Size) {
3222 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003223 case 1:
3224 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00003225 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003226 break;
3227 case 2:
3228 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3229 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3230 break;
3231 case 4:
3232 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3233 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3234 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00003235 }
3236
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003237 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3238 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3239 MF->insert(It, loopMBB);
3240 MF->insert(It, exitMBB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003241 exitMBB->transferSuccessors(BB);
3242
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003243 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003244 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3245 unsigned scratch2 = (!BinOpcode) ? incr :
3246 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3247
3248 // thisMBB:
3249 // ...
3250 // fallthrough --> loopMBB
3251 BB->addSuccessor(loopMBB);
3252
3253 // loopMBB:
3254 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003255 // <binop> scratch2, dest, incr
3256 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00003257 // cmp scratch, #0
3258 // bne- loopMBB
3259 // fallthrough --> exitMBB
3260 BB = loopMBB;
3261 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00003262 if (BinOpcode) {
3263 // operand order needs to go the other way for NAND
3264 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
3265 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3266 addReg(incr).addReg(dest)).addReg(0);
3267 else
3268 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3269 addReg(dest).addReg(incr)).addReg(0);
3270 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00003271
3272 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
3273 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003274 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00003275 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003276 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3277 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003278
3279 BB->addSuccessor(loopMBB);
3280 BB->addSuccessor(exitMBB);
3281
3282 // exitMBB:
3283 // ...
3284 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00003285
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003286 MF->DeleteMachineInstr(MI); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00003287
Jim Grosbachc3c23542009-12-14 04:22:04 +00003288 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00003289}
3290
3291MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00003292ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003293 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003294 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00003295 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003296 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00003297 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00003298 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00003299 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00003300 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00003301
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003302 case ARM::ATOMIC_LOAD_ADD_I8:
3303 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3304 case ARM::ATOMIC_LOAD_ADD_I16:
3305 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3306 case ARM::ATOMIC_LOAD_ADD_I32:
3307 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003308
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003309 case ARM::ATOMIC_LOAD_AND_I8:
3310 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3311 case ARM::ATOMIC_LOAD_AND_I16:
3312 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3313 case ARM::ATOMIC_LOAD_AND_I32:
3314 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003315
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003316 case ARM::ATOMIC_LOAD_OR_I8:
3317 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3318 case ARM::ATOMIC_LOAD_OR_I16:
3319 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3320 case ARM::ATOMIC_LOAD_OR_I32:
3321 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003322
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003323 case ARM::ATOMIC_LOAD_XOR_I8:
3324 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3325 case ARM::ATOMIC_LOAD_XOR_I16:
3326 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3327 case ARM::ATOMIC_LOAD_XOR_I32:
3328 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003329
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003330 case ARM::ATOMIC_LOAD_NAND_I8:
3331 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3332 case ARM::ATOMIC_LOAD_NAND_I16:
3333 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3334 case ARM::ATOMIC_LOAD_NAND_I32:
3335 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003336
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003337 case ARM::ATOMIC_LOAD_SUB_I8:
3338 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3339 case ARM::ATOMIC_LOAD_SUB_I16:
3340 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3341 case ARM::ATOMIC_LOAD_SUB_I32:
3342 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003343
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003344 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
3345 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
3346 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00003347
3348 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
3349 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
3350 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003351
Evan Cheng007ea272009-08-12 05:17:19 +00003352 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00003353 // To "insert" a SELECT_CC instruction, we actually have to insert the
3354 // diamond control-flow pattern. The incoming instruction knows the
3355 // destination vreg to set, the condition code register to branch on, the
3356 // true/false values to select between, and a branch opcode to use.
3357 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003358 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00003359 ++It;
3360
3361 // thisMBB:
3362 // ...
3363 // TrueVal = ...
3364 // cmpTY ccX, r1, r2
3365 // bCC copy1MBB
3366 // fallthrough --> copy0MBB
3367 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003368 MachineFunction *F = BB->getParent();
3369 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3370 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00003371 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00003372 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003373 F->insert(It, copy0MBB);
3374 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00003375 // Update machine-CFG edges by first adding all successors of the current
3376 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00003377 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003378 E = BB->succ_end(); I != E; ++I)
Evan Chengce319102009-09-19 09:51:03 +00003379 sinkMBB->addSuccessor(*I);
Evan Chenga8e29892007-01-19 07:51:42 +00003380 // Next, remove all successors of the current block, and add the true
3381 // and fallthrough blocks as its successors.
Evan Chengce319102009-09-19 09:51:03 +00003382 while (!BB->succ_empty())
Evan Chenga8e29892007-01-19 07:51:42 +00003383 BB->removeSuccessor(BB->succ_begin());
3384 BB->addSuccessor(copy0MBB);
3385 BB->addSuccessor(sinkMBB);
3386
3387 // copy0MBB:
3388 // %FalseValue = ...
3389 // # fallthrough to sinkMBB
3390 BB = copy0MBB;
3391
3392 // Update machine-CFG edges
3393 BB->addSuccessor(sinkMBB);
3394
3395 // sinkMBB:
3396 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3397 // ...
3398 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00003399 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00003400 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
3401 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
3402
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003403 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00003404 return BB;
3405 }
Evan Cheng86198642009-08-07 00:34:42 +00003406
3407 case ARM::tANDsp:
3408 case ARM::tADDspr_:
3409 case ARM::tSUBspi_:
3410 case ARM::t2SUBrSPi_:
3411 case ARM::t2SUBrSPi12_:
3412 case ARM::t2SUBrSPs_: {
3413 MachineFunction *MF = BB->getParent();
3414 unsigned DstReg = MI->getOperand(0).getReg();
3415 unsigned SrcReg = MI->getOperand(1).getReg();
3416 bool DstIsDead = MI->getOperand(0).isDead();
3417 bool SrcIsKill = MI->getOperand(1).isKill();
3418
3419 if (SrcReg != ARM::SP) {
3420 // Copy the source to SP from virtual register.
3421 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
3422 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3423 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
3424 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
3425 .addReg(SrcReg, getKillRegState(SrcIsKill));
3426 }
3427
3428 unsigned OpOpc = 0;
3429 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
3430 switch (MI->getOpcode()) {
3431 default:
3432 llvm_unreachable("Unexpected pseudo instruction!");
3433 case ARM::tANDsp:
3434 OpOpc = ARM::tAND;
3435 NeedPred = true;
3436 break;
3437 case ARM::tADDspr_:
3438 OpOpc = ARM::tADDspr;
3439 break;
3440 case ARM::tSUBspi_:
3441 OpOpc = ARM::tSUBspi;
3442 break;
3443 case ARM::t2SUBrSPi_:
3444 OpOpc = ARM::t2SUBrSPi;
3445 NeedPred = true; NeedCC = true;
3446 break;
3447 case ARM::t2SUBrSPi12_:
3448 OpOpc = ARM::t2SUBrSPi12;
3449 NeedPred = true;
3450 break;
3451 case ARM::t2SUBrSPs_:
3452 OpOpc = ARM::t2SUBrSPs;
3453 NeedPred = true; NeedCC = true; NeedOp3 = true;
3454 break;
3455 }
3456 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
3457 if (OpOpc == ARM::tAND)
3458 AddDefaultT1CC(MIB);
3459 MIB.addReg(ARM::SP);
3460 MIB.addOperand(MI->getOperand(2));
3461 if (NeedOp3)
3462 MIB.addOperand(MI->getOperand(3));
3463 if (NeedPred)
3464 AddDefaultPred(MIB);
3465 if (NeedCC)
3466 AddDefaultCC(MIB);
3467
3468 // Copy the result from SP to virtual register.
3469 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
3470 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3471 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
3472 BuildMI(BB, dl, TII->get(CopyOpc))
3473 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
3474 .addReg(ARM::SP);
3475 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3476 return BB;
3477 }
Evan Chenga8e29892007-01-19 07:51:42 +00003478 }
3479}
3480
3481//===----------------------------------------------------------------------===//
3482// ARM Optimization Hooks
3483//===----------------------------------------------------------------------===//
3484
Chris Lattnerd1980a52009-03-12 06:52:53 +00003485static
3486SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3487 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00003488 SelectionDAG &DAG = DCI.DAG;
3489 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003490 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00003491 unsigned Opc = N->getOpcode();
3492 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3493 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3494 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3495 ISD::CondCode CC = ISD::SETCC_INVALID;
3496
3497 if (isSlctCC) {
3498 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
3499 } else {
3500 SDValue CCOp = Slct.getOperand(0);
3501 if (CCOp.getOpcode() == ISD::SETCC)
3502 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
3503 }
3504
3505 bool DoXform = false;
3506 bool InvCC = false;
3507 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
3508 "Bad input!");
3509
3510 if (LHS.getOpcode() == ISD::Constant &&
3511 cast<ConstantSDNode>(LHS)->isNullValue()) {
3512 DoXform = true;
3513 } else if (CC != ISD::SETCC_INVALID &&
3514 RHS.getOpcode() == ISD::Constant &&
3515 cast<ConstantSDNode>(RHS)->isNullValue()) {
3516 std::swap(LHS, RHS);
3517 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003518 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00003519 Op0.getOperand(0).getValueType();
3520 bool isInt = OpVT.isInteger();
3521 CC = ISD::getSetCCInverse(CC, isInt);
3522
3523 if (!TLI.isCondCodeLegal(CC, OpVT))
3524 return SDValue(); // Inverse operator isn't legal.
3525
3526 DoXform = true;
3527 InvCC = true;
3528 }
3529
3530 if (DoXform) {
3531 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3532 if (isSlctCC)
3533 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3534 Slct.getOperand(0), Slct.getOperand(1), CC);
3535 SDValue CCOp = Slct.getOperand(0);
3536 if (InvCC)
3537 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3538 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3539 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3540 CCOp, OtherOp, Result);
3541 }
3542 return SDValue();
3543}
3544
3545/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3546static SDValue PerformADDCombine(SDNode *N,
3547 TargetLowering::DAGCombinerInfo &DCI) {
3548 // added by evan in r37685 with no testcase.
3549 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003550
Chris Lattnerd1980a52009-03-12 06:52:53 +00003551 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3552 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3553 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3554 if (Result.getNode()) return Result;
3555 }
3556 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3557 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3558 if (Result.getNode()) return Result;
3559 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003560
Chris Lattnerd1980a52009-03-12 06:52:53 +00003561 return SDValue();
3562}
3563
3564/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3565static SDValue PerformSUBCombine(SDNode *N,
3566 TargetLowering::DAGCombinerInfo &DCI) {
3567 // added by evan in r37685 with no testcase.
3568 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003569
Chris Lattnerd1980a52009-03-12 06:52:53 +00003570 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3571 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3572 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3573 if (Result.getNode()) return Result;
3574 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003575
Chris Lattnerd1980a52009-03-12 06:52:53 +00003576 return SDValue();
3577}
3578
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +00003579/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
3580/// ARMISD::VMOVRRD.
Jim Grosbache5165492009-11-09 00:11:35 +00003581static SDValue PerformVMOVRRDCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003582 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003583 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003584 SDValue InDouble = N->getOperand(0);
Jim Grosbache5165492009-11-09 00:11:35 +00003585 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003586 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003587 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003588}
3589
Bob Wilson5bafff32009-06-22 23:27:02 +00003590/// getVShiftImm - Check if this is a valid build_vector for the immediate
3591/// operand of a vector shift operation, where all the elements of the
3592/// build_vector must have the same constant integer value.
3593static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3594 // Ignore bit_converts.
3595 while (Op.getOpcode() == ISD::BIT_CONVERT)
3596 Op = Op.getOperand(0);
3597 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3598 APInt SplatBits, SplatUndef;
3599 unsigned SplatBitSize;
3600 bool HasAnyUndefs;
3601 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3602 HasAnyUndefs, ElementBits) ||
3603 SplatBitSize > ElementBits)
3604 return false;
3605 Cnt = SplatBits.getSExtValue();
3606 return true;
3607}
3608
3609/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3610/// operand of a vector shift left operation. That value must be in the range:
3611/// 0 <= Value < ElementBits for a left shift; or
3612/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003613static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003614 assert(VT.isVector() && "vector shift count is not a vector type");
3615 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3616 if (! getVShiftImm(Op, ElementBits, Cnt))
3617 return false;
3618 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3619}
3620
3621/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3622/// operand of a vector shift right operation. For a shift opcode, the value
3623/// is positive, but for an intrinsic the value count must be negative. The
3624/// absolute value must be in the range:
3625/// 1 <= |Value| <= ElementBits for a right shift; or
3626/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003627static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00003628 int64_t &Cnt) {
3629 assert(VT.isVector() && "vector shift count is not a vector type");
3630 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3631 if (! getVShiftImm(Op, ElementBits, Cnt))
3632 return false;
3633 if (isIntrinsic)
3634 Cnt = -Cnt;
3635 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3636}
3637
3638/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3639static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3640 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3641 switch (IntNo) {
3642 default:
3643 // Don't do anything for most intrinsics.
3644 break;
3645
3646 // Vector shifts: check for immediate versions and lower them.
3647 // Note: This is done during DAG combining instead of DAG legalizing because
3648 // the build_vectors for 64-bit vector element shift counts are generally
3649 // not legal, and it is hard to see their values after they get legalized to
3650 // loads from a constant pool.
3651 case Intrinsic::arm_neon_vshifts:
3652 case Intrinsic::arm_neon_vshiftu:
3653 case Intrinsic::arm_neon_vshiftls:
3654 case Intrinsic::arm_neon_vshiftlu:
3655 case Intrinsic::arm_neon_vshiftn:
3656 case Intrinsic::arm_neon_vrshifts:
3657 case Intrinsic::arm_neon_vrshiftu:
3658 case Intrinsic::arm_neon_vrshiftn:
3659 case Intrinsic::arm_neon_vqshifts:
3660 case Intrinsic::arm_neon_vqshiftu:
3661 case Intrinsic::arm_neon_vqshiftsu:
3662 case Intrinsic::arm_neon_vqshiftns:
3663 case Intrinsic::arm_neon_vqshiftnu:
3664 case Intrinsic::arm_neon_vqshiftnsu:
3665 case Intrinsic::arm_neon_vqrshiftns:
3666 case Intrinsic::arm_neon_vqrshiftnu:
3667 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00003668 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003669 int64_t Cnt;
3670 unsigned VShiftOpc = 0;
3671
3672 switch (IntNo) {
3673 case Intrinsic::arm_neon_vshifts:
3674 case Intrinsic::arm_neon_vshiftu:
3675 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3676 VShiftOpc = ARMISD::VSHL;
3677 break;
3678 }
3679 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3680 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3681 ARMISD::VSHRs : ARMISD::VSHRu);
3682 break;
3683 }
3684 return SDValue();
3685
3686 case Intrinsic::arm_neon_vshiftls:
3687 case Intrinsic::arm_neon_vshiftlu:
3688 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3689 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003690 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003691
3692 case Intrinsic::arm_neon_vrshifts:
3693 case Intrinsic::arm_neon_vrshiftu:
3694 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3695 break;
3696 return SDValue();
3697
3698 case Intrinsic::arm_neon_vqshifts:
3699 case Intrinsic::arm_neon_vqshiftu:
3700 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3701 break;
3702 return SDValue();
3703
3704 case Intrinsic::arm_neon_vqshiftsu:
3705 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3706 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003707 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003708
3709 case Intrinsic::arm_neon_vshiftn:
3710 case Intrinsic::arm_neon_vrshiftn:
3711 case Intrinsic::arm_neon_vqshiftns:
3712 case Intrinsic::arm_neon_vqshiftnu:
3713 case Intrinsic::arm_neon_vqshiftnsu:
3714 case Intrinsic::arm_neon_vqrshiftns:
3715 case Intrinsic::arm_neon_vqrshiftnu:
3716 case Intrinsic::arm_neon_vqrshiftnsu:
3717 // Narrowing shifts require an immediate right shift.
3718 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3719 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003720 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003721
3722 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003723 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003724 }
3725
3726 switch (IntNo) {
3727 case Intrinsic::arm_neon_vshifts:
3728 case Intrinsic::arm_neon_vshiftu:
3729 // Opcode already set above.
3730 break;
3731 case Intrinsic::arm_neon_vshiftls:
3732 case Intrinsic::arm_neon_vshiftlu:
3733 if (Cnt == VT.getVectorElementType().getSizeInBits())
3734 VShiftOpc = ARMISD::VSHLLi;
3735 else
3736 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3737 ARMISD::VSHLLs : ARMISD::VSHLLu);
3738 break;
3739 case Intrinsic::arm_neon_vshiftn:
3740 VShiftOpc = ARMISD::VSHRN; break;
3741 case Intrinsic::arm_neon_vrshifts:
3742 VShiftOpc = ARMISD::VRSHRs; break;
3743 case Intrinsic::arm_neon_vrshiftu:
3744 VShiftOpc = ARMISD::VRSHRu; break;
3745 case Intrinsic::arm_neon_vrshiftn:
3746 VShiftOpc = ARMISD::VRSHRN; break;
3747 case Intrinsic::arm_neon_vqshifts:
3748 VShiftOpc = ARMISD::VQSHLs; break;
3749 case Intrinsic::arm_neon_vqshiftu:
3750 VShiftOpc = ARMISD::VQSHLu; break;
3751 case Intrinsic::arm_neon_vqshiftsu:
3752 VShiftOpc = ARMISD::VQSHLsu; break;
3753 case Intrinsic::arm_neon_vqshiftns:
3754 VShiftOpc = ARMISD::VQSHRNs; break;
3755 case Intrinsic::arm_neon_vqshiftnu:
3756 VShiftOpc = ARMISD::VQSHRNu; break;
3757 case Intrinsic::arm_neon_vqshiftnsu:
3758 VShiftOpc = ARMISD::VQSHRNsu; break;
3759 case Intrinsic::arm_neon_vqrshiftns:
3760 VShiftOpc = ARMISD::VQRSHRNs; break;
3761 case Intrinsic::arm_neon_vqrshiftnu:
3762 VShiftOpc = ARMISD::VQRSHRNu; break;
3763 case Intrinsic::arm_neon_vqrshiftnsu:
3764 VShiftOpc = ARMISD::VQRSHRNsu; break;
3765 }
3766
3767 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003768 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003769 }
3770
3771 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00003772 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003773 int64_t Cnt;
3774 unsigned VShiftOpc = 0;
3775
3776 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3777 VShiftOpc = ARMISD::VSLI;
3778 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3779 VShiftOpc = ARMISD::VSRI;
3780 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003781 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003782 }
3783
3784 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3785 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00003786 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003787 }
3788
3789 case Intrinsic::arm_neon_vqrshifts:
3790 case Intrinsic::arm_neon_vqrshiftu:
3791 // No immediate versions of these to check for.
3792 break;
3793 }
3794
3795 return SDValue();
3796}
3797
3798/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3799/// lowers them. As with the vector shift intrinsics, this is done during DAG
3800/// combining instead of DAG legalizing because the build_vectors for 64-bit
3801/// vector element shift counts are generally not legal, and it is hard to see
3802/// their values after they get legalized to loads from a constant pool.
3803static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3804 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003805 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003806
3807 // Nothing to be done for scalar shifts.
3808 if (! VT.isVector())
3809 return SDValue();
3810
3811 assert(ST->hasNEON() && "unexpected vector shift");
3812 int64_t Cnt;
3813
3814 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003815 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003816
3817 case ISD::SHL:
3818 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3819 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003820 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003821 break;
3822
3823 case ISD::SRA:
3824 case ISD::SRL:
3825 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3826 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3827 ARMISD::VSHRs : ARMISD::VSHRu);
3828 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003829 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003830 }
3831 }
3832 return SDValue();
3833}
3834
3835/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3836/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3837static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3838 const ARMSubtarget *ST) {
3839 SDValue N0 = N->getOperand(0);
3840
3841 // Check for sign- and zero-extensions of vector extract operations of 8-
3842 // and 16-bit vector elements. NEON supports these directly. They are
3843 // handled during DAG combining because type legalization will promote them
3844 // to 32-bit types and it is messy to recognize the operations after that.
3845 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3846 SDValue Vec = N0.getOperand(0);
3847 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003848 EVT VT = N->getValueType(0);
3849 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003850 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3851
Owen Anderson825b72b2009-08-11 20:47:22 +00003852 if (VT == MVT::i32 &&
3853 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00003854 TLI.isTypeLegal(Vec.getValueType())) {
3855
3856 unsigned Opc = 0;
3857 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003858 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003859 case ISD::SIGN_EXTEND:
3860 Opc = ARMISD::VGETLANEs;
3861 break;
3862 case ISD::ZERO_EXTEND:
3863 case ISD::ANY_EXTEND:
3864 Opc = ARMISD::VGETLANEu;
3865 break;
3866 }
3867 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
3868 }
3869 }
3870
3871 return SDValue();
3872}
3873
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003874/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
3875/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
3876static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
3877 const ARMSubtarget *ST) {
3878 // If the target supports NEON, try to use vmax/vmin instructions for f32
3879 // selects like "x < y ? x : y". Unless the FiniteOnlyFPMath option is set,
3880 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
3881 // a NaN; only do the transformation when it matches that behavior.
3882
3883 // For now only do this when using NEON for FP operations; if using VFP, it
3884 // is not obvious that the benefit outweighs the cost of switching to the
3885 // NEON pipeline.
3886 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
3887 N->getValueType(0) != MVT::f32)
3888 return SDValue();
3889
3890 SDValue CondLHS = N->getOperand(0);
3891 SDValue CondRHS = N->getOperand(1);
3892 SDValue LHS = N->getOperand(2);
3893 SDValue RHS = N->getOperand(3);
3894 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
3895
3896 unsigned Opcode = 0;
3897 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00003898 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003899 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00003900 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003901 IsReversed = true ; // x CC y ? y : x
3902 } else {
3903 return SDValue();
3904 }
3905
Bob Wilsone742bb52010-02-24 22:15:53 +00003906 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003907 switch (CC) {
3908 default: break;
3909 case ISD::SETOLT:
3910 case ISD::SETOLE:
3911 case ISD::SETLT:
3912 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003913 case ISD::SETULT:
3914 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00003915 // If LHS is NaN, an ordered comparison will be false and the result will
3916 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
3917 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
3918 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
3919 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
3920 break;
3921 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
3922 // will return -0, so vmin can only be used for unsafe math or if one of
3923 // the operands is known to be nonzero.
3924 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
3925 !UnsafeFPMath &&
3926 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
3927 break;
3928 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003929 break;
3930
3931 case ISD::SETOGT:
3932 case ISD::SETOGE:
3933 case ISD::SETGT:
3934 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003935 case ISD::SETUGT:
3936 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00003937 // If LHS is NaN, an ordered comparison will be false and the result will
3938 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
3939 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
3940 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
3941 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
3942 break;
3943 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
3944 // will return +0, so vmax can only be used for unsafe math or if one of
3945 // the operands is known to be nonzero.
3946 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
3947 !UnsafeFPMath &&
3948 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
3949 break;
3950 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003951 break;
3952 }
3953
3954 if (!Opcode)
3955 return SDValue();
3956 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
3957}
3958
Dan Gohman475871a2008-07-27 21:46:04 +00003959SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003960 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003961 switch (N->getOpcode()) {
3962 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003963 case ISD::ADD: return PerformADDCombine(N, DCI);
3964 case ISD::SUB: return PerformSUBCombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00003965 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003966 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003967 case ISD::SHL:
3968 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003969 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003970 case ISD::SIGN_EXTEND:
3971 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003972 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
3973 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003974 }
Dan Gohman475871a2008-07-27 21:46:04 +00003975 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003976}
3977
Bill Wendlingaf566342009-08-15 21:21:19 +00003978bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
3979 if (!Subtarget->hasV6Ops())
3980 // Pre-v6 does not support unaligned mem access.
3981 return false;
Anton Korobeynikov90cfc132010-01-30 14:08:12 +00003982 else {
3983 // v6+ may or may not support unaligned mem access depending on the system
3984 // configuration.
3985 // FIXME: This is pretty conservative. Should we provide cmdline option to
3986 // control the behaviour?
Bill Wendlingaf566342009-08-15 21:21:19 +00003987 if (!Subtarget->isTargetDarwin())
3988 return false;
3989 }
3990
3991 switch (VT.getSimpleVT().SimpleTy) {
3992 default:
3993 return false;
3994 case MVT::i8:
3995 case MVT::i16:
3996 case MVT::i32:
3997 return true;
3998 // FIXME: VLD1 etc with standard alignment is legal.
3999 }
4000}
4001
Evan Chenge6c835f2009-08-14 20:09:37 +00004002static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
4003 if (V < 0)
4004 return false;
4005
4006 unsigned Scale = 1;
4007 switch (VT.getSimpleVT().SimpleTy) {
4008 default: return false;
4009 case MVT::i1:
4010 case MVT::i8:
4011 // Scale == 1;
4012 break;
4013 case MVT::i16:
4014 // Scale == 2;
4015 Scale = 2;
4016 break;
4017 case MVT::i32:
4018 // Scale == 4;
4019 Scale = 4;
4020 break;
4021 }
4022
4023 if ((V & (Scale - 1)) != 0)
4024 return false;
4025 V /= Scale;
4026 return V == (V & ((1LL << 5) - 1));
4027}
4028
4029static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
4030 const ARMSubtarget *Subtarget) {
4031 bool isNeg = false;
4032 if (V < 0) {
4033 isNeg = true;
4034 V = - V;
4035 }
4036
4037 switch (VT.getSimpleVT().SimpleTy) {
4038 default: return false;
4039 case MVT::i1:
4040 case MVT::i8:
4041 case MVT::i16:
4042 case MVT::i32:
4043 // + imm12 or - imm8
4044 if (isNeg)
4045 return V == (V & ((1LL << 8) - 1));
4046 return V == (V & ((1LL << 12) - 1));
4047 case MVT::f32:
4048 case MVT::f64:
4049 // Same as ARM mode. FIXME: NEON?
4050 if (!Subtarget->hasVFP2())
4051 return false;
4052 if ((V & 3) != 0)
4053 return false;
4054 V >>= 2;
4055 return V == (V & ((1LL << 8) - 1));
4056 }
4057}
4058
Evan Chengb01fad62007-03-12 23:30:29 +00004059/// isLegalAddressImmediate - Return true if the integer value can be used
4060/// as the offset of the target addressing mode for load / store of the
4061/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00004062static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004063 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00004064 if (V == 0)
4065 return true;
4066
Evan Cheng65011532009-03-09 19:15:00 +00004067 if (!VT.isSimple())
4068 return false;
4069
Evan Chenge6c835f2009-08-14 20:09:37 +00004070 if (Subtarget->isThumb1Only())
4071 return isLegalT1AddressImmediate(V, VT);
4072 else if (Subtarget->isThumb2())
4073 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00004074
Evan Chenge6c835f2009-08-14 20:09:37 +00004075 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00004076 if (V < 0)
4077 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00004078 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00004079 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004080 case MVT::i1:
4081 case MVT::i8:
4082 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00004083 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004084 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004085 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00004086 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004087 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004088 case MVT::f32:
4089 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00004090 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00004091 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00004092 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00004093 return false;
4094 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004095 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00004096 }
Evan Chenga8e29892007-01-19 07:51:42 +00004097}
4098
Evan Chenge6c835f2009-08-14 20:09:37 +00004099bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
4100 EVT VT) const {
4101 int Scale = AM.Scale;
4102 if (Scale < 0)
4103 return false;
4104
4105 switch (VT.getSimpleVT().SimpleTy) {
4106 default: return false;
4107 case MVT::i1:
4108 case MVT::i8:
4109 case MVT::i16:
4110 case MVT::i32:
4111 if (Scale == 1)
4112 return true;
4113 // r + r << imm
4114 Scale = Scale & ~1;
4115 return Scale == 2 || Scale == 4 || Scale == 8;
4116 case MVT::i64:
4117 // r + r
4118 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
4119 return true;
4120 return false;
4121 case MVT::isVoid:
4122 // Note, we allow "void" uses (basically, uses that aren't loads or
4123 // stores), because arm allows folding a scale into many arithmetic
4124 // operations. This should be made more precise and revisited later.
4125
4126 // Allow r << imm, but the imm has to be a multiple of two.
4127 if (Scale & 1) return false;
4128 return isPowerOf2_32(Scale);
4129 }
4130}
4131
Chris Lattner37caf8c2007-04-09 23:33:39 +00004132/// isLegalAddressingMode - Return true if the addressing mode represented
4133/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004134bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004135 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004136 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00004137 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00004138 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004139
Chris Lattner37caf8c2007-04-09 23:33:39 +00004140 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004141 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004142 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004143
Chris Lattner37caf8c2007-04-09 23:33:39 +00004144 switch (AM.Scale) {
4145 case 0: // no scale reg, must be "r+i" or "r", or "i".
4146 break;
4147 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00004148 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00004149 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004150 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00004151 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004152 // ARM doesn't support any R+R*scale+imm addr modes.
4153 if (AM.BaseOffs)
4154 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004155
Bob Wilson2c7dab12009-04-08 17:55:28 +00004156 if (!VT.isSimple())
4157 return false;
4158
Evan Chenge6c835f2009-08-14 20:09:37 +00004159 if (Subtarget->isThumb2())
4160 return isLegalT2ScaledAddressingMode(AM, VT);
4161
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004162 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00004163 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00004164 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004165 case MVT::i1:
4166 case MVT::i8:
4167 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004168 if (Scale < 0) Scale = -Scale;
4169 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004170 return true;
4171 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00004172 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00004173 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00004174 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004175 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004176 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004177 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00004178 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004179
Owen Anderson825b72b2009-08-11 20:47:22 +00004180 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004181 // Note, we allow "void" uses (basically, uses that aren't loads or
4182 // stores), because arm allows folding a scale into many arithmetic
4183 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004184
Chris Lattner37caf8c2007-04-09 23:33:39 +00004185 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00004186 if (Scale & 1) return false;
4187 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00004188 }
4189 break;
Evan Chengb01fad62007-03-12 23:30:29 +00004190 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00004191 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00004192}
4193
Evan Cheng77e47512009-11-11 19:05:52 +00004194/// isLegalICmpImmediate - Return true if the specified immediate is legal
4195/// icmp immediate, that is the target has icmp instructions which can compare
4196/// a register against the immediate without having to materialize the
4197/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00004198bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00004199 if (!Subtarget->isThumb())
4200 return ARM_AM::getSOImmVal(Imm) != -1;
4201 if (Subtarget->isThumb2())
4202 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00004203 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00004204}
4205
Owen Andersone50ed302009-08-10 22:56:29 +00004206static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004207 bool isSEXTLoad, SDValue &Base,
4208 SDValue &Offset, bool &isInc,
4209 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00004210 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4211 return false;
4212
Owen Anderson825b72b2009-08-11 20:47:22 +00004213 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00004214 // AddressingMode 3
4215 Base = Ptr->getOperand(0);
4216 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004217 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004218 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004219 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004220 isInc = false;
4221 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4222 return true;
4223 }
4224 }
4225 isInc = (Ptr->getOpcode() == ISD::ADD);
4226 Offset = Ptr->getOperand(1);
4227 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004228 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00004229 // AddressingMode 2
4230 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004231 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004232 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004233 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004234 isInc = false;
4235 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4236 Base = Ptr->getOperand(0);
4237 return true;
4238 }
4239 }
4240
4241 if (Ptr->getOpcode() == ISD::ADD) {
4242 isInc = true;
4243 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
4244 if (ShOpcVal != ARM_AM::no_shift) {
4245 Base = Ptr->getOperand(1);
4246 Offset = Ptr->getOperand(0);
4247 } else {
4248 Base = Ptr->getOperand(0);
4249 Offset = Ptr->getOperand(1);
4250 }
4251 return true;
4252 }
4253
4254 isInc = (Ptr->getOpcode() == ISD::ADD);
4255 Base = Ptr->getOperand(0);
4256 Offset = Ptr->getOperand(1);
4257 return true;
4258 }
4259
Jim Grosbache5165492009-11-09 00:11:35 +00004260 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00004261 return false;
4262}
4263
Owen Andersone50ed302009-08-10 22:56:29 +00004264static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004265 bool isSEXTLoad, SDValue &Base,
4266 SDValue &Offset, bool &isInc,
4267 SelectionDAG &DAG) {
4268 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4269 return false;
4270
4271 Base = Ptr->getOperand(0);
4272 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
4273 int RHSC = (int)RHS->getZExtValue();
4274 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
4275 assert(Ptr->getOpcode() == ISD::ADD);
4276 isInc = false;
4277 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4278 return true;
4279 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
4280 isInc = Ptr->getOpcode() == ISD::ADD;
4281 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
4282 return true;
4283 }
4284 }
4285
4286 return false;
4287}
4288
Evan Chenga8e29892007-01-19 07:51:42 +00004289/// getPreIndexedAddressParts - returns true by value, base pointer and
4290/// offset pointer and addressing mode by reference if the node's address
4291/// can be legally represented as pre-indexed load / store address.
4292bool
Dan Gohman475871a2008-07-27 21:46:04 +00004293ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
4294 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004295 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004296 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004297 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004298 return false;
4299
Owen Andersone50ed302009-08-10 22:56:29 +00004300 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004301 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004302 bool isSEXTLoad = false;
4303 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4304 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004305 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004306 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4307 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
4308 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004309 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004310 } else
4311 return false;
4312
4313 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004314 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004315 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004316 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
4317 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004318 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004319 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00004320 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00004321 if (!isLegal)
4322 return false;
4323
4324 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
4325 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004326}
4327
4328/// getPostIndexedAddressParts - returns true by value, base pointer and
4329/// offset pointer and addressing mode by reference if this node can be
4330/// combined with a load / store to form a post-indexed load / store.
4331bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00004332 SDValue &Base,
4333 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004334 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004335 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004336 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004337 return false;
4338
Owen Andersone50ed302009-08-10 22:56:29 +00004339 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004340 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004341 bool isSEXTLoad = false;
4342 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004343 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004344 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4345 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004346 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004347 } else
4348 return false;
4349
4350 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004351 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004352 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004353 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004354 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004355 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004356 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
4357 isInc, DAG);
4358 if (!isLegal)
4359 return false;
4360
4361 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
4362 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004363}
4364
Dan Gohman475871a2008-07-27 21:46:04 +00004365void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00004366 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004367 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004368 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00004369 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00004370 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004371 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004372 switch (Op.getOpcode()) {
4373 default: break;
4374 case ARMISD::CMOV: {
4375 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00004376 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004377 if (KnownZero == 0 && KnownOne == 0) return;
4378
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004379 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00004380 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
4381 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004382 KnownZero &= KnownZeroRHS;
4383 KnownOne &= KnownOneRHS;
4384 return;
4385 }
4386 }
4387}
4388
4389//===----------------------------------------------------------------------===//
4390// ARM Inline Assembly Support
4391//===----------------------------------------------------------------------===//
4392
4393/// getConstraintType - Given a constraint letter, return the type of
4394/// constraint it is for this target.
4395ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00004396ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
4397 if (Constraint.size() == 1) {
4398 switch (Constraint[0]) {
4399 default: break;
4400 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004401 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00004402 }
Evan Chenga8e29892007-01-19 07:51:42 +00004403 }
Chris Lattner4234f572007-03-25 02:14:49 +00004404 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00004405}
4406
Bob Wilson2dc4f542009-03-20 22:42:55 +00004407std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00004408ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004409 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004410 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004411 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00004412 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004413 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004414 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004415 return std::make_pair(0U, ARM::tGPRRegisterClass);
4416 else
4417 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004418 case 'r':
4419 return std::make_pair(0U, ARM::GPRRegisterClass);
4420 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004421 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004422 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00004423 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004424 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00004425 if (VT.getSizeInBits() == 128)
4426 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004427 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004428 }
4429 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00004430 if (StringRef("{cc}").equals_lower(Constraint))
4431 return std::make_pair(0U, ARM::CCRRegisterClass);
4432
Evan Chenga8e29892007-01-19 07:51:42 +00004433 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
4434}
4435
4436std::vector<unsigned> ARMTargetLowering::
4437getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004438 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004439 if (Constraint.size() != 1)
4440 return std::vector<unsigned>();
4441
4442 switch (Constraint[0]) { // GCC ARM Constraint Letters
4443 default: break;
4444 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004445 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4446 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4447 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004448 case 'r':
4449 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4450 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4451 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
4452 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004453 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004454 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004455 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
4456 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
4457 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
4458 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
4459 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
4460 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
4461 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
4462 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00004463 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004464 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
4465 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
4466 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
4467 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00004468 if (VT.getSizeInBits() == 128)
4469 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
4470 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004471 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004472 }
4473
4474 return std::vector<unsigned>();
4475}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004476
4477/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
4478/// vector. If it is invalid, don't add anything to Ops.
4479void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
4480 char Constraint,
4481 bool hasMemory,
4482 std::vector<SDValue>&Ops,
4483 SelectionDAG &DAG) const {
4484 SDValue Result(0, 0);
4485
4486 switch (Constraint) {
4487 default: break;
4488 case 'I': case 'J': case 'K': case 'L':
4489 case 'M': case 'N': case 'O':
4490 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
4491 if (!C)
4492 return;
4493
4494 int64_t CVal64 = C->getSExtValue();
4495 int CVal = (int) CVal64;
4496 // None of these constraints allow values larger than 32 bits. Check
4497 // that the value fits in an int.
4498 if (CVal != CVal64)
4499 return;
4500
4501 switch (Constraint) {
4502 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004503 if (Subtarget->isThumb1Only()) {
4504 // This must be a constant between 0 and 255, for ADD
4505 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004506 if (CVal >= 0 && CVal <= 255)
4507 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004508 } else if (Subtarget->isThumb2()) {
4509 // A constant that can be used as an immediate value in a
4510 // data-processing instruction.
4511 if (ARM_AM::getT2SOImmVal(CVal) != -1)
4512 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004513 } else {
4514 // A constant that can be used as an immediate value in a
4515 // data-processing instruction.
4516 if (ARM_AM::getSOImmVal(CVal) != -1)
4517 break;
4518 }
4519 return;
4520
4521 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004522 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004523 // This must be a constant between -255 and -1, for negated ADD
4524 // immediates. This can be used in GCC with an "n" modifier that
4525 // prints the negated value, for use with SUB instructions. It is
4526 // not useful otherwise but is implemented for compatibility.
4527 if (CVal >= -255 && CVal <= -1)
4528 break;
4529 } else {
4530 // This must be a constant between -4095 and 4095. It is not clear
4531 // what this constraint is intended for. Implemented for
4532 // compatibility with GCC.
4533 if (CVal >= -4095 && CVal <= 4095)
4534 break;
4535 }
4536 return;
4537
4538 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004539 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004540 // A 32-bit value where only one byte has a nonzero value. Exclude
4541 // zero to match GCC. This constraint is used by GCC internally for
4542 // constants that can be loaded with a move/shift combination.
4543 // It is not useful otherwise but is implemented for compatibility.
4544 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
4545 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004546 } else if (Subtarget->isThumb2()) {
4547 // A constant whose bitwise inverse can be used as an immediate
4548 // value in a data-processing instruction. This can be used in GCC
4549 // with a "B" modifier that prints the inverted value, for use with
4550 // BIC and MVN instructions. It is not useful otherwise but is
4551 // implemented for compatibility.
4552 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
4553 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004554 } else {
4555 // A constant whose bitwise inverse can be used as an immediate
4556 // value in a data-processing instruction. This can be used in GCC
4557 // with a "B" modifier that prints the inverted value, for use with
4558 // BIC and MVN instructions. It is not useful otherwise but is
4559 // implemented for compatibility.
4560 if (ARM_AM::getSOImmVal(~CVal) != -1)
4561 break;
4562 }
4563 return;
4564
4565 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004566 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004567 // This must be a constant between -7 and 7,
4568 // for 3-operand ADD/SUB immediate instructions.
4569 if (CVal >= -7 && CVal < 7)
4570 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004571 } else if (Subtarget->isThumb2()) {
4572 // A constant whose negation can be used as an immediate value in a
4573 // data-processing instruction. This can be used in GCC with an "n"
4574 // modifier that prints the negated value, for use with SUB
4575 // instructions. It is not useful otherwise but is implemented for
4576 // compatibility.
4577 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
4578 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004579 } else {
4580 // A constant whose negation can be used as an immediate value in a
4581 // data-processing instruction. This can be used in GCC with an "n"
4582 // modifier that prints the negated value, for use with SUB
4583 // instructions. It is not useful otherwise but is implemented for
4584 // compatibility.
4585 if (ARM_AM::getSOImmVal(-CVal) != -1)
4586 break;
4587 }
4588 return;
4589
4590 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004591 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004592 // This must be a multiple of 4 between 0 and 1020, for
4593 // ADD sp + immediate.
4594 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
4595 break;
4596 } else {
4597 // A power of two or a constant between 0 and 32. This is used in
4598 // GCC for the shift amount on shifted register operands, but it is
4599 // useful in general for any shift amounts.
4600 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
4601 break;
4602 }
4603 return;
4604
4605 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004606 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004607 // This must be a constant between 0 and 31, for shift amounts.
4608 if (CVal >= 0 && CVal <= 31)
4609 break;
4610 }
4611 return;
4612
4613 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004614 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004615 // This must be a multiple of 4 between -508 and 508, for
4616 // ADD/SUB sp = sp + immediate.
4617 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
4618 break;
4619 }
4620 return;
4621 }
4622 Result = DAG.getTargetConstant(CVal, Op.getValueType());
4623 break;
4624 }
4625
4626 if (Result.getNode()) {
4627 Ops.push_back(Result);
4628 return;
4629 }
4630 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
4631 Ops, DAG);
4632}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00004633
4634bool
4635ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4636 // The ARM target isn't yet aware of offsets.
4637 return false;
4638}
Evan Cheng39382422009-10-28 01:44:26 +00004639
4640int ARM::getVFPf32Imm(const APFloat &FPImm) {
4641 APInt Imm = FPImm.bitcastToAPInt();
4642 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
4643 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
4644 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
4645
4646 // We can handle 4 bits of mantissa.
4647 // mantissa = (16+UInt(e:f:g:h))/16.
4648 if (Mantissa & 0x7ffff)
4649 return -1;
4650 Mantissa >>= 19;
4651 if ((Mantissa & 0xf) != Mantissa)
4652 return -1;
4653
4654 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4655 if (Exp < -3 || Exp > 4)
4656 return -1;
4657 Exp = ((Exp+3) & 0x7) ^ 4;
4658
4659 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4660}
4661
4662int ARM::getVFPf64Imm(const APFloat &FPImm) {
4663 APInt Imm = FPImm.bitcastToAPInt();
4664 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
4665 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
4666 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
4667
4668 // We can handle 4 bits of mantissa.
4669 // mantissa = (16+UInt(e:f:g:h))/16.
4670 if (Mantissa & 0xffffffffffffLL)
4671 return -1;
4672 Mantissa >>= 48;
4673 if ((Mantissa & 0xf) != Mantissa)
4674 return -1;
4675
4676 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4677 if (Exp < -3 || Exp > 4)
4678 return -1;
4679 Exp = ((Exp+3) & 0x7) ^ 4;
4680
4681 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4682}
4683
4684/// isFPImmLegal - Returns true if the target can instruction select the
4685/// specified FP immediate natively. If false, the legalizer will
4686/// materialize the FP immediate as a load from a constant pool.
4687bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
4688 if (!Subtarget->hasVFP3())
4689 return false;
4690 if (VT == MVT::f32)
4691 return ARM::getVFPf32Imm(Imm) != -1;
4692 if (VT == MVT::f64)
4693 return ARM::getVFPf64Imm(Imm) != -1;
4694 return false;
4695}