blob: c3af8e695fc832f8399c256af4761048d91e1ee2 [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)
Evan Chenge7e0d622009-11-06 22:24:13 +0000136 : TargetLowering(TM, createTLOF(TM)) {
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);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000335 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000336 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())
Jim Grosbache5165492009-11-09 00:11:35 +0000392 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR 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:
Jim Grosbache5165492009-11-09 00:11:35 +0000437 // ARMISD::VMOVRRD - 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
Jim Grosbache5165492009-11-09 00:11:35 +0000496 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
497 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
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);
Jim Grosbache5165492009-11-09 00:11:35 +0000793 Val = DAG.getNode(ARMISD::VMOVDRR, 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);
Jim Grosbache5165492009-11-09 00:11:35 +0000808 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +0000809 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
Jim Grosbache5165492009-11-09 00:11:35 +0000873 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, 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 Chenge7e0d622009-11-06 22:24:13 +00001007 MachineFunction &MF = DAG.getMachineFunction();
1008 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chenga8e29892007-01-19 07:51:42 +00001009 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1010 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001011 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001012 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001013 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001014 getTargetMachine().getRelocationModel() != Reloc::Static;
1015 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001016 // ARM call to a local ARM function is predicable.
1017 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +00001018 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001019 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001020 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001021 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001022 ARMPCLabelIndex,
1023 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001024 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001025 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001026 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001027 DAG.getEntryNode(), CPAddr,
1028 PseudoSourceValue::getConstantPool(), 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001029 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001030 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001031 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001032 } else
1033 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001034 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001035 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001036 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001037 getTargetMachine().getRelocationModel() != Reloc::Static;
1038 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001039 // tBX takes a register source operand.
1040 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001041 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001042 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001043 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001044 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001045 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001046 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001047 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001048 DAG.getEntryNode(), CPAddr,
1049 PseudoSourceValue::getConstantPool(), 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001050 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001051 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001052 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +00001053 } else
Bill Wendling056292f2008-09-16 21:48:12 +00001054 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001055 }
1056
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001057 // FIXME: handle tail calls differently.
1058 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001059 if (Subtarget->isThumb()) {
1060 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001061 CallOpc = ARMISD::CALL_NOLINK;
1062 else
1063 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1064 } else {
1065 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001066 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1067 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001068 }
David Goodwinf1daf7d2009-07-08 23:10:31 +00001069 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +00001070 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Owen Anderson825b72b2009-08-11 20:47:22 +00001071 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001072 InFlag = Chain.getValue(1);
1073 }
1074
Dan Gohman475871a2008-07-27 21:46:04 +00001075 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001076 Ops.push_back(Chain);
1077 Ops.push_back(Callee);
1078
1079 // Add argument registers to the end of the list so that they are known live
1080 // into the call.
1081 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1082 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1083 RegsToPass[i].second.getValueType()));
1084
Gabor Greifba36cb52008-08-28 21:40:38 +00001085 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001086 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +00001087 // Returns a chain and a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00001088 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +00001089 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001090 InFlag = Chain.getValue(1);
1091
Chris Lattnere563bbc2008-10-11 22:08:30 +00001092 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1093 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001094 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001095 InFlag = Chain.getValue(1);
1096
Bob Wilson1f595bb2009-04-17 19:07:39 +00001097 // Handle result values, copying them out of physregs into vregs that we
1098 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001099 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1100 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001101}
1102
Dan Gohman98ca4f22009-08-05 01:29:28 +00001103SDValue
1104ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001105 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001106 const SmallVectorImpl<ISD::OutputArg> &Outs,
1107 DebugLoc dl, SelectionDAG &DAG) {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001108
Bob Wilsondee46d72009-04-17 20:35:10 +00001109 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001110 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001111
Bob Wilsondee46d72009-04-17 20:35:10 +00001112 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001113 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1114 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001115
Dan Gohman98ca4f22009-08-05 01:29:28 +00001116 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001117 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1118 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001119
1120 // If this is the first return lowered for this function, add
1121 // the regs to the liveout set for the function.
1122 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1123 for (unsigned i = 0; i != RVLocs.size(); ++i)
1124 if (RVLocs[i].isRegLoc())
1125 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001126 }
1127
Bob Wilson1f595bb2009-04-17 19:07:39 +00001128 SDValue Flag;
1129
1130 // Copy the result values into the output registers.
1131 for (unsigned i = 0, realRVLocIdx = 0;
1132 i != RVLocs.size();
1133 ++i, ++realRVLocIdx) {
1134 CCValAssign &VA = RVLocs[i];
1135 assert(VA.isRegLoc() && "Can only return in registers!");
1136
Dan Gohman98ca4f22009-08-05 01:29:28 +00001137 SDValue Arg = Outs[realRVLocIdx].Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001138
1139 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001140 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001141 case CCValAssign::Full: break;
1142 case CCValAssign::BCvt:
1143 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1144 break;
1145 }
1146
Bob Wilson1f595bb2009-04-17 19:07:39 +00001147 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001148 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001149 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001150 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1151 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001152 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001153 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001154
1155 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1156 Flag = Chain.getValue(1);
1157 VA = RVLocs[++i]; // skip ahead to next loc
1158 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1159 HalfGPRs.getValue(1), Flag);
1160 Flag = Chain.getValue(1);
1161 VA = RVLocs[++i]; // skip ahead to next loc
1162
1163 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001164 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1165 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001166 }
1167 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1168 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001169 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001170 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001171 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001172 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001173 VA = RVLocs[++i]; // skip ahead to next loc
1174 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1175 Flag);
1176 } else
1177 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1178
Bob Wilsondee46d72009-04-17 20:35:10 +00001179 // Guarantee that all emitted copies are
1180 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001181 Flag = Chain.getValue(1);
1182 }
1183
1184 SDValue result;
1185 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001186 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001187 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001188 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001189
1190 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001191}
1192
Bob Wilsonb62d2572009-11-03 00:02:05 +00001193// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1194// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1195// one of the above mentioned nodes. It has to be wrapped because otherwise
1196// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1197// be used to form addressing mode. These wrapped nodes will be selected
1198// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001199static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001200 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001201 // FIXME there is no actual debug info here
1202 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001203 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001204 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001205 if (CP->isMachineConstantPoolEntry())
1206 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1207 CP->getAlignment());
1208 else
1209 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1210 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001211 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001212}
1213
Bob Wilsonddb16df2009-10-30 05:45:42 +00001214SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001215 MachineFunction &MF = DAG.getMachineFunction();
1216 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1217 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001218 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001219 EVT PtrVT = getPointerTy();
Bob Wilsonddb16df2009-10-30 05:45:42 +00001220 BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001221 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1222 SDValue CPAddr;
1223 if (RelocM == Reloc::Static) {
1224 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1225 } else {
1226 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001227 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001228 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1229 ARMCP::CPBlockAddress,
1230 PCAdj);
1231 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1232 }
1233 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1234 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
1235 PseudoSourceValue::getConstantPool(), 0);
1236 if (RelocM == Reloc::Static)
1237 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001238 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001239 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001240}
1241
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001242// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001243SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001244ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1245 SelectionDAG &DAG) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001246 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001247 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001248 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001249 MachineFunction &MF = DAG.getMachineFunction();
1250 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1251 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001252 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001253 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001254 ARMCP::CPValue, PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001255 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001256 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001257 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
1258 PseudoSourceValue::getConstantPool(), 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001259 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001260
Evan Chenge7e0d622009-11-06 22:24:13 +00001261 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001262 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001263
1264 // call __tls_get_addr.
1265 ArgListTy Args;
1266 ArgListEntry Entry;
1267 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001268 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001269 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001270 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001271 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001272 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1273 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001274 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001275 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001276 return CallResult.first;
1277}
1278
1279// Lower ISD::GlobalTLSAddress using the "initial exec" or
1280// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001281SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001282ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001283 SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001284 GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001285 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001286 SDValue Offset;
1287 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001288 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001289 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001290 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001291
Chris Lattner4fb63d02009-07-15 04:12:33 +00001292 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001293 MachineFunction &MF = DAG.getMachineFunction();
1294 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1295 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1296 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001297 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1298 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001299 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001300 ARMCP::CPValue, PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001301 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001302 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001303 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
1304 PseudoSourceValue::getConstantPool(), 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001305 Chain = Offset.getValue(1);
1306
Evan Chenge7e0d622009-11-06 22:24:13 +00001307 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001308 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001309
Evan Cheng9eda6892009-10-31 03:39:36 +00001310 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
1311 PseudoSourceValue::getConstantPool(), 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001312 } else {
1313 // local exec model
Evan Chenge4e4ed32009-08-28 23:18:09 +00001314 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001315 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001316 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001317 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
1318 PseudoSourceValue::getConstantPool(), 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001319 }
1320
1321 // The address of the thread local variable is the add of the thread
1322 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001323 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001324}
1325
Dan Gohman475871a2008-07-27 21:46:04 +00001326SDValue
1327ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001328 // TODO: implement the "local dynamic" model
1329 assert(Subtarget->isTargetELF() &&
1330 "TLS not implemented for non-ELF targets");
1331 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1332 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1333 // otherwise use the "Local Exec" TLS Model
1334 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1335 return LowerToTLSGeneralDynamicModel(GA, DAG);
1336 else
1337 return LowerToTLSExecModels(GA, DAG);
1338}
1339
Dan Gohman475871a2008-07-27 21:46:04 +00001340SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001341 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001342 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001343 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001344 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1345 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1346 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001347 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001348 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001349 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +00001350 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001351 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001352 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001353 CPAddr,
1354 PseudoSourceValue::getConstantPool(), 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001355 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001356 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001357 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001358 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001359 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
1360 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001361 return Result;
1362 } else {
Evan Cheng1606e8e2009-03-13 07:51:59 +00001363 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001364 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001365 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1366 PseudoSourceValue::getConstantPool(), 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001367 }
1368}
1369
Dan Gohman475871a2008-07-27 21:46:04 +00001370SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001371 SelectionDAG &DAG) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001372 MachineFunction &MF = DAG.getMachineFunction();
1373 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1374 unsigned ARMPCLabelIndex = 0;
Owen Andersone50ed302009-08-10 22:56:29 +00001375 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001376 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001377 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1378 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001379 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001380 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001381 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001382 else {
Evan Chenge7e0d622009-11-06 22:24:13 +00001383 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001384 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1385 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001386 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001387 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001388 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001389 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001390
Evan Cheng9eda6892009-10-31 03:39:36 +00001391 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1392 PseudoSourceValue::getConstantPool(), 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001393 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001394
1395 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001396 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001397 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001398 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001399
Evan Cheng63476a82009-09-03 07:04:02 +00001400 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Evan Cheng9eda6892009-10-31 03:39:36 +00001401 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
1402 PseudoSourceValue::getGOT(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001403
1404 return Result;
1405}
1406
Dan Gohman475871a2008-07-27 21:46:04 +00001407SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001408 SelectionDAG &DAG){
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001409 assert(Subtarget->isTargetELF() &&
1410 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00001411 MachineFunction &MF = DAG.getMachineFunction();
1412 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1413 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Andersone50ed302009-08-10 22:56:29 +00001414 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001415 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001416 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00001417 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1418 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00001419 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001420 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001421 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001422 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1423 PseudoSourceValue::getConstantPool(), 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001424 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001425 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001426}
1427
Jim Grosbach0e0da732009-05-12 23:59:14 +00001428SDValue
1429ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001430 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001431 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001432 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001433 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00001434 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00001435 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00001436 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1437 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001438 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001439 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00001440 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1441 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001442 EVT PtrVT = getPointerTy();
1443 DebugLoc dl = Op.getDebugLoc();
1444 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1445 SDValue CPAddr;
1446 unsigned PCAdj = (RelocM != Reloc::PIC_)
1447 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001448 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001449 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1450 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001451 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001452 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001453 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00001454 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1455 PseudoSourceValue::getConstantPool(), 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001456 SDValue Chain = Result.getValue(1);
1457
1458 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001459 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00001460 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1461 }
1462 return Result;
1463 }
Jim Grosbachf9570122009-05-14 00:46:35 +00001464 case Intrinsic::eh_sjlj_setjmp:
Owen Anderson825b72b2009-08-11 20:47:22 +00001465 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1));
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001466 }
1467}
1468
Dan Gohman475871a2008-07-27 21:46:04 +00001469static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001470 unsigned VarArgsFrameIndex) {
Evan Chenga8e29892007-01-19 07:51:42 +00001471 // vastart just stores the address of the VarArgsFrameIndex slot into the
1472 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001473 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001474 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001475 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001476 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001477 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001478}
1479
Dan Gohman475871a2008-07-27 21:46:04 +00001480SDValue
Evan Cheng86198642009-08-07 00:34:42 +00001481ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
1482 SDNode *Node = Op.getNode();
1483 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001484 EVT VT = Node->getValueType(0);
Evan Cheng86198642009-08-07 00:34:42 +00001485 SDValue Chain = Op.getOperand(0);
1486 SDValue Size = Op.getOperand(1);
1487 SDValue Align = Op.getOperand(2);
1488
1489 // Chain the dynamic stack allocation so that it doesn't modify the stack
1490 // pointer when other instructions are using the stack.
1491 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1492
1493 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1494 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1495 if (AlignVal > StackAlign)
1496 // Do this now since selection pass cannot introduce new target
1497 // independent node.
1498 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1499
1500 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1501 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1502 // do even more horrible hack later.
1503 MachineFunction &MF = DAG.getMachineFunction();
1504 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1505 if (AFI->isThumb1OnlyFunction()) {
1506 bool Negate = true;
1507 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1508 if (C) {
1509 uint32_t Val = C->getZExtValue();
1510 if (Val <= 508 && ((Val & 3) == 0))
1511 Negate = false;
1512 }
1513 if (Negate)
1514 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1515 }
1516
Owen Anderson825b72b2009-08-11 20:47:22 +00001517 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
Evan Cheng86198642009-08-07 00:34:42 +00001518 SDValue Ops1[] = { Chain, Size, Align };
1519 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1520 Chain = Res.getValue(1);
1521 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1522 DAG.getIntPtrConstant(0, true), SDValue());
1523 SDValue Ops2[] = { Res, Chain };
1524 return DAG.getMergeValues(Ops2, 2, dl);
1525}
1526
1527SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00001528ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1529 SDValue &Root, SelectionDAG &DAG,
1530 DebugLoc dl) {
1531 MachineFunction &MF = DAG.getMachineFunction();
1532 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1533
1534 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001535 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00001536 RC = ARM::tGPRRegisterClass;
1537 else
1538 RC = ARM::GPRRegisterClass;
1539
1540 // Transform the arguments stored in physical registers into virtual ones.
1541 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001542 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001543
1544 SDValue ArgValue2;
1545 if (NextVA.isMemLoc()) {
1546 unsigned ArgSize = NextVA.getLocVT().getSizeInBits()/8;
1547 MachineFrameInfo *MFI = MF.getFrameInfo();
David Greene3f2bf852009-11-12 20:49:22 +00001548 int FI = MFI->CreateFixedObject(ArgSize, NextVA.getLocMemOffset(),
1549 true, false);
Bob Wilson5bafff32009-06-22 23:27:02 +00001550
1551 // Create load node to retrieve arguments from the stack.
1552 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001553 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
1554 PseudoSourceValue::getFixedStack(FI), 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00001555 } else {
1556 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001557 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00001558 }
1559
Jim Grosbache5165492009-11-09 00:11:35 +00001560 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00001561}
1562
1563SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001564ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001565 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001566 const SmallVectorImpl<ISD::InputArg>
1567 &Ins,
1568 DebugLoc dl, SelectionDAG &DAG,
1569 SmallVectorImpl<SDValue> &InVals) {
1570
Bob Wilson1f595bb2009-04-17 19:07:39 +00001571 MachineFunction &MF = DAG.getMachineFunction();
1572 MachineFrameInfo *MFI = MF.getFrameInfo();
1573
Bob Wilson1f595bb2009-04-17 19:07:39 +00001574 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1575
1576 // Assign locations to all of the incoming arguments.
1577 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001578 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1579 *DAG.getContext());
1580 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001581 CCAssignFnForNode(CallConv, /* Return*/ false,
1582 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001583
1584 SmallVector<SDValue, 16> ArgValues;
1585
1586 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1587 CCValAssign &VA = ArgLocs[i];
1588
Bob Wilsondee46d72009-04-17 20:35:10 +00001589 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001590 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001591 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00001592
Bob Wilson5bafff32009-06-22 23:27:02 +00001593 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001594 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001595 // f64 and vector types are split up into multiple registers or
1596 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00001597 RegVT = MVT::i32;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001598
Owen Anderson825b72b2009-08-11 20:47:22 +00001599 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001600 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001601 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00001602 VA = ArgLocs[++i]; // skip ahead to next loc
1603 SDValue ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00001604 Chain, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001605 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1606 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001607 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00001608 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00001609 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1610 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001611 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001612
Bob Wilson5bafff32009-06-22 23:27:02 +00001613 } else {
1614 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001615
Owen Anderson825b72b2009-08-11 20:47:22 +00001616 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00001617 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001618 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00001619 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001620 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001621 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001622 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001623 RC = (AFI->isThumb1OnlyFunction() ?
1624 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00001625 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00001626 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00001627
1628 // Transform the arguments in physical registers into virtual ones.
1629 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001630 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001631 }
1632
1633 // If this is an 8 or 16-bit value, it is really passed promoted
1634 // to 32 bits. Insert an assert[sz]ext to capture this, then
1635 // truncate to the right size.
1636 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001637 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001638 case CCValAssign::Full: break;
1639 case CCValAssign::BCvt:
1640 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1641 break;
1642 case CCValAssign::SExt:
1643 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1644 DAG.getValueType(VA.getValVT()));
1645 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1646 break;
1647 case CCValAssign::ZExt:
1648 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1649 DAG.getValueType(VA.getValVT()));
1650 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1651 break;
1652 }
1653
Dan Gohman98ca4f22009-08-05 01:29:28 +00001654 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001655
1656 } else { // VA.isRegLoc()
1657
1658 // sanity check
1659 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00001660 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001661
1662 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001663 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
1664 true, false);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001665
Bob Wilsondee46d72009-04-17 20:35:10 +00001666 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001667 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00001668 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
1669 PseudoSourceValue::getFixedStack(FI), 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001670 }
1671 }
1672
1673 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001674 if (isVarArg) {
1675 static const unsigned GPRArgRegs[] = {
1676 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1677 };
1678
Bob Wilsondee46d72009-04-17 20:35:10 +00001679 unsigned NumGPRs = CCInfo.getFirstUnallocated
1680 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001681
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001682 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1683 unsigned VARegSize = (4 - NumGPRs) * 4;
1684 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00001685 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001686 if (VARegSaveSize) {
1687 // If this function is vararg, store any remaining integer argument regs
1688 // to their spots on the stack so that they may be loaded by deferencing
1689 // the result of va_next.
1690 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001691 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
David Greene3f2bf852009-11-12 20:49:22 +00001692 VARegSaveSize - VARegSize,
1693 true, false);
Dan Gohman475871a2008-07-27 21:46:04 +00001694 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001695
Dan Gohman475871a2008-07-27 21:46:04 +00001696 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001697 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001698 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00001699 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001700 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001701 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001702 RC = ARM::GPRRegisterClass;
1703
Bob Wilson998e1252009-04-20 18:36:57 +00001704 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00001705 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Evan Cheng9eda6892009-10-31 03:39:36 +00001706 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
1707 PseudoSourceValue::getFixedStack(VarArgsFrameIndex), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001708 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001709 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001710 DAG.getConstant(4, getPointerTy()));
1711 }
1712 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001713 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001714 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001715 } else
1716 // This will point to the next argument passed via stack.
David Greene3f2bf852009-11-12 20:49:22 +00001717 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset, true, false);
Evan Chenga8e29892007-01-19 07:51:42 +00001718 }
1719
Dan Gohman98ca4f22009-08-05 01:29:28 +00001720 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00001721}
1722
1723/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001724static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001725 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001726 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001727 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001728 // Maybe this has already been legalized into the constant pool?
1729 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001730 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001731 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1732 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001733 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001734 }
1735 }
1736 return false;
1737}
1738
Evan Chenga8e29892007-01-19 07:51:42 +00001739/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1740/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00001741SDValue
1742ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
1743 SDValue &ARMCC, SelectionDAG &DAG, DebugLoc dl) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001744 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001745 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00001746 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001747 // Constant does not fit, try adjusting it by one?
1748 switch (CC) {
1749 default: break;
1750 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001751 case ISD::SETGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00001752 if (isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001753 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001754 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001755 }
1756 break;
1757 case ISD::SETULT:
1758 case ISD::SETUGE:
Evan Cheng06b53c02009-11-12 07:13:11 +00001759 if (C > 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001760 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00001761 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001762 }
1763 break;
1764 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001765 case ISD::SETGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00001766 if (isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001767 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001768 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00001769 }
1770 break;
1771 case ISD::SETULE:
1772 case ISD::SETUGT:
Evan Cheng06b53c02009-11-12 07:13:11 +00001773 if (C < 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001774 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00001775 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001776 }
1777 break;
1778 }
1779 }
1780 }
1781
1782 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001783 ARMISD::NodeType CompareType;
1784 switch (CondCode) {
1785 default:
1786 CompareType = ARMISD::CMP;
1787 break;
1788 case ARMCC::EQ:
1789 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00001790 // Uses only Z Flag
1791 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001792 break;
1793 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001794 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1795 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001796}
1797
1798/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001799static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001800 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001801 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001802 if (!isFloatingPointZero(RHS))
Owen Anderson825b72b2009-08-11 20:47:22 +00001803 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001804 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001805 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1806 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001807}
1808
Evan Cheng06b53c02009-11-12 07:13:11 +00001809SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001810 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001811 SDValue LHS = Op.getOperand(0);
1812 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001813 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001814 SDValue TrueVal = Op.getOperand(2);
1815 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001816 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001817
Owen Anderson825b72b2009-08-11 20:47:22 +00001818 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001819 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001820 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00001821 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Dale Johannesende064702009-02-06 21:50:26 +00001822 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001823 }
1824
1825 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001826 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00001827
Owen Anderson825b72b2009-08-11 20:47:22 +00001828 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1829 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001830 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1831 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001832 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001833 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001834 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001835 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00001836 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001837 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00001838 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001839 }
1840 return Result;
1841}
1842
Evan Cheng06b53c02009-11-12 07:13:11 +00001843SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00001844 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001845 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001846 SDValue LHS = Op.getOperand(2);
1847 SDValue RHS = Op.getOperand(3);
1848 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00001849 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001850
Owen Anderson825b72b2009-08-11 20:47:22 +00001851 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001852 SDValue ARMCC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001853 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng06b53c02009-11-12 07:13:11 +00001854 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001855 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00001856 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001857 }
1858
Owen Anderson825b72b2009-08-11 20:47:22 +00001859 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Chenga8e29892007-01-19 07:51:42 +00001860 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001861 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001862
Dale Johannesende064702009-02-06 21:50:26 +00001863 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001864 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1865 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1866 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001867 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00001868 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001869 if (CondCode2 != ARMCC::AL) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001870 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001871 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00001872 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001873 }
1874 return Res;
1875}
1876
Dan Gohman475871a2008-07-27 21:46:04 +00001877SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) {
1878 SDValue Chain = Op.getOperand(0);
1879 SDValue Table = Op.getOperand(1);
1880 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001881 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001882
Owen Andersone50ed302009-08-10 22:56:29 +00001883 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00001884 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1885 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00001886 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00001887 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00001888 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00001889 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
1890 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00001891 if (Subtarget->isThumb2()) {
1892 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
1893 // which does another jump to the destination. This also makes it easier
1894 // to translate it to TBB / TBH later.
1895 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00001896 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00001897 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001898 }
Evan Cheng66ac5312009-07-25 00:33:29 +00001899 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00001900 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
1901 PseudoSourceValue::getJumpTable(), 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00001902 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001903 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00001904 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001905 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00001906 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
1907 PseudoSourceValue::getJumpTable(), 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00001908 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00001909 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00001910 }
Evan Chenga8e29892007-01-19 07:51:42 +00001911}
1912
Dan Gohman475871a2008-07-27 21:46:04 +00001913static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00001914 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001915 unsigned Opc =
1916 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
Owen Anderson825b72b2009-08-11 20:47:22 +00001917 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
1918 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001919}
1920
Dan Gohman475871a2008-07-27 21:46:04 +00001921static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001922 EVT VT = Op.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001923 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001924 unsigned Opc =
1925 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1926
Owen Anderson825b72b2009-08-11 20:47:22 +00001927 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
Dale Johannesende064702009-02-06 21:50:26 +00001928 return DAG.getNode(Opc, dl, VT, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001929}
1930
Dan Gohman475871a2008-07-27 21:46:04 +00001931static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001932 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00001933 SDValue Tmp0 = Op.getOperand(0);
1934 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00001935 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001936 EVT VT = Op.getValueType();
1937 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001938 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
1939 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001940 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1941 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001942 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001943}
1944
Jim Grosbach0e0da732009-05-12 23:59:14 +00001945SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
1946 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1947 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00001948 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00001949 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
1950 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00001951 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00001952 ? ARM::R7 : ARM::R11;
1953 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
1954 while (Depth--)
1955 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
1956 return FrameAddr;
1957}
1958
Dan Gohman475871a2008-07-27 21:46:04 +00001959SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00001960ARMTargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001961 SDValue Chain,
1962 SDValue Dst, SDValue Src,
1963 SDValue Size, unsigned Align,
Dan Gohman707e0182008-04-12 04:36:06 +00001964 bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00001965 const Value *DstSV, uint64_t DstSVOff,
1966 const Value *SrcSV, uint64_t SrcSVOff){
Evan Cheng4102eb52007-10-22 22:11:27 +00001967 // Do repeated 4-byte loads and stores. To be improved.
Dan Gohman707e0182008-04-12 04:36:06 +00001968 // This requires 4-byte alignment.
1969 if ((Align & 3) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00001970 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001971 // This requires the copy size to be a constant, preferrably
1972 // within a subtarget-specific limit.
1973 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
1974 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00001975 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001976 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001977 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00001978 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001979
1980 unsigned BytesLeft = SizeVal & 3;
1981 unsigned NumMemOps = SizeVal >> 2;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001982 unsigned EmittedNumMemOps = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001983 EVT VT = MVT::i32;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001984 unsigned VTSize = 4;
Evan Cheng4102eb52007-10-22 22:11:27 +00001985 unsigned i = 0;
Evan Chenge5e7ce42007-05-18 01:19:57 +00001986 const unsigned MAX_LOADS_IN_LDM = 6;
Dan Gohman475871a2008-07-27 21:46:04 +00001987 SDValue TFOps[MAX_LOADS_IN_LDM];
1988 SDValue Loads[MAX_LOADS_IN_LDM];
Dan Gohman1f13c682008-04-28 17:15:20 +00001989 uint64_t SrcOff = 0, DstOff = 0;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001990
Evan Cheng4102eb52007-10-22 22:11:27 +00001991 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
1992 // same number of stores. The loads and stores will get combined into
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001993 // ldm/stm later on.
Evan Cheng4102eb52007-10-22 22:11:27 +00001994 while (EmittedNumMemOps < NumMemOps) {
1995 for (i = 0;
1996 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001997 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00001998 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
1999 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002000 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002001 TFOps[i] = Loads[i].getValue(1);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002002 SrcOff += VTSize;
2003 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002004 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002005
Evan Cheng4102eb52007-10-22 22:11:27 +00002006 for (i = 0;
2007 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00002008 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00002009 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
2010 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002011 DstSV, DstSVOff + DstOff);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002012 DstOff += VTSize;
2013 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002014 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00002015
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002016 EmittedNumMemOps += i;
2017 }
2018
Bob Wilson2dc4f542009-03-20 22:42:55 +00002019 if (BytesLeft == 0)
Evan Cheng4102eb52007-10-22 22:11:27 +00002020 return Chain;
2021
2022 // Issue loads / stores for the trailing (1 - 3) bytes.
2023 unsigned BytesLeftSave = BytesLeft;
2024 i = 0;
2025 while (BytesLeft) {
2026 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002027 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00002028 VTSize = 2;
2029 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00002030 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00002031 VTSize = 1;
2032 }
2033
Dale Johannesen0f502f62009-02-03 22:26:09 +00002034 Loads[i] = DAG.getLoad(VT, dl, Chain,
Owen Anderson825b72b2009-08-11 20:47:22 +00002035 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
2036 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002037 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002038 TFOps[i] = Loads[i].getValue(1);
2039 ++i;
2040 SrcOff += VTSize;
2041 BytesLeft -= VTSize;
2042 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002043 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00002044
2045 i = 0;
2046 BytesLeft = BytesLeftSave;
2047 while (BytesLeft) {
2048 if (BytesLeft >= 2) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002049 VT = MVT::i16;
Evan Cheng4102eb52007-10-22 22:11:27 +00002050 VTSize = 2;
2051 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00002052 VT = MVT::i8;
Evan Cheng4102eb52007-10-22 22:11:27 +00002053 VTSize = 1;
2054 }
2055
Dale Johannesen0f502f62009-02-03 22:26:09 +00002056 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Owen Anderson825b72b2009-08-11 20:47:22 +00002057 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
2058 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00002059 DstSV, DstSVOff + DstOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00002060 ++i;
2061 DstOff += VTSize;
2062 BytesLeft -= VTSize;
2063 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002064 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00002065}
2066
Duncan Sands1607f052008-12-01 11:39:25 +00002067static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00002068 SDValue Op = N->getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +00002069 DebugLoc dl = N->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002070 if (N->getValueType(0) == MVT::f64) {
Jim Grosbache5165492009-11-09 00:11:35 +00002071 // Turn i64->f64 into VMOVDRR.
Owen Anderson825b72b2009-08-11 20:47:22 +00002072 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2073 DAG.getConstant(0, MVT::i32));
2074 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2075 DAG.getConstant(1, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00002076 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00002077 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002078
Jim Grosbache5165492009-11-09 00:11:35 +00002079 // Turn f64->i64 into VMOVRRD.
2080 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002081 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002082
Chris Lattner27a6c732007-11-24 07:07:01 +00002083 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002084 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
Chris Lattner27a6c732007-11-24 07:07:01 +00002085}
2086
Bob Wilson5bafff32009-06-22 23:27:02 +00002087/// getZeroVector - Returns a vector of specified type with all zero elements.
2088///
Owen Andersone50ed302009-08-10 22:56:29 +00002089static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002090 assert(VT.isVector() && "Expected a vector type");
2091
2092 // Zero vectors are used to represent vector negation and in those cases
2093 // will be implemented with the NEON VNEG instruction. However, VNEG does
2094 // not support i64 elements, so sometimes the zero vectors will need to be
2095 // explicitly constructed. For those cases, and potentially other uses in
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002096 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
Bob Wilson5bafff32009-06-22 23:27:02 +00002097 // to their dest type. This ensures they get CSE'd.
2098 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002099 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2100 SmallVector<SDValue, 8> Ops;
2101 MVT TVT;
2102
2103 if (VT.getSizeInBits() == 64) {
2104 Ops.assign(8, Cst); TVT = MVT::v8i8;
2105 } else {
2106 Ops.assign(16, Cst); TVT = MVT::v16i8;
2107 }
2108 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002109
2110 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2111}
2112
2113/// getOnesVector - Returns a vector of specified type with all bits set.
2114///
Owen Andersone50ed302009-08-10 22:56:29 +00002115static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002116 assert(VT.isVector() && "Expected a vector type");
2117
Bob Wilson929ffa22009-10-30 20:13:25 +00002118 // Always build ones vectors as <16 x i8> or <8 x i8> bitcasted to their
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002119 // dest type. This ensures they get CSE'd.
Bob Wilson5bafff32009-06-22 23:27:02 +00002120 SDValue Vec;
Anton Korobeynikov2ba62ef2009-09-08 22:51:43 +00002121 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2122 SmallVector<SDValue, 8> Ops;
2123 MVT TVT;
2124
2125 if (VT.getSizeInBits() == 64) {
2126 Ops.assign(8, Cst); TVT = MVT::v8i8;
2127 } else {
2128 Ops.assign(16, Cst); TVT = MVT::v16i8;
2129 }
2130 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
Bob Wilson5bafff32009-06-22 23:27:02 +00002131
2132 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2133}
2134
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002135/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2136/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Evan Cheng06b53c02009-11-12 07:13:11 +00002137SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op, SelectionDAG &DAG) {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002138 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2139 EVT VT = Op.getValueType();
2140 unsigned VTBits = VT.getSizeInBits();
2141 DebugLoc dl = Op.getDebugLoc();
2142 SDValue ShOpLo = Op.getOperand(0);
2143 SDValue ShOpHi = Op.getOperand(1);
2144 SDValue ShAmt = Op.getOperand(2);
2145 SDValue ARMCC;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002146 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002147
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002148 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2149
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002150 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2151 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2152 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2153 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2154 DAG.getConstant(VTBits, MVT::i32));
2155 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2156 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002157 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002158
2159 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2160 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002161 ARMCC, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002162 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002163 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC,
2164 CCR, Cmp);
2165
2166 SDValue Ops[2] = { Lo, Hi };
2167 return DAG.getMergeValues(Ops, 2, dl);
2168}
2169
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002170/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2171/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Evan Cheng06b53c02009-11-12 07:13:11 +00002172SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op, SelectionDAG &DAG) {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002173 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2174 EVT VT = Op.getValueType();
2175 unsigned VTBits = VT.getSizeInBits();
2176 DebugLoc dl = Op.getDebugLoc();
2177 SDValue ShOpLo = Op.getOperand(0);
2178 SDValue ShOpHi = Op.getOperand(1);
2179 SDValue ShAmt = Op.getOperand(2);
2180 SDValue ARMCC;
2181
2182 assert(Op.getOpcode() == ISD::SHL_PARTS);
2183 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2184 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2185 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2186 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2187 DAG.getConstant(VTBits, MVT::i32));
2188 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2189 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2190
2191 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2192 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2193 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng06b53c02009-11-12 07:13:11 +00002194 ARMCC, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002195 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
2196 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMCC,
2197 CCR, Cmp);
2198
2199 SDValue Ops[2] = { Lo, Hi };
2200 return DAG.getMergeValues(Ops, 2, dl);
2201}
2202
Bob Wilson5bafff32009-06-22 23:27:02 +00002203static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2204 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002205 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002206 DebugLoc dl = N->getDebugLoc();
2207
2208 // Lower vector shifts on NEON to use VSHL.
2209 if (VT.isVector()) {
2210 assert(ST->hasNEON() && "unexpected vector shift");
2211
2212 // Left shifts translate directly to the vshiftu intrinsic.
2213 if (N->getOpcode() == ISD::SHL)
2214 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002215 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002216 N->getOperand(0), N->getOperand(1));
2217
2218 assert((N->getOpcode() == ISD::SRA ||
2219 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2220
2221 // NEON uses the same intrinsics for both left and right shifts. For
2222 // right shifts, the shift amounts are negative, so negate the vector of
2223 // shift amounts.
Owen Andersone50ed302009-08-10 22:56:29 +00002224 EVT ShiftVT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002225 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2226 getZeroVector(ShiftVT, DAG, dl),
2227 N->getOperand(1));
2228 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2229 Intrinsic::arm_neon_vshifts :
2230 Intrinsic::arm_neon_vshiftu);
2231 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002232 DAG.getConstant(vshiftInt, MVT::i32),
Bob Wilson5bafff32009-06-22 23:27:02 +00002233 N->getOperand(0), NegatedCount);
2234 }
2235
Eli Friedmance392eb2009-08-22 03:13:10 +00002236 // We can get here for a node like i32 = ISD::SHL i32, i64
2237 if (VT != MVT::i64)
2238 return SDValue();
2239
2240 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00002241 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00002242
Chris Lattner27a6c732007-11-24 07:07:01 +00002243 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2244 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002245 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00002246 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002247
Chris Lattner27a6c732007-11-24 07:07:01 +00002248 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00002249 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00002250
Chris Lattner27a6c732007-11-24 07:07:01 +00002251 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00002252 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2253 DAG.getConstant(0, MVT::i32));
2254 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2255 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002256
Chris Lattner27a6c732007-11-24 07:07:01 +00002257 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2258 // captures the result into a carry flag.
2259 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Owen Anderson825b72b2009-08-11 20:47:22 +00002260 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002261
Chris Lattner27a6c732007-11-24 07:07:01 +00002262 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00002263 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00002264
Chris Lattner27a6c732007-11-24 07:07:01 +00002265 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002266 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00002267}
2268
Bob Wilson5bafff32009-06-22 23:27:02 +00002269static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2270 SDValue TmpOp0, TmpOp1;
2271 bool Invert = false;
2272 bool Swap = false;
2273 unsigned Opc = 0;
2274
2275 SDValue Op0 = Op.getOperand(0);
2276 SDValue Op1 = Op.getOperand(1);
2277 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00002278 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002279 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2280 DebugLoc dl = Op.getDebugLoc();
2281
2282 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2283 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002284 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002285 case ISD::SETUNE:
2286 case ISD::SETNE: Invert = true; // Fallthrough
2287 case ISD::SETOEQ:
2288 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2289 case ISD::SETOLT:
2290 case ISD::SETLT: Swap = true; // Fallthrough
2291 case ISD::SETOGT:
2292 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2293 case ISD::SETOLE:
2294 case ISD::SETLE: Swap = true; // Fallthrough
2295 case ISD::SETOGE:
2296 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2297 case ISD::SETUGE: Swap = true; // Fallthrough
2298 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2299 case ISD::SETUGT: Swap = true; // Fallthrough
2300 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2301 case ISD::SETUEQ: Invert = true; // Fallthrough
2302 case ISD::SETONE:
2303 // Expand this to (OLT | OGT).
2304 TmpOp0 = Op0;
2305 TmpOp1 = Op1;
2306 Opc = ISD::OR;
2307 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2308 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2309 break;
2310 case ISD::SETUO: Invert = true; // Fallthrough
2311 case ISD::SETO:
2312 // Expand this to (OLT | OGE).
2313 TmpOp0 = Op0;
2314 TmpOp1 = Op1;
2315 Opc = ISD::OR;
2316 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2317 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2318 break;
2319 }
2320 } else {
2321 // Integer comparisons.
2322 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002323 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002324 case ISD::SETNE: Invert = true;
2325 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2326 case ISD::SETLT: Swap = true;
2327 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2328 case ISD::SETLE: Swap = true;
2329 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2330 case ISD::SETULT: Swap = true;
2331 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2332 case ISD::SETULE: Swap = true;
2333 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2334 }
2335
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002336 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00002337 if (Opc == ARMISD::VCEQ) {
2338
2339 SDValue AndOp;
2340 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2341 AndOp = Op0;
2342 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2343 AndOp = Op1;
2344
2345 // Ignore bitconvert.
2346 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2347 AndOp = AndOp.getOperand(0);
2348
2349 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2350 Opc = ARMISD::VTST;
2351 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2352 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2353 Invert = !Invert;
2354 }
2355 }
2356 }
2357
2358 if (Swap)
2359 std::swap(Op0, Op1);
2360
2361 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2362
2363 if (Invert)
2364 Result = DAG.getNOT(dl, Result, VT);
2365
2366 return Result;
2367}
2368
2369/// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2370/// VMOV instruction, and if so, return the constant being splatted.
2371static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2372 unsigned SplatBitSize, SelectionDAG &DAG) {
2373 switch (SplatBitSize) {
2374 case 8:
2375 // Any 1-byte value is OK.
2376 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Owen Anderson825b72b2009-08-11 20:47:22 +00002377 return DAG.getTargetConstant(SplatBits, MVT::i8);
Bob Wilson5bafff32009-06-22 23:27:02 +00002378
2379 case 16:
2380 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2381 if ((SplatBits & ~0xff) == 0 ||
2382 (SplatBits & ~0xff00) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002383 return DAG.getTargetConstant(SplatBits, MVT::i16);
Bob Wilson5bafff32009-06-22 23:27:02 +00002384 break;
2385
2386 case 32:
2387 // NEON's 32-bit VMOV supports splat values where:
2388 // * only one byte is nonzero, or
2389 // * the least significant byte is 0xff and the second byte is nonzero, or
2390 // * the least significant 2 bytes are 0xff and the third is nonzero.
2391 if ((SplatBits & ~0xff) == 0 ||
2392 (SplatBits & ~0xff00) == 0 ||
2393 (SplatBits & ~0xff0000) == 0 ||
2394 (SplatBits & ~0xff000000) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00002395 return DAG.getTargetConstant(SplatBits, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002396
2397 if ((SplatBits & ~0xffff) == 0 &&
2398 ((SplatBits | SplatUndef) & 0xff) == 0xff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002399 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002400
2401 if ((SplatBits & ~0xffffff) == 0 &&
2402 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
Owen Anderson825b72b2009-08-11 20:47:22 +00002403 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002404
2405 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2406 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2407 // VMOV.I32. A (very) minor optimization would be to replicate the value
2408 // and fall through here to test for a valid 64-bit splat. But, then the
2409 // caller would also need to check and handle the change in size.
2410 break;
2411
2412 case 64: {
2413 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2414 uint64_t BitMask = 0xff;
2415 uint64_t Val = 0;
2416 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2417 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2418 Val |= BitMask;
2419 else if ((SplatBits & BitMask) != 0)
2420 return SDValue();
2421 BitMask <<= 8;
2422 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002423 return DAG.getTargetConstant(Val, MVT::i64);
Bob Wilson5bafff32009-06-22 23:27:02 +00002424 }
2425
2426 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002427 llvm_unreachable("unexpected size for isVMOVSplat");
Bob Wilson5bafff32009-06-22 23:27:02 +00002428 break;
2429 }
2430
2431 return SDValue();
2432}
2433
2434/// getVMOVImm - If this is a build_vector of constants which can be
2435/// formed by using a VMOV instruction of the specified element size,
2436/// return the constant being splatted. The ByteSize field indicates the
2437/// number of bytes of each element [1248].
2438SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2439 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2440 APInt SplatBits, SplatUndef;
2441 unsigned SplatBitSize;
2442 bool HasAnyUndefs;
2443 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2444 HasAnyUndefs, ByteSize * 8))
2445 return SDValue();
2446
2447 if (SplatBitSize > ByteSize * 8)
2448 return SDValue();
2449
2450 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2451 SplatBitSize, DAG);
2452}
2453
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002454static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2455 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002456 unsigned NumElts = VT.getVectorNumElements();
2457 ReverseVEXT = false;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002458 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002459
2460 // If this is a VEXT shuffle, the immediate value is the index of the first
2461 // element. The other shuffle indices must be the successive elements after
2462 // the first one.
2463 unsigned ExpectedElt = Imm;
2464 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002465 // Increment the expected index. If it wraps around, it may still be
2466 // a VEXT but the source vectors must be swapped.
2467 ExpectedElt += 1;
2468 if (ExpectedElt == NumElts * 2) {
2469 ExpectedElt = 0;
2470 ReverseVEXT = true;
2471 }
2472
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002473 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002474 return false;
2475 }
2476
2477 // Adjust the index value if the source operands will be swapped.
2478 if (ReverseVEXT)
2479 Imm -= NumElts;
2480
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002481 return true;
2482}
2483
Bob Wilson8bb9e482009-07-26 00:39:34 +00002484/// isVREVMask - Check if a vector shuffle corresponds to a VREV
2485/// instruction with the specified blocksize. (The order of the elements
2486/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002487static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2488 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00002489 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2490 "Only possible block sizes for VREV are: 16, 32, 64");
2491
Bob Wilson8bb9e482009-07-26 00:39:34 +00002492 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00002493 if (EltSz == 64)
2494 return false;
2495
2496 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002497 unsigned BlockElts = M[0] + 1;
Bob Wilson8bb9e482009-07-26 00:39:34 +00002498
2499 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2500 return false;
2501
2502 for (unsigned i = 0; i < NumElts; ++i) {
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002503 if ((unsigned) M[i] !=
Bob Wilson8bb9e482009-07-26 00:39:34 +00002504 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2505 return false;
2506 }
2507
2508 return true;
2509}
2510
Bob Wilsonc692cb72009-08-21 20:54:19 +00002511static bool isVTRNMask(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 for (unsigned i = 0; i < NumElts; i += 2) {
2520 if ((unsigned) M[i] != i + WhichResult ||
2521 (unsigned) M[i+1] != i + NumElts + WhichResult)
2522 return false;
2523 }
2524 return true;
2525}
2526
2527static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2528 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002529 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2530 if (EltSz == 64)
2531 return false;
2532
Bob Wilsonc692cb72009-08-21 20:54:19 +00002533 unsigned NumElts = VT.getVectorNumElements();
2534 WhichResult = (M[0] == 0 ? 0 : 1);
2535 for (unsigned i = 0; i != NumElts; ++i) {
2536 if ((unsigned) M[i] != 2 * i + WhichResult)
2537 return false;
2538 }
2539
2540 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002541 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002542 return false;
2543
2544 return true;
2545}
2546
2547static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2548 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00002549 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2550 if (EltSz == 64)
2551 return false;
2552
Bob Wilsonc692cb72009-08-21 20:54:19 +00002553 unsigned NumElts = VT.getVectorNumElements();
2554 WhichResult = (M[0] == 0 ? 0 : 1);
2555 unsigned Idx = WhichResult * NumElts / 2;
2556 for (unsigned i = 0; i != NumElts; i += 2) {
2557 if ((unsigned) M[i] != Idx ||
2558 (unsigned) M[i+1] != Idx + NumElts)
2559 return false;
2560 Idx += 1;
2561 }
2562
2563 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00002564 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002565 return false;
2566
2567 return true;
2568}
2569
Owen Andersone50ed302009-08-10 22:56:29 +00002570static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002571 // Canonicalize all-zeros and all-ones vectors.
Bob Wilsond06791f2009-08-13 01:57:47 +00002572 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002573 if (ConstVal->isNullValue())
2574 return getZeroVector(VT, DAG, dl);
2575 if (ConstVal->isAllOnesValue())
2576 return getOnesVector(VT, DAG, dl);
2577
Owen Andersone50ed302009-08-10 22:56:29 +00002578 EVT CanonicalVT;
Bob Wilson5bafff32009-06-22 23:27:02 +00002579 if (VT.is64BitVector()) {
2580 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002581 case 8: CanonicalVT = MVT::v8i8; break;
2582 case 16: CanonicalVT = MVT::v4i16; break;
2583 case 32: CanonicalVT = MVT::v2i32; break;
2584 case 64: CanonicalVT = MVT::v1i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002585 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002586 }
2587 } else {
2588 assert(VT.is128BitVector() && "unknown splat vector size");
2589 switch (Val.getValueType().getSizeInBits()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002590 case 8: CanonicalVT = MVT::v16i8; break;
2591 case 16: CanonicalVT = MVT::v8i16; break;
2592 case 32: CanonicalVT = MVT::v4i32; break;
2593 case 64: CanonicalVT = MVT::v2i64; break;
Torok Edwinc23197a2009-07-14 16:55:14 +00002594 default: llvm_unreachable("unexpected splat element type"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00002595 }
2596 }
2597
2598 // Build a canonical splat for this value.
2599 SmallVector<SDValue, 8> Ops;
2600 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2601 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2602 Ops.size());
2603 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2604}
2605
2606// If this is a case we can't handle, return null and let the default
2607// expansion code take care of it.
2608static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bob Wilsond06791f2009-08-13 01:57:47 +00002609 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00002610 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002611 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002612
2613 APInt SplatBits, SplatUndef;
2614 unsigned SplatBitSize;
2615 bool HasAnyUndefs;
2616 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00002617 if (SplatBitSize <= 64) {
2618 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2619 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2620 if (Val.getNode())
2621 return BuildSplat(Val, VT, DAG, dl);
2622 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00002623 }
2624
2625 // If there are only 2 elements in a 128-bit vector, insert them into an
2626 // undef vector. This handles the common case for 128-bit vector argument
2627 // passing, where the insertions should be translated to subreg accesses
2628 // with no real instructions.
2629 if (VT.is128BitVector() && Op.getNumOperands() == 2) {
2630 SDValue Val = DAG.getUNDEF(VT);
2631 SDValue Op0 = Op.getOperand(0);
2632 SDValue Op1 = Op.getOperand(1);
2633 if (Op0.getOpcode() != ISD::UNDEF)
2634 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op0,
2635 DAG.getIntPtrConstant(0));
2636 if (Op1.getOpcode() != ISD::UNDEF)
2637 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op1,
2638 DAG.getIntPtrConstant(1));
2639 return Val;
Bob Wilson5bafff32009-06-22 23:27:02 +00002640 }
2641
2642 return SDValue();
2643}
2644
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002645/// isShuffleMaskLegal - Targets can use this to indicate that they only
2646/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2647/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2648/// are assumed to be legal.
2649bool
2650ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2651 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002652 if (VT.getVectorNumElements() == 4 &&
2653 (VT.is128BitVector() || VT.is64BitVector())) {
2654 unsigned PFIndexes[4];
2655 for (unsigned i = 0; i != 4; ++i) {
2656 if (M[i] < 0)
2657 PFIndexes[i] = 8;
2658 else
2659 PFIndexes[i] = M[i];
2660 }
2661
2662 // Compute the index in the perfect shuffle table.
2663 unsigned PFTableIndex =
2664 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2665 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2666 unsigned Cost = (PFEntry >> 30);
2667
2668 if (Cost <= 4)
2669 return true;
2670 }
2671
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002672 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00002673 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002674
2675 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2676 isVREVMask(M, VT, 64) ||
2677 isVREVMask(M, VT, 32) ||
2678 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00002679 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2680 isVTRNMask(M, VT, WhichResult) ||
2681 isVUZPMask(M, VT, WhichResult) ||
2682 isVZIPMask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002683}
2684
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002685/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2686/// the specified operations to build the shuffle.
2687static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2688 SDValue RHS, SelectionDAG &DAG,
2689 DebugLoc dl) {
2690 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2691 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2692 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2693
2694 enum {
2695 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2696 OP_VREV,
2697 OP_VDUP0,
2698 OP_VDUP1,
2699 OP_VDUP2,
2700 OP_VDUP3,
2701 OP_VEXT1,
2702 OP_VEXT2,
2703 OP_VEXT3,
2704 OP_VUZPL, // VUZP, left result
2705 OP_VUZPR, // VUZP, right result
2706 OP_VZIPL, // VZIP, left result
2707 OP_VZIPR, // VZIP, right result
2708 OP_VTRNL, // VTRN, left result
2709 OP_VTRNR // VTRN, right result
2710 };
2711
2712 if (OpNum == OP_COPY) {
2713 if (LHSID == (1*9+2)*9+3) return LHS;
2714 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2715 return RHS;
2716 }
2717
2718 SDValue OpLHS, OpRHS;
2719 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2720 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2721 EVT VT = OpLHS.getValueType();
2722
2723 switch (OpNum) {
2724 default: llvm_unreachable("Unknown shuffle opcode!");
2725 case OP_VREV:
2726 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2727 case OP_VDUP0:
2728 case OP_VDUP1:
2729 case OP_VDUP2:
2730 case OP_VDUP3:
2731 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002732 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002733 case OP_VEXT1:
2734 case OP_VEXT2:
2735 case OP_VEXT3:
2736 return DAG.getNode(ARMISD::VEXT, dl, VT,
2737 OpLHS, OpRHS,
2738 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2739 case OP_VUZPL:
2740 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002741 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002742 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
2743 case OP_VZIPL:
2744 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002745 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002746 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
2747 case OP_VTRNL:
2748 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00002749 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2750 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002751 }
2752}
2753
Bob Wilson5bafff32009-06-22 23:27:02 +00002754static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002755 SDValue V1 = Op.getOperand(0);
2756 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00002757 DebugLoc dl = Op.getDebugLoc();
2758 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002759 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002760 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00002761
Bob Wilson28865062009-08-13 02:13:04 +00002762 // Convert shuffles that are directly supported on NEON to target-specific
2763 // DAG nodes, instead of keeping them as shuffles and matching them again
2764 // during code selection. This is more efficient and avoids the possibility
2765 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00002766 // FIXME: floating-point vectors should be canonicalized to integer vectors
2767 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002768 SVN->getMask(ShuffleMask);
2769
2770 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
Bob Wilson0ce37102009-08-14 05:08:32 +00002771 int Lane = SVN->getSplatIndex();
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00002772 // If this is undef splat, generate it via "just" vdup, if possible.
2773 if (Lane == -1) Lane = 0;
2774
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002775 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
2776 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00002777 }
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002778 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002779 DAG.getConstant(Lane, MVT::i32));
Bob Wilson0ce37102009-08-14 05:08:32 +00002780 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002781
2782 bool ReverseVEXT;
2783 unsigned Imm;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002784 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002785 if (ReverseVEXT)
Bob Wilsonc692cb72009-08-21 20:54:19 +00002786 std::swap(V1, V2);
2787 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Bob Wilsonde95c1b82009-08-19 17:03:43 +00002788 DAG.getConstant(Imm, MVT::i32));
2789 }
2790
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002791 if (isVREVMask(ShuffleMask, VT, 64))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002792 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002793 if (isVREVMask(ShuffleMask, VT, 32))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002794 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00002795 if (isVREVMask(ShuffleMask, VT, 16))
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002796 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
2797
Bob Wilsonc692cb72009-08-21 20:54:19 +00002798 // Check for Neon shuffles that modify both input vectors in place.
2799 // If both results are used, i.e., if there are two shuffles with the same
2800 // source operands and with masks corresponding to both results of one of
2801 // these operations, DAG memoization will ensure that a single node is
2802 // used for both shuffles.
2803 unsigned WhichResult;
2804 if (isVTRNMask(ShuffleMask, VT, WhichResult))
2805 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2806 V1, V2).getValue(WhichResult);
2807 if (isVUZPMask(ShuffleMask, VT, WhichResult))
2808 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2809 V1, V2).getValue(WhichResult);
2810 if (isVZIPMask(ShuffleMask, VT, WhichResult))
2811 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2812 V1, V2).getValue(WhichResult);
2813
2814 // If the shuffle is not directly supported and it has 4 elements, use
2815 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00002816 if (VT.getVectorNumElements() == 4 &&
2817 (VT.is128BitVector() || VT.is64BitVector())) {
2818 unsigned PFIndexes[4];
2819 for (unsigned i = 0; i != 4; ++i) {
2820 if (ShuffleMask[i] < 0)
2821 PFIndexes[i] = 8;
2822 else
2823 PFIndexes[i] = ShuffleMask[i];
2824 }
2825
2826 // Compute the index in the perfect shuffle table.
2827 unsigned PFTableIndex =
2828 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2829
2830 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2831 unsigned Cost = (PFEntry >> 30);
2832
2833 if (Cost <= 4)
2834 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
2835 }
Bob Wilsond8e17572009-08-12 22:31:50 +00002836
Bob Wilson22cac0d2009-08-14 05:16:33 +00002837 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00002838}
2839
Bob Wilson5bafff32009-06-22 23:27:02 +00002840static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002841 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00002842 DebugLoc dl = Op.getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00002843 SDValue Vec = Op.getOperand(0);
2844 SDValue Lane = Op.getOperand(1);
Bob Wilson934f98b2009-10-15 23:12:05 +00002845 assert(VT == MVT::i32 &&
2846 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
2847 "unexpected type for custom-lowering vector extract");
2848 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
Bob Wilson5bafff32009-06-22 23:27:02 +00002849}
2850
Bob Wilsona6d65862009-08-03 20:36:38 +00002851static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
2852 // The only time a CONCAT_VECTORS operation can have legal types is when
2853 // two 64-bit vectors are concatenated to a 128-bit vector.
2854 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
2855 "unexpected CONCAT_VECTORS");
2856 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00002857 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00002858 SDValue Op0 = Op.getOperand(0);
2859 SDValue Op1 = Op.getOperand(1);
2860 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002861 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2862 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00002863 DAG.getIntPtrConstant(0));
2864 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00002865 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2866 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00002867 DAG.getIntPtrConstant(1));
2868 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00002869}
2870
Dan Gohman475871a2008-07-27 21:46:04 +00002871SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00002872 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002873 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00002874 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002875 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002876 case ISD::GlobalAddress:
2877 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
2878 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002879 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00002880 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
2881 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002882 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Evan Cheng86198642009-08-07 00:34:42 +00002883 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002884 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
2885 case ISD::SINT_TO_FP:
2886 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
2887 case ISD::FP_TO_SINT:
2888 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
2889 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00002890 case ISD::RETURNADDR: break;
Jim Grosbach0e0da732009-05-12 23:59:14 +00002891 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002892 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002893 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00002894 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002895 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00002896 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00002897 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00002898 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002899 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00002900 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002901 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
2902 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2903 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00002904 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00002905 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00002906 }
Dan Gohman475871a2008-07-27 21:46:04 +00002907 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00002908}
2909
Duncan Sands1607f052008-12-01 11:39:25 +00002910/// ReplaceNodeResults - Replace the results of node with an illegal result
2911/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00002912void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
2913 SmallVectorImpl<SDValue>&Results,
2914 SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00002915 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00002916 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002917 llvm_unreachable("Don't know how to custom expand this!");
Duncan Sands1607f052008-12-01 11:39:25 +00002918 return;
2919 case ISD::BIT_CONVERT:
2920 Results.push_back(ExpandBIT_CONVERT(N, DAG));
2921 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00002922 case ISD::SRL:
Duncan Sands1607f052008-12-01 11:39:25 +00002923 case ISD::SRA: {
Bob Wilson5bafff32009-06-22 23:27:02 +00002924 SDValue Res = LowerShift(N, DAG, Subtarget);
Duncan Sands1607f052008-12-01 11:39:25 +00002925 if (Res.getNode())
2926 Results.push_back(Res);
2927 return;
2928 }
Chris Lattner27a6c732007-11-24 07:07:01 +00002929 }
2930}
Chris Lattner27a6c732007-11-24 07:07:01 +00002931
Evan Chenga8e29892007-01-19 07:51:42 +00002932//===----------------------------------------------------------------------===//
2933// ARM Scheduler Hooks
2934//===----------------------------------------------------------------------===//
2935
2936MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00002937ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00002938 MachineBasicBlock *BB,
2939 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002940 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00002941 DebugLoc dl = MI->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002942 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00002943 default:
2944 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng007ea272009-08-12 05:17:19 +00002945 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00002946 // To "insert" a SELECT_CC instruction, we actually have to insert the
2947 // diamond control-flow pattern. The incoming instruction knows the
2948 // destination vreg to set, the condition code register to branch on, the
2949 // true/false values to select between, and a branch opcode to use.
2950 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002951 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00002952 ++It;
2953
2954 // thisMBB:
2955 // ...
2956 // TrueVal = ...
2957 // cmpTY ccX, r1, r2
2958 // bCC copy1MBB
2959 // fallthrough --> copy0MBB
2960 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002961 MachineFunction *F = BB->getParent();
2962 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
2963 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00002964 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00002965 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002966 F->insert(It, copy0MBB);
2967 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00002968 // Update machine-CFG edges by first adding all successors of the current
2969 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00002970 // Also inform sdisel of the edge changes.
2971 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
2972 E = BB->succ_end(); I != E; ++I) {
2973 EM->insert(std::make_pair(*I, sinkMBB));
2974 sinkMBB->addSuccessor(*I);
2975 }
Evan Chenga8e29892007-01-19 07:51:42 +00002976 // Next, remove all successors of the current block, and add the true
2977 // and fallthrough blocks as its successors.
Evan Chengce319102009-09-19 09:51:03 +00002978 while (!BB->succ_empty())
Evan Chenga8e29892007-01-19 07:51:42 +00002979 BB->removeSuccessor(BB->succ_begin());
2980 BB->addSuccessor(copy0MBB);
2981 BB->addSuccessor(sinkMBB);
2982
2983 // copy0MBB:
2984 // %FalseValue = ...
2985 // # fallthrough to sinkMBB
2986 BB = copy0MBB;
2987
2988 // Update machine-CFG edges
2989 BB->addSuccessor(sinkMBB);
2990
2991 // sinkMBB:
2992 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2993 // ...
2994 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00002995 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00002996 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
2997 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
2998
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002999 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00003000 return BB;
3001 }
Evan Cheng86198642009-08-07 00:34:42 +00003002
3003 case ARM::tANDsp:
3004 case ARM::tADDspr_:
3005 case ARM::tSUBspi_:
3006 case ARM::t2SUBrSPi_:
3007 case ARM::t2SUBrSPi12_:
3008 case ARM::t2SUBrSPs_: {
3009 MachineFunction *MF = BB->getParent();
3010 unsigned DstReg = MI->getOperand(0).getReg();
3011 unsigned SrcReg = MI->getOperand(1).getReg();
3012 bool DstIsDead = MI->getOperand(0).isDead();
3013 bool SrcIsKill = MI->getOperand(1).isKill();
3014
3015 if (SrcReg != ARM::SP) {
3016 // Copy the source to SP from virtual register.
3017 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
3018 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3019 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
3020 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
3021 .addReg(SrcReg, getKillRegState(SrcIsKill));
3022 }
3023
3024 unsigned OpOpc = 0;
3025 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
3026 switch (MI->getOpcode()) {
3027 default:
3028 llvm_unreachable("Unexpected pseudo instruction!");
3029 case ARM::tANDsp:
3030 OpOpc = ARM::tAND;
3031 NeedPred = true;
3032 break;
3033 case ARM::tADDspr_:
3034 OpOpc = ARM::tADDspr;
3035 break;
3036 case ARM::tSUBspi_:
3037 OpOpc = ARM::tSUBspi;
3038 break;
3039 case ARM::t2SUBrSPi_:
3040 OpOpc = ARM::t2SUBrSPi;
3041 NeedPred = true; NeedCC = true;
3042 break;
3043 case ARM::t2SUBrSPi12_:
3044 OpOpc = ARM::t2SUBrSPi12;
3045 NeedPred = true;
3046 break;
3047 case ARM::t2SUBrSPs_:
3048 OpOpc = ARM::t2SUBrSPs;
3049 NeedPred = true; NeedCC = true; NeedOp3 = true;
3050 break;
3051 }
3052 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
3053 if (OpOpc == ARM::tAND)
3054 AddDefaultT1CC(MIB);
3055 MIB.addReg(ARM::SP);
3056 MIB.addOperand(MI->getOperand(2));
3057 if (NeedOp3)
3058 MIB.addOperand(MI->getOperand(3));
3059 if (NeedPred)
3060 AddDefaultPred(MIB);
3061 if (NeedCC)
3062 AddDefaultCC(MIB);
3063
3064 // Copy the result from SP to virtual register.
3065 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
3066 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3067 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
3068 BuildMI(BB, dl, TII->get(CopyOpc))
3069 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
3070 .addReg(ARM::SP);
3071 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3072 return BB;
3073 }
Evan Chenga8e29892007-01-19 07:51:42 +00003074 }
3075}
3076
3077//===----------------------------------------------------------------------===//
3078// ARM Optimization Hooks
3079//===----------------------------------------------------------------------===//
3080
Chris Lattnerd1980a52009-03-12 06:52:53 +00003081static
3082SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3083 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00003084 SelectionDAG &DAG = DCI.DAG;
3085 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003086 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00003087 unsigned Opc = N->getOpcode();
3088 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3089 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3090 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3091 ISD::CondCode CC = ISD::SETCC_INVALID;
3092
3093 if (isSlctCC) {
3094 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
3095 } else {
3096 SDValue CCOp = Slct.getOperand(0);
3097 if (CCOp.getOpcode() == ISD::SETCC)
3098 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
3099 }
3100
3101 bool DoXform = false;
3102 bool InvCC = false;
3103 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
3104 "Bad input!");
3105
3106 if (LHS.getOpcode() == ISD::Constant &&
3107 cast<ConstantSDNode>(LHS)->isNullValue()) {
3108 DoXform = true;
3109 } else if (CC != ISD::SETCC_INVALID &&
3110 RHS.getOpcode() == ISD::Constant &&
3111 cast<ConstantSDNode>(RHS)->isNullValue()) {
3112 std::swap(LHS, RHS);
3113 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003114 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00003115 Op0.getOperand(0).getValueType();
3116 bool isInt = OpVT.isInteger();
3117 CC = ISD::getSetCCInverse(CC, isInt);
3118
3119 if (!TLI.isCondCodeLegal(CC, OpVT))
3120 return SDValue(); // Inverse operator isn't legal.
3121
3122 DoXform = true;
3123 InvCC = true;
3124 }
3125
3126 if (DoXform) {
3127 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3128 if (isSlctCC)
3129 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3130 Slct.getOperand(0), Slct.getOperand(1), CC);
3131 SDValue CCOp = Slct.getOperand(0);
3132 if (InvCC)
3133 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3134 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3135 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3136 CCOp, OtherOp, Result);
3137 }
3138 return SDValue();
3139}
3140
3141/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3142static SDValue PerformADDCombine(SDNode *N,
3143 TargetLowering::DAGCombinerInfo &DCI) {
3144 // added by evan in r37685 with no testcase.
3145 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003146
Chris Lattnerd1980a52009-03-12 06:52:53 +00003147 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3148 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3149 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3150 if (Result.getNode()) return Result;
3151 }
3152 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3153 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3154 if (Result.getNode()) return Result;
3155 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003156
Chris Lattnerd1980a52009-03-12 06:52:53 +00003157 return SDValue();
3158}
3159
3160/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3161static SDValue PerformSUBCombine(SDNode *N,
3162 TargetLowering::DAGCombinerInfo &DCI) {
3163 // added by evan in r37685 with no testcase.
3164 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003165
Chris Lattnerd1980a52009-03-12 06:52:53 +00003166 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3167 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3168 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3169 if (Result.getNode()) return Result;
3170 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003171
Chris Lattnerd1980a52009-03-12 06:52:53 +00003172 return SDValue();
3173}
3174
Jim Grosbache5165492009-11-09 00:11:35 +00003175/// PerformVMOVRRDCombine - Target-specific dag combine xforms for ARMISD::VMOVRRD.
3176static SDValue PerformVMOVRRDCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003177 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003178 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00003179 SDValue InDouble = N->getOperand(0);
Jim Grosbache5165492009-11-09 00:11:35 +00003180 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003181 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003182 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003183}
3184
Bob Wilson5bafff32009-06-22 23:27:02 +00003185/// getVShiftImm - Check if this is a valid build_vector for the immediate
3186/// operand of a vector shift operation, where all the elements of the
3187/// build_vector must have the same constant integer value.
3188static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3189 // Ignore bit_converts.
3190 while (Op.getOpcode() == ISD::BIT_CONVERT)
3191 Op = Op.getOperand(0);
3192 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3193 APInt SplatBits, SplatUndef;
3194 unsigned SplatBitSize;
3195 bool HasAnyUndefs;
3196 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3197 HasAnyUndefs, ElementBits) ||
3198 SplatBitSize > ElementBits)
3199 return false;
3200 Cnt = SplatBits.getSExtValue();
3201 return true;
3202}
3203
3204/// isVShiftLImm - Check if this is a valid build_vector for the immediate
3205/// operand of a vector shift left operation. That value must be in the range:
3206/// 0 <= Value < ElementBits for a left shift; or
3207/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003208static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003209 assert(VT.isVector() && "vector shift count is not a vector type");
3210 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3211 if (! getVShiftImm(Op, ElementBits, Cnt))
3212 return false;
3213 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3214}
3215
3216/// isVShiftRImm - Check if this is a valid build_vector for the immediate
3217/// operand of a vector shift right operation. For a shift opcode, the value
3218/// is positive, but for an intrinsic the value count must be negative. The
3219/// absolute value must be in the range:
3220/// 1 <= |Value| <= ElementBits for a right shift; or
3221/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00003222static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00003223 int64_t &Cnt) {
3224 assert(VT.isVector() && "vector shift count is not a vector type");
3225 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3226 if (! getVShiftImm(Op, ElementBits, Cnt))
3227 return false;
3228 if (isIntrinsic)
3229 Cnt = -Cnt;
3230 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3231}
3232
3233/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3234static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3235 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3236 switch (IntNo) {
3237 default:
3238 // Don't do anything for most intrinsics.
3239 break;
3240
3241 // Vector shifts: check for immediate versions and lower them.
3242 // Note: This is done during DAG combining instead of DAG legalizing because
3243 // the build_vectors for 64-bit vector element shift counts are generally
3244 // not legal, and it is hard to see their values after they get legalized to
3245 // loads from a constant pool.
3246 case Intrinsic::arm_neon_vshifts:
3247 case Intrinsic::arm_neon_vshiftu:
3248 case Intrinsic::arm_neon_vshiftls:
3249 case Intrinsic::arm_neon_vshiftlu:
3250 case Intrinsic::arm_neon_vshiftn:
3251 case Intrinsic::arm_neon_vrshifts:
3252 case Intrinsic::arm_neon_vrshiftu:
3253 case Intrinsic::arm_neon_vrshiftn:
3254 case Intrinsic::arm_neon_vqshifts:
3255 case Intrinsic::arm_neon_vqshiftu:
3256 case Intrinsic::arm_neon_vqshiftsu:
3257 case Intrinsic::arm_neon_vqshiftns:
3258 case Intrinsic::arm_neon_vqshiftnu:
3259 case Intrinsic::arm_neon_vqshiftnsu:
3260 case Intrinsic::arm_neon_vqrshiftns:
3261 case Intrinsic::arm_neon_vqrshiftnu:
3262 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00003263 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003264 int64_t Cnt;
3265 unsigned VShiftOpc = 0;
3266
3267 switch (IntNo) {
3268 case Intrinsic::arm_neon_vshifts:
3269 case Intrinsic::arm_neon_vshiftu:
3270 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3271 VShiftOpc = ARMISD::VSHL;
3272 break;
3273 }
3274 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3275 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3276 ARMISD::VSHRs : ARMISD::VSHRu);
3277 break;
3278 }
3279 return SDValue();
3280
3281 case Intrinsic::arm_neon_vshiftls:
3282 case Intrinsic::arm_neon_vshiftlu:
3283 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3284 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003285 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003286
3287 case Intrinsic::arm_neon_vrshifts:
3288 case Intrinsic::arm_neon_vrshiftu:
3289 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3290 break;
3291 return SDValue();
3292
3293 case Intrinsic::arm_neon_vqshifts:
3294 case Intrinsic::arm_neon_vqshiftu:
3295 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3296 break;
3297 return SDValue();
3298
3299 case Intrinsic::arm_neon_vqshiftsu:
3300 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3301 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003302 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003303
3304 case Intrinsic::arm_neon_vshiftn:
3305 case Intrinsic::arm_neon_vrshiftn:
3306 case Intrinsic::arm_neon_vqshiftns:
3307 case Intrinsic::arm_neon_vqshiftnu:
3308 case Intrinsic::arm_neon_vqshiftnsu:
3309 case Intrinsic::arm_neon_vqrshiftns:
3310 case Intrinsic::arm_neon_vqrshiftnu:
3311 case Intrinsic::arm_neon_vqrshiftnsu:
3312 // Narrowing shifts require an immediate right shift.
3313 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3314 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00003315 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003316
3317 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003318 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003319 }
3320
3321 switch (IntNo) {
3322 case Intrinsic::arm_neon_vshifts:
3323 case Intrinsic::arm_neon_vshiftu:
3324 // Opcode already set above.
3325 break;
3326 case Intrinsic::arm_neon_vshiftls:
3327 case Intrinsic::arm_neon_vshiftlu:
3328 if (Cnt == VT.getVectorElementType().getSizeInBits())
3329 VShiftOpc = ARMISD::VSHLLi;
3330 else
3331 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3332 ARMISD::VSHLLs : ARMISD::VSHLLu);
3333 break;
3334 case Intrinsic::arm_neon_vshiftn:
3335 VShiftOpc = ARMISD::VSHRN; break;
3336 case Intrinsic::arm_neon_vrshifts:
3337 VShiftOpc = ARMISD::VRSHRs; break;
3338 case Intrinsic::arm_neon_vrshiftu:
3339 VShiftOpc = ARMISD::VRSHRu; break;
3340 case Intrinsic::arm_neon_vrshiftn:
3341 VShiftOpc = ARMISD::VRSHRN; break;
3342 case Intrinsic::arm_neon_vqshifts:
3343 VShiftOpc = ARMISD::VQSHLs; break;
3344 case Intrinsic::arm_neon_vqshiftu:
3345 VShiftOpc = ARMISD::VQSHLu; break;
3346 case Intrinsic::arm_neon_vqshiftsu:
3347 VShiftOpc = ARMISD::VQSHLsu; break;
3348 case Intrinsic::arm_neon_vqshiftns:
3349 VShiftOpc = ARMISD::VQSHRNs; break;
3350 case Intrinsic::arm_neon_vqshiftnu:
3351 VShiftOpc = ARMISD::VQSHRNu; break;
3352 case Intrinsic::arm_neon_vqshiftnsu:
3353 VShiftOpc = ARMISD::VQSHRNsu; break;
3354 case Intrinsic::arm_neon_vqrshiftns:
3355 VShiftOpc = ARMISD::VQRSHRNs; break;
3356 case Intrinsic::arm_neon_vqrshiftnu:
3357 VShiftOpc = ARMISD::VQRSHRNu; break;
3358 case Intrinsic::arm_neon_vqrshiftnsu:
3359 VShiftOpc = ARMISD::VQRSHRNsu; break;
3360 }
3361
3362 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003363 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003364 }
3365
3366 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00003367 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003368 int64_t Cnt;
3369 unsigned VShiftOpc = 0;
3370
3371 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3372 VShiftOpc = ARMISD::VSLI;
3373 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3374 VShiftOpc = ARMISD::VSRI;
3375 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003376 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00003377 }
3378
3379 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3380 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00003381 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003382 }
3383
3384 case Intrinsic::arm_neon_vqrshifts:
3385 case Intrinsic::arm_neon_vqrshiftu:
3386 // No immediate versions of these to check for.
3387 break;
3388 }
3389
3390 return SDValue();
3391}
3392
3393/// PerformShiftCombine - Checks for immediate versions of vector shifts and
3394/// lowers them. As with the vector shift intrinsics, this is done during DAG
3395/// combining instead of DAG legalizing because the build_vectors for 64-bit
3396/// vector element shift counts are generally not legal, and it is hard to see
3397/// their values after they get legalized to loads from a constant pool.
3398static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3399 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003400 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003401
3402 // Nothing to be done for scalar shifts.
3403 if (! VT.isVector())
3404 return SDValue();
3405
3406 assert(ST->hasNEON() && "unexpected vector shift");
3407 int64_t Cnt;
3408
3409 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003410 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003411
3412 case ISD::SHL:
3413 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3414 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003415 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003416 break;
3417
3418 case ISD::SRA:
3419 case ISD::SRL:
3420 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3421 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3422 ARMISD::VSHRs : ARMISD::VSHRu);
3423 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00003424 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00003425 }
3426 }
3427 return SDValue();
3428}
3429
3430/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3431/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3432static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3433 const ARMSubtarget *ST) {
3434 SDValue N0 = N->getOperand(0);
3435
3436 // Check for sign- and zero-extensions of vector extract operations of 8-
3437 // and 16-bit vector elements. NEON supports these directly. They are
3438 // handled during DAG combining because type legalization will promote them
3439 // to 32-bit types and it is messy to recognize the operations after that.
3440 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3441 SDValue Vec = N0.getOperand(0);
3442 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003443 EVT VT = N->getValueType(0);
3444 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003445 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3446
Owen Anderson825b72b2009-08-11 20:47:22 +00003447 if (VT == MVT::i32 &&
3448 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson5bafff32009-06-22 23:27:02 +00003449 TLI.isTypeLegal(Vec.getValueType())) {
3450
3451 unsigned Opc = 0;
3452 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003453 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00003454 case ISD::SIGN_EXTEND:
3455 Opc = ARMISD::VGETLANEs;
3456 break;
3457 case ISD::ZERO_EXTEND:
3458 case ISD::ANY_EXTEND:
3459 Opc = ARMISD::VGETLANEu;
3460 break;
3461 }
3462 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
3463 }
3464 }
3465
3466 return SDValue();
3467}
3468
Dan Gohman475871a2008-07-27 21:46:04 +00003469SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003470 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003471 switch (N->getOpcode()) {
3472 default: break;
Chris Lattnerd1980a52009-03-12 06:52:53 +00003473 case ISD::ADD: return PerformADDCombine(N, DCI);
3474 case ISD::SUB: return PerformSUBCombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00003475 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson5bafff32009-06-22 23:27:02 +00003476 case ISD::INTRINSIC_WO_CHAIN:
3477 return PerformIntrinsicCombine(N, DCI.DAG);
3478 case ISD::SHL:
3479 case ISD::SRA:
3480 case ISD::SRL:
3481 return PerformShiftCombine(N, DCI.DAG, Subtarget);
3482 case ISD::SIGN_EXTEND:
3483 case ISD::ZERO_EXTEND:
3484 case ISD::ANY_EXTEND:
3485 return PerformExtendCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003486 }
Dan Gohman475871a2008-07-27 21:46:04 +00003487 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00003488}
3489
Bill Wendlingaf566342009-08-15 21:21:19 +00003490bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
3491 if (!Subtarget->hasV6Ops())
3492 // Pre-v6 does not support unaligned mem access.
3493 return false;
3494 else if (!Subtarget->hasV6Ops()) {
3495 // v6 may or may not support unaligned mem access.
3496 if (!Subtarget->isTargetDarwin())
3497 return false;
3498 }
3499
3500 switch (VT.getSimpleVT().SimpleTy) {
3501 default:
3502 return false;
3503 case MVT::i8:
3504 case MVT::i16:
3505 case MVT::i32:
3506 return true;
3507 // FIXME: VLD1 etc with standard alignment is legal.
3508 }
3509}
3510
Evan Chenge6c835f2009-08-14 20:09:37 +00003511static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
3512 if (V < 0)
3513 return false;
3514
3515 unsigned Scale = 1;
3516 switch (VT.getSimpleVT().SimpleTy) {
3517 default: return false;
3518 case MVT::i1:
3519 case MVT::i8:
3520 // Scale == 1;
3521 break;
3522 case MVT::i16:
3523 // Scale == 2;
3524 Scale = 2;
3525 break;
3526 case MVT::i32:
3527 // Scale == 4;
3528 Scale = 4;
3529 break;
3530 }
3531
3532 if ((V & (Scale - 1)) != 0)
3533 return false;
3534 V /= Scale;
3535 return V == (V & ((1LL << 5) - 1));
3536}
3537
3538static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
3539 const ARMSubtarget *Subtarget) {
3540 bool isNeg = false;
3541 if (V < 0) {
3542 isNeg = true;
3543 V = - V;
3544 }
3545
3546 switch (VT.getSimpleVT().SimpleTy) {
3547 default: return false;
3548 case MVT::i1:
3549 case MVT::i8:
3550 case MVT::i16:
3551 case MVT::i32:
3552 // + imm12 or - imm8
3553 if (isNeg)
3554 return V == (V & ((1LL << 8) - 1));
3555 return V == (V & ((1LL << 12) - 1));
3556 case MVT::f32:
3557 case MVT::f64:
3558 // Same as ARM mode. FIXME: NEON?
3559 if (!Subtarget->hasVFP2())
3560 return false;
3561 if ((V & 3) != 0)
3562 return false;
3563 V >>= 2;
3564 return V == (V & ((1LL << 8) - 1));
3565 }
3566}
3567
Evan Chengb01fad62007-03-12 23:30:29 +00003568/// isLegalAddressImmediate - Return true if the integer value can be used
3569/// as the offset of the target addressing mode for load / store of the
3570/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00003571static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003572 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00003573 if (V == 0)
3574 return true;
3575
Evan Cheng65011532009-03-09 19:15:00 +00003576 if (!VT.isSimple())
3577 return false;
3578
Evan Chenge6c835f2009-08-14 20:09:37 +00003579 if (Subtarget->isThumb1Only())
3580 return isLegalT1AddressImmediate(V, VT);
3581 else if (Subtarget->isThumb2())
3582 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00003583
Evan Chenge6c835f2009-08-14 20:09:37 +00003584 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00003585 if (V < 0)
3586 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00003587 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00003588 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003589 case MVT::i1:
3590 case MVT::i8:
3591 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00003592 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003593 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003594 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00003595 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003596 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003597 case MVT::f32:
3598 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00003599 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00003600 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00003601 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00003602 return false;
3603 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003604 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00003605 }
Evan Chenga8e29892007-01-19 07:51:42 +00003606}
3607
Evan Chenge6c835f2009-08-14 20:09:37 +00003608bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
3609 EVT VT) const {
3610 int Scale = AM.Scale;
3611 if (Scale < 0)
3612 return false;
3613
3614 switch (VT.getSimpleVT().SimpleTy) {
3615 default: return false;
3616 case MVT::i1:
3617 case MVT::i8:
3618 case MVT::i16:
3619 case MVT::i32:
3620 if (Scale == 1)
3621 return true;
3622 // r + r << imm
3623 Scale = Scale & ~1;
3624 return Scale == 2 || Scale == 4 || Scale == 8;
3625 case MVT::i64:
3626 // r + r
3627 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
3628 return true;
3629 return false;
3630 case MVT::isVoid:
3631 // Note, we allow "void" uses (basically, uses that aren't loads or
3632 // stores), because arm allows folding a scale into many arithmetic
3633 // operations. This should be made more precise and revisited later.
3634
3635 // Allow r << imm, but the imm has to be a multiple of two.
3636 if (Scale & 1) return false;
3637 return isPowerOf2_32(Scale);
3638 }
3639}
3640
Chris Lattner37caf8c2007-04-09 23:33:39 +00003641/// isLegalAddressingMode - Return true if the addressing mode represented
3642/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003643bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00003644 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00003645 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00003646 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00003647 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003648
Chris Lattner37caf8c2007-04-09 23:33:39 +00003649 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003650 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003651 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003652
Chris Lattner37caf8c2007-04-09 23:33:39 +00003653 switch (AM.Scale) {
3654 case 0: // no scale reg, must be "r+i" or "r", or "i".
3655 break;
3656 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00003657 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00003658 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003659 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00003660 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00003661 // ARM doesn't support any R+R*scale+imm addr modes.
3662 if (AM.BaseOffs)
3663 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003664
Bob Wilson2c7dab12009-04-08 17:55:28 +00003665 if (!VT.isSimple())
3666 return false;
3667
Evan Chenge6c835f2009-08-14 20:09:37 +00003668 if (Subtarget->isThumb2())
3669 return isLegalT2ScaledAddressingMode(AM, VT);
3670
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003671 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00003672 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00003673 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00003674 case MVT::i1:
3675 case MVT::i8:
3676 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003677 if (Scale < 0) Scale = -Scale;
3678 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003679 return true;
3680 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00003681 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003682 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00003683 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003684 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00003685 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00003686 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00003687 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00003688
Owen Anderson825b72b2009-08-11 20:47:22 +00003689 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00003690 // Note, we allow "void" uses (basically, uses that aren't loads or
3691 // stores), because arm allows folding a scale into many arithmetic
3692 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00003693
Chris Lattner37caf8c2007-04-09 23:33:39 +00003694 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00003695 if (Scale & 1) return false;
3696 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00003697 }
3698 break;
Evan Chengb01fad62007-03-12 23:30:29 +00003699 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00003700 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00003701}
3702
Evan Cheng77e47512009-11-11 19:05:52 +00003703/// isLegalICmpImmediate - Return true if the specified immediate is legal
3704/// icmp immediate, that is the target has icmp instructions which can compare
3705/// a register against the immediate without having to materialize the
3706/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00003707bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00003708 if (!Subtarget->isThumb())
3709 return ARM_AM::getSOImmVal(Imm) != -1;
3710 if (Subtarget->isThumb2())
3711 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00003712 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00003713}
3714
Owen Andersone50ed302009-08-10 22:56:29 +00003715static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003716 bool isSEXTLoad, SDValue &Base,
3717 SDValue &Offset, bool &isInc,
3718 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00003719 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3720 return false;
3721
Owen Anderson825b72b2009-08-11 20:47:22 +00003722 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00003723 // AddressingMode 3
3724 Base = Ptr->getOperand(0);
3725 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003726 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003727 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003728 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003729 isInc = false;
3730 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3731 return true;
3732 }
3733 }
3734 isInc = (Ptr->getOpcode() == ISD::ADD);
3735 Offset = Ptr->getOperand(1);
3736 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00003737 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00003738 // AddressingMode 2
3739 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003740 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00003741 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003742 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00003743 isInc = false;
3744 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3745 Base = Ptr->getOperand(0);
3746 return true;
3747 }
3748 }
3749
3750 if (Ptr->getOpcode() == ISD::ADD) {
3751 isInc = true;
3752 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
3753 if (ShOpcVal != ARM_AM::no_shift) {
3754 Base = Ptr->getOperand(1);
3755 Offset = Ptr->getOperand(0);
3756 } else {
3757 Base = Ptr->getOperand(0);
3758 Offset = Ptr->getOperand(1);
3759 }
3760 return true;
3761 }
3762
3763 isInc = (Ptr->getOpcode() == ISD::ADD);
3764 Base = Ptr->getOperand(0);
3765 Offset = Ptr->getOperand(1);
3766 return true;
3767 }
3768
Jim Grosbache5165492009-11-09 00:11:35 +00003769 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00003770 return false;
3771}
3772
Owen Andersone50ed302009-08-10 22:56:29 +00003773static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00003774 bool isSEXTLoad, SDValue &Base,
3775 SDValue &Offset, bool &isInc,
3776 SelectionDAG &DAG) {
3777 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3778 return false;
3779
3780 Base = Ptr->getOperand(0);
3781 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
3782 int RHSC = (int)RHS->getZExtValue();
3783 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
3784 assert(Ptr->getOpcode() == ISD::ADD);
3785 isInc = false;
3786 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3787 return true;
3788 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
3789 isInc = Ptr->getOpcode() == ISD::ADD;
3790 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
3791 return true;
3792 }
3793 }
3794
3795 return false;
3796}
3797
Evan Chenga8e29892007-01-19 07:51:42 +00003798/// getPreIndexedAddressParts - returns true by value, base pointer and
3799/// offset pointer and addressing mode by reference if the node's address
3800/// can be legally represented as pre-indexed load / store address.
3801bool
Dan Gohman475871a2008-07-27 21:46:04 +00003802ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
3803 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003804 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003805 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003806 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003807 return false;
3808
Owen Andersone50ed302009-08-10 22:56:29 +00003809 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003810 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003811 bool isSEXTLoad = false;
3812 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
3813 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003814 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003815 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3816 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
3817 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003818 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003819 } else
3820 return false;
3821
3822 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003823 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003824 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003825 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
3826 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003827 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003828 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00003829 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00003830 if (!isLegal)
3831 return false;
3832
3833 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
3834 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003835}
3836
3837/// getPostIndexedAddressParts - returns true by value, base pointer and
3838/// offset pointer and addressing mode by reference if this node can be
3839/// combined with a load / store to form a post-indexed load / store.
3840bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00003841 SDValue &Base,
3842 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003843 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00003844 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00003845 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00003846 return false;
3847
Owen Andersone50ed302009-08-10 22:56:29 +00003848 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00003849 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00003850 bool isSEXTLoad = false;
3851 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003852 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003853 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3854 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003855 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00003856 } else
3857 return false;
3858
3859 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00003860 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00003861 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00003862 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00003863 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00003864 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00003865 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
3866 isInc, DAG);
3867 if (!isLegal)
3868 return false;
3869
3870 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
3871 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00003872}
3873
Dan Gohman475871a2008-07-27 21:46:04 +00003874void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00003875 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00003876 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003877 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00003878 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00003879 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003880 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003881 switch (Op.getOpcode()) {
3882 default: break;
3883 case ARMISD::CMOV: {
3884 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00003885 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003886 if (KnownZero == 0 && KnownOne == 0) return;
3887
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003888 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00003889 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
3890 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00003891 KnownZero &= KnownZeroRHS;
3892 KnownOne &= KnownOneRHS;
3893 return;
3894 }
3895 }
3896}
3897
3898//===----------------------------------------------------------------------===//
3899// ARM Inline Assembly Support
3900//===----------------------------------------------------------------------===//
3901
3902/// getConstraintType - Given a constraint letter, return the type of
3903/// constraint it is for this target.
3904ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00003905ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
3906 if (Constraint.size() == 1) {
3907 switch (Constraint[0]) {
3908 default: break;
3909 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003910 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00003911 }
Evan Chenga8e29892007-01-19 07:51:42 +00003912 }
Chris Lattner4234f572007-03-25 02:14:49 +00003913 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00003914}
3915
Bob Wilson2dc4f542009-03-20 22:42:55 +00003916std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00003917ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003918 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003919 if (Constraint.size() == 1) {
3920 // GCC RS6000 Constraint Letters
3921 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003922 case 'l':
David Goodwinf1daf7d2009-07-08 23:10:31 +00003923 if (Subtarget->isThumb1Only())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003924 return std::make_pair(0U, ARM::tGPRRegisterClass);
3925 else
3926 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003927 case 'r':
3928 return std::make_pair(0U, ARM::GPRRegisterClass);
3929 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003930 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003931 return std::make_pair(0U, ARM::SPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00003932 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003933 return std::make_pair(0U, ARM::DPRRegisterClass);
3934 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003935 }
3936 }
3937 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3938}
3939
3940std::vector<unsigned> ARMTargetLowering::
3941getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003942 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003943 if (Constraint.size() != 1)
3944 return std::vector<unsigned>();
3945
3946 switch (Constraint[0]) { // GCC ARM Constraint Letters
3947 default: break;
3948 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00003949 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3950 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3951 0);
Evan Chenga8e29892007-01-19 07:51:42 +00003952 case 'r':
3953 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3954 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3955 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
3956 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003957 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00003958 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003959 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
3960 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
3961 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
3962 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
3963 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
3964 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
3965 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
3966 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00003967 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00003968 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
3969 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
3970 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
3971 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
3972 break;
Evan Chenga8e29892007-01-19 07:51:42 +00003973 }
3974
3975 return std::vector<unsigned>();
3976}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00003977
3978/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3979/// vector. If it is invalid, don't add anything to Ops.
3980void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3981 char Constraint,
3982 bool hasMemory,
3983 std::vector<SDValue>&Ops,
3984 SelectionDAG &DAG) const {
3985 SDValue Result(0, 0);
3986
3987 switch (Constraint) {
3988 default: break;
3989 case 'I': case 'J': case 'K': case 'L':
3990 case 'M': case 'N': case 'O':
3991 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
3992 if (!C)
3993 return;
3994
3995 int64_t CVal64 = C->getSExtValue();
3996 int CVal = (int) CVal64;
3997 // None of these constraints allow values larger than 32 bits. Check
3998 // that the value fits in an int.
3999 if (CVal != CVal64)
4000 return;
4001
4002 switch (Constraint) {
4003 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004004 if (Subtarget->isThumb1Only()) {
4005 // This must be a constant between 0 and 255, for ADD
4006 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004007 if (CVal >= 0 && CVal <= 255)
4008 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004009 } else if (Subtarget->isThumb2()) {
4010 // A constant that can be used as an immediate value in a
4011 // data-processing instruction.
4012 if (ARM_AM::getT2SOImmVal(CVal) != -1)
4013 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004014 } else {
4015 // A constant that can be used as an immediate value in a
4016 // data-processing instruction.
4017 if (ARM_AM::getSOImmVal(CVal) != -1)
4018 break;
4019 }
4020 return;
4021
4022 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004023 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004024 // This must be a constant between -255 and -1, for negated ADD
4025 // immediates. This can be used in GCC with an "n" modifier that
4026 // prints the negated value, for use with SUB instructions. It is
4027 // not useful otherwise but is implemented for compatibility.
4028 if (CVal >= -255 && CVal <= -1)
4029 break;
4030 } else {
4031 // This must be a constant between -4095 and 4095. It is not clear
4032 // what this constraint is intended for. Implemented for
4033 // compatibility with GCC.
4034 if (CVal >= -4095 && CVal <= 4095)
4035 break;
4036 }
4037 return;
4038
4039 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004040 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004041 // A 32-bit value where only one byte has a nonzero value. Exclude
4042 // zero to match GCC. This constraint is used by GCC internally for
4043 // constants that can be loaded with a move/shift combination.
4044 // It is not useful otherwise but is implemented for compatibility.
4045 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
4046 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004047 } else if (Subtarget->isThumb2()) {
4048 // A constant whose bitwise inverse can be used as an immediate
4049 // value in a data-processing instruction. This can be used in GCC
4050 // with a "B" modifier that prints the inverted value, for use with
4051 // BIC and MVN instructions. It is not useful otherwise but is
4052 // implemented for compatibility.
4053 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
4054 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004055 } else {
4056 // A constant whose bitwise inverse can be used as an immediate
4057 // value in a data-processing instruction. This can be used in GCC
4058 // with a "B" modifier that prints the inverted value, for use with
4059 // BIC and MVN instructions. It is not useful otherwise but is
4060 // implemented for compatibility.
4061 if (ARM_AM::getSOImmVal(~CVal) != -1)
4062 break;
4063 }
4064 return;
4065
4066 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004067 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004068 // This must be a constant between -7 and 7,
4069 // for 3-operand ADD/SUB immediate instructions.
4070 if (CVal >= -7 && CVal < 7)
4071 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00004072 } else if (Subtarget->isThumb2()) {
4073 // A constant whose negation can be used as an immediate value in a
4074 // data-processing instruction. This can be used in GCC with an "n"
4075 // modifier that prints the negated value, for use with SUB
4076 // instructions. It is not useful otherwise but is implemented for
4077 // compatibility.
4078 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
4079 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004080 } else {
4081 // A constant whose negation can be used as an immediate value in a
4082 // data-processing instruction. This can be used in GCC with an "n"
4083 // modifier that prints the negated value, for use with SUB
4084 // instructions. It is not useful otherwise but is implemented for
4085 // compatibility.
4086 if (ARM_AM::getSOImmVal(-CVal) != -1)
4087 break;
4088 }
4089 return;
4090
4091 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004092 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004093 // This must be a multiple of 4 between 0 and 1020, for
4094 // ADD sp + immediate.
4095 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
4096 break;
4097 } else {
4098 // A power of two or a constant between 0 and 32. This is used in
4099 // GCC for the shift amount on shifted register operands, but it is
4100 // useful in general for any shift amounts.
4101 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
4102 break;
4103 }
4104 return;
4105
4106 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004107 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004108 // This must be a constant between 0 and 31, for shift amounts.
4109 if (CVal >= 0 && CVal <= 31)
4110 break;
4111 }
4112 return;
4113
4114 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00004115 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00004116 // This must be a multiple of 4 between -508 and 508, for
4117 // ADD/SUB sp = sp + immediate.
4118 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
4119 break;
4120 }
4121 return;
4122 }
4123 Result = DAG.getTargetConstant(CVal, Op.getValueType());
4124 break;
4125 }
4126
4127 if (Result.getNode()) {
4128 Ops.push_back(Result);
4129 return;
4130 }
4131 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
4132 Ops, DAG);
4133}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00004134
4135bool
4136ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4137 // The ARM target isn't yet aware of offsets.
4138 return false;
4139}
Evan Cheng39382422009-10-28 01:44:26 +00004140
4141int ARM::getVFPf32Imm(const APFloat &FPImm) {
4142 APInt Imm = FPImm.bitcastToAPInt();
4143 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
4144 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
4145 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
4146
4147 // We can handle 4 bits of mantissa.
4148 // mantissa = (16+UInt(e:f:g:h))/16.
4149 if (Mantissa & 0x7ffff)
4150 return -1;
4151 Mantissa >>= 19;
4152 if ((Mantissa & 0xf) != Mantissa)
4153 return -1;
4154
4155 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4156 if (Exp < -3 || Exp > 4)
4157 return -1;
4158 Exp = ((Exp+3) & 0x7) ^ 4;
4159
4160 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4161}
4162
4163int ARM::getVFPf64Imm(const APFloat &FPImm) {
4164 APInt Imm = FPImm.bitcastToAPInt();
4165 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
4166 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
4167 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
4168
4169 // We can handle 4 bits of mantissa.
4170 // mantissa = (16+UInt(e:f:g:h))/16.
4171 if (Mantissa & 0xffffffffffffLL)
4172 return -1;
4173 Mantissa >>= 48;
4174 if ((Mantissa & 0xf) != Mantissa)
4175 return -1;
4176
4177 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4178 if (Exp < -3 || Exp > 4)
4179 return -1;
4180 Exp = ((Exp+3) & 0x7) ^ 4;
4181
4182 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4183}
4184
4185/// isFPImmLegal - Returns true if the target can instruction select the
4186/// specified FP immediate natively. If false, the legalizer will
4187/// materialize the FP immediate as a load from a constant pool.
4188bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
4189 if (!Subtarget->hasVFP3())
4190 return false;
4191 if (VT == MVT::f32)
4192 return ARM::getVFPf32Imm(Imm) != -1;
4193 if (VT == MVT::f64)
4194 return ARM::getVFPf64Imm(Imm) != -1;
4195 return false;
4196}