blob: 25fc86fd0f6829b49b9bd8fa837e9e1bcd8075a7 [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"
Evan Chengb6ab2542007-01-31 08:40:13 +000040#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000041#include "llvm/ADT/VectorExtras.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000042#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000043#include "llvm/Support/MathExtras.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000044#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000045using namespace llvm;
46
Owen Andersone50ed302009-08-10 22:56:29 +000047static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000048 CCValAssign::LocInfo &LocInfo,
49 ISD::ArgFlagsTy &ArgFlags,
50 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000051static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +000052 CCValAssign::LocInfo &LocInfo,
53 ISD::ArgFlagsTy &ArgFlags,
54 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000055static bool RetCC_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 RetCC_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);
63
Owen Andersone50ed302009-08-10 22:56:29 +000064void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
65 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000066 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000067 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000068 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
69 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000070
Owen Anderson70671842009-08-10 20:18:46 +000071 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000072 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000073 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000074 }
75
Owen Andersone50ed302009-08-10 22:56:29 +000076 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +000077 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +000078 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +000079 if (ElemTy == MVT::i8 || ElemTy == MVT::i16)
Owen Anderson70671842009-08-10 20:18:46 +000080 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +000081 if (ElemTy != MVT::i32) {
82 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
83 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
84 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
85 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
86 }
Owen Anderson70671842009-08-10 20:18:46 +000087 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
88 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Owen Anderson70671842009-08-10 20:18:46 +000089 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Custom);
Anton Korobeynikov8e6c2b92009-08-21 12:40:35 +000090 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +000091 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +000092 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
93 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
94 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +000095 }
96
97 // Promote all bit-wise operations.
98 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +000099 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000100 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
101 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000102 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000103 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000104 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000105 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000106 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000107 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000108 }
Bob Wilson16330762009-09-16 00:17:28 +0000109
110 // Neon does not support vector divide/remainder operations.
111 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
112 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
113 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
114 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
115 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
116 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000117}
118
Owen Andersone50ed302009-08-10 22:56:29 +0000119void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000120 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000121 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000122}
123
Owen Andersone50ed302009-08-10 22:56:29 +0000124void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000125 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000126 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000127}
128
Chris Lattnerf0144122009-07-28 03:13:23 +0000129static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
130 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000131 return new TargetLoweringObjectFileMachO();
Chris Lattner80ec2792009-08-02 00:34:36 +0000132 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000133}
134
Evan Chenga8e29892007-01-19 07:51:42 +0000135ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000136 : TargetLowering(TM, createTLOF(TM)), ARMPCLabelIndex(0) {
Evan Chenga8e29892007-01-19 07:51:42 +0000137 Subtarget = &TM.getSubtarget<ARMSubtarget>();
138
Evan Chengb1df8f22007-04-27 08:15:43 +0000139 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000140 // Uses VFP for Thumb libfuncs if available.
141 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
142 // Single-precision floating-point arithmetic.
143 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
144 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
145 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
146 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000147
Evan Chengb1df8f22007-04-27 08:15:43 +0000148 // Double-precision floating-point arithmetic.
149 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
150 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
151 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
152 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000153
Evan Chengb1df8f22007-04-27 08:15:43 +0000154 // Single-precision comparisons.
155 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
156 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
157 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
158 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
159 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
160 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
161 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
162 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000163
Evan Chengb1df8f22007-04-27 08:15:43 +0000164 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
165 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
166 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
167 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
168 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
169 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
170 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
171 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000172
Evan Chengb1df8f22007-04-27 08:15:43 +0000173 // Double-precision comparisons.
174 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
175 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
176 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
177 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
178 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
179 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
180 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
181 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000182
Evan Chengb1df8f22007-04-27 08:15:43 +0000183 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
184 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
185 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
186 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
187 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
188 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
189 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
190 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000191
Evan Chengb1df8f22007-04-27 08:15:43 +0000192 // Floating-point to integer conversions.
193 // i64 conversions are done via library routines even when generating VFP
194 // instructions, so use the same ones.
195 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
196 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
197 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
198 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000199
Evan Chengb1df8f22007-04-27 08:15:43 +0000200 // Conversions between floating types.
201 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
202 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
203
204 // Integer to floating-point conversions.
205 // i64 conversions are done via library routines even when generating VFP
206 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000207 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
208 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000209 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
210 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
211 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
212 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
213 }
Evan Chenga8e29892007-01-19 07:51:42 +0000214 }
215
Bob Wilson2f954612009-05-22 17:38:41 +0000216 // These libcalls are not available in 32-bit.
217 setLibcallName(RTLIB::SHL_I128, 0);
218 setLibcallName(RTLIB::SRL_I128, 0);
219 setLibcallName(RTLIB::SRA_I128, 0);
220
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000221 // Libcalls should use the AAPCS base standard ABI, even if hard float
222 // is in effect, as per the ARM RTABI specification, section 4.1.2.
223 if (Subtarget->isAAPCS_ABI()) {
224 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
225 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
226 CallingConv::ARM_AAPCS);
227 }
228 }
229
David Goodwinf1daf7d2009-07-08 23:10:31 +0000230 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000231 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000232 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000234 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
236 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000237
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000239 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000240
241 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000242 addDRTypeForNEON(MVT::v2f32);
243 addDRTypeForNEON(MVT::v8i8);
244 addDRTypeForNEON(MVT::v4i16);
245 addDRTypeForNEON(MVT::v2i32);
246 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000247
Owen Anderson825b72b2009-08-11 20:47:22 +0000248 addQRTypeForNEON(MVT::v4f32);
249 addQRTypeForNEON(MVT::v2f64);
250 addQRTypeForNEON(MVT::v16i8);
251 addQRTypeForNEON(MVT::v8i16);
252 addQRTypeForNEON(MVT::v4i32);
253 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000254
Bob Wilson74dc72e2009-09-15 23:55:57 +0000255 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
256 // neither Neon nor VFP support any arithmetic operations on it.
257 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
258 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
259 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
260 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
261 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
262 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
263 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
264 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
265 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
266 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
267 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
268 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
269 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
270 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
271 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
272 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
273 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
274 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
275 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
276 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
277 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
278 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
279 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
280 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
281
Bob Wilson642b3292009-09-16 00:32:15 +0000282 // Neon does not support some operations on v1i64 and v2i64 types.
283 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
284 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
285 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
286 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
287
Bob Wilson5bafff32009-06-22 23:27:02 +0000288 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
289 setTargetDAGCombine(ISD::SHL);
290 setTargetDAGCombine(ISD::SRL);
291 setTargetDAGCombine(ISD::SRA);
292 setTargetDAGCombine(ISD::SIGN_EXTEND);
293 setTargetDAGCombine(ISD::ZERO_EXTEND);
294 setTargetDAGCombine(ISD::ANY_EXTEND);
295 }
296
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000297 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000298
299 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000301
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000302 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000304
Evan Chenga8e29892007-01-19 07:51:42 +0000305 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000306 if (!Subtarget->isThumb1Only()) {
307 for (unsigned im = (unsigned)ISD::PRE_INC;
308 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setIndexedLoadAction(im, MVT::i1, Legal);
310 setIndexedLoadAction(im, MVT::i8, Legal);
311 setIndexedLoadAction(im, MVT::i16, Legal);
312 setIndexedLoadAction(im, MVT::i32, Legal);
313 setIndexedStoreAction(im, MVT::i1, Legal);
314 setIndexedStoreAction(im, MVT::i8, Legal);
315 setIndexedStoreAction(im, MVT::i16, Legal);
316 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000317 }
Evan Chenga8e29892007-01-19 07:51:42 +0000318 }
319
320 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000321 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::MUL, MVT::i64, Expand);
323 setOperationAction(ISD::MULHU, MVT::i32, Expand);
324 setOperationAction(ISD::MULHS, MVT::i32, Expand);
325 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
326 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000327 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000328 setOperationAction(ISD::MUL, MVT::i64, Expand);
329 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000330 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000331 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000332 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000333 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000334 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000335 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
336 setOperationAction(ISD::SRL, MVT::i64, Custom);
337 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000338
339 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000340 setOperationAction(ISD::ROTL, MVT::i32, Expand);
341 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
342 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000343 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000344 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000345
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000346 // Only ARMv6 has BSWAP.
347 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000348 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000349
Evan Chenga8e29892007-01-19 07:51:42 +0000350 // These are expanded into libcalls.
Owen Anderson825b72b2009-08-11 20:47:22 +0000351 setOperationAction(ISD::SDIV, MVT::i32, Expand);
352 setOperationAction(ISD::UDIV, MVT::i32, Expand);
353 setOperationAction(ISD::SREM, MVT::i32, Expand);
354 setOperationAction(ISD::UREM, MVT::i32, Expand);
355 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
356 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000357
Evan Chenga8e29892007-01-19 07:51:42 +0000358 // Support label based line numbers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000359 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
360 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000361
Owen Anderson825b72b2009-08-11 20:47:22 +0000362 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
363 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
364 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
365 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000366 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000367
Evan Chenga8e29892007-01-19 07:51:42 +0000368 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000369 setOperationAction(ISD::VASTART, MVT::Other, Custom);
370 setOperationAction(ISD::VAARG, MVT::Other, Expand);
371 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
372 setOperationAction(ISD::VAEND, MVT::Other, Expand);
373 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
374 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000375 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
376 // FIXME: Shouldn't need this, since no register is used, but the legalizer
377 // doesn't yet know how to not do that for SjLj.
378 setExceptionSelectorRegister(ARM::R0);
Evan Cheng86198642009-08-07 00:34:42 +0000379 if (Subtarget->isThumb())
Owen Anderson825b72b2009-08-11 20:47:22 +0000380 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Evan Cheng86198642009-08-07 00:34:42 +0000381 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
383 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000384
Evan Chengd27c9fc2009-07-03 01:43:10 +0000385 if (!Subtarget->hasV6Ops() && !Subtarget->isThumb2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000386 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
387 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000388 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000389 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000390
David Goodwinf1daf7d2009-07-08 23:10:31 +0000391 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
Evan Chengc7c77292008-11-04 19:57:48 +0000392 // Turn f64->i64 into FMRRD, i64 -> f64 to FMDRR iff target supports vfp2.
Owen Anderson825b72b2009-08-11 20:47:22 +0000393 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000394
395 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000396 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000397
Owen Anderson825b72b2009-08-11 20:47:22 +0000398 setOperationAction(ISD::SETCC, MVT::i32, Expand);
399 setOperationAction(ISD::SETCC, MVT::f32, Expand);
400 setOperationAction(ISD::SETCC, MVT::f64, Expand);
401 setOperationAction(ISD::SELECT, MVT::i32, Expand);
402 setOperationAction(ISD::SELECT, MVT::f32, Expand);
403 setOperationAction(ISD::SELECT, MVT::f64, Expand);
404 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
405 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
406 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000407
Owen Anderson825b72b2009-08-11 20:47:22 +0000408 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
409 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
410 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
411 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
412 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000413
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000414 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000415 setOperationAction(ISD::FSIN, MVT::f64, Expand);
416 setOperationAction(ISD::FSIN, MVT::f32, Expand);
417 setOperationAction(ISD::FCOS, MVT::f32, Expand);
418 setOperationAction(ISD::FCOS, MVT::f64, Expand);
419 setOperationAction(ISD::FREM, MVT::f64, Expand);
420 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000421 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000422 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
423 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000424 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 setOperationAction(ISD::FPOW, MVT::f64, Expand);
426 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000427
Evan Chenga8e29892007-01-19 07:51:42 +0000428 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
David Goodwinf1daf7d2009-07-08 23:10:31 +0000429 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000430 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
431 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
432 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
433 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000434 }
Evan Chenga8e29892007-01-19 07:51:42 +0000435
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000436 // We have target-specific dag combine patterns for the following nodes:
437 // ARMISD::FMRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000438 setTargetDAGCombine(ISD::ADD);
439 setTargetDAGCombine(ISD::SUB);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000440
Evan Chenga8e29892007-01-19 07:51:42 +0000441 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Chenga8e29892007-01-19 07:51:42 +0000442 setSchedulingPreference(SchedulingForRegPressure);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000443
Evan Chengbc9b7542009-08-15 07:59:10 +0000444 // FIXME: If-converter should use instruction latency to determine
445 // profitability rather than relying on fixed limits.
446 if (Subtarget->getCPUString() == "generic") {
447 // Generic (and overly aggressive) if-conversion limits.
448 setIfCvtBlockSizeLimit(10);
449 setIfCvtDupBlockSizeLimit(2);
450 } else if (Subtarget->hasV6Ops()) {
451 setIfCvtBlockSizeLimit(2);
452 setIfCvtDupBlockSizeLimit(1);
453 } else {
454 setIfCvtBlockSizeLimit(3);
455 setIfCvtDupBlockSizeLimit(2);
Evan Cheng8557c2b2009-06-19 01:51:50 +0000456 }
457
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000458 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Bob Wilsone6abdff2009-05-18 20:55:32 +0000459 // Do not enable CodePlacementOpt for now: it currently runs after the
460 // ARMConstantIslandPass and messes up branch relaxation and placement
461 // of constant islands.
462 // benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000463}
464
Evan Chenga8e29892007-01-19 07:51:42 +0000465const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
466 switch (Opcode) {
467 default: return 0;
468 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000469 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
470 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000471 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000472 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
473 case ARMISD::tCALL: return "ARMISD::tCALL";
474 case ARMISD::BRCOND: return "ARMISD::BRCOND";
475 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000476 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000477 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
478 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
479 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000480 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000481 case ARMISD::CMPFP: return "ARMISD::CMPFP";
482 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
483 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
484 case ARMISD::CMOV: return "ARMISD::CMOV";
485 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000486
Evan Chenga8e29892007-01-19 07:51:42 +0000487 case ARMISD::FTOSI: return "ARMISD::FTOSI";
488 case ARMISD::FTOUI: return "ARMISD::FTOUI";
489 case ARMISD::SITOF: return "ARMISD::SITOF";
490 case ARMISD::UITOF: return "ARMISD::UITOF";
Evan Chenga8e29892007-01-19 07:51:42 +0000491
492 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
493 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
494 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000495
Evan Chenga8e29892007-01-19 07:51:42 +0000496 case ARMISD::FMRRD: return "ARMISD::FMRRD";
497 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000498
Evan Chengc5942082009-10-28 06:55:03 +0000499 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
500 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
501
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000502 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000503
Evan Cheng86198642009-08-07 00:34:42 +0000504 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
505
Bob Wilson5bafff32009-06-22 23:27:02 +0000506 case ARMISD::VCEQ: return "ARMISD::VCEQ";
507 case ARMISD::VCGE: return "ARMISD::VCGE";
508 case ARMISD::VCGEU: return "ARMISD::VCGEU";
509 case ARMISD::VCGT: return "ARMISD::VCGT";
510 case ARMISD::VCGTU: return "ARMISD::VCGTU";
511 case ARMISD::VTST: return "ARMISD::VTST";
512
513 case ARMISD::VSHL: return "ARMISD::VSHL";
514 case ARMISD::VSHRs: return "ARMISD::VSHRs";
515 case ARMISD::VSHRu: return "ARMISD::VSHRu";
516 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
517 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
518 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
519 case ARMISD::VSHRN: return "ARMISD::VSHRN";
520 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
521 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
522 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
523 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
524 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
525 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
526 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
527 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
528 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
529 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
530 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
531 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
532 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
533 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000534 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000535 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000536 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000537 case ARMISD::VREV64: return "ARMISD::VREV64";
538 case ARMISD::VREV32: return "ARMISD::VREV32";
539 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000540 case ARMISD::VZIP: return "ARMISD::VZIP";
541 case ARMISD::VUZP: return "ARMISD::VUZP";
542 case ARMISD::VTRN: return "ARMISD::VTRN";
Evan Chenga8e29892007-01-19 07:51:42 +0000543 }
544}
545
Bill Wendlingb4202b82009-07-01 18:50:55 +0000546/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000547unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Evan Cheng048e36f2009-10-02 06:57:25 +0000548 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 0 : 1;
Bill Wendling20c568f2009-06-30 22:38:32 +0000549}
550
Evan Chenga8e29892007-01-19 07:51:42 +0000551//===----------------------------------------------------------------------===//
552// Lowering Code
553//===----------------------------------------------------------------------===//
554
Evan Chenga8e29892007-01-19 07:51:42 +0000555/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
556static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
557 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000558 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000559 case ISD::SETNE: return ARMCC::NE;
560 case ISD::SETEQ: return ARMCC::EQ;
561 case ISD::SETGT: return ARMCC::GT;
562 case ISD::SETGE: return ARMCC::GE;
563 case ISD::SETLT: return ARMCC::LT;
564 case ISD::SETLE: return ARMCC::LE;
565 case ISD::SETUGT: return ARMCC::HI;
566 case ISD::SETUGE: return ARMCC::HS;
567 case ISD::SETULT: return ARMCC::LO;
568 case ISD::SETULE: return ARMCC::LS;
569 }
570}
571
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000572/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
573static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000574 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000575 CondCode2 = ARMCC::AL;
576 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000577 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000578 case ISD::SETEQ:
579 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
580 case ISD::SETGT:
581 case ISD::SETOGT: CondCode = ARMCC::GT; break;
582 case ISD::SETGE:
583 case ISD::SETOGE: CondCode = ARMCC::GE; break;
584 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000585 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000586 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
587 case ISD::SETO: CondCode = ARMCC::VC; break;
588 case ISD::SETUO: CondCode = ARMCC::VS; break;
589 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
590 case ISD::SETUGT: CondCode = ARMCC::HI; break;
591 case ISD::SETUGE: CondCode = ARMCC::PL; break;
592 case ISD::SETLT:
593 case ISD::SETULT: CondCode = ARMCC::LT; break;
594 case ISD::SETLE:
595 case ISD::SETULE: CondCode = ARMCC::LE; break;
596 case ISD::SETNE:
597 case ISD::SETUNE: CondCode = ARMCC::NE; break;
598 }
Evan Chenga8e29892007-01-19 07:51:42 +0000599}
600
Bob Wilson1f595bb2009-04-17 19:07:39 +0000601//===----------------------------------------------------------------------===//
602// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000603//===----------------------------------------------------------------------===//
604
605#include "ARMGenCallingConv.inc"
606
607// APCS f64 is in register pairs, possibly split to stack
Owen Andersone50ed302009-08-10 22:56:29 +0000608static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000609 CCValAssign::LocInfo &LocInfo,
610 CCState &State, bool CanFail) {
611 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
612
613 // Try to get the first register.
614 if (unsigned Reg = State.AllocateReg(RegList, 4))
615 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
616 else {
617 // For the 2nd half of a v2f64, do not fail.
618 if (CanFail)
619 return false;
620
621 // Put the whole thing on the stack.
622 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
623 State.AllocateStack(8, 4),
624 LocVT, LocInfo));
625 return true;
626 }
627
628 // Try to get the second register.
629 if (unsigned Reg = State.AllocateReg(RegList, 4))
630 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
631 else
632 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
633 State.AllocateStack(4, 4),
634 LocVT, LocInfo));
635 return true;
636}
637
Owen Andersone50ed302009-08-10 22:56:29 +0000638static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000639 CCValAssign::LocInfo &LocInfo,
640 ISD::ArgFlagsTy &ArgFlags,
641 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000642 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
643 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000644 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000645 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
646 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000647 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000648}
649
650// AAPCS f64 is in aligned register pairs
Owen Andersone50ed302009-08-10 22:56:29 +0000651static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000652 CCValAssign::LocInfo &LocInfo,
653 CCState &State, bool CanFail) {
654 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
655 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
656
657 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
658 if (Reg == 0) {
659 // For the 2nd half of a v2f64, do not just fail.
660 if (CanFail)
661 return false;
662
663 // Put the whole thing on the stack.
664 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
665 State.AllocateStack(8, 8),
666 LocVT, LocInfo));
667 return true;
668 }
669
670 unsigned i;
671 for (i = 0; i < 2; ++i)
672 if (HiRegList[i] == Reg)
673 break;
674
675 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
676 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
677 LocVT, LocInfo));
678 return true;
679}
680
Owen Andersone50ed302009-08-10 22:56:29 +0000681static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000682 CCValAssign::LocInfo &LocInfo,
683 ISD::ArgFlagsTy &ArgFlags,
684 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000685 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
686 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000687 if (LocVT == MVT::v2f64 &&
Bob Wilson5bafff32009-06-22 23:27:02 +0000688 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
689 return false;
690 return true; // we handled it
691}
692
Owen Andersone50ed302009-08-10 22:56:29 +0000693static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson5bafff32009-06-22 23:27:02 +0000694 CCValAssign::LocInfo &LocInfo, CCState &State) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000695 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
696 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
697
Bob Wilsone65586b2009-04-17 20:40:45 +0000698 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
699 if (Reg == 0)
700 return false; // we didn't handle it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000701
Bob Wilsone65586b2009-04-17 20:40:45 +0000702 unsigned i;
703 for (i = 0; i < 2; ++i)
704 if (HiRegList[i] == Reg)
705 break;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000706
Bob Wilson5bafff32009-06-22 23:27:02 +0000707 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bob Wilsone65586b2009-04-17 20:40:45 +0000708 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000709 LocVT, LocInfo));
710 return true;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000711}
712
Owen Andersone50ed302009-08-10 22:56:29 +0000713static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000714 CCValAssign::LocInfo &LocInfo,
715 ISD::ArgFlagsTy &ArgFlags,
716 CCState &State) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000717 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
718 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000719 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
Bob Wilson5bafff32009-06-22 23:27:02 +0000720 return false;
Bob Wilsone65586b2009-04-17 20:40:45 +0000721 return true; // we handled it
Bob Wilson1f595bb2009-04-17 19:07:39 +0000722}
723
Owen Andersone50ed302009-08-10 22:56:29 +0000724static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000725 CCValAssign::LocInfo &LocInfo,
726 ISD::ArgFlagsTy &ArgFlags,
727 CCState &State) {
728 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
729 State);
730}
731
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000732/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
733/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000734CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000735 bool Return,
736 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000737 switch (CC) {
738 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000739 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000740 case CallingConv::C:
741 case CallingConv::Fast:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000742 // Use target triple & subtarget features to do actual dispatch.
743 if (Subtarget->isAAPCS_ABI()) {
744 if (Subtarget->hasVFP2() &&
745 FloatABIType == FloatABI::Hard && !isVarArg)
746 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
747 else
748 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
749 } else
750 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000751 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000752 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000753 case CallingConv::ARM_AAPCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000754 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000755 case CallingConv::ARM_APCS:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000756 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000757 }
758}
759
Dan Gohman98ca4f22009-08-05 01:29:28 +0000760/// LowerCallResult - Lower the result values of a call into the
761/// appropriate copies out of appropriate physical registers.
762SDValue
763ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000764 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000765 const SmallVectorImpl<ISD::InputArg> &Ins,
766 DebugLoc dl, SelectionDAG &DAG,
767 SmallVectorImpl<SDValue> &InVals) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000768
Bob Wilson1f595bb2009-04-17 19:07:39 +0000769 // Assign locations to each value returned by this call.
770 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000771 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000772 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000773 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000774 CCAssignFnForNode(CallConv, /* Return*/ true,
775 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000776
777 // Copy all of the result registers out of their specified physreg.
778 for (unsigned i = 0; i != RVLocs.size(); ++i) {
779 CCValAssign VA = RVLocs[i];
780
Bob Wilson80915242009-04-25 00:33:20 +0000781 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000782 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000783 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000784 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +0000785 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000786 Chain = Lo.getValue(1);
787 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000788 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000789 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000790 InFlag);
791 Chain = Hi.getValue(1);
792 InFlag = Hi.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +0000793 Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +0000794
Owen Anderson825b72b2009-08-11 20:47:22 +0000795 if (VA.getLocVT() == MVT::v2f64) {
796 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
797 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
798 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000799
800 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000801 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000802 Chain = Lo.getValue(1);
803 InFlag = Lo.getValue(2);
804 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +0000805 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +0000806 Chain = Hi.getValue(1);
807 InFlag = Hi.getValue(2);
Owen Anderson825b72b2009-08-11 20:47:22 +0000808 Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
809 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
810 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +0000811 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000812 } else {
Bob Wilson80915242009-04-25 00:33:20 +0000813 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
814 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +0000815 Chain = Val.getValue(1);
816 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000817 }
Bob Wilson80915242009-04-25 00:33:20 +0000818
819 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000820 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +0000821 case CCValAssign::Full: break;
822 case CCValAssign::BCvt:
823 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
824 break;
825 }
826
Dan Gohman98ca4f22009-08-05 01:29:28 +0000827 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000828 }
829
Dan Gohman98ca4f22009-08-05 01:29:28 +0000830 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000831}
832
833/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
834/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +0000835/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +0000836/// a byval function parameter.
837/// Sometimes what we are copying is the end of a larger object, the part that
838/// does not fit in registers.
839static SDValue
840CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
841 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
842 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000843 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000844 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
845 /*AlwaysInline=*/false, NULL, 0, NULL, 0);
846}
847
Bob Wilsondee46d72009-04-17 20:35:10 +0000848/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000849SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000850ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
851 SDValue StackPtr, SDValue Arg,
852 DebugLoc dl, SelectionDAG &DAG,
853 const CCValAssign &VA,
854 ISD::ArgFlagsTy Flags) {
Bob Wilson1f595bb2009-04-17 19:07:39 +0000855 unsigned LocMemOffset = VA.getLocMemOffset();
856 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
857 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
858 if (Flags.isByVal()) {
859 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
860 }
861 return DAG.getStore(Chain, dl, Arg, PtrOff,
862 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chenga8e29892007-01-19 07:51:42 +0000863}
864
Dan Gohman98ca4f22009-08-05 01:29:28 +0000865void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +0000866 SDValue Chain, SDValue &Arg,
867 RegsToPassVector &RegsToPass,
868 CCValAssign &VA, CCValAssign &NextVA,
869 SDValue &StackPtr,
870 SmallVector<SDValue, 8> &MemOpChains,
871 ISD::ArgFlagsTy Flags) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000872
873 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000874 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +0000875 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
876
877 if (NextVA.isRegLoc())
878 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
879 else {
880 assert(NextVA.isMemLoc());
881 if (StackPtr.getNode() == 0)
882 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
883
Dan Gohman98ca4f22009-08-05 01:29:28 +0000884 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
885 dl, DAG, NextVA,
886 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000887 }
888}
889
Dan Gohman98ca4f22009-08-05 01:29:28 +0000890/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +0000891/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
892/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000893SDValue
894ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000895 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000896 bool isTailCall,
897 const SmallVectorImpl<ISD::OutputArg> &Outs,
898 const SmallVectorImpl<ISD::InputArg> &Ins,
899 DebugLoc dl, SelectionDAG &DAG,
900 SmallVectorImpl<SDValue> &InVals) {
Evan Chenga8e29892007-01-19 07:51:42 +0000901
Bob Wilson1f595bb2009-04-17 19:07:39 +0000902 // Analyze operands of the call, assigning locations to each operand.
903 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000904 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
905 *DAG.getContext());
906 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000907 CCAssignFnForNode(CallConv, /* Return*/ false,
908 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +0000909
Bob Wilson1f595bb2009-04-17 19:07:39 +0000910 // Get a count of how many bytes are to be pushed on the stack.
911 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000912
913 // Adjust the stack pointer for the new arguments...
914 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000915 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000916
Owen Anderson825b72b2009-08-11 20:47:22 +0000917 SDValue StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000918
Bob Wilson5bafff32009-06-22 23:27:02 +0000919 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +0000920 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000921
Bob Wilson1f595bb2009-04-17 19:07:39 +0000922 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +0000923 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +0000924 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
925 i != e;
926 ++i, ++realArgIdx) {
927 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000928 SDValue Arg = Outs[realArgIdx].Val;
929 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +0000930
Bob Wilson1f595bb2009-04-17 19:07:39 +0000931 // Promote the value if needed.
932 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000933 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +0000934 case CCValAssign::Full: break;
935 case CCValAssign::SExt:
936 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
937 break;
938 case CCValAssign::ZExt:
939 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
940 break;
941 case CCValAssign::AExt:
942 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
943 break;
944 case CCValAssign::BCvt:
945 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
946 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000947 }
948
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000949 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +0000950 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000951 if (VA.getLocVT() == MVT::v2f64) {
952 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
953 DAG.getConstant(0, MVT::i32));
954 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
955 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000956
Dan Gohman98ca4f22009-08-05 01:29:28 +0000957 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000958 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
959
960 VA = ArgLocs[++i]; // skip ahead to next loc
961 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000962 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +0000963 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
964 } else {
965 assert(VA.isMemLoc());
966 if (StackPtr.getNode() == 0)
967 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
968
Dan Gohman98ca4f22009-08-05 01:29:28 +0000969 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
970 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +0000971 }
972 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000973 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +0000974 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +0000975 }
976 } else if (VA.isRegLoc()) {
977 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
978 } else {
979 assert(VA.isMemLoc());
980 if (StackPtr.getNode() == 0)
981 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
982
Dan Gohman98ca4f22009-08-05 01:29:28 +0000983 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
984 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +0000985 }
Evan Chenga8e29892007-01-19 07:51:42 +0000986 }
987
988 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000989 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +0000990 &MemOpChains[0], MemOpChains.size());
991
992 // Build a sequence of copy-to-reg nodes chained together with token chain
993 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +0000994 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +0000995 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +0000996 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000997 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000998 InFlag = Chain.getValue(1);
999 }
1000
Bill Wendling056292f2008-09-16 21:48:12 +00001001 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1002 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1003 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001004 bool isDirect = false;
1005 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001006 bool isLocalARMFunc = false;
Evan Chenga8e29892007-01-19 07:51:42 +00001007 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1008 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001009 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001010 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001011 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001012 getTargetMachine().getRelocationModel() != Reloc::Static;
1013 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001014 // ARM call to a local ARM function is predicable.
1015 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +00001016 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001017 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge4e4ed32009-08-28 23:18:09 +00001018 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001019 ARMPCLabelIndex,
1020 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001021 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001022 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001023 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001024 DAG.getEntryNode(), CPAddr,
1025 PseudoSourceValue::getConstantPool(), 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001026 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001027 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001028 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001029 } else
1030 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001031 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001032 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001033 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001034 getTargetMachine().getRelocationModel() != Reloc::Static;
1035 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001036 // tBX takes a register source operand.
1037 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001038 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Owen Anderson1d0be152009-08-13 21:58:54 +00001039 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001040 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001041 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001042 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001043 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001044 DAG.getEntryNode(), CPAddr,
1045 PseudoSourceValue::getConstantPool(), 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001046 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001047 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001048 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001049 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001050 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001051 }
1052
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001053 // FIXME: handle tail calls differently.
1054 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001055 if (Subtarget->isThumb()) {
1056 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001057 CallOpc = ARMISD::CALL_NOLINK;
1058 else
1059 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1060 } else {
1061 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001062 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1063 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001064 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001065 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001066 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001067 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001068 InFlag = Chain.getValue(1);
1069 }
1070
Dan Gohman475871a2008-07-27 21:46:04 +00001071 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001072 Ops.push_back(Chain);
1073 Ops.push_back(Callee);
1074
1075 // Add argument registers to the end of the list so that they are known live
1076 // into the call.
1077 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1078 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1079 RegsToPass[i].second.getValueType()));
1080
Gabor Greifba36cb52008-08-28 21:40:38 +00001081 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001082 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +00001083 // Returns a chain and a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00001084 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +00001085 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001086 InFlag = Chain.getValue(1);
1087
Chris Lattnere563bbc2008-10-11 22:08:30 +00001088 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1089 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001090 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001091 InFlag = Chain.getValue(1);
1092
Bob Wilson1f595bb2009-04-17 19:07:39 +00001093 // Handle result values, copying them out of physregs into vregs that we
1094 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001095 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1096 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001097}
1098
Dan Gohman98ca4f22009-08-05 01:29:28 +00001099SDValue
1100ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001101 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001102 const SmallVectorImpl<ISD::OutputArg> &Outs,
1103 DebugLoc dl, SelectionDAG &DAG) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001104
Bob Wilsondee46d72009-04-17 20:35:10 +00001105 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001106 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001107
Bob Wilsondee46d72009-04-17 20:35:10 +00001108 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001109 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1110 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001111
Dan Gohman98ca4f22009-08-05 01:29:28 +00001112 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001113 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1114 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001115
1116 // If this is the first return lowered for this function, add
1117 // the regs to the liveout set for the function.
1118 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1119 for (unsigned i = 0; i != RVLocs.size(); ++i)
1120 if (RVLocs[i].isRegLoc())
1121 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001122 }
1123
Bob Wilson1f595bb2009-04-17 19:07:39 +00001124 SDValue Flag;
1125
1126 // Copy the result values into the output registers.
1127 for (unsigned i = 0, realRVLocIdx = 0;
1128 i != RVLocs.size();
1129 ++i, ++realRVLocIdx) {
1130 CCValAssign &VA = RVLocs[i];
1131 assert(VA.isRegLoc() && "Can only return in registers!");
1132
Dan Gohman98ca4f22009-08-05 01:29:28 +00001133 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001134
1135 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001136 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001137 case CCValAssign::Full: break;
1138 case CCValAssign::BCvt:
1139 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1140 break;
1141 }
1142
Bob Wilson1f595bb2009-04-17 19:07:39 +00001143 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001144 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001145 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001146 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1147 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001148 SDValue HalfGPRs = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001149 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001150
1151 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1152 Flag = Chain.getValue(1);
1153 VA = RVLocs[++i]; // skip ahead to next loc
1154 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1155 HalfGPRs.getValue(1), Flag);
1156 Flag = Chain.getValue(1);
1157 VA = RVLocs[++i]; // skip ahead to next loc
1158
1159 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001160 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1161 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001162 }
1163 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1164 // available.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001165 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001166 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001167 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001168 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001169 VA = RVLocs[++i]; // skip ahead to next loc
1170 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1171 Flag);
1172 } else
1173 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1174
Bob Wilsondee46d72009-04-17 20:35:10 +00001175 // Guarantee that all emitted copies are
1176 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001177 Flag = Chain.getValue(1);
1178 }
1179
1180 SDValue result;
1181 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001182 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001183 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001184 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001185
1186 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001187}
1188
Bob Wilsonddb16df2009-10-30 05:45:42 +00001189// ConstantPool, BlockAddress, JumpTable, GlobalAddress, and ExternalSymbol are
1190// lowered as their target counterpart wrapped in the ARMISD::Wrapper
1191// node. Suppose N is one of the above mentioned nodes. It has to be wrapped
1192// because otherwise Select(N) returns N. So the raw TargetGlobalAddress
1193// nodes, etc. can only be used to form addressing mode. These wrapped nodes
1194// will be selected into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001195static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001196 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001197 // FIXME there is no actual debug info here
1198 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001199 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001200 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001201 if (CP->isMachineConstantPoolEntry())
1202 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1203 CP->getAlignment());
1204 else
1205 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1206 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001207 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001208}
1209
Bob Wilsonddb16df2009-10-30 05:45:42 +00001210SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) {
1211 DebugLoc DL = Op.getDebugLoc();
1212 BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1213 SDValue Result = DAG.getBlockAddress(BA, DL, /*isTarget=*/true);
1214 return DAG.getNode(ARMISD::Wrapper, DL, getPointerTy(), Result);
1215}
1216
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001217// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001218SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001219ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1220 SelectionDAG &DAG) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001221 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001222 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001223 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1224 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001225 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001226 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001227 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001228 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001229 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
1230 PseudoSourceValue::getConstantPool(), 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001231 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001232
Owen Anderson825b72b2009-08-11 20:47:22 +00001233 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001234 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001235
1236 // call __tls_get_addr.
1237 ArgListTy Args;
1238 ArgListEntry Entry;
1239 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001240 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001241 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001242 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001243 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001244 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1245 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001246 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001247 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001248 return CallResult.first;
1249}
1250
1251// Lower ISD::GlobalTLSAddress using the "initial exec" or
1252// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001253SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001254ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001255 SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001256 GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001257 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001258 SDValue Offset;
1259 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001260 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001261 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001262 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001263
Chris Lattner4fb63d02009-07-15 04:12:33 +00001264 if (GV->isDeclaration()) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001265 // initial exec model
1266 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1267 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001268 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001269 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001270 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001271 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001272 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
1273 PseudoSourceValue::getConstantPool(), 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001274 Chain = Offset.getValue(1);
1275
Owen Anderson825b72b2009-08-11 20:47:22 +00001276 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001277 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001278
Evan Cheng9eda6892009-10-31 03:39:36 +00001279 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
1280 PseudoSourceValue::getConstantPool(), 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001281 } else {
1282 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001283 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001284 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001285 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001286 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
1287 PseudoSourceValue::getConstantPool(), 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001288 }
1289
1290 // The address of the thread local variable is the add of the thread
1291 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001292 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001293}
1294
Dan Gohman475871a2008-07-27 21:46:04 +00001295SDValue
1296ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001297 // TODO: implement the "local dynamic" model
1298 assert(Subtarget->isTargetELF() &&
1299 "TLS not implemented for non-ELF targets");
1300 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1301 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1302 // otherwise use the "Local Exec" TLS Model
1303 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1304 return LowerToTLSGeneralDynamicModel(GA, DAG);
1305 else
1306 return LowerToTLSExecModels(GA, DAG);
1307}
1308
Dan Gohman475871a2008-07-27 21:46:04 +00001309SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001310 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001311 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001312 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001313 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1314 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1315 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001316 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001317 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001318 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001319 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001320 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001321 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001322 CPAddr,
1323 PseudoSourceValue::getConstantPool(), 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001324 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001325 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001326 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001327 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001328 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
1329 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001330 return Result;
1331 } else {
Evan Cheng1606e8e2009-03-13 07:51:59 +00001332 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001333 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001334 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1335 PseudoSourceValue::getConstantPool(), 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001336 }
1337}
1338
Dan Gohman475871a2008-07-27 21:46:04 +00001339SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001340 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001341 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001342 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001343 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1344 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001345 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001346 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001347 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001348 else {
Evan Chenge4e4ed32009-08-28 23:18:09 +00001349 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1350 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001351 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001352 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001353 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001354 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001355
Evan Cheng9eda6892009-10-31 03:39:36 +00001356 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1357 PseudoSourceValue::getConstantPool(), 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001358 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001359
1360 if (RelocM == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001361 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001362 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001363 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001364
Evan Cheng63476a82009-09-03 07:04:02 +00001365 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Evan Cheng9eda6892009-10-31 03:39:36 +00001366 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
1367 PseudoSourceValue::getGOT(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001368
1369 return Result;
1370}
1371
Dan Gohman475871a2008-07-27 21:46:04 +00001372SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001373 SelectionDAG &DAG){
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001374 assert(Subtarget->isTargetELF() &&
1375 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Owen Andersone50ed302009-08-10 22:56:29 +00001376 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001377 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001378 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001379 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1380 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001381 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001382 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001383 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001384 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1385 PseudoSourceValue::getConstantPool(), 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001386 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001387 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001388}
1389
Jim Grosbach0e0da732009-05-12 23:59:14 +00001390SDValue
1391ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001392 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001393 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001394 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001395 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001396 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001397 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001398 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1399 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001400 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001401 MachineFunction &MF = DAG.getMachineFunction();
1402 EVT PtrVT = getPointerTy();
1403 DebugLoc dl = Op.getDebugLoc();
1404 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1405 SDValue CPAddr;
1406 unsigned PCAdj = (RelocM != Reloc::PIC_)
1407 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001408 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001409 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1410 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001411 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001412 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001413 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00001414 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1415 PseudoSourceValue::getConstantPool(), 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001416 SDValue Chain = Result.getValue(1);
1417
1418 if (RelocM == Reloc::PIC_) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001419 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001420 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1421 }
1422 return Result;
1423 }
Jim Grosbachf9570122009-05-14 00:46:35 +00001424 case Intrinsic::eh_sjlj_setjmp:
Owen Anderson825b72b2009-08-11 20:47:22 +00001425 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1));
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001426 }
1427}
1428
Dan Gohman475871a2008-07-27 21:46:04 +00001429static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001430 unsigned VarArgsFrameIndex) {
Evan Chenga8e29892007-01-19 07:51:42 +00001431 // vastart just stores the address of the VarArgsFrameIndex slot into the
1432 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001433 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001434 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001435 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001436 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001437 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001438}
1439
Dan Gohman475871a2008-07-27 21:46:04 +00001440SDValue
Evan Cheng86198642009-08-07 00:34:42 +00001441ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
1442 SDNode *Node = Op.getNode();
1443 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001444 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001445 SDValue Chain = Op.getOperand(0);
1446 SDValue Size = Op.getOperand(1);
1447 SDValue Align = Op.getOperand(2);
1448
1449 // Chain the dynamic stack allocation so that it doesn't modify the stack
1450 // pointer when other instructions are using the stack.
1451 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1452
1453 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1454 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1455 if (AlignVal > StackAlign)
1456 // Do this now since selection pass cannot introduce new target
1457 // independent node.
1458 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1459
1460 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1461 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1462 // do even more horrible hack later.
1463 MachineFunction &MF = DAG.getMachineFunction();
1464 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1465 if (AFI->isThumb1OnlyFunction()) {
1466 bool Negate = true;
1467 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1468 if (C) {
1469 uint32_t Val = C->getZExtValue();
1470 if (Val <= 508 && ((Val & 3) == 0))
1471 Negate = false;
1472 }
1473 if (Negate)
1474 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1475 }
1476
Owen Anderson825b72b2009-08-11 20:47:22 +00001477 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001478 SDValue Ops1[] = { Chain, Size, Align };
1479 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1480 Chain = Res.getValue(1);
1481 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1482 DAG.getIntPtrConstant(0, true), SDValue());
1483 SDValue Ops2[] = { Res, Chain };
1484 return DAG.getMergeValues(Ops2, 2, dl);
1485}
1486
1487SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001488ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1489 SDValue &Root, SelectionDAG &DAG,
1490 DebugLoc dl) {
1491 MachineFunction &MF = DAG.getMachineFunction();
1492 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1493
1494 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001495 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001496 RC = ARM::tGPRRegisterClass;
1497 else
1498 RC = ARM::GPRRegisterClass;
1499
1500 // Transform the arguments stored in physical registers into virtual ones.
1501 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001502 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001503
1504 SDValue ArgValue2;
1505 if (NextVA.isMemLoc()) {
1506 unsigned ArgSize = NextVA.getLocVT().getSizeInBits()/8;
1507 MachineFrameInfo *MFI = MF.getFrameInfo();
1508 int FI = MFI->CreateFixedObject(ArgSize, NextVA.getLocMemOffset());
1509
1510 // Create load node to retrieve arguments from the stack.
1511 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001512 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
1513 PseudoSourceValue::getFixedStack(FI), 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001514 } else {
1515 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001516 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001517 }
1518
Owen Anderson825b72b2009-08-11 20:47:22 +00001519 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001520}
1521
1522SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001523ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001524 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001525 const SmallVectorImpl<ISD::InputArg>
1526 &Ins,
1527 DebugLoc dl, SelectionDAG &DAG,
1528 SmallVectorImpl<SDValue> &InVals) {
1529
Bob Wilson1f595bb2009-04-17 19:07:39 +00001530 MachineFunction &MF = DAG.getMachineFunction();
1531 MachineFrameInfo *MFI = MF.getFrameInfo();
1532
Bob Wilson1f595bb2009-04-17 19:07:39 +00001533 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1534
1535 // Assign locations to all of the incoming arguments.
1536 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001537 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1538 *DAG.getContext());
1539 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001540 CCAssignFnForNode(CallConv, /* Return*/ false,
1541 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001542
1543 SmallVector<SDValue, 16> ArgValues;
1544
1545 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1546 CCValAssign &VA = ArgLocs[i];
1547
Bob Wilsondee46d72009-04-17 20:35:10 +00001548 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001549 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001550 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00001551
Bob Wilson5bafff32009-06-22 23:27:02 +00001552 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001553 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001554 // f64 and vector types are split up into multiple registers or
1555 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00001556 RegVT = MVT::i32;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001557
Owen Anderson825b72b2009-08-11 20:47:22 +00001558 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001559 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001560 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00001561 VA = ArgLocs[++i]; // skip ahead to next loc
1562 SDValue ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001563 Chain, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001564 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1565 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001566 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00001567 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001568 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1569 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001570 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001571
Bob Wilson5bafff32009-06-22 23:27:02 +00001572 } else {
1573 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001574
Owen Anderson825b72b2009-08-11 20:47:22 +00001575 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00001576 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001577 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00001578 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001579 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001580 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001581 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001582 RC = (AFI->isThumb1OnlyFunction() ?
1583 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00001584 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001585 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00001586
1587 // Transform the arguments in physical registers into virtual ones.
1588 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001589 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001590 }
1591
1592 // If this is an 8 or 16-bit value, it is really passed promoted
1593 // to 32 bits. Insert an assert[sz]ext to capture this, then
1594 // truncate to the right size.
1595 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001596 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001597 case CCValAssign::Full: break;
1598 case CCValAssign::BCvt:
1599 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1600 break;
1601 case CCValAssign::SExt:
1602 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1603 DAG.getValueType(VA.getValVT()));
1604 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1605 break;
1606 case CCValAssign::ZExt:
1607 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1608 DAG.getValueType(VA.getValVT()));
1609 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1610 break;
1611 }
1612
Dan Gohman98ca4f22009-08-05 01:29:28 +00001613 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001614
1615 } else { // VA.isRegLoc()
1616
1617 // sanity check
1618 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00001619 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001620
1621 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1622 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset());
1623
Bob Wilsondee46d72009-04-17 20:35:10 +00001624 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001625 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001626 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
1627 PseudoSourceValue::getFixedStack(FI), 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001628 }
1629 }
1630
1631 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001632 if (isVarArg) {
1633 static const unsigned GPRArgRegs[] = {
1634 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1635 };
1636
Bob Wilsondee46d72009-04-17 20:35:10 +00001637 unsigned NumGPRs = CCInfo.getFirstUnallocated
1638 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001639
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001640 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1641 unsigned VARegSize = (4 - NumGPRs) * 4;
1642 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00001643 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001644 if (VARegSaveSize) {
1645 // If this function is vararg, store any remaining integer argument regs
1646 // to their spots on the stack so that they may be loaded by deferencing
1647 // the result of va_next.
1648 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001649 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1650 VARegSaveSize - VARegSize);
Dan Gohman475871a2008-07-27 21:46:04 +00001651 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001652
Dan Gohman475871a2008-07-27 21:46:04 +00001653 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001654 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001655 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001656 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001657 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001658 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001659 RC = ARM::GPRRegisterClass;
1660
Bob Wilson998e1252009-04-20 18:36:57 +00001661 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001662 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Evan Cheng9eda6892009-10-31 03:39:36 +00001663 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
1664 PseudoSourceValue::getFixedStack(VarArgsFrameIndex), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001665 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001666 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001667 DAG.getConstant(4, getPointerTy()));
1668 }
1669 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001670 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001671 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001672 } else
1673 // This will point to the next argument passed via stack.
1674 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1675 }
1676
Dan Gohman98ca4f22009-08-05 01:29:28 +00001677 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00001678}
1679
1680/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001681static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001682 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001683 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001684 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001685 // Maybe this has already been legalized into the constant pool?
1686 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001687 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001688 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1689 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001690 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001691 }
1692 }
1693 return false;
1694}
1695
David Goodwinf1daf7d2009-07-08 23:10:31 +00001696static bool isLegalCmpImmediate(unsigned C, bool isThumb1Only) {
1697 return ( isThumb1Only && (C & ~255U) == 0) ||
1698 (!isThumb1Only && ARM_AM::getSOImmVal(C) != -1);
Evan Chenga8e29892007-01-19 07:51:42 +00001699}
1700
1701/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1702/// the given operands.
Dan Gohman475871a2008-07-27 21:46:04 +00001703static SDValue getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
David Goodwinf1daf7d2009-07-08 23:10:31 +00001704 SDValue &ARMCC, SelectionDAG &DAG, bool isThumb1Only,
Dale Johannesende064702009-02-06 21:50:26 +00001705 DebugLoc dl) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001706 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001707 unsigned C = RHSC->getZExtValue();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001708 if (!isLegalCmpImmediate(C, isThumb1Only)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001709 // Constant does not fit, try adjusting it by one?
1710 switch (CC) {
1711 default: break;
1712 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001713 case ISD::SETGE:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001714 if (isLegalCmpImmediate(C-1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001715 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001716 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001717 }
1718 break;
1719 case ISD::SETULT:
1720 case ISD::SETUGE:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001721 if (C > 0 && isLegalCmpImmediate(C-1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001722 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001723 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001724 }
1725 break;
1726 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001727 case ISD::SETGT:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001728 if (isLegalCmpImmediate(C+1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001729 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001730 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001731 }
1732 break;
1733 case ISD::SETULE:
1734 case ISD::SETUGT:
David Goodwinf1daf7d2009-07-08 23:10:31 +00001735 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb1Only)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001736 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001737 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001738 }
1739 break;
1740 }
1741 }
1742 }
1743
1744 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001745 ARMISD::NodeType CompareType;
1746 switch (CondCode) {
1747 default:
1748 CompareType = ARMISD::CMP;
1749 break;
1750 case ARMCC::EQ:
1751 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00001752 // Uses only Z Flag
1753 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001754 break;
1755 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001756 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1757 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001758}
1759
1760/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001761static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001762 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001763 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001764 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00001765 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001766 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001767 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1768 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001769}
1770
Dan Gohman475871a2008-07-27 21:46:04 +00001771static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001772 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00001773 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001774 SDValue LHS = Op.getOperand(0);
1775 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001776 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001777 SDValue TrueVal = Op.getOperand(2);
1778 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001779 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001780
Owen Anderson825b72b2009-08-11 20:47:22 +00001781 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001782 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001783 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
David Goodwinf1daf7d2009-07-08 23:10:31 +00001784 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
Dale Johannesende064702009-02-06 21:50:26 +00001785 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001786 }
1787
1788 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001789 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00001790
Owen Anderson825b72b2009-08-11 20:47:22 +00001791 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1792 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001793 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1794 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001795 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001796 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001797 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001798 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00001799 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001800 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00001801 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001802 }
1803 return Result;
1804}
1805
Dan Gohman475871a2008-07-27 21:46:04 +00001806static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001807 const ARMSubtarget *ST) {
Dan Gohman475871a2008-07-27 21:46:04 +00001808 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001809 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001810 SDValue LHS = Op.getOperand(2);
1811 SDValue RHS = Op.getOperand(3);
1812 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00001813 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001814
Owen Anderson825b72b2009-08-11 20:47:22 +00001815 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001816 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001817 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
David Goodwinf1daf7d2009-07-08 23:10:31 +00001818 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001819 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00001820 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001821 }
1822
Owen Anderson825b72b2009-08-11 20:47:22 +00001823 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00001824 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001825 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001826
Dale Johannesende064702009-02-06 21:50:26 +00001827 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001828 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1829 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1830 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001831 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00001832 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001833 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001834 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001835 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00001836 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001837 }
1838 return Res;
1839}
1840
Dan Gohman475871a2008-07-27 21:46:04 +00001841SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) {
1842 SDValue Chain = Op.getOperand(0);
1843 SDValue Table = Op.getOperand(1);
1844 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001845 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001846
Owen Andersone50ed302009-08-10 22:56:29 +00001847 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00001848 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1849 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00001850 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00001851 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00001852 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00001853 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
1854 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00001855 if (Subtarget->isThumb2()) {
1856 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
1857 // which does another jump to the destination. This also makes it easier
1858 // to translate it to TBB / TBH later.
1859 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00001860 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00001861 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001862 }
Evan Cheng66ac5312009-07-25 00:33:29 +00001863 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00001864 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
1865 PseudoSourceValue::getJumpTable(), 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00001866 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001867 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00001868 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001869 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00001870 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
1871 PseudoSourceValue::getJumpTable(), 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00001872 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00001873 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001874 }
Evan Chenga8e29892007-01-19 07:51:42 +00001875}
1876
Dan Gohman475871a2008-07-27 21:46:04 +00001877static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00001878 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001879 unsigned Opc =
1880 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
Owen Anderson825b72b2009-08-11 20:47:22 +00001881 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
1882 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001883}
1884
Dan Gohman475871a2008-07-27 21:46:04 +00001885static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001886 EVT VT = Op.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001887 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001888 unsigned Opc =
1889 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1890
Owen Anderson825b72b2009-08-11 20:47:22 +00001891 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
Dale Johannesende064702009-02-06 21:50:26 +00001892 return DAG.getNode(Opc, dl, VT, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001893}
1894
Dan Gohman475871a2008-07-27 21:46:04 +00001895static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001896 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00001897 SDValue Tmp0 = Op.getOperand(0);
1898 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00001899 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001900 EVT VT = Op.getValueType();
1901 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001902 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
1903 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001904 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1905 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001906 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001907}
1908
Jim Grosbach0e0da732009-05-12 23:59:14 +00001909SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
1910 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1911 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00001912 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001913 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
1914 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00001915 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00001916 ? ARM::R7 : ARM::R11;
1917 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
1918 while (Depth--)
1919 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
1920 return FrameAddr;
1921}
1922
Dan Gohman475871a2008-07-27 21:46:04 +00001923SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00001924ARMTargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001925 SDValue Chain,
1926 SDValue Dst, SDValue Src,
1927 SDValue Size, unsigned Align,
Dan Gohman707e0182008-04-12 04:36:06 +00001928 bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00001929 const Value *DstSV, uint64_t DstSVOff,
1930 const Value *SrcSV, uint64_t SrcSVOff){
Evan Cheng4102eb52007-10-22 22:11:27 +00001931 // Do repeated 4-byte loads and stores. To be improved.
Dan Gohman707e0182008-04-12 04:36:06 +00001932 // This requires 4-byte alignment.
1933 if ((Align & 3) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00001934 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001935 // This requires the copy size to be a constant, preferrably
1936 // within a subtarget-specific limit.
1937 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
1938 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00001939 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001940 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001941 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00001942 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001943
1944 unsigned BytesLeft = SizeVal & 3;
1945 unsigned NumMemOps = SizeVal >> 2;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001946 unsigned EmittedNumMemOps = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001947 EVT VT = MVT::i32;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001948 unsigned VTSize = 4;
Evan Cheng4102eb52007-10-22 22:11:27 +00001949 unsigned i = 0;
Evan Chenge5e7ce42007-05-18 01:19:57 +00001950 const unsigned MAX_LOADS_IN_LDM = 6;
Dan Gohman475871a2008-07-27 21:46:04 +00001951 SDValue TFOps[MAX_LOADS_IN_LDM];
1952 SDValue Loads[MAX_LOADS_IN_LDM];
Dan Gohman1f13c682008-04-28 17:15:20 +00001953 uint64_t SrcOff = 0, DstOff = 0;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001954
Evan Cheng4102eb52007-10-22 22:11:27 +00001955 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
1956 // same number of stores. The loads and stores will get combined into
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001957 // ldm/stm later on.
Evan Cheng4102eb52007-10-22 22:11:27 +00001958 while (EmittedNumMemOps < NumMemOps) {
1959 for (i = 0;
1960 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001961 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00001962 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
1963 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001964 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001965 TFOps[i] = Loads[i].getValue(1);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001966 SrcOff += VTSize;
1967 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001968 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001969
Evan Cheng4102eb52007-10-22 22:11:27 +00001970 for (i = 0;
1971 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001972 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00001973 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
1974 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001975 DstSV, DstSVOff + DstOff);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001976 DstOff += VTSize;
1977 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001978 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00001979
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001980 EmittedNumMemOps += i;
1981 }
1982
Bob Wilson2dc4f542009-03-20 22:42:55 +00001983 if (BytesLeft == 0)
Evan Cheng4102eb52007-10-22 22:11:27 +00001984 return Chain;
1985
1986 // Issue loads / stores for the trailing (1 - 3) bytes.
1987 unsigned BytesLeftSave = BytesLeft;
1988 i = 0;
1989 while (BytesLeft) {
1990 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001991 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00001992 VTSize = 2;
1993 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001994 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00001995 VTSize = 1;
1996 }
1997
Dale Johannesen0f502f62009-02-03 22:26:09 +00001998 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00001999 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
2000 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002001 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002002 TFOps[i] = Loads[i].getValue(1);
2003 ++i;
2004 SrcOff += VTSize;
2005 BytesLeft -= VTSize;
2006 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002007 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00002008
2009 i = 0;
2010 BytesLeft = BytesLeftSave;
2011 while (BytesLeft) {
2012 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002013 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00002014 VTSize = 2;
2015 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00002016 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00002017 VTSize = 1;
2018 }
2019
Dale Johannesen0f502f62009-02-03 22:26:09 +00002020 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00002021 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
2022 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002023 DstSV, DstSVOff + DstOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002024 ++i;
2025 DstOff += VTSize;
2026 BytesLeft -= VTSize;
2027 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002028 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002029}
2030
Duncan Sands1607f052008-12-01 11:39:25 +00002031static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00002032 SDValue Op = N->getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +00002033 DebugLoc dl = N->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002034 if (N->getValueType(0) == MVT::f64) {
Evan Chengc7c77292008-11-04 19:57:48 +00002035 // Turn i64->f64 into FMDRR.
Owen Anderson825b72b2009-08-11 20:47:22 +00002036 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2037 DAG.getConstant(0, MVT::i32));
2038 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2039 DAG.getConstant(1, MVT::i32));
2040 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002041 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002042
Evan Chengc7c77292008-11-04 19:57:48 +00002043 // Turn f64->i64 into FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00002044 SDValue Cvt = DAG.getNode(ARMISD::FMRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002045 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002046
Chris Lattner27a6c732007-11-24 07:07:01 +00002047 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002048 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
Chris Lattner27a6c732007-11-24 07:07:01 +00002049}
2050
Bob Wilson5bafff32009-06-22 23:27:02 +00002051/// getZeroVector - Returns a vector of specified type with all zero elements.
2052///
Owen Andersone50ed302009-08-10 22:56:29 +00002053static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002054 assert(VT.isVector() && "Expected a vector type");
2055
2056 // Zero vectors are used to represent vector negation and in those cases
2057 // will be implemented with the NEON VNEG instruction. However, VNEG does
2058 // not support i64 elements, so sometimes the zero vectors will need to be
2059 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002060 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilson5bafff32009-06-22 23:27:02 +00002061 // to their dest type. This ensures they get CSE'd.
2062 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002063 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2064 SmallVector<SDValue, 8> Ops;
2065 MVT TVT;
2066
2067 if (VT.getSizeInBits() == 64) {
2068 Ops.assign(8, Cst); TVT = MVT::v8i8;
2069 } else {
2070 Ops.assign(16, Cst); TVT = MVT::v16i8;
2071 }
2072 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002073
2074 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2075}
2076
2077/// getOnesVector - Returns a vector of specified type with all bits set.
2078///
Owen Andersone50ed302009-08-10 22:56:29 +00002079static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002080 assert(VT.isVector() && "Expected a vector type");
2081
Bob Wilson929ffa22009-10-30 20:13:25 +00002082 // Always build ones vectors as <16 x i8> or <8 x i8> bitcasted to their
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002083 // dest type. This ensures they get CSE'd.
Bob Wilson5bafff32009-06-22 23:27:02 +00002084 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002085 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2086 SmallVector<SDValue, 8> Ops;
2087 MVT TVT;
2088
2089 if (VT.getSizeInBits() == 64) {
2090 Ops.assign(8, Cst); TVT = MVT::v8i8;
2091 } else {
2092 Ops.assign(16, Cst); TVT = MVT::v16i8;
2093 }
2094 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002095
2096 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2097}
2098
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002099/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2100/// i32 values and take a 2 x i32 value to shift plus a shift amount.
2101static SDValue LowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
2102 const ARMSubtarget *ST) {
2103 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2104 EVT VT = Op.getValueType();
2105 unsigned VTBits = VT.getSizeInBits();
2106 DebugLoc dl = Op.getDebugLoc();
2107 SDValue ShOpLo = Op.getOperand(0);
2108 SDValue ShOpHi = Op.getOperand(1);
2109 SDValue ShAmt = Op.getOperand(2);
2110 SDValue ARMCC;
2111
2112 assert(Op.getOpcode() == ISD::SRA_PARTS);
2113 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2114 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2115 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2116 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2117 DAG.getConstant(VTBits, MVT::i32));
2118 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2119 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2120 SDValue TrueVal = DAG.getNode(ISD::SRA, dl, VT, ShOpHi, ExtraShAmt);
2121
2122 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2123 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
2124 ARMCC, DAG, ST->isThumb1Only(), dl);
2125 SDValue Hi = DAG.getNode(ISD::SRA, dl, VT, ShOpHi, ShAmt);
2126 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC,
2127 CCR, Cmp);
2128
2129 SDValue Ops[2] = { Lo, Hi };
2130 return DAG.getMergeValues(Ops, 2, dl);
2131}
2132
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002133/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2134/// i32 values and take a 2 x i32 value to shift plus a shift amount.
2135static SDValue LowerShiftLeftParts(SDValue Op, SelectionDAG &DAG,
2136 const ARMSubtarget *ST) {
2137 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2138 EVT VT = Op.getValueType();
2139 unsigned VTBits = VT.getSizeInBits();
2140 DebugLoc dl = Op.getDebugLoc();
2141 SDValue ShOpLo = Op.getOperand(0);
2142 SDValue ShOpHi = Op.getOperand(1);
2143 SDValue ShAmt = Op.getOperand(2);
2144 SDValue ARMCC;
2145
2146 assert(Op.getOpcode() == ISD::SHL_PARTS);
2147 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2148 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2149 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2150 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2151 DAG.getConstant(VTBits, MVT::i32));
2152 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2153 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2154
2155 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2156 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2157 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
2158 ARMCC, DAG, ST->isThumb1Only(), dl);
2159 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
2160 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMCC,
2161 CCR, Cmp);
2162
2163 SDValue Ops[2] = { Lo, Hi };
2164 return DAG.getMergeValues(Ops, 2, dl);
2165}
2166
Bob Wilson5bafff32009-06-22 23:27:02 +00002167static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2168 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002169 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002170 DebugLoc dl = N->getDebugLoc();
2171
2172 // Lower vector shifts on NEON to use VSHL.
2173 if (VT.isVector()) {
2174 assert(ST->hasNEON() && "unexpected vector shift");
2175
2176 // Left shifts translate directly to the vshiftu intrinsic.
2177 if (N->getOpcode() == ISD::SHL)
2178 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002179 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002180 N->getOperand(0), N->getOperand(1));
2181
2182 assert((N->getOpcode() == ISD::SRA ||
2183 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2184
2185 // NEON uses the same intrinsics for both left and right shifts. For
2186 // right shifts, the shift amounts are negative, so negate the vector of
2187 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002188 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002189 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2190 getZeroVector(ShiftVT, DAG, dl),
2191 N->getOperand(1));
2192 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2193 Intrinsic::arm_neon_vshifts :
2194 Intrinsic::arm_neon_vshiftu);
2195 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002196 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002197 N->getOperand(0), NegatedCount);
2198 }
2199
Eli Friedmance392eb2009-08-22 03:13:10 +00002200 // We can get here for a node like i32 = ISD::SHL i32, i64
2201 if (VT != MVT::i64)
2202 return SDValue();
2203
2204 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002205 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002206
Chris Lattner27a6c732007-11-24 07:07:01 +00002207 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2208 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002209 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002210 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002211
Chris Lattner27a6c732007-11-24 07:07:01 +00002212 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002213 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002214
Chris Lattner27a6c732007-11-24 07:07:01 +00002215 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002216 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2217 DAG.getConstant(0, MVT::i32));
2218 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2219 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002220
Chris Lattner27a6c732007-11-24 07:07:01 +00002221 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2222 // captures the result into a carry flag.
2223 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002224 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002225
Chris Lattner27a6c732007-11-24 07:07:01 +00002226 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002227 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002228
Chris Lattner27a6c732007-11-24 07:07:01 +00002229 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002230 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002231}
2232
Bob Wilson5bafff32009-06-22 23:27:02 +00002233static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2234 SDValue TmpOp0, TmpOp1;
2235 bool Invert = false;
2236 bool Swap = false;
2237 unsigned Opc = 0;
2238
2239 SDValue Op0 = Op.getOperand(0);
2240 SDValue Op1 = Op.getOperand(1);
2241 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002242 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002243 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2244 DebugLoc dl = Op.getDebugLoc();
2245
2246 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2247 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002248 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002249 case ISD::SETUNE:
2250 case ISD::SETNE: Invert = true; // Fallthrough
2251 case ISD::SETOEQ:
2252 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2253 case ISD::SETOLT:
2254 case ISD::SETLT: Swap = true; // Fallthrough
2255 case ISD::SETOGT:
2256 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2257 case ISD::SETOLE:
2258 case ISD::SETLE: Swap = true; // Fallthrough
2259 case ISD::SETOGE:
2260 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2261 case ISD::SETUGE: Swap = true; // Fallthrough
2262 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2263 case ISD::SETUGT: Swap = true; // Fallthrough
2264 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2265 case ISD::SETUEQ: Invert = true; // Fallthrough
2266 case ISD::SETONE:
2267 // Expand this to (OLT | OGT).
2268 TmpOp0 = Op0;
2269 TmpOp1 = Op1;
2270 Opc = ISD::OR;
2271 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2272 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2273 break;
2274 case ISD::SETUO: Invert = true; // Fallthrough
2275 case ISD::SETO:
2276 // Expand this to (OLT | OGE).
2277 TmpOp0 = Op0;
2278 TmpOp1 = Op1;
2279 Opc = ISD::OR;
2280 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2281 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2282 break;
2283 }
2284 } else {
2285 // Integer comparisons.
2286 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002287 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002288 case ISD::SETNE: Invert = true;
2289 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2290 case ISD::SETLT: Swap = true;
2291 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2292 case ISD::SETLE: Swap = true;
2293 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2294 case ISD::SETULT: Swap = true;
2295 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2296 case ISD::SETULE: Swap = true;
2297 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2298 }
2299
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002300 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002301 if (Opc == ARMISD::VCEQ) {
2302
2303 SDValue AndOp;
2304 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2305 AndOp = Op0;
2306 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2307 AndOp = Op1;
2308
2309 // Ignore bitconvert.
2310 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2311 AndOp = AndOp.getOperand(0);
2312
2313 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2314 Opc = ARMISD::VTST;
2315 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2316 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2317 Invert = !Invert;
2318 }
2319 }
2320 }
2321
2322 if (Swap)
2323 std::swap(Op0, Op1);
2324
2325 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2326
2327 if (Invert)
2328 Result = DAG.getNOT(dl, Result, VT);
2329
2330 return Result;
2331}
2332
2333/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2334/// VMOV instruction, and if so, return the constant being splatted.
2335static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2336 unsigned SplatBitSize, SelectionDAG &DAG) {
2337 switch (SplatBitSize) {
2338 case 8:
2339 // Any 1-byte value is OK.
2340 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson825b72b2009-08-11 20:47:22 +00002341 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilson5bafff32009-06-22 23:27:02 +00002342
2343 case 16:
2344 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2345 if ((SplatBits & ~0xff) == 0 ||
2346 (SplatBits & ~0xff00) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002347 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilson5bafff32009-06-22 23:27:02 +00002348 break;
2349
2350 case 32:
2351 // NEON's 32-bit VMOV supports splat values where:
2352 // * only one byte is nonzero, or
2353 // * the least significant byte is 0xff and the second byte is nonzero, or
2354 // * the least significant 2 bytes are 0xff and the third is nonzero.
2355 if ((SplatBits & ~0xff) == 0 ||
2356 (SplatBits & ~0xff00) == 0 ||
2357 (SplatBits & ~0xff0000) == 0 ||
2358 (SplatBits & ~0xff000000) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002359 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002360
2361 if ((SplatBits & ~0xffff) == 0 &&
2362 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002363 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002364
2365 if ((SplatBits & ~0xffffff) == 0 &&
2366 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002367 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002368
2369 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2370 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2371 // VMOV.I32. A (very) minor optimization would be to replicate the value
2372 // and fall through here to test for a valid 64-bit splat. But, then the
2373 // caller would also need to check and handle the change in size.
2374 break;
2375
2376 case 64: {
2377 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2378 uint64_t BitMask = 0xff;
2379 uint64_t Val = 0;
2380 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2381 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2382 Val |= BitMask;
2383 else if ((SplatBits & BitMask) != 0)
2384 return SDValue();
2385 BitMask <<= 8;
2386 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002387 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilson5bafff32009-06-22 23:27:02 +00002388 }
2389
2390 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002391 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilson5bafff32009-06-22 23:27:02 +00002392 break;
2393 }
2394
2395 return SDValue();
2396}
2397
2398/// getVMOVImm - If this is a build_vector of constants which can be
2399/// formed by using a VMOV instruction of the specified element size,
2400/// return the constant being splatted. The ByteSize field indicates the
2401/// number of bytes of each element [1248].
2402SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2403 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2404 APInt SplatBits, SplatUndef;
2405 unsigned SplatBitSize;
2406 bool HasAnyUndefs;
2407 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2408 HasAnyUndefs, ByteSize * 8))
2409 return SDValue();
2410
2411 if (SplatBitSize > ByteSize * 8)
2412 return SDValue();
2413
2414 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2415 SplatBitSize, DAG);
2416}
2417
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002418static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2419 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002420 unsigned NumElts = VT.getVectorNumElements();
2421 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002422 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002423
2424 // If this is a VEXT shuffle, the immediate value is the index of the first
2425 // element. The other shuffle indices must be the successive elements after
2426 // the first one.
2427 unsigned ExpectedElt = Imm;
2428 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002429 // Increment the expected index. If it wraps around, it may still be
2430 // a VEXT but the source vectors must be swapped.
2431 ExpectedElt += 1;
2432 if (ExpectedElt == NumElts * 2) {
2433 ExpectedElt = 0;
2434 ReverseVEXT = true;
2435 }
2436
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002437 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002438 return false;
2439 }
2440
2441 // Adjust the index value if the source operands will be swapped.
2442 if (ReverseVEXT)
2443 Imm -= NumElts;
2444
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002445 return true;
2446}
2447
Bob Wilson8bb9e482009-07-26 00:39:34 +00002448/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2449/// instruction with the specified blocksize. (The order of the elements
2450/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002451static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2452 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002453 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2454 "Only possible block sizes for VREV are: 16, 32, 64");
2455
Bob Wilson8bb9e482009-07-26 00:39:34 +00002456 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00002457 if (EltSz == 64)
2458 return false;
2459
2460 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002461 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002462
2463 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2464 return false;
2465
2466 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002467 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002468 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2469 return false;
2470 }
2471
2472 return true;
2473}
2474
Bob Wilsonc692cb72009-08-21 20:54:19 +00002475static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2476 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002477 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2478 if (EltSz == 64)
2479 return false;
2480
Bob Wilsonc692cb72009-08-21 20:54:19 +00002481 unsigned NumElts = VT.getVectorNumElements();
2482 WhichResult = (M[0] == 0 ? 0 : 1);
2483 for (unsigned i = 0; i < NumElts; i += 2) {
2484 if ((unsigned) M[i] != i + WhichResult ||
2485 (unsigned) M[i+1] != i + NumElts + WhichResult)
2486 return false;
2487 }
2488 return true;
2489}
2490
2491static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2492 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002493 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2494 if (EltSz == 64)
2495 return false;
2496
Bob Wilsonc692cb72009-08-21 20:54:19 +00002497 unsigned NumElts = VT.getVectorNumElements();
2498 WhichResult = (M[0] == 0 ? 0 : 1);
2499 for (unsigned i = 0; i != NumElts; ++i) {
2500 if ((unsigned) M[i] != 2 * i + WhichResult)
2501 return false;
2502 }
2503
2504 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002505 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002506 return false;
2507
2508 return true;
2509}
2510
2511static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2512 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002513 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2514 if (EltSz == 64)
2515 return false;
2516
Bob Wilsonc692cb72009-08-21 20:54:19 +00002517 unsigned NumElts = VT.getVectorNumElements();
2518 WhichResult = (M[0] == 0 ? 0 : 1);
2519 unsigned Idx = WhichResult * NumElts / 2;
2520 for (unsigned i = 0; i != NumElts; i += 2) {
2521 if ((unsigned) M[i] != Idx ||
2522 (unsigned) M[i+1] != Idx + NumElts)
2523 return false;
2524 Idx += 1;
2525 }
2526
2527 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002528 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002529 return false;
2530
2531 return true;
2532}
2533
Owen Andersone50ed302009-08-10 22:56:29 +00002534static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002535 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00002536 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002537 if (ConstVal->isNullValue())
2538 return getZeroVector(VT, DAG, dl);
2539 if (ConstVal->isAllOnesValue())
2540 return getOnesVector(VT, DAG, dl);
2541
Owen Andersone50ed302009-08-10 22:56:29 +00002542 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00002543 if (VT.is64BitVector()) {
2544 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002545 case 8: CanonicalVT = MVT::v8i8; break;
2546 case 16: CanonicalVT = MVT::v4i16; break;
2547 case 32: CanonicalVT = MVT::v2i32; break;
2548 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002549 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002550 }
2551 } else {
2552 assert(VT.is128BitVector() && "unknown splat vector size");
2553 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002554 case 8: CanonicalVT = MVT::v16i8; break;
2555 case 16: CanonicalVT = MVT::v8i16; break;
2556 case 32: CanonicalVT = MVT::v4i32; break;
2557 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002558 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002559 }
2560 }
2561
2562 // Build a canonical splat for this value.
2563 SmallVector<SDValue, 8> Ops;
2564 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2565 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2566 Ops.size());
2567 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2568}
2569
2570// If this is a case we can't handle, return null and let the default
2571// expansion code take care of it.
2572static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00002573 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002574 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002575 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002576
2577 APInt SplatBits, SplatUndef;
2578 unsigned SplatBitSize;
2579 bool HasAnyUndefs;
2580 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00002581 if (SplatBitSize <= 64) {
2582 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2583 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2584 if (Val.getNode())
2585 return BuildSplat(Val, VT, DAG, dl);
2586 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00002587 }
2588
2589 // If there are only 2 elements in a 128-bit vector, insert them into an
2590 // undef vector. This handles the common case for 128-bit vector argument
2591 // passing, where the insertions should be translated to subreg accesses
2592 // with no real instructions.
2593 if (VT.is128BitVector() && Op.getNumOperands() == 2) {
2594 SDValue Val = DAG.getUNDEF(VT);
2595 SDValue Op0 = Op.getOperand(0);
2596 SDValue Op1 = Op.getOperand(1);
2597 if (Op0.getOpcode() != ISD::UNDEF)
2598 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op0,
2599 DAG.getIntPtrConstant(0));
2600 if (Op1.getOpcode() != ISD::UNDEF)
2601 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op1,
2602 DAG.getIntPtrConstant(1));
2603 return Val;
Bob Wilson5bafff32009-06-22 23:27:02 +00002604 }
2605
2606 return SDValue();
2607}
2608
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002609/// isShuffleMaskLegal - Targets can use this to indicate that they only
2610/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2611/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2612/// are assumed to be legal.
2613bool
2614ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2615 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002616 if (VT.getVectorNumElements() == 4 &&
2617 (VT.is128BitVector() || VT.is64BitVector())) {
2618 unsigned PFIndexes[4];
2619 for (unsigned i = 0; i != 4; ++i) {
2620 if (M[i] < 0)
2621 PFIndexes[i] = 8;
2622 else
2623 PFIndexes[i] = M[i];
2624 }
2625
2626 // Compute the index in the perfect shuffle table.
2627 unsigned PFTableIndex =
2628 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2629 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2630 unsigned Cost = (PFEntry >> 30);
2631
2632 if (Cost <= 4)
2633 return true;
2634 }
2635
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002636 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00002637 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002638
2639 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2640 isVREVMask(M, VT, 64) ||
2641 isVREVMask(M, VT, 32) ||
2642 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00002643 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2644 isVTRNMask(M, VT, WhichResult) ||
2645 isVUZPMask(M, VT, WhichResult) ||
2646 isVZIPMask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002647}
2648
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002649/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2650/// the specified operations to build the shuffle.
2651static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2652 SDValue RHS, SelectionDAG &DAG,
2653 DebugLoc dl) {
2654 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2655 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2656 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2657
2658 enum {
2659 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2660 OP_VREV,
2661 OP_VDUP0,
2662 OP_VDUP1,
2663 OP_VDUP2,
2664 OP_VDUP3,
2665 OP_VEXT1,
2666 OP_VEXT2,
2667 OP_VEXT3,
2668 OP_VUZPL, // VUZP, left result
2669 OP_VUZPR, // VUZP, right result
2670 OP_VZIPL, // VZIP, left result
2671 OP_VZIPR, // VZIP, right result
2672 OP_VTRNL, // VTRN, left result
2673 OP_VTRNR // VTRN, right result
2674 };
2675
2676 if (OpNum == OP_COPY) {
2677 if (LHSID == (1*9+2)*9+3) return LHS;
2678 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2679 return RHS;
2680 }
2681
2682 SDValue OpLHS, OpRHS;
2683 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2684 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2685 EVT VT = OpLHS.getValueType();
2686
2687 switch (OpNum) {
2688 default: llvm_unreachable("Unknown shuffle opcode!");
2689 case OP_VREV:
2690 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2691 case OP_VDUP0:
2692 case OP_VDUP1:
2693 case OP_VDUP2:
2694 case OP_VDUP3:
2695 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002696 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002697 case OP_VEXT1:
2698 case OP_VEXT2:
2699 case OP_VEXT3:
2700 return DAG.getNode(ARMISD::VEXT, dl, VT,
2701 OpLHS, OpRHS,
2702 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2703 case OP_VUZPL:
2704 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002705 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002706 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
2707 case OP_VZIPL:
2708 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002709 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002710 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
2711 case OP_VTRNL:
2712 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002713 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2714 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002715 }
2716}
2717
Bob Wilson5bafff32009-06-22 23:27:02 +00002718static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002719 SDValue V1 = Op.getOperand(0);
2720 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00002721 DebugLoc dl = Op.getDebugLoc();
2722 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002723 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002724 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00002725
Bob Wilson28865062009-08-13 02:13:04 +00002726 // Convert shuffles that are directly supported on NEON to target-specific
2727 // DAG nodes, instead of keeping them as shuffles and matching them again
2728 // during code selection. This is more efficient and avoids the possibility
2729 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00002730 // FIXME: floating-point vectors should be canonicalized to integer vectors
2731 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002732 SVN->getMask(ShuffleMask);
2733
2734 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson0ce37102009-08-14 05:08:32 +00002735 int Lane = SVN->getSplatIndex();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002736 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
2737 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00002738 }
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002739 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002740 DAG.getConstant(Lane, MVT::i32));
Bob Wilson0ce37102009-08-14 05:08:32 +00002741 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002742
2743 bool ReverseVEXT;
2744 unsigned Imm;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002745 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002746 if (ReverseVEXT)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002747 std::swap(V1, V2);
2748 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002749 DAG.getConstant(Imm, MVT::i32));
2750 }
2751
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002752 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002753 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002754 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002755 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002756 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002757 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
2758
Bob Wilsonc692cb72009-08-21 20:54:19 +00002759 // Check for Neon shuffles that modify both input vectors in place.
2760 // If both results are used, i.e., if there are two shuffles with the same
2761 // source operands and with masks corresponding to both results of one of
2762 // these operations, DAG memoization will ensure that a single node is
2763 // used for both shuffles.
2764 unsigned WhichResult;
2765 if (isVTRNMask(ShuffleMask, VT, WhichResult))
2766 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2767 V1, V2).getValue(WhichResult);
2768 if (isVUZPMask(ShuffleMask, VT, WhichResult))
2769 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2770 V1, V2).getValue(WhichResult);
2771 if (isVZIPMask(ShuffleMask, VT, WhichResult))
2772 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2773 V1, V2).getValue(WhichResult);
2774
2775 // If the shuffle is not directly supported and it has 4 elements, use
2776 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002777 if (VT.getVectorNumElements() == 4 &&
2778 (VT.is128BitVector() || VT.is64BitVector())) {
2779 unsigned PFIndexes[4];
2780 for (unsigned i = 0; i != 4; ++i) {
2781 if (ShuffleMask[i] < 0)
2782 PFIndexes[i] = 8;
2783 else
2784 PFIndexes[i] = ShuffleMask[i];
2785 }
2786
2787 // Compute the index in the perfect shuffle table.
2788 unsigned PFTableIndex =
2789 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2790
2791 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2792 unsigned Cost = (PFEntry >> 30);
2793
2794 if (Cost <= 4)
2795 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
2796 }
Bob Wilsond8e17572009-08-12 22:31:50 +00002797
Bob Wilson22cac0d2009-08-14 05:16:33 +00002798 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002799}
2800
Bob Wilson5bafff32009-06-22 23:27:02 +00002801static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002802 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002803 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00002804 SDValue Vec = Op.getOperand(0);
2805 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00002806 assert(VT == MVT::i32 &&
2807 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
2808 "unexpected type for custom-lowering vector extract");
2809 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00002810}
2811
Bob Wilsona6d65862009-08-03 20:36:38 +00002812static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
2813 // The only time a CONCAT_VECTORS operation can have legal types is when
2814 // two 64-bit vectors are concatenated to a 128-bit vector.
2815 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
2816 "unexpected CONCAT_VECTORS");
2817 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002818 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00002819 SDValue Op0 = Op.getOperand(0);
2820 SDValue Op1 = Op.getOperand(1);
2821 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002822 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2823 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00002824 DAG.getIntPtrConstant(0));
2825 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002826 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2827 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00002828 DAG.getIntPtrConstant(1));
2829 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00002830}
2831
Dan Gohman475871a2008-07-27 21:46:04 +00002832SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002833 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002834 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00002835 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002836 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002837 case ISD::GlobalAddress:
2838 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
2839 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002840 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002841 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
2842 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
2843 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00002844 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002845 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
2846 case ISD::SINT_TO_FP:
2847 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
2848 case ISD::FP_TO_SINT:
2849 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
2850 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00002851 case ISD::RETURNADDR: break;
Jim Grosbach0e0da732009-05-12 23:59:14 +00002852 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002853 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002854 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00002855 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002856 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00002857 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00002858 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002859 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG, Subtarget);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002860 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00002861 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
2862 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2863 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002864 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00002865 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002866 }
Dan Gohman475871a2008-07-27 21:46:04 +00002867 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00002868}
2869
Duncan Sands1607f052008-12-01 11:39:25 +00002870/// ReplaceNodeResults - Replace the results of node with an illegal result
2871/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00002872void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
2873 SmallVectorImpl<SDValue>&Results,
2874 SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00002875 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00002876 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002877 llvm_unreachable("Don't know how to custom expand this!");
Duncan Sands1607f052008-12-01 11:39:25 +00002878 return;
2879 case ISD::BIT_CONVERT:
2880 Results.push_back(ExpandBIT_CONVERT(N, DAG));
2881 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00002882 case ISD::SRL:
Duncan Sands1607f052008-12-01 11:39:25 +00002883 case ISD::SRA: {
Bob Wilson5bafff32009-06-22 23:27:02 +00002884 SDValue Res = LowerShift(N, DAG, Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00002885 if (Res.getNode())
2886 Results.push_back(Res);
2887 return;
2888 }
Chris Lattner27a6c732007-11-24 07:07:01 +00002889 }
2890}
Chris Lattner27a6c732007-11-24 07:07:01 +00002891
Evan Chenga8e29892007-01-19 07:51:42 +00002892//===----------------------------------------------------------------------===//
2893// ARM Scheduler Hooks
2894//===----------------------------------------------------------------------===//
2895
2896MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00002897ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00002898 MachineBasicBlock *BB,
2899 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002900 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00002901 DebugLoc dl = MI->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002902 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00002903 default:
2904 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng007ea272009-08-12 05:17:19 +00002905 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00002906 // To "insert" a SELECT_CC instruction, we actually have to insert the
2907 // diamond control-flow pattern. The incoming instruction knows the
2908 // destination vreg to set, the condition code register to branch on, the
2909 // true/false values to select between, and a branch opcode to use.
2910 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002911 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00002912 ++It;
2913
2914 // thisMBB:
2915 // ...
2916 // TrueVal = ...
2917 // cmpTY ccX, r1, r2
2918 // bCC copy1MBB
2919 // fallthrough --> copy0MBB
2920 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002921 MachineFunction *F = BB->getParent();
2922 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
2923 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00002924 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00002925 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002926 F->insert(It, copy0MBB);
2927 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00002928 // Update machine-CFG edges by first adding all successors of the current
2929 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00002930 // Also inform sdisel of the edge changes.
2931 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
2932 E = BB->succ_end(); I != E; ++I) {
2933 EM->insert(std::make_pair(*I, sinkMBB));
2934 sinkMBB->addSuccessor(*I);
2935 }
Evan Chenga8e29892007-01-19 07:51:42 +00002936 // Next, remove all successors of the current block, and add the true
2937 // and fallthrough blocks as its successors.
Evan Chengce319102009-09-19 09:51:03 +00002938 while (!BB->succ_empty())
Evan Chenga8e29892007-01-19 07:51:42 +00002939 BB->removeSuccessor(BB->succ_begin());
2940 BB->addSuccessor(copy0MBB);
2941 BB->addSuccessor(sinkMBB);
2942
2943 // copy0MBB:
2944 // %FalseValue = ...
2945 // # fallthrough to sinkMBB
2946 BB = copy0MBB;
2947
2948 // Update machine-CFG edges
2949 BB->addSuccessor(sinkMBB);
2950
2951 // sinkMBB:
2952 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2953 // ...
2954 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00002955 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00002956 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
2957 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
2958
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002959 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00002960 return BB;
2961 }
Evan Cheng86198642009-08-07 00:34:42 +00002962
2963 case ARM::tANDsp:
2964 case ARM::tADDspr_:
2965 case ARM::tSUBspi_:
2966 case ARM::t2SUBrSPi_:
2967 case ARM::t2SUBrSPi12_:
2968 case ARM::t2SUBrSPs_: {
2969 MachineFunction *MF = BB->getParent();
2970 unsigned DstReg = MI->getOperand(0).getReg();
2971 unsigned SrcReg = MI->getOperand(1).getReg();
2972 bool DstIsDead = MI->getOperand(0).isDead();
2973 bool SrcIsKill = MI->getOperand(1).isKill();
2974
2975 if (SrcReg != ARM::SP) {
2976 // Copy the source to SP from virtual register.
2977 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
2978 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
2979 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
2980 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
2981 .addReg(SrcReg, getKillRegState(SrcIsKill));
2982 }
2983
2984 unsigned OpOpc = 0;
2985 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
2986 switch (MI->getOpcode()) {
2987 default:
2988 llvm_unreachable("Unexpected pseudo instruction!");
2989 case ARM::tANDsp:
2990 OpOpc = ARM::tAND;
2991 NeedPred = true;
2992 break;
2993 case ARM::tADDspr_:
2994 OpOpc = ARM::tADDspr;
2995 break;
2996 case ARM::tSUBspi_:
2997 OpOpc = ARM::tSUBspi;
2998 break;
2999 case ARM::t2SUBrSPi_:
3000 OpOpc = ARM::t2SUBrSPi;
3001 NeedPred = true; NeedCC = true;
3002 break;
3003 case ARM::t2SUBrSPi12_:
3004 OpOpc = ARM::t2SUBrSPi12;
3005 NeedPred = true;
3006 break;
3007 case ARM::t2SUBrSPs_:
3008 OpOpc = ARM::t2SUBrSPs;
3009 NeedPred = true; NeedCC = true; NeedOp3 = true;
3010 break;
3011 }
3012 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
3013 if (OpOpc == ARM::tAND)
3014 AddDefaultT1CC(MIB);
3015 MIB.addReg(ARM::SP);
3016 MIB.addOperand(MI->getOperand(2));
3017 if (NeedOp3)
3018 MIB.addOperand(MI->getOperand(3));
3019 if (NeedPred)
3020 AddDefaultPred(MIB);
3021 if (NeedCC)
3022 AddDefaultCC(MIB);
3023
3024 // Copy the result from SP to virtual register.
3025 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
3026 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3027 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
3028 BuildMI(BB, dl, TII->get(CopyOpc))
3029 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
3030 .addReg(ARM::SP);
3031 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3032 return BB;
3033 }
Evan Chenga8e29892007-01-19 07:51:42 +00003034 }
3035}
3036
3037//===----------------------------------------------------------------------===//
3038// ARM Optimization Hooks
3039//===----------------------------------------------------------------------===//
3040
Chris Lattnerd1980a52009-03-12 06:52:53 +00003041static
3042SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3043 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00003044 SelectionDAG &DAG = DCI.DAG;
3045 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003046 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00003047 unsigned Opc = N->getOpcode();
3048 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3049 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3050 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3051 ISD::CondCode CC = ISD::SETCC_INVALID;
3052
3053 if (isSlctCC) {
3054 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
3055 } else {
3056 SDValue CCOp = Slct.getOperand(0);
3057 if (CCOp.getOpcode() == ISD::SETCC)
3058 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
3059 }
3060
3061 bool DoXform = false;
3062 bool InvCC = false;
3063 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
3064 "Bad input!");
3065
3066 if (LHS.getOpcode() == ISD::Constant &&
3067 cast<ConstantSDNode>(LHS)->isNullValue()) {
3068 DoXform = true;
3069 } else if (CC != ISD::SETCC_INVALID &&
3070 RHS.getOpcode() == ISD::Constant &&
3071 cast<ConstantSDNode>(RHS)->isNullValue()) {
3072 std::swap(LHS, RHS);
3073 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003074 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00003075 Op0.getOperand(0).getValueType();
3076 bool isInt = OpVT.isInteger();
3077 CC = ISD::getSetCCInverse(CC, isInt);
3078
3079 if (!TLI.isCondCodeLegal(CC, OpVT))
3080 return SDValue(); // Inverse operator isn't legal.
3081
3082 DoXform = true;
3083 InvCC = true;
3084 }
3085
3086 if (DoXform) {
3087 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3088 if (isSlctCC)
3089 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3090 Slct.getOperand(0), Slct.getOperand(1), CC);
3091 SDValue CCOp = Slct.getOperand(0);
3092 if (InvCC)
3093 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3094 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3095 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3096 CCOp, OtherOp, Result);
3097 }
3098 return SDValue();
3099}
3100
3101/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3102static SDValue PerformADDCombine(SDNode *N,
3103 TargetLowering::DAGCombinerInfo &DCI) {
3104 // added by evan in r37685 with no testcase.
3105 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003106
Chris Lattnerd1980a52009-03-12 06:52:53 +00003107 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3108 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3109 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3110 if (Result.getNode()) return Result;
3111 }
3112 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3113 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3114 if (Result.getNode()) return Result;
3115 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003116
Chris Lattnerd1980a52009-03-12 06:52:53 +00003117 return SDValue();
3118}
3119
3120/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3121static SDValue PerformSUBCombine(SDNode *N,
3122 TargetLowering::DAGCombinerInfo &DCI) {
3123 // added by evan in r37685 with no testcase.
3124 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003125
Chris Lattnerd1980a52009-03-12 06:52:53 +00003126 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3127 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3128 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3129 if (Result.getNode()) return Result;
3130 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003131
Chris Lattnerd1980a52009-03-12 06:52:53 +00003132 return SDValue();
3133}
3134
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003135/// PerformFMRRDCombine - Target-specific dag combine xforms for ARMISD::FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003136static SDValue PerformFMRRDCombine(SDNode *N,
3137 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003138 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003139 SDValue InDouble = N->getOperand(0);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003140 if (InDouble.getOpcode() == ARMISD::FMDRR)
3141 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003142 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003143}
3144
Bob Wilson5bafff32009-06-22 23:27:02 +00003145/// getVShiftImm - Check if this is a valid build_vector for the immediate
3146/// operand of a vector shift operation, where all the elements of the
3147/// build_vector must have the same constant integer value.
3148static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3149 // Ignore bit_converts.
3150 while (Op.getOpcode() == ISD::BIT_CONVERT)
3151 Op = Op.getOperand(0);
3152 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3153 APInt SplatBits, SplatUndef;
3154 unsigned SplatBitSize;
3155 bool HasAnyUndefs;
3156 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3157 HasAnyUndefs, ElementBits) ||
3158 SplatBitSize > ElementBits)
3159 return false;
3160 Cnt = SplatBits.getSExtValue();
3161 return true;
3162}
3163
3164/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3165/// operand of a vector shift left operation. That value must be in the range:
3166/// 0 <= Value < ElementBits for a left shift; or
3167/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003168static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003169 assert(VT.isVector() && "vector shift count is not a vector type");
3170 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3171 if (! getVShiftImm(Op, ElementBits, Cnt))
3172 return false;
3173 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3174}
3175
3176/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3177/// operand of a vector shift right operation. For a shift opcode, the value
3178/// is positive, but for an intrinsic the value count must be negative. The
3179/// absolute value must be in the range:
3180/// 1 <= |Value| <= ElementBits for a right shift; or
3181/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003182static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00003183 int64_t &Cnt) {
3184 assert(VT.isVector() && "vector shift count is not a vector type");
3185 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3186 if (! getVShiftImm(Op, ElementBits, Cnt))
3187 return false;
3188 if (isIntrinsic)
3189 Cnt = -Cnt;
3190 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3191}
3192
3193/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3194static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3195 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3196 switch (IntNo) {
3197 default:
3198 // Don't do anything for most intrinsics.
3199 break;
3200
3201 // Vector shifts: check for immediate versions and lower them.
3202 // Note: This is done during DAG combining instead of DAG legalizing because
3203 // the build_vectors for 64-bit vector element shift counts are generally
3204 // not legal, and it is hard to see their values after they get legalized to
3205 // loads from a constant pool.
3206 case Intrinsic::arm_neon_vshifts:
3207 case Intrinsic::arm_neon_vshiftu:
3208 case Intrinsic::arm_neon_vshiftls:
3209 case Intrinsic::arm_neon_vshiftlu:
3210 case Intrinsic::arm_neon_vshiftn:
3211 case Intrinsic::arm_neon_vrshifts:
3212 case Intrinsic::arm_neon_vrshiftu:
3213 case Intrinsic::arm_neon_vrshiftn:
3214 case Intrinsic::arm_neon_vqshifts:
3215 case Intrinsic::arm_neon_vqshiftu:
3216 case Intrinsic::arm_neon_vqshiftsu:
3217 case Intrinsic::arm_neon_vqshiftns:
3218 case Intrinsic::arm_neon_vqshiftnu:
3219 case Intrinsic::arm_neon_vqshiftnsu:
3220 case Intrinsic::arm_neon_vqrshiftns:
3221 case Intrinsic::arm_neon_vqrshiftnu:
3222 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00003223 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003224 int64_t Cnt;
3225 unsigned VShiftOpc = 0;
3226
3227 switch (IntNo) {
3228 case Intrinsic::arm_neon_vshifts:
3229 case Intrinsic::arm_neon_vshiftu:
3230 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3231 VShiftOpc = ARMISD::VSHL;
3232 break;
3233 }
3234 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3235 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3236 ARMISD::VSHRs : ARMISD::VSHRu);
3237 break;
3238 }
3239 return SDValue();
3240
3241 case Intrinsic::arm_neon_vshiftls:
3242 case Intrinsic::arm_neon_vshiftlu:
3243 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3244 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003245 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003246
3247 case Intrinsic::arm_neon_vrshifts:
3248 case Intrinsic::arm_neon_vrshiftu:
3249 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3250 break;
3251 return SDValue();
3252
3253 case Intrinsic::arm_neon_vqshifts:
3254 case Intrinsic::arm_neon_vqshiftu:
3255 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3256 break;
3257 return SDValue();
3258
3259 case Intrinsic::arm_neon_vqshiftsu:
3260 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3261 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003262 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003263
3264 case Intrinsic::arm_neon_vshiftn:
3265 case Intrinsic::arm_neon_vrshiftn:
3266 case Intrinsic::arm_neon_vqshiftns:
3267 case Intrinsic::arm_neon_vqshiftnu:
3268 case Intrinsic::arm_neon_vqshiftnsu:
3269 case Intrinsic::arm_neon_vqrshiftns:
3270 case Intrinsic::arm_neon_vqrshiftnu:
3271 case Intrinsic::arm_neon_vqrshiftnsu:
3272 // Narrowing shifts require an immediate right shift.
3273 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3274 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003275 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003276
3277 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003278 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003279 }
3280
3281 switch (IntNo) {
3282 case Intrinsic::arm_neon_vshifts:
3283 case Intrinsic::arm_neon_vshiftu:
3284 // Opcode already set above.
3285 break;
3286 case Intrinsic::arm_neon_vshiftls:
3287 case Intrinsic::arm_neon_vshiftlu:
3288 if (Cnt == VT.getVectorElementType().getSizeInBits())
3289 VShiftOpc = ARMISD::VSHLLi;
3290 else
3291 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3292 ARMISD::VSHLLs : ARMISD::VSHLLu);
3293 break;
3294 case Intrinsic::arm_neon_vshiftn:
3295 VShiftOpc = ARMISD::VSHRN; break;
3296 case Intrinsic::arm_neon_vrshifts:
3297 VShiftOpc = ARMISD::VRSHRs; break;
3298 case Intrinsic::arm_neon_vrshiftu:
3299 VShiftOpc = ARMISD::VRSHRu; break;
3300 case Intrinsic::arm_neon_vrshiftn:
3301 VShiftOpc = ARMISD::VRSHRN; break;
3302 case Intrinsic::arm_neon_vqshifts:
3303 VShiftOpc = ARMISD::VQSHLs; break;
3304 case Intrinsic::arm_neon_vqshiftu:
3305 VShiftOpc = ARMISD::VQSHLu; break;
3306 case Intrinsic::arm_neon_vqshiftsu:
3307 VShiftOpc = ARMISD::VQSHLsu; break;
3308 case Intrinsic::arm_neon_vqshiftns:
3309 VShiftOpc = ARMISD::VQSHRNs; break;
3310 case Intrinsic::arm_neon_vqshiftnu:
3311 VShiftOpc = ARMISD::VQSHRNu; break;
3312 case Intrinsic::arm_neon_vqshiftnsu:
3313 VShiftOpc = ARMISD::VQSHRNsu; break;
3314 case Intrinsic::arm_neon_vqrshiftns:
3315 VShiftOpc = ARMISD::VQRSHRNs; break;
3316 case Intrinsic::arm_neon_vqrshiftnu:
3317 VShiftOpc = ARMISD::VQRSHRNu; break;
3318 case Intrinsic::arm_neon_vqrshiftnsu:
3319 VShiftOpc = ARMISD::VQRSHRNsu; break;
3320 }
3321
3322 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003323 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003324 }
3325
3326 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00003327 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003328 int64_t Cnt;
3329 unsigned VShiftOpc = 0;
3330
3331 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3332 VShiftOpc = ARMISD::VSLI;
3333 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3334 VShiftOpc = ARMISD::VSRI;
3335 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003336 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003337 }
3338
3339 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3340 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00003341 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003342 }
3343
3344 case Intrinsic::arm_neon_vqrshifts:
3345 case Intrinsic::arm_neon_vqrshiftu:
3346 // No immediate versions of these to check for.
3347 break;
3348 }
3349
3350 return SDValue();
3351}
3352
3353/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3354/// lowers them. As with the vector shift intrinsics, this is done during DAG
3355/// combining instead of DAG legalizing because the build_vectors for 64-bit
3356/// vector element shift counts are generally not legal, and it is hard to see
3357/// their values after they get legalized to loads from a constant pool.
3358static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3359 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003360 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003361
3362 // Nothing to be done for scalar shifts.
3363 if (! VT.isVector())
3364 return SDValue();
3365
3366 assert(ST->hasNEON() && "unexpected vector shift");
3367 int64_t Cnt;
3368
3369 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003370 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003371
3372 case ISD::SHL:
3373 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3374 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003375 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003376 break;
3377
3378 case ISD::SRA:
3379 case ISD::SRL:
3380 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3381 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3382 ARMISD::VSHRs : ARMISD::VSHRu);
3383 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003384 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003385 }
3386 }
3387 return SDValue();
3388}
3389
3390/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3391/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3392static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3393 const ARMSubtarget *ST) {
3394 SDValue N0 = N->getOperand(0);
3395
3396 // Check for sign- and zero-extensions of vector extract operations of 8-
3397 // and 16-bit vector elements. NEON supports these directly. They are
3398 // handled during DAG combining because type legalization will promote them
3399 // to 32-bit types and it is messy to recognize the operations after that.
3400 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3401 SDValue Vec = N0.getOperand(0);
3402 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003403 EVT VT = N->getValueType(0);
3404 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003405 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3406
Owen Anderson825b72b2009-08-11 20:47:22 +00003407 if (VT == MVT::i32 &&
3408 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00003409 TLI.isTypeLegal(Vec.getValueType())) {
3410
3411 unsigned Opc = 0;
3412 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003413 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003414 case ISD::SIGN_EXTEND:
3415 Opc = ARMISD::VGETLANEs;
3416 break;
3417 case ISD::ZERO_EXTEND:
3418 case ISD::ANY_EXTEND:
3419 Opc = ARMISD::VGETLANEu;
3420 break;
3421 }
3422 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
3423 }
3424 }
3425
3426 return SDValue();
3427}
3428
Dan Gohman475871a2008-07-27 21:46:04 +00003429SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003430 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003431 switch (N->getOpcode()) {
3432 default: break;
Chris Lattnerd1980a52009-03-12 06:52:53 +00003433 case ISD::ADD: return PerformADDCombine(N, DCI);
3434 case ISD::SUB: return PerformSUBCombine(N, DCI);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003435 case ARMISD::FMRRD: return PerformFMRRDCombine(N, DCI);
Bob Wilson5bafff32009-06-22 23:27:02 +00003436 case ISD::INTRINSIC_WO_CHAIN:
3437 return PerformIntrinsicCombine(N, DCI.DAG);
3438 case ISD::SHL:
3439 case ISD::SRA:
3440 case ISD::SRL:
3441 return PerformShiftCombine(N, DCI.DAG, Subtarget);
3442 case ISD::SIGN_EXTEND:
3443 case ISD::ZERO_EXTEND:
3444 case ISD::ANY_EXTEND:
3445 return PerformExtendCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003446 }
Dan Gohman475871a2008-07-27 21:46:04 +00003447 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003448}
3449
Bill Wendlingaf566342009-08-15 21:21:19 +00003450bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
3451 if (!Subtarget->hasV6Ops())
3452 // Pre-v6 does not support unaligned mem access.
3453 return false;
3454 else if (!Subtarget->hasV6Ops()) {
3455 // v6 may or may not support unaligned mem access.
3456 if (!Subtarget->isTargetDarwin())
3457 return false;
3458 }
3459
3460 switch (VT.getSimpleVT().SimpleTy) {
3461 default:
3462 return false;
3463 case MVT::i8:
3464 case MVT::i16:
3465 case MVT::i32:
3466 return true;
3467 // FIXME: VLD1 etc with standard alignment is legal.
3468 }
3469}
3470
Evan Chenge6c835f2009-08-14 20:09:37 +00003471static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
3472 if (V < 0)
3473 return false;
3474
3475 unsigned Scale = 1;
3476 switch (VT.getSimpleVT().SimpleTy) {
3477 default: return false;
3478 case MVT::i1:
3479 case MVT::i8:
3480 // Scale == 1;
3481 break;
3482 case MVT::i16:
3483 // Scale == 2;
3484 Scale = 2;
3485 break;
3486 case MVT::i32:
3487 // Scale == 4;
3488 Scale = 4;
3489 break;
3490 }
3491
3492 if ((V & (Scale - 1)) != 0)
3493 return false;
3494 V /= Scale;
3495 return V == (V & ((1LL << 5) - 1));
3496}
3497
3498static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
3499 const ARMSubtarget *Subtarget) {
3500 bool isNeg = false;
3501 if (V < 0) {
3502 isNeg = true;
3503 V = - V;
3504 }
3505
3506 switch (VT.getSimpleVT().SimpleTy) {
3507 default: return false;
3508 case MVT::i1:
3509 case MVT::i8:
3510 case MVT::i16:
3511 case MVT::i32:
3512 // + imm12 or - imm8
3513 if (isNeg)
3514 return V == (V & ((1LL << 8) - 1));
3515 return V == (V & ((1LL << 12) - 1));
3516 case MVT::f32:
3517 case MVT::f64:
3518 // Same as ARM mode. FIXME: NEON?
3519 if (!Subtarget->hasVFP2())
3520 return false;
3521 if ((V & 3) != 0)
3522 return false;
3523 V >>= 2;
3524 return V == (V & ((1LL << 8) - 1));
3525 }
3526}
3527
Evan Chengb01fad62007-03-12 23:30:29 +00003528/// isLegalAddressImmediate - Return true if the integer value can be used
3529/// as the offset of the target addressing mode for load / store of the
3530/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00003531static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003532 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00003533 if (V == 0)
3534 return true;
3535
Evan Cheng65011532009-03-09 19:15:00 +00003536 if (!VT.isSimple())
3537 return false;
3538
Evan Chenge6c835f2009-08-14 20:09:37 +00003539 if (Subtarget->isThumb1Only())
3540 return isLegalT1AddressImmediate(V, VT);
3541 else if (Subtarget->isThumb2())
3542 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00003543
Evan Chenge6c835f2009-08-14 20:09:37 +00003544 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00003545 if (V < 0)
3546 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00003547 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00003548 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003549 case MVT::i1:
3550 case MVT::i8:
3551 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00003552 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003553 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003554 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00003555 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003556 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003557 case MVT::f32:
3558 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00003559 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00003560 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00003561 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00003562 return false;
3563 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003564 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00003565 }
Evan Chenga8e29892007-01-19 07:51:42 +00003566}
3567
Evan Chenge6c835f2009-08-14 20:09:37 +00003568bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
3569 EVT VT) const {
3570 int Scale = AM.Scale;
3571 if (Scale < 0)
3572 return false;
3573
3574 switch (VT.getSimpleVT().SimpleTy) {
3575 default: return false;
3576 case MVT::i1:
3577 case MVT::i8:
3578 case MVT::i16:
3579 case MVT::i32:
3580 if (Scale == 1)
3581 return true;
3582 // r + r << imm
3583 Scale = Scale & ~1;
3584 return Scale == 2 || Scale == 4 || Scale == 8;
3585 case MVT::i64:
3586 // r + r
3587 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
3588 return true;
3589 return false;
3590 case MVT::isVoid:
3591 // Note, we allow "void" uses (basically, uses that aren't loads or
3592 // stores), because arm allows folding a scale into many arithmetic
3593 // operations. This should be made more precise and revisited later.
3594
3595 // Allow r << imm, but the imm has to be a multiple of two.
3596 if (Scale & 1) return false;
3597 return isPowerOf2_32(Scale);
3598 }
3599}
3600
Chris Lattner37caf8c2007-04-09 23:33:39 +00003601/// isLegalAddressingMode - Return true if the addressing mode represented
3602/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003603bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003604 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00003605 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00003606 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00003607 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003608
Chris Lattner37caf8c2007-04-09 23:33:39 +00003609 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003610 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003611 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003612
Chris Lattner37caf8c2007-04-09 23:33:39 +00003613 switch (AM.Scale) {
3614 case 0: // no scale reg, must be "r+i" or "r", or "i".
3615 break;
3616 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00003617 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00003618 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003619 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00003620 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003621 // ARM doesn't support any R+R*scale+imm addr modes.
3622 if (AM.BaseOffs)
3623 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003624
Bob Wilson2c7dab12009-04-08 17:55:28 +00003625 if (!VT.isSimple())
3626 return false;
3627
Evan Chenge6c835f2009-08-14 20:09:37 +00003628 if (Subtarget->isThumb2())
3629 return isLegalT2ScaledAddressingMode(AM, VT);
3630
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003631 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00003632 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00003633 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003634 case MVT::i1:
3635 case MVT::i8:
3636 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003637 if (Scale < 0) Scale = -Scale;
3638 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003639 return true;
3640 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00003641 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003642 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00003643 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003644 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003645 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003646 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00003647 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003648
Owen Anderson825b72b2009-08-11 20:47:22 +00003649 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003650 // Note, we allow "void" uses (basically, uses that aren't loads or
3651 // stores), because arm allows folding a scale into many arithmetic
3652 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003653
Chris Lattner37caf8c2007-04-09 23:33:39 +00003654 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00003655 if (Scale & 1) return false;
3656 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00003657 }
3658 break;
Evan Chengb01fad62007-03-12 23:30:29 +00003659 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00003660 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00003661}
3662
Owen Andersone50ed302009-08-10 22:56:29 +00003663static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003664 bool isSEXTLoad, SDValue &Base,
3665 SDValue &Offset, bool &isInc,
3666 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00003667 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3668 return false;
3669
Owen Anderson825b72b2009-08-11 20:47:22 +00003670 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00003671 // AddressingMode 3
3672 Base = Ptr->getOperand(0);
3673 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003674 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003675 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003676 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003677 isInc = false;
3678 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3679 return true;
3680 }
3681 }
3682 isInc = (Ptr->getOpcode() == ISD::ADD);
3683 Offset = Ptr->getOperand(1);
3684 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00003685 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00003686 // AddressingMode 2
3687 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003688 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003689 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003690 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003691 isInc = false;
3692 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3693 Base = Ptr->getOperand(0);
3694 return true;
3695 }
3696 }
3697
3698 if (Ptr->getOpcode() == ISD::ADD) {
3699 isInc = true;
3700 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
3701 if (ShOpcVal != ARM_AM::no_shift) {
3702 Base = Ptr->getOperand(1);
3703 Offset = Ptr->getOperand(0);
3704 } else {
3705 Base = Ptr->getOperand(0);
3706 Offset = Ptr->getOperand(1);
3707 }
3708 return true;
3709 }
3710
3711 isInc = (Ptr->getOpcode() == ISD::ADD);
3712 Base = Ptr->getOperand(0);
3713 Offset = Ptr->getOperand(1);
3714 return true;
3715 }
3716
3717 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
3718 return false;
3719}
3720
Owen Andersone50ed302009-08-10 22:56:29 +00003721static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003722 bool isSEXTLoad, SDValue &Base,
3723 SDValue &Offset, bool &isInc,
3724 SelectionDAG &DAG) {
3725 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3726 return false;
3727
3728 Base = Ptr->getOperand(0);
3729 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
3730 int RHSC = (int)RHS->getZExtValue();
3731 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
3732 assert(Ptr->getOpcode() == ISD::ADD);
3733 isInc = false;
3734 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3735 return true;
3736 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
3737 isInc = Ptr->getOpcode() == ISD::ADD;
3738 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
3739 return true;
3740 }
3741 }
3742
3743 return false;
3744}
3745
Evan Chenga8e29892007-01-19 07:51:42 +00003746/// getPreIndexedAddressParts - returns true by value, base pointer and
3747/// offset pointer and addressing mode by reference if the node's address
3748/// can be legally represented as pre-indexed load / store address.
3749bool
Dan Gohman475871a2008-07-27 21:46:04 +00003750ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
3751 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003752 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003753 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003754 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003755 return false;
3756
Owen Andersone50ed302009-08-10 22:56:29 +00003757 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003758 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003759 bool isSEXTLoad = false;
3760 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
3761 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003762 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003763 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3764 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
3765 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003766 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003767 } else
3768 return false;
3769
3770 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003771 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003772 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003773 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
3774 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003775 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003776 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00003777 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00003778 if (!isLegal)
3779 return false;
3780
3781 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
3782 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003783}
3784
3785/// getPostIndexedAddressParts - returns true by value, base pointer and
3786/// offset pointer and addressing mode by reference if this node can be
3787/// combined with a load / store to form a post-indexed load / store.
3788bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00003789 SDValue &Base,
3790 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003791 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003792 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003793 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003794 return false;
3795
Owen Andersone50ed302009-08-10 22:56:29 +00003796 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003797 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003798 bool isSEXTLoad = false;
3799 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003800 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003801 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3802 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003803 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003804 } else
3805 return false;
3806
3807 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003808 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003809 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003810 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003811 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003812 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003813 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
3814 isInc, DAG);
3815 if (!isLegal)
3816 return false;
3817
3818 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
3819 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003820}
3821
Dan Gohman475871a2008-07-27 21:46:04 +00003822void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00003823 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003824 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003825 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00003826 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00003827 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003828 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003829 switch (Op.getOpcode()) {
3830 default: break;
3831 case ARMISD::CMOV: {
3832 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00003833 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003834 if (KnownZero == 0 && KnownOne == 0) return;
3835
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003836 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00003837 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
3838 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003839 KnownZero &= KnownZeroRHS;
3840 KnownOne &= KnownOneRHS;
3841 return;
3842 }
3843 }
3844}
3845
3846//===----------------------------------------------------------------------===//
3847// ARM Inline Assembly Support
3848//===----------------------------------------------------------------------===//
3849
3850/// getConstraintType - Given a constraint letter, return the type of
3851/// constraint it is for this target.
3852ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00003853ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
3854 if (Constraint.size() == 1) {
3855 switch (Constraint[0]) {
3856 default: break;
3857 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003858 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00003859 }
Evan Chenga8e29892007-01-19 07:51:42 +00003860 }
Chris Lattner4234f572007-03-25 02:14:49 +00003861 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00003862}
3863
Bob Wilson2dc4f542009-03-20 22:42:55 +00003864std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00003865ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003866 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003867 if (Constraint.size() == 1) {
3868 // GCC RS6000 Constraint Letters
3869 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003870 case 'l':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003871 if (Subtarget->isThumb1Only())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003872 return std::make_pair(0U, ARM::tGPRRegisterClass);
3873 else
3874 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003875 case 'r':
3876 return std::make_pair(0U, ARM::GPRRegisterClass);
3877 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003878 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003879 return std::make_pair(0U, ARM::SPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00003880 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003881 return std::make_pair(0U, ARM::DPRRegisterClass);
3882 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003883 }
3884 }
3885 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3886}
3887
3888std::vector<unsigned> ARMTargetLowering::
3889getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003890 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003891 if (Constraint.size() != 1)
3892 return std::vector<unsigned>();
3893
3894 switch (Constraint[0]) { // GCC ARM Constraint Letters
3895 default: break;
3896 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003897 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3898 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3899 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003900 case 'r':
3901 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3902 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3903 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
3904 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003905 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003906 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003907 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
3908 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
3909 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
3910 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
3911 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
3912 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
3913 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
3914 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00003915 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003916 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
3917 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
3918 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
3919 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
3920 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003921 }
3922
3923 return std::vector<unsigned>();
3924}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003925
3926/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3927/// vector. If it is invalid, don't add anything to Ops.
3928void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3929 char Constraint,
3930 bool hasMemory,
3931 std::vector<SDValue>&Ops,
3932 SelectionDAG &DAG) const {
3933 SDValue Result(0, 0);
3934
3935 switch (Constraint) {
3936 default: break;
3937 case 'I': case 'J': case 'K': case 'L':
3938 case 'M': case 'N': case 'O':
3939 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
3940 if (!C)
3941 return;
3942
3943 int64_t CVal64 = C->getSExtValue();
3944 int CVal = (int) CVal64;
3945 // None of these constraints allow values larger than 32 bits. Check
3946 // that the value fits in an int.
3947 if (CVal != CVal64)
3948 return;
3949
3950 switch (Constraint) {
3951 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003952 if (Subtarget->isThumb1Only()) {
3953 // This must be a constant between 0 and 255, for ADD
3954 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003955 if (CVal >= 0 && CVal <= 255)
3956 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003957 } else if (Subtarget->isThumb2()) {
3958 // A constant that can be used as an immediate value in a
3959 // data-processing instruction.
3960 if (ARM_AM::getT2SOImmVal(CVal) != -1)
3961 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003962 } else {
3963 // A constant that can be used as an immediate value in a
3964 // data-processing instruction.
3965 if (ARM_AM::getSOImmVal(CVal) != -1)
3966 break;
3967 }
3968 return;
3969
3970 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003971 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003972 // This must be a constant between -255 and -1, for negated ADD
3973 // immediates. This can be used in GCC with an "n" modifier that
3974 // prints the negated value, for use with SUB instructions. It is
3975 // not useful otherwise but is implemented for compatibility.
3976 if (CVal >= -255 && CVal <= -1)
3977 break;
3978 } else {
3979 // This must be a constant between -4095 and 4095. It is not clear
3980 // what this constraint is intended for. Implemented for
3981 // compatibility with GCC.
3982 if (CVal >= -4095 && CVal <= 4095)
3983 break;
3984 }
3985 return;
3986
3987 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003988 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003989 // A 32-bit value where only one byte has a nonzero value. Exclude
3990 // zero to match GCC. This constraint is used by GCC internally for
3991 // constants that can be loaded with a move/shift combination.
3992 // It is not useful otherwise but is implemented for compatibility.
3993 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
3994 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00003995 } else if (Subtarget->isThumb2()) {
3996 // A constant whose bitwise inverse can be used as an immediate
3997 // value in a data-processing instruction. This can be used in GCC
3998 // with a "B" modifier that prints the inverted value, for use with
3999 // BIC and MVN instructions. It is not useful otherwise but is
4000 // implemented for compatibility.
4001 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
4002 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004003 } else {
4004 // A constant whose bitwise inverse can be used as an immediate
4005 // value in a data-processing instruction. This can be used in GCC
4006 // with a "B" modifier that prints the inverted value, for use with
4007 // BIC and MVN instructions. It is not useful otherwise but is
4008 // implemented for compatibility.
4009 if (ARM_AM::getSOImmVal(~CVal) != -1)
4010 break;
4011 }
4012 return;
4013
4014 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004015 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004016 // This must be a constant between -7 and 7,
4017 // for 3-operand ADD/SUB immediate instructions.
4018 if (CVal >= -7 && CVal < 7)
4019 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004020 } else if (Subtarget->isThumb2()) {
4021 // A constant whose negation can be used as an immediate value in a
4022 // data-processing instruction. This can be used in GCC with an "n"
4023 // modifier that prints the negated value, for use with SUB
4024 // instructions. It is not useful otherwise but is implemented for
4025 // compatibility.
4026 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
4027 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004028 } else {
4029 // A constant whose negation can be used as an immediate value in a
4030 // data-processing instruction. This can be used in GCC with an "n"
4031 // modifier that prints the negated value, for use with SUB
4032 // instructions. It is not useful otherwise but is implemented for
4033 // compatibility.
4034 if (ARM_AM::getSOImmVal(-CVal) != -1)
4035 break;
4036 }
4037 return;
4038
4039 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004040 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004041 // This must be a multiple of 4 between 0 and 1020, for
4042 // ADD sp + immediate.
4043 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
4044 break;
4045 } else {
4046 // A power of two or a constant between 0 and 32. This is used in
4047 // GCC for the shift amount on shifted register operands, but it is
4048 // useful in general for any shift amounts.
4049 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
4050 break;
4051 }
4052 return;
4053
4054 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004055 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004056 // This must be a constant between 0 and 31, for shift amounts.
4057 if (CVal >= 0 && CVal <= 31)
4058 break;
4059 }
4060 return;
4061
4062 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004063 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004064 // This must be a multiple of 4 between -508 and 508, for
4065 // ADD/SUB sp = sp + immediate.
4066 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
4067 break;
4068 }
4069 return;
4070 }
4071 Result = DAG.getTargetConstant(CVal, Op.getValueType());
4072 break;
4073 }
4074
4075 if (Result.getNode()) {
4076 Ops.push_back(Result);
4077 return;
4078 }
4079 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
4080 Ops, DAG);
4081}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00004082
4083bool
4084ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4085 // The ARM target isn't yet aware of offsets.
4086 return false;
4087}
Evan Cheng39382422009-10-28 01:44:26 +00004088
4089int ARM::getVFPf32Imm(const APFloat &FPImm) {
4090 APInt Imm = FPImm.bitcastToAPInt();
4091 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
4092 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
4093 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
4094
4095 // We can handle 4 bits of mantissa.
4096 // mantissa = (16+UInt(e:f:g:h))/16.
4097 if (Mantissa & 0x7ffff)
4098 return -1;
4099 Mantissa >>= 19;
4100 if ((Mantissa & 0xf) != Mantissa)
4101 return -1;
4102
4103 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4104 if (Exp < -3 || Exp > 4)
4105 return -1;
4106 Exp = ((Exp+3) & 0x7) ^ 4;
4107
4108 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4109}
4110
4111int ARM::getVFPf64Imm(const APFloat &FPImm) {
4112 APInt Imm = FPImm.bitcastToAPInt();
4113 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
4114 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
4115 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
4116
4117 // We can handle 4 bits of mantissa.
4118 // mantissa = (16+UInt(e:f:g:h))/16.
4119 if (Mantissa & 0xffffffffffffLL)
4120 return -1;
4121 Mantissa >>= 48;
4122 if ((Mantissa & 0xf) != Mantissa)
4123 return -1;
4124
4125 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4126 if (Exp < -3 || Exp > 4)
4127 return -1;
4128 Exp = ((Exp+3) & 0x7) ^ 4;
4129
4130 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4131}
4132
4133/// isFPImmLegal - Returns true if the target can instruction select the
4134/// specified FP immediate natively. If false, the legalizer will
4135/// materialize the FP immediate as a load from a constant pool.
4136bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
4137 if (!Subtarget->hasVFP3())
4138 return false;
4139 if (VT == MVT::f32)
4140 return ARM::getVFPf32Imm(Imm) != -1;
4141 if (VT == MVT::f64)
4142 return ARM::getVFPf64Imm(Imm) != -1;
4143 return false;
4144}