blob: 340e8870443ff8da9fa2bfece7ecff6f36a86428 [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
Bob Wilson74dc72e2009-09-15 23:55:57 +0000269 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
270 // neither Neon nor VFP support any arithmetic operations on it.
271 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
272 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
273 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
274 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
275 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
276 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
277 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
278 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
279 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
280 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
281 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
282 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
283 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
284 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
285 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
286 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
287 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
288 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
289 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
290 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
291 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
292 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
293 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
294 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
295
Bob Wilson642b3292009-09-16 00:32:15 +0000296 // Neon does not support some operations on v1i64 and v2i64 types.
297 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
298 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
299 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
300 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
301
Bob Wilson5bafff32009-06-22 23:27:02 +0000302 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
303 setTargetDAGCombine(ISD::SHL);
304 setTargetDAGCombine(ISD::SRL);
305 setTargetDAGCombine(ISD::SRA);
306 setTargetDAGCombine(ISD::SIGN_EXTEND);
307 setTargetDAGCombine(ISD::ZERO_EXTEND);
308 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000309 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson5bafff32009-06-22 23:27:02 +0000310 }
311
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000312 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000313
314 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000315 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000316
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000317 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000319
Evan Chenga8e29892007-01-19 07:51:42 +0000320 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000321 if (!Subtarget->isThumb1Only()) {
322 for (unsigned im = (unsigned)ISD::PRE_INC;
323 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setIndexedLoadAction(im, MVT::i1, Legal);
325 setIndexedLoadAction(im, MVT::i8, Legal);
326 setIndexedLoadAction(im, MVT::i16, Legal);
327 setIndexedLoadAction(im, MVT::i32, Legal);
328 setIndexedStoreAction(im, MVT::i1, Legal);
329 setIndexedStoreAction(im, MVT::i8, Legal);
330 setIndexedStoreAction(im, MVT::i16, Legal);
331 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000332 }
Evan Chenga8e29892007-01-19 07:51:42 +0000333 }
334
335 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000336 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 setOperationAction(ISD::MUL, MVT::i64, Expand);
338 setOperationAction(ISD::MULHU, MVT::i32, Expand);
339 setOperationAction(ISD::MULHS, MVT::i32, Expand);
340 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
341 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000342 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 setOperationAction(ISD::MUL, MVT::i64, Expand);
344 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000345 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000346 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000347 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000348 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000349 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000350 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000351 setOperationAction(ISD::SRL, MVT::i64, Custom);
352 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000353
354 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000356 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000357 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000358 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000359 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000360
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000361 // Only ARMv6 has BSWAP.
362 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000363 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000364
Evan Chenga8e29892007-01-19 07:51:42 +0000365 // These are expanded into libcalls.
Jim Grosbach29402132010-05-05 23:44:43 +0000366 if (!Subtarget->hasDivide()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000367 // v7M has a hardware divider
368 setOperationAction(ISD::SDIV, MVT::i32, Expand);
369 setOperationAction(ISD::UDIV, MVT::i32, Expand);
370 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 setOperationAction(ISD::SREM, MVT::i32, Expand);
372 setOperationAction(ISD::UREM, MVT::i32, Expand);
373 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
374 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000375
Owen Anderson825b72b2009-08-11 20:47:22 +0000376 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
377 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
378 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
379 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000380 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000381
Evan Chengfb3611d2010-05-11 07:26:32 +0000382 setOperationAction(ISD::TRAP, MVT::Other, Legal);
383
Evan Chenga8e29892007-01-19 07:51:42 +0000384 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 setOperationAction(ISD::VASTART, MVT::Other, Custom);
386 setOperationAction(ISD::VAARG, MVT::Other, Expand);
387 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
388 setOperationAction(ISD::VAEND, MVT::Other, Expand);
389 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
390 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000391 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
392 // FIXME: Shouldn't need this, since no register is used, but the legalizer
393 // doesn't yet know how to not do that for SjLj.
394 setExceptionSelectorRegister(ARM::R0);
Evan Cheng3a1588a2010-04-15 22:20:34 +0000395 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Jim Grosbach3728e962009-12-10 00:11:09 +0000396 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000397
Jim Grosbach4b77f6a2010-05-07 18:34:55 +0000398 // If the subtarget does not have extract instructions, sign_extend_inreg
399 // needs to be expanded. Extract is available in ARM mode on v6 and up,
400 // and on most Thumb2 implementations.
401 if ((!Subtarget->isThumb() && !Subtarget->hasV6Ops())
402 || (Subtarget->isThumb2() && !Subtarget->hasT2ExtractPack())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000403 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
404 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000405 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000406 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000407
David Goodwinf1daf7d2009-07-08 23:10:31 +0000408 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000409 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
410 // iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000412
413 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000415
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::SETCC, MVT::i32, Expand);
417 setOperationAction(ISD::SETCC, MVT::f32, Expand);
418 setOperationAction(ISD::SETCC, MVT::f64, Expand);
419 setOperationAction(ISD::SELECT, MVT::i32, Expand);
420 setOperationAction(ISD::SELECT, MVT::f32, Expand);
421 setOperationAction(ISD::SELECT, MVT::f64, Expand);
422 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
423 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
424 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000425
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
427 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
428 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
429 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
430 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000431
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000432 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setOperationAction(ISD::FSIN, MVT::f64, Expand);
434 setOperationAction(ISD::FSIN, MVT::f32, Expand);
435 setOperationAction(ISD::FCOS, MVT::f32, Expand);
436 setOperationAction(ISD::FCOS, MVT::f64, Expand);
437 setOperationAction(ISD::FREM, MVT::f64, Expand);
438 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000439 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000440 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
441 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000442 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setOperationAction(ISD::FPOW, MVT::f64, Expand);
444 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000445
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000446 // Various VFP goodness
447 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000448 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
449 if (Subtarget->hasVFP2()) {
450 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
451 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
452 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
453 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
454 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000455 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000456 if (!Subtarget->hasFP16()) {
457 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
458 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000459 }
Evan Cheng110cf482008-04-01 01:50:16 +0000460 }
Evan Chenga8e29892007-01-19 07:51:42 +0000461
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000462 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000463 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000464 setTargetDAGCombine(ISD::ADD);
465 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000466 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000467
Evan Chenga8e29892007-01-19 07:51:42 +0000468 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000469
Evan Cheng211ffa12010-05-19 20:19:50 +0000470 setSchedulingPreference(Sched::RegPressure);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000471
Evan Chengbc9b7542009-08-15 07:59:10 +0000472 // FIXME: If-converter should use instruction latency to determine
473 // profitability rather than relying on fixed limits.
474 if (Subtarget->getCPUString() == "generic") {
475 // Generic (and overly aggressive) if-conversion limits.
476 setIfCvtBlockSizeLimit(10);
477 setIfCvtDupBlockSizeLimit(2);
Jim Grosbach35075a72010-03-24 16:15:14 +0000478 } else if (Subtarget->hasV7Ops()) {
Jim Grosbachfceabef2010-03-24 00:03:13 +0000479 setIfCvtBlockSizeLimit(3);
480 setIfCvtDupBlockSizeLimit(1);
Evan Chengbc9b7542009-08-15 07:59:10 +0000481 } else if (Subtarget->hasV6Ops()) {
482 setIfCvtBlockSizeLimit(2);
483 setIfCvtDupBlockSizeLimit(1);
484 } else {
485 setIfCvtBlockSizeLimit(3);
486 setIfCvtDupBlockSizeLimit(2);
Evan Cheng8557c2b2009-06-19 01:51:50 +0000487 }
488
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000489 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Bob Wilsone6abdff2009-05-18 20:55:32 +0000490 // Do not enable CodePlacementOpt for now: it currently runs after the
491 // ARMConstantIslandPass and messes up branch relaxation and placement
492 // of constant islands.
493 // benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000494}
495
Evan Chenga8e29892007-01-19 07:51:42 +0000496const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
497 switch (Opcode) {
498 default: return 0;
499 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000500 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
501 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000502 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000503 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
504 case ARMISD::tCALL: return "ARMISD::tCALL";
505 case ARMISD::BRCOND: return "ARMISD::BRCOND";
506 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000507 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000508 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
509 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
510 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000511 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000512 case ARMISD::CMPFP: return "ARMISD::CMPFP";
513 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
514 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
515 case ARMISD::CMOV: return "ARMISD::CMOV";
516 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000517
Jim Grosbach3482c802010-01-18 19:58:49 +0000518 case ARMISD::RBIT: return "ARMISD::RBIT";
519
Bob Wilson76a312b2010-03-19 22:51:32 +0000520 case ARMISD::FTOSI: return "ARMISD::FTOSI";
521 case ARMISD::FTOUI: return "ARMISD::FTOUI";
522 case ARMISD::SITOF: return "ARMISD::SITOF";
523 case ARMISD::UITOF: return "ARMISD::UITOF";
524
Evan Chenga8e29892007-01-19 07:51:42 +0000525 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
526 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
527 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000528
Jim Grosbache5165492009-11-09 00:11:35 +0000529 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
530 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000531
Evan Chengc5942082009-10-28 06:55:03 +0000532 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
533 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
534
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000535 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000536
Evan Cheng86198642009-08-07 00:34:42 +0000537 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
538
Jim Grosbach3728e962009-12-10 00:11:09 +0000539 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
540 case ARMISD::SYNCBARRIER: return "ARMISD::SYNCBARRIER";
541
Bob Wilson5bafff32009-06-22 23:27:02 +0000542 case ARMISD::VCEQ: return "ARMISD::VCEQ";
543 case ARMISD::VCGE: return "ARMISD::VCGE";
544 case ARMISD::VCGEU: return "ARMISD::VCGEU";
545 case ARMISD::VCGT: return "ARMISD::VCGT";
546 case ARMISD::VCGTU: return "ARMISD::VCGTU";
547 case ARMISD::VTST: return "ARMISD::VTST";
548
549 case ARMISD::VSHL: return "ARMISD::VSHL";
550 case ARMISD::VSHRs: return "ARMISD::VSHRs";
551 case ARMISD::VSHRu: return "ARMISD::VSHRu";
552 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
553 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
554 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
555 case ARMISD::VSHRN: return "ARMISD::VSHRN";
556 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
557 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
558 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
559 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
560 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
561 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
562 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
563 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
564 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
565 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
566 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
567 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
568 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
569 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000570 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000571 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000572 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000573 case ARMISD::VREV64: return "ARMISD::VREV64";
574 case ARMISD::VREV32: return "ARMISD::VREV32";
575 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000576 case ARMISD::VZIP: return "ARMISD::VZIP";
577 case ARMISD::VUZP: return "ARMISD::VUZP";
578 case ARMISD::VTRN: return "ARMISD::VTRN";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000579 case ARMISD::FMAX: return "ARMISD::FMAX";
580 case ARMISD::FMIN: return "ARMISD::FMIN";
Evan Chenga8e29892007-01-19 07:51:42 +0000581 }
582}
583
Evan Cheng06b666c2010-05-15 02:18:07 +0000584/// getRegClassFor - Return the register class that should be used for the
585/// specified value type.
586TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
587 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
588 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
589 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000590 if (Subtarget->hasNEON()) {
591 if (VT == MVT::v4i64)
592 return ARM::QQPRRegisterClass;
593 else if (VT == MVT::v8i64)
594 return ARM::QQQQPRRegisterClass;
595 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000596 return TargetLowering::getRegClassFor(VT);
597}
598
Bill Wendlingb4202b82009-07-01 18:50:55 +0000599/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000600unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Evan Cheng048e36f2009-10-02 06:57:25 +0000601 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 0 : 1;
Bill Wendling20c568f2009-06-30 22:38:32 +0000602}
603
Evan Cheng1cc39842010-05-20 23:26:43 +0000604Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
605 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
606 EVT VT = N->getValueType(i);
607 if (VT.isFloatingPoint() || VT.isVector())
608 return Sched::Latency;
609 }
610 return Sched::RegPressure;
611}
612
Evan Chenga8e29892007-01-19 07:51:42 +0000613//===----------------------------------------------------------------------===//
614// Lowering Code
615//===----------------------------------------------------------------------===//
616
Evan Chenga8e29892007-01-19 07:51:42 +0000617/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
618static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
619 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000620 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000621 case ISD::SETNE: return ARMCC::NE;
622 case ISD::SETEQ: return ARMCC::EQ;
623 case ISD::SETGT: return ARMCC::GT;
624 case ISD::SETGE: return ARMCC::GE;
625 case ISD::SETLT: return ARMCC::LT;
626 case ISD::SETLE: return ARMCC::LE;
627 case ISD::SETUGT: return ARMCC::HI;
628 case ISD::SETUGE: return ARMCC::HS;
629 case ISD::SETULT: return ARMCC::LO;
630 case ISD::SETULE: return ARMCC::LS;
631 }
632}
633
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000634/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
635static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000636 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000637 CondCode2 = ARMCC::AL;
638 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000639 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000640 case ISD::SETEQ:
641 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
642 case ISD::SETGT:
643 case ISD::SETOGT: CondCode = ARMCC::GT; break;
644 case ISD::SETGE:
645 case ISD::SETOGE: CondCode = ARMCC::GE; break;
646 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000647 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000648 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
649 case ISD::SETO: CondCode = ARMCC::VC; break;
650 case ISD::SETUO: CondCode = ARMCC::VS; break;
651 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
652 case ISD::SETUGT: CondCode = ARMCC::HI; break;
653 case ISD::SETUGE: CondCode = ARMCC::PL; break;
654 case ISD::SETLT:
655 case ISD::SETULT: CondCode = ARMCC::LT; break;
656 case ISD::SETLE:
657 case ISD::SETULE: CondCode = ARMCC::LE; break;
658 case ISD::SETNE:
659 case ISD::SETUNE: CondCode = ARMCC::NE; break;
660 }
Evan Chenga8e29892007-01-19 07:51:42 +0000661}
662
Bob Wilson1f595bb2009-04-17 19:07:39 +0000663//===----------------------------------------------------------------------===//
664// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000665//===----------------------------------------------------------------------===//
666
667#include "ARMGenCallingConv.inc"
668
669// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000670static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000671 CCValAssign::LocInfo &LocInfo,
672 CCState &State, bool CanFail) {
673 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
674
675 // Try to get the first register.
676 if (unsigned Reg = State.AllocateReg(RegList, 4))
677 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
678 else {
679 // For the 2nd half of a v2f64, do not fail.
680 if (CanFail)
681 return false;
682
683 // Put the whole thing on the stack.
684 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
685 State.AllocateStack(8, 4),
686 LocVT, LocInfo));
687 return true;
688 }
689
690 // Try to get the second register.
691 if (unsigned Reg = State.AllocateReg(RegList, 4))
692 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
693 else
694 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
695 State.AllocateStack(4, 4),
696 LocVT, LocInfo));
697 return true;
698}
699
Owen Andersone50ed302009-08-10 22:56:29 +0000700static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000701 CCValAssign::LocInfo &LocInfo,
702 ISD::ArgFlagsTy &ArgFlags,
703 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000704 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
705 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000706 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000707 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
708 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000709 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000710}
711
712// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000713static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000714 CCValAssign::LocInfo &LocInfo,
715 CCState &State, bool CanFail) {
716 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
717 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
718
719 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
720 if (Reg == 0) {
721 // For the 2nd half of a v2f64, do not just fail.
722 if (CanFail)
723 return false;
724
725 // Put the whole thing on the stack.
726 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
727 State.AllocateStack(8, 8),
728 LocVT, LocInfo));
729 return true;
730 }
731
732 unsigned i;
733 for (i = 0; i < 2; ++i)
734 if (HiRegList[i] == Reg)
735 break;
736
737 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
738 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
739 LocVT, LocInfo));
740 return true;
741}
742
Owen Andersone50ed302009-08-10 22:56:29 +0000743static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000744 CCValAssign::LocInfo &LocInfo,
745 ISD::ArgFlagsTy &ArgFlags,
746 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000747 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
748 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000749 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000750 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
751 return false;
752 return true; // we handled it
753}
754
Owen Andersone50ed302009-08-10 22:56:29 +0000755static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000756 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000757 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
758 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
759
Bob Wilsone65586b2009-04-17 20:40:45 +0000760 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
761 if (Reg == 0)
762 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000763
Bob Wilsone65586b2009-04-17 20:40:45 +0000764 unsigned i;
765 for (i = 0; i < 2; ++i)
766 if (HiRegList[i] == Reg)
767 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000768
Bob Wilson5bafff32009-06-22 23:27:02 +0000769 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000770 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000771 LocVT, LocInfo));
772 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000773}
774
Owen Andersone50ed302009-08-10 22:56:29 +0000775static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000776 CCValAssign::LocInfo &LocInfo,
777 ISD::ArgFlagsTy &ArgFlags,
778 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000779 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
780 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000781 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000782 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000783 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000784}
785
Owen Andersone50ed302009-08-10 22:56:29 +0000786static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000787 CCValAssign::LocInfo &LocInfo,
788 ISD::ArgFlagsTy &ArgFlags,
789 CCState &State) {
790 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
791 State);
792}
793
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000794/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
795/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000796CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000797 bool Return,
798 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000799 switch (CC) {
800 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000801 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000802 case CallingConv::C:
803 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000804 // Use target triple & subtarget features to do actual dispatch.
805 if (Subtarget->isAAPCS_ABI()) {
806 if (Subtarget->hasVFP2() &&
807 FloatABIType == FloatABI::Hard && !isVarArg)
808 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
809 else
810 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
811 } else
812 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000813 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000814 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000815 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000816 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000817 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000818 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000819 }
820}
821
Dan Gohman98ca4f22009-08-05 01:29:28 +0000822/// LowerCallResult - Lower the result values of a call into the
823/// appropriate copies out of appropriate physical registers.
824SDValue
825ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000826 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000827 const SmallVectorImpl<ISD::InputArg> &Ins,
828 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000829 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000830
Bob Wilson1f595bb2009-04-17 19:07:39 +0000831 // Assign locations to each value returned by this call.
832 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000833 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000834 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000835 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000836 CCAssignFnForNode(CallConv, /* Return*/ true,
837 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000838
839 // Copy all of the result registers out of their specified physreg.
840 for (unsigned i = 0; i != RVLocs.size(); ++i) {
841 CCValAssign VA = RVLocs[i];
842
Bob Wilson80915242009-04-25 00:33:20 +0000843 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000844 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000845 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000846 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000847 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000848 Chain = Lo.getValue(1);
849 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000850 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000851 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000852 InFlag);
853 Chain = Hi.getValue(1);
854 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000855 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000856
Owen Anderson825b72b2009-08-11 20:47:22 +0000857 if (VA.getLocVT() == MVT::v2f64) {
858 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
859 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
860 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000861
862 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000863 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000864 Chain = Lo.getValue(1);
865 InFlag = Lo.getValue(2);
866 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000867 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000868 Chain = Hi.getValue(1);
869 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +0000870 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +0000871 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
872 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000873 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000874 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000875 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
876 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000877 Chain = Val.getValue(1);
878 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000879 }
Bob Wilson80915242009-04-25 00:33:20 +0000880
881 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000882 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000883 case CCValAssign::Full: break;
884 case CCValAssign::BCvt:
885 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
886 break;
887 }
888
Dan Gohman98ca4f22009-08-05 01:29:28 +0000889 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000890 }
891
Dan Gohman98ca4f22009-08-05 01:29:28 +0000892 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000893}
894
895/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
896/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000897/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000898/// a byval function parameter.
899/// Sometimes what we are copying is the end of a larger object, the part that
900/// does not fit in registers.
901static SDValue
902CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
903 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
904 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000905 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000906 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +0000907 /*isVolatile=*/false, /*AlwaysInline=*/false,
908 NULL, 0, NULL, 0);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000909}
910
Bob Wilsondee46d72009-04-17 20:35:10 +0000911/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000912SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000913ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
914 SDValue StackPtr, SDValue Arg,
915 DebugLoc dl, SelectionDAG &DAG,
916 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +0000917 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000918 unsigned LocMemOffset = VA.getLocMemOffset();
919 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
920 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
921 if (Flags.isByVal()) {
922 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
923 }
924 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene1b58cab2010-02-15 16:55:24 +0000925 PseudoSourceValue::getStack(), LocMemOffset,
926 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +0000927}
928
Dan Gohman98ca4f22009-08-05 01:29:28 +0000929void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000930 SDValue Chain, SDValue &Arg,
931 RegsToPassVector &RegsToPass,
932 CCValAssign &VA, CCValAssign &NextVA,
933 SDValue &StackPtr,
934 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +0000935 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +0000936
Jim Grosbache5165492009-11-09 00:11:35 +0000937 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000938 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000939 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
940
941 if (NextVA.isRegLoc())
942 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
943 else {
944 assert(NextVA.isMemLoc());
945 if (StackPtr.getNode() == 0)
946 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
947
Dan Gohman98ca4f22009-08-05 01:29:28 +0000948 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
949 dl, DAG, NextVA,
950 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000951 }
952}
953
Dan Gohman98ca4f22009-08-05 01:29:28 +0000954/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000955/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
956/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000957SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000958ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000959 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000960 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000961 const SmallVectorImpl<ISD::OutputArg> &Outs,
962 const SmallVectorImpl<ISD::InputArg> &Ins,
963 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000964 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +0000965 // ARM target does not yet support tail call optimization.
966 isTailCall = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000967
Bob Wilson1f595bb2009-04-17 19:07:39 +0000968 // Analyze operands of the call, assigning locations to each operand.
969 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000970 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
971 *DAG.getContext());
972 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000973 CCAssignFnForNode(CallConv, /* Return*/ false,
974 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +0000975
Bob Wilson1f595bb2009-04-17 19:07:39 +0000976 // Get a count of how many bytes are to be pushed on the stack.
977 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000978
979 // Adjust the stack pointer for the new arguments...
980 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000981 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000982
Jim Grosbachf9a4b762010-02-24 01:43:03 +0000983 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000984
Bob Wilson5bafff32009-06-22 23:27:02 +0000985 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000986 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000987
Bob Wilson1f595bb2009-04-17 19:07:39 +0000988 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +0000989 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000990 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
991 i != e;
992 ++i, ++realArgIdx) {
993 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000994 SDValue Arg = Outs[realArgIdx].Val;
995 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +0000996
Bob Wilson1f595bb2009-04-17 19:07:39 +0000997 // Promote the value if needed.
998 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000999 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001000 case CCValAssign::Full: break;
1001 case CCValAssign::SExt:
1002 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1003 break;
1004 case CCValAssign::ZExt:
1005 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1006 break;
1007 case CCValAssign::AExt:
1008 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1009 break;
1010 case CCValAssign::BCvt:
1011 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1012 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001013 }
1014
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001015 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001016 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001017 if (VA.getLocVT() == MVT::v2f64) {
1018 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1019 DAG.getConstant(0, MVT::i32));
1020 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1021 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001022
Dan Gohman98ca4f22009-08-05 01:29:28 +00001023 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001024 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1025
1026 VA = ArgLocs[++i]; // skip ahead to next loc
1027 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001028 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001029 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1030 } else {
1031 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001032
Dan Gohman98ca4f22009-08-05 01:29:28 +00001033 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1034 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001035 }
1036 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001037 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001038 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001039 }
1040 } else if (VA.isRegLoc()) {
1041 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1042 } else {
1043 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001044
Dan Gohman98ca4f22009-08-05 01:29:28 +00001045 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1046 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001047 }
Evan Chenga8e29892007-01-19 07:51:42 +00001048 }
1049
1050 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001051 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001052 &MemOpChains[0], MemOpChains.size());
1053
1054 // Build a sequence of copy-to-reg nodes chained together with token chain
1055 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001056 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +00001057 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001058 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001059 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +00001060 InFlag = Chain.getValue(1);
1061 }
1062
Bill Wendling056292f2008-09-16 21:48:12 +00001063 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1064 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1065 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001066 bool isDirect = false;
1067 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001068 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001069 MachineFunction &MF = DAG.getMachineFunction();
1070 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001071
1072 if (EnableARMLongCalls) {
1073 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1074 && "long-calls with non-static relocation model!");
1075 // Handle a global address or an external symbol. If it's not one of
1076 // those, the target's already in a register, so we don't need to do
1077 // anything extra.
1078 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001079 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001080 // Create a constant pool entry for the callee address
1081 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1082 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1083 ARMPCLabelIndex,
1084 ARMCP::CPValue, 0);
1085 // Get the address of the callee into a register
1086 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1087 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1088 Callee = DAG.getLoad(getPointerTy(), dl,
1089 DAG.getEntryNode(), CPAddr,
1090 PseudoSourceValue::getConstantPool(), 0,
1091 false, false, 0);
1092 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1093 const char *Sym = S->getSymbol();
1094
1095 // Create a constant pool entry for the callee address
1096 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1097 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1098 Sym, ARMPCLabelIndex, 0);
1099 // Get the address of the callee into a register
1100 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1101 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1102 Callee = DAG.getLoad(getPointerTy(), dl,
1103 DAG.getEntryNode(), CPAddr,
1104 PseudoSourceValue::getConstantPool(), 0,
1105 false, false, 0);
1106 }
1107 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001108 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001109 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001110 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001111 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001112 getTargetMachine().getRelocationModel() != Reloc::Static;
1113 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001114 // ARM call to a local ARM function is predicable.
1115 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +00001116 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001117 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001118 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001119 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001120 ARMPCLabelIndex,
1121 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001122 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001123 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001124 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001125 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001126 PseudoSourceValue::getConstantPool(), 0,
1127 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001128 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001129 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001130 getPointerTy(), Callee, PICLabel);
Jim Grosbache7b52522010-04-14 22:28:31 +00001131 } else
Evan Chengc60e76d2007-01-30 20:37:08 +00001132 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001133 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001134 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001135 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001136 getTargetMachine().getRelocationModel() != Reloc::Static;
1137 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001138 // tBX takes a register source operand.
1139 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001140 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001141 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001142 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001143 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001144 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001145 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001146 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001147 DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001148 PseudoSourceValue::getConstantPool(), 0,
1149 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001150 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001151 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001152 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001153 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001154 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001155 }
1156
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001157 // FIXME: handle tail calls differently.
1158 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001159 if (Subtarget->isThumb()) {
1160 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001161 CallOpc = ARMISD::CALL_NOLINK;
1162 else
1163 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1164 } else {
1165 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001166 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1167 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001168 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001169 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001170 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001171 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001172 InFlag = Chain.getValue(1);
1173 }
1174
Dan Gohman475871a2008-07-27 21:46:04 +00001175 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001176 Ops.push_back(Chain);
1177 Ops.push_back(Callee);
1178
1179 // Add argument registers to the end of the list so that they are known live
1180 // into the call.
1181 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1182 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1183 RegsToPass[i].second.getValueType()));
1184
Gabor Greifba36cb52008-08-28 21:40:38 +00001185 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001186 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +00001187 // Returns a chain and a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00001188 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +00001189 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001190 InFlag = Chain.getValue(1);
1191
Chris Lattnere563bbc2008-10-11 22:08:30 +00001192 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1193 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001194 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001195 InFlag = Chain.getValue(1);
1196
Bob Wilson1f595bb2009-04-17 19:07:39 +00001197 // Handle result values, copying them out of physregs into vregs that we
1198 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001199 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1200 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001201}
1202
Dan Gohman98ca4f22009-08-05 01:29:28 +00001203SDValue
1204ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001205 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001206 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmand858e902010-04-17 15:26:15 +00001207 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001208
Bob Wilsondee46d72009-04-17 20:35:10 +00001209 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001210 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001211
Bob Wilsondee46d72009-04-17 20:35:10 +00001212 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001213 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1214 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001215
Dan Gohman98ca4f22009-08-05 01:29:28 +00001216 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001217 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1218 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001219
1220 // If this is the first return lowered for this function, add
1221 // the regs to the liveout set for the function.
1222 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1223 for (unsigned i = 0; i != RVLocs.size(); ++i)
1224 if (RVLocs[i].isRegLoc())
1225 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001226 }
1227
Bob Wilson1f595bb2009-04-17 19:07:39 +00001228 SDValue Flag;
1229
1230 // Copy the result values into the output registers.
1231 for (unsigned i = 0, realRVLocIdx = 0;
1232 i != RVLocs.size();
1233 ++i, ++realRVLocIdx) {
1234 CCValAssign &VA = RVLocs[i];
1235 assert(VA.isRegLoc() && "Can only return in registers!");
1236
Dan Gohman98ca4f22009-08-05 01:29:28 +00001237 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001238
1239 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001240 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001241 case CCValAssign::Full: break;
1242 case CCValAssign::BCvt:
1243 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1244 break;
1245 }
1246
Bob Wilson1f595bb2009-04-17 19:07:39 +00001247 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001248 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001249 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001250 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1251 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001252 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001253 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001254
1255 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1256 Flag = Chain.getValue(1);
1257 VA = RVLocs[++i]; // skip ahead to next loc
1258 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1259 HalfGPRs.getValue(1), Flag);
1260 Flag = Chain.getValue(1);
1261 VA = RVLocs[++i]; // skip ahead to next loc
1262
1263 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001264 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1265 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001266 }
1267 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1268 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001269 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001270 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001271 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001272 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001273 VA = RVLocs[++i]; // skip ahead to next loc
1274 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1275 Flag);
1276 } else
1277 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1278
Bob Wilsondee46d72009-04-17 20:35:10 +00001279 // Guarantee that all emitted copies are
1280 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001281 Flag = Chain.getValue(1);
1282 }
1283
1284 SDValue result;
1285 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001286 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001287 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001288 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001289
1290 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001291}
1292
Bob Wilsonb62d2572009-11-03 00:02:05 +00001293// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1294// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1295// one of the above mentioned nodes. It has to be wrapped because otherwise
1296// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1297// be used to form addressing mode. These wrapped nodes will be selected
1298// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001299static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001300 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001301 // FIXME there is no actual debug info here
1302 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001303 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001304 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001305 if (CP->isMachineConstantPoolEntry())
1306 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1307 CP->getAlignment());
1308 else
1309 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1310 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001311 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001312}
1313
Dan Gohmand858e902010-04-17 15:26:15 +00001314SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1315 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001316 MachineFunction &MF = DAG.getMachineFunction();
1317 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1318 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001319 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001320 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001321 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001322 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1323 SDValue CPAddr;
1324 if (RelocM == Reloc::Static) {
1325 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1326 } else {
1327 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001328 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001329 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1330 ARMCP::CPBlockAddress,
1331 PCAdj);
1332 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1333 }
1334 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1335 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001336 PseudoSourceValue::getConstantPool(), 0,
1337 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001338 if (RelocM == Reloc::Static)
1339 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001340 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001341 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001342}
1343
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001344// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001345SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001346ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001347 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001348 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001349 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001350 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001351 MachineFunction &MF = DAG.getMachineFunction();
1352 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1353 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001354 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001355 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001356 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001357 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001358 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001359 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
David Greene1b58cab2010-02-15 16:55:24 +00001360 PseudoSourceValue::getConstantPool(), 0,
1361 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001362 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001363
Evan Chenge7e0d622009-11-06 22:24:13 +00001364 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001365 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001366
1367 // call __tls_get_addr.
1368 ArgListTy Args;
1369 ArgListEntry Entry;
1370 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001371 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001372 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001373 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001374 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001375 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1376 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001377 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001378 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001379 return CallResult.first;
1380}
1381
1382// Lower ISD::GlobalTLSAddress using the "initial exec" or
1383// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001384SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001385ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001386 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001387 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001388 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001389 SDValue Offset;
1390 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001391 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001392 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001393 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001394
Chris Lattner4fb63d02009-07-15 04:12:33 +00001395 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001396 MachineFunction &MF = DAG.getMachineFunction();
1397 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1398 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1399 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001400 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1401 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001402 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001403 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001404 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001405 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001406 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001407 PseudoSourceValue::getConstantPool(), 0,
1408 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001409 Chain = Offset.getValue(1);
1410
Evan Chenge7e0d622009-11-06 22:24:13 +00001411 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001412 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001413
Evan Cheng9eda6892009-10-31 03:39:36 +00001414 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001415 PseudoSourceValue::getConstantPool(), 0,
1416 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001417 } else {
1418 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001419 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001420 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001421 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001422 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
David Greene1b58cab2010-02-15 16:55:24 +00001423 PseudoSourceValue::getConstantPool(), 0,
1424 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001425 }
1426
1427 // The address of the thread local variable is the add of the thread
1428 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001429 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001430}
1431
Dan Gohman475871a2008-07-27 21:46:04 +00001432SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001433ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001434 // TODO: implement the "local dynamic" model
1435 assert(Subtarget->isTargetELF() &&
1436 "TLS not implemented for non-ELF targets");
1437 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1438 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1439 // otherwise use the "Local Exec" TLS Model
1440 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1441 return LowerToTLSGeneralDynamicModel(GA, DAG);
1442 else
1443 return LowerToTLSExecModels(GA, DAG);
1444}
1445
Dan Gohman475871a2008-07-27 21:46:04 +00001446SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001447 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001448 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001449 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001450 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001451 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1452 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001453 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001454 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001455 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001456 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001457 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001458 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001459 CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001460 PseudoSourceValue::getConstantPool(), 0,
1461 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001462 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001463 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001464 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001465 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001466 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001467 PseudoSourceValue::getGOT(), 0,
1468 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001469 return Result;
1470 } else {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001471 // If we have T2 ops, we can materialize the address directly via movt/movw
1472 // pair. This is always cheaper.
1473 if (Subtarget->useMovt()) {
1474 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
1475 DAG.getTargetGlobalAddress(GV, PtrVT));
1476 } else {
1477 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1478 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1479 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001480 PseudoSourceValue::getConstantPool(), 0,
1481 false, false, 0);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001482 }
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001483 }
1484}
1485
Dan Gohman475871a2008-07-27 21:46:04 +00001486SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001487 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001488 MachineFunction &MF = DAG.getMachineFunction();
1489 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1490 unsigned ARMPCLabelIndex = 0;
Owen Andersone50ed302009-08-10 22:56:29 +00001491 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001492 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001493 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001494 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001495 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001496 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001497 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001498 else {
Evan Chenge7e0d622009-11-06 22:24:13 +00001499 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001500 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1501 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001502 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001503 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001504 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001505 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001506
Evan Cheng9eda6892009-10-31 03:39:36 +00001507 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001508 PseudoSourceValue::getConstantPool(), 0,
1509 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001510 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001511
1512 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001513 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001514 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001515 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001516
Evan Cheng63476a82009-09-03 07:04:02 +00001517 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Evan Cheng9eda6892009-10-31 03:39:36 +00001518 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
David Greene1b58cab2010-02-15 16:55:24 +00001519 PseudoSourceValue::getGOT(), 0,
1520 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001521
1522 return Result;
1523}
1524
Dan Gohman475871a2008-07-27 21:46:04 +00001525SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001526 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001527 assert(Subtarget->isTargetELF() &&
1528 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00001529 MachineFunction &MF = DAG.getMachineFunction();
1530 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1531 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Andersone50ed302009-08-10 22:56:29 +00001532 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001533 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001534 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001535 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1536 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001537 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001538 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001539 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001540 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001541 PseudoSourceValue::getConstantPool(), 0,
1542 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001543 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001544 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001545}
1546
Jim Grosbach0e0da732009-05-12 23:59:14 +00001547SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00001548ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001549 const ARMSubtarget *Subtarget)
1550 const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001551 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001552 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001553 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001554 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001555 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001556 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001557 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1558 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001559 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001560 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00001561 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1562 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001563 EVT PtrVT = getPointerTy();
1564 DebugLoc dl = Op.getDebugLoc();
1565 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1566 SDValue CPAddr;
1567 unsigned PCAdj = (RelocM != Reloc::PIC_)
1568 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001569 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001570 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1571 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001572 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001573 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001574 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00001575 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
David Greene1b58cab2010-02-15 16:55:24 +00001576 PseudoSourceValue::getConstantPool(), 0,
1577 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001578 SDValue Chain = Result.getValue(1);
1579
1580 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001581 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001582 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1583 }
1584 return Result;
1585 }
Jim Grosbachf9570122009-05-14 00:46:35 +00001586 case Intrinsic::eh_sjlj_setjmp:
Jim Grosbacha87ded22010-02-08 23:22:00 +00001587 SDValue Val = Subtarget->isThumb() ?
1588 DAG.getCopyFromReg(DAG.getEntryNode(), dl, ARM::SP, MVT::i32) :
1589 DAG.getConstant(0, MVT::i32);
1590 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1),
1591 Val);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001592 }
1593}
1594
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001595static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
1596 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00001597 DebugLoc dl = Op.getDebugLoc();
1598 SDValue Op5 = Op.getOperand(5);
1599 SDValue Res;
1600 unsigned isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue();
1601 if (isDeviceBarrier) {
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001602 if (Subtarget->hasV7Ops())
1603 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0));
1604 else
1605 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0),
1606 DAG.getConstant(0, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00001607 } else {
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00001608 if (Subtarget->hasV7Ops())
1609 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
1610 else
1611 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
1612 DAG.getConstant(0, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00001613 }
1614 return Res;
1615}
1616
Dan Gohman1e93df62010-04-17 14:41:14 +00001617static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
1618 MachineFunction &MF = DAG.getMachineFunction();
1619 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
1620
Evan Chenga8e29892007-01-19 07:51:42 +00001621 // vastart just stores the address of the VarArgsFrameIndex slot into the
1622 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001623 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001624 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00001625 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001626 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
David Greene1b58cab2010-02-15 16:55:24 +00001627 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
1628 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001629}
1630
Dan Gohman475871a2008-07-27 21:46:04 +00001631SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001632ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1633 SelectionDAG &DAG) const {
Evan Cheng86198642009-08-07 00:34:42 +00001634 SDNode *Node = Op.getNode();
1635 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001636 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001637 SDValue Chain = Op.getOperand(0);
1638 SDValue Size = Op.getOperand(1);
1639 SDValue Align = Op.getOperand(2);
1640
1641 // Chain the dynamic stack allocation so that it doesn't modify the stack
1642 // pointer when other instructions are using the stack.
1643 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1644
1645 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1646 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1647 if (AlignVal > StackAlign)
1648 // Do this now since selection pass cannot introduce new target
1649 // independent node.
1650 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1651
1652 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1653 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1654 // do even more horrible hack later.
1655 MachineFunction &MF = DAG.getMachineFunction();
1656 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1657 if (AFI->isThumb1OnlyFunction()) {
1658 bool Negate = true;
1659 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1660 if (C) {
1661 uint32_t Val = C->getZExtValue();
1662 if (Val <= 508 && ((Val & 3) == 0))
1663 Negate = false;
1664 }
1665 if (Negate)
1666 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1667 }
1668
Owen Anderson825b72b2009-08-11 20:47:22 +00001669 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001670 SDValue Ops1[] = { Chain, Size, Align };
1671 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1672 Chain = Res.getValue(1);
1673 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1674 DAG.getIntPtrConstant(0, true), SDValue());
1675 SDValue Ops2[] = { Res, Chain };
1676 return DAG.getMergeValues(Ops2, 2, dl);
1677}
1678
1679SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001680ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1681 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001682 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001683 MachineFunction &MF = DAG.getMachineFunction();
1684 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1685
1686 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001687 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001688 RC = ARM::tGPRRegisterClass;
1689 else
1690 RC = ARM::GPRRegisterClass;
1691
1692 // Transform the arguments stored in physical registers into virtual ones.
1693 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001694 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001695
1696 SDValue ArgValue2;
1697 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001698 MachineFrameInfo *MFI = MF.getFrameInfo();
Bob Wilson6a234f02010-04-13 22:03:22 +00001699 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true, false);
Bob Wilson5bafff32009-06-22 23:27:02 +00001700
1701 // Create load node to retrieve arguments from the stack.
1702 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001703 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00001704 PseudoSourceValue::getFixedStack(FI), 0,
1705 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001706 } else {
1707 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001708 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001709 }
1710
Jim Grosbache5165492009-11-09 00:11:35 +00001711 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001712}
1713
1714SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001715ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001716 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001717 const SmallVectorImpl<ISD::InputArg>
1718 &Ins,
1719 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001720 SmallVectorImpl<SDValue> &InVals)
1721 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001722
Bob Wilson1f595bb2009-04-17 19:07:39 +00001723 MachineFunction &MF = DAG.getMachineFunction();
1724 MachineFrameInfo *MFI = MF.getFrameInfo();
1725
Bob Wilson1f595bb2009-04-17 19:07:39 +00001726 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1727
1728 // Assign locations to all of the incoming arguments.
1729 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001730 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1731 *DAG.getContext());
1732 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001733 CCAssignFnForNode(CallConv, /* Return*/ false,
1734 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001735
1736 SmallVector<SDValue, 16> ArgValues;
1737
1738 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1739 CCValAssign &VA = ArgLocs[i];
1740
Bob Wilsondee46d72009-04-17 20:35:10 +00001741 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001742 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001743 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00001744
Bob Wilson5bafff32009-06-22 23:27:02 +00001745 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001746 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001747 // f64 and vector types are split up into multiple registers or
1748 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00001749 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001750 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001751 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00001752 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00001753 SDValue ArgValue2;
1754 if (VA.isMemLoc()) {
1755 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(),
1756 true, false);
1757 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1758 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
1759 PseudoSourceValue::getFixedStack(FI), 0,
1760 false, false, 0);
1761 } else {
1762 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
1763 Chain, DAG, dl);
1764 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001765 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1766 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001767 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00001768 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001769 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1770 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001771 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001772
Bob Wilson5bafff32009-06-22 23:27:02 +00001773 } else {
1774 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001775
Owen Anderson825b72b2009-08-11 20:47:22 +00001776 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00001777 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001778 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00001779 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001780 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001781 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001782 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001783 RC = (AFI->isThumb1OnlyFunction() ?
1784 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00001785 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001786 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00001787
1788 // Transform the arguments in physical registers into virtual ones.
1789 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001790 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001791 }
1792
1793 // If this is an 8 or 16-bit value, it is really passed promoted
1794 // to 32 bits. Insert an assert[sz]ext to capture this, then
1795 // truncate to the right size.
1796 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001797 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001798 case CCValAssign::Full: break;
1799 case CCValAssign::BCvt:
1800 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1801 break;
1802 case CCValAssign::SExt:
1803 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1804 DAG.getValueType(VA.getValVT()));
1805 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1806 break;
1807 case CCValAssign::ZExt:
1808 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1809 DAG.getValueType(VA.getValVT()));
1810 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1811 break;
1812 }
1813
Dan Gohman98ca4f22009-08-05 01:29:28 +00001814 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001815
1816 } else { // VA.isRegLoc()
1817
1818 // sanity check
1819 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00001820 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001821
1822 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001823 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
1824 true, false);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001825
Bob Wilsondee46d72009-04-17 20:35:10 +00001826 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001827 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001828 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
David Greene1b58cab2010-02-15 16:55:24 +00001829 PseudoSourceValue::getFixedStack(FI), 0,
1830 false, false, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001831 }
1832 }
1833
1834 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001835 if (isVarArg) {
1836 static const unsigned GPRArgRegs[] = {
1837 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1838 };
1839
Bob Wilsondee46d72009-04-17 20:35:10 +00001840 unsigned NumGPRs = CCInfo.getFirstUnallocated
1841 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001842
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001843 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1844 unsigned VARegSize = (4 - NumGPRs) * 4;
1845 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00001846 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001847 if (VARegSaveSize) {
1848 // If this function is vararg, store any remaining integer argument regs
1849 // to their spots on the stack so that they may be loaded by deferencing
1850 // the result of va_next.
1851 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00001852 AFI->setVarArgsFrameIndex(
1853 MFI->CreateFixedObject(VARegSaveSize,
1854 ArgOffset + VARegSaveSize - VARegSize,
1855 true, false));
1856 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
1857 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001858
Dan Gohman475871a2008-07-27 21:46:04 +00001859 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001860 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001861 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001862 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001863 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001864 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001865 RC = ARM::GPRRegisterClass;
1866
Bob Wilson998e1252009-04-20 18:36:57 +00001867 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001868 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00001869 SDValue Store =
1870 DAG.getStore(Val.getValue(1), dl, Val, FIN,
1871 PseudoSourceValue::getFixedStack(AFI->getVarArgsFrameIndex()), 0,
1872 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001873 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001874 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001875 DAG.getConstant(4, getPointerTy()));
1876 }
1877 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001878 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001879 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001880 } else
1881 // This will point to the next argument passed via stack.
Dan Gohman1e93df62010-04-17 14:41:14 +00001882 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset,
1883 true, false));
Evan Chenga8e29892007-01-19 07:51:42 +00001884 }
1885
Dan Gohman98ca4f22009-08-05 01:29:28 +00001886 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00001887}
1888
1889/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001890static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001891 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001892 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001893 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001894 // Maybe this has already been legalized into the constant pool?
1895 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001896 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001897 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00001898 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001899 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001900 }
1901 }
1902 return false;
1903}
1904
Evan Chenga8e29892007-01-19 07:51:42 +00001905/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1906/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00001907SDValue
1908ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Dan Gohmand858e902010-04-17 15:26:15 +00001909 SDValue &ARMCC, SelectionDAG &DAG,
1910 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00001911 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001912 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00001913 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001914 // Constant does not fit, try adjusting it by one?
1915 switch (CC) {
1916 default: break;
1917 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001918 case ISD::SETGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00001919 if (isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001920 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001921 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001922 }
1923 break;
1924 case ISD::SETULT:
1925 case ISD::SETUGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00001926 if (C > 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001927 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001928 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001929 }
1930 break;
1931 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001932 case ISD::SETGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00001933 if (isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001934 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001935 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001936 }
1937 break;
1938 case ISD::SETULE:
1939 case ISD::SETUGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00001940 if (C < 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001941 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001942 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001943 }
1944 break;
1945 }
1946 }
1947 }
1948
1949 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001950 ARMISD::NodeType CompareType;
1951 switch (CondCode) {
1952 default:
1953 CompareType = ARMISD::CMP;
1954 break;
1955 case ARMCC::EQ:
1956 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00001957 // Uses only Z Flag
1958 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001959 break;
1960 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001961 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1962 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001963}
1964
1965/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001966static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001967 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001968 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001969 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00001970 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001971 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001972 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1973 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001974}
1975
Dan Gohmand858e902010-04-17 15:26:15 +00001976SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001977 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001978 SDValue LHS = Op.getOperand(0);
1979 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001980 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001981 SDValue TrueVal = Op.getOperand(2);
1982 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001983 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001984
Owen Anderson825b72b2009-08-11 20:47:22 +00001985 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001986 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001987 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00001988 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Dale Johannesende064702009-02-06 21:50:26 +00001989 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001990 }
1991
1992 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001993 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00001994
Owen Anderson825b72b2009-08-11 20:47:22 +00001995 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1996 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001997 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1998 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001999 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002000 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002001 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002002 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00002003 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002004 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00002005 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002006 }
2007 return Result;
2008}
2009
Dan Gohmand858e902010-04-17 15:26:15 +00002010SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002011 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002012 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002013 SDValue LHS = Op.getOperand(2);
2014 SDValue RHS = Op.getOperand(3);
2015 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002016 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002017
Owen Anderson825b72b2009-08-11 20:47:22 +00002018 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00002019 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002020 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00002021 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002022 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00002023 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002024 }
2025
Owen Anderson825b72b2009-08-11 20:47:22 +00002026 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00002027 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002028 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002029
Dale Johannesende064702009-02-06 21:50:26 +00002030 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002031 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
2032 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2033 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002034 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002035 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002036 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002037 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00002038 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002039 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002040 }
2041 return Res;
2042}
2043
Dan Gohmand858e902010-04-17 15:26:15 +00002044SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002045 SDValue Chain = Op.getOperand(0);
2046 SDValue Table = Op.getOperand(1);
2047 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002048 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002049
Owen Andersone50ed302009-08-10 22:56:29 +00002050 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002051 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2052 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002053 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002054 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002055 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002056 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2057 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002058 if (Subtarget->isThumb2()) {
2059 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2060 // which does another jump to the destination. This also makes it easier
2061 // to translate it to TBB / TBH later.
2062 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002063 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002064 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002065 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002066 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002067 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002068 PseudoSourceValue::getJumpTable(), 0,
2069 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002070 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002071 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002072 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002073 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002074 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
David Greene1b58cab2010-02-15 16:55:24 +00002075 PseudoSourceValue::getJumpTable(), 0, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002076 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002077 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002078 }
Evan Chenga8e29892007-01-19 07:51:42 +00002079}
2080
Bob Wilson76a312b2010-03-19 22:51:32 +00002081static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2082 DebugLoc dl = Op.getDebugLoc();
2083 unsigned Opc;
2084
2085 switch (Op.getOpcode()) {
2086 default:
2087 assert(0 && "Invalid opcode!");
2088 case ISD::FP_TO_SINT:
2089 Opc = ARMISD::FTOSI;
2090 break;
2091 case ISD::FP_TO_UINT:
2092 Opc = ARMISD::FTOUI;
2093 break;
2094 }
2095 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
2096 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
2097}
2098
2099static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2100 EVT VT = Op.getValueType();
2101 DebugLoc dl = Op.getDebugLoc();
2102 unsigned Opc;
2103
2104 switch (Op.getOpcode()) {
2105 default:
2106 assert(0 && "Invalid opcode!");
2107 case ISD::SINT_TO_FP:
2108 Opc = ARMISD::SITOF;
2109 break;
2110 case ISD::UINT_TO_FP:
2111 Opc = ARMISD::UITOF;
2112 break;
2113 }
2114
2115 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
2116 return DAG.getNode(Opc, dl, VT, Op);
2117}
2118
Dan Gohman475871a2008-07-27 21:46:04 +00002119static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002120 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002121 SDValue Tmp0 = Op.getOperand(0);
2122 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002123 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002124 EVT VT = Op.getValueType();
2125 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00002126 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
2127 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002128 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
2129 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002130 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002131}
2132
Dan Gohmand858e902010-04-17 15:26:15 +00002133SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002134 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2135 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00002136 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002137 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2138 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002139 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002140 ? ARM::R7 : ARM::R11;
2141 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2142 while (Depth--)
David Greene1b58cab2010-02-15 16:55:24 +00002143 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
2144 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002145 return FrameAddr;
2146}
2147
Bob Wilson9f3f0612010-04-17 05:30:19 +00002148/// ExpandBIT_CONVERT - If the target supports VFP, this function is called to
2149/// expand a bit convert where either the source or destination type is i64 to
2150/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2151/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2152/// vectors), since the legalizer won't know what to do with that.
Duncan Sands1607f052008-12-01 11:39:25 +00002153static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002154 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2155 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002156 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002157
Bob Wilson9f3f0612010-04-17 05:30:19 +00002158 // This function is only supposed to be called for i64 types, either as the
2159 // source or destination of the bit convert.
2160 EVT SrcVT = Op.getValueType();
2161 EVT DstVT = N->getValueType(0);
2162 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
2163 "ExpandBIT_CONVERT called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002164
Bob Wilson9f3f0612010-04-17 05:30:19 +00002165 // Turn i64->f64 into VMOVDRR.
2166 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002167 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2168 DAG.getConstant(0, MVT::i32));
2169 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2170 DAG.getConstant(1, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00002171 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002172 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002173
Jim Grosbache5165492009-11-09 00:11:35 +00002174 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002175 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2176 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2177 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2178 // Merge the pieces into a single i64 value.
2179 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2180 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002181
Bob Wilson9f3f0612010-04-17 05:30:19 +00002182 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002183}
2184
Bob Wilson5bafff32009-06-22 23:27:02 +00002185/// getZeroVector - Returns a vector of specified type with all zero elements.
2186///
Owen Andersone50ed302009-08-10 22:56:29 +00002187static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002188 assert(VT.isVector() && "Expected a vector type");
2189
2190 // Zero vectors are used to represent vector negation and in those cases
2191 // will be implemented with the NEON VNEG instruction. However, VNEG does
2192 // not support i64 elements, so sometimes the zero vectors will need to be
2193 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002194 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilson5bafff32009-06-22 23:27:02 +00002195 // to their dest type. This ensures they get CSE'd.
2196 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002197 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2198 SmallVector<SDValue, 8> Ops;
2199 MVT TVT;
2200
2201 if (VT.getSizeInBits() == 64) {
2202 Ops.assign(8, Cst); TVT = MVT::v8i8;
2203 } else {
2204 Ops.assign(16, Cst); TVT = MVT::v16i8;
2205 }
2206 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002207
2208 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2209}
2210
2211/// getOnesVector - Returns a vector of specified type with all bits set.
2212///
Owen Andersone50ed302009-08-10 22:56:29 +00002213static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002214 assert(VT.isVector() && "Expected a vector type");
2215
Bob Wilson929ffa22009-10-30 20:13:25 +00002216 // Always build ones vectors as <16 x i8> or <8 x i8> bitcasted to their
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002217 // dest type. This ensures they get CSE'd.
Bob Wilson5bafff32009-06-22 23:27:02 +00002218 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002219 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2220 SmallVector<SDValue, 8> Ops;
2221 MVT TVT;
2222
2223 if (VT.getSizeInBits() == 64) {
2224 Ops.assign(8, Cst); TVT = MVT::v8i8;
2225 } else {
2226 Ops.assign(16, Cst); TVT = MVT::v16i8;
2227 }
2228 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002229
2230 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2231}
2232
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002233/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2234/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002235SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2236 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002237 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2238 EVT VT = Op.getValueType();
2239 unsigned VTBits = VT.getSizeInBits();
2240 DebugLoc dl = Op.getDebugLoc();
2241 SDValue ShOpLo = Op.getOperand(0);
2242 SDValue ShOpHi = Op.getOperand(1);
2243 SDValue ShAmt = Op.getOperand(2);
2244 SDValue ARMCC;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002245 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002246
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002247 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2248
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002249 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2250 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2251 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2252 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2253 DAG.getConstant(VTBits, MVT::i32));
2254 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2255 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002256 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002257
2258 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2259 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002260 ARMCC, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002261 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002262 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC,
2263 CCR, Cmp);
2264
2265 SDValue Ops[2] = { Lo, Hi };
2266 return DAG.getMergeValues(Ops, 2, dl);
2267}
2268
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002269/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2270/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002271SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2272 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002273 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2274 EVT VT = Op.getValueType();
2275 unsigned VTBits = VT.getSizeInBits();
2276 DebugLoc dl = Op.getDebugLoc();
2277 SDValue ShOpLo = Op.getOperand(0);
2278 SDValue ShOpHi = Op.getOperand(1);
2279 SDValue ShAmt = Op.getOperand(2);
2280 SDValue ARMCC;
2281
2282 assert(Op.getOpcode() == ISD::SHL_PARTS);
2283 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2284 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2285 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2286 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2287 DAG.getConstant(VTBits, MVT::i32));
2288 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2289 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2290
2291 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2292 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2293 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002294 ARMCC, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002295 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
2296 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMCC,
2297 CCR, Cmp);
2298
2299 SDValue Ops[2] = { Lo, Hi };
2300 return DAG.getMergeValues(Ops, 2, dl);
2301}
2302
Jim Grosbach3482c802010-01-18 19:58:49 +00002303static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
2304 const ARMSubtarget *ST) {
2305 EVT VT = N->getValueType(0);
2306 DebugLoc dl = N->getDebugLoc();
2307
2308 if (!ST->hasV6T2Ops())
2309 return SDValue();
2310
2311 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
2312 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
2313}
2314
Bob Wilson5bafff32009-06-22 23:27:02 +00002315static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2316 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002317 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002318 DebugLoc dl = N->getDebugLoc();
2319
2320 // Lower vector shifts on NEON to use VSHL.
2321 if (VT.isVector()) {
2322 assert(ST->hasNEON() && "unexpected vector shift");
2323
2324 // Left shifts translate directly to the vshiftu intrinsic.
2325 if (N->getOpcode() == ISD::SHL)
2326 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002327 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002328 N->getOperand(0), N->getOperand(1));
2329
2330 assert((N->getOpcode() == ISD::SRA ||
2331 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2332
2333 // NEON uses the same intrinsics for both left and right shifts. For
2334 // right shifts, the shift amounts are negative, so negate the vector of
2335 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002336 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002337 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2338 getZeroVector(ShiftVT, DAG, dl),
2339 N->getOperand(1));
2340 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2341 Intrinsic::arm_neon_vshifts :
2342 Intrinsic::arm_neon_vshiftu);
2343 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002344 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002345 N->getOperand(0), NegatedCount);
2346 }
2347
Eli Friedmance392eb2009-08-22 03:13:10 +00002348 // We can get here for a node like i32 = ISD::SHL i32, i64
2349 if (VT != MVT::i64)
2350 return SDValue();
2351
2352 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002353 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002354
Chris Lattner27a6c732007-11-24 07:07:01 +00002355 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2356 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002357 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002358 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002359
Chris Lattner27a6c732007-11-24 07:07:01 +00002360 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002361 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002362
Chris Lattner27a6c732007-11-24 07:07:01 +00002363 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002364 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2365 DAG.getConstant(0, MVT::i32));
2366 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2367 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002368
Chris Lattner27a6c732007-11-24 07:07:01 +00002369 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2370 // captures the result into a carry flag.
2371 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002372 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002373
Chris Lattner27a6c732007-11-24 07:07:01 +00002374 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002375 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002376
Chris Lattner27a6c732007-11-24 07:07:01 +00002377 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002378 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002379}
2380
Bob Wilson5bafff32009-06-22 23:27:02 +00002381static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2382 SDValue TmpOp0, TmpOp1;
2383 bool Invert = false;
2384 bool Swap = false;
2385 unsigned Opc = 0;
2386
2387 SDValue Op0 = Op.getOperand(0);
2388 SDValue Op1 = Op.getOperand(1);
2389 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002390 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002391 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2392 DebugLoc dl = Op.getDebugLoc();
2393
2394 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2395 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002396 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002397 case ISD::SETUNE:
2398 case ISD::SETNE: Invert = true; // Fallthrough
2399 case ISD::SETOEQ:
2400 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2401 case ISD::SETOLT:
2402 case ISD::SETLT: Swap = true; // Fallthrough
2403 case ISD::SETOGT:
2404 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2405 case ISD::SETOLE:
2406 case ISD::SETLE: Swap = true; // Fallthrough
2407 case ISD::SETOGE:
2408 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2409 case ISD::SETUGE: Swap = true; // Fallthrough
2410 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2411 case ISD::SETUGT: Swap = true; // Fallthrough
2412 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2413 case ISD::SETUEQ: Invert = true; // Fallthrough
2414 case ISD::SETONE:
2415 // Expand this to (OLT | OGT).
2416 TmpOp0 = Op0;
2417 TmpOp1 = Op1;
2418 Opc = ISD::OR;
2419 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2420 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2421 break;
2422 case ISD::SETUO: Invert = true; // Fallthrough
2423 case ISD::SETO:
2424 // Expand this to (OLT | OGE).
2425 TmpOp0 = Op0;
2426 TmpOp1 = Op1;
2427 Opc = ISD::OR;
2428 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2429 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2430 break;
2431 }
2432 } else {
2433 // Integer comparisons.
2434 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002435 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002436 case ISD::SETNE: Invert = true;
2437 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2438 case ISD::SETLT: Swap = true;
2439 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2440 case ISD::SETLE: Swap = true;
2441 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2442 case ISD::SETULT: Swap = true;
2443 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2444 case ISD::SETULE: Swap = true;
2445 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2446 }
2447
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002448 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002449 if (Opc == ARMISD::VCEQ) {
2450
2451 SDValue AndOp;
2452 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2453 AndOp = Op0;
2454 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2455 AndOp = Op1;
2456
2457 // Ignore bitconvert.
2458 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2459 AndOp = AndOp.getOperand(0);
2460
2461 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2462 Opc = ARMISD::VTST;
2463 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2464 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2465 Invert = !Invert;
2466 }
2467 }
2468 }
2469
2470 if (Swap)
2471 std::swap(Op0, Op1);
2472
2473 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2474
2475 if (Invert)
2476 Result = DAG.getNOT(dl, Result, VT);
2477
2478 return Result;
2479}
2480
2481/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2482/// VMOV instruction, and if so, return the constant being splatted.
2483static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2484 unsigned SplatBitSize, SelectionDAG &DAG) {
2485 switch (SplatBitSize) {
2486 case 8:
2487 // Any 1-byte value is OK.
2488 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson825b72b2009-08-11 20:47:22 +00002489 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilson5bafff32009-06-22 23:27:02 +00002490
2491 case 16:
2492 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2493 if ((SplatBits & ~0xff) == 0 ||
2494 (SplatBits & ~0xff00) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002495 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilson5bafff32009-06-22 23:27:02 +00002496 break;
2497
2498 case 32:
2499 // NEON's 32-bit VMOV supports splat values where:
2500 // * only one byte is nonzero, or
2501 // * the least significant byte is 0xff and the second byte is nonzero, or
2502 // * the least significant 2 bytes are 0xff and the third is nonzero.
2503 if ((SplatBits & ~0xff) == 0 ||
2504 (SplatBits & ~0xff00) == 0 ||
2505 (SplatBits & ~0xff0000) == 0 ||
2506 (SplatBits & ~0xff000000) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002507 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002508
2509 if ((SplatBits & ~0xffff) == 0 &&
2510 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002511 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002512
2513 if ((SplatBits & ~0xffffff) == 0 &&
2514 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002515 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002516
2517 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2518 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2519 // VMOV.I32. A (very) minor optimization would be to replicate the value
2520 // and fall through here to test for a valid 64-bit splat. But, then the
2521 // caller would also need to check and handle the change in size.
2522 break;
2523
2524 case 64: {
2525 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2526 uint64_t BitMask = 0xff;
2527 uint64_t Val = 0;
2528 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2529 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2530 Val |= BitMask;
2531 else if ((SplatBits & BitMask) != 0)
2532 return SDValue();
2533 BitMask <<= 8;
2534 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002535 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilson5bafff32009-06-22 23:27:02 +00002536 }
2537
2538 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002539 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilson5bafff32009-06-22 23:27:02 +00002540 break;
2541 }
2542
2543 return SDValue();
2544}
2545
2546/// getVMOVImm - If this is a build_vector of constants which can be
2547/// formed by using a VMOV instruction of the specified element size,
2548/// return the constant being splatted. The ByteSize field indicates the
2549/// number of bytes of each element [1248].
2550SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2551 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2552 APInt SplatBits, SplatUndef;
2553 unsigned SplatBitSize;
2554 bool HasAnyUndefs;
2555 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2556 HasAnyUndefs, ByteSize * 8))
2557 return SDValue();
2558
2559 if (SplatBitSize > ByteSize * 8)
2560 return SDValue();
2561
2562 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2563 SplatBitSize, DAG);
2564}
2565
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002566static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2567 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002568 unsigned NumElts = VT.getVectorNumElements();
2569 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002570 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002571
2572 // If this is a VEXT shuffle, the immediate value is the index of the first
2573 // element. The other shuffle indices must be the successive elements after
2574 // the first one.
2575 unsigned ExpectedElt = Imm;
2576 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002577 // Increment the expected index. If it wraps around, it may still be
2578 // a VEXT but the source vectors must be swapped.
2579 ExpectedElt += 1;
2580 if (ExpectedElt == NumElts * 2) {
2581 ExpectedElt = 0;
2582 ReverseVEXT = true;
2583 }
2584
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002585 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002586 return false;
2587 }
2588
2589 // Adjust the index value if the source operands will be swapped.
2590 if (ReverseVEXT)
2591 Imm -= NumElts;
2592
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002593 return true;
2594}
2595
Bob Wilson8bb9e482009-07-26 00:39:34 +00002596/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2597/// instruction with the specified blocksize. (The order of the elements
2598/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002599static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2600 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002601 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2602 "Only possible block sizes for VREV are: 16, 32, 64");
2603
Bob Wilson8bb9e482009-07-26 00:39:34 +00002604 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00002605 if (EltSz == 64)
2606 return false;
2607
2608 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002609 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002610
2611 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2612 return false;
2613
2614 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002615 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002616 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2617 return false;
2618 }
2619
2620 return true;
2621}
2622
Bob Wilsonc692cb72009-08-21 20:54:19 +00002623static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2624 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002625 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2626 if (EltSz == 64)
2627 return false;
2628
Bob Wilsonc692cb72009-08-21 20:54:19 +00002629 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 + NumElts + WhichResult)
2634 return false;
2635 }
2636 return true;
2637}
2638
Bob Wilson324f4f12009-12-03 06:40:55 +00002639/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
2640/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2641/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
2642static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2643 unsigned &WhichResult) {
2644 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2645 if (EltSz == 64)
2646 return false;
2647
2648 unsigned NumElts = VT.getVectorNumElements();
2649 WhichResult = (M[0] == 0 ? 0 : 1);
2650 for (unsigned i = 0; i < NumElts; i += 2) {
2651 if ((unsigned) M[i] != i + WhichResult ||
2652 (unsigned) M[i+1] != i + WhichResult)
2653 return false;
2654 }
2655 return true;
2656}
2657
Bob Wilsonc692cb72009-08-21 20:54:19 +00002658static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2659 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002660 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2661 if (EltSz == 64)
2662 return false;
2663
Bob Wilsonc692cb72009-08-21 20:54:19 +00002664 unsigned NumElts = VT.getVectorNumElements();
2665 WhichResult = (M[0] == 0 ? 0 : 1);
2666 for (unsigned i = 0; i != NumElts; ++i) {
2667 if ((unsigned) M[i] != 2 * i + WhichResult)
2668 return false;
2669 }
2670
2671 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002672 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002673 return false;
2674
2675 return true;
2676}
2677
Bob Wilson324f4f12009-12-03 06:40:55 +00002678/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
2679/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2680/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
2681static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2682 unsigned &WhichResult) {
2683 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2684 if (EltSz == 64)
2685 return false;
2686
2687 unsigned Half = VT.getVectorNumElements() / 2;
2688 WhichResult = (M[0] == 0 ? 0 : 1);
2689 for (unsigned j = 0; j != 2; ++j) {
2690 unsigned Idx = WhichResult;
2691 for (unsigned i = 0; i != Half; ++i) {
2692 if ((unsigned) M[i + j * Half] != Idx)
2693 return false;
2694 Idx += 2;
2695 }
2696 }
2697
2698 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2699 if (VT.is64BitVector() && EltSz == 32)
2700 return false;
2701
2702 return true;
2703}
2704
Bob Wilsonc692cb72009-08-21 20:54:19 +00002705static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2706 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002707 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2708 if (EltSz == 64)
2709 return false;
2710
Bob Wilsonc692cb72009-08-21 20:54:19 +00002711 unsigned NumElts = VT.getVectorNumElements();
2712 WhichResult = (M[0] == 0 ? 0 : 1);
2713 unsigned Idx = WhichResult * NumElts / 2;
2714 for (unsigned i = 0; i != NumElts; i += 2) {
2715 if ((unsigned) M[i] != Idx ||
2716 (unsigned) M[i+1] != Idx + NumElts)
2717 return false;
2718 Idx += 1;
2719 }
2720
2721 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002722 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002723 return false;
2724
2725 return true;
2726}
2727
Bob Wilson324f4f12009-12-03 06:40:55 +00002728/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
2729/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2730/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
2731static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2732 unsigned &WhichResult) {
2733 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2734 if (EltSz == 64)
2735 return false;
2736
2737 unsigned NumElts = VT.getVectorNumElements();
2738 WhichResult = (M[0] == 0 ? 0 : 1);
2739 unsigned Idx = WhichResult * NumElts / 2;
2740 for (unsigned i = 0; i != NumElts; i += 2) {
2741 if ((unsigned) M[i] != Idx ||
2742 (unsigned) M[i+1] != Idx)
2743 return false;
2744 Idx += 1;
2745 }
2746
2747 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2748 if (VT.is64BitVector() && EltSz == 32)
2749 return false;
2750
2751 return true;
2752}
2753
2754
Owen Andersone50ed302009-08-10 22:56:29 +00002755static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002756 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00002757 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002758 if (ConstVal->isNullValue())
2759 return getZeroVector(VT, DAG, dl);
2760 if (ConstVal->isAllOnesValue())
2761 return getOnesVector(VT, DAG, dl);
2762
Owen Andersone50ed302009-08-10 22:56:29 +00002763 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00002764 if (VT.is64BitVector()) {
2765 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002766 case 8: CanonicalVT = MVT::v8i8; break;
2767 case 16: CanonicalVT = MVT::v4i16; break;
2768 case 32: CanonicalVT = MVT::v2i32; break;
2769 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002770 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002771 }
2772 } else {
2773 assert(VT.is128BitVector() && "unknown splat vector size");
2774 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002775 case 8: CanonicalVT = MVT::v16i8; break;
2776 case 16: CanonicalVT = MVT::v8i16; break;
2777 case 32: CanonicalVT = MVT::v4i32; break;
2778 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002779 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002780 }
2781 }
2782
2783 // Build a canonical splat for this value.
2784 SmallVector<SDValue, 8> Ops;
2785 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2786 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2787 Ops.size());
2788 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2789}
2790
2791// If this is a case we can't handle, return null and let the default
2792// expansion code take care of it.
2793static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00002794 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002795 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002796 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002797
2798 APInt SplatBits, SplatUndef;
2799 unsigned SplatBitSize;
2800 bool HasAnyUndefs;
2801 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00002802 if (SplatBitSize <= 64) {
2803 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2804 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2805 if (Val.getNode())
2806 return BuildSplat(Val, VT, DAG, dl);
2807 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00002808 }
2809
2810 // If there are only 2 elements in a 128-bit vector, insert them into an
2811 // undef vector. This handles the common case for 128-bit vector argument
2812 // passing, where the insertions should be translated to subreg accesses
2813 // with no real instructions.
2814 if (VT.is128BitVector() && Op.getNumOperands() == 2) {
2815 SDValue Val = DAG.getUNDEF(VT);
2816 SDValue Op0 = Op.getOperand(0);
2817 SDValue Op1 = Op.getOperand(1);
2818 if (Op0.getOpcode() != ISD::UNDEF)
2819 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op0,
2820 DAG.getIntPtrConstant(0));
2821 if (Op1.getOpcode() != ISD::UNDEF)
2822 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op1,
2823 DAG.getIntPtrConstant(1));
2824 return Val;
Bob Wilson5bafff32009-06-22 23:27:02 +00002825 }
2826
2827 return SDValue();
2828}
2829
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002830/// isShuffleMaskLegal - Targets can use this to indicate that they only
2831/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2832/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2833/// are assumed to be legal.
2834bool
2835ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2836 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002837 if (VT.getVectorNumElements() == 4 &&
2838 (VT.is128BitVector() || VT.is64BitVector())) {
2839 unsigned PFIndexes[4];
2840 for (unsigned i = 0; i != 4; ++i) {
2841 if (M[i] < 0)
2842 PFIndexes[i] = 8;
2843 else
2844 PFIndexes[i] = M[i];
2845 }
2846
2847 // Compute the index in the perfect shuffle table.
2848 unsigned PFTableIndex =
2849 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2850 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2851 unsigned Cost = (PFEntry >> 30);
2852
2853 if (Cost <= 4)
2854 return true;
2855 }
2856
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002857 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00002858 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002859
2860 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2861 isVREVMask(M, VT, 64) ||
2862 isVREVMask(M, VT, 32) ||
2863 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00002864 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2865 isVTRNMask(M, VT, WhichResult) ||
2866 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00002867 isVZIPMask(M, VT, WhichResult) ||
2868 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
2869 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
2870 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002871}
2872
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002873/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2874/// the specified operations to build the shuffle.
2875static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2876 SDValue RHS, SelectionDAG &DAG,
2877 DebugLoc dl) {
2878 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2879 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2880 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2881
2882 enum {
2883 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2884 OP_VREV,
2885 OP_VDUP0,
2886 OP_VDUP1,
2887 OP_VDUP2,
2888 OP_VDUP3,
2889 OP_VEXT1,
2890 OP_VEXT2,
2891 OP_VEXT3,
2892 OP_VUZPL, // VUZP, left result
2893 OP_VUZPR, // VUZP, right result
2894 OP_VZIPL, // VZIP, left result
2895 OP_VZIPR, // VZIP, right result
2896 OP_VTRNL, // VTRN, left result
2897 OP_VTRNR // VTRN, right result
2898 };
2899
2900 if (OpNum == OP_COPY) {
2901 if (LHSID == (1*9+2)*9+3) return LHS;
2902 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2903 return RHS;
2904 }
2905
2906 SDValue OpLHS, OpRHS;
2907 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2908 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2909 EVT VT = OpLHS.getValueType();
2910
2911 switch (OpNum) {
2912 default: llvm_unreachable("Unknown shuffle opcode!");
2913 case OP_VREV:
2914 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2915 case OP_VDUP0:
2916 case OP_VDUP1:
2917 case OP_VDUP2:
2918 case OP_VDUP3:
2919 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002920 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002921 case OP_VEXT1:
2922 case OP_VEXT2:
2923 case OP_VEXT3:
2924 return DAG.getNode(ARMISD::VEXT, dl, VT,
2925 OpLHS, OpRHS,
2926 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2927 case OP_VUZPL:
2928 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002929 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002930 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
2931 case OP_VZIPL:
2932 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002933 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002934 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
2935 case OP_VTRNL:
2936 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002937 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2938 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002939 }
2940}
2941
Bob Wilson5bafff32009-06-22 23:27:02 +00002942static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002943 SDValue V1 = Op.getOperand(0);
2944 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00002945 DebugLoc dl = Op.getDebugLoc();
2946 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002947 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002948 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00002949
Bob Wilson28865062009-08-13 02:13:04 +00002950 // Convert shuffles that are directly supported on NEON to target-specific
2951 // DAG nodes, instead of keeping them as shuffles and matching them again
2952 // during code selection. This is more efficient and avoids the possibility
2953 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00002954 // FIXME: floating-point vectors should be canonicalized to integer vectors
2955 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002956 SVN->getMask(ShuffleMask);
2957
2958 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson0ce37102009-08-14 05:08:32 +00002959 int Lane = SVN->getSplatIndex();
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00002960 // If this is undef splat, generate it via "just" vdup, if possible.
2961 if (Lane == -1) Lane = 0;
2962
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002963 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
2964 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00002965 }
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002966 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002967 DAG.getConstant(Lane, MVT::i32));
Bob Wilson0ce37102009-08-14 05:08:32 +00002968 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002969
2970 bool ReverseVEXT;
2971 unsigned Imm;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002972 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002973 if (ReverseVEXT)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002974 std::swap(V1, V2);
2975 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002976 DAG.getConstant(Imm, MVT::i32));
2977 }
2978
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002979 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002980 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002981 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002982 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002983 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002984 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
2985
Bob Wilsonc692cb72009-08-21 20:54:19 +00002986 // Check for Neon shuffles that modify both input vectors in place.
2987 // If both results are used, i.e., if there are two shuffles with the same
2988 // source operands and with masks corresponding to both results of one of
2989 // these operations, DAG memoization will ensure that a single node is
2990 // used for both shuffles.
2991 unsigned WhichResult;
2992 if (isVTRNMask(ShuffleMask, VT, WhichResult))
2993 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2994 V1, V2).getValue(WhichResult);
2995 if (isVUZPMask(ShuffleMask, VT, WhichResult))
2996 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2997 V1, V2).getValue(WhichResult);
2998 if (isVZIPMask(ShuffleMask, VT, WhichResult))
2999 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3000 V1, V2).getValue(WhichResult);
3001
Bob Wilson324f4f12009-12-03 06:40:55 +00003002 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
3003 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3004 V1, V1).getValue(WhichResult);
3005 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3006 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3007 V1, V1).getValue(WhichResult);
3008 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3009 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3010 V1, V1).getValue(WhichResult);
3011
Bob Wilsonc692cb72009-08-21 20:54:19 +00003012 // If the shuffle is not directly supported and it has 4 elements, use
3013 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003014 if (VT.getVectorNumElements() == 4 &&
3015 (VT.is128BitVector() || VT.is64BitVector())) {
3016 unsigned PFIndexes[4];
3017 for (unsigned i = 0; i != 4; ++i) {
3018 if (ShuffleMask[i] < 0)
3019 PFIndexes[i] = 8;
3020 else
3021 PFIndexes[i] = ShuffleMask[i];
3022 }
3023
3024 // Compute the index in the perfect shuffle table.
3025 unsigned PFTableIndex =
3026 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3027
3028 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3029 unsigned Cost = (PFEntry >> 30);
3030
3031 if (Cost <= 4)
3032 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
3033 }
Bob Wilsond8e17572009-08-12 22:31:50 +00003034
Bob Wilson63b88452010-05-20 18:39:53 +00003035 // v2f64 and v2i64 shuffles are just register copies.
3036 if (VT == MVT::v2f64 || VT == MVT::v2i64) {
3037 // Do the expansion as f64 since i64 is not legal.
3038 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, V1);
3039 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, V2);
3040 SDValue Val = DAG.getUNDEF(MVT::v2f64);
3041 for (unsigned i = 0; i < 2; ++i) {
3042 if (ShuffleMask[i] < 0)
3043 continue;
3044 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
3045 ShuffleMask[i] < 2 ? V1 : V2,
3046 DAG.getConstant(ShuffleMask[i] & 1, MVT::i32));
3047 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3048 Elt, DAG.getConstant(i, MVT::i32));
3049 }
3050 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
3051 }
3052
Bob Wilson22cac0d2009-08-14 05:16:33 +00003053 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003054}
3055
Bob Wilson5bafff32009-06-22 23:27:02 +00003056static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003057 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003058 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003059 SDValue Vec = Op.getOperand(0);
3060 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00003061 assert(VT == MVT::i32 &&
3062 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
3063 "unexpected type for custom-lowering vector extract");
3064 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00003065}
3066
Bob Wilsona6d65862009-08-03 20:36:38 +00003067static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3068 // The only time a CONCAT_VECTORS operation can have legal types is when
3069 // two 64-bit vectors are concatenated to a 128-bit vector.
3070 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
3071 "unexpected CONCAT_VECTORS");
3072 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00003073 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00003074 SDValue Op0 = Op.getOperand(0);
3075 SDValue Op1 = Op.getOperand(1);
3076 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003077 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3078 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00003079 DAG.getIntPtrConstant(0));
3080 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00003081 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3082 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00003083 DAG.getIntPtrConstant(1));
3084 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003085}
3086
Dan Gohmand858e902010-04-17 15:26:15 +00003087SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003088 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003089 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00003090 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00003091 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003092 case ISD::GlobalAddress:
3093 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
3094 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00003095 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00003096 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
3097 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003098 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00003099 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00003100 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00003101 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00003102 case ISD::SINT_TO_FP:
3103 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
3104 case ISD::FP_TO_SINT:
3105 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003106 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00003107 case ISD::RETURNADDR: break;
Jim Grosbach0e0da732009-05-12 23:59:14 +00003108 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00003109 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00003110 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
3111 Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00003112 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003113 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00003114 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00003115 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00003116 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003117 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00003118 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00003119 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00003120 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
3121 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3122 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00003123 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00003124 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00003125 }
Dan Gohman475871a2008-07-27 21:46:04 +00003126 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003127}
3128
Duncan Sands1607f052008-12-01 11:39:25 +00003129/// ReplaceNodeResults - Replace the results of node with an illegal result
3130/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00003131void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
3132 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00003133 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00003134 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00003135 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00003136 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003137 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003138 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003139 case ISD::BIT_CONVERT:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003140 Res = ExpandBIT_CONVERT(N, DAG);
3141 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00003142 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00003143 case ISD::SRA:
3144 Res = LowerShift(N, DAG, Subtarget);
3145 break;
Duncan Sands1607f052008-12-01 11:39:25 +00003146 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00003147 if (Res.getNode())
3148 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00003149}
Chris Lattner27a6c732007-11-24 07:07:01 +00003150
Evan Chenga8e29892007-01-19 07:51:42 +00003151//===----------------------------------------------------------------------===//
3152// ARM Scheduler Hooks
3153//===----------------------------------------------------------------------===//
3154
3155MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003156ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
3157 MachineBasicBlock *BB,
3158 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00003159 unsigned dest = MI->getOperand(0).getReg();
3160 unsigned ptr = MI->getOperand(1).getReg();
3161 unsigned oldval = MI->getOperand(2).getReg();
3162 unsigned newval = MI->getOperand(3).getReg();
3163 unsigned scratch = BB->getParent()->getRegInfo()
3164 .createVirtualRegister(ARM::GPRRegisterClass);
3165 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3166 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003167 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00003168
3169 unsigned ldrOpc, strOpc;
3170 switch (Size) {
3171 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003172 case 1:
3173 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
3174 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
3175 break;
3176 case 2:
3177 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3178 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3179 break;
3180 case 4:
3181 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3182 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3183 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00003184 }
3185
3186 MachineFunction *MF = BB->getParent();
3187 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3188 MachineFunction::iterator It = BB;
3189 ++It; // insert the new blocks after the current block
3190
3191 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3192 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3193 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3194 MF->insert(It, loop1MBB);
3195 MF->insert(It, loop2MBB);
3196 MF->insert(It, exitMBB);
3197 exitMBB->transferSuccessors(BB);
3198
3199 // thisMBB:
3200 // ...
3201 // fallthrough --> loop1MBB
3202 BB->addSuccessor(loop1MBB);
3203
3204 // loop1MBB:
3205 // ldrex dest, [ptr]
3206 // cmp dest, oldval
3207 // bne exitMBB
3208 BB = loop1MBB;
3209 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003210 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003211 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003212 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3213 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003214 BB->addSuccessor(loop2MBB);
3215 BB->addSuccessor(exitMBB);
3216
3217 // loop2MBB:
3218 // strex scratch, newval, [ptr]
3219 // cmp scratch, #0
3220 // bne loop1MBB
3221 BB = loop2MBB;
3222 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
3223 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003224 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00003225 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003226 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3227 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003228 BB->addSuccessor(loop1MBB);
3229 BB->addSuccessor(exitMBB);
3230
3231 // exitMBB:
3232 // ...
3233 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00003234
3235 MF->DeleteMachineInstr(MI); // The instruction is gone now.
3236
Jim Grosbach5278eb82009-12-11 01:42:04 +00003237 return BB;
3238}
3239
3240MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00003241ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
3242 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00003243 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
3244 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3245
3246 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003247 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003248 MachineFunction::iterator It = BB;
3249 ++It;
3250
3251 unsigned dest = MI->getOperand(0).getReg();
3252 unsigned ptr = MI->getOperand(1).getReg();
3253 unsigned incr = MI->getOperand(2).getReg();
3254 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00003255
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003256 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003257 unsigned ldrOpc, strOpc;
3258 switch (Size) {
3259 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003260 case 1:
3261 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00003262 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003263 break;
3264 case 2:
3265 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3266 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3267 break;
3268 case 4:
3269 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3270 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3271 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00003272 }
3273
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003274 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3275 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3276 MF->insert(It, loopMBB);
3277 MF->insert(It, exitMBB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003278 exitMBB->transferSuccessors(BB);
3279
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003280 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00003281 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3282 unsigned scratch2 = (!BinOpcode) ? incr :
3283 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3284
3285 // thisMBB:
3286 // ...
3287 // fallthrough --> loopMBB
3288 BB->addSuccessor(loopMBB);
3289
3290 // loopMBB:
3291 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003292 // <binop> scratch2, dest, incr
3293 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00003294 // cmp scratch, #0
3295 // bne- loopMBB
3296 // fallthrough --> exitMBB
3297 BB = loopMBB;
3298 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00003299 if (BinOpcode) {
3300 // operand order needs to go the other way for NAND
3301 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
3302 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3303 addReg(incr).addReg(dest)).addReg(0);
3304 else
3305 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3306 addReg(dest).addReg(incr)).addReg(0);
3307 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00003308
3309 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
3310 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003311 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00003312 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003313 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3314 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00003315
3316 BB->addSuccessor(loopMBB);
3317 BB->addSuccessor(exitMBB);
3318
3319 // exitMBB:
3320 // ...
3321 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00003322
Jim Grosbach867bbbf2010-01-15 00:22:18 +00003323 MF->DeleteMachineInstr(MI); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00003324
Jim Grosbachc3c23542009-12-14 04:22:04 +00003325 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00003326}
3327
3328MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00003329ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003330 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003331 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00003332 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003333 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00003334 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00003335 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00003336 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00003337 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00003338
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003339 case ARM::ATOMIC_LOAD_ADD_I8:
3340 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3341 case ARM::ATOMIC_LOAD_ADD_I16:
3342 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3343 case ARM::ATOMIC_LOAD_ADD_I32:
3344 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003345
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003346 case ARM::ATOMIC_LOAD_AND_I8:
3347 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3348 case ARM::ATOMIC_LOAD_AND_I16:
3349 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3350 case ARM::ATOMIC_LOAD_AND_I32:
3351 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003352
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003353 case ARM::ATOMIC_LOAD_OR_I8:
3354 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3355 case ARM::ATOMIC_LOAD_OR_I16:
3356 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3357 case ARM::ATOMIC_LOAD_OR_I32:
3358 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003359
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003360 case ARM::ATOMIC_LOAD_XOR_I8:
3361 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3362 case ARM::ATOMIC_LOAD_XOR_I16:
3363 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3364 case ARM::ATOMIC_LOAD_XOR_I32:
3365 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003366
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003367 case ARM::ATOMIC_LOAD_NAND_I8:
3368 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3369 case ARM::ATOMIC_LOAD_NAND_I16:
3370 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3371 case ARM::ATOMIC_LOAD_NAND_I32:
3372 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003373
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003374 case ARM::ATOMIC_LOAD_SUB_I8:
3375 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3376 case ARM::ATOMIC_LOAD_SUB_I16:
3377 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3378 case ARM::ATOMIC_LOAD_SUB_I32:
3379 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00003380
Jim Grosbacha36c8f22009-12-14 20:14:59 +00003381 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
3382 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
3383 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00003384
3385 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
3386 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
3387 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00003388
Evan Cheng007ea272009-08-12 05:17:19 +00003389 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00003390 // To "insert" a SELECT_CC instruction, we actually have to insert the
3391 // diamond control-flow pattern. The incoming instruction knows the
3392 // destination vreg to set, the condition code register to branch on, the
3393 // true/false values to select between, and a branch opcode to use.
3394 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003395 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00003396 ++It;
3397
3398 // thisMBB:
3399 // ...
3400 // TrueVal = ...
3401 // cmpTY ccX, r1, r2
3402 // bCC copy1MBB
3403 // fallthrough --> copy0MBB
3404 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003405 MachineFunction *F = BB->getParent();
3406 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3407 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00003408 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00003409 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003410 F->insert(It, copy0MBB);
3411 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00003412 // Update machine-CFG edges by first adding all successors of the current
3413 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00003414 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00003415 E = BB->succ_end(); I != E; ++I)
Evan Chengce319102009-09-19 09:51:03 +00003416 sinkMBB->addSuccessor(*I);
Evan Chenga8e29892007-01-19 07:51:42 +00003417 // Next, remove all successors of the current block, and add the true
3418 // and fallthrough blocks as its successors.
Evan Chengce319102009-09-19 09:51:03 +00003419 while (!BB->succ_empty())
Evan Chenga8e29892007-01-19 07:51:42 +00003420 BB->removeSuccessor(BB->succ_begin());
3421 BB->addSuccessor(copy0MBB);
3422 BB->addSuccessor(sinkMBB);
3423
3424 // copy0MBB:
3425 // %FalseValue = ...
3426 // # fallthrough to sinkMBB
3427 BB = copy0MBB;
3428
3429 // Update machine-CFG edges
3430 BB->addSuccessor(sinkMBB);
3431
3432 // sinkMBB:
3433 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3434 // ...
3435 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00003436 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00003437 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
3438 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
3439
Dan Gohman8e5f2c62008-07-07 23:14:23 +00003440 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00003441 return BB;
3442 }
Evan Cheng86198642009-08-07 00:34:42 +00003443
3444 case ARM::tANDsp:
3445 case ARM::tADDspr_:
3446 case ARM::tSUBspi_:
3447 case ARM::t2SUBrSPi_:
3448 case ARM::t2SUBrSPi12_:
3449 case ARM::t2SUBrSPs_: {
3450 MachineFunction *MF = BB->getParent();
3451 unsigned DstReg = MI->getOperand(0).getReg();
3452 unsigned SrcReg = MI->getOperand(1).getReg();
3453 bool DstIsDead = MI->getOperand(0).isDead();
3454 bool SrcIsKill = MI->getOperand(1).isKill();
3455
3456 if (SrcReg != ARM::SP) {
3457 // Copy the source to SP from virtual register.
3458 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
3459 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3460 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
3461 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
3462 .addReg(SrcReg, getKillRegState(SrcIsKill));
3463 }
3464
3465 unsigned OpOpc = 0;
3466 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
3467 switch (MI->getOpcode()) {
3468 default:
3469 llvm_unreachable("Unexpected pseudo instruction!");
3470 case ARM::tANDsp:
3471 OpOpc = ARM::tAND;
3472 NeedPred = true;
3473 break;
3474 case ARM::tADDspr_:
3475 OpOpc = ARM::tADDspr;
3476 break;
3477 case ARM::tSUBspi_:
3478 OpOpc = ARM::tSUBspi;
3479 break;
3480 case ARM::t2SUBrSPi_:
3481 OpOpc = ARM::t2SUBrSPi;
3482 NeedPred = true; NeedCC = true;
3483 break;
3484 case ARM::t2SUBrSPi12_:
3485 OpOpc = ARM::t2SUBrSPi12;
3486 NeedPred = true;
3487 break;
3488 case ARM::t2SUBrSPs_:
3489 OpOpc = ARM::t2SUBrSPs;
3490 NeedPred = true; NeedCC = true; NeedOp3 = true;
3491 break;
3492 }
3493 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
3494 if (OpOpc == ARM::tAND)
3495 AddDefaultT1CC(MIB);
3496 MIB.addReg(ARM::SP);
3497 MIB.addOperand(MI->getOperand(2));
3498 if (NeedOp3)
3499 MIB.addOperand(MI->getOperand(3));
3500 if (NeedPred)
3501 AddDefaultPred(MIB);
3502 if (NeedCC)
3503 AddDefaultCC(MIB);
3504
3505 // Copy the result from SP to virtual register.
3506 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
3507 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3508 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
3509 BuildMI(BB, dl, TII->get(CopyOpc))
3510 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
3511 .addReg(ARM::SP);
3512 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3513 return BB;
3514 }
Evan Chenga8e29892007-01-19 07:51:42 +00003515 }
3516}
3517
3518//===----------------------------------------------------------------------===//
3519// ARM Optimization Hooks
3520//===----------------------------------------------------------------------===//
3521
Chris Lattnerd1980a52009-03-12 06:52:53 +00003522static
3523SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3524 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00003525 SelectionDAG &DAG = DCI.DAG;
3526 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003527 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00003528 unsigned Opc = N->getOpcode();
3529 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3530 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3531 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3532 ISD::CondCode CC = ISD::SETCC_INVALID;
3533
3534 if (isSlctCC) {
3535 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
3536 } else {
3537 SDValue CCOp = Slct.getOperand(0);
3538 if (CCOp.getOpcode() == ISD::SETCC)
3539 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
3540 }
3541
3542 bool DoXform = false;
3543 bool InvCC = false;
3544 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
3545 "Bad input!");
3546
3547 if (LHS.getOpcode() == ISD::Constant &&
3548 cast<ConstantSDNode>(LHS)->isNullValue()) {
3549 DoXform = true;
3550 } else if (CC != ISD::SETCC_INVALID &&
3551 RHS.getOpcode() == ISD::Constant &&
3552 cast<ConstantSDNode>(RHS)->isNullValue()) {
3553 std::swap(LHS, RHS);
3554 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003555 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00003556 Op0.getOperand(0).getValueType();
3557 bool isInt = OpVT.isInteger();
3558 CC = ISD::getSetCCInverse(CC, isInt);
3559
3560 if (!TLI.isCondCodeLegal(CC, OpVT))
3561 return SDValue(); // Inverse operator isn't legal.
3562
3563 DoXform = true;
3564 InvCC = true;
3565 }
3566
3567 if (DoXform) {
3568 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3569 if (isSlctCC)
3570 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3571 Slct.getOperand(0), Slct.getOperand(1), CC);
3572 SDValue CCOp = Slct.getOperand(0);
3573 if (InvCC)
3574 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3575 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3576 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3577 CCOp, OtherOp, Result);
3578 }
3579 return SDValue();
3580}
3581
3582/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3583static SDValue PerformADDCombine(SDNode *N,
3584 TargetLowering::DAGCombinerInfo &DCI) {
3585 // added by evan in r37685 with no testcase.
3586 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003587
Chris Lattnerd1980a52009-03-12 06:52:53 +00003588 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3589 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3590 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3591 if (Result.getNode()) return Result;
3592 }
3593 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3594 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3595 if (Result.getNode()) return Result;
3596 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003597
Chris Lattnerd1980a52009-03-12 06:52:53 +00003598 return SDValue();
3599}
3600
3601/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3602static SDValue PerformSUBCombine(SDNode *N,
3603 TargetLowering::DAGCombinerInfo &DCI) {
3604 // added by evan in r37685 with no testcase.
3605 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003606
Chris Lattnerd1980a52009-03-12 06:52:53 +00003607 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3608 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3609 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3610 if (Result.getNode()) return Result;
3611 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003612
Chris Lattnerd1980a52009-03-12 06:52:53 +00003613 return SDValue();
3614}
3615
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003616static SDValue PerformMULCombine(SDNode *N,
3617 TargetLowering::DAGCombinerInfo &DCI,
3618 const ARMSubtarget *Subtarget) {
3619 SelectionDAG &DAG = DCI.DAG;
3620
3621 if (Subtarget->isThumb1Only())
3622 return SDValue();
3623
3624 if (DAG.getMachineFunction().
3625 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
3626 return SDValue();
3627
3628 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
3629 return SDValue();
3630
3631 EVT VT = N->getValueType(0);
3632 if (VT != MVT::i32)
3633 return SDValue();
3634
3635 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
3636 if (!C)
3637 return SDValue();
3638
3639 uint64_t MulAmt = C->getZExtValue();
3640 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
3641 ShiftAmt = ShiftAmt & (32 - 1);
3642 SDValue V = N->getOperand(0);
3643 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003644
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003645 SDValue Res;
3646 MulAmt >>= ShiftAmt;
3647 if (isPowerOf2_32(MulAmt - 1)) {
3648 // (mul x, 2^N + 1) => (add (shl x, N), x)
3649 Res = DAG.getNode(ISD::ADD, DL, VT,
3650 V, DAG.getNode(ISD::SHL, DL, VT,
3651 V, DAG.getConstant(Log2_32(MulAmt-1),
3652 MVT::i32)));
3653 } else if (isPowerOf2_32(MulAmt + 1)) {
3654 // (mul x, 2^N - 1) => (sub (shl x, N), x)
3655 Res = DAG.getNode(ISD::SUB, DL, VT,
3656 DAG.getNode(ISD::SHL, DL, VT,
3657 V, DAG.getConstant(Log2_32(MulAmt+1),
3658 MVT::i32)),
3659 V);
3660 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003661 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003662
3663 if (ShiftAmt != 0)
3664 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
3665 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003666
3667 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00003668 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00003669 return SDValue();
3670}
3671
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +00003672/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
3673/// ARMISD::VMOVRRD.
Jim Grosbache5165492009-11-09 00:11:35 +00003674static SDValue PerformVMOVRRDCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003675 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003676 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003677 SDValue InDouble = N->getOperand(0);
Jim Grosbache5165492009-11-09 00:11:35 +00003678 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003679 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003680 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003681}
3682
Bob Wilson5bafff32009-06-22 23:27:02 +00003683/// getVShiftImm - Check if this is a valid build_vector for the immediate
3684/// operand of a vector shift operation, where all the elements of the
3685/// build_vector must have the same constant integer value.
3686static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3687 // Ignore bit_converts.
3688 while (Op.getOpcode() == ISD::BIT_CONVERT)
3689 Op = Op.getOperand(0);
3690 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3691 APInt SplatBits, SplatUndef;
3692 unsigned SplatBitSize;
3693 bool HasAnyUndefs;
3694 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3695 HasAnyUndefs, ElementBits) ||
3696 SplatBitSize > ElementBits)
3697 return false;
3698 Cnt = SplatBits.getSExtValue();
3699 return true;
3700}
3701
3702/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3703/// operand of a vector shift left operation. That value must be in the range:
3704/// 0 <= Value < ElementBits for a left shift; or
3705/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003706static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003707 assert(VT.isVector() && "vector shift count is not a vector type");
3708 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3709 if (! getVShiftImm(Op, ElementBits, Cnt))
3710 return false;
3711 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3712}
3713
3714/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3715/// operand of a vector shift right operation. For a shift opcode, the value
3716/// is positive, but for an intrinsic the value count must be negative. The
3717/// absolute value must be in the range:
3718/// 1 <= |Value| <= ElementBits for a right shift; or
3719/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003720static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00003721 int64_t &Cnt) {
3722 assert(VT.isVector() && "vector shift count is not a vector type");
3723 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3724 if (! getVShiftImm(Op, ElementBits, Cnt))
3725 return false;
3726 if (isIntrinsic)
3727 Cnt = -Cnt;
3728 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3729}
3730
3731/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3732static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3733 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3734 switch (IntNo) {
3735 default:
3736 // Don't do anything for most intrinsics.
3737 break;
3738
3739 // Vector shifts: check for immediate versions and lower them.
3740 // Note: This is done during DAG combining instead of DAG legalizing because
3741 // the build_vectors for 64-bit vector element shift counts are generally
3742 // not legal, and it is hard to see their values after they get legalized to
3743 // loads from a constant pool.
3744 case Intrinsic::arm_neon_vshifts:
3745 case Intrinsic::arm_neon_vshiftu:
3746 case Intrinsic::arm_neon_vshiftls:
3747 case Intrinsic::arm_neon_vshiftlu:
3748 case Intrinsic::arm_neon_vshiftn:
3749 case Intrinsic::arm_neon_vrshifts:
3750 case Intrinsic::arm_neon_vrshiftu:
3751 case Intrinsic::arm_neon_vrshiftn:
3752 case Intrinsic::arm_neon_vqshifts:
3753 case Intrinsic::arm_neon_vqshiftu:
3754 case Intrinsic::arm_neon_vqshiftsu:
3755 case Intrinsic::arm_neon_vqshiftns:
3756 case Intrinsic::arm_neon_vqshiftnu:
3757 case Intrinsic::arm_neon_vqshiftnsu:
3758 case Intrinsic::arm_neon_vqrshiftns:
3759 case Intrinsic::arm_neon_vqrshiftnu:
3760 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00003761 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003762 int64_t Cnt;
3763 unsigned VShiftOpc = 0;
3764
3765 switch (IntNo) {
3766 case Intrinsic::arm_neon_vshifts:
3767 case Intrinsic::arm_neon_vshiftu:
3768 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3769 VShiftOpc = ARMISD::VSHL;
3770 break;
3771 }
3772 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3773 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3774 ARMISD::VSHRs : ARMISD::VSHRu);
3775 break;
3776 }
3777 return SDValue();
3778
3779 case Intrinsic::arm_neon_vshiftls:
3780 case Intrinsic::arm_neon_vshiftlu:
3781 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3782 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003783 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003784
3785 case Intrinsic::arm_neon_vrshifts:
3786 case Intrinsic::arm_neon_vrshiftu:
3787 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3788 break;
3789 return SDValue();
3790
3791 case Intrinsic::arm_neon_vqshifts:
3792 case Intrinsic::arm_neon_vqshiftu:
3793 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3794 break;
3795 return SDValue();
3796
3797 case Intrinsic::arm_neon_vqshiftsu:
3798 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3799 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003800 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003801
3802 case Intrinsic::arm_neon_vshiftn:
3803 case Intrinsic::arm_neon_vrshiftn:
3804 case Intrinsic::arm_neon_vqshiftns:
3805 case Intrinsic::arm_neon_vqshiftnu:
3806 case Intrinsic::arm_neon_vqshiftnsu:
3807 case Intrinsic::arm_neon_vqrshiftns:
3808 case Intrinsic::arm_neon_vqrshiftnu:
3809 case Intrinsic::arm_neon_vqrshiftnsu:
3810 // Narrowing shifts require an immediate right shift.
3811 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3812 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003813 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003814
3815 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003816 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003817 }
3818
3819 switch (IntNo) {
3820 case Intrinsic::arm_neon_vshifts:
3821 case Intrinsic::arm_neon_vshiftu:
3822 // Opcode already set above.
3823 break;
3824 case Intrinsic::arm_neon_vshiftls:
3825 case Intrinsic::arm_neon_vshiftlu:
3826 if (Cnt == VT.getVectorElementType().getSizeInBits())
3827 VShiftOpc = ARMISD::VSHLLi;
3828 else
3829 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3830 ARMISD::VSHLLs : ARMISD::VSHLLu);
3831 break;
3832 case Intrinsic::arm_neon_vshiftn:
3833 VShiftOpc = ARMISD::VSHRN; break;
3834 case Intrinsic::arm_neon_vrshifts:
3835 VShiftOpc = ARMISD::VRSHRs; break;
3836 case Intrinsic::arm_neon_vrshiftu:
3837 VShiftOpc = ARMISD::VRSHRu; break;
3838 case Intrinsic::arm_neon_vrshiftn:
3839 VShiftOpc = ARMISD::VRSHRN; break;
3840 case Intrinsic::arm_neon_vqshifts:
3841 VShiftOpc = ARMISD::VQSHLs; break;
3842 case Intrinsic::arm_neon_vqshiftu:
3843 VShiftOpc = ARMISD::VQSHLu; break;
3844 case Intrinsic::arm_neon_vqshiftsu:
3845 VShiftOpc = ARMISD::VQSHLsu; break;
3846 case Intrinsic::arm_neon_vqshiftns:
3847 VShiftOpc = ARMISD::VQSHRNs; break;
3848 case Intrinsic::arm_neon_vqshiftnu:
3849 VShiftOpc = ARMISD::VQSHRNu; break;
3850 case Intrinsic::arm_neon_vqshiftnsu:
3851 VShiftOpc = ARMISD::VQSHRNsu; break;
3852 case Intrinsic::arm_neon_vqrshiftns:
3853 VShiftOpc = ARMISD::VQRSHRNs; break;
3854 case Intrinsic::arm_neon_vqrshiftnu:
3855 VShiftOpc = ARMISD::VQRSHRNu; break;
3856 case Intrinsic::arm_neon_vqrshiftnsu:
3857 VShiftOpc = ARMISD::VQRSHRNsu; break;
3858 }
3859
3860 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003861 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003862 }
3863
3864 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00003865 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003866 int64_t Cnt;
3867 unsigned VShiftOpc = 0;
3868
3869 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3870 VShiftOpc = ARMISD::VSLI;
3871 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3872 VShiftOpc = ARMISD::VSRI;
3873 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003874 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003875 }
3876
3877 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3878 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00003879 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003880 }
3881
3882 case Intrinsic::arm_neon_vqrshifts:
3883 case Intrinsic::arm_neon_vqrshiftu:
3884 // No immediate versions of these to check for.
3885 break;
3886 }
3887
3888 return SDValue();
3889}
3890
3891/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3892/// lowers them. As with the vector shift intrinsics, this is done during DAG
3893/// combining instead of DAG legalizing because the build_vectors for 64-bit
3894/// vector element shift counts are generally not legal, and it is hard to see
3895/// their values after they get legalized to loads from a constant pool.
3896static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3897 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003898 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003899
3900 // Nothing to be done for scalar shifts.
3901 if (! VT.isVector())
3902 return SDValue();
3903
3904 assert(ST->hasNEON() && "unexpected vector shift");
3905 int64_t Cnt;
3906
3907 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003908 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003909
3910 case ISD::SHL:
3911 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3912 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003913 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003914 break;
3915
3916 case ISD::SRA:
3917 case ISD::SRL:
3918 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3919 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3920 ARMISD::VSHRs : ARMISD::VSHRu);
3921 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003922 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003923 }
3924 }
3925 return SDValue();
3926}
3927
3928/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3929/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3930static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3931 const ARMSubtarget *ST) {
3932 SDValue N0 = N->getOperand(0);
3933
3934 // Check for sign- and zero-extensions of vector extract operations of 8-
3935 // and 16-bit vector elements. NEON supports these directly. They are
3936 // handled during DAG combining because type legalization will promote them
3937 // to 32-bit types and it is messy to recognize the operations after that.
3938 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3939 SDValue Vec = N0.getOperand(0);
3940 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003941 EVT VT = N->getValueType(0);
3942 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003943 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3944
Owen Anderson825b72b2009-08-11 20:47:22 +00003945 if (VT == MVT::i32 &&
3946 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00003947 TLI.isTypeLegal(Vec.getValueType())) {
3948
3949 unsigned Opc = 0;
3950 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003951 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003952 case ISD::SIGN_EXTEND:
3953 Opc = ARMISD::VGETLANEs;
3954 break;
3955 case ISD::ZERO_EXTEND:
3956 case ISD::ANY_EXTEND:
3957 Opc = ARMISD::VGETLANEu;
3958 break;
3959 }
3960 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
3961 }
3962 }
3963
3964 return SDValue();
3965}
3966
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003967/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
3968/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
3969static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
3970 const ARMSubtarget *ST) {
3971 // If the target supports NEON, try to use vmax/vmin instructions for f32
3972 // selects like "x < y ? x : y". Unless the FiniteOnlyFPMath option is set,
3973 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
3974 // a NaN; only do the transformation when it matches that behavior.
3975
3976 // For now only do this when using NEON for FP operations; if using VFP, it
3977 // is not obvious that the benefit outweighs the cost of switching to the
3978 // NEON pipeline.
3979 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
3980 N->getValueType(0) != MVT::f32)
3981 return SDValue();
3982
3983 SDValue CondLHS = N->getOperand(0);
3984 SDValue CondRHS = N->getOperand(1);
3985 SDValue LHS = N->getOperand(2);
3986 SDValue RHS = N->getOperand(3);
3987 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
3988
3989 unsigned Opcode = 0;
3990 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00003991 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003992 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00003993 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00003994 IsReversed = true ; // x CC y ? y : x
3995 } else {
3996 return SDValue();
3997 }
3998
Bob Wilsone742bb52010-02-24 22:15:53 +00003999 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004000 switch (CC) {
4001 default: break;
4002 case ISD::SETOLT:
4003 case ISD::SETOLE:
4004 case ISD::SETLT:
4005 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004006 case ISD::SETULT:
4007 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004008 // If LHS is NaN, an ordered comparison will be false and the result will
4009 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
4010 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4011 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
4012 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4013 break;
4014 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
4015 // will return -0, so vmin can only be used for unsafe math or if one of
4016 // the operands is known to be nonzero.
4017 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
4018 !UnsafeFPMath &&
4019 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4020 break;
4021 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004022 break;
4023
4024 case ISD::SETOGT:
4025 case ISD::SETOGE:
4026 case ISD::SETGT:
4027 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004028 case ISD::SETUGT:
4029 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00004030 // If LHS is NaN, an ordered comparison will be false and the result will
4031 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
4032 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4033 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
4034 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4035 break;
4036 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
4037 // will return +0, so vmax can only be used for unsafe math or if one of
4038 // the operands is known to be nonzero.
4039 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
4040 !UnsafeFPMath &&
4041 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4042 break;
4043 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004044 break;
4045 }
4046
4047 if (!Opcode)
4048 return SDValue();
4049 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
4050}
4051
Dan Gohman475871a2008-07-27 21:46:04 +00004052SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004053 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004054 switch (N->getOpcode()) {
4055 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004056 case ISD::ADD: return PerformADDCombine(N, DCI);
4057 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004058 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbache5165492009-11-09 00:11:35 +00004059 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004060 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004061 case ISD::SHL:
4062 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004063 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004064 case ISD::SIGN_EXTEND:
4065 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00004066 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
4067 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004068 }
Dan Gohman475871a2008-07-27 21:46:04 +00004069 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00004070}
4071
Bill Wendlingaf566342009-08-15 21:21:19 +00004072bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
4073 if (!Subtarget->hasV6Ops())
4074 // Pre-v6 does not support unaligned mem access.
4075 return false;
Anton Korobeynikov90cfc132010-01-30 14:08:12 +00004076 else {
4077 // v6+ may or may not support unaligned mem access depending on the system
4078 // configuration.
4079 // FIXME: This is pretty conservative. Should we provide cmdline option to
4080 // control the behaviour?
Bill Wendlingaf566342009-08-15 21:21:19 +00004081 if (!Subtarget->isTargetDarwin())
4082 return false;
4083 }
4084
4085 switch (VT.getSimpleVT().SimpleTy) {
4086 default:
4087 return false;
4088 case MVT::i8:
4089 case MVT::i16:
4090 case MVT::i32:
4091 return true;
4092 // FIXME: VLD1 etc with standard alignment is legal.
4093 }
4094}
4095
Evan Chenge6c835f2009-08-14 20:09:37 +00004096static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
4097 if (V < 0)
4098 return false;
4099
4100 unsigned Scale = 1;
4101 switch (VT.getSimpleVT().SimpleTy) {
4102 default: return false;
4103 case MVT::i1:
4104 case MVT::i8:
4105 // Scale == 1;
4106 break;
4107 case MVT::i16:
4108 // Scale == 2;
4109 Scale = 2;
4110 break;
4111 case MVT::i32:
4112 // Scale == 4;
4113 Scale = 4;
4114 break;
4115 }
4116
4117 if ((V & (Scale - 1)) != 0)
4118 return false;
4119 V /= Scale;
4120 return V == (V & ((1LL << 5) - 1));
4121}
4122
4123static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
4124 const ARMSubtarget *Subtarget) {
4125 bool isNeg = false;
4126 if (V < 0) {
4127 isNeg = true;
4128 V = - V;
4129 }
4130
4131 switch (VT.getSimpleVT().SimpleTy) {
4132 default: return false;
4133 case MVT::i1:
4134 case MVT::i8:
4135 case MVT::i16:
4136 case MVT::i32:
4137 // + imm12 or - imm8
4138 if (isNeg)
4139 return V == (V & ((1LL << 8) - 1));
4140 return V == (V & ((1LL << 12) - 1));
4141 case MVT::f32:
4142 case MVT::f64:
4143 // Same as ARM mode. FIXME: NEON?
4144 if (!Subtarget->hasVFP2())
4145 return false;
4146 if ((V & 3) != 0)
4147 return false;
4148 V >>= 2;
4149 return V == (V & ((1LL << 8) - 1));
4150 }
4151}
4152
Evan Chengb01fad62007-03-12 23:30:29 +00004153/// isLegalAddressImmediate - Return true if the integer value can be used
4154/// as the offset of the target addressing mode for load / store of the
4155/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00004156static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004157 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00004158 if (V == 0)
4159 return true;
4160
Evan Cheng65011532009-03-09 19:15:00 +00004161 if (!VT.isSimple())
4162 return false;
4163
Evan Chenge6c835f2009-08-14 20:09:37 +00004164 if (Subtarget->isThumb1Only())
4165 return isLegalT1AddressImmediate(V, VT);
4166 else if (Subtarget->isThumb2())
4167 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00004168
Evan Chenge6c835f2009-08-14 20:09:37 +00004169 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00004170 if (V < 0)
4171 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00004172 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00004173 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004174 case MVT::i1:
4175 case MVT::i8:
4176 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00004177 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004178 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004179 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00004180 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004181 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004182 case MVT::f32:
4183 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00004184 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00004185 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00004186 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00004187 return false;
4188 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004189 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00004190 }
Evan Chenga8e29892007-01-19 07:51:42 +00004191}
4192
Evan Chenge6c835f2009-08-14 20:09:37 +00004193bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
4194 EVT VT) const {
4195 int Scale = AM.Scale;
4196 if (Scale < 0)
4197 return false;
4198
4199 switch (VT.getSimpleVT().SimpleTy) {
4200 default: return false;
4201 case MVT::i1:
4202 case MVT::i8:
4203 case MVT::i16:
4204 case MVT::i32:
4205 if (Scale == 1)
4206 return true;
4207 // r + r << imm
4208 Scale = Scale & ~1;
4209 return Scale == 2 || Scale == 4 || Scale == 8;
4210 case MVT::i64:
4211 // r + r
4212 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
4213 return true;
4214 return false;
4215 case MVT::isVoid:
4216 // Note, we allow "void" uses (basically, uses that aren't loads or
4217 // stores), because arm allows folding a scale into many arithmetic
4218 // operations. This should be made more precise and revisited later.
4219
4220 // Allow r << imm, but the imm has to be a multiple of two.
4221 if (Scale & 1) return false;
4222 return isPowerOf2_32(Scale);
4223 }
4224}
4225
Chris Lattner37caf8c2007-04-09 23:33:39 +00004226/// isLegalAddressingMode - Return true if the addressing mode represented
4227/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004228bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00004229 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004230 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00004231 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00004232 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004233
Chris Lattner37caf8c2007-04-09 23:33:39 +00004234 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004235 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004236 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004237
Chris Lattner37caf8c2007-04-09 23:33:39 +00004238 switch (AM.Scale) {
4239 case 0: // no scale reg, must be "r+i" or "r", or "i".
4240 break;
4241 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00004242 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00004243 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004244 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00004245 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00004246 // ARM doesn't support any R+R*scale+imm addr modes.
4247 if (AM.BaseOffs)
4248 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004249
Bob Wilson2c7dab12009-04-08 17:55:28 +00004250 if (!VT.isSimple())
4251 return false;
4252
Evan Chenge6c835f2009-08-14 20:09:37 +00004253 if (Subtarget->isThumb2())
4254 return isLegalT2ScaledAddressingMode(AM, VT);
4255
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004256 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00004257 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00004258 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00004259 case MVT::i1:
4260 case MVT::i8:
4261 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004262 if (Scale < 0) Scale = -Scale;
4263 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004264 return true;
4265 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00004266 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00004267 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00004268 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004269 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00004270 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00004271 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00004272 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00004273
Owen Anderson825b72b2009-08-11 20:47:22 +00004274 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00004275 // Note, we allow "void" uses (basically, uses that aren't loads or
4276 // stores), because arm allows folding a scale into many arithmetic
4277 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00004278
Chris Lattner37caf8c2007-04-09 23:33:39 +00004279 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00004280 if (Scale & 1) return false;
4281 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00004282 }
4283 break;
Evan Chengb01fad62007-03-12 23:30:29 +00004284 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00004285 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00004286}
4287
Evan Cheng77e47512009-11-11 19:05:52 +00004288/// isLegalICmpImmediate - Return true if the specified immediate is legal
4289/// icmp immediate, that is the target has icmp instructions which can compare
4290/// a register against the immediate without having to materialize the
4291/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00004292bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00004293 if (!Subtarget->isThumb())
4294 return ARM_AM::getSOImmVal(Imm) != -1;
4295 if (Subtarget->isThumb2())
4296 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00004297 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00004298}
4299
Owen Andersone50ed302009-08-10 22:56:29 +00004300static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004301 bool isSEXTLoad, SDValue &Base,
4302 SDValue &Offset, bool &isInc,
4303 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00004304 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4305 return false;
4306
Owen Anderson825b72b2009-08-11 20:47:22 +00004307 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00004308 // AddressingMode 3
4309 Base = Ptr->getOperand(0);
4310 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004311 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004312 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004313 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004314 isInc = false;
4315 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4316 return true;
4317 }
4318 }
4319 isInc = (Ptr->getOpcode() == ISD::ADD);
4320 Offset = Ptr->getOperand(1);
4321 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004322 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00004323 // AddressingMode 2
4324 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004325 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004326 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004327 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00004328 isInc = false;
4329 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4330 Base = Ptr->getOperand(0);
4331 return true;
4332 }
4333 }
4334
4335 if (Ptr->getOpcode() == ISD::ADD) {
4336 isInc = true;
4337 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
4338 if (ShOpcVal != ARM_AM::no_shift) {
4339 Base = Ptr->getOperand(1);
4340 Offset = Ptr->getOperand(0);
4341 } else {
4342 Base = Ptr->getOperand(0);
4343 Offset = Ptr->getOperand(1);
4344 }
4345 return true;
4346 }
4347
4348 isInc = (Ptr->getOpcode() == ISD::ADD);
4349 Base = Ptr->getOperand(0);
4350 Offset = Ptr->getOperand(1);
4351 return true;
4352 }
4353
Jim Grosbache5165492009-11-09 00:11:35 +00004354 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00004355 return false;
4356}
4357
Owen Andersone50ed302009-08-10 22:56:29 +00004358static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00004359 bool isSEXTLoad, SDValue &Base,
4360 SDValue &Offset, bool &isInc,
4361 SelectionDAG &DAG) {
4362 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4363 return false;
4364
4365 Base = Ptr->getOperand(0);
4366 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
4367 int RHSC = (int)RHS->getZExtValue();
4368 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
4369 assert(Ptr->getOpcode() == ISD::ADD);
4370 isInc = false;
4371 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4372 return true;
4373 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
4374 isInc = Ptr->getOpcode() == ISD::ADD;
4375 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
4376 return true;
4377 }
4378 }
4379
4380 return false;
4381}
4382
Evan Chenga8e29892007-01-19 07:51:42 +00004383/// getPreIndexedAddressParts - returns true by value, base pointer and
4384/// offset pointer and addressing mode by reference if the node's address
4385/// can be legally represented as pre-indexed load / store address.
4386bool
Dan Gohman475871a2008-07-27 21:46:04 +00004387ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
4388 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004389 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004390 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004391 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004392 return false;
4393
Owen Andersone50ed302009-08-10 22:56:29 +00004394 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004395 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004396 bool isSEXTLoad = false;
4397 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4398 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004399 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004400 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4401 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
4402 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004403 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00004404 } else
4405 return false;
4406
4407 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004408 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004409 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004410 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
4411 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004412 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004413 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00004414 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00004415 if (!isLegal)
4416 return false;
4417
4418 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
4419 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004420}
4421
4422/// getPostIndexedAddressParts - returns true by value, base pointer and
4423/// offset pointer and addressing mode by reference if this node can be
4424/// combined with a load / store to form a post-indexed load / store.
4425bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00004426 SDValue &Base,
4427 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00004428 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00004429 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00004430 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00004431 return false;
4432
Owen Andersone50ed302009-08-10 22:56:29 +00004433 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00004434 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00004435 bool isSEXTLoad = false;
4436 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004437 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00004438 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00004439 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4440 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004441 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00004442 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00004443 } else
4444 return false;
4445
4446 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00004447 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00004448 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00004449 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00004450 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00004451 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00004452 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
4453 isInc, DAG);
4454 if (!isLegal)
4455 return false;
4456
Evan Cheng28dad2a2010-05-18 21:31:17 +00004457 if (Ptr != Base) {
4458 // Swap base ptr and offset to catch more post-index load / store when
4459 // it's legal. In Thumb2 mode, offset must be an immediate.
4460 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
4461 !Subtarget->isThumb2())
4462 std::swap(Base, Offset);
4463
4464 // Post-indexed load / store update the base pointer.
4465 if (Ptr != Base)
4466 return false;
4467 }
4468
Evan Chenge88d5ce2009-07-02 07:28:31 +00004469 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
4470 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00004471}
4472
Dan Gohman475871a2008-07-27 21:46:04 +00004473void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00004474 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00004475 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004476 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00004477 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00004478 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004479 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004480 switch (Op.getOpcode()) {
4481 default: break;
4482 case ARMISD::CMOV: {
4483 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00004484 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004485 if (KnownZero == 0 && KnownOne == 0) return;
4486
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00004487 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00004488 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
4489 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00004490 KnownZero &= KnownZeroRHS;
4491 KnownOne &= KnownOneRHS;
4492 return;
4493 }
4494 }
4495}
4496
4497//===----------------------------------------------------------------------===//
4498// ARM Inline Assembly Support
4499//===----------------------------------------------------------------------===//
4500
4501/// getConstraintType - Given a constraint letter, return the type of
4502/// constraint it is for this target.
4503ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00004504ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
4505 if (Constraint.size() == 1) {
4506 switch (Constraint[0]) {
4507 default: break;
4508 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004509 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00004510 }
Evan Chenga8e29892007-01-19 07:51:42 +00004511 }
Chris Lattner4234f572007-03-25 02:14:49 +00004512 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00004513}
4514
Bob Wilson2dc4f542009-03-20 22:42:55 +00004515std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00004516ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004517 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004518 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004519 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00004520 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004521 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00004522 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004523 return std::make_pair(0U, ARM::tGPRRegisterClass);
4524 else
4525 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004526 case 'r':
4527 return std::make_pair(0U, ARM::GPRRegisterClass);
4528 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004529 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004530 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00004531 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004532 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00004533 if (VT.getSizeInBits() == 128)
4534 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004535 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004536 }
4537 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00004538 if (StringRef("{cc}").equals_lower(Constraint))
4539 return std::make_pair(0U, ARM::CCRRegisterClass);
4540
Evan Chenga8e29892007-01-19 07:51:42 +00004541 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
4542}
4543
4544std::vector<unsigned> ARMTargetLowering::
4545getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00004546 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004547 if (Constraint.size() != 1)
4548 return std::vector<unsigned>();
4549
4550 switch (Constraint[0]) { // GCC ARM Constraint Letters
4551 default: break;
4552 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00004553 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4554 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4555 0);
Evan Chenga8e29892007-01-19 07:51:42 +00004556 case 'r':
4557 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4558 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4559 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
4560 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004561 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00004562 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004563 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
4564 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
4565 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
4566 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
4567 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
4568 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
4569 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
4570 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00004571 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004572 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
4573 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
4574 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
4575 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00004576 if (VT.getSizeInBits() == 128)
4577 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
4578 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00004579 break;
Evan Chenga8e29892007-01-19 07:51:42 +00004580 }
4581
4582 return std::vector<unsigned>();
4583}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004584
4585/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
4586/// vector. If it is invalid, don't add anything to Ops.
4587void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
4588 char Constraint,
4589 bool hasMemory,
4590 std::vector<SDValue>&Ops,
4591 SelectionDAG &DAG) const {
4592 SDValue Result(0, 0);
4593
4594 switch (Constraint) {
4595 default: break;
4596 case 'I': case 'J': case 'K': case 'L':
4597 case 'M': case 'N': case 'O':
4598 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
4599 if (!C)
4600 return;
4601
4602 int64_t CVal64 = C->getSExtValue();
4603 int CVal = (int) CVal64;
4604 // None of these constraints allow values larger than 32 bits. Check
4605 // that the value fits in an int.
4606 if (CVal != CVal64)
4607 return;
4608
4609 switch (Constraint) {
4610 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004611 if (Subtarget->isThumb1Only()) {
4612 // This must be a constant between 0 and 255, for ADD
4613 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004614 if (CVal >= 0 && CVal <= 255)
4615 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004616 } else if (Subtarget->isThumb2()) {
4617 // A constant that can be used as an immediate value in a
4618 // data-processing instruction.
4619 if (ARM_AM::getT2SOImmVal(CVal) != -1)
4620 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004621 } else {
4622 // A constant that can be used as an immediate value in a
4623 // data-processing instruction.
4624 if (ARM_AM::getSOImmVal(CVal) != -1)
4625 break;
4626 }
4627 return;
4628
4629 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004630 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004631 // This must be a constant between -255 and -1, for negated ADD
4632 // immediates. This can be used in GCC with an "n" modifier that
4633 // prints the negated value, for use with SUB instructions. It is
4634 // not useful otherwise but is implemented for compatibility.
4635 if (CVal >= -255 && CVal <= -1)
4636 break;
4637 } else {
4638 // This must be a constant between -4095 and 4095. It is not clear
4639 // what this constraint is intended for. Implemented for
4640 // compatibility with GCC.
4641 if (CVal >= -4095 && CVal <= 4095)
4642 break;
4643 }
4644 return;
4645
4646 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004647 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004648 // A 32-bit value where only one byte has a nonzero value. Exclude
4649 // zero to match GCC. This constraint is used by GCC internally for
4650 // constants that can be loaded with a move/shift combination.
4651 // It is not useful otherwise but is implemented for compatibility.
4652 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
4653 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004654 } else if (Subtarget->isThumb2()) {
4655 // A constant whose bitwise inverse can be used as an immediate
4656 // value in a data-processing instruction. This can be used in GCC
4657 // with a "B" modifier that prints the inverted value, for use with
4658 // BIC and MVN instructions. It is not useful otherwise but is
4659 // implemented for compatibility.
4660 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
4661 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004662 } else {
4663 // A constant whose bitwise inverse can be used as an immediate
4664 // value in a data-processing instruction. This can be used in GCC
4665 // with a "B" modifier that prints the inverted value, for use with
4666 // BIC and MVN instructions. It is not useful otherwise but is
4667 // implemented for compatibility.
4668 if (ARM_AM::getSOImmVal(~CVal) != -1)
4669 break;
4670 }
4671 return;
4672
4673 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004674 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004675 // This must be a constant between -7 and 7,
4676 // for 3-operand ADD/SUB immediate instructions.
4677 if (CVal >= -7 && CVal < 7)
4678 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004679 } else if (Subtarget->isThumb2()) {
4680 // A constant whose negation can be used as an immediate value in a
4681 // data-processing instruction. This can be used in GCC with an "n"
4682 // modifier that prints the negated value, for use with SUB
4683 // instructions. It is not useful otherwise but is implemented for
4684 // compatibility.
4685 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
4686 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004687 } else {
4688 // A constant whose negation can be used as an immediate value in a
4689 // data-processing instruction. This can be used in GCC with an "n"
4690 // modifier that prints the negated value, for use with SUB
4691 // instructions. It is not useful otherwise but is implemented for
4692 // compatibility.
4693 if (ARM_AM::getSOImmVal(-CVal) != -1)
4694 break;
4695 }
4696 return;
4697
4698 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004699 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004700 // This must be a multiple of 4 between 0 and 1020, for
4701 // ADD sp + immediate.
4702 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
4703 break;
4704 } else {
4705 // A power of two or a constant between 0 and 32. This is used in
4706 // GCC for the shift amount on shifted register operands, but it is
4707 // useful in general for any shift amounts.
4708 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
4709 break;
4710 }
4711 return;
4712
4713 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004714 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004715 // This must be a constant between 0 and 31, for shift amounts.
4716 if (CVal >= 0 && CVal <= 31)
4717 break;
4718 }
4719 return;
4720
4721 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004722 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004723 // This must be a multiple of 4 between -508 and 508, for
4724 // ADD/SUB sp = sp + immediate.
4725 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
4726 break;
4727 }
4728 return;
4729 }
4730 Result = DAG.getTargetConstant(CVal, Op.getValueType());
4731 break;
4732 }
4733
4734 if (Result.getNode()) {
4735 Ops.push_back(Result);
4736 return;
4737 }
4738 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
4739 Ops, DAG);
4740}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00004741
4742bool
4743ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4744 // The ARM target isn't yet aware of offsets.
4745 return false;
4746}
Evan Cheng39382422009-10-28 01:44:26 +00004747
4748int ARM::getVFPf32Imm(const APFloat &FPImm) {
4749 APInt Imm = FPImm.bitcastToAPInt();
4750 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
4751 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
4752 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
4753
4754 // We can handle 4 bits of mantissa.
4755 // mantissa = (16+UInt(e:f:g:h))/16.
4756 if (Mantissa & 0x7ffff)
4757 return -1;
4758 Mantissa >>= 19;
4759 if ((Mantissa & 0xf) != Mantissa)
4760 return -1;
4761
4762 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4763 if (Exp < -3 || Exp > 4)
4764 return -1;
4765 Exp = ((Exp+3) & 0x7) ^ 4;
4766
4767 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4768}
4769
4770int ARM::getVFPf64Imm(const APFloat &FPImm) {
4771 APInt Imm = FPImm.bitcastToAPInt();
4772 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
4773 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
4774 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
4775
4776 // We can handle 4 bits of mantissa.
4777 // mantissa = (16+UInt(e:f:g:h))/16.
4778 if (Mantissa & 0xffffffffffffLL)
4779 return -1;
4780 Mantissa >>= 48;
4781 if ((Mantissa & 0xf) != Mantissa)
4782 return -1;
4783
4784 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4785 if (Exp < -3 || Exp > 4)
4786 return -1;
4787 Exp = ((Exp+3) & 0x7) ^ 4;
4788
4789 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4790}
4791
4792/// isFPImmLegal - Returns true if the target can instruction select the
4793/// specified FP immediate natively. If false, the legalizer will
4794/// materialize the FP immediate as a load from a constant pool.
4795bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
4796 if (!Subtarget->hasVFP3())
4797 return false;
4798 if (VT == MVT::f32)
4799 return ARM::getVFPf32Imm(Imm) != -1;
4800 if (VT == MVT::f64)
4801 return ARM::getVFPf64Imm(Imm) != -1;
4802 return false;
4803}